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