1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 /*
40 * Socket operations for use by nfs
41 */
42
43 #include "opt_kgssapi.h"
44 #include "opt_nfs.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
55 #include <sys/proc.h>
56 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
61
62 #include <rpc/rpc.h>
63 #include <rpc/krpc.h>
64
65 #include <kgssapi/krb5/kcrypto.h>
66
67 #include <fs/nfs/nfsport.h>
68
69 #ifdef KDTRACE_HOOKS
70 #include <sys/dtrace_bsd.h>
71
72 dtrace_nfsclient_nfs23_start_probe_func_t
73 dtrace_nfscl_nfs234_start_probe;
74
75 dtrace_nfsclient_nfs23_done_probe_func_t
76 dtrace_nfscl_nfs234_done_probe;
77
78 /*
79 * Registered probes by RPC type.
80 */
81 uint32_t nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
82 uint32_t nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
83
84 uint32_t nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
85 uint32_t nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
86
87 uint32_t nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
88 uint32_t nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
89 #endif
90
91 NFSSTATESPINLOCK;
92 NFSREQSPINLOCK;
93 NFSDLOCKMUTEX;
94 NFSCLSTATEMUTEX;
95 extern struct nfsstatsv1 nfsstatsv1;
96 extern struct nfsreqhead nfsd_reqq;
97 extern int nfscl_ticks;
98 extern void (*ncl_call_invalcaches)(struct vnode *);
99 extern int nfs_numnfscbd;
100 extern int nfscl_debuglevel;
101 extern int nfsrv_lease;
102
103 SVCPOOL *nfscbd_pool;
104 static int nfsrv_gsscallbackson = 0;
105 static int nfs_bufpackets = 4;
106 static int nfs_reconnects;
107 static int nfs3_jukebox_delay = 10;
108 static int nfs_skip_wcc_data_onerr = 1;
109 static int nfs_dsretries = 2;
110 static struct timespec nfs_trylater_max = {
111 .tv_sec = NFS_TRYLATERDEL,
112 .tv_nsec = 0,
113 };
114
115 SYSCTL_DECL(_vfs_nfs);
116
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
118 "Buffer reservation size 2 < x < 64");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
120 "Number of times the nfs client has had to reconnect");
121 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
122 "Number of seconds to delay a retry after receiving EJUKEBOX");
123 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
124 "Disable weak cache consistency checking when server returns an error");
125 SYSCTL_INT(_vfs_nfs, OID_AUTO, dsretries, CTLFLAG_RW, &nfs_dsretries, 0,
126 "Number of retries for a DS RPC before failure");
127
128 static void nfs_down(struct nfsmount *, struct thread *, const char *,
129 int, int);
130 static void nfs_up(struct nfsmount *, struct thread *, const char *,
131 int, int);
132 static int nfs_msg(struct thread *, const char *, const char *, int);
133
134 struct nfs_cached_auth {
135 int ca_refs; /* refcount, including 1 from the cache */
136 uid_t ca_uid; /* uid that corresponds to this auth */
137 AUTH *ca_auth; /* RPC auth handle */
138 };
139
140 static int nfsv2_procid[NFS_V3NPROCS] = {
141 NFSV2PROC_NULL,
142 NFSV2PROC_GETATTR,
143 NFSV2PROC_SETATTR,
144 NFSV2PROC_LOOKUP,
145 NFSV2PROC_NOOP,
146 NFSV2PROC_READLINK,
147 NFSV2PROC_READ,
148 NFSV2PROC_WRITE,
149 NFSV2PROC_CREATE,
150 NFSV2PROC_MKDIR,
151 NFSV2PROC_SYMLINK,
152 NFSV2PROC_CREATE,
153 NFSV2PROC_REMOVE,
154 NFSV2PROC_RMDIR,
155 NFSV2PROC_RENAME,
156 NFSV2PROC_LINK,
157 NFSV2PROC_READDIR,
158 NFSV2PROC_NOOP,
159 NFSV2PROC_STATFS,
160 NFSV2PROC_NOOP,
161 NFSV2PROC_NOOP,
162 NFSV2PROC_NOOP,
163 };
164
165 /*
166 * Initialize sockets and congestion for a new NFS connection.
167 * We do not free the sockaddr if error.
168 * Which arguments are set to NULL indicate what kind of call it is.
169 * cred == NULL --> a call to connect to a pNFS DS
170 * nmp == NULL --> indicates an upcall to userland or a NFSv4.0 callback
171 */
172 int
173 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
174 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
175 {
176 int rcvreserve, sndreserve;
177 int pktscale, pktscalesav;
178 struct sockaddr *saddr;
179 struct ucred *origcred;
180 CLIENT *client;
181 struct netconfig *nconf;
182 struct socket *so;
183 int one = 1, retries, error = 0;
184 struct thread *td = curthread;
185 SVCXPRT *xprt;
186 struct timeval timo;
187
188 /*
189 * We need to establish the socket using the credentials of
190 * the mountpoint. Some parts of this process (such as
191 * sobind() and soconnect()) will use the curent thread's
192 * credential instead of the socket credential. To work
193 * around this, temporarily change the current thread's
194 * credential to that of the mountpoint.
195 *
196 * XXX: It would be better to explicitly pass the correct
197 * credential to sobind() and soconnect().
198 */
199 origcred = td->td_ucred;
200
201 /*
202 * Use the credential in nr_cred, if not NULL.
203 */
204 if (nrp->nr_cred != NULL)
205 td->td_ucred = nrp->nr_cred;
206 else
207 td->td_ucred = cred;
208 saddr = nrp->nr_nam;
209
210 if (saddr->sa_family == AF_INET)
211 if (nrp->nr_sotype == SOCK_DGRAM)
212 nconf = getnetconfigent("udp");
213 else
214 nconf = getnetconfigent("tcp");
215 else if (saddr->sa_family == AF_LOCAL)
216 nconf = getnetconfigent("local");
217 else
218 if (nrp->nr_sotype == SOCK_DGRAM)
219 nconf = getnetconfigent("udp6");
220 else
221 nconf = getnetconfigent("tcp6");
222
223 pktscale = nfs_bufpackets;
224 if (pktscale < 2)
225 pktscale = 2;
226 if (pktscale > 64)
227 pktscale = 64;
228 pktscalesav = pktscale;
229 /*
230 * soreserve() can fail if sb_max is too small, so shrink pktscale
231 * and try again if there is an error.
232 * Print a log message suggesting increasing sb_max.
233 * Creating a socket and doing this is necessary since, if the
234 * reservation sizes are too large and will make soreserve() fail,
235 * the connection will work until a large send is attempted and
236 * then it will loop in the krpc code.
237 */
238 so = NULL;
239 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
240 error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
241 nrp->nr_soproto, td->td_ucred, td);
242 if (error != 0)
243 goto out;
244 do {
245 if (error != 0 && pktscale > 2) {
246 if (nmp != NULL && nrp->nr_sotype == SOCK_STREAM &&
247 pktscale == pktscalesav)
248 printf("Consider increasing kern.ipc.maxsockbuf\n");
249 pktscale--;
250 }
251 if (nrp->nr_sotype == SOCK_DGRAM) {
252 if (nmp != NULL) {
253 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
254 pktscale;
255 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
256 pktscale;
257 } else {
258 sndreserve = rcvreserve = 1024 * pktscale;
259 }
260 } else {
261 if (nrp->nr_sotype != SOCK_STREAM)
262 panic("nfscon sotype");
263 if (nmp != NULL) {
264 sndreserve = (NFS_MAXBSIZE + NFS_MAXXDR +
265 sizeof (u_int32_t)) * pktscale;
266 rcvreserve = (NFS_MAXBSIZE + NFS_MAXXDR +
267 sizeof (u_int32_t)) * pktscale;
268 } else {
269 sndreserve = rcvreserve = 1024 * pktscale;
270 }
271 }
272 error = soreserve(so, sndreserve, rcvreserve);
273 if (error != 0 && nmp != NULL && nrp->nr_sotype == SOCK_STREAM &&
274 pktscale <= 2)
275 printf("Must increase kern.ipc.maxsockbuf or reduce"
276 " rsize, wsize\n");
277 } while (error != 0 && pktscale > 2);
278 soclose(so);
279 if (error != 0)
280 goto out;
281
282 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
283 nrp->nr_vers, sndreserve, rcvreserve);
284 CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq");
285 if (nmp != NULL) {
286 if ((nmp->nm_flag & NFSMNT_INT))
287 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
288 if ((nmp->nm_flag & NFSMNT_RESVPORT))
289 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
290 if (NFSHASSOFT(nmp)) {
291 if (nmp->nm_sotype == SOCK_DGRAM)
292 /*
293 * For UDP, the large timeout for a reconnect
294 * will be set to "nm_retry * nm_timeo / 2", so
295 * we only want to do 2 reconnect timeout
296 * retries.
297 */
298 retries = 2;
299 else
300 retries = nmp->nm_retry;
301 } else
302 retries = INT_MAX;
303 if (NFSHASNFSV4N(nmp)) {
304 if (cred != NULL) {
305 if (NFSHASSOFT(nmp)) {
306 /*
307 * This should be a DS mount.
308 * Use CLSET_TIMEOUT to set the timeout
309 * for connections to DSs instead of
310 * specifying a timeout on each RPC.
311 * This is done so that SO_SNDTIMEO
312 * is set on the TCP socket as well
313 * as specifying a time limit when
314 * waiting for an RPC reply. Useful
315 * if the send queue for the TCP
316 * connection has become constipated,
317 * due to a failed DS.
318 * The choice of lease_duration / 4 is
319 * fairly arbitrary, but seems to work
320 * ok, with a lower bound of 10sec.
321 */
322 timo.tv_sec = nfsrv_lease / 4;
323 if (timo.tv_sec < 10)
324 timo.tv_sec = 10;
325 timo.tv_usec = 0;
326 CLNT_CONTROL(client, CLSET_TIMEOUT,
327 &timo);
328 }
329 /*
330 * Make sure the nfscbd_pool doesn't get
331 * destroyed while doing this.
332 */
333 NFSD_LOCK();
334 if (nfs_numnfscbd > 0) {
335 nfs_numnfscbd++;
336 NFSD_UNLOCK();
337 xprt = svc_vc_create_backchannel(
338 nfscbd_pool);
339 CLNT_CONTROL(client, CLSET_BACKCHANNEL,
340 xprt);
341 NFSD_LOCK();
342 nfs_numnfscbd--;
343 if (nfs_numnfscbd == 0)
344 wakeup(&nfs_numnfscbd);
345 }
346 NFSD_UNLOCK();
347 } else {
348 /*
349 * cred == NULL for a DS connect.
350 * For connects to a DS, set a retry limit
351 * so that failed DSs will be detected.
352 * This is ok for NFSv4.1, since a DS does
353 * not maintain open/lock state and is the
354 * only case where using a "soft" mount is
355 * recommended for NFSv4.
356 * For mounts from the MDS to DS, this is done
357 * via mount options, but that is not the case
358 * here. The retry limit here can be adjusted
359 * via the sysctl vfs.nfs.dsretries.
360 * See the comment above w.r.t. timeout.
361 */
362 timo.tv_sec = nfsrv_lease / 4;
363 if (timo.tv_sec < 10)
364 timo.tv_sec = 10;
365 timo.tv_usec = 0;
366 CLNT_CONTROL(client, CLSET_TIMEOUT, &timo);
367 retries = nfs_dsretries;
368 }
369 }
370 } else {
371 /*
372 * Three cases:
373 * - Null RPC callback to client
374 * - Non-Null RPC callback to client, wait a little longer
375 * - upcalls to nfsuserd and gssd (clp == NULL)
376 */
377 if (callback_retry_mult == 0) {
378 retries = NFSV4_UPCALLRETRY;
379 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
380 } else {
381 retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
382 }
383 }
384 CLNT_CONTROL(client, CLSET_RETRIES, &retries);
385
386 if (nmp != NULL) {
387 /*
388 * For UDP, there are 2 timeouts:
389 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
390 * that does a retransmit of an RPC request using the same
391 * socket and xid. This is what you normally want to do,
392 * since NFS servers depend on "same xid" for their
393 * Duplicate Request Cache.
394 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
395 * retransmits on the same socket should fail and a fresh
396 * socket created. Each of these timeouts counts as one
397 * CLSET_RETRIES as set above.
398 * Set the initial retransmit timeout for UDP. This timeout
399 * doesn't exist for TCP and the following call just fails,
400 * which is ok.
401 */
402 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
403 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
404 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
405 }
406
407 mtx_lock(&nrp->nr_mtx);
408 if (nrp->nr_client != NULL) {
409 mtx_unlock(&nrp->nr_mtx);
410 /*
411 * Someone else already connected.
412 */
413 CLNT_RELEASE(client);
414 } else {
415 nrp->nr_client = client;
416 /*
417 * Protocols that do not require connections may be optionally
418 * left unconnected for servers that reply from a port other
419 * than NFS_PORT.
420 */
421 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
422 mtx_unlock(&nrp->nr_mtx);
423 CLNT_CONTROL(client, CLSET_CONNECT, &one);
424 } else
425 mtx_unlock(&nrp->nr_mtx);
426 }
427
428 out:
429 /* Restore current thread's credentials. */
430 td->td_ucred = origcred;
431
432 NFSEXITCODE(error);
433 return (error);
434 }
435
436 /*
437 * NFS disconnect. Clean up and unlink.
438 */
439 void
440 newnfs_disconnect(struct nfssockreq *nrp)
441 {
442 CLIENT *client;
443
444 mtx_lock(&nrp->nr_mtx);
445 if (nrp->nr_client != NULL) {
446 client = nrp->nr_client;
447 nrp->nr_client = NULL;
448 mtx_unlock(&nrp->nr_mtx);
449 rpc_gss_secpurge_call(client);
450 CLNT_CLOSE(client);
451 CLNT_RELEASE(client);
452 } else {
453 mtx_unlock(&nrp->nr_mtx);
454 }
455 }
456
457 static AUTH *
458 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
459 char *srv_principal, gss_OID mech_oid, struct ucred *cred)
460 {
461 rpc_gss_service_t svc;
462 AUTH *auth;
463
464 switch (secflavour) {
465 case RPCSEC_GSS_KRB5:
466 case RPCSEC_GSS_KRB5I:
467 case RPCSEC_GSS_KRB5P:
468 if (!mech_oid) {
469 if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
470 return (NULL);
471 }
472 if (secflavour == RPCSEC_GSS_KRB5)
473 svc = rpc_gss_svc_none;
474 else if (secflavour == RPCSEC_GSS_KRB5I)
475 svc = rpc_gss_svc_integrity;
476 else
477 svc = rpc_gss_svc_privacy;
478
479 if (clnt_principal == NULL)
480 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
481 srv_principal, mech_oid, svc);
482 else {
483 auth = rpc_gss_seccreate_call(nrp->nr_client, cred,
484 clnt_principal, srv_principal, "kerberosv5",
485 svc, NULL, NULL, NULL);
486 return (auth);
487 }
488 if (auth != NULL)
489 return (auth);
490 /* fallthrough */
491 case AUTH_SYS:
492 default:
493 return (authunix_create(cred));
494
495 }
496 }
497
498 /*
499 * Callback from the RPC code to generate up/down notifications.
500 */
501
502 struct nfs_feedback_arg {
503 struct nfsmount *nf_mount;
504 int nf_lastmsg; /* last tprintf */
505 int nf_tprintfmsg;
506 struct thread *nf_td;
507 };
508
509 static void
510 nfs_feedback(int type, int proc, void *arg)
511 {
512 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
513 struct nfsmount *nmp = nf->nf_mount;
514 time_t now;
515
516 switch (type) {
517 case FEEDBACK_REXMIT2:
518 case FEEDBACK_RECONNECT:
519 now = NFSD_MONOSEC;
520 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
521 nfs_down(nmp, nf->nf_td,
522 "not responding", 0, NFSSTA_TIMEO);
523 nf->nf_tprintfmsg = TRUE;
524 nf->nf_lastmsg = now;
525 }
526 break;
527
528 case FEEDBACK_OK:
529 nfs_up(nf->nf_mount, nf->nf_td,
530 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
531 break;
532 }
533 }
534
535 /*
536 * newnfs_request - goes something like this
537 * - does the rpc by calling the krpc layer
538 * - break down rpc header and return with nfs reply
539 * nb: always frees up nd_mreq mbuf list
540 */
541 int
542 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
543 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
544 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
545 u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *dssep)
546 {
547 uint32_t retseq, retval, slotseq, *tl;
548 int i = 0, j = 0, opcnt, set_sigset = 0, slot;
549 int error = 0, usegssname = 0, secflavour = AUTH_SYS;
550 int freeslot, maxslot, reterr, slotpos, timeo;
551 u_int16_t procnum;
552 struct nfs_feedback_arg nf;
553 struct timeval timo;
554 AUTH *auth;
555 struct rpc_callextra ext;
556 enum clnt_stat stat;
557 struct nfsreq *rep = NULL;
558 char *srv_principal = NULL, *clnt_principal = NULL;
559 sigset_t oldset;
560 struct ucred *authcred;
561 struct nfsclsession *sep;
562 uint8_t sessionid[NFSX_V4SESSIONID];
563 struct timespec trylater_delay, ts, waituntil;
564
565 /* Initially 1msec. */
566 trylater_delay.tv_sec = 0;
567 trylater_delay.tv_nsec = 1000000;
568 sep = dssep;
569 if (xidp != NULL)
570 *xidp = 0;
571 /* Reject requests while attempting a forced unmount. */
572 if (nmp != NULL && NFSCL_FORCEDISM(nmp->nm_mountp)) {
573 m_freem(nd->nd_mreq);
574 return (ESTALE);
575 }
576
577 /*
578 * Set authcred, which is used to acquire RPC credentials to
579 * the cred argument, by default. The crhold() should not be
580 * necessary, but will ensure that some future code change
581 * doesn't result in the credential being free'd prematurely.
582 */
583 authcred = crhold(cred);
584
585 /* For client side interruptible mounts, mask off the signals. */
586 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
587 newnfs_set_sigmask(td, &oldset);
588 set_sigset = 1;
589 }
590
591 /*
592 * XXX if not already connected call nfs_connect now. Longer
593 * term, change nfs_mount to call nfs_connect unconditionally
594 * and let clnt_reconnect_create handle reconnects.
595 */
596 if (nrp->nr_client == NULL)
597 newnfs_connect(nmp, nrp, cred, td, 0);
598
599 /*
600 * For a client side mount, nmp is != NULL and clp == NULL. For
601 * server calls (callbacks or upcalls), nmp == NULL.
602 */
603 if (clp != NULL) {
604 NFSLOCKSTATE();
605 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
606 secflavour = RPCSEC_GSS_KRB5;
607 if (nd->nd_procnum != NFSPROC_NULL) {
608 if (clp->lc_flags & LCL_GSSINTEGRITY)
609 secflavour = RPCSEC_GSS_KRB5I;
610 else if (clp->lc_flags & LCL_GSSPRIVACY)
611 secflavour = RPCSEC_GSS_KRB5P;
612 }
613 }
614 NFSUNLOCKSTATE();
615 } else if (nmp != NULL && NFSHASKERB(nmp) &&
616 nd->nd_procnum != NFSPROC_NULL) {
617 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
618 nd->nd_flag |= ND_USEGSSNAME;
619 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
620 /*
621 * If there is a client side host based credential,
622 * use that, otherwise use the system uid, if set.
623 * The system uid is in the nmp->nm_sockreq.nr_cred
624 * credentials.
625 */
626 if (nmp->nm_krbnamelen > 0) {
627 usegssname = 1;
628 clnt_principal = nmp->nm_krbname;
629 } else if (nmp->nm_uid != (uid_t)-1) {
630 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
631 ("newnfs_request: NULL nr_cred"));
632 crfree(authcred);
633 authcred = crhold(nmp->nm_sockreq.nr_cred);
634 }
635 } else if (nmp->nm_krbnamelen == 0 &&
636 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
637 /*
638 * If there is no host based principal name and
639 * the system uid is set and this is root, use the
640 * system uid, since root won't have user
641 * credentials in a credentials cache file.
642 * The system uid is in the nmp->nm_sockreq.nr_cred
643 * credentials.
644 */
645 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
646 ("newnfs_request: NULL nr_cred"));
647 crfree(authcred);
648 authcred = crhold(nmp->nm_sockreq.nr_cred);
649 }
650 if (NFSHASINTEGRITY(nmp))
651 secflavour = RPCSEC_GSS_KRB5I;
652 else if (NFSHASPRIVACY(nmp))
653 secflavour = RPCSEC_GSS_KRB5P;
654 else
655 secflavour = RPCSEC_GSS_KRB5;
656 srv_principal = NFSMNT_SRVKRBNAME(nmp);
657 } else if (nmp != NULL && !NFSHASKERB(nmp) &&
658 nd->nd_procnum != NFSPROC_NULL &&
659 (nd->nd_flag & ND_USEGSSNAME) != 0) {
660 /*
661 * Use the uid that did the mount when the RPC is doing
662 * NFSv4 system operations, as indicated by the
663 * ND_USEGSSNAME flag, for the AUTH_SYS case.
664 * The credentials in nm_sockreq.nr_cred were used for the
665 * mount.
666 */
667 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
668 ("newnfs_request: NULL nr_cred"));
669 crfree(authcred);
670 authcred = crhold(nmp->nm_sockreq.nr_cred);
671 }
672
673 if (nmp != NULL) {
674 bzero(&nf, sizeof(struct nfs_feedback_arg));
675 nf.nf_mount = nmp;
676 nf.nf_td = td;
677 nf.nf_lastmsg = NFSD_MONOSEC -
678 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
679 }
680
681 if (nd->nd_procnum == NFSPROC_NULL)
682 auth = authnone_create();
683 else if (usegssname) {
684 /*
685 * For this case, the authenticator is held in the
686 * nfssockreq structure, so don't release the reference count
687 * held on it. --> Don't AUTH_DESTROY() it in this function.
688 */
689 if (nrp->nr_auth == NULL)
690 nrp->nr_auth = nfs_getauth(nrp, secflavour,
691 clnt_principal, srv_principal, NULL, authcred);
692 else
693 rpc_gss_refresh_auth_call(nrp->nr_auth);
694 auth = nrp->nr_auth;
695 } else
696 auth = nfs_getauth(nrp, secflavour, NULL,
697 srv_principal, NULL, authcred);
698 crfree(authcred);
699 if (auth == NULL) {
700 m_freem(nd->nd_mreq);
701 if (set_sigset)
702 newnfs_restore_sigmask(td, &oldset);
703 return (EACCES);
704 }
705 bzero(&ext, sizeof(ext));
706 ext.rc_auth = auth;
707 if (nmp != NULL) {
708 ext.rc_feedback = nfs_feedback;
709 ext.rc_feedback_arg = &nf;
710 }
711
712 procnum = nd->nd_procnum;
713 if ((nd->nd_flag & ND_NFSV4) &&
714 nd->nd_procnum != NFSPROC_NULL &&
715 nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
716 procnum = NFSV4PROC_COMPOUND;
717
718 if (nmp != NULL) {
719 NFSINCRGLOBAL(nfsstatsv1.rpcrequests);
720
721 /* Map the procnum to the old NFSv2 one, as required. */
722 if ((nd->nd_flag & ND_NFSV2) != 0) {
723 if (nd->nd_procnum < NFS_V3NPROCS)
724 procnum = nfsv2_procid[nd->nd_procnum];
725 else
726 procnum = NFSV2PROC_NOOP;
727 }
728
729 /*
730 * Now only used for the R_DONTRECOVER case, but until that is
731 * supported within the krpc code, I need to keep a queue of
732 * outstanding RPCs for nfsv4 client requests.
733 */
734 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
735 rep = malloc(sizeof(struct nfsreq),
736 M_NFSDREQ, M_WAITOK);
737 #ifdef KDTRACE_HOOKS
738 if (dtrace_nfscl_nfs234_start_probe != NULL) {
739 uint32_t probe_id;
740 int probe_procnum;
741
742 if (nd->nd_flag & ND_NFSV4) {
743 probe_id =
744 nfscl_nfs4_start_probes[nd->nd_procnum];
745 probe_procnum = nd->nd_procnum;
746 } else if (nd->nd_flag & ND_NFSV3) {
747 probe_id = nfscl_nfs3_start_probes[procnum];
748 probe_procnum = procnum;
749 } else {
750 probe_id =
751 nfscl_nfs2_start_probes[nd->nd_procnum];
752 probe_procnum = procnum;
753 }
754 if (probe_id != 0)
755 (dtrace_nfscl_nfs234_start_probe)
756 (probe_id, vp, nd->nd_mreq, cred,
757 probe_procnum);
758 }
759 #endif
760 }
761 freeslot = -1; /* Set to slot that needs to be free'd */
762 tryagain:
763 slot = -1; /* Slot that needs a sequence# increment. */
764 /*
765 * This timeout specifies when a new socket should be created,
766 * along with new xid values. For UDP, this should be done
767 * infrequently, since retransmits of RPC requests should normally
768 * use the same xid.
769 */
770 if (nmp == NULL) {
771 if (clp == NULL) {
772 timo.tv_sec = NFSV4_UPCALLTIMEO;
773 timo.tv_usec = 0;
774 } else {
775 timo.tv_sec = NFSV4_CALLBACKTIMEO / 1000;
776 timo.tv_usec = NFSV4_CALLBACKTIMEO * 1000;
777 }
778 } else {
779 if (nrp->nr_sotype != SOCK_DGRAM) {
780 timo.tv_usec = 0;
781 if ((nmp->nm_flag & NFSMNT_NFSV4))
782 timo.tv_sec = INT_MAX;
783 else
784 timo.tv_sec = NFS_TCPTIMEO;
785 } else {
786 if (NFSHASSOFT(nmp)) {
787 /*
788 * CLSET_RETRIES is set to 2, so this should be
789 * half of the total timeout required.
790 */
791 timeo = nmp->nm_retry * nmp->nm_timeo / 2;
792 if (timeo < 1)
793 timeo = 1;
794 timo.tv_sec = timeo / NFS_HZ;
795 timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
796 NFS_HZ;
797 } else {
798 /* For UDP hard mounts, use a large value. */
799 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
800 timo.tv_usec = 0;
801 }
802 }
803
804 if (rep != NULL) {
805 rep->r_flags = 0;
806 rep->r_nmp = nmp;
807 /*
808 * Chain request into list of outstanding requests.
809 */
810 NFSLOCKREQ();
811 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
812 NFSUNLOCKREQ();
813 }
814 }
815
816 nd->nd_mrep = NULL;
817 if (clp != NULL && sep != NULL)
818 stat = clnt_bck_call(nrp->nr_client, &ext, procnum,
819 nd->nd_mreq, &nd->nd_mrep, timo, sep->nfsess_xprt);
820 else
821 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum,
822 nd->nd_mreq, &nd->nd_mrep, timo);
823 NFSCL_DEBUG(2, "clnt call=%d\n", stat);
824
825 if (rep != NULL) {
826 /*
827 * RPC done, unlink the request.
828 */
829 NFSLOCKREQ();
830 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
831 NFSUNLOCKREQ();
832 }
833
834 /*
835 * If there was a successful reply and a tprintf msg.
836 * tprintf a response.
837 */
838 if (stat == RPC_SUCCESS) {
839 error = 0;
840 } else if (stat == RPC_TIMEDOUT) {
841 NFSINCRGLOBAL(nfsstatsv1.rpctimeouts);
842 error = ETIMEDOUT;
843 } else if (stat == RPC_VERSMISMATCH) {
844 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
845 error = EOPNOTSUPP;
846 } else if (stat == RPC_PROGVERSMISMATCH) {
847 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
848 error = EPROTONOSUPPORT;
849 } else if (stat == RPC_INTR) {
850 error = EINTR;
851 } else if (stat == RPC_CANTSEND || stat == RPC_CANTRECV ||
852 stat == RPC_SYSTEMERROR) {
853 /* Check for a session slot that needs to be free'd. */
854 if ((nd->nd_flag & (ND_NFSV41 | ND_HASSLOTID)) ==
855 (ND_NFSV41 | ND_HASSLOTID) && nmp != NULL &&
856 nd->nd_procnum != NFSPROC_NULL) {
857 /*
858 * This should only occur when either the MDS or
859 * a client has an RPC against a DS fail.
860 * This happens because these cases use "soft"
861 * connections that can time out and fail.
862 * The slot used for this RPC is now in a
863 * non-deterministic state, but if the slot isn't
864 * free'd, threads can get stuck waiting for a slot.
865 */
866 if (sep == NULL)
867 sep = nfsmnt_mdssession(nmp);
868 /*
869 * Bump the sequence# out of range, so that reuse of
870 * this slot will result in an NFSERR_SEQMISORDERED
871 * error and not a bogus cached RPC reply.
872 */
873 mtx_lock(&sep->nfsess_mtx);
874 sep->nfsess_slotseq[nd->nd_slotid] += 10;
875 mtx_unlock(&sep->nfsess_mtx);
876 /* And free the slot. */
877 nfsv4_freeslot(sep, nd->nd_slotid, false);
878 }
879 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
880 error = ENXIO;
881 } else {
882 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
883 error = EACCES;
884 }
885 if (error) {
886 m_freem(nd->nd_mreq);
887 if (usegssname == 0)
888 AUTH_DESTROY(auth);
889 if (rep != NULL)
890 free(rep, M_NFSDREQ);
891 if (set_sigset)
892 newnfs_restore_sigmask(td, &oldset);
893 return (error);
894 }
895
896 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
897
898 /*
899 * Search for any mbufs that are not a multiple of 4 bytes long
900 * or with m_data not longword aligned.
901 * These could cause pointer alignment problems, so copy them to
902 * well aligned mbufs.
903 */
904 newnfs_realign(&nd->nd_mrep, M_WAITOK);
905 nd->nd_md = nd->nd_mrep;
906 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
907 nd->nd_repstat = 0;
908 if (nd->nd_procnum != NFSPROC_NULL &&
909 nd->nd_procnum != NFSV4PROC_CBNULL) {
910 /* If sep == NULL, set it to the default in nmp. */
911 if (sep == NULL && nmp != NULL)
912 sep = nfsmnt_mdssession(nmp);
913 /*
914 * and now the actual NFS xdr.
915 */
916 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
917 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
918 if (nd->nd_repstat >= 10000)
919 NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
920 (int)nd->nd_repstat);
921
922 /*
923 * Get rid of the tag, return count and SEQUENCE result for
924 * NFSv4.
925 */
926 if ((nd->nd_flag & ND_NFSV4) != 0 && nd->nd_repstat !=
927 NFSERR_MINORVERMISMATCH) {
928 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
929 i = fxdr_unsigned(int, *tl);
930 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
931 if (error)
932 goto nfsmout;
933 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
934 opcnt = fxdr_unsigned(int, *tl++);
935 i = fxdr_unsigned(int, *tl++);
936 j = fxdr_unsigned(int, *tl);
937 if (j >= 10000)
938 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
939 /*
940 * If the first op is Sequence, free up the slot.
941 */
942 if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) ||
943 (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0))
944 NFSCL_DEBUG(1, "failed seq=%d\n", j);
945 if (((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) ||
946 (clp != NULL && i == NFSV4OP_CBSEQUENCE &&
947 j == 0)) && sep != NULL) {
948 if (i == NFSV4OP_SEQUENCE)
949 NFSM_DISSECT(tl, uint32_t *,
950 NFSX_V4SESSIONID +
951 5 * NFSX_UNSIGNED);
952 else
953 NFSM_DISSECT(tl, uint32_t *,
954 NFSX_V4SESSIONID +
955 4 * NFSX_UNSIGNED);
956 mtx_lock(&sep->nfsess_mtx);
957 if (bcmp(tl, sep->nfsess_sessionid,
958 NFSX_V4SESSIONID) == 0) {
959 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
960 retseq = fxdr_unsigned(uint32_t, *tl++);
961 slot = fxdr_unsigned(int, *tl++);
962 if ((nd->nd_flag & ND_HASSLOTID) != 0) {
963 if (slot != nd->nd_slotid) {
964 printf("newnfs_request:"
965 " Wrong session "
966 "slot=%d\n", slot);
967 slot = nd->nd_slotid;
968 }
969 freeslot = slot;
970 } else if (slot != 0) {
971 printf("newnfs_request: Bad "
972 "session slot=%d\n", slot);
973 slot = 0;
974 }
975 if (retseq != sep->nfsess_slotseq[slot])
976 printf("retseq diff 0x%x\n",
977 retseq);
978 retval = fxdr_unsigned(uint32_t, *++tl);
979 if ((retval + 1) < sep->nfsess_foreslots
980 )
981 sep->nfsess_foreslots = (retval
982 + 1);
983 else if ((retval + 1) >
984 sep->nfsess_foreslots)
985 sep->nfsess_foreslots = (retval
986 < 64) ? (retval + 1) : 64;
987 }
988 mtx_unlock(&sep->nfsess_mtx);
989
990 /* Grab the op and status for the next one. */
991 if (opcnt > 1) {
992 NFSM_DISSECT(tl, uint32_t *,
993 2 * NFSX_UNSIGNED);
994 i = fxdr_unsigned(int, *tl++);
995 j = fxdr_unsigned(int, *tl);
996 }
997 }
998 }
999 if (nd->nd_repstat != 0) {
1000 if (nd->nd_repstat == NFSERR_BADSESSION &&
1001 nmp != NULL && dssep == NULL &&
1002 (nd->nd_flag & ND_NFSV41) != 0) {
1003 /*
1004 * If this is a client side MDS RPC, mark
1005 * the MDS session defunct and initiate
1006 * recovery, as required.
1007 * The nfsess_defunct field is protected by
1008 * the NFSLOCKMNT()/nm_mtx lock and not the
1009 * nfsess_mtx lock to simplify its handling,
1010 * for the MDS session. This lock is also
1011 * sufficient for nfsess_sessionid, since it
1012 * never changes in the structure.
1013 */
1014 NFSCL_DEBUG(1, "Got badsession\n");
1015 NFSLOCKCLSTATE();
1016 NFSLOCKMNT(nmp);
1017 sep = NFSMNT_MDSSESSION(nmp);
1018 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence,
1019 NFSX_V4SESSIONID) == 0) {
1020 printf("Initiate recovery. If server "
1021 "has not rebooted, "
1022 "check NFS clients for unique "
1023 "/etc/hostid's\n");
1024 /* Initiate recovery. */
1025 sep->nfsess_defunct = 1;
1026 NFSCL_DEBUG(1, "Marked defunct\n");
1027 if (nmp->nm_clp != NULL) {
1028 nmp->nm_clp->nfsc_flags |=
1029 NFSCLFLAGS_RECOVER;
1030 wakeup(nmp->nm_clp);
1031 }
1032 }
1033 NFSUNLOCKCLSTATE();
1034 /*
1035 * Sleep for up to 1sec waiting for a new
1036 * session.
1037 */
1038 mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO,
1039 "nfsbadsess", hz);
1040 /*
1041 * Get the session again, in case a new one
1042 * has been created during the sleep.
1043 */
1044 sep = NFSMNT_MDSSESSION(nmp);
1045 NFSUNLOCKMNT(nmp);
1046 if ((nd->nd_flag & ND_LOOPBADSESS) != 0) {
1047 reterr = nfsv4_sequencelookup(nmp, sep,
1048 &slotpos, &maxslot, &slotseq,
1049 sessionid);
1050 if (reterr == 0) {
1051 /* Fill in new session info. */
1052 NFSCL_DEBUG(1,
1053 "Filling in new sequence\n");
1054 tl = nd->nd_sequence;
1055 bcopy(sessionid, tl,
1056 NFSX_V4SESSIONID);
1057 tl += NFSX_V4SESSIONID /
1058 NFSX_UNSIGNED;
1059 *tl++ = txdr_unsigned(slotseq);
1060 *tl++ = txdr_unsigned(slotpos);
1061 *tl = txdr_unsigned(maxslot);
1062 nd->nd_slotid = slotpos;
1063 nd->nd_flag |= ND_HASSLOTID;
1064 }
1065 if (reterr == NFSERR_BADSESSION ||
1066 reterr == 0) {
1067 NFSCL_DEBUG(1,
1068 "Badsession looping\n");
1069 m_freem(nd->nd_mrep);
1070 nd->nd_mrep = NULL;
1071 goto tryagain;
1072 }
1073 nd->nd_repstat = reterr;
1074 NFSCL_DEBUG(1, "Got err=%d\n", reterr);
1075 }
1076 }
1077 /*
1078 * When clp != NULL, it is a callback and all
1079 * callback operations can be retried for NFSERR_DELAY.
1080 */
1081 if (((nd->nd_repstat == NFSERR_DELAY ||
1082 nd->nd_repstat == NFSERR_GRACE) &&
1083 (nd->nd_flag & ND_NFSV4) && (clp != NULL ||
1084 (nd->nd_procnum != NFSPROC_DELEGRETURN &&
1085 nd->nd_procnum != NFSPROC_SETATTR &&
1086 nd->nd_procnum != NFSPROC_READ &&
1087 nd->nd_procnum != NFSPROC_READDS &&
1088 nd->nd_procnum != NFSPROC_WRITE &&
1089 nd->nd_procnum != NFSPROC_WRITEDS &&
1090 nd->nd_procnum != NFSPROC_OPEN &&
1091 nd->nd_procnum != NFSPROC_OPENLAYGET &&
1092 nd->nd_procnum != NFSPROC_CREATE &&
1093 nd->nd_procnum != NFSPROC_CREATELAYGET &&
1094 nd->nd_procnum != NFSPROC_OPENCONFIRM &&
1095 nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
1096 nd->nd_procnum != NFSPROC_CLOSE &&
1097 nd->nd_procnum != NFSPROC_LOCK &&
1098 nd->nd_procnum != NFSPROC_LOCKU))) ||
1099 (nd->nd_repstat == NFSERR_DELAY &&
1100 (nd->nd_flag & ND_NFSV4) == 0) ||
1101 nd->nd_repstat == NFSERR_RESOURCE) {
1102 /* Clip at NFS_TRYLATERDEL. */
1103 if (timespeccmp(&trylater_delay,
1104 &nfs_trylater_max, >))
1105 trylater_delay = nfs_trylater_max;
1106 getnanouptime(&waituntil);
1107 timespecadd(&waituntil, &trylater_delay,
1108 &waituntil);
1109 do {
1110 nfs_catnap(PZERO, 0, "nfstry");
1111 getnanouptime(&ts);
1112 } while (timespeccmp(&ts, &waituntil, <));
1113 timespecadd(&trylater_delay, &trylater_delay,
1114 &trylater_delay); /* Double each time. */
1115 if (slot != -1) {
1116 mtx_lock(&sep->nfsess_mtx);
1117 sep->nfsess_slotseq[slot]++;
1118 *nd->nd_slotseq = txdr_unsigned(
1119 sep->nfsess_slotseq[slot]);
1120 mtx_unlock(&sep->nfsess_mtx);
1121 }
1122 m_freem(nd->nd_mrep);
1123 nd->nd_mrep = NULL;
1124 goto tryagain;
1125 }
1126
1127 /*
1128 * If the File Handle was stale, invalidate the
1129 * lookup cache, just in case.
1130 * (vp != NULL implies a client side call)
1131 */
1132 if (nd->nd_repstat == ESTALE && vp != NULL) {
1133 cache_purge(vp);
1134 if (ncl_call_invalcaches != NULL)
1135 (*ncl_call_invalcaches)(vp);
1136 }
1137 }
1138 if ((nd->nd_flag & ND_NFSV4) != 0) {
1139 /* Free the slot, as required. */
1140 if (freeslot != -1)
1141 nfsv4_freeslot(sep, freeslot, false);
1142 /*
1143 * If this op is Putfh, throw its results away.
1144 */
1145 if (j >= 10000)
1146 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
1147 if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
1148 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
1149 i = fxdr_unsigned(int, *tl++);
1150 j = fxdr_unsigned(int, *tl);
1151 if (j >= 10000)
1152 NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
1153 j);
1154 /*
1155 * All Compounds that do an Op that must
1156 * be in sequence consist of NFSV4OP_PUTFH
1157 * followed by one of these. As such, we
1158 * can determine if the seqid# should be
1159 * incremented, here.
1160 */
1161 if ((i == NFSV4OP_OPEN ||
1162 i == NFSV4OP_OPENCONFIRM ||
1163 i == NFSV4OP_OPENDOWNGRADE ||
1164 i == NFSV4OP_CLOSE ||
1165 i == NFSV4OP_LOCK ||
1166 i == NFSV4OP_LOCKU) &&
1167 (j == 0 ||
1168 (j != NFSERR_STALECLIENTID &&
1169 j != NFSERR_STALESTATEID &&
1170 j != NFSERR_BADSTATEID &&
1171 j != NFSERR_BADSEQID &&
1172 j != NFSERR_BADXDR &&
1173 j != NFSERR_RESOURCE &&
1174 j != NFSERR_NOFILEHANDLE)))
1175 nd->nd_flag |= ND_INCRSEQID;
1176 }
1177 /*
1178 * If this op's status is non-zero, mark
1179 * that there is no more data to process.
1180 * The exception is Setattr, which always has xdr
1181 * when it has failed.
1182 */
1183 if (j != 0 && i != NFSV4OP_SETATTR)
1184 nd->nd_flag |= ND_NOMOREDATA;
1185
1186 /*
1187 * If R_DONTRECOVER is set, replace the stale error
1188 * reply, so that recovery isn't initiated.
1189 */
1190 if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
1191 nd->nd_repstat == NFSERR_BADSESSION ||
1192 nd->nd_repstat == NFSERR_STALESTATEID) &&
1193 rep != NULL && (rep->r_flags & R_DONTRECOVER))
1194 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
1195 }
1196 }
1197
1198 #ifdef KDTRACE_HOOKS
1199 if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
1200 uint32_t probe_id;
1201 int probe_procnum;
1202
1203 if (nd->nd_flag & ND_NFSV4) {
1204 probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
1205 probe_procnum = nd->nd_procnum;
1206 } else if (nd->nd_flag & ND_NFSV3) {
1207 probe_id = nfscl_nfs3_done_probes[procnum];
1208 probe_procnum = procnum;
1209 } else {
1210 probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
1211 probe_procnum = procnum;
1212 }
1213 if (probe_id != 0)
1214 (dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
1215 nd->nd_mreq, cred, probe_procnum, 0);
1216 }
1217 #endif
1218
1219 m_freem(nd->nd_mreq);
1220 if (usegssname == 0)
1221 AUTH_DESTROY(auth);
1222 if (rep != NULL)
1223 free(rep, M_NFSDREQ);
1224 if (set_sigset)
1225 newnfs_restore_sigmask(td, &oldset);
1226 return (0);
1227 nfsmout:
1228 mbuf_freem(nd->nd_mrep);
1229 mbuf_freem(nd->nd_mreq);
1230 if (usegssname == 0)
1231 AUTH_DESTROY(auth);
1232 if (rep != NULL)
1233 free(rep, M_NFSDREQ);
1234 if (set_sigset)
1235 newnfs_restore_sigmask(td, &oldset);
1236 return (error);
1237 }
1238
1239 /*
1240 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1241 * wait for all requests to complete. This is used by forced unmounts
1242 * to terminate any outstanding RPCs.
1243 */
1244 int
1245 newnfs_nmcancelreqs(struct nfsmount *nmp)
1246 {
1247 struct nfsclds *dsp;
1248 struct __rpc_client *cl;
1249
1250 if (nmp->nm_sockreq.nr_client != NULL)
1251 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1252 lookformore:
1253 NFSLOCKMNT(nmp);
1254 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
1255 NFSLOCKDS(dsp);
1256 if (dsp != TAILQ_FIRST(&nmp->nm_sess) &&
1257 (dsp->nfsclds_flags & NFSCLDS_CLOSED) == 0 &&
1258 dsp->nfsclds_sockp != NULL &&
1259 dsp->nfsclds_sockp->nr_client != NULL) {
1260 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
1261 cl = dsp->nfsclds_sockp->nr_client;
1262 NFSUNLOCKDS(dsp);
1263 NFSUNLOCKMNT(nmp);
1264 CLNT_CLOSE(cl);
1265 goto lookformore;
1266 }
1267 NFSUNLOCKDS(dsp);
1268 }
1269 NFSUNLOCKMNT(nmp);
1270 return (0);
1271 }
1272
1273 /*
1274 * Any signal that can interrupt an NFS operation in an intr mount
1275 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1276 */
1277 int newnfs_sig_set[] = {
1278 SIGINT,
1279 SIGTERM,
1280 SIGHUP,
1281 SIGKILL,
1282 SIGQUIT
1283 };
1284
1285 /*
1286 * Check to see if one of the signals in our subset is pending on
1287 * the process (in an intr mount).
1288 */
1289 static int
1290 nfs_sig_pending(sigset_t set)
1291 {
1292 int i;
1293
1294 for (i = 0 ; i < nitems(newnfs_sig_set); i++)
1295 if (SIGISMEMBER(set, newnfs_sig_set[i]))
1296 return (1);
1297 return (0);
1298 }
1299
1300 /*
1301 * The set/restore sigmask functions are used to (temporarily) overwrite
1302 * the thread td_sigmask during an RPC call (for example). These are also
1303 * used in other places in the NFS client that might tsleep().
1304 */
1305 void
1306 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1307 {
1308 sigset_t newset;
1309 int i;
1310 struct proc *p;
1311
1312 SIGFILLSET(newset);
1313 if (td == NULL)
1314 td = curthread; /* XXX */
1315 p = td->td_proc;
1316 /* Remove the NFS set of signals from newset */
1317 PROC_LOCK(p);
1318 mtx_lock(&p->p_sigacts->ps_mtx);
1319 for (i = 0 ; i < nitems(newnfs_sig_set); i++) {
1320 /*
1321 * But make sure we leave the ones already masked
1322 * by the process, ie. remove the signal from the
1323 * temporary signalmask only if it wasn't already
1324 * in p_sigmask.
1325 */
1326 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1327 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1328 SIGDELSET(newset, newnfs_sig_set[i]);
1329 }
1330 mtx_unlock(&p->p_sigacts->ps_mtx);
1331 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
1332 SIGPROCMASK_PROC_LOCKED);
1333 PROC_UNLOCK(p);
1334 }
1335
1336 void
1337 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1338 {
1339 if (td == NULL)
1340 td = curthread; /* XXX */
1341 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1342 }
1343
1344 /*
1345 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1346 * old one after msleep() returns.
1347 */
1348 int
1349 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1350 {
1351 sigset_t oldset;
1352 int error;
1353
1354 if ((priority & PCATCH) == 0)
1355 return msleep(ident, mtx, priority, wmesg, timo);
1356 if (td == NULL)
1357 td = curthread; /* XXX */
1358 newnfs_set_sigmask(td, &oldset);
1359 error = msleep(ident, mtx, priority, wmesg, timo);
1360 newnfs_restore_sigmask(td, &oldset);
1361 return (error);
1362 }
1363
1364 /*
1365 * Test for a termination condition pending on the process.
1366 * This is used for NFSMNT_INT mounts.
1367 */
1368 int
1369 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1370 {
1371 struct proc *p;
1372 sigset_t tmpset;
1373
1374 /* Terminate all requests while attempting a forced unmount. */
1375 if (NFSCL_FORCEDISM(nmp->nm_mountp))
1376 return (EIO);
1377 if (!(nmp->nm_flag & NFSMNT_INT))
1378 return (0);
1379 if (td == NULL)
1380 return (0);
1381 p = td->td_proc;
1382 PROC_LOCK(p);
1383 tmpset = p->p_siglist;
1384 SIGSETOR(tmpset, td->td_siglist);
1385 SIGSETNAND(tmpset, td->td_sigmask);
1386 mtx_lock(&p->p_sigacts->ps_mtx);
1387 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1388 mtx_unlock(&p->p_sigacts->ps_mtx);
1389 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1390 && nfs_sig_pending(tmpset)) {
1391 PROC_UNLOCK(p);
1392 return (EINTR);
1393 }
1394 PROC_UNLOCK(p);
1395 return (0);
1396 }
1397
1398 static int
1399 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1400 {
1401 struct proc *p;
1402
1403 p = td ? td->td_proc : NULL;
1404 if (error) {
1405 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n",
1406 server, msg, error);
1407 } else {
1408 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1409 }
1410 return (0);
1411 }
1412
1413 static void
1414 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1415 int error, int flags)
1416 {
1417 if (nmp == NULL)
1418 return;
1419 mtx_lock(&nmp->nm_mtx);
1420 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1421 nmp->nm_state |= NFSSTA_TIMEO;
1422 mtx_unlock(&nmp->nm_mtx);
1423 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1424 VQ_NOTRESP, 0);
1425 } else
1426 mtx_unlock(&nmp->nm_mtx);
1427 mtx_lock(&nmp->nm_mtx);
1428 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1429 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1430 mtx_unlock(&nmp->nm_mtx);
1431 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1432 VQ_NOTRESPLOCK, 0);
1433 } else
1434 mtx_unlock(&nmp->nm_mtx);
1435 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1436 }
1437
1438 static void
1439 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1440 int flags, int tprintfmsg)
1441 {
1442 if (nmp == NULL)
1443 return;
1444 if (tprintfmsg) {
1445 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1446 }
1447
1448 mtx_lock(&nmp->nm_mtx);
1449 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1450 nmp->nm_state &= ~NFSSTA_TIMEO;
1451 mtx_unlock(&nmp->nm_mtx);
1452 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1453 VQ_NOTRESP, 1);
1454 } else
1455 mtx_unlock(&nmp->nm_mtx);
1456
1457 mtx_lock(&nmp->nm_mtx);
1458 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1459 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1460 mtx_unlock(&nmp->nm_mtx);
1461 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1462 VQ_NOTRESPLOCK, 1);
1463 } else
1464 mtx_unlock(&nmp->nm_mtx);
1465 }
1466
Cache object: 7bef28cac369a0b2599f222585258340
|