1 /*	$NetBSD: nfs_commonkrpc.c,v 1.1.1.1 2013/09/30 07:19:36 dholland Exp $	*/
2 /*-
3  * Copyright (c) 1989, 1991, 1993, 1995
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Rick Macklem at The University of Guelph.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #include <sys/cdefs.h>
36 /* __FBSDID("FreeBSD: head/sys/fs/nfs/nfs_commonkrpc.c 253049 2013-07-09 01:05:28Z rmacklem "); */
37 __RCSID("$NetBSD: nfs_commonkrpc.c,v 1.1.1.1 2013/09/30 07:19:36 dholland Exp $");
38 
39 /*
40  * Socket operations for use by nfs
41  */
42 
43 #include "opt_kdtrace.h"
44 #include "opt_kgssapi.h"
45 #include "opt_nfs.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/limits.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/signalvar.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
61 #include <sys/vnode.h>
62 
63 #include <rpc/rpc.h>
64 
65 #include <kgssapi/krb5/kcrypto.h>
66 
67 #include <fs/nfs/nfsport.h>
68 
69 #ifdef KDTRACE_HOOKS
70 #include <sys/dtrace_bsd.h>
71 
72 dtrace_nfsclient_nfs23_start_probe_func_t
73 		dtrace_nfscl_nfs234_start_probe;
74 
75 dtrace_nfsclient_nfs23_done_probe_func_t
76 		dtrace_nfscl_nfs234_done_probe;
77 
78 /*
79  * Registered probes by RPC type.
80  */
81 uint32_t	nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
82 uint32_t	nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
83 
84 uint32_t	nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
85 uint32_t	nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
86 
87 uint32_t	nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
88 uint32_t	nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
89 #endif
90 
91 NFSSTATESPINLOCK;
92 NFSREQSPINLOCK;
93 NFSDLOCKMUTEX;
94 extern struct nfsstats newnfsstats;
95 extern struct nfsreqhead nfsd_reqq;
96 extern int nfscl_ticks;
97 extern void (*ncl_call_invalcaches)(struct vnode *);
98 extern int nfs_numnfscbd;
99 extern int nfscl_debuglevel;
100 
101 SVCPOOL		*nfscbd_pool;
102 static int	nfsrv_gsscallbackson = 0;
103 static int	nfs_bufpackets = 4;
104 static int	nfs_reconnects;
105 static int	nfs3_jukebox_delay = 10;
106 static int	nfs_skip_wcc_data_onerr = 1;
107 
108 SYSCTL_DECL(_vfs_nfs);
109 
110 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
111     "Buffer reservation size 2 < x < 64");
112 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
113     "Number of times the nfs client has had to reconnect");
114 SYSCTL_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");
116 SYSCTL_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 
119 static void	nfs_down(struct nfsmount *, struct thread *, const char *,
120     int, int);
121 static void	nfs_up(struct nfsmount *, struct thread *, const char *,
122     int, int);
123 static int	nfs_msg(struct thread *, const char *, const char *, int);
124 
125 struct 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 
131 static 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  */
160 int
newnfs_connect(struct nfsmount * nmp,struct nfssockreq * nrp,struct ucred * cred,NFSPROC_T * p,int callback_retry_mult)161 newnfs_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 		if (NFSHASNFSV4N(nmp)) {
285 			/*
286 			 * Make sure the nfscbd_pool doesn't get destroyed
287 			 * while doing this.
288 			 */
289 			NFSD_LOCK();
290 			if (nfs_numnfscbd > 0) {
291 				nfs_numnfscbd++;
292 				NFSD_UNLOCK();
293 				xprt = svc_vc_create_backchannel(nfscbd_pool);
294 				CLNT_CONTROL(client, CLSET_BACKCHANNEL, xprt);
295 				NFSD_LOCK();
296 				nfs_numnfscbd--;
297 				if (nfs_numnfscbd == 0)
298 					wakeup(&nfs_numnfscbd);
299 			}
300 			NFSD_UNLOCK();
301 		}
302 	} else {
303 		/*
304 		 * Three cases:
305 		 * - Null RPC callback to client
306 		 * - Non-Null RPC callback to client, wait a little longer
307 		 * - upcalls to nfsuserd and gssd (clp == NULL)
308 		 */
309 		if (callback_retry_mult == 0) {
310 			retries = NFSV4_UPCALLRETRY;
311 			CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
312 		} else {
313 			retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
314 		}
315 	}
316 	CLNT_CONTROL(client, CLSET_RETRIES, &retries);
317 
318 	if (nmp != NULL) {
319 		/*
320 		 * For UDP, there are 2 timeouts:
321 		 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
322 		 *   that does a retransmit of an RPC request using the same
323 		 *   socket and xid. This is what you normally want to do,
324 		 *   since NFS servers depend on "same xid" for their
325 		 *   Duplicate Request Cache.
326 		 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
327 		 *   retransmits on the same socket should fail and a fresh
328 		 *   socket created. Each of these timeouts counts as one
329 		 *   CLSET_RETRIES as set above.
330 		 * Set the initial retransmit timeout for UDP. This timeout
331 		 * doesn't exist for TCP and the following call just fails,
332 		 * which is ok.
333 		 */
334 		timo.tv_sec = nmp->nm_timeo / NFS_HZ;
335 		timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
336 		CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
337 	}
338 
339 	mtx_lock(&nrp->nr_mtx);
340 	if (nrp->nr_client != NULL) {
341 		/*
342 		 * Someone else already connected.
343 		 */
344 		CLNT_RELEASE(client);
345 	} else {
346 		nrp->nr_client = client;
347 	}
348 
349 	/*
350 	 * Protocols that do not require connections may be optionally left
351 	 * unconnected for servers that reply from a port other than NFS_PORT.
352 	 */
353 	if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
354 		mtx_unlock(&nrp->nr_mtx);
355 		CLNT_CONTROL(client, CLSET_CONNECT, &one);
356 	} else {
357 		mtx_unlock(&nrp->nr_mtx);
358 	}
359 
360 	/* Restore current thread's credentials. */
361 	td->td_ucred = origcred;
362 
363 out:
364 	NFSEXITCODE(error);
365 	return (error);
366 }
367 
368 /*
369  * NFS disconnect. Clean up and unlink.
370  */
371 void
newnfs_disconnect(struct nfssockreq * nrp)372 newnfs_disconnect(struct nfssockreq *nrp)
373 {
374 	CLIENT *client;
375 
376 	mtx_lock(&nrp->nr_mtx);
377 	if (nrp->nr_client != NULL) {
378 		client = nrp->nr_client;
379 		nrp->nr_client = NULL;
380 		mtx_unlock(&nrp->nr_mtx);
381 		rpc_gss_secpurge_call(client);
382 		CLNT_CLOSE(client);
383 		CLNT_RELEASE(client);
384 	} else {
385 		mtx_unlock(&nrp->nr_mtx);
386 	}
387 }
388 
389 static AUTH *
nfs_getauth(struct nfssockreq * nrp,int secflavour,char * clnt_principal,char * srv_principal,gss_OID mech_oid,struct ucred * cred)390 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
391     char *srv_principal, gss_OID mech_oid, struct ucred *cred)
392 {
393 	rpc_gss_service_t svc;
394 	AUTH *auth;
395 
396 	switch (secflavour) {
397 	case RPCSEC_GSS_KRB5:
398 	case RPCSEC_GSS_KRB5I:
399 	case RPCSEC_GSS_KRB5P:
400 		if (!mech_oid) {
401 			if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
402 				return (NULL);
403 		}
404 		if (secflavour == RPCSEC_GSS_KRB5)
405 			svc = rpc_gss_svc_none;
406 		else if (secflavour == RPCSEC_GSS_KRB5I)
407 			svc = rpc_gss_svc_integrity;
408 		else
409 			svc = rpc_gss_svc_privacy;
410 
411 		if (clnt_principal == NULL)
412 			auth = rpc_gss_secfind_call(nrp->nr_client, cred,
413 			    srv_principal, mech_oid, svc);
414 		else {
415 			auth = rpc_gss_seccreate_call(nrp->nr_client, cred,
416 			    clnt_principal, srv_principal, "kerberosv5",
417 			    svc, NULL, NULL, NULL);
418 			return (auth);
419 		}
420 		if (auth != NULL)
421 			return (auth);
422 		/* fallthrough */
423 	case AUTH_SYS:
424 	default:
425 		return (authunix_create(cred));
426 
427 	}
428 }
429 
430 /*
431  * Callback from the RPC code to generate up/down notifications.
432  */
433 
434 struct nfs_feedback_arg {
435 	struct nfsmount *nf_mount;
436 	int		nf_lastmsg;	/* last tprintf */
437 	int		nf_tprintfmsg;
438 	struct thread	*nf_td;
439 };
440 
441 static void
nfs_feedback(int type,int proc,void * arg)442 nfs_feedback(int type, int proc, void *arg)
443 {
444 	struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
445 	struct nfsmount *nmp = nf->nf_mount;
446 	time_t now;
447 
448 	switch (type) {
449 	case FEEDBACK_REXMIT2:
450 	case FEEDBACK_RECONNECT:
451 		now = NFSD_MONOSEC;
452 		if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
453 			nfs_down(nmp, nf->nf_td,
454 			    "not responding", 0, NFSSTA_TIMEO);
455 			nf->nf_tprintfmsg = TRUE;
456 			nf->nf_lastmsg = now;
457 		}
458 		break;
459 
460 	case FEEDBACK_OK:
461 		nfs_up(nf->nf_mount, nf->nf_td,
462 		    "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
463 		break;
464 	}
465 }
466 
467 /*
468  * newnfs_request - goes something like this
469  *	- does the rpc by calling the krpc layer
470  *	- break down rpc header and return with nfs reply
471  * nb: always frees up nd_mreq mbuf list
472  */
473 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 * sep)474 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
475     struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
476     struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
477     u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *sep)
478 {
479 	u_int32_t retseq, retval, *tl;
480 	time_t waituntil;
481 	int i = 0, j = 0, opcnt, set_sigset = 0, slot;
482 	int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
483 	int freeslot, timeo;
484 	u_int16_t procnum;
485 	u_int trylater_delay = 1;
486 	struct nfs_feedback_arg nf;
487 	struct timeval timo;
488 	AUTH *auth;
489 	struct rpc_callextra ext;
490 	enum clnt_stat stat;
491 	struct nfsreq *rep = NULL;
492 	char *srv_principal = NULL, *clnt_principal = NULL;
493 	sigset_t oldset;
494 	struct ucred *authcred;
495 
496 	if (xidp != NULL)
497 		*xidp = 0;
498 	/* Reject requests while attempting a forced unmount. */
499 	if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
500 		m_freem(nd->nd_mreq);
501 		return (ESTALE);
502 	}
503 
504 	/*
505 	 * Set authcred, which is used to acquire RPC credentials to
506 	 * the cred argument, by default. The crhold() should not be
507 	 * necessary, but will ensure that some future code change
508 	 * doesn't result in the credential being free'd prematurely.
509 	 */
510 	authcred = crhold(cred);
511 
512 	/* For client side interruptible mounts, mask off the signals. */
513 	if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
514 		newnfs_set_sigmask(td, &oldset);
515 		set_sigset = 1;
516 	}
517 
518 	/*
519 	 * XXX if not already connected call nfs_connect now. Longer
520 	 * term, change nfs_mount to call nfs_connect unconditionally
521 	 * and let clnt_reconnect_create handle reconnects.
522 	 */
523 	if (nrp->nr_client == NULL)
524 		newnfs_connect(nmp, nrp, cred, td, 0);
525 
526 	/*
527 	 * For a client side mount, nmp is != NULL and clp == NULL. For
528 	 * server calls (callbacks or upcalls), nmp == NULL.
529 	 */
530 	if (clp != NULL) {
531 		NFSLOCKSTATE();
532 		if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
533 			secflavour = RPCSEC_GSS_KRB5;
534 			if (nd->nd_procnum != NFSPROC_NULL) {
535 				if (clp->lc_flags & LCL_GSSINTEGRITY)
536 					secflavour = RPCSEC_GSS_KRB5I;
537 				else if (clp->lc_flags & LCL_GSSPRIVACY)
538 					secflavour = RPCSEC_GSS_KRB5P;
539 			}
540 		}
541 		NFSUNLOCKSTATE();
542 	} else if (nmp != NULL && NFSHASKERB(nmp) &&
543 	     nd->nd_procnum != NFSPROC_NULL) {
544 		if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
545 			nd->nd_flag |= ND_USEGSSNAME;
546 		if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
547 			/*
548 			 * If there is a client side host based credential,
549 			 * use that, otherwise use the system uid, if set.
550 			 * The system uid is in the nmp->nm_sockreq.nr_cred
551 			 * credentials.
552 			 */
553 			if (nmp->nm_krbnamelen > 0) {
554 				usegssname = 1;
555 				clnt_principal = nmp->nm_krbname;
556 			} else if (nmp->nm_uid != (uid_t)-1) {
557 				KASSERT(nmp->nm_sockreq.nr_cred != NULL,
558 				    ("newnfs_request: NULL nr_cred"));
559 				crfree(authcred);
560 				authcred = crhold(nmp->nm_sockreq.nr_cred);
561 			}
562 		} else if (nmp->nm_krbnamelen == 0 &&
563 		    nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
564 			/*
565 			 * If there is no host based principal name and
566 			 * the system uid is set and this is root, use the
567 			 * system uid, since root won't have user
568 			 * credentials in a credentials cache file.
569 			 * The system uid is in the nmp->nm_sockreq.nr_cred
570 			 * credentials.
571 			 */
572 			KASSERT(nmp->nm_sockreq.nr_cred != NULL,
573 			    ("newnfs_request: NULL nr_cred"));
574 			crfree(authcred);
575 			authcred = crhold(nmp->nm_sockreq.nr_cred);
576 		}
577 		if (NFSHASINTEGRITY(nmp))
578 			secflavour = RPCSEC_GSS_KRB5I;
579 		else if (NFSHASPRIVACY(nmp))
580 			secflavour = RPCSEC_GSS_KRB5P;
581 		else
582 			secflavour = RPCSEC_GSS_KRB5;
583 		srv_principal = NFSMNT_SRVKRBNAME(nmp);
584 	} else if (nmp != NULL && !NFSHASKERB(nmp) &&
585 	    nd->nd_procnum != NFSPROC_NULL &&
586 	    (nd->nd_flag & ND_USEGSSNAME) != 0) {
587 		/*
588 		 * Use the uid that did the mount when the RPC is doing
589 		 * NFSv4 system operations, as indicated by the
590 		 * ND_USEGSSNAME flag, for the AUTH_SYS case.
591 		 * The credentials in nm_sockreq.nr_cred were used for the
592 		 * mount.
593 		 */
594 		KASSERT(nmp->nm_sockreq.nr_cred != NULL,
595 		    ("newnfs_request: NULL nr_cred"));
596 		crfree(authcred);
597 		authcred = crhold(nmp->nm_sockreq.nr_cred);
598 	}
599 
600 	if (nmp != NULL) {
601 		bzero(&nf, sizeof(struct nfs_feedback_arg));
602 		nf.nf_mount = nmp;
603 		nf.nf_td = td;
604 		nf.nf_lastmsg = NFSD_MONOSEC -
605 		    ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
606 	}
607 
608 	if (nd->nd_procnum == NFSPROC_NULL)
609 		auth = authnone_create();
610 	else if (usegssname) {
611 		/*
612 		 * For this case, the authenticator is held in the
613 		 * nfssockreq structure, so don't release the reference count
614 		 * held on it. --> Don't AUTH_DESTROY() it in this function.
615 		 */
616 		if (nrp->nr_auth == NULL)
617 			nrp->nr_auth = nfs_getauth(nrp, secflavour,
618 			    clnt_principal, srv_principal, NULL, authcred);
619 		else
620 			rpc_gss_refresh_auth_call(nrp->nr_auth);
621 		auth = nrp->nr_auth;
622 	} else
623 		auth = nfs_getauth(nrp, secflavour, NULL,
624 		    srv_principal, NULL, authcred);
625 	crfree(authcred);
626 	if (auth == NULL) {
627 		m_freem(nd->nd_mreq);
628 		if (set_sigset)
629 			newnfs_restore_sigmask(td, &oldset);
630 		return (EACCES);
631 	}
632 	bzero(&ext, sizeof(ext));
633 	ext.rc_auth = auth;
634 	if (nmp != NULL) {
635 		ext.rc_feedback = nfs_feedback;
636 		ext.rc_feedback_arg = &nf;
637 	}
638 
639 	procnum = nd->nd_procnum;
640 	if ((nd->nd_flag & ND_NFSV4) &&
641 	    nd->nd_procnum != NFSPROC_NULL &&
642 	    nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
643 		procnum = NFSV4PROC_COMPOUND;
644 
645 	if (nmp != NULL) {
646 		NFSINCRGLOBAL(newnfsstats.rpcrequests);
647 
648 		/* Map the procnum to the old NFSv2 one, as required. */
649 		if ((nd->nd_flag & ND_NFSV2) != 0) {
650 			if (nd->nd_procnum < NFS_V3NPROCS)
651 				procnum = nfsv2_procid[nd->nd_procnum];
652 			else
653 				procnum = NFSV2PROC_NOOP;
654 		}
655 
656 		/*
657 		 * Now only used for the R_DONTRECOVER case, but until that is
658 		 * supported within the krpc code, I need to keep a queue of
659 		 * outstanding RPCs for nfsv4 client requests.
660 		 */
661 		if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
662 			MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
663 			    M_NFSDREQ, M_WAITOK);
664 #ifdef KDTRACE_HOOKS
665 		if (dtrace_nfscl_nfs234_start_probe != NULL) {
666 			uint32_t probe_id;
667 			int probe_procnum;
668 
669 			if (nd->nd_flag & ND_NFSV4) {
670 				probe_id =
671 				    nfscl_nfs4_start_probes[nd->nd_procnum];
672 				probe_procnum = nd->nd_procnum;
673 			} else if (nd->nd_flag & ND_NFSV3) {
674 				probe_id = nfscl_nfs3_start_probes[procnum];
675 				probe_procnum = procnum;
676 			} else {
677 				probe_id =
678 				    nfscl_nfs2_start_probes[nd->nd_procnum];
679 				probe_procnum = procnum;
680 			}
681 			if (probe_id != 0)
682 				(dtrace_nfscl_nfs234_start_probe)
683 				    (probe_id, vp, nd->nd_mreq, cred,
684 				     probe_procnum);
685 		}
686 #endif
687 	}
688 	trycnt = 0;
689 	freeslot = -1;		/* Set to slot that needs to be free'd */
690 tryagain:
691 	slot = -1;		/* Slot that needs a sequence# increment. */
692 	/*
693 	 * This timeout specifies when a new socket should be created,
694 	 * along with new xid values. For UDP, this should be done
695 	 * infrequently, since retransmits of RPC requests should normally
696 	 * use the same xid.
697 	 */
698 	if (nmp == NULL) {
699 		timo.tv_usec = 0;
700 		if (clp == NULL)
701 			timo.tv_sec = NFSV4_UPCALLTIMEO;
702 		else
703 			timo.tv_sec = NFSV4_CALLBACKTIMEO;
704 	} else {
705 		if (nrp->nr_sotype != SOCK_DGRAM) {
706 			timo.tv_usec = 0;
707 			if ((nmp->nm_flag & NFSMNT_NFSV4))
708 				timo.tv_sec = INT_MAX;
709 			else
710 				timo.tv_sec = NFS_TCPTIMEO;
711 		} else {
712 			if (NFSHASSOFT(nmp)) {
713 				/*
714 				 * CLSET_RETRIES is set to 2, so this should be
715 				 * half of the total timeout required.
716 				 */
717 				timeo = nmp->nm_retry * nmp->nm_timeo / 2;
718 				if (timeo < 1)
719 					timeo = 1;
720 				timo.tv_sec = timeo / NFS_HZ;
721 				timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
722 				    NFS_HZ;
723 			} else {
724 				/* For UDP hard mounts, use a large value. */
725 				timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
726 				timo.tv_usec = 0;
727 			}
728 		}
729 
730 		if (rep != NULL) {
731 			rep->r_flags = 0;
732 			rep->r_nmp = nmp;
733 			/*
734 			 * Chain request into list of outstanding requests.
735 			 */
736 			NFSLOCKREQ();
737 			TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
738 			NFSUNLOCKREQ();
739 		}
740 	}
741 
742 	nd->nd_mrep = NULL;
743 	stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
744 	    &nd->nd_mrep, timo);
745 
746 	if (rep != NULL) {
747 		/*
748 		 * RPC done, unlink the request.
749 		 */
750 		NFSLOCKREQ();
751 		TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
752 		NFSUNLOCKREQ();
753 	}
754 
755 	/*
756 	 * If there was a successful reply and a tprintf msg.
757 	 * tprintf a response.
758 	 */
759 	if (stat == RPC_SUCCESS) {
760 		error = 0;
761 	} else if (stat == RPC_TIMEDOUT) {
762 		NFSINCRGLOBAL(newnfsstats.rpctimeouts);
763 		error = ETIMEDOUT;
764 	} else if (stat == RPC_VERSMISMATCH) {
765 		NFSINCRGLOBAL(newnfsstats.rpcinvalid);
766 		error = EOPNOTSUPP;
767 	} else if (stat == RPC_PROGVERSMISMATCH) {
768 		NFSINCRGLOBAL(newnfsstats.rpcinvalid);
769 		error = EPROTONOSUPPORT;
770 	} else if (stat == RPC_INTR) {
771 		error = EINTR;
772 	} else {
773 		NFSINCRGLOBAL(newnfsstats.rpcinvalid);
774 		error = EACCES;
775 	}
776 	if (error) {
777 		m_freem(nd->nd_mreq);
778 		if (usegssname == 0)
779 			AUTH_DESTROY(auth);
780 		if (rep != NULL)
781 			FREE((caddr_t)rep, M_NFSDREQ);
782 		if (set_sigset)
783 			newnfs_restore_sigmask(td, &oldset);
784 		return (error);
785 	}
786 
787 	KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
788 
789 	/*
790 	 * Search for any mbufs that are not a multiple of 4 bytes long
791 	 * or with m_data not longword aligned.
792 	 * These could cause pointer alignment problems, so copy them to
793 	 * well aligned mbufs.
794 	 */
795 	newnfs_realign(&nd->nd_mrep, M_WAITOK);
796 	nd->nd_md = nd->nd_mrep;
797 	nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
798 	nd->nd_repstat = 0;
799 	if (nd->nd_procnum != NFSPROC_NULL) {
800 		/* If sep == NULL, set it to the default in nmp. */
801 		if (sep == NULL && nmp != NULL)
802 			sep = NFSMNT_MDSSESSION(nmp);
803 		/*
804 		 * and now the actual NFS xdr.
805 		 */
806 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
807 		nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
808 		if (nd->nd_repstat >= 10000)
809 			NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
810 			    (int)nd->nd_repstat);
811 
812 		/*
813 		 * Get rid of the tag, return count and SEQUENCE result for
814 		 * NFSv4.
815 		 */
816 		if ((nd->nd_flag & ND_NFSV4) != 0) {
817 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
818 			i = fxdr_unsigned(int, *tl);
819 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
820 			if (error)
821 				goto nfsmout;
822 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
823 			opcnt = fxdr_unsigned(int, *tl++);
824 			i = fxdr_unsigned(int, *tl++);
825 			j = fxdr_unsigned(int, *tl);
826 			if (j >= 10000)
827 				NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
828 			/*
829 			 * If the first op is Sequence, free up the slot.
830 			 */
831 			if (nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0)
832 				NFSCL_DEBUG(1, "failed seq=%d\n", j);
833 			if (nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) {
834 				NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
835 				    5 * NFSX_UNSIGNED);
836 				mtx_lock(&sep->nfsess_mtx);
837 				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
838 				retseq = fxdr_unsigned(uint32_t, *tl++);
839 				slot = fxdr_unsigned(int, *tl++);
840 				freeslot = slot;
841 				if (retseq != sep->nfsess_slotseq[slot])
842 					printf("retseq diff 0x%x\n", retseq);
843 				retval = fxdr_unsigned(uint32_t, *++tl);
844 				if ((retval + 1) < sep->nfsess_foreslots)
845 					sep->nfsess_foreslots = (retval + 1);
846 				else if ((retval + 1) > sep->nfsess_foreslots)
847 					sep->nfsess_foreslots = (retval < 64) ?
848 					    (retval + 1) : 64;
849 				mtx_unlock(&sep->nfsess_mtx);
850 
851 				/* Grab the op and status for the next one. */
852 				if (opcnt > 1) {
853 					NFSM_DISSECT(tl, uint32_t *,
854 					    2 * NFSX_UNSIGNED);
855 					i = fxdr_unsigned(int, *tl++);
856 					j = fxdr_unsigned(int, *tl);
857 				}
858 			}
859 		}
860 		if (nd->nd_repstat != 0) {
861 			if (((nd->nd_repstat == NFSERR_DELAY ||
862 			      nd->nd_repstat == NFSERR_GRACE) &&
863 			     (nd->nd_flag & ND_NFSV4) &&
864 			     nd->nd_procnum != NFSPROC_DELEGRETURN &&
865 			     nd->nd_procnum != NFSPROC_SETATTR &&
866 			     nd->nd_procnum != NFSPROC_READ &&
867 			     nd->nd_procnum != NFSPROC_READDS &&
868 			     nd->nd_procnum != NFSPROC_WRITE &&
869 			     nd->nd_procnum != NFSPROC_WRITEDS &&
870 			     nd->nd_procnum != NFSPROC_OPEN &&
871 			     nd->nd_procnum != NFSPROC_CREATE &&
872 			     nd->nd_procnum != NFSPROC_OPENCONFIRM &&
873 			     nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
874 			     nd->nd_procnum != NFSPROC_CLOSE &&
875 			     nd->nd_procnum != NFSPROC_LOCK &&
876 			     nd->nd_procnum != NFSPROC_LOCKU) ||
877 			    (nd->nd_repstat == NFSERR_DELAY &&
878 			     (nd->nd_flag & ND_NFSV4) == 0) ||
879 			    nd->nd_repstat == NFSERR_RESOURCE) {
880 				if (trylater_delay > NFS_TRYLATERDEL)
881 					trylater_delay = NFS_TRYLATERDEL;
882 				waituntil = NFSD_MONOSEC + trylater_delay;
883 				while (NFSD_MONOSEC < waituntil)
884 					(void) nfs_catnap(PZERO, 0, "nfstry");
885 				trylater_delay *= 2;
886 				if (slot != -1) {
887 					mtx_lock(&sep->nfsess_mtx);
888 					sep->nfsess_slotseq[slot]++;
889 					*nd->nd_slotseq = txdr_unsigned(
890 					    sep->nfsess_slotseq[slot]);
891 					mtx_unlock(&sep->nfsess_mtx);
892 				}
893 				m_freem(nd->nd_mrep);
894 				nd->nd_mrep = NULL;
895 				goto tryagain;
896 			}
897 
898 			/*
899 			 * If the File Handle was stale, invalidate the
900 			 * lookup cache, just in case.
901 			 * (vp != NULL implies a client side call)
902 			 */
903 			if (nd->nd_repstat == ESTALE && vp != NULL) {
904 				cache_purge(vp);
905 				if (ncl_call_invalcaches != NULL)
906 					(*ncl_call_invalcaches)(vp);
907 			}
908 		}
909 		if ((nd->nd_flag & ND_NFSV4) != 0) {
910 			/* Free the slot, as required. */
911 			if (freeslot != -1)
912 				nfsv4_freeslot(sep, freeslot);
913 			/*
914 			 * If this op is Putfh, throw its results away.
915 			 */
916 			if (j >= 10000)
917 				NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
918 			if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
919 				NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
920 				i = fxdr_unsigned(int, *tl++);
921 				j = fxdr_unsigned(int, *tl);
922 				if (j >= 10000)
923 					NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
924 					    j);
925 				/*
926 				 * All Compounds that do an Op that must
927 				 * be in sequence consist of NFSV4OP_PUTFH
928 				 * followed by one of these. As such, we
929 				 * can determine if the seqid# should be
930 				 * incremented, here.
931 				 */
932 				if ((i == NFSV4OP_OPEN ||
933 				     i == NFSV4OP_OPENCONFIRM ||
934 				     i == NFSV4OP_OPENDOWNGRADE ||
935 				     i == NFSV4OP_CLOSE ||
936 				     i == NFSV4OP_LOCK ||
937 				     i == NFSV4OP_LOCKU) &&
938 				    (j == 0 ||
939 				     (j != NFSERR_STALECLIENTID &&
940 				      j != NFSERR_STALESTATEID &&
941 				      j != NFSERR_BADSTATEID &&
942 				      j != NFSERR_BADSEQID &&
943 				      j != NFSERR_BADXDR &&
944 				      j != NFSERR_RESOURCE &&
945 				      j != NFSERR_NOFILEHANDLE)))
946 					nd->nd_flag |= ND_INCRSEQID;
947 			}
948 			/*
949 			 * If this op's status is non-zero, mark
950 			 * that there is no more data to process.
951 			 */
952 			if (j)
953 				nd->nd_flag |= ND_NOMOREDATA;
954 
955 			/*
956 			 * If R_DONTRECOVER is set, replace the stale error
957 			 * reply, so that recovery isn't initiated.
958 			 */
959 			if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
960 			     nd->nd_repstat == NFSERR_BADSESSION ||
961 			     nd->nd_repstat == NFSERR_STALESTATEID) &&
962 			    rep != NULL && (rep->r_flags & R_DONTRECOVER))
963 				nd->nd_repstat = NFSERR_STALEDONTRECOVER;
964 		}
965 	}
966 
967 #ifdef KDTRACE_HOOKS
968 	if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
969 		uint32_t probe_id;
970 		int probe_procnum;
971 
972 		if (nd->nd_flag & ND_NFSV4) {
973 			probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
974 			probe_procnum = nd->nd_procnum;
975 		} else if (nd->nd_flag & ND_NFSV3) {
976 			probe_id = nfscl_nfs3_done_probes[procnum];
977 			probe_procnum = procnum;
978 		} else {
979 			probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
980 			probe_procnum = procnum;
981 		}
982 		if (probe_id != 0)
983 			(dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
984 			    nd->nd_mreq, cred, probe_procnum, 0);
985 	}
986 #endif
987 
988 	m_freem(nd->nd_mreq);
989 	if (usegssname == 0)
990 		AUTH_DESTROY(auth);
991 	if (rep != NULL)
992 		FREE((caddr_t)rep, M_NFSDREQ);
993 	if (set_sigset)
994 		newnfs_restore_sigmask(td, &oldset);
995 	return (0);
996 nfsmout:
997 	mbuf_freem(nd->nd_mrep);
998 	mbuf_freem(nd->nd_mreq);
999 	if (usegssname == 0)
1000 		AUTH_DESTROY(auth);
1001 	if (rep != NULL)
1002 		FREE((caddr_t)rep, M_NFSDREQ);
1003 	if (set_sigset)
1004 		newnfs_restore_sigmask(td, &oldset);
1005 	return (error);
1006 }
1007 
1008 /*
1009  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1010  * wait for all requests to complete. This is used by forced unmounts
1011  * to terminate any outstanding RPCs.
1012  */
1013 int
newnfs_nmcancelreqs(struct nfsmount * nmp)1014 newnfs_nmcancelreqs(struct nfsmount *nmp)
1015 {
1016 
1017 	if (nmp->nm_sockreq.nr_client != NULL)
1018 		CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1019 	return (0);
1020 }
1021 
1022 /*
1023  * Any signal that can interrupt an NFS operation in an intr mount
1024  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1025  */
1026 int newnfs_sig_set[] = {
1027 	SIGINT,
1028 	SIGTERM,
1029 	SIGHUP,
1030 	SIGKILL,
1031 	SIGQUIT
1032 };
1033 
1034 /*
1035  * Check to see if one of the signals in our subset is pending on
1036  * the process (in an intr mount).
1037  */
1038 static int
nfs_sig_pending(sigset_t set)1039 nfs_sig_pending(sigset_t set)
1040 {
1041 	int i;
1042 
1043 	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
1044 		if (SIGISMEMBER(set, newnfs_sig_set[i]))
1045 			return (1);
1046 	return (0);
1047 }
1048 
1049 /*
1050  * The set/restore sigmask functions are used to (temporarily) overwrite
1051  * the thread td_sigmask during an RPC call (for example). These are also
1052  * used in other places in the NFS client that might tsleep().
1053  */
1054 void
newnfs_set_sigmask(struct thread * td,sigset_t * oldset)1055 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1056 {
1057 	sigset_t newset;
1058 	int i;
1059 	struct proc *p;
1060 
1061 	SIGFILLSET(newset);
1062 	if (td == NULL)
1063 		td = curthread; /* XXX */
1064 	p = td->td_proc;
1065 	/* Remove the NFS set of signals from newset */
1066 	PROC_LOCK(p);
1067 	mtx_lock(&p->p_sigacts->ps_mtx);
1068 	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
1069 		/*
1070 		 * But make sure we leave the ones already masked
1071 		 * by the process, ie. remove the signal from the
1072 		 * temporary signalmask only if it wasn't already
1073 		 * in p_sigmask.
1074 		 */
1075 		if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1076 		    !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1077 			SIGDELSET(newset, newnfs_sig_set[i]);
1078 	}
1079 	mtx_unlock(&p->p_sigacts->ps_mtx);
1080 	kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
1081 	    SIGPROCMASK_PROC_LOCKED);
1082 	PROC_UNLOCK(p);
1083 }
1084 
1085 void
newnfs_restore_sigmask(struct thread * td,sigset_t * set)1086 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1087 {
1088 	if (td == NULL)
1089 		td = curthread; /* XXX */
1090 	kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1091 }
1092 
1093 /*
1094  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1095  * old one after msleep() returns.
1096  */
1097 int
newnfs_msleep(struct thread * td,void * ident,struct mtx * mtx,int priority,char * wmesg,int timo)1098 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1099 {
1100 	sigset_t oldset;
1101 	int error;
1102 	struct proc *p;
1103 
1104 	if ((priority & PCATCH) == 0)
1105 		return msleep(ident, mtx, priority, wmesg, timo);
1106 	if (td == NULL)
1107 		td = curthread; /* XXX */
1108 	newnfs_set_sigmask(td, &oldset);
1109 	error = msleep(ident, mtx, priority, wmesg, timo);
1110 	newnfs_restore_sigmask(td, &oldset);
1111 	p = td->td_proc;
1112 	return (error);
1113 }
1114 
1115 /*
1116  * Test for a termination condition pending on the process.
1117  * This is used for NFSMNT_INT mounts.
1118  */
1119 int
newnfs_sigintr(struct nfsmount * nmp,struct thread * td)1120 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1121 {
1122 	struct proc *p;
1123 	sigset_t tmpset;
1124 
1125 	/* Terminate all requests while attempting a forced unmount. */
1126 	if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1127 		return (EIO);
1128 	if (!(nmp->nm_flag & NFSMNT_INT))
1129 		return (0);
1130 	if (td == NULL)
1131 		return (0);
1132 	p = td->td_proc;
1133 	PROC_LOCK(p);
1134 	tmpset = p->p_siglist;
1135 	SIGSETOR(tmpset, td->td_siglist);
1136 	SIGSETNAND(tmpset, td->td_sigmask);
1137 	mtx_lock(&p->p_sigacts->ps_mtx);
1138 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1139 	mtx_unlock(&p->p_sigacts->ps_mtx);
1140 	if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1141 	    && nfs_sig_pending(tmpset)) {
1142 		PROC_UNLOCK(p);
1143 		return (EINTR);
1144 	}
1145 	PROC_UNLOCK(p);
1146 	return (0);
1147 }
1148 
1149 static int
nfs_msg(struct thread * td,const char * server,const char * msg,int error)1150 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1151 {
1152 	struct proc *p;
1153 
1154 	p = td ? td->td_proc : NULL;
1155 	if (error) {
1156 		tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
1157 		    server, msg, error);
1158 	} else {
1159 		tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
1160 	}
1161 	return (0);
1162 }
1163 
1164 static void
nfs_down(struct nfsmount * nmp,struct thread * td,const char * msg,int error,int flags)1165 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1166     int error, int flags)
1167 {
1168 	if (nmp == NULL)
1169 		return;
1170 	mtx_lock(&nmp->nm_mtx);
1171 	if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1172 		nmp->nm_state |= NFSSTA_TIMEO;
1173 		mtx_unlock(&nmp->nm_mtx);
1174 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1175 		    VQ_NOTRESP, 0);
1176 	} else
1177 		mtx_unlock(&nmp->nm_mtx);
1178 	mtx_lock(&nmp->nm_mtx);
1179 	if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1180 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
1181 		mtx_unlock(&nmp->nm_mtx);
1182 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1183 		    VQ_NOTRESPLOCK, 0);
1184 	} else
1185 		mtx_unlock(&nmp->nm_mtx);
1186 	nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1187 }
1188 
1189 static void
nfs_up(struct nfsmount * nmp,struct thread * td,const char * msg,int flags,int tprintfmsg)1190 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1191     int flags, int tprintfmsg)
1192 {
1193 	if (nmp == NULL)
1194 		return;
1195 	if (tprintfmsg) {
1196 		nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1197 	}
1198 
1199 	mtx_lock(&nmp->nm_mtx);
1200 	if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1201 		nmp->nm_state &= ~NFSSTA_TIMEO;
1202 		mtx_unlock(&nmp->nm_mtx);
1203 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1204 		    VQ_NOTRESP, 1);
1205 	} else
1206 		mtx_unlock(&nmp->nm_mtx);
1207 
1208 	mtx_lock(&nmp->nm_mtx);
1209 	if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1210 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1211 		mtx_unlock(&nmp->nm_mtx);
1212 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1213 		    VQ_NOTRESPLOCK, 1);
1214 	} else
1215 		mtx_unlock(&nmp->nm_mtx);
1216 }
1217 
1218