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