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