xref: /freebsd/sys/fs/nfs/nfs_commonkrpc.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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_CANTSEND || stat == RPC_CANTRECV ||
929 	     stat == RPC_SYSTEMERROR || stat == RPC_INTR) {
930 		/* Check for a session slot that needs to be free'd. */
931 		if ((nd->nd_flag & (ND_NFSV41 | ND_HASSLOTID)) ==
932 		    (ND_NFSV41 | ND_HASSLOTID) && nmp != NULL &&
933 		    nd->nd_procnum != NFSPROC_NULL) {
934 			/*
935 			 * This should only occur when either the MDS or
936 			 * a client has an RPC against a DS fail.
937 			 * This happens because these cases use "soft"
938 			 * connections that can time out and fail.
939 			 * The slot used for this RPC is now in a
940 			 * non-deterministic state, but if the slot isn't
941 			 * free'd, threads can get stuck waiting for a slot.
942 			 */
943 			if (sep == NULL)
944 				sep = nfsmnt_mdssession(nmp);
945 			/*
946 			 * Bump the sequence# out of range, so that reuse of
947 			 * this slot will result in an NFSERR_SEQMISORDERED
948 			 * error and not a bogus cached RPC reply.
949 			 */
950 			mtx_lock(&sep->nfsess_mtx);
951 			sep->nfsess_slotseq[nd->nd_slotid] += 10;
952 			sep->nfsess_badslots |= (0x1ULL << nd->nd_slotid);
953 			mtx_unlock(&sep->nfsess_mtx);
954 			/* And free the slot. */
955 			nfsv4_freeslot(sep, nd->nd_slotid, false);
956 		}
957 		if (stat == RPC_INTR)
958 			error = EINTR;
959 		else {
960 			NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
961 			error = ENXIO;
962 		}
963 	} else {
964 		NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
965 		error = EACCES;
966 	}
967 	if (error) {
968 		m_freem(nd->nd_mreq);
969 		if (usegssname == 0)
970 			AUTH_DESTROY(auth);
971 		if (rep != NULL)
972 			free(rep, M_NFSDREQ);
973 		if (set_sigset)
974 			newnfs_restore_sigmask(td, &oldset);
975 		return (error);
976 	}
977 
978 	KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
979 
980 	/*
981 	 * Search for any mbufs that are not a multiple of 4 bytes long
982 	 * or with m_data not longword aligned.
983 	 * These could cause pointer alignment problems, so copy them to
984 	 * well aligned mbufs.
985 	 */
986 	newnfs_realign(&nd->nd_mrep, M_WAITOK);
987 	nd->nd_md = nd->nd_mrep;
988 	nd->nd_dpos = mtod(nd->nd_md, caddr_t);
989 	nd->nd_repstat = 0;
990 	if (nd->nd_procnum != NFSPROC_NULL &&
991 	    nd->nd_procnum != NFSV4PROC_CBNULL) {
992 		/* If sep == NULL, set it to the default in nmp. */
993 		if (sep == NULL && nmp != NULL)
994 			sep = nfsmnt_mdssession(nmp);
995 		/*
996 		 * and now the actual NFS xdr.
997 		 */
998 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
999 		nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
1000 		if (nd->nd_repstat >= 10000)
1001 			NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
1002 			    (int)nd->nd_repstat);
1003 
1004 		/*
1005 		 * Get rid of the tag, return count and SEQUENCE result for
1006 		 * NFSv4.
1007 		 */
1008 		if ((nd->nd_flag & ND_NFSV4) != 0 && nd->nd_repstat !=
1009 		    NFSERR_MINORVERMISMATCH) {
1010 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
1011 			i = fxdr_unsigned(int, *tl);
1012 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
1013 			if (error)
1014 				goto nfsmout;
1015 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1016 			opcnt = fxdr_unsigned(int, *tl++);
1017 			i = fxdr_unsigned(int, *tl++);
1018 			j = fxdr_unsigned(int, *tl);
1019 			if (j >= 10000)
1020 				NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
1021 			/*
1022 			 * If the first op is Sequence, free up the slot.
1023 			 */
1024 			if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) ||
1025 			   (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0)) {
1026 				NFSCL_DEBUG(1, "failed seq=%d\n", j);
1027 				if (sep != NULL && i == NFSV4OP_SEQUENCE &&
1028 				    j == NFSERR_SEQMISORDERED) {
1029 					mtx_lock(&sep->nfsess_mtx);
1030 					sep->nfsess_badslots |=
1031 					    (0x1ULL << nd->nd_slotid);
1032 					mtx_unlock(&sep->nfsess_mtx);
1033 				}
1034 			}
1035 			if (((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) ||
1036 			    (clp != NULL && i == NFSV4OP_CBSEQUENCE &&
1037 			    j == 0)) && sep != NULL) {
1038 				if (i == NFSV4OP_SEQUENCE)
1039 					NFSM_DISSECT(tl, uint32_t *,
1040 					    NFSX_V4SESSIONID +
1041 					    5 * NFSX_UNSIGNED);
1042 				else
1043 					NFSM_DISSECT(tl, uint32_t *,
1044 					    NFSX_V4SESSIONID +
1045 					    4 * NFSX_UNSIGNED);
1046 				mtx_lock(&sep->nfsess_mtx);
1047 				if (bcmp(tl, sep->nfsess_sessionid,
1048 				    NFSX_V4SESSIONID) == 0) {
1049 					tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
1050 					retseq = fxdr_unsigned(uint32_t, *tl++);
1051 					slot = fxdr_unsigned(int, *tl++);
1052 					if ((nd->nd_flag & ND_HASSLOTID) != 0) {
1053 						if (slot >= NFSV4_SLOTS ||
1054 						    (i == NFSV4OP_CBSEQUENCE &&
1055 						     slot >= NFSV4_CBSLOTS)) {
1056 							printf("newnfs_request:"
1057 							    " Bogus slot\n");
1058 							slot = nd->nd_slotid;
1059 						} else if (slot !=
1060 						    nd->nd_slotid) {
1061 						    printf("newnfs_request:"
1062 							" Wrong session "
1063 							"srvslot=%d "
1064 							"slot=%d\n", slot,
1065 							nd->nd_slotid);
1066 						    if (i == NFSV4OP_SEQUENCE) {
1067 							/*
1068 							 * Mark both slots as
1069 							 * bad, because we do
1070 							 * not know if the
1071 							 * server has advanced
1072 							 * the sequence# for
1073 							 * either of them.
1074 							 */
1075 							sep->nfsess_badslots |=
1076 							    (0x1ULL << slot);
1077 							sep->nfsess_badslots |=
1078 							    (0x1ULL <<
1079 							     nd->nd_slotid);
1080 						    }
1081 						    slot = nd->nd_slotid;
1082 						}
1083 					} else if (slot != 0) {
1084 						printf("newnfs_request: Bad "
1085 						    "session slot=%d\n", slot);
1086 						slot = 0;
1087 					}
1088 					freeslot = slot;
1089 					if (retseq != sep->nfsess_slotseq[slot])
1090 						printf("retseq diff 0x%x\n",
1091 						    retseq);
1092 					retval = fxdr_unsigned(uint32_t, *++tl);
1093 					if ((retval + 1) < sep->nfsess_foreslots
1094 					    )
1095 						sep->nfsess_foreslots = (retval
1096 						    + 1);
1097 					else if ((retval + 1) >
1098 					    sep->nfsess_foreslots)
1099 						sep->nfsess_foreslots = (retval
1100 						    < 64) ? (retval + 1) : 64;
1101 				}
1102 				mtx_unlock(&sep->nfsess_mtx);
1103 
1104 				/* Grab the op and status for the next one. */
1105 				if (opcnt > 1) {
1106 					NFSM_DISSECT(tl, uint32_t *,
1107 					    2 * NFSX_UNSIGNED);
1108 					i = fxdr_unsigned(int, *tl++);
1109 					j = fxdr_unsigned(int, *tl);
1110 				}
1111 			}
1112 		}
1113 		if (nd->nd_repstat != 0) {
1114 			if (nd->nd_repstat == NFSERR_BADSESSION &&
1115 			    nmp != NULL && dssep == NULL &&
1116 			    (nd->nd_flag & ND_NFSV41) != 0) {
1117 				/*
1118 				 * If this is a client side MDS RPC, mark
1119 				 * the MDS session defunct and initiate
1120 				 * recovery, as required.
1121 				 * The nfsess_defunct field is protected by
1122 				 * the NFSLOCKMNT()/nm_mtx lock and not the
1123 				 * nfsess_mtx lock to simplify its handling,
1124 				 * for the MDS session. This lock is also
1125 				 * sufficient for nfsess_sessionid, since it
1126 				 * never changes in the structure.
1127 				 */
1128 				NFSCL_DEBUG(1, "Got badsession\n");
1129 				NFSLOCKCLSTATE();
1130 				NFSLOCKMNT(nmp);
1131 				sep = NFSMNT_MDSSESSION(nmp);
1132 				if (bcmp(sep->nfsess_sessionid, nd->nd_sequence,
1133 				    NFSX_V4SESSIONID) == 0) {
1134 					/* Initiate recovery. */
1135 					sep->nfsess_defunct = 1;
1136 					NFSCL_DEBUG(1, "Marked defunct\n");
1137 					if (nmp->nm_clp != NULL) {
1138 						nmp->nm_clp->nfsc_flags |=
1139 						    NFSCLFLAGS_RECOVER;
1140 						wakeup(nmp->nm_clp);
1141 					}
1142 				}
1143 				NFSUNLOCKCLSTATE();
1144 				/*
1145 				 * Sleep for up to 1sec waiting for a new
1146 				 * session.
1147 				 */
1148 				mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO,
1149 				    "nfsbadsess", hz);
1150 				/*
1151 				 * Get the session again, in case a new one
1152 				 * has been created during the sleep.
1153 				 */
1154 				sep = NFSMNT_MDSSESSION(nmp);
1155 				NFSUNLOCKMNT(nmp);
1156 				if ((nd->nd_flag & ND_LOOPBADSESS) != 0) {
1157 					reterr = nfsv4_sequencelookup(nmp, sep,
1158 					    &slotpos, &maxslot, &slotseq,
1159 					    sessionid, true);
1160 					if (reterr == 0) {
1161 						/* Fill in new session info. */
1162 						NFSCL_DEBUG(1,
1163 						  "Filling in new sequence\n");
1164 						tl = nd->nd_sequence;
1165 						bcopy(sessionid, tl,
1166 						    NFSX_V4SESSIONID);
1167 						tl += NFSX_V4SESSIONID /
1168 						    NFSX_UNSIGNED;
1169 						*tl++ = txdr_unsigned(slotseq);
1170 						*tl++ = txdr_unsigned(slotpos);
1171 						*tl = txdr_unsigned(maxslot);
1172 					}
1173 					if (reterr == NFSERR_BADSESSION ||
1174 					    reterr == 0) {
1175 						NFSCL_DEBUG(1,
1176 						    "Badsession looping\n");
1177 						m_freem(nd->nd_mrep);
1178 						nd->nd_mrep = NULL;
1179 						goto tryagain;
1180 					}
1181 					nd->nd_repstat = reterr;
1182 					NFSCL_DEBUG(1, "Got err=%d\n", reterr);
1183 				}
1184 			}
1185 			/*
1186 			 * When clp != NULL, it is a callback and all
1187 			 * callback operations can be retried for NFSERR_DELAY.
1188 			 */
1189 			if (((nd->nd_repstat == NFSERR_DELAY ||
1190 			      nd->nd_repstat == NFSERR_GRACE) &&
1191 			     (nd->nd_flag & ND_NFSV4) && (clp != NULL ||
1192 			     (nd->nd_procnum != NFSPROC_DELEGRETURN &&
1193 			     nd->nd_procnum != NFSPROC_SETATTR &&
1194 			     nd->nd_procnum != NFSPROC_READ &&
1195 			     nd->nd_procnum != NFSPROC_READDS &&
1196 			     nd->nd_procnum != NFSPROC_WRITE &&
1197 			     nd->nd_procnum != NFSPROC_WRITEDS &&
1198 			     nd->nd_procnum != NFSPROC_OPEN &&
1199 			     nd->nd_procnum != NFSPROC_OPENLAYGET &&
1200 			     nd->nd_procnum != NFSPROC_CREATE &&
1201 			     nd->nd_procnum != NFSPROC_CREATELAYGET &&
1202 			     nd->nd_procnum != NFSPROC_OPENCONFIRM &&
1203 			     nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
1204 			     nd->nd_procnum != NFSPROC_CLOSE &&
1205 			     nd->nd_procnum != NFSPROC_LOCK &&
1206 			     nd->nd_procnum != NFSPROC_LOCKU))) ||
1207 			    (nd->nd_repstat == NFSERR_DELAY &&
1208 			     (nd->nd_flag & ND_NFSV4) == 0) ||
1209 			    nd->nd_repstat == NFSERR_RESOURCE) {
1210 				/* Clip at NFS_TRYLATERDEL. */
1211 				if (timespeccmp(&trylater_delay,
1212 				    &nfs_trylater_max, >))
1213 					trylater_delay = nfs_trylater_max;
1214 				getnanouptime(&waituntil);
1215 				timespecadd(&waituntil, &trylater_delay,
1216 				    &waituntil);
1217 				do {
1218 					nfs_catnap(PZERO, 0, "nfstry");
1219 					getnanouptime(&ts);
1220 				} while (timespeccmp(&ts, &waituntil, <));
1221 				timespecadd(&trylater_delay, &trylater_delay,
1222 				    &trylater_delay);	/* Double each time. */
1223 				if (slot != -1) {
1224 					mtx_lock(&sep->nfsess_mtx);
1225 					sep->nfsess_slotseq[slot]++;
1226 					*nd->nd_slotseq = txdr_unsigned(
1227 					    sep->nfsess_slotseq[slot]);
1228 					mtx_unlock(&sep->nfsess_mtx);
1229 				}
1230 				m_freem(nd->nd_mrep);
1231 				nd->nd_mrep = NULL;
1232 				goto tryagain;
1233 			}
1234 
1235 			/*
1236 			 * If the File Handle was stale, invalidate the
1237 			 * lookup cache, just in case.
1238 			 * (vp != NULL implies a client side call)
1239 			 */
1240 			if (nd->nd_repstat == ESTALE && vp != NULL) {
1241 				cache_purge(vp);
1242 				if (ncl_call_invalcaches != NULL)
1243 					(*ncl_call_invalcaches)(vp);
1244 			}
1245 		}
1246 		if ((nd->nd_flag & ND_NFSV4) != 0) {
1247 			/* Free the slot, as required. */
1248 			if (freeslot != -1)
1249 				nfsv4_freeslot(sep, freeslot, false);
1250 			/*
1251 			 * If this op is Putfh, throw its results away.
1252 			 */
1253 			if (j >= 10000)
1254 				NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
1255 			if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
1256 				NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
1257 				i = fxdr_unsigned(int, *tl++);
1258 				j = fxdr_unsigned(int, *tl);
1259 				if (j >= 10000)
1260 					NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
1261 					    j);
1262 				/*
1263 				 * All Compounds that do an Op that must
1264 				 * be in sequence consist of NFSV4OP_PUTFH
1265 				 * followed by one of these. As such, we
1266 				 * can determine if the seqid# should be
1267 				 * incremented, here.
1268 				 */
1269 				if ((i == NFSV4OP_OPEN ||
1270 				     i == NFSV4OP_OPENCONFIRM ||
1271 				     i == NFSV4OP_OPENDOWNGRADE ||
1272 				     i == NFSV4OP_CLOSE ||
1273 				     i == NFSV4OP_LOCK ||
1274 				     i == NFSV4OP_LOCKU) &&
1275 				    (j == 0 ||
1276 				     (j != NFSERR_STALECLIENTID &&
1277 				      j != NFSERR_STALESTATEID &&
1278 				      j != NFSERR_BADSTATEID &&
1279 				      j != NFSERR_BADSEQID &&
1280 				      j != NFSERR_BADXDR &&
1281 				      j != NFSERR_RESOURCE &&
1282 				      j != NFSERR_NOFILEHANDLE)))
1283 					nd->nd_flag |= ND_INCRSEQID;
1284 			}
1285 			/*
1286 			 * If this op's status is non-zero, mark
1287 			 * that there is no more data to process.
1288 			 * The exception is Setattr, which always has xdr
1289 			 * when it has failed.
1290 			 */
1291 			if (j != 0 && i != NFSV4OP_SETATTR)
1292 				nd->nd_flag |= ND_NOMOREDATA;
1293 
1294 			/*
1295 			 * If R_DONTRECOVER is set, replace the stale error
1296 			 * reply, so that recovery isn't initiated.
1297 			 */
1298 			if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
1299 			     nd->nd_repstat == NFSERR_BADSESSION ||
1300 			     nd->nd_repstat == NFSERR_STALESTATEID) &&
1301 			    rep != NULL && (rep->r_flags & R_DONTRECOVER))
1302 				nd->nd_repstat = NFSERR_STALEDONTRECOVER;
1303 		}
1304 	}
1305 
1306 #ifdef KDTRACE_HOOKS
1307 	if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
1308 		uint32_t probe_id;
1309 		int probe_procnum;
1310 
1311 		if (nd->nd_flag & ND_NFSV4) {
1312 			probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
1313 			probe_procnum = nd->nd_procnum;
1314 		} else if (nd->nd_flag & ND_NFSV3) {
1315 			probe_id = nfscl_nfs3_done_probes[procnum];
1316 			probe_procnum = procnum;
1317 		} else {
1318 			probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
1319 			probe_procnum = procnum;
1320 		}
1321 		if (probe_id != 0)
1322 			(dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
1323 			    nd->nd_mreq, cred, probe_procnum, 0);
1324 	}
1325 #endif
1326 
1327 	m_freem(nd->nd_mreq);
1328 	if (usegssname == 0)
1329 		AUTH_DESTROY(auth);
1330 	if (rep != NULL)
1331 		free(rep, M_NFSDREQ);
1332 	if (set_sigset)
1333 		newnfs_restore_sigmask(td, &oldset);
1334 	return (0);
1335 nfsmout:
1336 	m_freem(nd->nd_mrep);
1337 	m_freem(nd->nd_mreq);
1338 	if (usegssname == 0)
1339 		AUTH_DESTROY(auth);
1340 	if (rep != NULL)
1341 		free(rep, M_NFSDREQ);
1342 	if (set_sigset)
1343 		newnfs_restore_sigmask(td, &oldset);
1344 	return (error);
1345 }
1346 
1347 /*
1348  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1349  * wait for all requests to complete. This is used by forced unmounts
1350  * to terminate any outstanding RPCs.
1351  */
1352 int
1353 newnfs_nmcancelreqs(struct nfsmount *nmp)
1354 {
1355 	struct nfsclds *dsp;
1356 	struct __rpc_client *cl;
1357 	int i;
1358 
1359 	if (nmp->nm_sockreq.nr_client != NULL)
1360 		CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1361 	for (i = 0; i < nmp->nm_aconnect; i++)
1362 		if (nmp->nm_aconn[i] != NULL)
1363 			CLNT_CLOSE(nmp->nm_aconn[i]);
1364 lookformore:
1365 	NFSLOCKMNT(nmp);
1366 	TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
1367 		NFSLOCKDS(dsp);
1368 		if (dsp != TAILQ_FIRST(&nmp->nm_sess) &&
1369 		    (dsp->nfsclds_flags & NFSCLDS_CLOSED) == 0 &&
1370 		    dsp->nfsclds_sockp != NULL &&
1371 		    dsp->nfsclds_sockp->nr_client != NULL) {
1372 			dsp->nfsclds_flags |= NFSCLDS_CLOSED;
1373 			cl = dsp->nfsclds_sockp->nr_client;
1374 			NFSUNLOCKDS(dsp);
1375 			NFSUNLOCKMNT(nmp);
1376 			CLNT_CLOSE(cl);
1377 			goto lookformore;
1378 		}
1379 		NFSUNLOCKDS(dsp);
1380 	}
1381 	NFSUNLOCKMNT(nmp);
1382 	return (0);
1383 }
1384 
1385 /*
1386  * Any signal that can interrupt an NFS operation in an intr mount
1387  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1388  */
1389 int newnfs_sig_set[] = {
1390 	SIGINT,
1391 	SIGTERM,
1392 	SIGHUP,
1393 	SIGKILL,
1394 	SIGQUIT
1395 };
1396 
1397 /*
1398  * Check to see if one of the signals in our subset is pending on
1399  * the process (in an intr mount).
1400  */
1401 static int
1402 nfs_sig_pending(sigset_t set)
1403 {
1404 	int i;
1405 
1406 	for (i = 0 ; i < nitems(newnfs_sig_set); i++)
1407 		if (SIGISMEMBER(set, newnfs_sig_set[i]))
1408 			return (1);
1409 	return (0);
1410 }
1411 
1412 /*
1413  * The set/restore sigmask functions are used to (temporarily) overwrite
1414  * the thread td_sigmask during an RPC call (for example). These are also
1415  * used in other places in the NFS client that might tsleep().
1416  */
1417 void
1418 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1419 {
1420 	sigset_t newset;
1421 	int i;
1422 	struct proc *p;
1423 
1424 	SIGFILLSET(newset);
1425 	if (td == NULL)
1426 		td = curthread; /* XXX */
1427 	p = td->td_proc;
1428 	/* Remove the NFS set of signals from newset */
1429 	PROC_LOCK(p);
1430 	mtx_lock(&p->p_sigacts->ps_mtx);
1431 	for (i = 0 ; i < nitems(newnfs_sig_set); i++) {
1432 		/*
1433 		 * But make sure we leave the ones already masked
1434 		 * by the process, ie. remove the signal from the
1435 		 * temporary signalmask only if it wasn't already
1436 		 * in p_sigmask.
1437 		 */
1438 		if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1439 		    !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1440 			SIGDELSET(newset, newnfs_sig_set[i]);
1441 	}
1442 	mtx_unlock(&p->p_sigacts->ps_mtx);
1443 	kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
1444 	    SIGPROCMASK_PROC_LOCKED);
1445 	PROC_UNLOCK(p);
1446 }
1447 
1448 void
1449 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1450 {
1451 	if (td == NULL)
1452 		td = curthread; /* XXX */
1453 	kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1454 }
1455 
1456 /*
1457  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1458  * old one after msleep() returns.
1459  */
1460 int
1461 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1462 {
1463 	sigset_t oldset;
1464 	int error;
1465 
1466 	if ((priority & PCATCH) == 0)
1467 		return msleep(ident, mtx, priority, wmesg, timo);
1468 	if (td == NULL)
1469 		td = curthread; /* XXX */
1470 	newnfs_set_sigmask(td, &oldset);
1471 	error = msleep(ident, mtx, priority, wmesg, timo);
1472 	newnfs_restore_sigmask(td, &oldset);
1473 	return (error);
1474 }
1475 
1476 /*
1477  * Test for a termination condition pending on the process.
1478  * This is used for NFSMNT_INT mounts.
1479  */
1480 int
1481 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1482 {
1483 	struct proc *p;
1484 	sigset_t tmpset;
1485 
1486 	/* Terminate all requests while attempting a forced unmount. */
1487 	if (NFSCL_FORCEDISM(nmp->nm_mountp))
1488 		return (EIO);
1489 	if (!(nmp->nm_flag & NFSMNT_INT))
1490 		return (0);
1491 	if (td == NULL)
1492 		return (0);
1493 	p = td->td_proc;
1494 	PROC_LOCK(p);
1495 	tmpset = p->p_siglist;
1496 	SIGSETOR(tmpset, td->td_siglist);
1497 	SIGSETNAND(tmpset, td->td_sigmask);
1498 	mtx_lock(&p->p_sigacts->ps_mtx);
1499 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1500 	mtx_unlock(&p->p_sigacts->ps_mtx);
1501 	if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1502 	    && nfs_sig_pending(tmpset)) {
1503 		PROC_UNLOCK(p);
1504 		return (EINTR);
1505 	}
1506 	PROC_UNLOCK(p);
1507 	return (0);
1508 }
1509 
1510 static int
1511 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1512 {
1513 	struct proc *p;
1514 
1515 	p = td ? td->td_proc : NULL;
1516 	if (error) {
1517 		tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n",
1518 		    server, msg, error);
1519 	} else {
1520 		tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1521 	}
1522 	return (0);
1523 }
1524 
1525 static void
1526 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1527     int error, int flags)
1528 {
1529 	if (nmp == NULL)
1530 		return;
1531 	mtx_lock(&nmp->nm_mtx);
1532 	if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1533 		nmp->nm_state |= NFSSTA_TIMEO;
1534 		mtx_unlock(&nmp->nm_mtx);
1535 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1536 		    VQ_NOTRESP, 0);
1537 	} else
1538 		mtx_unlock(&nmp->nm_mtx);
1539 	mtx_lock(&nmp->nm_mtx);
1540 	if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1541 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
1542 		mtx_unlock(&nmp->nm_mtx);
1543 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1544 		    VQ_NOTRESPLOCK, 0);
1545 	} else
1546 		mtx_unlock(&nmp->nm_mtx);
1547 	nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1548 }
1549 
1550 static void
1551 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1552     int flags, int tprintfmsg)
1553 {
1554 	if (nmp == NULL)
1555 		return;
1556 	if (tprintfmsg) {
1557 		nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1558 	}
1559 
1560 	mtx_lock(&nmp->nm_mtx);
1561 	if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1562 		nmp->nm_state &= ~NFSSTA_TIMEO;
1563 		mtx_unlock(&nmp->nm_mtx);
1564 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1565 		    VQ_NOTRESP, 1);
1566 	} else
1567 		mtx_unlock(&nmp->nm_mtx);
1568 
1569 	mtx_lock(&nmp->nm_mtx);
1570 	if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1571 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1572 		mtx_unlock(&nmp->nm_mtx);
1573 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1574 		    VQ_NOTRESPLOCK, 1);
1575 	} else
1576 		mtx_unlock(&nmp->nm_mtx);
1577 }
1578