xref: /freebsd/sys/fs/nfs/nfs_commonkrpc.c (revision 15e8331f0e4acf4ad9f101dfad62e71101c881d5)
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_inet6.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/rpcclnt.h>
63 
64 #include <kgssapi/krb5/kcrypto.h>
65 
66 #include <fs/nfs/nfsport.h>
67 
68 NFSSTATESPINLOCK;
69 NFSREQSPINLOCK;
70 extern struct nfsstats newnfsstats;
71 extern struct nfsreqhead nfsd_reqq;
72 extern int nfscl_ticks;
73 extern void (*ncl_call_invalcaches)(struct vnode *);
74 
75 static int	nfsrv_gsscallbackson = 0;
76 static int	nfs_bufpackets = 4;
77 static int	nfs_reconnects;
78 static int	nfs3_jukebox_delay = 10;
79 static int	nfs_skip_wcc_data_onerr = 1;
80 static int	nfs_keytab_enctype = ETYPE_DES_CBC_CRC;
81 
82 SYSCTL_DECL(_vfs_newnfs);
83 
84 SYSCTL_INT(_vfs_newnfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
85     "Buffer reservation size 2 < x < 64");
86 SYSCTL_INT(_vfs_newnfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
87     "Number of times the nfs client has had to reconnect");
88 SYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
89     "Number of seconds to delay a retry after receiving EJUKEBOX");
90 SYSCTL_INT(_vfs_newnfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
91     "Disable weak cache consistency checking when server returns an error");
92 SYSCTL_INT(_vfs_newnfs, OID_AUTO, keytab_enctype, CTLFLAG_RW, &nfs_keytab_enctype, 0,
93     "Encryption type for the keytab entry used by nfs");
94 
95 static void	nfs_down(struct nfsmount *, struct thread *, const char *,
96     int, int);
97 static void	nfs_up(struct nfsmount *, struct thread *, const char *,
98     int, int);
99 static int	nfs_msg(struct thread *, const char *, const char *, int);
100 
101 extern int nfsv2_procid[];
102 
103 struct nfs_cached_auth {
104 	int		ca_refs; /* refcount, including 1 from the cache */
105 	uid_t		ca_uid;	 /* uid that corresponds to this auth */
106 	AUTH		*ca_auth; /* RPC auth handle */
107 };
108 
109 /*
110  * Initialize sockets and congestion for a new NFS connection.
111  * We do not free the sockaddr if error.
112  */
113 int
114 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
115     struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
116 {
117 	int rcvreserve, sndreserve;
118 	int pktscale;
119 	struct sockaddr *saddr;
120 	struct ucred *origcred;
121 	CLIENT *client;
122 	struct netconfig *nconf;
123 	struct socket *so;
124 	int one = 1, retries, error, printsbmax = 0;
125 	struct thread *td = curthread;
126 
127 	/*
128 	 * We need to establish the socket using the credentials of
129 	 * the mountpoint.  Some parts of this process (such as
130 	 * sobind() and soconnect()) will use the curent thread's
131 	 * credential instead of the socket credential.  To work
132 	 * around this, temporarily change the current thread's
133 	 * credential to that of the mountpoint.
134 	 *
135 	 * XXX: It would be better to explicitly pass the correct
136 	 * credential to sobind() and soconnect().
137 	 */
138 	origcred = td->td_ucred;
139 
140 	/*
141 	 * Use the credential in nr_cred, if not NULL.
142 	 */
143 	if (nrp->nr_cred != NULL)
144 		td->td_ucred = nrp->nr_cred;
145 	else
146 		td->td_ucred = cred;
147 	saddr = nrp->nr_nam;
148 
149 	if (saddr->sa_family == AF_INET)
150 		if (nrp->nr_sotype == SOCK_DGRAM)
151 			nconf = getnetconfigent("udp");
152 		else
153 			nconf = getnetconfigent("tcp");
154 	else
155 		if (nrp->nr_sotype == SOCK_DGRAM)
156 			nconf = getnetconfigent("udp6");
157 		else
158 			nconf = getnetconfigent("tcp6");
159 
160 	pktscale = nfs_bufpackets;
161 	if (pktscale < 2)
162 		pktscale = 2;
163 	if (pktscale > 64)
164 		pktscale = 64;
165 	/*
166 	 * soreserve() can fail if sb_max is too small, so shrink pktscale
167 	 * and try again if there is an error.
168 	 * Print a log message suggesting increasing sb_max.
169 	 * Creating a socket and doing this is necessary since, if the
170 	 * reservation sizes are too large and will make soreserve() fail,
171 	 * the connection will work until a large send is attempted and
172 	 * then it will loop in the krpc code.
173 	 */
174 	so = NULL;
175 	saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
176 	error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
177 	    nrp->nr_soproto, td->td_ucred, td);
178 	if (error) {
179 		td->td_ucred = origcred;
180 		return (error);
181 	}
182 	do {
183 	    if (error != 0 && pktscale > 2) {
184 		pktscale--;
185 		if (printsbmax == 0) {
186 		    printf("nfscl: consider increasing kern.ipc.maxsockbuf\n");
187 		    printsbmax = 1;
188 		}
189 	    }
190 	    if (nrp->nr_sotype == SOCK_DGRAM) {
191 		if (nmp != NULL) {
192 			sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
193 			    pktscale;
194 			rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
195 			    pktscale;
196 		} else {
197 			sndreserve = rcvreserve = 1024 * pktscale;
198 		}
199 	    } else {
200 		if (nrp->nr_sotype != SOCK_STREAM)
201 			panic("nfscon sotype");
202 		if (nmp != NULL) {
203 			sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
204 			    sizeof (u_int32_t)) * pktscale;
205 			rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
206 			    sizeof (u_int32_t)) * pktscale;
207 		} else {
208 			sndreserve = rcvreserve = 1024 * pktscale;
209 		}
210 	    }
211 	    error = soreserve(so, sndreserve, rcvreserve);
212 	} while (error != 0 && pktscale > 2);
213 	soclose(so);
214 	if (error) {
215 		td->td_ucred = origcred;
216 		return (error);
217 	}
218 
219 	client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
220 	    nrp->nr_vers, sndreserve, rcvreserve);
221 	CLNT_CONTROL(client, CLSET_WAITCHAN, "newnfsreq");
222 	if (nmp != NULL) {
223 		if ((nmp->nm_flag & NFSMNT_INT))
224 			CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
225 		if ((nmp->nm_flag & NFSMNT_RESVPORT))
226 			CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
227 		if (NFSHASSOFT(nmp))
228 			retries = nmp->nm_retry;
229 		else
230 			retries = INT_MAX;
231 	} else {
232 		/*
233 		 * Three cases:
234 		 * - Null RPC callback to client
235 		 * - Non-Null RPC callback to client, wait a little longer
236 		 * - upcalls to nfsuserd and gssd (clp == NULL)
237 		 */
238 		if (callback_retry_mult == 0) {
239 			retries = NFSV4_UPCALLRETRY;
240 			CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
241 		} else {
242 			retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
243 		}
244 	}
245 	CLNT_CONTROL(client, CLSET_RETRIES, &retries);
246 
247 	mtx_lock(&nrp->nr_mtx);
248 	if (nrp->nr_client != NULL) {
249 		/*
250 		 * Someone else already connected.
251 		 */
252 		CLNT_RELEASE(client);
253 	} else {
254 		nrp->nr_client = client;
255 	}
256 
257 	/*
258 	 * Protocols that do not require connections may be optionally left
259 	 * unconnected for servers that reply from a port other than NFS_PORT.
260 	 */
261 	if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
262 		mtx_unlock(&nrp->nr_mtx);
263 		CLNT_CONTROL(client, CLSET_CONNECT, &one);
264 	} else {
265 		mtx_unlock(&nrp->nr_mtx);
266 	}
267 
268 	/* Restore current thread's credentials. */
269 	td->td_ucred = origcred;
270 	return (0);
271 }
272 
273 /*
274  * NFS disconnect. Clean up and unlink.
275  */
276 void
277 newnfs_disconnect(struct nfssockreq *nrp)
278 {
279 	CLIENT *client;
280 
281 	mtx_lock(&nrp->nr_mtx);
282 	if (nrp->nr_client != NULL) {
283 		client = nrp->nr_client;
284 		nrp->nr_client = NULL;
285 		mtx_unlock(&nrp->nr_mtx);
286 #ifdef KGSSAPI
287 		rpc_gss_secpurge(client);
288 #endif
289 		CLNT_CLOSE(client);
290 		CLNT_RELEASE(client);
291 	} else {
292 		mtx_unlock(&nrp->nr_mtx);
293 	}
294 }
295 
296 static AUTH *
297 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
298     char *srv_principal, gss_OID mech_oid, struct ucred *cred)
299 {
300 #ifdef KGSSAPI
301 	rpc_gss_service_t svc;
302 	AUTH *auth;
303 	rpc_gss_options_req_t req_options;
304 #endif
305 
306 	switch (secflavour) {
307 #ifdef KGSSAPI
308 	case RPCSEC_GSS_KRB5:
309 	case RPCSEC_GSS_KRB5I:
310 	case RPCSEC_GSS_KRB5P:
311 		if (!mech_oid) {
312 			if (!rpc_gss_mech_to_oid("kerberosv5", &mech_oid))
313 				return (NULL);
314 		}
315 		if (secflavour == RPCSEC_GSS_KRB5)
316 			svc = rpc_gss_svc_none;
317 		else if (secflavour == RPCSEC_GSS_KRB5I)
318 			svc = rpc_gss_svc_integrity;
319 		else
320 			svc = rpc_gss_svc_privacy;
321 		req_options.req_flags = GSS_C_MUTUAL_FLAG;
322 		req_options.time_req = 0;
323 		req_options.my_cred = GSS_C_NO_CREDENTIAL;
324 		req_options.input_channel_bindings = NULL;
325 		req_options.enc_type = nfs_keytab_enctype;
326 
327 		auth = rpc_gss_secfind(nrp->nr_client, cred,
328 		    clnt_principal, srv_principal, mech_oid, svc,
329 		    &req_options);
330 		return (auth);
331 #endif
332 	case AUTH_SYS:
333 	default:
334 		return (authunix_create(cred));
335 
336 	}
337 }
338 
339 /*
340  * Callback from the RPC code to generate up/down notifications.
341  */
342 
343 struct nfs_feedback_arg {
344 	struct nfsmount *nf_mount;
345 	int		nf_lastmsg;	/* last tprintf */
346 	int		nf_tprintfmsg;
347 	struct thread	*nf_td;
348 };
349 
350 static void
351 nfs_feedback(int type, int proc, void *arg)
352 {
353 	struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
354 	struct nfsmount *nmp = nf->nf_mount;
355 	struct timeval now;
356 
357 	getmicrouptime(&now);
358 
359 	switch (type) {
360 	case FEEDBACK_REXMIT2:
361 	case FEEDBACK_RECONNECT:
362 		if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
363 			nfs_down(nmp, nf->nf_td,
364 			    "not responding", 0, NFSSTA_TIMEO);
365 			nf->nf_tprintfmsg = TRUE;
366 			nf->nf_lastmsg = now.tv_sec;
367 		}
368 		break;
369 
370 	case FEEDBACK_OK:
371 		nfs_up(nf->nf_mount, nf->nf_td,
372 		    "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
373 		break;
374 	}
375 }
376 
377 /*
378  * newnfs_request - goes something like this
379  *	- does the rpc by calling the krpc layer
380  *	- break down rpc header and return with nfs reply
381  * nb: always frees up nd_mreq mbuf list
382  */
383 int
384 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
385     struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
386     struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
387     u_char *retsum, int toplevel, u_int64_t *xidp)
388 {
389 	u_int32_t *tl;
390 	time_t waituntil;
391 	int i, j;
392 	int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
393 	u_int16_t procnum;
394 	u_int trylater_delay = 1;
395 	struct nfs_feedback_arg nf;
396 	struct timeval timo, now;
397 	AUTH *auth;
398 	struct rpc_callextra ext;
399 	enum clnt_stat stat;
400 	struct nfsreq *rep = NULL;
401 	char *srv_principal = NULL;
402 
403 	if (xidp != NULL)
404 		*xidp = 0;
405 	/* Reject requests while attempting a forced unmount. */
406 	if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
407 		m_freem(nd->nd_mreq);
408 		return (ESTALE);
409 	}
410 
411 	/*
412 	 * For a client side mount, nmp is != NULL and clp == NULL. For
413 	 * server calls (callbacks or upcalls), nmp == NULL.
414 	 */
415 	if (clp != NULL) {
416 		NFSLOCKSTATE();
417 		if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
418 			secflavour = RPCSEC_GSS_KRB5;
419 			if (nd->nd_procnum != NFSPROC_NULL) {
420 				if (clp->lc_flags & LCL_GSSINTEGRITY)
421 					secflavour = RPCSEC_GSS_KRB5I;
422 				else if (clp->lc_flags & LCL_GSSPRIVACY)
423 					secflavour = RPCSEC_GSS_KRB5P;
424 			}
425 		}
426 		NFSUNLOCKSTATE();
427 	} else if (nmp != NULL && NFSHASKERB(nmp) &&
428 	     nd->nd_procnum != NFSPROC_NULL) {
429 		if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
430 			nd->nd_flag |= ND_USEGSSNAME;
431 		if ((nd->nd_flag & ND_USEGSSNAME) && nmp->nm_krbnamelen > 0)
432 			usegssname = 1;
433 		if (NFSHASINTEGRITY(nmp))
434 			secflavour = RPCSEC_GSS_KRB5I;
435 		else if (NFSHASPRIVACY(nmp))
436 			secflavour = RPCSEC_GSS_KRB5P;
437 		else
438 			secflavour = RPCSEC_GSS_KRB5;
439 		srv_principal = NFSMNT_SRVKRBNAME(nmp);
440 	}
441 
442 	if (nmp != NULL) {
443 		bzero(&nf, sizeof(struct nfs_feedback_arg));
444 		nf.nf_mount = nmp;
445 		nf.nf_td = td;
446 		getmicrouptime(&now);
447 		nf.nf_lastmsg = now.tv_sec -
448 		    ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
449 	}
450 
451 	/*
452 	 * XXX if not already connected call nfs_connect now. Longer
453 	 * term, change nfs_mount to call nfs_connect unconditionally
454 	 * and let clnt_reconnect_create handle reconnects.
455 	 */
456 	if (nrp->nr_client == NULL)
457 		newnfs_connect(nmp, nrp, cred, td, 0);
458 
459 	if (nd->nd_procnum == NFSPROC_NULL)
460 		auth = authnone_create();
461 	else if (usegssname)
462 		auth = nfs_getauth(nrp, secflavour, nmp->nm_krbname,
463 		    srv_principal, NULL, cred);
464 	else
465 		auth = nfs_getauth(nrp, secflavour, NULL,
466 		    srv_principal, NULL, cred);
467 	if (auth == NULL) {
468 		m_freem(nd->nd_mreq);
469 		return (EACCES);
470 	}
471 	bzero(&ext, sizeof(ext));
472 	ext.rc_auth = auth;
473 	if (nmp != NULL) {
474 		ext.rc_feedback = nfs_feedback;
475 		ext.rc_feedback_arg = &nf;
476 	}
477 
478 	procnum = nd->nd_procnum;
479 	if ((nd->nd_flag & ND_NFSV4) &&
480 	    nd->nd_procnum != NFSPROC_NULL &&
481 	    nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
482 		procnum = NFSV4PROC_COMPOUND;
483 
484 	if (nmp != NULL) {
485 		NFSINCRGLOBAL(newnfsstats.rpcrequests);
486 		/*
487 		 * Now only used for the R_DONTRECOVER case, but until that is
488 		 * supported within the krpc code, I need to keep a queue of
489 		 * outstanding RPCs for nfsv4 client requests.
490 		 */
491 		if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
492 			MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
493 			    M_NFSDREQ, M_WAITOK);
494 	}
495 	trycnt = 0;
496 tryagain:
497 	if (nmp == NULL) {
498 		timo.tv_usec = 0;
499 		if (clp == NULL)
500 			timo.tv_sec = NFSV4_UPCALLTIMEO;
501 		else
502 			timo.tv_sec = NFSV4_CALLBACKTIMEO;
503 	} else {
504 		if (nrp->nr_sotype != SOCK_DGRAM) {
505 			timo.tv_usec = 0;
506 			if ((nmp->nm_flag & NFSMNT_NFSV4))
507 				timo.tv_sec = INT_MAX;
508 			else
509 				timo.tv_sec = NFS_TCPTIMEO;
510 		} else {
511 			timo.tv_sec = nmp->nm_timeo / NFS_HZ;
512 			timo.tv_usec = (nmp->nm_timeo * 1000000) / NFS_HZ;
513 		}
514 
515 		if (rep != NULL) {
516 			rep->r_flags = 0;
517 			rep->r_nmp = nmp;
518 			/*
519 			 * Chain request into list of outstanding requests.
520 			 */
521 			NFSLOCKREQ();
522 			TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
523 			NFSUNLOCKREQ();
524 		}
525 	}
526 
527 	nd->nd_mrep = NULL;
528 	stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
529 	    &nd->nd_mrep, timo);
530 
531 	if (rep != NULL) {
532 		/*
533 		 * RPC done, unlink the request.
534 		 */
535 		NFSLOCKREQ();
536 		TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
537 		NFSUNLOCKREQ();
538 	}
539 
540 	/*
541 	 * If there was a successful reply and a tprintf msg.
542 	 * tprintf a response.
543 	 */
544 	if (stat == RPC_SUCCESS) {
545 		error = 0;
546 	} else if (stat == RPC_TIMEDOUT) {
547 		error = ETIMEDOUT;
548 	} else if (stat == RPC_VERSMISMATCH) {
549 		error = EOPNOTSUPP;
550 	} else if (stat == RPC_PROGVERSMISMATCH) {
551 		error = EPROTONOSUPPORT;
552 	} else {
553 		error = EACCES;
554 	}
555 	if (error) {
556 		m_freem(nd->nd_mreq);
557 		AUTH_DESTROY(auth);
558 		if (rep != NULL)
559 			FREE((caddr_t)rep, M_NFSDREQ);
560 		return (error);
561 	}
562 
563 	KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
564 
565 	nd->nd_md = nd->nd_mrep;
566 	nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
567 	nd->nd_repstat = 0;
568 	if (nd->nd_procnum != NFSPROC_NULL) {
569 		/*
570 		 * and now the actual NFS xdr.
571 		 */
572 		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
573 		nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
574 		if (nd->nd_repstat != 0) {
575 			if ((nd->nd_repstat == NFSERR_DELAY &&
576 			     (nd->nd_flag & ND_NFSV4) &&
577 			     nd->nd_procnum != NFSPROC_SETATTR &&
578 			     nd->nd_procnum != NFSPROC_READ &&
579 			     nd->nd_procnum != NFSPROC_WRITE &&
580 			     nd->nd_procnum != NFSPROC_OPEN &&
581 			     nd->nd_procnum != NFSPROC_CREATE &&
582 			     nd->nd_procnum != NFSPROC_OPENCONFIRM &&
583 			     nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
584 			     nd->nd_procnum != NFSPROC_CLOSE &&
585 			     nd->nd_procnum != NFSPROC_LOCK &&
586 			     nd->nd_procnum != NFSPROC_LOCKU) ||
587 			    (nd->nd_repstat == NFSERR_DELAY &&
588 			     (nd->nd_flag & ND_NFSV4) == 0) ||
589 			    nd->nd_repstat == NFSERR_RESOURCE) {
590 				if (trylater_delay > NFS_TRYLATERDEL)
591 					trylater_delay = NFS_TRYLATERDEL;
592 				waituntil = NFSD_MONOSEC + trylater_delay;
593 				while (NFSD_MONOSEC < waituntil)
594 					(void) nfs_catnap(PZERO, "nfstry");
595 				trylater_delay *= 2;
596 				goto tryagain;
597 			}
598 
599 			/*
600 			 * If the File Handle was stale, invalidate the
601 			 * lookup cache, just in case.
602 			 * (vp != NULL implies a client side call)
603 			 */
604 			if (nd->nd_repstat == ESTALE && vp != NULL) {
605 				cache_purge(vp);
606 				if (ncl_call_invalcaches != NULL)
607 					(*ncl_call_invalcaches)(vp);
608 			}
609 		}
610 
611 		/*
612 		 * Get rid of the tag, return count, and PUTFH result for V4.
613 		 */
614 		if (nd->nd_flag & ND_NFSV4) {
615 			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
616 			i = fxdr_unsigned(int, *tl);
617 			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
618 			if (error)
619 				goto nfsmout;
620 			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
621 			i = fxdr_unsigned(int, *++tl);
622 
623 			/*
624 			 * If the first op's status is non-zero, mark that
625 			 * there is no more data to process.
626 			 */
627 			if (*++tl)
628 				nd->nd_flag |= ND_NOMOREDATA;
629 
630 			/*
631 			 * If the first op is Putfh, throw its results away
632 			 * and toss the op# and status for the first op.
633 			 */
634 			if (nmp != NULL && i == NFSV4OP_PUTFH && *tl == 0) {
635 				NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
636 				i = fxdr_unsigned(int, *tl++);
637 				j = fxdr_unsigned(int, *tl);
638 				/*
639 				 * All Compounds that do an Op that must
640 				 * be in sequence consist of NFSV4OP_PUTFH
641 				 * followed by one of these. As such, we
642 				 * can determine if the seqid# should be
643 				 * incremented, here.
644 				 */
645 				if ((i == NFSV4OP_OPEN ||
646 				     i == NFSV4OP_OPENCONFIRM ||
647 				     i == NFSV4OP_OPENDOWNGRADE ||
648 				     i == NFSV4OP_CLOSE ||
649 				     i == NFSV4OP_LOCK ||
650 				     i == NFSV4OP_LOCKU) &&
651 				    (j == 0 ||
652 				     (j != NFSERR_STALECLIENTID &&
653 				      j != NFSERR_STALESTATEID &&
654 				      j != NFSERR_BADSTATEID &&
655 				      j != NFSERR_BADSEQID &&
656 				      j != NFSERR_BADXDR &&
657 				      j != NFSERR_RESOURCE &&
658 				      j != NFSERR_NOFILEHANDLE)))
659 					nd->nd_flag |= ND_INCRSEQID;
660 				/*
661 				 * If the first op's status is non-zero, mark
662 				 * that there is no more data to process.
663 				 */
664 				if (j)
665 					nd->nd_flag |= ND_NOMOREDATA;
666 			}
667 
668 			/*
669 			 * If R_DONTRECOVER is set, replace the stale error
670 			 * reply, so that recovery isn't initiated.
671 			 */
672 			if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
673 			     nd->nd_repstat == NFSERR_STALESTATEID) &&
674 			    rep != NULL && (rep->r_flags & R_DONTRECOVER))
675 				nd->nd_repstat = NFSERR_STALEDONTRECOVER;
676 		}
677 	}
678 
679 	m_freem(nd->nd_mreq);
680 	AUTH_DESTROY(auth);
681 	if (rep != NULL)
682 		FREE((caddr_t)rep, M_NFSDREQ);
683 	return (0);
684 nfsmout:
685 	mbuf_freem(nd->nd_mrep);
686 	mbuf_freem(nd->nd_mreq);
687 	AUTH_DESTROY(auth);
688 	if (rep != NULL)
689 		FREE((caddr_t)rep, M_NFSDREQ);
690 	return (error);
691 }
692 
693 /*
694  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
695  * wait for all requests to complete. This is used by forced unmounts
696  * to terminate any outstanding RPCs.
697  */
698 int
699 newnfs_nmcancelreqs(struct nfsmount *nmp)
700 {
701 
702 	if (nmp->nm_sockreq.nr_client != NULL)
703 		CLNT_CLOSE(nmp->nm_sockreq.nr_client);
704 	return (0);
705 }
706 
707 /*
708  * Any signal that can interrupt an NFS operation in an intr mount
709  * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
710  */
711 int newnfs_sig_set[] = {
712 	SIGINT,
713 	SIGTERM,
714 	SIGHUP,
715 	SIGKILL,
716 	SIGSTOP,
717 	SIGQUIT
718 };
719 
720 /*
721  * Check to see if one of the signals in our subset is pending on
722  * the process (in an intr mount).
723  */
724 static int
725 nfs_sig_pending(sigset_t set)
726 {
727 	int i;
728 
729 	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
730 		if (SIGISMEMBER(set, newnfs_sig_set[i]))
731 			return (1);
732 	return (0);
733 }
734 
735 /*
736  * The set/restore sigmask functions are used to (temporarily) overwrite
737  * the process p_sigmask during an RPC call (for example). These are also
738  * used in other places in the NFS client that might tsleep().
739  */
740 void
741 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
742 {
743 	sigset_t newset;
744 	int i;
745 	struct proc *p;
746 
747 	SIGFILLSET(newset);
748 	if (td == NULL)
749 		td = curthread; /* XXX */
750 	p = td->td_proc;
751 	/* Remove the NFS set of signals from newset */
752 	PROC_LOCK(p);
753 	mtx_lock(&p->p_sigacts->ps_mtx);
754 	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
755 		/*
756 		 * But make sure we leave the ones already masked
757 		 * by the process, ie. remove the signal from the
758 		 * temporary signalmask only if it wasn't already
759 		 * in p_sigmask.
760 		 */
761 		if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
762 		    !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
763 			SIGDELSET(newset, newnfs_sig_set[i]);
764 	}
765 	mtx_unlock(&p->p_sigacts->ps_mtx);
766 	PROC_UNLOCK(p);
767 	kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
768 }
769 
770 void
771 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
772 {
773 	if (td == NULL)
774 		td = curthread; /* XXX */
775 	kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
776 }
777 
778 /*
779  * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
780  * old one after msleep() returns.
781  */
782 int
783 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
784 {
785 	sigset_t oldset;
786 	int error;
787 	struct proc *p;
788 
789 	if ((priority & PCATCH) == 0)
790 		return msleep(ident, mtx, priority, wmesg, timo);
791 	if (td == NULL)
792 		td = curthread; /* XXX */
793 	newnfs_set_sigmask(td, &oldset);
794 	error = msleep(ident, mtx, priority, wmesg, timo);
795 	newnfs_restore_sigmask(td, &oldset);
796 	p = td->td_proc;
797 	return (error);
798 }
799 
800 /*
801  * Test for a termination condition pending on the process.
802  * This is used for NFSMNT_INT mounts.
803  */
804 int
805 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
806 {
807 	struct proc *p;
808 	sigset_t tmpset;
809 
810 	/* Terminate all requests while attempting a forced unmount. */
811 	if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
812 		return (EIO);
813 	if (!(nmp->nm_flag & NFSMNT_INT))
814 		return (0);
815 	if (td == NULL)
816 		return (0);
817 	p = td->td_proc;
818 	PROC_LOCK(p);
819 	tmpset = p->p_siglist;
820 	SIGSETOR(tmpset, td->td_siglist);
821 	SIGSETNAND(tmpset, td->td_sigmask);
822 	mtx_lock(&p->p_sigacts->ps_mtx);
823 	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
824 	mtx_unlock(&p->p_sigacts->ps_mtx);
825 	if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
826 	    && nfs_sig_pending(tmpset)) {
827 		PROC_UNLOCK(p);
828 		return (EINTR);
829 	}
830 	PROC_UNLOCK(p);
831 	return (0);
832 }
833 
834 static int
835 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
836 {
837 	struct proc *p;
838 
839 	p = td ? td->td_proc : NULL;
840 	if (error) {
841 		tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
842 		    server, msg, error);
843 	} else {
844 		tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
845 	}
846 	return (0);
847 }
848 
849 static void
850 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
851     int error, int flags)
852 {
853 	if (nmp == NULL)
854 		return;
855 	mtx_lock(&nmp->nm_mtx);
856 	if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
857 		nmp->nm_state |= NFSSTA_TIMEO;
858 		mtx_unlock(&nmp->nm_mtx);
859 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
860 		    VQ_NOTRESP, 0);
861 	} else
862 		mtx_unlock(&nmp->nm_mtx);
863 	mtx_lock(&nmp->nm_mtx);
864 	if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
865 		nmp->nm_state |= NFSSTA_LOCKTIMEO;
866 		mtx_unlock(&nmp->nm_mtx);
867 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
868 		    VQ_NOTRESPLOCK, 0);
869 	} else
870 		mtx_unlock(&nmp->nm_mtx);
871 	nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
872 }
873 
874 static void
875 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
876     int flags, int tprintfmsg)
877 {
878 	if (nmp == NULL)
879 		return;
880 	if (tprintfmsg) {
881 		nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
882 	}
883 
884 	mtx_lock(&nmp->nm_mtx);
885 	if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
886 		nmp->nm_state &= ~NFSSTA_TIMEO;
887 		mtx_unlock(&nmp->nm_mtx);
888 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
889 		    VQ_NOTRESP, 1);
890 	} else
891 		mtx_unlock(&nmp->nm_mtx);
892 
893 	mtx_lock(&nmp->nm_mtx);
894 	if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
895 		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
896 		mtx_unlock(&nmp->nm_mtx);
897 		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
898 		    VQ_NOTRESPLOCK, 1);
899 	} else
900 		mtx_unlock(&nmp->nm_mtx);
901 }
902 
903