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