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