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