2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1991, 1993, 1995
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Rick Macklem at The University of Guelph.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Socket operations for use by nfs
43 #include "opt_kgssapi.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
51 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/mutex.h>
56 #include <sys/signalvar.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
65 #include <kgssapi/krb5/kcrypto.h>
67 #include <fs/nfs/nfsport.h>
70 #include <sys/dtrace_bsd.h>
72 dtrace_nfsclient_nfs23_start_probe_func_t
73 dtrace_nfscl_nfs234_start_probe;
75 dtrace_nfsclient_nfs23_done_probe_func_t
76 dtrace_nfscl_nfs234_done_probe;
79 * Registered probes by RPC type.
81 uint32_t nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
82 uint32_t nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
84 uint32_t nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
85 uint32_t nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
87 uint32_t nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
88 uint32_t nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
95 extern struct nfsstatsv1 nfsstatsv1;
96 extern struct nfsreqhead nfsd_reqq;
97 extern int nfscl_ticks;
98 extern void (*ncl_call_invalcaches)(struct vnode *);
99 extern int nfs_numnfscbd;
100 extern int nfscl_debuglevel;
102 SVCPOOL *nfscbd_pool;
103 static int nfsrv_gsscallbackson = 0;
104 static int nfs_bufpackets = 4;
105 static int nfs_reconnects;
106 static int nfs3_jukebox_delay = 10;
107 static int nfs_skip_wcc_data_onerr = 1;
109 SYSCTL_DECL(_vfs_nfs);
111 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
112 "Buffer reservation size 2 < x < 64");
113 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
114 "Number of times the nfs client has had to reconnect");
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
116 "Number of seconds to delay a retry after receiving EJUKEBOX");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
118 "Disable weak cache consistency checking when server returns an error");
120 static void nfs_down(struct nfsmount *, struct thread *, const char *,
122 static void nfs_up(struct nfsmount *, struct thread *, const char *,
124 static int nfs_msg(struct thread *, const char *, const char *, int);
126 struct nfs_cached_auth {
127 int ca_refs; /* refcount, including 1 from the cache */
128 uid_t ca_uid; /* uid that corresponds to this auth */
129 AUTH *ca_auth; /* RPC auth handle */
132 static int nfsv2_procid[NFS_V3NPROCS] = {
158 * Initialize sockets and congestion for a new NFS connection.
159 * We do not free the sockaddr if error.
160 * Which arguments are set to NULL indicate what kind of call it is.
161 * cred == NULL --> a call to connect to a pNFS DS
162 * nmp == NULL --> indicates an upcall to userland or a NFSv4 callback
165 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
166 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
168 int rcvreserve, sndreserve;
169 int pktscale, pktscalesav;
170 struct sockaddr *saddr;
171 struct ucred *origcred;
173 struct netconfig *nconf;
175 int one = 1, retries, error = 0;
176 struct thread *td = curthread;
181 * We need to establish the socket using the credentials of
182 * the mountpoint. Some parts of this process (such as
183 * sobind() and soconnect()) will use the curent thread's
184 * credential instead of the socket credential. To work
185 * around this, temporarily change the current thread's
186 * credential to that of the mountpoint.
188 * XXX: It would be better to explicitly pass the correct
189 * credential to sobind() and soconnect().
191 origcred = td->td_ucred;
194 * Use the credential in nr_cred, if not NULL.
196 if (nrp->nr_cred != NULL)
197 td->td_ucred = nrp->nr_cred;
202 if (saddr->sa_family == AF_INET)
203 if (nrp->nr_sotype == SOCK_DGRAM)
204 nconf = getnetconfigent("udp");
206 nconf = getnetconfigent("tcp");
207 else if (saddr->sa_family == AF_LOCAL)
208 nconf = getnetconfigent("local");
210 if (nrp->nr_sotype == SOCK_DGRAM)
211 nconf = getnetconfigent("udp6");
213 nconf = getnetconfigent("tcp6");
215 pktscale = nfs_bufpackets;
220 pktscalesav = pktscale;
222 * soreserve() can fail if sb_max is too small, so shrink pktscale
223 * and try again if there is an error.
224 * Print a log message suggesting increasing sb_max.
225 * Creating a socket and doing this is necessary since, if the
226 * reservation sizes are too large and will make soreserve() fail,
227 * the connection will work until a large send is attempted and
228 * then it will loop in the krpc code.
231 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
232 error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
233 nrp->nr_soproto, td->td_ucred, td);
235 td->td_ucred = origcred;
239 if (error != 0 && pktscale > 2) {
240 if (nmp != NULL && nrp->nr_sotype == SOCK_STREAM &&
241 pktscale == pktscalesav)
242 printf("Consider increasing kern.ipc.maxsockbuf\n");
245 if (nrp->nr_sotype == SOCK_DGRAM) {
247 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
249 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
252 sndreserve = rcvreserve = 1024 * pktscale;
255 if (nrp->nr_sotype != SOCK_STREAM)
256 panic("nfscon sotype");
258 sndreserve = (NFS_MAXBSIZE + NFS_MAXXDR +
259 sizeof (u_int32_t)) * pktscale;
260 rcvreserve = (NFS_MAXBSIZE + NFS_MAXXDR +
261 sizeof (u_int32_t)) * pktscale;
263 sndreserve = rcvreserve = 1024 * pktscale;
266 error = soreserve(so, sndreserve, rcvreserve);
267 if (error != 0 && nmp != NULL && nrp->nr_sotype == SOCK_STREAM &&
269 printf("Must increase kern.ipc.maxsockbuf or reduce"
271 } while (error != 0 && pktscale > 2);
274 td->td_ucred = origcred;
278 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
279 nrp->nr_vers, sndreserve, rcvreserve);
280 CLNT_CONTROL(client, CLSET_WAITCHAN, "nfsreq");
282 if ((nmp->nm_flag & NFSMNT_INT))
283 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
284 if ((nmp->nm_flag & NFSMNT_RESVPORT))
285 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
286 if (NFSHASSOFT(nmp)) {
287 if (nmp->nm_sotype == SOCK_DGRAM)
289 * For UDP, the large timeout for a reconnect
290 * will be set to "nm_retry * nm_timeo / 2", so
291 * we only want to do 2 reconnect timeout
296 retries = nmp->nm_retry;
299 if (NFSHASNFSV4N(nmp)) {
302 * Make sure the nfscbd_pool doesn't get
303 * destroyed while doing this.
306 if (nfs_numnfscbd > 0) {
309 xprt = svc_vc_create_backchannel(
311 CLNT_CONTROL(client, CLSET_BACKCHANNEL,
315 if (nfs_numnfscbd == 0)
316 wakeup(&nfs_numnfscbd);
321 * cred == NULL for a DS connect.
322 * For connects to a DS, set a retry limit
323 * so that failed DSs will be detected.
324 * This is ok for NFSv4.1, since a DS does
325 * not maintain open/lock state and is the
326 * only case where using a "soft" mount is
327 * recommended for NFSv4.
335 * - Null RPC callback to client
336 * - Non-Null RPC callback to client, wait a little longer
337 * - upcalls to nfsuserd and gssd (clp == NULL)
339 if (callback_retry_mult == 0) {
340 retries = NFSV4_UPCALLRETRY;
341 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
343 retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
346 CLNT_CONTROL(client, CLSET_RETRIES, &retries);
350 * For UDP, there are 2 timeouts:
351 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
352 * that does a retransmit of an RPC request using the same
353 * socket and xid. This is what you normally want to do,
354 * since NFS servers depend on "same xid" for their
355 * Duplicate Request Cache.
356 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
357 * retransmits on the same socket should fail and a fresh
358 * socket created. Each of these timeouts counts as one
359 * CLSET_RETRIES as set above.
360 * Set the initial retransmit timeout for UDP. This timeout
361 * doesn't exist for TCP and the following call just fails,
364 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
365 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
366 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
369 mtx_lock(&nrp->nr_mtx);
370 if (nrp->nr_client != NULL) {
371 mtx_unlock(&nrp->nr_mtx);
373 * Someone else already connected.
375 CLNT_RELEASE(client);
377 nrp->nr_client = client;
379 * Protocols that do not require connections may be optionally
380 * left unconnected for servers that reply from a port other
383 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
384 mtx_unlock(&nrp->nr_mtx);
385 CLNT_CONTROL(client, CLSET_CONNECT, &one);
387 mtx_unlock(&nrp->nr_mtx);
391 /* Restore current thread's credentials. */
392 td->td_ucred = origcred;
400 * NFS disconnect. Clean up and unlink.
403 newnfs_disconnect(struct nfssockreq *nrp)
407 mtx_lock(&nrp->nr_mtx);
408 if (nrp->nr_client != NULL) {
409 client = nrp->nr_client;
410 nrp->nr_client = NULL;
411 mtx_unlock(&nrp->nr_mtx);
412 rpc_gss_secpurge_call(client);
414 CLNT_RELEASE(client);
416 mtx_unlock(&nrp->nr_mtx);
421 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
422 char *srv_principal, gss_OID mech_oid, struct ucred *cred)
424 rpc_gss_service_t svc;
427 switch (secflavour) {
428 case RPCSEC_GSS_KRB5:
429 case RPCSEC_GSS_KRB5I:
430 case RPCSEC_GSS_KRB5P:
432 if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
435 if (secflavour == RPCSEC_GSS_KRB5)
436 svc = rpc_gss_svc_none;
437 else if (secflavour == RPCSEC_GSS_KRB5I)
438 svc = rpc_gss_svc_integrity;
440 svc = rpc_gss_svc_privacy;
442 if (clnt_principal == NULL)
443 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
444 srv_principal, mech_oid, svc);
446 auth = rpc_gss_seccreate_call(nrp->nr_client, cred,
447 clnt_principal, srv_principal, "kerberosv5",
448 svc, NULL, NULL, NULL);
456 return (authunix_create(cred));
462 * Callback from the RPC code to generate up/down notifications.
465 struct nfs_feedback_arg {
466 struct nfsmount *nf_mount;
467 int nf_lastmsg; /* last tprintf */
469 struct thread *nf_td;
473 nfs_feedback(int type, int proc, void *arg)
475 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
476 struct nfsmount *nmp = nf->nf_mount;
480 case FEEDBACK_REXMIT2:
481 case FEEDBACK_RECONNECT:
483 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now) {
484 nfs_down(nmp, nf->nf_td,
485 "not responding", 0, NFSSTA_TIMEO);
486 nf->nf_tprintfmsg = TRUE;
487 nf->nf_lastmsg = now;
492 nfs_up(nf->nf_mount, nf->nf_td,
493 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
499 * newnfs_request - goes something like this
500 * - does the rpc by calling the krpc layer
501 * - break down rpc header and return with nfs reply
502 * nb: always frees up nd_mreq mbuf list
505 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
506 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
507 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
508 u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *dssep)
510 uint32_t retseq, retval, slotseq, *tl;
512 int i = 0, j = 0, opcnt, set_sigset = 0, slot;
513 int error = 0, usegssname = 0, secflavour = AUTH_SYS;
514 int freeslot, maxslot, reterr, slotpos, timeo;
516 u_int trylater_delay = 1;
517 struct nfs_feedback_arg nf;
520 struct rpc_callextra ext;
522 struct nfsreq *rep = NULL;
523 char *srv_principal = NULL, *clnt_principal = NULL;
525 struct ucred *authcred;
526 struct nfsclsession *sep;
527 uint8_t sessionid[NFSX_V4SESSIONID];
532 /* Reject requests while attempting a forced unmount. */
533 if (nmp != NULL && NFSCL_FORCEDISM(nmp->nm_mountp)) {
534 m_freem(nd->nd_mreq);
539 * Set authcred, which is used to acquire RPC credentials to
540 * the cred argument, by default. The crhold() should not be
541 * necessary, but will ensure that some future code change
542 * doesn't result in the credential being free'd prematurely.
544 authcred = crhold(cred);
546 /* For client side interruptible mounts, mask off the signals. */
547 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
548 newnfs_set_sigmask(td, &oldset);
553 * XXX if not already connected call nfs_connect now. Longer
554 * term, change nfs_mount to call nfs_connect unconditionally
555 * and let clnt_reconnect_create handle reconnects.
557 if (nrp->nr_client == NULL)
558 newnfs_connect(nmp, nrp, cred, td, 0);
561 * For a client side mount, nmp is != NULL and clp == NULL. For
562 * server calls (callbacks or upcalls), nmp == NULL.
566 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
567 secflavour = RPCSEC_GSS_KRB5;
568 if (nd->nd_procnum != NFSPROC_NULL) {
569 if (clp->lc_flags & LCL_GSSINTEGRITY)
570 secflavour = RPCSEC_GSS_KRB5I;
571 else if (clp->lc_flags & LCL_GSSPRIVACY)
572 secflavour = RPCSEC_GSS_KRB5P;
576 } else if (nmp != NULL && NFSHASKERB(nmp) &&
577 nd->nd_procnum != NFSPROC_NULL) {
578 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
579 nd->nd_flag |= ND_USEGSSNAME;
580 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
582 * If there is a client side host based credential,
583 * use that, otherwise use the system uid, if set.
584 * The system uid is in the nmp->nm_sockreq.nr_cred
587 if (nmp->nm_krbnamelen > 0) {
589 clnt_principal = nmp->nm_krbname;
590 } else if (nmp->nm_uid != (uid_t)-1) {
591 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
592 ("newnfs_request: NULL nr_cred"));
594 authcred = crhold(nmp->nm_sockreq.nr_cred);
596 } else if (nmp->nm_krbnamelen == 0 &&
597 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
599 * If there is no host based principal name and
600 * the system uid is set and this is root, use the
601 * system uid, since root won't have user
602 * credentials in a credentials cache file.
603 * The system uid is in the nmp->nm_sockreq.nr_cred
606 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
607 ("newnfs_request: NULL nr_cred"));
609 authcred = crhold(nmp->nm_sockreq.nr_cred);
611 if (NFSHASINTEGRITY(nmp))
612 secflavour = RPCSEC_GSS_KRB5I;
613 else if (NFSHASPRIVACY(nmp))
614 secflavour = RPCSEC_GSS_KRB5P;
616 secflavour = RPCSEC_GSS_KRB5;
617 srv_principal = NFSMNT_SRVKRBNAME(nmp);
618 } else if (nmp != NULL && !NFSHASKERB(nmp) &&
619 nd->nd_procnum != NFSPROC_NULL &&
620 (nd->nd_flag & ND_USEGSSNAME) != 0) {
622 * Use the uid that did the mount when the RPC is doing
623 * NFSv4 system operations, as indicated by the
624 * ND_USEGSSNAME flag, for the AUTH_SYS case.
625 * The credentials in nm_sockreq.nr_cred were used for the
628 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
629 ("newnfs_request: NULL nr_cred"));
631 authcred = crhold(nmp->nm_sockreq.nr_cred);
635 bzero(&nf, sizeof(struct nfs_feedback_arg));
638 nf.nf_lastmsg = NFSD_MONOSEC -
639 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
642 if (nd->nd_procnum == NFSPROC_NULL)
643 auth = authnone_create();
644 else if (usegssname) {
646 * For this case, the authenticator is held in the
647 * nfssockreq structure, so don't release the reference count
648 * held on it. --> Don't AUTH_DESTROY() it in this function.
650 if (nrp->nr_auth == NULL)
651 nrp->nr_auth = nfs_getauth(nrp, secflavour,
652 clnt_principal, srv_principal, NULL, authcred);
654 rpc_gss_refresh_auth_call(nrp->nr_auth);
657 auth = nfs_getauth(nrp, secflavour, NULL,
658 srv_principal, NULL, authcred);
661 m_freem(nd->nd_mreq);
663 newnfs_restore_sigmask(td, &oldset);
666 bzero(&ext, sizeof(ext));
669 ext.rc_feedback = nfs_feedback;
670 ext.rc_feedback_arg = &nf;
673 procnum = nd->nd_procnum;
674 if ((nd->nd_flag & ND_NFSV4) &&
675 nd->nd_procnum != NFSPROC_NULL &&
676 nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
677 procnum = NFSV4PROC_COMPOUND;
680 NFSINCRGLOBAL(nfsstatsv1.rpcrequests);
682 /* Map the procnum to the old NFSv2 one, as required. */
683 if ((nd->nd_flag & ND_NFSV2) != 0) {
684 if (nd->nd_procnum < NFS_V3NPROCS)
685 procnum = nfsv2_procid[nd->nd_procnum];
687 procnum = NFSV2PROC_NOOP;
691 * Now only used for the R_DONTRECOVER case, but until that is
692 * supported within the krpc code, I need to keep a queue of
693 * outstanding RPCs for nfsv4 client requests.
695 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
696 rep = malloc(sizeof(struct nfsreq),
697 M_NFSDREQ, M_WAITOK);
699 if (dtrace_nfscl_nfs234_start_probe != NULL) {
703 if (nd->nd_flag & ND_NFSV4) {
705 nfscl_nfs4_start_probes[nd->nd_procnum];
706 probe_procnum = nd->nd_procnum;
707 } else if (nd->nd_flag & ND_NFSV3) {
708 probe_id = nfscl_nfs3_start_probes[procnum];
709 probe_procnum = procnum;
712 nfscl_nfs2_start_probes[nd->nd_procnum];
713 probe_procnum = procnum;
716 (dtrace_nfscl_nfs234_start_probe)
717 (probe_id, vp, nd->nd_mreq, cred,
722 freeslot = -1; /* Set to slot that needs to be free'd */
724 slot = -1; /* Slot that needs a sequence# increment. */
726 * This timeout specifies when a new socket should be created,
727 * along with new xid values. For UDP, this should be done
728 * infrequently, since retransmits of RPC requests should normally
734 timo.tv_sec = NFSV4_UPCALLTIMEO;
736 timo.tv_sec = NFSV4_CALLBACKTIMEO;
738 if (nrp->nr_sotype != SOCK_DGRAM) {
740 if ((nmp->nm_flag & NFSMNT_NFSV4))
741 timo.tv_sec = INT_MAX;
743 timo.tv_sec = NFS_TCPTIMEO;
745 if (NFSHASSOFT(nmp)) {
747 * CLSET_RETRIES is set to 2, so this should be
748 * half of the total timeout required.
750 timeo = nmp->nm_retry * nmp->nm_timeo / 2;
753 timo.tv_sec = timeo / NFS_HZ;
754 timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
757 /* For UDP hard mounts, use a large value. */
758 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
767 * Chain request into list of outstanding requests.
770 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
776 if (clp != NULL && sep != NULL)
777 stat = clnt_bck_call(nrp->nr_client, &ext, procnum,
778 nd->nd_mreq, &nd->nd_mrep, timo, sep->nfsess_xprt);
780 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum,
781 nd->nd_mreq, &nd->nd_mrep, timo);
782 NFSCL_DEBUG(2, "clnt call=%d\n", stat);
786 * RPC done, unlink the request.
789 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
794 * If there was a successful reply and a tprintf msg.
795 * tprintf a response.
797 if (stat == RPC_SUCCESS) {
799 } else if (stat == RPC_TIMEDOUT) {
800 NFSINCRGLOBAL(nfsstatsv1.rpctimeouts);
802 } else if (stat == RPC_VERSMISMATCH) {
803 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
805 } else if (stat == RPC_PROGVERSMISMATCH) {
806 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
807 error = EPROTONOSUPPORT;
808 } else if (stat == RPC_INTR) {
810 } else if (stat == RPC_CANTSEND || stat == RPC_CANTRECV ||
811 stat == RPC_SYSTEMERROR) {
812 if ((nd->nd_flag & ND_NFSV41) != 0 && nmp != NULL &&
813 nd->nd_procnum != NFSPROC_NULL) {
815 * The nfsess_defunct field is protected by
816 * the NFSLOCKMNT()/nm_mtx lock and not the
817 * nfsess_mtx lock to simplify its handling,
818 * for the MDS session. This lock is also
819 * sufficient for nfsess_sessionid, since it
820 * never changes in the structure.
824 /* The session must be marked defunct. */
827 * This is either an MDS proxy operation or
828 * a client mount with "soft,retrans=N" options.
829 * Mark the MDS session defunct and initiate
830 * recovery, as required.
832 NFSCL_DEBUG(1, "Failed soft proxy RPC\n");
833 sep = NFSMNT_MDSSESSION(nmp);
834 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence,
835 NFSX_V4SESSIONID) == 0) {
836 /* Initiate recovery. */
837 sep->nfsess_defunct = 1;
838 NFSCL_DEBUG(1, "Marked defunct\n");
839 if (nmp->nm_clp != NULL) {
840 nmp->nm_clp->nfsc_flags |=
847 * This is a client side DS RPC. Just mark
848 * the session defunct. A subsequent LayoutGet
849 * should get a new session.
851 NFSCL_DEBUG(1, "Failed client DS RPC\n");
852 if (bcmp(dssep->nfsess_sessionid,
853 nd->nd_sequence, NFSX_V4SESSIONID) == 0) {
854 /* Mark it defunct. */
855 dssep->nfsess_defunct = 1;
856 NFSCL_DEBUG(1, "Marked defunct\n");
862 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
865 NFSINCRGLOBAL(nfsstatsv1.rpcinvalid);
869 m_freem(nd->nd_mreq);
873 free(rep, M_NFSDREQ);
875 newnfs_restore_sigmask(td, &oldset);
879 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
882 * Search for any mbufs that are not a multiple of 4 bytes long
883 * or with m_data not longword aligned.
884 * These could cause pointer alignment problems, so copy them to
885 * well aligned mbufs.
887 newnfs_realign(&nd->nd_mrep, M_WAITOK);
888 nd->nd_md = nd->nd_mrep;
889 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
891 if (nd->nd_procnum != NFSPROC_NULL &&
892 nd->nd_procnum != NFSV4PROC_CBNULL) {
893 /* If sep == NULL, set it to the default in nmp. */
894 if (sep == NULL && nmp != NULL)
895 sep = nfsmnt_mdssession(nmp);
897 * and now the actual NFS xdr.
899 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
900 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
901 if (nd->nd_repstat >= 10000)
902 NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
903 (int)nd->nd_repstat);
906 * Get rid of the tag, return count and SEQUENCE result for
909 if ((nd->nd_flag & ND_NFSV4) != 0) {
910 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
911 i = fxdr_unsigned(int, *tl);
912 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
915 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
916 opcnt = fxdr_unsigned(int, *tl++);
917 i = fxdr_unsigned(int, *tl++);
918 j = fxdr_unsigned(int, *tl);
920 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
922 * If the first op is Sequence, free up the slot.
924 if ((nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0) ||
925 (clp != NULL && i == NFSV4OP_CBSEQUENCE && j != 0))
926 NFSCL_DEBUG(1, "failed seq=%d\n", j);
927 if (((nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) ||
928 (clp != NULL && i == NFSV4OP_CBSEQUENCE &&
929 j == 0)) && sep != NULL) {
930 if (i == NFSV4OP_SEQUENCE)
931 NFSM_DISSECT(tl, uint32_t *,
935 NFSM_DISSECT(tl, uint32_t *,
938 mtx_lock(&sep->nfsess_mtx);
939 if (bcmp(tl, sep->nfsess_sessionid,
940 NFSX_V4SESSIONID) == 0) {
941 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
942 retseq = fxdr_unsigned(uint32_t, *tl++);
943 slot = fxdr_unsigned(int, *tl++);
945 if (retseq != sep->nfsess_slotseq[slot])
946 printf("retseq diff 0x%x\n",
948 retval = fxdr_unsigned(uint32_t, *++tl);
949 if ((retval + 1) < sep->nfsess_foreslots
951 sep->nfsess_foreslots = (retval
953 else if ((retval + 1) >
954 sep->nfsess_foreslots)
955 sep->nfsess_foreslots = (retval
956 < 64) ? (retval + 1) : 64;
958 mtx_unlock(&sep->nfsess_mtx);
960 /* Grab the op and status for the next one. */
962 NFSM_DISSECT(tl, uint32_t *,
964 i = fxdr_unsigned(int, *tl++);
965 j = fxdr_unsigned(int, *tl);
969 if (nd->nd_repstat != 0) {
970 if (nd->nd_repstat == NFSERR_BADSESSION &&
971 nmp != NULL && dssep == NULL &&
972 (nd->nd_flag & ND_NFSV41) != 0) {
974 * If this is a client side MDS RPC, mark
975 * the MDS session defunct and initiate
976 * recovery, as required.
977 * The nfsess_defunct field is protected by
978 * the NFSLOCKMNT()/nm_mtx lock and not the
979 * nfsess_mtx lock to simplify its handling,
980 * for the MDS session. This lock is also
981 * sufficient for nfsess_sessionid, since it
982 * never changes in the structure.
984 NFSCL_DEBUG(1, "Got badsession\n");
987 sep = NFSMNT_MDSSESSION(nmp);
988 if (bcmp(sep->nfsess_sessionid, nd->nd_sequence,
989 NFSX_V4SESSIONID) == 0) {
990 /* Initiate recovery. */
991 sep->nfsess_defunct = 1;
992 NFSCL_DEBUG(1, "Marked defunct\n");
993 if (nmp->nm_clp != NULL) {
994 nmp->nm_clp->nfsc_flags |=
1001 * Sleep for up to 1sec waiting for a new
1004 mtx_sleep(&nmp->nm_sess, &nmp->nm_mtx, PZERO,
1007 * Get the session again, in case a new one
1008 * has been created during the sleep.
1010 sep = NFSMNT_MDSSESSION(nmp);
1012 if ((nd->nd_flag & ND_LOOPBADSESS) != 0) {
1013 reterr = nfsv4_sequencelookup(nmp, sep,
1014 &slotpos, &maxslot, &slotseq,
1017 /* Fill in new session info. */
1019 "Filling in new sequence\n");
1020 tl = nd->nd_sequence;
1021 bcopy(sessionid, tl,
1023 tl += NFSX_V4SESSIONID /
1025 *tl++ = txdr_unsigned(slotseq);
1026 *tl++ = txdr_unsigned(slotpos);
1027 *tl = txdr_unsigned(maxslot);
1029 if (reterr == NFSERR_BADSESSION ||
1032 "Badsession looping\n");
1033 m_freem(nd->nd_mrep);
1037 nd->nd_repstat = reterr;
1038 NFSCL_DEBUG(1, "Got err=%d\n", reterr);
1041 if (((nd->nd_repstat == NFSERR_DELAY ||
1042 nd->nd_repstat == NFSERR_GRACE) &&
1043 (nd->nd_flag & ND_NFSV4) &&
1044 nd->nd_procnum != NFSPROC_DELEGRETURN &&
1045 nd->nd_procnum != NFSPROC_SETATTR &&
1046 nd->nd_procnum != NFSPROC_READ &&
1047 nd->nd_procnum != NFSPROC_READDS &&
1048 nd->nd_procnum != NFSPROC_WRITE &&
1049 nd->nd_procnum != NFSPROC_WRITEDS &&
1050 nd->nd_procnum != NFSPROC_OPEN &&
1051 nd->nd_procnum != NFSPROC_CREATE &&
1052 nd->nd_procnum != NFSPROC_OPENCONFIRM &&
1053 nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
1054 nd->nd_procnum != NFSPROC_CLOSE &&
1055 nd->nd_procnum != NFSPROC_LOCK &&
1056 nd->nd_procnum != NFSPROC_LOCKU) ||
1057 (nd->nd_repstat == NFSERR_DELAY &&
1058 (nd->nd_flag & ND_NFSV4) == 0) ||
1059 nd->nd_repstat == NFSERR_RESOURCE) {
1060 if (trylater_delay > NFS_TRYLATERDEL)
1061 trylater_delay = NFS_TRYLATERDEL;
1062 waituntil = NFSD_MONOSEC + trylater_delay;
1063 while (NFSD_MONOSEC < waituntil)
1064 (void) nfs_catnap(PZERO, 0, "nfstry");
1065 trylater_delay *= 2;
1067 mtx_lock(&sep->nfsess_mtx);
1068 sep->nfsess_slotseq[slot]++;
1069 *nd->nd_slotseq = txdr_unsigned(
1070 sep->nfsess_slotseq[slot]);
1071 mtx_unlock(&sep->nfsess_mtx);
1073 m_freem(nd->nd_mrep);
1079 * If the File Handle was stale, invalidate the
1080 * lookup cache, just in case.
1081 * (vp != NULL implies a client side call)
1083 if (nd->nd_repstat == ESTALE && vp != NULL) {
1085 if (ncl_call_invalcaches != NULL)
1086 (*ncl_call_invalcaches)(vp);
1089 if ((nd->nd_flag & ND_NFSV4) != 0) {
1090 /* Free the slot, as required. */
1092 nfsv4_freeslot(sep, freeslot);
1094 * If this op is Putfh, throw its results away.
1097 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
1098 if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
1099 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
1100 i = fxdr_unsigned(int, *tl++);
1101 j = fxdr_unsigned(int, *tl);
1103 NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
1106 * All Compounds that do an Op that must
1107 * be in sequence consist of NFSV4OP_PUTFH
1108 * followed by one of these. As such, we
1109 * can determine if the seqid# should be
1110 * incremented, here.
1112 if ((i == NFSV4OP_OPEN ||
1113 i == NFSV4OP_OPENCONFIRM ||
1114 i == NFSV4OP_OPENDOWNGRADE ||
1115 i == NFSV4OP_CLOSE ||
1116 i == NFSV4OP_LOCK ||
1117 i == NFSV4OP_LOCKU) &&
1119 (j != NFSERR_STALECLIENTID &&
1120 j != NFSERR_STALESTATEID &&
1121 j != NFSERR_BADSTATEID &&
1122 j != NFSERR_BADSEQID &&
1123 j != NFSERR_BADXDR &&
1124 j != NFSERR_RESOURCE &&
1125 j != NFSERR_NOFILEHANDLE)))
1126 nd->nd_flag |= ND_INCRSEQID;
1129 * If this op's status is non-zero, mark
1130 * that there is no more data to process.
1131 * The exception is Setattr, which always has xdr
1132 * when it has failed.
1134 if (j != 0 && i != NFSV4OP_SETATTR)
1135 nd->nd_flag |= ND_NOMOREDATA;
1138 * If R_DONTRECOVER is set, replace the stale error
1139 * reply, so that recovery isn't initiated.
1141 if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
1142 nd->nd_repstat == NFSERR_BADSESSION ||
1143 nd->nd_repstat == NFSERR_STALESTATEID) &&
1144 rep != NULL && (rep->r_flags & R_DONTRECOVER))
1145 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
1149 #ifdef KDTRACE_HOOKS
1150 if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
1154 if (nd->nd_flag & ND_NFSV4) {
1155 probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
1156 probe_procnum = nd->nd_procnum;
1157 } else if (nd->nd_flag & ND_NFSV3) {
1158 probe_id = nfscl_nfs3_done_probes[procnum];
1159 probe_procnum = procnum;
1161 probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
1162 probe_procnum = procnum;
1165 (dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
1166 nd->nd_mreq, cred, probe_procnum, 0);
1170 m_freem(nd->nd_mreq);
1171 if (usegssname == 0)
1174 free(rep, M_NFSDREQ);
1176 newnfs_restore_sigmask(td, &oldset);
1179 mbuf_freem(nd->nd_mrep);
1180 mbuf_freem(nd->nd_mreq);
1181 if (usegssname == 0)
1184 free(rep, M_NFSDREQ);
1186 newnfs_restore_sigmask(td, &oldset);
1191 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1192 * wait for all requests to complete. This is used by forced unmounts
1193 * to terminate any outstanding RPCs.
1196 newnfs_nmcancelreqs(struct nfsmount *nmp)
1198 struct nfsclds *dsp;
1199 struct __rpc_client *cl;
1201 if (nmp->nm_sockreq.nr_client != NULL)
1202 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1205 TAILQ_FOREACH(dsp, &nmp->nm_sess, nfsclds_list) {
1207 if (dsp != TAILQ_FIRST(&nmp->nm_sess) &&
1208 (dsp->nfsclds_flags & NFSCLDS_CLOSED) == 0 &&
1209 dsp->nfsclds_sockp != NULL &&
1210 dsp->nfsclds_sockp->nr_client != NULL) {
1211 dsp->nfsclds_flags |= NFSCLDS_CLOSED;
1212 cl = dsp->nfsclds_sockp->nr_client;
1225 * Any signal that can interrupt an NFS operation in an intr mount
1226 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1228 int newnfs_sig_set[] = {
1237 * Check to see if one of the signals in our subset is pending on
1238 * the process (in an intr mount).
1241 nfs_sig_pending(sigset_t set)
1245 for (i = 0 ; i < nitems(newnfs_sig_set); i++)
1246 if (SIGISMEMBER(set, newnfs_sig_set[i]))
1252 * The set/restore sigmask functions are used to (temporarily) overwrite
1253 * the thread td_sigmask during an RPC call (for example). These are also
1254 * used in other places in the NFS client that might tsleep().
1257 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1265 td = curthread; /* XXX */
1267 /* Remove the NFS set of signals from newset */
1269 mtx_lock(&p->p_sigacts->ps_mtx);
1270 for (i = 0 ; i < nitems(newnfs_sig_set); i++) {
1272 * But make sure we leave the ones already masked
1273 * by the process, ie. remove the signal from the
1274 * temporary signalmask only if it wasn't already
1277 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1278 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1279 SIGDELSET(newset, newnfs_sig_set[i]);
1281 mtx_unlock(&p->p_sigacts->ps_mtx);
1282 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset,
1283 SIGPROCMASK_PROC_LOCKED);
1288 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1291 td = curthread; /* XXX */
1292 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1296 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1297 * old one after msleep() returns.
1300 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1305 if ((priority & PCATCH) == 0)
1306 return msleep(ident, mtx, priority, wmesg, timo);
1308 td = curthread; /* XXX */
1309 newnfs_set_sigmask(td, &oldset);
1310 error = msleep(ident, mtx, priority, wmesg, timo);
1311 newnfs_restore_sigmask(td, &oldset);
1316 * Test for a termination condition pending on the process.
1317 * This is used for NFSMNT_INT mounts.
1320 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1325 /* Terminate all requests while attempting a forced unmount. */
1326 if (NFSCL_FORCEDISM(nmp->nm_mountp))
1328 if (!(nmp->nm_flag & NFSMNT_INT))
1334 tmpset = p->p_siglist;
1335 SIGSETOR(tmpset, td->td_siglist);
1336 SIGSETNAND(tmpset, td->td_sigmask);
1337 mtx_lock(&p->p_sigacts->ps_mtx);
1338 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1339 mtx_unlock(&p->p_sigacts->ps_mtx);
1340 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1341 && nfs_sig_pending(tmpset)) {
1350 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1354 p = td ? td->td_proc : NULL;
1356 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n",
1357 server, msg, error);
1359 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1365 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1366 int error, int flags)
1370 mtx_lock(&nmp->nm_mtx);
1371 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1372 nmp->nm_state |= NFSSTA_TIMEO;
1373 mtx_unlock(&nmp->nm_mtx);
1374 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1377 mtx_unlock(&nmp->nm_mtx);
1378 mtx_lock(&nmp->nm_mtx);
1379 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1380 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1381 mtx_unlock(&nmp->nm_mtx);
1382 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1385 mtx_unlock(&nmp->nm_mtx);
1386 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1390 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1391 int flags, int tprintfmsg)
1396 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1399 mtx_lock(&nmp->nm_mtx);
1400 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1401 nmp->nm_state &= ~NFSSTA_TIMEO;
1402 mtx_unlock(&nmp->nm_mtx);
1403 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1406 mtx_unlock(&nmp->nm_mtx);
1408 mtx_lock(&nmp->nm_mtx);
1409 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1410 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1411 mtx_unlock(&nmp->nm_mtx);
1412 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1415 mtx_unlock(&nmp->nm_mtx);