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