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