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