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_kdtrace.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>
68 #include <sys/dtrace_bsd.h>
70 dtrace_nfsclient_nfs23_start_probe_func_t
71 dtrace_nfscl_nfs234_start_probe;
73 dtrace_nfsclient_nfs23_done_probe_func_t
74 dtrace_nfscl_nfs234_done_probe;
77 * Registered probes by RPC type.
79 uint32_t nfscl_nfs2_start_probes[NFSV41_NPROCS + 1];
80 uint32_t nfscl_nfs2_done_probes[NFSV41_NPROCS + 1];
82 uint32_t nfscl_nfs3_start_probes[NFSV41_NPROCS + 1];
83 uint32_t nfscl_nfs3_done_probes[NFSV41_NPROCS + 1];
85 uint32_t nfscl_nfs4_start_probes[NFSV41_NPROCS + 1];
86 uint32_t nfscl_nfs4_done_probes[NFSV41_NPROCS + 1];
92 extern struct nfsstats newnfsstats;
93 extern struct nfsreqhead nfsd_reqq;
94 extern int nfscl_ticks;
95 extern void (*ncl_call_invalcaches)(struct vnode *);
96 extern int nfs_numnfscbd;
97 extern int nfscl_debuglevel;
100 static int nfsrv_gsscallbackson = 0;
101 static int nfs_bufpackets = 4;
102 static int nfs_reconnects;
103 static int nfs3_jukebox_delay = 10;
104 static int nfs_skip_wcc_data_onerr = 1;
105 static int nfs_keytab_enctype = ETYPE_DES_CBC_CRC;
107 SYSCTL_DECL(_vfs_nfs);
109 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0,
110 "Buffer reservation size 2 < x < 64");
111 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
112 "Number of times the nfs client has had to reconnect");
113 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
114 "Number of seconds to delay a retry after receiving EJUKEBOX");
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0,
116 "Disable weak cache consistency checking when server returns an error");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, keytab_enctype, CTLFLAG_RW, &nfs_keytab_enctype, 0,
118 "Encryption type for the keytab entry used by nfs");
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.
162 newnfs_connect(struct nfsmount *nmp, struct nfssockreq *nrp,
163 struct ucred *cred, NFSPROC_T *p, int callback_retry_mult)
165 int rcvreserve, sndreserve;
167 struct sockaddr *saddr;
168 struct ucred *origcred;
170 struct netconfig *nconf;
172 int one = 1, retries, error = 0;
173 struct thread *td = curthread;
178 * We need to establish the socket using the credentials of
179 * the mountpoint. Some parts of this process (such as
180 * sobind() and soconnect()) will use the curent thread's
181 * credential instead of the socket credential. To work
182 * around this, temporarily change the current thread's
183 * credential to that of the mountpoint.
185 * XXX: It would be better to explicitly pass the correct
186 * credential to sobind() and soconnect().
188 origcred = td->td_ucred;
191 * Use the credential in nr_cred, if not NULL.
193 if (nrp->nr_cred != NULL)
194 td->td_ucred = nrp->nr_cred;
199 if (saddr->sa_family == AF_INET)
200 if (nrp->nr_sotype == SOCK_DGRAM)
201 nconf = getnetconfigent("udp");
203 nconf = getnetconfigent("tcp");
205 if (nrp->nr_sotype == SOCK_DGRAM)
206 nconf = getnetconfigent("udp6");
208 nconf = getnetconfigent("tcp6");
210 pktscale = nfs_bufpackets;
216 * soreserve() can fail if sb_max is too small, so shrink pktscale
217 * and try again if there is an error.
218 * Print a log message suggesting increasing sb_max.
219 * Creating a socket and doing this is necessary since, if the
220 * reservation sizes are too large and will make soreserve() fail,
221 * the connection will work until a large send is attempted and
222 * then it will loop in the krpc code.
225 saddr = NFSSOCKADDR(nrp->nr_nam, struct sockaddr *);
226 error = socreate(saddr->sa_family, &so, nrp->nr_sotype,
227 nrp->nr_soproto, td->td_ucred, td);
229 td->td_ucred = origcred;
233 if (error != 0 && pktscale > 2)
235 if (nrp->nr_sotype == SOCK_DGRAM) {
237 sndreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
239 rcvreserve = (NFS_MAXDGRAMDATA + NFS_MAXPKTHDR) *
242 sndreserve = rcvreserve = 1024 * pktscale;
245 if (nrp->nr_sotype != SOCK_STREAM)
246 panic("nfscon sotype");
248 sndreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
249 sizeof (u_int32_t)) * pktscale;
250 rcvreserve = (NFS_MAXBSIZE + NFS_MAXPKTHDR +
251 sizeof (u_int32_t)) * pktscale;
253 sndreserve = rcvreserve = 1024 * pktscale;
256 error = soreserve(so, sndreserve, rcvreserve);
257 } while (error != 0 && pktscale > 2);
260 td->td_ucred = origcred;
264 client = clnt_reconnect_create(nconf, saddr, nrp->nr_prog,
265 nrp->nr_vers, sndreserve, rcvreserve);
266 CLNT_CONTROL(client, CLSET_WAITCHAN, "newnfsreq");
268 if ((nmp->nm_flag & NFSMNT_INT))
269 CLNT_CONTROL(client, CLSET_INTERRUPTIBLE, &one);
270 if ((nmp->nm_flag & NFSMNT_RESVPORT))
271 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
272 if (NFSHASSOFT(nmp)) {
273 if (nmp->nm_sotype == SOCK_DGRAM)
275 * For UDP, the large timeout for a reconnect
276 * will be set to "nm_retry * nm_timeo / 2", so
277 * we only want to do 2 reconnect timeout
282 retries = nmp->nm_retry;
285 if (NFSHASNFSV4N(nmp)) {
287 * Make sure the nfscbd_pool doesn't get destroyed
291 if (nfs_numnfscbd > 0) {
294 xprt = svc_vc_create_backchannel(nfscbd_pool);
295 CLNT_CONTROL(client, CLSET_BACKCHANNEL, xprt);
298 if (nfs_numnfscbd == 0)
299 wakeup(&nfs_numnfscbd);
306 * - Null RPC callback to client
307 * - Non-Null RPC callback to client, wait a little longer
308 * - upcalls to nfsuserd and gssd (clp == NULL)
310 if (callback_retry_mult == 0) {
311 retries = NFSV4_UPCALLRETRY;
312 CLNT_CONTROL(client, CLSET_PRIVPORT, &one);
314 retries = NFSV4_CALLBACKRETRY * callback_retry_mult;
317 CLNT_CONTROL(client, CLSET_RETRIES, &retries);
321 * For UDP, there are 2 timeouts:
322 * - CLSET_RETRY_TIMEOUT sets the initial timeout for the timer
323 * that does a retransmit of an RPC request using the same
324 * socket and xid. This is what you normally want to do,
325 * since NFS servers depend on "same xid" for their
326 * Duplicate Request Cache.
327 * - timeout specified in CLNT_CALL_MBUF(), which specifies when
328 * retransmits on the same socket should fail and a fresh
329 * socket created. Each of these timeouts counts as one
330 * CLSET_RETRIES as set above.
331 * Set the initial retransmit timeout for UDP. This timeout
332 * doesn't exist for TCP and the following call just fails,
335 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
336 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * 1000000 / NFS_HZ;
337 CLNT_CONTROL(client, CLSET_RETRY_TIMEOUT, &timo);
340 mtx_lock(&nrp->nr_mtx);
341 if (nrp->nr_client != NULL) {
343 * Someone else already connected.
345 CLNT_RELEASE(client);
347 nrp->nr_client = client;
351 * Protocols that do not require connections may be optionally left
352 * unconnected for servers that reply from a port other than NFS_PORT.
354 if (nmp == NULL || (nmp->nm_flag & NFSMNT_NOCONN) == 0) {
355 mtx_unlock(&nrp->nr_mtx);
356 CLNT_CONTROL(client, CLSET_CONNECT, &one);
358 mtx_unlock(&nrp->nr_mtx);
361 /* Restore current thread's credentials. */
362 td->td_ucred = origcred;
370 * NFS disconnect. Clean up and unlink.
373 newnfs_disconnect(struct nfssockreq *nrp)
377 mtx_lock(&nrp->nr_mtx);
378 if (nrp->nr_client != NULL) {
379 client = nrp->nr_client;
380 nrp->nr_client = NULL;
381 mtx_unlock(&nrp->nr_mtx);
382 rpc_gss_secpurge_call(client);
384 CLNT_RELEASE(client);
386 mtx_unlock(&nrp->nr_mtx);
391 nfs_getauth(struct nfssockreq *nrp, int secflavour, char *clnt_principal,
392 char *srv_principal, gss_OID mech_oid, struct ucred *cred)
394 rpc_gss_service_t svc;
397 rpc_gss_options_req_t req_options;
400 switch (secflavour) {
401 case RPCSEC_GSS_KRB5:
402 case RPCSEC_GSS_KRB5I:
403 case RPCSEC_GSS_KRB5P:
405 if (!rpc_gss_mech_to_oid_call("kerberosv5", &mech_oid))
408 if (secflavour == RPCSEC_GSS_KRB5)
409 svc = rpc_gss_svc_none;
410 else if (secflavour == RPCSEC_GSS_KRB5I)
411 svc = rpc_gss_svc_integrity;
413 svc = rpc_gss_svc_privacy;
415 req_options.req_flags = GSS_C_MUTUAL_FLAG;
416 req_options.time_req = 0;
417 req_options.my_cred = GSS_C_NO_CREDENTIAL;
418 req_options.input_channel_bindings = NULL;
419 req_options.enc_type = nfs_keytab_enctype;
421 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
422 clnt_principal, srv_principal, mech_oid, svc,
426 * Until changes to the rpcsec_gss code are committed,
427 * there is no support for host based initiator
428 * principals. As such, that case cannot yet be handled.
430 if (clnt_principal == NULL)
431 auth = rpc_gss_secfind_call(nrp->nr_client, cred,
432 srv_principal, mech_oid, svc);
441 return (authunix_create(cred));
447 * Callback from the RPC code to generate up/down notifications.
450 struct nfs_feedback_arg {
451 struct nfsmount *nf_mount;
452 int nf_lastmsg; /* last tprintf */
454 struct thread *nf_td;
458 nfs_feedback(int type, int proc, void *arg)
460 struct nfs_feedback_arg *nf = (struct nfs_feedback_arg *) arg;
461 struct nfsmount *nmp = nf->nf_mount;
464 getmicrouptime(&now);
467 case FEEDBACK_REXMIT2:
468 case FEEDBACK_RECONNECT:
469 if (nf->nf_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
470 nfs_down(nmp, nf->nf_td,
471 "not responding", 0, NFSSTA_TIMEO);
472 nf->nf_tprintfmsg = TRUE;
473 nf->nf_lastmsg = now.tv_sec;
478 nfs_up(nf->nf_mount, nf->nf_td,
479 "is alive again", NFSSTA_TIMEO, nf->nf_tprintfmsg);
485 * newnfs_request - goes something like this
486 * - does the rpc by calling the krpc layer
487 * - break down rpc header and return with nfs reply
488 * nb: always frees up nd_mreq mbuf list
491 newnfs_request(struct nfsrv_descript *nd, struct nfsmount *nmp,
492 struct nfsclient *clp, struct nfssockreq *nrp, vnode_t vp,
493 struct thread *td, struct ucred *cred, u_int32_t prog, u_int32_t vers,
494 u_char *retsum, int toplevel, u_int64_t *xidp, struct nfsclsession *sep)
496 u_int32_t retseq, retval, *tl;
498 int i = 0, j = 0, opcnt, set_sigset = 0, slot;
499 int trycnt, error = 0, usegssname = 0, secflavour = AUTH_SYS;
502 u_int trylater_delay = 1;
503 struct nfs_feedback_arg nf;
504 struct timeval timo, now;
506 struct rpc_callextra ext;
508 struct nfsreq *rep = NULL;
509 char *srv_principal = NULL;
511 struct ucred *authcred;
515 /* Reject requests while attempting a forced unmount. */
516 if (nmp != NULL && (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)) {
517 m_freem(nd->nd_mreq);
522 * Set authcred, which is used to acquire RPC credentials to
523 * the cred argument, by default. The crhold() should not be
524 * necessary, but will ensure that some future code change
525 * doesn't result in the credential being free'd prematurely.
527 authcred = crhold(cred);
529 /* For client side interruptible mounts, mask off the signals. */
530 if (nmp != NULL && td != NULL && NFSHASINT(nmp)) {
531 newnfs_set_sigmask(td, &oldset);
536 * XXX if not already connected call nfs_connect now. Longer
537 * term, change nfs_mount to call nfs_connect unconditionally
538 * and let clnt_reconnect_create handle reconnects.
540 if (nrp->nr_client == NULL)
541 newnfs_connect(nmp, nrp, cred, td, 0);
544 * For a client side mount, nmp is != NULL and clp == NULL. For
545 * server calls (callbacks or upcalls), nmp == NULL.
549 if ((clp->lc_flags & LCL_GSS) && nfsrv_gsscallbackson) {
550 secflavour = RPCSEC_GSS_KRB5;
551 if (nd->nd_procnum != NFSPROC_NULL) {
552 if (clp->lc_flags & LCL_GSSINTEGRITY)
553 secflavour = RPCSEC_GSS_KRB5I;
554 else if (clp->lc_flags & LCL_GSSPRIVACY)
555 secflavour = RPCSEC_GSS_KRB5P;
559 } else if (nmp != NULL && NFSHASKERB(nmp) &&
560 nd->nd_procnum != NFSPROC_NULL) {
561 if (NFSHASALLGSSNAME(nmp) && nmp->nm_krbnamelen > 0)
562 nd->nd_flag |= ND_USEGSSNAME;
563 if ((nd->nd_flag & ND_USEGSSNAME) != 0) {
565 * If there is a client side host based credential,
566 * use that, otherwise use the system uid, if set.
567 * The system uid is in the nmp->nm_sockreq.nr_cred
570 if (nmp->nm_krbnamelen > 0) {
572 } else if (nmp->nm_uid != (uid_t)-1) {
573 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
574 ("newnfs_request: NULL nr_cred"));
576 authcred = crhold(nmp->nm_sockreq.nr_cred);
578 } else if (nmp->nm_krbnamelen == 0 &&
579 nmp->nm_uid != (uid_t)-1 && cred->cr_uid == (uid_t)0) {
581 * If there is no host based principal name and
582 * the system uid is set and this is root, use the
583 * system uid, since root won't have user
584 * credentials in a credentials cache file.
585 * The system uid is in the nmp->nm_sockreq.nr_cred
588 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
589 ("newnfs_request: NULL nr_cred"));
591 authcred = crhold(nmp->nm_sockreq.nr_cred);
593 if (NFSHASINTEGRITY(nmp))
594 secflavour = RPCSEC_GSS_KRB5I;
595 else if (NFSHASPRIVACY(nmp))
596 secflavour = RPCSEC_GSS_KRB5P;
598 secflavour = RPCSEC_GSS_KRB5;
599 srv_principal = NFSMNT_SRVKRBNAME(nmp);
600 } else if (nmp != NULL && !NFSHASKERB(nmp) &&
601 nd->nd_procnum != NFSPROC_NULL &&
602 (nd->nd_flag & ND_USEGSSNAME) != 0) {
604 * Use the uid that did the mount when the RPC is doing
605 * NFSv4 system operations, as indicated by the
606 * ND_USEGSSNAME flag, for the AUTH_SYS case.
607 * The credentials in nm_sockreq.nr_cred were used for the
610 KASSERT(nmp->nm_sockreq.nr_cred != NULL,
611 ("newnfs_request: NULL nr_cred"));
613 authcred = crhold(nmp->nm_sockreq.nr_cred);
617 bzero(&nf, sizeof(struct nfs_feedback_arg));
620 getmicrouptime(&now);
621 nf.nf_lastmsg = now.tv_sec -
622 ((nmp->nm_tprintf_delay)-(nmp->nm_tprintf_initial_delay));
625 if (nd->nd_procnum == NFSPROC_NULL)
626 auth = authnone_create();
628 auth = nfs_getauth(nrp, secflavour, nmp->nm_krbname,
629 srv_principal, NULL, authcred);
631 auth = nfs_getauth(nrp, secflavour, NULL,
632 srv_principal, NULL, authcred);
635 m_freem(nd->nd_mreq);
637 newnfs_restore_sigmask(td, &oldset);
640 bzero(&ext, sizeof(ext));
643 ext.rc_feedback = nfs_feedback;
644 ext.rc_feedback_arg = &nf;
647 procnum = nd->nd_procnum;
648 if ((nd->nd_flag & ND_NFSV4) &&
649 nd->nd_procnum != NFSPROC_NULL &&
650 nd->nd_procnum != NFSV4PROC_CBCOMPOUND)
651 procnum = NFSV4PROC_COMPOUND;
654 NFSINCRGLOBAL(newnfsstats.rpcrequests);
656 /* Map the procnum to the old NFSv2 one, as required. */
657 if ((nd->nd_flag & ND_NFSV2) != 0) {
658 if (nd->nd_procnum < NFS_V3NPROCS)
659 procnum = nfsv2_procid[nd->nd_procnum];
661 procnum = NFSV2PROC_NOOP;
665 * Now only used for the R_DONTRECOVER case, but until that is
666 * supported within the krpc code, I need to keep a queue of
667 * outstanding RPCs for nfsv4 client requests.
669 if ((nd->nd_flag & ND_NFSV4) && procnum == NFSV4PROC_COMPOUND)
670 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq),
671 M_NFSDREQ, M_WAITOK);
673 if (dtrace_nfscl_nfs234_start_probe != NULL) {
677 if (nd->nd_flag & ND_NFSV4) {
679 nfscl_nfs4_start_probes[nd->nd_procnum];
680 probe_procnum = nd->nd_procnum;
681 } else if (nd->nd_flag & ND_NFSV3) {
682 probe_id = nfscl_nfs3_start_probes[procnum];
683 probe_procnum = procnum;
686 nfscl_nfs2_start_probes[nd->nd_procnum];
687 probe_procnum = procnum;
690 (dtrace_nfscl_nfs234_start_probe)
691 (probe_id, vp, nd->nd_mreq, cred,
697 freeslot = -1; /* Set to slot that needs to be free'd */
699 slot = -1; /* Slot that needs a sequence# increment. */
701 * This timeout specifies when a new socket should be created,
702 * along with new xid values. For UDP, this should be done
703 * infrequently, since retransmits of RPC requests should normally
709 timo.tv_sec = NFSV4_UPCALLTIMEO;
711 timo.tv_sec = NFSV4_CALLBACKTIMEO;
713 if (nrp->nr_sotype != SOCK_DGRAM) {
715 if ((nmp->nm_flag & NFSMNT_NFSV4))
716 timo.tv_sec = INT_MAX;
718 timo.tv_sec = NFS_TCPTIMEO;
720 if (NFSHASSOFT(nmp)) {
722 * CLSET_RETRIES is set to 2, so this should be
723 * half of the total timeout required.
725 timeo = nmp->nm_retry * nmp->nm_timeo / 2;
728 timo.tv_sec = timeo / NFS_HZ;
729 timo.tv_usec = (timeo % NFS_HZ) * 1000000 /
732 /* For UDP hard mounts, use a large value. */
733 timo.tv_sec = NFS_MAXTIMEO / NFS_HZ;
742 * Chain request into list of outstanding requests.
745 TAILQ_INSERT_TAIL(&nfsd_reqq, rep, r_chain);
751 stat = CLNT_CALL_MBUF(nrp->nr_client, &ext, procnum, nd->nd_mreq,
756 * RPC done, unlink the request.
759 TAILQ_REMOVE(&nfsd_reqq, rep, r_chain);
764 * If there was a successful reply and a tprintf msg.
765 * tprintf a response.
767 if (stat == RPC_SUCCESS) {
769 } else if (stat == RPC_TIMEDOUT) {
771 } else if (stat == RPC_VERSMISMATCH) {
773 } else if (stat == RPC_PROGVERSMISMATCH) {
774 error = EPROTONOSUPPORT;
779 m_freem(nd->nd_mreq);
782 FREE((caddr_t)rep, M_NFSDREQ);
784 newnfs_restore_sigmask(td, &oldset);
788 KASSERT(nd->nd_mrep != NULL, ("mrep shouldn't be NULL if no error\n"));
791 * Search for any mbufs that are not a multiple of 4 bytes long
792 * or with m_data not longword aligned.
793 * These could cause pointer alignment problems, so copy them to
794 * well aligned mbufs.
796 newnfs_realign(&nd->nd_mrep);
797 nd->nd_md = nd->nd_mrep;
798 nd->nd_dpos = NFSMTOD(nd->nd_md, caddr_t);
800 if (nd->nd_procnum != NFSPROC_NULL) {
801 /* If sep == NULL, set it to the default in nmp. */
802 if (sep == NULL && nmp != NULL)
803 sep = NFSMNT_MDSSESSION(nmp);
805 * and now the actual NFS xdr.
807 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
808 nd->nd_repstat = fxdr_unsigned(u_int32_t, *tl);
809 if (nd->nd_repstat >= 10000)
810 NFSCL_DEBUG(1, "proc=%d reps=%d\n", (int)nd->nd_procnum,
811 (int)nd->nd_repstat);
814 * Get rid of the tag, return count and SEQUENCE result for
817 if ((nd->nd_flag & ND_NFSV4) != 0) {
818 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
819 i = fxdr_unsigned(int, *tl);
820 error = nfsm_advance(nd, NFSM_RNDUP(i), -1);
823 NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
824 opcnt = fxdr_unsigned(int, *tl++);
825 i = fxdr_unsigned(int, *tl++);
826 j = fxdr_unsigned(int, *tl);
828 NFSCL_DEBUG(1, "fop=%d fst=%d\n", i, j);
830 * If the first op is Sequence, free up the slot.
832 if (nmp != NULL && i == NFSV4OP_SEQUENCE && j != 0)
833 NFSCL_DEBUG(1, "failed seq=%d\n", j);
834 if (nmp != NULL && i == NFSV4OP_SEQUENCE && j == 0) {
835 NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
837 mtx_lock(&sep->nfsess_mtx);
838 tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
839 retseq = fxdr_unsigned(uint32_t, *tl++);
840 slot = fxdr_unsigned(int, *tl++);
842 if (retseq != sep->nfsess_slotseq[slot])
843 printf("retseq diff 0x%x\n", retseq);
844 retval = fxdr_unsigned(uint32_t, *++tl);
845 if ((retval + 1) < sep->nfsess_foreslots)
846 sep->nfsess_foreslots = (retval + 1);
847 else if ((retval + 1) > sep->nfsess_foreslots)
848 sep->nfsess_foreslots = (retval < 64) ?
850 mtx_unlock(&sep->nfsess_mtx);
852 /* Grab the op and status for the next one. */
854 NFSM_DISSECT(tl, uint32_t *,
856 i = fxdr_unsigned(int, *tl++);
857 j = fxdr_unsigned(int, *tl);
861 if (nd->nd_repstat != 0) {
862 if (((nd->nd_repstat == NFSERR_DELAY ||
863 nd->nd_repstat == NFSERR_GRACE) &&
864 (nd->nd_flag & ND_NFSV4) &&
865 nd->nd_procnum != NFSPROC_DELEGRETURN &&
866 nd->nd_procnum != NFSPROC_SETATTR &&
867 nd->nd_procnum != NFSPROC_READ &&
868 nd->nd_procnum != NFSPROC_READDS &&
869 nd->nd_procnum != NFSPROC_WRITE &&
870 nd->nd_procnum != NFSPROC_WRITEDS &&
871 nd->nd_procnum != NFSPROC_OPEN &&
872 nd->nd_procnum != NFSPROC_CREATE &&
873 nd->nd_procnum != NFSPROC_OPENCONFIRM &&
874 nd->nd_procnum != NFSPROC_OPENDOWNGRADE &&
875 nd->nd_procnum != NFSPROC_CLOSE &&
876 nd->nd_procnum != NFSPROC_LOCK &&
877 nd->nd_procnum != NFSPROC_LOCKU) ||
878 (nd->nd_repstat == NFSERR_DELAY &&
879 (nd->nd_flag & ND_NFSV4) == 0) ||
880 nd->nd_repstat == NFSERR_RESOURCE) {
881 if (trylater_delay > NFS_TRYLATERDEL)
882 trylater_delay = NFS_TRYLATERDEL;
883 waituntil = NFSD_MONOSEC + trylater_delay;
884 while (NFSD_MONOSEC < waituntil)
885 (void) nfs_catnap(PZERO, 0, "nfstry");
888 mtx_lock(&sep->nfsess_mtx);
889 sep->nfsess_slotseq[slot]++;
890 *nd->nd_slotseq = txdr_unsigned(
891 sep->nfsess_slotseq[slot]);
892 mtx_unlock(&sep->nfsess_mtx);
894 m_freem(nd->nd_mrep);
900 * If the File Handle was stale, invalidate the
901 * lookup cache, just in case.
902 * (vp != NULL implies a client side call)
904 if (nd->nd_repstat == ESTALE && vp != NULL) {
906 if (ncl_call_invalcaches != NULL)
907 (*ncl_call_invalcaches)(vp);
910 if ((nd->nd_flag & ND_NFSV4) != 0) {
911 /* Free the slot, as required. */
913 nfsv4_freeslot(sep, freeslot);
915 * If this op is Putfh, throw its results away.
918 NFSCL_DEBUG(1, "nop=%d nst=%d\n", i, j);
919 if (nmp != NULL && i == NFSV4OP_PUTFH && j == 0) {
920 NFSM_DISSECT(tl,u_int32_t *,2 * NFSX_UNSIGNED);
921 i = fxdr_unsigned(int, *tl++);
922 j = fxdr_unsigned(int, *tl);
924 NFSCL_DEBUG(1, "n2op=%d n2st=%d\n", i,
927 * All Compounds that do an Op that must
928 * be in sequence consist of NFSV4OP_PUTFH
929 * followed by one of these. As such, we
930 * can determine if the seqid# should be
933 if ((i == NFSV4OP_OPEN ||
934 i == NFSV4OP_OPENCONFIRM ||
935 i == NFSV4OP_OPENDOWNGRADE ||
936 i == NFSV4OP_CLOSE ||
938 i == NFSV4OP_LOCKU) &&
940 (j != NFSERR_STALECLIENTID &&
941 j != NFSERR_STALESTATEID &&
942 j != NFSERR_BADSTATEID &&
943 j != NFSERR_BADSEQID &&
944 j != NFSERR_BADXDR &&
945 j != NFSERR_RESOURCE &&
946 j != NFSERR_NOFILEHANDLE)))
947 nd->nd_flag |= ND_INCRSEQID;
950 * If this op's status is non-zero, mark
951 * that there is no more data to process.
954 nd->nd_flag |= ND_NOMOREDATA;
957 * If R_DONTRECOVER is set, replace the stale error
958 * reply, so that recovery isn't initiated.
960 if ((nd->nd_repstat == NFSERR_STALECLIENTID ||
961 nd->nd_repstat == NFSERR_BADSESSION ||
962 nd->nd_repstat == NFSERR_STALESTATEID) &&
963 rep != NULL && (rep->r_flags & R_DONTRECOVER))
964 nd->nd_repstat = NFSERR_STALEDONTRECOVER;
969 if (nmp != NULL && dtrace_nfscl_nfs234_done_probe != NULL) {
973 if (nd->nd_flag & ND_NFSV4) {
974 probe_id = nfscl_nfs4_done_probes[nd->nd_procnum];
975 probe_procnum = nd->nd_procnum;
976 } else if (nd->nd_flag & ND_NFSV3) {
977 probe_id = nfscl_nfs3_done_probes[procnum];
978 probe_procnum = procnum;
980 probe_id = nfscl_nfs2_done_probes[nd->nd_procnum];
981 probe_procnum = procnum;
984 (dtrace_nfscl_nfs234_done_probe)(probe_id, vp,
985 nd->nd_mreq, cred, probe_procnum, 0);
989 m_freem(nd->nd_mreq);
992 FREE((caddr_t)rep, M_NFSDREQ);
994 newnfs_restore_sigmask(td, &oldset);
997 mbuf_freem(nd->nd_mrep);
998 mbuf_freem(nd->nd_mreq);
1001 FREE((caddr_t)rep, M_NFSDREQ);
1003 newnfs_restore_sigmask(td, &oldset);
1008 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1009 * wait for all requests to complete. This is used by forced unmounts
1010 * to terminate any outstanding RPCs.
1013 newnfs_nmcancelreqs(struct nfsmount *nmp)
1016 if (nmp->nm_sockreq.nr_client != NULL)
1017 CLNT_CLOSE(nmp->nm_sockreq.nr_client);
1022 * Any signal that can interrupt an NFS operation in an intr mount
1023 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1025 int newnfs_sig_set[] = {
1035 * Check to see if one of the signals in our subset is pending on
1036 * the process (in an intr mount).
1039 nfs_sig_pending(sigset_t set)
1043 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++)
1044 if (SIGISMEMBER(set, newnfs_sig_set[i]))
1050 * The set/restore sigmask functions are used to (temporarily) overwrite
1051 * the process p_sigmask during an RPC call (for example). These are also
1052 * used in other places in the NFS client that might tsleep().
1055 newnfs_set_sigmask(struct thread *td, sigset_t *oldset)
1063 td = curthread; /* XXX */
1065 /* Remove the NFS set of signals from newset */
1067 mtx_lock(&p->p_sigacts->ps_mtx);
1068 for (i = 0 ; i < sizeof(newnfs_sig_set)/sizeof(int) ; i++) {
1070 * But make sure we leave the ones already masked
1071 * by the process, ie. remove the signal from the
1072 * temporary signalmask only if it wasn't already
1075 if (!SIGISMEMBER(td->td_sigmask, newnfs_sig_set[i]) &&
1076 !SIGISMEMBER(p->p_sigacts->ps_sigignore, newnfs_sig_set[i]))
1077 SIGDELSET(newset, newnfs_sig_set[i]);
1079 mtx_unlock(&p->p_sigacts->ps_mtx);
1081 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1085 newnfs_restore_sigmask(struct thread *td, sigset_t *set)
1088 td = curthread; /* XXX */
1089 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1093 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1094 * old one after msleep() returns.
1097 newnfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1103 if ((priority & PCATCH) == 0)
1104 return msleep(ident, mtx, priority, wmesg, timo);
1106 td = curthread; /* XXX */
1107 newnfs_set_sigmask(td, &oldset);
1108 error = msleep(ident, mtx, priority, wmesg, timo);
1109 newnfs_restore_sigmask(td, &oldset);
1115 * Test for a termination condition pending on the process.
1116 * This is used for NFSMNT_INT mounts.
1119 newnfs_sigintr(struct nfsmount *nmp, struct thread *td)
1124 /* Terminate all requests while attempting a forced unmount. */
1125 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1127 if (!(nmp->nm_flag & NFSMNT_INT))
1133 tmpset = p->p_siglist;
1134 SIGSETOR(tmpset, td->td_siglist);
1135 SIGSETNAND(tmpset, td->td_sigmask);
1136 mtx_lock(&p->p_sigacts->ps_mtx);
1137 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1138 mtx_unlock(&p->p_sigacts->ps_mtx);
1139 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1140 && nfs_sig_pending(tmpset)) {
1149 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1153 p = td ? td->td_proc : NULL;
1155 tprintf(p, LOG_INFO, "newnfs server %s: %s, error %d\n",
1156 server, msg, error);
1158 tprintf(p, LOG_INFO, "newnfs server %s: %s\n", server, msg);
1164 nfs_down(struct nfsmount *nmp, struct thread *td, const char *msg,
1165 int error, int flags)
1169 mtx_lock(&nmp->nm_mtx);
1170 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1171 nmp->nm_state |= NFSSTA_TIMEO;
1172 mtx_unlock(&nmp->nm_mtx);
1173 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1176 mtx_unlock(&nmp->nm_mtx);
1177 mtx_lock(&nmp->nm_mtx);
1178 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1179 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1180 mtx_unlock(&nmp->nm_mtx);
1181 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1184 mtx_unlock(&nmp->nm_mtx);
1185 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1189 nfs_up(struct nfsmount *nmp, struct thread *td, const char *msg,
1190 int flags, int tprintfmsg)
1195 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1198 mtx_lock(&nmp->nm_mtx);
1199 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1200 nmp->nm_state &= ~NFSSTA_TIMEO;
1201 mtx_unlock(&nmp->nm_mtx);
1202 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1205 mtx_unlock(&nmp->nm_mtx);
1207 mtx_lock(&nmp->nm_mtx);
1208 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1209 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1210 mtx_unlock(&nmp->nm_mtx);
1211 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1214 mtx_unlock(&nmp->nm_mtx);