nfs_commonkrpc.c revision 192616
180709Sjake/*-
280709Sjake * Copyright (c) 1989, 1991, 1993, 1995
380709Sjake *	The Regents of the University of California.  All rights reserved.
480709Sjake *
580709Sjake * This code is derived from software contributed to Berkeley by
680709Sjake * Rick Macklem at The University of Guelph.
780709Sjake *
880709Sjake * Redistribution and use in source and binary forms, with or without
980709Sjake * modification, are permitted provided that the following conditions
1080709Sjake * are met:
1180709Sjake * 1. Redistributions of source code must retain the above copyright
1280709Sjake *    notice, this list of conditions and the following disclaimer.
1380709Sjake * 2. Redistributions in binary form must reproduce the above copyright
1481337Sobrien *    notice, this list of conditions and the following disclaimer in the
1580709Sjake *    documentation and/or other materials provided with the distribution.
1680709Sjake * 4. Neither the name of the University nor the names of its contributors
1781337Sobrien *    may be used to endorse or promote products derived from this software
1880709Sjake *    without specific prior written permission.
1980709Sjake *
2080709Sjake * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2180709Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2280709Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2380709Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2480709Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2580709Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2680709Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2780709Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2880709Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2980709Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3080709Sjake * SUCH DAMAGE.
3180709Sjake *
3280709Sjake */
33131952Smarcel
3480709Sjake#include <sys/cdefs.h>
3580709Sjake__FBSDID("$FreeBSD: head/sys/fs/nfs/nfs_commonkrpc.c 192616 2009-05-23 00:40:17Z rmacklem $");
3680709Sjake
3780709Sjake/*
3880709Sjake * Socket operations for use by nfs
3980709Sjake */
4080709Sjake
4180709Sjake#include "opt_inet6.h"
4280709Sjake#include "opt_kgssapi.h"
4380709Sjake#include "opt_nfs.h"
4480709Sjake
4580709Sjake#include <sys/param.h>
4680709Sjake#include <sys/systm.h>
4780709Sjake#include <sys/kernel.h>
4880709Sjake#include <sys/limits.h>
4980709Sjake#include <sys/lock.h>
5080709Sjake#include <sys/malloc.h>
5180709Sjake#include <sys/mbuf.h>
5292205Sjake#include <sys/mount.h>
5384181Sjake#include <sys/mutex.h>
5480709Sjake#include <sys/proc.h>
5580709Sjake#include <sys/signalvar.h>
5680709Sjake#include <sys/syscallsubr.h>
5780709Sjake#include <sys/sysctl.h>
58131952Smarcel#include <sys/syslog.h>
59131952Smarcel#include <sys/vnode.h>
6080709Sjake
61131952Smarcel#include <rpc/rpc.h>
6280709Sjake
63131952Smarcel#include <kgssapi/krb5/kcrypto.h>
64131952Smarcel
65131952Smarcel#include <fs/nfs/nfsport.h>
66131952Smarcel
67131952SmarcelNFSSTATESPINLOCK;
68131952SmarcelNFSREQSPINLOCK;
69131952Smarcelextern struct nfsstats newnfsstats;
70131952Smarcelextern struct nfsreqhead nfsd_reqq;
71131952Smarcelextern int nfscl_ticks;
7280709Sjakeextern void (*ncl_call_invalcaches)(struct vnode *);
7380709Sjake
74131952Smarcelstatic int	nfsrv_gsscallbackson = 0;
7580709Sjakestatic int	nfs_bufpackets = 4;
7680709Sjakestatic int	nfs_reconnects;
77131952Smarcelstatic int	nfs3_jukebox_delay = 10;
78131952Smarcelstatic int	nfs_skip_wcc_data_onerr = 1;
7980709Sjakestatic int	nfs_keytab_enctype = ETYPE_DES_CBC_CRC;
80131952Smarcel
8180709SjakeSYSCTL_DECL(_vfs_newnfs);
82131952Smarcel
83131952SmarcelSYSCTL_INT(_vfs_newnfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
84131952Smarcel    "Buffer reservation size 2 < x < 64");
85131952SmarcelSYSCTL_INT(_vfs_newnfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
86131952Smarcel    "Number of times the nfs client has had to reconnect");
87131952SmarcelSYSCTL_INT(_vfs_newnfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
88131952Smarcel    "Number of seconds to delay a retry after receiving EJUKEBOX");
89131952SmarcelSYSCTL_INT(_vfs_newnfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
90131952Smarcel    "Disable weak cache consistency checking when server returns an error");
9180709SjakeSYSCTL_INT(_vfs_newnfs, OID_AUTO, keytab_enctype, CTLFLAG_RW, &nfs_keytab_enctype, 0,
9280709Sjake    "Encryption type for the keytab entry used by nfs");
9387702Sjhb
9487702Sjhbstatic void	nfs_down(struct nfsmount *, struct thread *, const char *,
9587702Sjhb    int, int);
9687702Sjhbstatic void	nfs_up(struct nfsmount *, struct thread *, const char *,
9787702Sjhb    int, int);
9880709Sjakestatic int	nfs_msg(struct thread *, const char *, const char *, int);
9980709Sjake
10080709Sjakeextern int nfsv2_procid[];
10180709Sjake
10286147Stmmstruct nfs_cached_auth {
10386147Stmm	int		ca_refs; /* refcount, including 1 from the cache */
10486147Stmm	uid_t		ca_uid;	 /* uid that corresponds to this auth */
10586147Stmm	AUTH		*ca_auth; /* RPC auth handle */
10686147Stmm};
107
108/*
109 * Initialize sockets and congestion for a new NFS connection.
110 * We do not free the sockaddr if error.
111 */
112int
113newnfs_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 */
275void
276newnfs_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
295static AUTH *
296nfs_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		return (auth);
345#endif	/* KGSSAPI */
346	case AUTH_SYS:
347	default:
348		return (authunix_create(cred));
349
350	}
351}
352
353/*
354 * Callback from the RPC code to generate up/down notifications.
355 */
356
357struct nfs_feedback_arg {
358	struct nfsmount *nf_mount;
359	int		nf_lastmsg;	/* last tprintf */
360	int		nf_tprintfmsg;
361	struct thread	*nf_td;
362};
363
364static void
365nfs_feedback(int type, int proc, void *arg)
366{
367	struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
368	struct nfsmount *nmp = nf->nf_mount;
369	struct timeval now;
370
371	getmicrouptime(&now);
372
373	switch (type) {
374	case FEEDBACK_REXMIT2:
375	case FEEDBACK_RECONNECT:
376		if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
377			nfs_down(nmp, nf->nf_td,
378			    "not responding", 0, NFSSTA_TIMEO);
379			nf->nf_tprintfmsg = TRUE;
380			nf->nf_lastmsg = now.tv_sec;
381		}
382		break;
383
384	case FEEDBACK_OK:
385		nfs_up(nf->nf_mount, nf->nf_td,
386		    "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
387		break;
388	}
389}
390
391/*
392 * newnfs_request - goes something like this
393 *	- does the rpc by calling the krpc layer
394 *	- break down rpc header and return with nfs reply
395 * nb: always frees up nd_mreq mbuf list
396 */
397int
398newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
399    struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
400    struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
401    u_char *retsum, int toplevel, u_int64_t *xidp)
402{
403	u_int32_t *tl;
404	time_t waituntil;
405	int i, j;
406	int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
407	u_int16_t procnum;
408	u_int trylater_delay = 1;
409	struct nfs_feedback_arg nf;
410	struct timeval timo, now;
411	AUTH *auth;
412	struct rpc_callextra ext;
413	enum clnt_stat stat;
414	struct nfsreq *rep = NULL;
415	char *srv_principal = NULL;
416
417	if (xidp != NULL)
418		*xidp = 0;
419	/* Reject requests while attempting a forced unmount. */
420	if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
421		m_freem(nd->nd_mreq);
422		return (ESTALE);
423	}
424
425	/*
426	 * For a client side mount, nmp is != NULL and clp == NULL. For
427	 * server calls (callbacks or upcalls), nmp == NULL.
428	 */
429	if (clp != NULL) {
430		NFSLOCKSTATE();
431		if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
432			secflavour = RPCSEC_GSS_KRB5;
433			if (nd->nd_procnum != NFSPROC_NULL) {
434				if (clp->lc_flags & LCL_GSSINTEGRITY)
435					secflavour = RPCSEC_GSS_KRB5I;
436				else if (clp->lc_flags & LCL_GSSPRIVACY)
437					secflavour = RPCSEC_GSS_KRB5P;
438			}
439		}
440		NFSUNLOCKSTATE();
441	} else if (nmp != NULL && NFSHASKERB(nmp) &&
442	     nd->nd_procnum != NFSPROC_NULL) {
443		if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
444			nd->nd_flag |= ND_USEGSSNAME;
445		if ((nd->nd_flag & ND_USEGSSNAME) && nmp->nm_krbnamelen > 0)
446			usegssname = 1;
447		if (NFSHASINTEGRITY(nmp))
448			secflavour = RPCSEC_GSS_KRB5I;
449		else if (NFSHASPRIVACY(nmp))
450			secflavour = RPCSEC_GSS_KRB5P;
451		else
452			secflavour = RPCSEC_GSS_KRB5;
453		srv_principal = NFSMNT_SRVKRBNAME(nmp);
454	}
455
456	if (nmp != NULL) {
457		bzero(&nf, sizeof(struct nfs_feedback_arg));
458		nf.nf_mount = nmp;
459		nf.nf_td = td;
460		getmicrouptime(&now);
461		nf.nf_lastmsg = now.tv_sec -
462		    ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
463	}
464
465	/*
466	 * XXX if not already connected call nfs_connect now. Longer
467	 * term, change nfs_mount to call nfs_connect unconditionally
468	 * and let clnt_reconnect_create handle reconnects.
469	 */
470	if (nrp->nr_client == NULL)
471		newnfs_connect(nmp, nrp, cred, td, 0);
472
473	if (nd->nd_procnum == NFSPROC_NULL)
474		auth = authnone_create();
475	else if (usegssname)
476		auth = nfs_getauth(nrp, secflavour, nmp->nm_krbname,
477		    srv_principal, NULL, cred);
478	else
479		auth = nfs_getauth(nrp, secflavour, NULL,
480		    srv_principal, NULL, cred);
481	if (auth == NULL) {
482		m_freem(nd->nd_mreq);
483		return (EACCES);
484	}
485	bzero(&ext, sizeof(ext));
486	ext.rc_auth = auth;
487	if (nmp != NULL) {
488		ext.rc_feedback = nfs_feedback;
489		ext.rc_feedback_arg = &nf;
490	}
491
492	procnum = nd->nd_procnum;
493	if ((nd->nd_flag & ND_NFSV4) &&
494	    nd->nd_procnum != NFSPROC_NULL &&
495	    nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
496		procnum = NFSV4PROC_COMPOUND;
497
498	if (nmp != NULL) {
499		NFSINCRGLOBAL(newnfsstats.rpcrequests);
500		/*
501		 * Now only used for the R_DONTRECOVER case, but until that is
502		 * supported within the krpc code, I need to keep a queue of
503		 * outstanding RPCs for nfsv4 client requests.
504		 */
505		if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
506			MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
507			    M_NFSDREQ, M_WAITOK);
508	}
509	trycnt = 0;
510tryagain:
511	if (nmp == NULL) {
512		timo.tv_usec = 0;
513		if (clp == NULL)
514			timo.tv_sec = NFSV4_UPCALLTIMEO;
515		else
516			timo.tv_sec = NFSV4_CALLBACKTIMEO;
517	} else {
518		if (nrp->nr_sotype != SOCK_DGRAM) {
519			timo.tv_usec = 0;
520			if ((nmp->nm_flag & NFSMNT_NFSV4))
521				timo.tv_sec = INT_MAX;
522			else
523				timo.tv_sec = NFS_TCPTIMEO;
524		} else {
525			timo.tv_sec = nmp->nm_timeo / NFS_HZ;
526			timo.tv_usec = (nmp->nm_timeo * 1000000) / NFS_HZ;
527		}
528
529		if (rep != NULL) {
530			rep->r_flags = 0;
531			rep->r_nmp = nmp;
532			/*
533			 * Chain request into list of outstanding requests.
534			 */
535			NFSLOCKREQ();
536			TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
537			NFSUNLOCKREQ();
538		}
539	}
540
541	nd->nd_mrep = NULL;
542	stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
543	    &nd->nd_mrep, timo);
544
545	if (rep != NULL) {
546		/*
547		 * RPC done, unlink the request.
548		 */
549		NFSLOCKREQ();
550		TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
551		NFSUNLOCKREQ();
552	}
553
554	/*
555	 * If there was a successful reply and a tprintf msg.
556	 * tprintf a response.
557	 */
558	if (stat == RPC_SUCCESS) {
559		error = 0;
560	} else if (stat == RPC_TIMEDOUT) {
561		error = ETIMEDOUT;
562	} else if (stat == RPC_VERSMISMATCH) {
563		error = EOPNOTSUPP;
564	} else if (stat == RPC_PROGVERSMISMATCH) {
565		error = EPROTONOSUPPORT;
566	} else {
567		error = EACCES;
568	}
569	if (error) {
570		m_freem(nd->nd_mreq);
571		AUTH_DESTROY(auth);
572		if (rep != NULL)
573			FREE((caddr_t)rep, M_NFSDREQ);
574		return (error);
575	}
576
577	KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
578
579	nd->nd_md = nd->nd_mrep;
580	nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
581	nd->nd_repstat = 0;
582	if (nd->nd_procnum != NFSPROC_NULL) {
583		/*
584		 * and now the actual NFS xdr.
585		 */
586		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
587		nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
588		if (nd->nd_repstat != 0) {
589			if ((nd->nd_repstat == NFSERR_DELAY &&
590			     (nd->nd_flag & ND_NFSV4) &&
591			     nd->nd_procnum != NFSPROC_SETATTR &&
592			     nd->nd_procnum != NFSPROC_READ &&
593			     nd->nd_procnum != NFSPROC_WRITE &&
594			     nd->nd_procnum != NFSPROC_OPEN &&
595			     nd->nd_procnum != NFSPROC_CREATE &&
596			     nd->nd_procnum != NFSPROC_OPENCONFIRM &&
597			     nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
598			     nd->nd_procnum != NFSPROC_CLOSE &&
599			     nd->nd_procnum != NFSPROC_LOCK &&
600			     nd->nd_procnum != NFSPROC_LOCKU) ||
601			    (nd->nd_repstat == NFSERR_DELAY &&
602			     (nd->nd_flag & ND_NFSV4) == 0) ||
603			    nd->nd_repstat == NFSERR_RESOURCE) {
604				if (trylater_delay > NFS_TRYLATERDEL)
605					trylater_delay = NFS_TRYLATERDEL;
606				waituntil = NFSD_MONOSEC + trylater_delay;
607				while (NFSD_MONOSEC < waituntil)
608					(void) nfs_catnap(PZERO, "nfstry");
609				trylater_delay *= 2;
610				goto tryagain;
611			}
612
613			/*
614			 * If the File Handle was stale, invalidate the
615			 * lookup cache, just in case.
616			 * (vp != NULL implies a client side call)
617			 */
618			if (nd->nd_repstat == ESTALE && vp != NULL) {
619				cache_purge(vp);
620				if (ncl_call_invalcaches != NULL)
621					(*ncl_call_invalcaches)(vp);
622			}
623		}
624
625		/*
626		 * Get rid of the tag, return count, and PUTFH result for V4.
627		 */
628		if (nd->nd_flag & ND_NFSV4) {
629			NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
630			i = fxdr_unsigned(int, *tl);
631			error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
632			if (error)
633				goto nfsmout;
634			NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
635			i = fxdr_unsigned(int, *++tl);
636
637			/*
638			 * If the first op's status is non-zero, mark that
639			 * there is no more data to process.
640			 */
641			if (*++tl)
642				nd->nd_flag |= ND_NOMOREDATA;
643
644			/*
645			 * If the first op is Putfh, throw its results away
646			 * and toss the op# and status for the first op.
647			 */
648			if (nmp != NULL && i == NFSV4OP_PUTFH && *tl == 0) {
649				NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
650				i = fxdr_unsigned(int, *tl++);
651				j = fxdr_unsigned(int, *tl);
652				/*
653				 * All Compounds that do an Op that must
654				 * be in sequence consist of NFSV4OP_PUTFH
655				 * followed by one of these. As such, we
656				 * can determine if the seqid# should be
657				 * incremented, here.
658				 */
659				if ((i == NFSV4OP_OPEN ||
660				     i == NFSV4OP_OPENCONFIRM ||
661				     i == NFSV4OP_OPENDOWNGRADE ||
662				     i == NFSV4OP_CLOSE ||
663				     i == NFSV4OP_LOCK ||
664				     i == NFSV4OP_LOCKU) &&
665				    (j == 0 ||
666				     (j != NFSERR_STALECLIENTID &&
667				      j != NFSERR_STALESTATEID &&
668				      j != NFSERR_BADSTATEID &&
669				      j != NFSERR_BADSEQID &&
670				      j != NFSERR_BADXDR &&
671				      j != NFSERR_RESOURCE &&
672				      j != NFSERR_NOFILEHANDLE)))
673					nd->nd_flag |= ND_INCRSEQID;
674				/*
675				 * If the first op's status is non-zero, mark
676				 * that there is no more data to process.
677				 */
678				if (j)
679					nd->nd_flag |= ND_NOMOREDATA;
680			}
681
682			/*
683			 * If R_DONTRECOVER is set, replace the stale error
684			 * reply, so that recovery isn't initiated.
685			 */
686			if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
687			     nd->nd_repstat == NFSERR_STALESTATEID) &&
688			    rep != NULL && (rep->r_flags & R_DONTRECOVER))
689				nd->nd_repstat = NFSERR_STALEDONTRECOVER;
690		}
691	}
692
693	m_freem(nd->nd_mreq);
694	AUTH_DESTROY(auth);
695	if (rep != NULL)
696		FREE((caddr_t)rep, M_NFSDREQ);
697	return (0);
698nfsmout:
699	mbuf_freem(nd->nd_mrep);
700	mbuf_freem(nd->nd_mreq);
701	AUTH_DESTROY(auth);
702	if (rep != NULL)
703		FREE((caddr_t)rep, M_NFSDREQ);
704	return (error);
705}
706
707/*
708 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
709 * wait for all requests to complete. This is used by forced unmounts
710 * to terminate any outstanding RPCs.
711 */
712int
713newnfs_nmcancelreqs(struct nfsmount *nmp)
714{
715
716	if (nmp->nm_sockreq.nr_client != NULL)
717		CLNT_CLOSE(nmp->nm_sockreq.nr_client);
718	return (0);
719}
720
721/*
722 * Any signal that can interrupt an NFS operation in an intr mount
723 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
724 */
725int newnfs_sig_set[] = {
726	SIGINT,
727	SIGTERM,
728	SIGHUP,
729	SIGKILL,
730	SIGSTOP,
731	SIGQUIT
732};
733
734/*
735 * Check to see if one of the signals in our subset is pending on
736 * the process (in an intr mount).
737 */
738static int
739nfs_sig_pending(sigset_t set)
740{
741	int i;
742
743	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
744		if (SIGISMEMBER(set, newnfs_sig_set[i]))
745			return (1);
746	return (0);
747}
748
749/*
750 * The set/restore sigmask functions are used to (temporarily) overwrite
751 * the process p_sigmask during an RPC call (for example). These are also
752 * used in other places in the NFS client that might tsleep().
753 */
754void
755newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
756{
757	sigset_t newset;
758	int i;
759	struct proc *p;
760
761	SIGFILLSET(newset);
762	if (td == NULL)
763		td = curthread; /* XXX */
764	p = td->td_proc;
765	/* Remove the NFS set of signals from newset */
766	PROC_LOCK(p);
767	mtx_lock(&p->p_sigacts->ps_mtx);
768	for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
769		/*
770		 * But make sure we leave the ones already masked
771		 * by the process, ie. remove the signal from the
772		 * temporary signalmask only if it wasn't already
773		 * in p_sigmask.
774		 */
775		if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
776		    !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
777			SIGDELSET(newset, newnfs_sig_set[i]);
778	}
779	mtx_unlock(&p->p_sigacts->ps_mtx);
780	PROC_UNLOCK(p);
781	kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
782}
783
784void
785newnfs_restore_sigmask(struct thread *td, sigset_t *set)
786{
787	if (td == NULL)
788		td = curthread; /* XXX */
789	kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
790}
791
792/*
793 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
794 * old one after msleep() returns.
795 */
796int
797newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
798{
799	sigset_t oldset;
800	int error;
801	struct proc *p;
802
803	if ((priority & PCATCH) == 0)
804		return msleep(ident, mtx, priority, wmesg, timo);
805	if (td == NULL)
806		td = curthread; /* XXX */
807	newnfs_set_sigmask(td, &oldset);
808	error = msleep(ident, mtx, priority, wmesg, timo);
809	newnfs_restore_sigmask(td, &oldset);
810	p = td->td_proc;
811	return (error);
812}
813
814/*
815 * Test for a termination condition pending on the process.
816 * This is used for NFSMNT_INT mounts.
817 */
818int
819newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
820{
821	struct proc *p;
822	sigset_t tmpset;
823
824	/* Terminate all requests while attempting a forced unmount. */
825	if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
826		return (EIO);
827	if (!(nmp->nm_flag & NFSMNT_INT))
828		return (0);
829	if (td == NULL)
830		return (0);
831	p = td->td_proc;
832	PROC_LOCK(p);
833	tmpset = p->p_siglist;
834	SIGSETOR(tmpset, td->td_siglist);
835	SIGSETNAND(tmpset, td->td_sigmask);
836	mtx_lock(&p->p_sigacts->ps_mtx);
837	SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
838	mtx_unlock(&p->p_sigacts->ps_mtx);
839	if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
840	    && nfs_sig_pending(tmpset)) {
841		PROC_UNLOCK(p);
842		return (EINTR);
843	}
844	PROC_UNLOCK(p);
845	return (0);
846}
847
848static int
849nfs_msg(struct thread *td, const char *server, const char *msg, int error)
850{
851	struct proc *p;
852
853	p = td ? td->td_proc : NULL;
854	if (error) {
855		tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
856		    server, msg, error);
857	} else {
858		tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
859	}
860	return (0);
861}
862
863static void
864nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
865    int error, int flags)
866{
867	if (nmp == NULL)
868		return;
869	mtx_lock(&nmp->nm_mtx);
870	if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
871		nmp->nm_state |= NFSSTA_TIMEO;
872		mtx_unlock(&nmp->nm_mtx);
873		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
874		    VQ_NOTRESP, 0);
875	} else
876		mtx_unlock(&nmp->nm_mtx);
877	mtx_lock(&nmp->nm_mtx);
878	if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
879		nmp->nm_state |= NFSSTA_LOCKTIMEO;
880		mtx_unlock(&nmp->nm_mtx);
881		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
882		    VQ_NOTRESPLOCK, 0);
883	} else
884		mtx_unlock(&nmp->nm_mtx);
885	nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
886}
887
888static void
889nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
890    int flags, int tprintfmsg)
891{
892	if (nmp == NULL)
893		return;
894	if (tprintfmsg) {
895		nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
896	}
897
898	mtx_lock(&nmp->nm_mtx);
899	if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
900		nmp->nm_state &= ~NFSSTA_TIMEO;
901		mtx_unlock(&nmp->nm_mtx);
902		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
903		    VQ_NOTRESP, 1);
904	} else
905		mtx_unlock(&nmp->nm_mtx);
906
907	mtx_lock(&nmp->nm_mtx);
908	if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
909		nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
910		mtx_unlock(&nmp->nm_mtx);
911		vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
912		    VQ_NOTRESPLOCK, 1);
913	} else
914		mtx_unlock(&nmp->nm_mtx);
915}
916
917