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