2 * Copyright (c) 1989, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Socket operations for use by nfs
41 #include "opt_inet6.h"
42 #include "opt_kgssapi.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
50 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.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>
63 #include <kgssapi/krb5/kcrypto.h>
65 #include <fs/nfs/nfsport.h>
69 extern struct nfsstats newnfsstats;
70 extern struct nfsreqhead nfsd_reqq;
71 extern int nfscl_ticks;
72 extern void (*ncl_call_invalcaches)(struct vnode *);
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;
81 SYSCTL_DECL(_vfs_newnfs);
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");
94 static void nfs_down(struct nfsmount *, struct thread *, const char *,
96 static void nfs_up(struct nfsmount *, struct thread *, const char *,
98 static int nfs_msg(struct thread *, const char *, const char *, int);
100 struct nfs_cached_auth {
101 int ca_refs; /* refcount, including 1 from the cache */
102 uid_t ca_uid; /* uid that corresponds to this auth */
103 AUTH *ca_auth; /* RPC auth handle */
106 static int nfsv2_procid[NFS_V3NPROCS] = {
132 * Initialize sockets and congestion for a new NFS connection.
133 * We do not free the sockaddr if error.
136 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
137 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
139 int rcvreserve, sndreserve;
141 struct sockaddr *saddr;
142 struct ucred *origcred;
144 struct netconfig *nconf;
146 int one = 1, retries, error, printsbmax = 0;
147 struct thread *td = curthread;
150 * We need to establish the socket using the credentials of
151 * the mountpoint. Some parts of this process (such as
152 * sobind() and soconnect()) will use the curent thread's
153 * credential instead of the socket credential. To work
154 * around this, temporarily change the current thread's
155 * credential to that of the mountpoint.
157 * XXX: It would be better to explicitly pass the correct
158 * credential to sobind() and soconnect().
160 origcred = td->td_ucred;
163 * Use the credential in nr_cred, if not NULL.
165 if (nrp->nr_cred != NULL)
166 td->td_ucred = nrp->nr_cred;
171 if (saddr->sa_family == AF_INET)
172 if (nrp->nr_sotype == SOCK_DGRAM)
173 nconf = getnetconfigent("udp");
175 nconf = getnetconfigent("tcp");
177 if (nrp->nr_sotype == SOCK_DGRAM)
178 nconf = getnetconfigent("udp6");
180 nconf = getnetconfigent("tcp6");
182 pktscale = nfs_bufpackets;
188 * soreserve() can fail if sb_max is too small, so shrink pktscale
189 * and try again if there is an error.
190 * Print a log message suggesting increasing sb_max.
191 * Creating a socket and doing this is necessary since, if the
192 * reservation sizes are too large and will make soreserve() fail,
193 * the connection will work until a large send is attempted and
194 * then it will loop in the krpc code.
197 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
198 error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
199 nrp->nr_soproto, td->td_ucred, td);
201 td->td_ucred = origcred;
205 if (error != 0 && pktscale > 2) {
207 if (printsbmax == 0) {
208 printf("nfscl: consider increasing kern.ipc.maxsockbuf\n");
212 if (nrp->nr_sotype == SOCK_DGRAM) {
214 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
216 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
219 sndreserve = rcvreserve = 1024 * pktscale;
222 if (nrp->nr_sotype != SOCK_STREAM)
223 panic("nfscon sotype");
225 sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
226 sizeof (u_int32_t)) * pktscale;
227 rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
228 sizeof (u_int32_t)) * pktscale;
230 sndreserve = rcvreserve = 1024 * pktscale;
233 error = soreserve(so, sndreserve, rcvreserve);
234 } while (error != 0 && pktscale > 2);
237 td->td_ucred = origcred;
241 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
242 nrp->nr_vers, sndreserve, rcvreserve);
243 CLNT_CONTROL(client, CLSET_WAITCHAN, "newnfsreq");
245 if ((nmp->nm_flag & NFSMNT_INT))
246 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
247 if ((nmp->nm_flag & NFSMNT_RESVPORT))
248 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
250 retries = nmp->nm_retry;
256 * - Null RPC callback to client
257 * - Non-Null RPC callback to client, wait a little longer
258 * - upcalls to nfsuserd and gssd (clp == NULL)
260 if (callback_retry_mult == 0) {
261 retries = NFSV4_UPCALLRETRY;
262 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
264 retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
267 CLNT_CONTROL(client, CLSET_RETRIES, &retries);
269 mtx_lock(&nrp->nr_mtx);
270 if (nrp->nr_client != NULL) {
272 * Someone else already connected.
274 CLNT_RELEASE(client);
276 nrp->nr_client = client;
280 * Protocols that do not require connections may be optionally left
281 * unconnected for servers that reply from a port other than NFS_PORT.
283 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
284 mtx_unlock(&nrp->nr_mtx);
285 CLNT_CONTROL(client, CLSET_CONNECT, &one);
287 mtx_unlock(&nrp->nr_mtx);
290 /* Restore current thread's credentials. */
291 td->td_ucred = origcred;
296 * NFS disconnect. Clean up and unlink.
299 newnfs_disconnect(struct nfssockreq *nrp)
303 mtx_lock(&nrp->nr_mtx);
304 if (nrp->nr_client != NULL) {
305 client = nrp->nr_client;
306 nrp->nr_client = NULL;
307 mtx_unlock(&nrp->nr_mtx);
309 rpc_gss_secpurge(client);
312 CLNT_RELEASE(client);
314 mtx_unlock(&nrp->nr_mtx);
319 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
320 char *srv_principal, gss_OID mech_oid, struct ucred *cred)
323 rpc_gss_service_t svc;
326 rpc_gss_options_req_t req_options;
330 switch (secflavour) {
332 case RPCSEC_GSS_KRB5:
333 case RPCSEC_GSS_KRB5I:
334 case RPCSEC_GSS_KRB5P:
336 if (!rpc_gss_mech_to_oid("kerberosv5", &mech_oid))
339 if (secflavour == RPCSEC_GSS_KRB5)
340 svc = rpc_gss_svc_none;
341 else if (secflavour == RPCSEC_GSS_KRB5I)
342 svc = rpc_gss_svc_integrity;
344 svc = rpc_gss_svc_privacy;
346 req_options.req_flags = GSS_C_MUTUAL_FLAG;
347 req_options.time_req = 0;
348 req_options.my_cred = GSS_C_NO_CREDENTIAL;
349 req_options.input_channel_bindings = NULL;
350 req_options.enc_type = nfs_keytab_enctype;
352 auth = rpc_gss_secfind(nrp->nr_client, cred,
353 clnt_principal, srv_principal, mech_oid, svc,
357 * Until changes to the rpcsec_gss code are committed,
358 * there is no support for host based initiator
359 * principals. As such, that case cannot yet be handled.
361 if (clnt_principal == NULL)
362 auth = rpc_gss_secfind(nrp->nr_client, cred,
363 srv_principal, mech_oid, svc);
373 return (authunix_create(cred));
379 * Callback from the RPC code to generate up/down notifications.
382 struct nfs_feedback_arg {
383 struct nfsmount *nf_mount;
384 int nf_lastmsg; /* last tprintf */
386 struct thread *nf_td;
390 nfs_feedback(int type, int proc, void *arg)
392 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
393 struct nfsmount *nmp = nf->nf_mount;
396 getmicrouptime(&now);
399 case FEEDBACK_REXMIT2:
400 case FEEDBACK_RECONNECT:
401 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
402 nfs_down(nmp, nf->nf_td,
403 "not responding", 0, NFSSTA_TIMEO);
404 nf->nf_tprintfmsg = TRUE;
405 nf->nf_lastmsg = now.tv_sec;
410 nfs_up(nf->nf_mount, nf->nf_td,
411 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
417 * newnfs_request - goes something like this
418 * - does the rpc by calling the krpc layer
419 * - break down rpc header and return with nfs reply
420 * nb: always frees up nd_mreq mbuf list
423 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
424 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
425 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
426 u_char *retsum, int toplevel, u_int64_t *xidp)
430 int i, j, set_uid = 0, set_sigset = 0;
431 int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
433 u_int trylater_delay = 1;
434 struct nfs_feedback_arg nf;
435 struct timeval timo, now;
437 struct rpc_callextra ext;
439 struct nfsreq *rep = NULL;
440 char *srv_principal = NULL;
441 uid_t saved_uid = (uid_t)-1;
446 /* Reject requests while attempting a forced unmount. */
447 if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
448 m_freem(nd->nd_mreq);
452 /* For client side interruptible mounts, mask off the signals. */
453 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
454 newnfs_set_sigmask(td, &oldset);
459 * XXX if not already connected call nfs_connect now. Longer
460 * term, change nfs_mount to call nfs_connect unconditionally
461 * and let clnt_reconnect_create handle reconnects.
463 if (nrp->nr_client == NULL)
464 newnfs_connect(nmp, nrp, cred, td, 0);
467 * For a client side mount, nmp is != NULL and clp == NULL. For
468 * server calls (callbacks or upcalls), nmp == NULL.
472 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
473 secflavour = RPCSEC_GSS_KRB5;
474 if (nd->nd_procnum != NFSPROC_NULL) {
475 if (clp->lc_flags & LCL_GSSINTEGRITY)
476 secflavour = RPCSEC_GSS_KRB5I;
477 else if (clp->lc_flags & LCL_GSSPRIVACY)
478 secflavour = RPCSEC_GSS_KRB5P;
482 } else if (nmp != NULL && NFSHASKERB(nmp) &&
483 nd->nd_procnum != NFSPROC_NULL) {
484 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
485 nd->nd_flag |= ND_USEGSSNAME;
486 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
488 * If there is a client side host based credential,
489 * use that, otherwise use the system uid, if set.
491 if (nmp->nm_krbnamelen > 0) {
493 } else if (nmp->nm_uid != (uid_t)-1) {
494 saved_uid = cred->cr_uid;
495 cred->cr_uid = nmp->nm_uid;
498 } else if (nmp->nm_krbnamelen == 0 &&
499 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
501 * If there is no host based principal name and
502 * the system uid is set and this is root, use the
503 * system uid, since root won't have user
504 * credentials in a credentials cache file.
506 saved_uid = cred->cr_uid;
507 cred->cr_uid = nmp->nm_uid;
510 if (NFSHASINTEGRITY(nmp))
511 secflavour = RPCSEC_GSS_KRB5I;
512 else if (NFSHASPRIVACY(nmp))
513 secflavour = RPCSEC_GSS_KRB5P;
515 secflavour = RPCSEC_GSS_KRB5;
516 srv_principal = NFSMNT_SRVKRBNAME(nmp);
520 bzero(&nf, sizeof(struct nfs_feedback_arg));
523 getmicrouptime(&now);
524 nf.nf_lastmsg = now.tv_sec -
525 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
528 if (nd->nd_procnum == NFSPROC_NULL)
529 auth = authnone_create();
531 auth = nfs_getauth(nrp, secflavour, nmp->nm_krbname,
532 srv_principal, NULL, cred);
534 auth = nfs_getauth(nrp, secflavour, NULL,
535 srv_principal, NULL, cred);
537 cred->cr_uid = saved_uid;
539 m_freem(nd->nd_mreq);
541 newnfs_restore_sigmask(td, &oldset);
544 bzero(&ext, sizeof(ext));
547 ext.rc_feedback = nfs_feedback;
548 ext.rc_feedback_arg = &nf;
551 procnum = nd->nd_procnum;
552 if ((nd->nd_flag & ND_NFSV4) &&
553 nd->nd_procnum != NFSPROC_NULL &&
554 nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
555 procnum = NFSV4PROC_COMPOUND;
558 NFSINCRGLOBAL(newnfsstats.rpcrequests);
560 /* Map the procnum to the old NFSv2 one, as required. */
561 if ((nd->nd_flag & ND_NFSV2) != 0) {
562 if (nd->nd_procnum < NFS_V3NPROCS)
563 procnum = nfsv2_procid[nd->nd_procnum];
565 procnum = NFSV2PROC_NOOP;
569 * Now only used for the R_DONTRECOVER case, but until that is
570 * supported within the krpc code, I need to keep a queue of
571 * outstanding RPCs for nfsv4 client requests.
573 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
574 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
575 M_NFSDREQ, M_WAITOK);
582 timo.tv_sec = NFSV4_UPCALLTIMEO;
584 timo.tv_sec = NFSV4_CALLBACKTIMEO;
586 if (nrp->nr_sotype != SOCK_DGRAM) {
588 if ((nmp->nm_flag & NFSMNT_NFSV4))
589 timo.tv_sec = INT_MAX;
591 timo.tv_sec = NFS_TCPTIMEO;
593 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
594 timo.tv_usec = (nmp->nm_timeo * 1000000) / NFS_HZ;
601 * Chain request into list of outstanding requests.
604 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
610 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
615 * RPC done, unlink the request.
618 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
623 * If there was a successful reply and a tprintf msg.
624 * tprintf a response.
626 if (stat == RPC_SUCCESS) {
628 } else if (stat == RPC_TIMEDOUT) {
630 } else if (stat == RPC_VERSMISMATCH) {
632 } else if (stat == RPC_PROGVERSMISMATCH) {
633 error = EPROTONOSUPPORT;
638 m_freem(nd->nd_mreq);
641 FREE((caddr_t)rep, M_NFSDREQ);
643 newnfs_restore_sigmask(td, &oldset);
647 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
650 * Search for any mbufs that are not a multiple of 4 bytes long
651 * or with m_data not longword aligned.
652 * These could cause pointer alignment problems, so copy them to
653 * well aligned mbufs.
655 newnfs_realign(&nd->nd_mrep);
656 nd->nd_md = nd->nd_mrep;
657 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
659 if (nd->nd_procnum != NFSPROC_NULL) {
661 * and now the actual NFS xdr.
663 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
664 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
665 if (nd->nd_repstat != 0) {
666 if ((nd->nd_repstat == NFSERR_DELAY &&
667 (nd->nd_flag & ND_NFSV4) &&
668 nd->nd_procnum != NFSPROC_SETATTR &&
669 nd->nd_procnum != NFSPROC_READ &&
670 nd->nd_procnum != NFSPROC_WRITE &&
671 nd->nd_procnum != NFSPROC_OPEN &&
672 nd->nd_procnum != NFSPROC_CREATE &&
673 nd->nd_procnum != NFSPROC_OPENCONFIRM &&
674 nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
675 nd->nd_procnum != NFSPROC_CLOSE &&
676 nd->nd_procnum != NFSPROC_LOCK &&
677 nd->nd_procnum != NFSPROC_LOCKU) ||
678 (nd->nd_repstat == NFSERR_DELAY &&
679 (nd->nd_flag & ND_NFSV4) == 0) ||
680 nd->nd_repstat == NFSERR_RESOURCE) {
681 if (trylater_delay > NFS_TRYLATERDEL)
682 trylater_delay = NFS_TRYLATERDEL;
683 waituntil = NFSD_MONOSEC + trylater_delay;
684 while (NFSD_MONOSEC < waituntil)
685 (void) nfs_catnap(PZERO, 0, "nfstry");
691 * If the File Handle was stale, invalidate the
692 * lookup cache, just in case.
693 * (vp != NULL implies a client side call)
695 if (nd->nd_repstat == ESTALE && vp != NULL) {
697 if (ncl_call_invalcaches != NULL)
698 (*ncl_call_invalcaches)(vp);
703 * Get rid of the tag, return count, and PUTFH result for V4.
705 if (nd->nd_flag & ND_NFSV4) {
706 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
707 i = fxdr_unsigned(int, *tl);
708 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
711 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
712 i = fxdr_unsigned(int, *++tl);
715 * If the first op's status is non-zero, mark that
716 * there is no more data to process.
719 nd->nd_flag |= ND_NOMOREDATA;
722 * If the first op is Putfh, throw its results away
723 * and toss the op# and status for the first op.
725 if (nmp != NULL && i == NFSV4OP_PUTFH && *tl == 0) {
726 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
727 i = fxdr_unsigned(int, *tl++);
728 j = fxdr_unsigned(int, *tl);
730 * All Compounds that do an Op that must
731 * be in sequence consist of NFSV4OP_PUTFH
732 * followed by one of these. As such, we
733 * can determine if the seqid# should be
736 if ((i == NFSV4OP_OPEN ||
737 i == NFSV4OP_OPENCONFIRM ||
738 i == NFSV4OP_OPENDOWNGRADE ||
739 i == NFSV4OP_CLOSE ||
741 i == NFSV4OP_LOCKU) &&
743 (j != NFSERR_STALECLIENTID &&
744 j != NFSERR_STALESTATEID &&
745 j != NFSERR_BADSTATEID &&
746 j != NFSERR_BADSEQID &&
747 j != NFSERR_BADXDR &&
748 j != NFSERR_RESOURCE &&
749 j != NFSERR_NOFILEHANDLE)))
750 nd->nd_flag |= ND_INCRSEQID;
752 * If the first op's status is non-zero, mark
753 * that there is no more data to process.
756 nd->nd_flag |= ND_NOMOREDATA;
760 * If R_DONTRECOVER is set, replace the stale error
761 * reply, so that recovery isn't initiated.
763 if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
764 nd->nd_repstat == NFSERR_STALESTATEID) &&
765 rep != NULL && (rep->r_flags & R_DONTRECOVER))
766 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
770 m_freem(nd->nd_mreq);
773 FREE((caddr_t)rep, M_NFSDREQ);
775 newnfs_restore_sigmask(td, &oldset);
778 mbuf_freem(nd->nd_mrep);
779 mbuf_freem(nd->nd_mreq);
782 FREE((caddr_t)rep, M_NFSDREQ);
784 newnfs_restore_sigmask(td, &oldset);
789 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
790 * wait for all requests to complete. This is used by forced unmounts
791 * to terminate any outstanding RPCs.
794 newnfs_nmcancelreqs(struct nfsmount *nmp)
797 if (nmp->nm_sockreq.nr_client != NULL)
798 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
803 * Any signal that can interrupt an NFS operation in an intr mount
804 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
806 int newnfs_sig_set[] = {
816 * Check to see if one of the signals in our subset is pending on
817 * the process (in an intr mount).
820 nfs_sig_pending(sigset_t set)
824 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
825 if (SIGISMEMBER(set, newnfs_sig_set[i]))
831 * The set/restore sigmask functions are used to (temporarily) overwrite
832 * the process p_sigmask during an RPC call (for example). These are also
833 * used in other places in the NFS client that might tsleep().
836 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
844 td = curthread; /* XXX */
846 /* Remove the NFS set of signals from newset */
848 mtx_lock(&p->p_sigacts->ps_mtx);
849 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
851 * But make sure we leave the ones already masked
852 * by the process, ie. remove the signal from the
853 * temporary signalmask only if it wasn't already
856 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
857 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
858 SIGDELSET(newset, newnfs_sig_set[i]);
860 mtx_unlock(&p->p_sigacts->ps_mtx);
862 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
866 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
869 td = curthread; /* XXX */
870 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
874 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
875 * old one after msleep() returns.
878 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
884 if ((priority & PCATCH) == 0)
885 return msleep(ident, mtx, priority, wmesg, timo);
887 td = curthread; /* XXX */
888 newnfs_set_sigmask(td, &oldset);
889 error = msleep(ident, mtx, priority, wmesg, timo);
890 newnfs_restore_sigmask(td, &oldset);
896 * Test for a termination condition pending on the process.
897 * This is used for NFSMNT_INT mounts.
900 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
905 /* Terminate all requests while attempting a forced unmount. */
906 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
908 if (!(nmp->nm_flag & NFSMNT_INT))
914 tmpset = p->p_siglist;
915 SIGSETOR(tmpset, td->td_siglist);
916 SIGSETNAND(tmpset, td->td_sigmask);
917 mtx_lock(&p->p_sigacts->ps_mtx);
918 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
919 mtx_unlock(&p->p_sigacts->ps_mtx);
920 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
921 && nfs_sig_pending(tmpset)) {
930 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
934 p = td ? td->td_proc : NULL;
936 tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
939 tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
945 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
946 int error, int flags)
950 mtx_lock(&nmp->nm_mtx);
951 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
952 nmp->nm_state |= NFSSTA_TIMEO;
953 mtx_unlock(&nmp->nm_mtx);
954 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
957 mtx_unlock(&nmp->nm_mtx);
958 mtx_lock(&nmp->nm_mtx);
959 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
960 nmp->nm_state |= NFSSTA_LOCKTIMEO;
961 mtx_unlock(&nmp->nm_mtx);
962 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
965 mtx_unlock(&nmp->nm_mtx);
966 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
970 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
971 int flags, int tprintfmsg)
976 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
979 mtx_lock(&nmp->nm_mtx);
980 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
981 nmp->nm_state &= ~NFSSTA_TIMEO;
982 mtx_unlock(&nmp->nm_mtx);
983 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
986 mtx_unlock(&nmp->nm_mtx);
988 mtx_lock(&nmp->nm_mtx);
989 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
990 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
991 mtx_unlock(&nmp->nm_mtx);
992 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
995 mtx_unlock(&nmp->nm_mtx);