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
32 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Socket operations for use by nfs
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/mutex.h>
53 #include <sys/protosw.h>
54 #include <sys/signalvar.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sysctl.h>
59 #include <sys/syslog.h>
60 #include <sys/vnode.h>
62 #include <netinet/in.h>
63 #include <netinet/tcp.h>
65 #include <rpc/rpcclnt.h>
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfsclient/nfs.h>
70 #include <nfs/xdr_subs.h>
71 #include <nfsclient/nfsm_subs.h>
72 #include <nfsclient/nfsmount.h>
73 #include <nfsclient/nfsnode.h>
75 #include <nfs4client/nfs4.h>
80 extern u_int32_t nfs_xid;
82 static int nfs_realign_test;
83 static int nfs_realign_count;
84 static int nfs_bufpackets = 4;
85 static int nfs_reconnects;
86 static int nfs3_jukebox_delay = 10;
87 static int nfs_skip_wcc_data_onerr = 1;
89 SYSCTL_DECL(_vfs_nfs);
91 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
95 "number of times the nfs client has had to reconnect");
96 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
97 "number of seconds to delay a retry after receiving EJUKEBOX");
98 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0, "");
101 * There is a congestion window for outstanding rpcs maintained per mount
102 * point. The cwnd size is adjusted in roughly the way that:
103 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
104 * SIGCOMM '88". ACM, August 1988.
105 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
106 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
107 * of rpcs is in progress.
108 * (The sent count and cwnd are scaled for integer arith.)
109 * Variants of "slow start" were tried and were found to be too much of a
110 * performance hit (ave. rtt 3 times larger),
111 * I suspect due to the large rtt that nfs rpcs have.
113 #define NFS_CWNDSCALE 256
114 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
115 #define NFS_NBACKOFF 8
116 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
117 struct callout nfs_callout;
119 static int nfs_msg(struct thread *, const char *, const char *, int);
120 static int nfs_realign(struct mbuf **pm, int hsiz);
121 static int nfs_reply(struct nfsreq *);
122 static void nfs_softterm(struct nfsreq *rep);
123 static int nfs_reconnect(struct nfsreq *rep);
124 static void nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag);
125 static void nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag);
127 extern struct mtx nfs_reqq_mtx;
133 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
134 NFS_DEFAULT_TIMER, /* NULL */
135 NFS_GETATTR_TIMER, /* GETATTR */
136 NFS_DEFAULT_TIMER, /* SETATTR */
137 NFS_LOOKUP_TIMER, /* LOOKUP */
138 NFS_GETATTR_TIMER, /* ACCESS */
139 NFS_READ_TIMER, /* READLINK */
140 NFS_READ_TIMER, /* READ */
141 NFS_WRITE_TIMER, /* WRITE */
142 NFS_DEFAULT_TIMER, /* CREATE */
143 NFS_DEFAULT_TIMER, /* MKDIR */
144 NFS_DEFAULT_TIMER, /* SYMLINK */
145 NFS_DEFAULT_TIMER, /* MKNOD */
146 NFS_DEFAULT_TIMER, /* REMOVE */
147 NFS_DEFAULT_TIMER, /* RMDIR */
148 NFS_DEFAULT_TIMER, /* RENAME */
149 NFS_DEFAULT_TIMER, /* LINK */
150 NFS_READ_TIMER, /* READDIR */
151 NFS_READ_TIMER, /* READDIRPLUS */
152 NFS_DEFAULT_TIMER, /* FSSTAT */
153 NFS_DEFAULT_TIMER, /* FSINFO */
154 NFS_DEFAULT_TIMER, /* PATHCONF */
155 NFS_DEFAULT_TIMER, /* COMMIT */
156 NFS_DEFAULT_TIMER, /* NOOP */
160 * Choose the correct RTT timer for this NFS procedure.
162 static inline enum nfs_rto_timer_t
163 nfs_rto_timer(u_int32_t procnum)
165 return nfs_proct[procnum];
169 * Initialize the RTT estimator state for a new mount point.
172 nfs_init_rtt(struct nfsmount *nmp)
176 for (i = 0; i < NFS_MAX_TIMER; i++)
177 nmp->nm_srtt[i] = NFS_INITRTT;
178 for (i = 0; i < NFS_MAX_TIMER; i++)
179 nmp->nm_sdrtt[i] = 0;
183 * Update a mount point's RTT estimator state using data from the
186 * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
188 * NB: Since the timer resolution of NFS_HZ is so course, it can often
189 * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
190 * between N + dt and N + 2 - dt ticks, add 1 before calculating the
194 nfs_update_rtt(struct nfsreq *rep)
196 int t1 = rep->r_rtt + 1;
197 int index = nfs_rto_timer(rep->r_procnum) - 1;
198 int *srtt = &rep->r_nmp->nm_srtt[index];
199 int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
210 * Estimate RTO for an NFS RPC sent via an unreliable datagram.
212 * Use the mean and mean deviation of RTT for the appropriate type
213 * of RPC for the frequent RPCs and a default for the others.
214 * The justification for doing "other" this way is that these RPCs
215 * happen so infrequently that timer est. would probably be stale.
216 * Also, since many of these RPCs are non-idempotent, a conservative
217 * timeout is desired.
219 * getattr, lookup - A+2D
224 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
226 enum nfs_rto_timer_t timer = nfs_rto_timer(procnum);
227 int index = timer - 1;
231 case NFS_GETATTR_TIMER:
232 case NFS_LOOKUP_TIMER:
233 rto = ((nmp->nm_srtt[index] + 3) >> 2) +
234 ((nmp->nm_sdrtt[index] + 1) >> 1);
237 case NFS_WRITE_TIMER:
238 rto = ((nmp->nm_srtt[index] + 7) >> 3) +
239 (nmp->nm_sdrtt[index] + 1);
246 if (rto < NFS_MINRTO)
248 else if (rto > NFS_MAXRTO)
256 * Initialize sockets and congestion for a new NFS connection.
257 * We do not free the sockaddr if error.
260 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
263 int error, rcvreserve, sndreserve;
265 struct sockaddr *saddr;
266 struct thread *td = &thread0; /* only used for socreate and sobind */
270 if (nmp->nm_sotype == SOCK_STREAM) {
271 mtx_lock(&nmp->nm_mtx);
272 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
273 nmp->nm_nfstcpstate.rpcresid = 0;
274 mtx_unlock(&nmp->nm_mtx);
278 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
279 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
283 nmp->nm_soflags = so->so_proto->pr_flags;
286 * Some servers require that the client port be a reserved port number.
288 if (nmp->nm_flag & NFSMNT_RESVPORT) {
291 struct sockaddr_in6 ssin;
294 bzero(&sopt, sizeof sopt);
295 switch(saddr->sa_family) {
297 sopt.sopt_level = IPPROTO_IP;
298 sopt.sopt_name = IP_PORTRANGE;
299 ip = IP_PORTRANGE_LOW;
300 ip2 = IP_PORTRANGE_DEFAULT;
301 len = sizeof (struct sockaddr_in);
305 sopt.sopt_level = IPPROTO_IPV6;
306 sopt.sopt_name = IPV6_PORTRANGE;
307 ip = IPV6_PORTRANGE_LOW;
308 ip2 = IPV6_PORTRANGE_DEFAULT;
309 len = sizeof (struct sockaddr_in6);
315 sa = (struct sockaddr *)&ssin;
318 sa->sa_family = saddr->sa_family;
319 sopt.sopt_dir = SOPT_SET;
320 sopt.sopt_val = (void *)&ip;
321 sopt.sopt_valsize = sizeof(ip);
322 error = sosetopt(so, &sopt);
325 error = sobind(so, sa, td);
329 error = sosetopt(so, &sopt);
336 * Protocols that do not require connections may be optionally left
337 * unconnected for servers that reply from a port other than NFS_PORT.
339 mtx_lock(&nmp->nm_mtx);
340 if (nmp->nm_flag & NFSMNT_NOCONN) {
341 if (nmp->nm_soflags & PR_CONNREQUIRED) {
343 mtx_unlock(&nmp->nm_mtx);
346 mtx_unlock(&nmp->nm_mtx);
348 mtx_unlock(&nmp->nm_mtx);
349 error = soconnect(so, nmp->nm_nam, td);
354 * Wait for the connection to complete. Cribbed from the
355 * connect system call but with the wait timing out so
356 * that interruptible mounts don't hang here for a long time.
359 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
360 (void) msleep(&so->so_timeo, SOCK_MTX(so),
361 PSOCK, "nfscon", 2 * hz);
362 if ((so->so_state & SS_ISCONNECTING) &&
363 so->so_error == 0 && rep &&
364 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
365 so->so_state &= ~SS_ISCONNECTING;
371 error = so->so_error;
378 so->so_rcv.sb_timeo = 12 * hz;
379 so->so_snd.sb_timeo = 5 * hz;
382 * Get buffer reservation size from sysctl, but impose reasonable
385 pktscale = nfs_bufpackets;
390 mtx_lock(&nmp->nm_mtx);
391 if (nmp->nm_sotype == SOCK_DGRAM) {
392 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
393 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
394 NFS_MAXPKTHDR) * pktscale;
395 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
396 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
397 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
398 NFS_MAXPKTHDR) * pktscale;
400 if (nmp->nm_sotype != SOCK_STREAM)
401 panic("nfscon sotype");
402 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
406 bzero(&sopt, sizeof sopt);
407 sopt.sopt_dir = SOPT_SET;
408 sopt.sopt_level = SOL_SOCKET;
409 sopt.sopt_name = SO_KEEPALIVE;
410 sopt.sopt_val = &val;
411 sopt.sopt_valsize = sizeof val;
413 mtx_unlock(&nmp->nm_mtx);
415 mtx_lock(&nmp->nm_mtx);
417 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
421 bzero(&sopt, sizeof sopt);
422 sopt.sopt_dir = SOPT_SET;
423 sopt.sopt_level = IPPROTO_TCP;
424 sopt.sopt_name = TCP_NODELAY;
425 sopt.sopt_val = &val;
426 sopt.sopt_valsize = sizeof val;
428 mtx_unlock(&nmp->nm_mtx);
430 mtx_lock(&nmp->nm_mtx);
432 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
433 sizeof (u_int32_t)) * pktscale;
434 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
435 sizeof (u_int32_t)) * pktscale;
437 mtx_unlock(&nmp->nm_mtx);
438 error = soreserve(so, sndreserve, rcvreserve);
441 SOCKBUF_LOCK(&so->so_rcv);
442 so->so_rcv.sb_flags |= SB_NOINTR;
443 so->so_upcallarg = (caddr_t)nmp;
444 if (so->so_type == SOCK_STREAM)
445 so->so_upcall = nfs_clnt_tcp_soupcall;
447 so->so_upcall = nfs_clnt_udp_soupcall;
448 so->so_rcv.sb_flags |= SB_UPCALL;
449 SOCKBUF_UNLOCK(&so->so_rcv);
450 SOCKBUF_LOCK(&so->so_snd);
451 so->so_snd.sb_flags |= SB_NOINTR;
452 SOCKBUF_UNLOCK(&so->so_snd);
454 mtx_lock(&nmp->nm_mtx);
455 /* Initialize other non-zero congestion variables */
457 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
459 nmp->nm_timeouts = 0;
460 mtx_unlock(&nmp->nm_mtx);
472 * Called when a connection is broken on a reliable protocol.
473 * - clean up the old socket
474 * - nfs_connect() again
475 * - set R_MUSTRESEND for all outstanding requests on mount point
476 * If this fails the mount point is DEAD!
477 * nb: Must be called with the nfs_sndlock() set on the mount point.
480 nfs_reconnect(struct nfsreq *rep)
483 struct nfsmount *nmp = rep->r_nmp;
488 while ((error = nfs_connect(nmp, rep)) != 0) {
489 if (error == ERESTART)
491 if (error == EIO || error == EINTR)
493 (void) tsleep(&lbolt, PSOCK, "nfscon", 0);
497 * Clear the FORCE_RECONNECT flag only after the connect
498 * succeeds. To prevent races between multiple processes
499 * waiting on the mountpoint where the connection is being
500 * torn down. The first one to acquire the sndlock will
501 * retry the connection. The others block on the sndlock
502 * until the connection is established successfully, and
503 * then re-transmit the request.
505 mtx_lock(&nmp->nm_mtx);
506 nmp->nm_nfstcpstate.flags &= ~NFS_TCP_FORCE_RECONNECT;
507 nmp->nm_nfstcpstate.rpcresid = 0;
508 mtx_unlock(&nmp->nm_mtx);
511 * Loop through outstanding request list and fix up all requests
514 mtx_lock(&nfs_reqq_mtx);
515 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
516 if (rp->r_nmp == nmp) {
517 mtx_lock(&rp->r_mtx);
518 rp->r_flags |= R_MUSTRESEND;
519 mtx_unlock(&rp->r_mtx);
522 mtx_unlock(&nfs_reqq_mtx);
527 * NFS disconnect. Clean up and unlink.
530 nfs_disconnect(struct nfsmount *nmp)
536 mtx_lock(&nmp->nm_mtx);
540 mtx_unlock(&nmp->nm_mtx);
541 SOCKBUF_LOCK(&so->so_rcv);
542 so->so_upcallarg = NULL;
543 so->so_upcall = NULL;
544 so->so_rcv.sb_flags &= ~SB_UPCALL;
545 SOCKBUF_UNLOCK(&so->so_rcv);
546 soshutdown(so, SHUT_WR);
549 mtx_unlock(&nmp->nm_mtx);
553 nfs_safedisconnect(struct nfsmount *nmp)
555 struct nfsreq dummyreq;
557 bzero(&dummyreq, sizeof(dummyreq));
558 dummyreq.r_nmp = nmp;
563 * This is the nfs send routine. For connection based socket types, it
564 * must be called with an nfs_sndlock() on the socket.
565 * - return EINTR if the RPC is terminated, 0 otherwise
566 * - set R_MUSTRESEND if the send fails for any reason
567 * - do any cleanup required by recoverable socket errors (?)
570 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
573 struct sockaddr *sendnam;
574 int error, error2, soflags, flags;
578 KASSERT(rep, ("nfs_send: called with rep == NULL"));
580 error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
585 mtx_lock(&rep->r_nmp->nm_mtx);
586 mtx_lock(&rep->r_mtx);
587 if ((so = rep->r_nmp->nm_so) == NULL) {
588 rep->r_flags |= R_MUSTRESEND;
589 mtx_unlock(&rep->r_mtx);
590 mtx_unlock(&rep->r_nmp->nm_mtx);
595 rep->r_flags &= ~R_MUSTRESEND;
596 soflags = rep->r_nmp->nm_soflags;
597 mtx_unlock(&rep->r_mtx);
598 mtx_unlock(&rep->r_nmp->nm_mtx);
600 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
604 if (so->so_type == SOCK_SEQPACKET)
609 error = sosend(so, sendnam, 0, top, 0, flags, curthread /*XXX*/);
610 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
612 mtx_lock(&rep->r_mtx);
613 rep->r_flags |= R_MUSTRESEND;
614 mtx_unlock(&rep->r_mtx);
619 * Don't report EPIPE errors on nfs sockets.
620 * These can be due to idle tcp mounts which will be closed by
621 * netapp, solaris, etc. if left idle too long.
623 if (error != EPIPE) {
624 log(LOG_INFO, "nfs send error %d for server %s\n",
626 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
629 * Deal with errors for the client side.
631 error2 = NFS_SIGREP(rep);
635 mtx_lock(&rep->r_mtx);
636 rep->r_flags |= R_MUSTRESEND;
637 mtx_unlock(&rep->r_mtx);
641 * Handle any recoverable (soft) socket errors here. (?)
642 * Make EWOULDBLOCK a recoverable error, we'll rexmit from nfs_timer().
644 if (error != EINTR && error != ERESTART && error != EIO && error != EPIPE)
653 nfs_reply(struct nfsreq *rep)
655 register struct socket *so;
656 register struct mbuf *m;
657 int error = 0, sotype, slpflag;
661 sotype = rep->r_nmp->nm_sotype;
663 * For reliable protocols, lock against other senders/receivers
664 * in case a reconnect is necessary.
666 if (sotype != SOCK_DGRAM) {
667 error = nfs_sndlock(rep);
671 mtx_lock(&rep->r_nmp->nm_mtx);
672 mtx_lock(&rep->r_mtx);
674 mtx_unlock(&rep->r_mtx);
675 mtx_unlock(&rep->r_nmp->nm_mtx);
680 if (rep->r_flags & R_SOFTTERM) {
681 mtx_unlock(&rep->r_mtx);
682 mtx_unlock(&rep->r_nmp->nm_mtx);
687 so = rep->r_nmp->nm_so;
689 (rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) {
690 mtx_unlock(&rep->r_mtx);
691 mtx_unlock(&rep->r_nmp->nm_mtx);
692 error = nfs_reconnect(rep);
699 while (rep->r_flags & R_MUSTRESEND) {
700 mtx_unlock(&rep->r_mtx);
701 mtx_unlock(&rep->r_nmp->nm_mtx);
702 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
703 nfsstats.rpcretries++;
704 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
706 if (error == EINTR || error == ERESTART ||
707 (error = nfs_reconnect(rep)) != 0) {
713 mtx_lock(&rep->r_nmp->nm_mtx);
714 mtx_lock(&rep->r_mtx);
716 mtx_unlock(&rep->r_nmp->nm_mtx);
717 mtx_unlock(&rep->r_mtx);
721 mtx_lock(&rep->r_nmp->nm_mtx);
722 if (rep->r_nmp->nm_flag & NFSMNT_INT)
724 mtx_unlock(&rep->r_nmp->nm_mtx);
725 mtx_lock(&rep->r_mtx);
726 while ((rep->r_mrep == NULL) && (error == 0) &&
727 ((rep->r_flags & R_SOFTTERM) == 0) &&
728 ((sotype == SOCK_DGRAM) || ((rep->r_flags & R_MUSTRESEND) == 0)))
729 error = msleep((caddr_t)rep, &rep->r_mtx,
730 slpflag | (PZERO - 1), "nfsreq", 0);
731 if (error == EINTR || error == ERESTART) {
732 /* NFS operations aren't restartable. Map ERESTART to EINTR */
734 mtx_unlock(&rep->r_mtx);
737 if (rep->r_flags & R_SOFTTERM) {
738 /* Request was terminated because we exceeded the retries (soft mount) */
740 mtx_unlock(&rep->r_mtx);
743 mtx_unlock(&rep->r_mtx);
744 if (sotype == SOCK_STREAM) {
745 mtx_lock(&rep->r_nmp->nm_mtx);
746 mtx_lock(&rep->r_mtx);
747 if (((rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) ||
748 (rep->r_flags & R_MUSTRESEND))) {
749 mtx_unlock(&rep->r_mtx);
750 mtx_unlock(&rep->r_nmp->nm_mtx);
751 error = nfs_sndlock(rep);
756 mtx_unlock(&rep->r_mtx);
757 mtx_unlock(&rep->r_nmp->nm_mtx);
767 * Make nfs_realign() non-blocking. Also make nfsm_dissect() nonblocking.
770 nfs_clnt_match_xid(struct socket *so,
771 struct nfsmount *nmp,
781 * Search for any mbufs that are not a multiple of 4 bytes long
782 * or with m_data not longword aligned.
783 * These could cause pointer alignment problems, so copy them to
784 * well aligned mbufs.
786 if (nfs_realign(&mrep, 5 * NFSX_UNSIGNED) == ENOMEM) {
788 nfsstats.rpcinvalid++;
793 * Get the xid and check that it is an rpc reply
796 dpos = mtod(md, caddr_t);
797 tl = nfsm_dissect_nonblock(u_int32_t *, 2*NFSX_UNSIGNED);
799 if (*tl != rpc_reply) {
802 nfsstats.rpcinvalid++;
806 mtx_lock(&nfs_reqq_mtx);
808 * Loop through the request list to match up the reply
809 * Iff no match, just drop the datagram
811 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
812 mtx_lock(&nmp->nm_mtx);
813 mtx_lock(&rep->r_mtx);
814 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
820 * Update congestion window.
821 * Do the additive increase of
824 if (nmp->nm_cwnd <= nmp->nm_sent) {
826 (NFS_CWNDSCALE * NFS_CWNDSCALE +
827 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
828 if (nmp->nm_cwnd > NFS_MAXCWND)
829 nmp->nm_cwnd = NFS_MAXCWND;
831 if (rep->r_flags & R_SENT) {
832 rep->r_flags &= ~R_SENT;
833 nmp->nm_sent -= NFS_CWNDSCALE;
835 if (rep->r_flags & R_TIMING)
837 nmp->nm_timeouts = 0;
838 wakeup((caddr_t)rep);
839 mtx_unlock(&rep->r_mtx);
840 mtx_unlock(&nmp->nm_mtx);
843 mtx_unlock(&rep->r_mtx);
844 mtx_unlock(&nmp->nm_mtx);
847 * If not matched to a request, drop it.
848 * If it's mine, wake up requestor.
851 nfsstats.rpcunexpected++;
854 mtx_unlock(&nfs_reqq_mtx);
858 nfs_mark_for_reconnect(struct nfsmount *nmp)
862 mtx_lock(&nmp->nm_mtx);
863 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
864 mtx_unlock(&nmp->nm_mtx);
866 * Wakeup all processes that are waiting for replies
867 * on this mount point. One of them does the reconnect.
869 mtx_lock(&nfs_reqq_mtx);
870 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
871 if (rp->r_nmp == nmp) {
872 mtx_lock(&rp->r_mtx);
873 rp->r_flags |= R_MUSTRESEND;
875 mtx_unlock(&rp->r_mtx);
878 mtx_unlock(&nfs_reqq_mtx);
882 nfstcp_readable(struct socket *so, int bytes)
886 SOCKBUF_LOCK(&so->so_rcv);
887 retval = (so->so_rcv.sb_cc >= (bytes) ||
888 (so->so_rcv.sb_state & SBS_CANTRCVMORE) ||
890 SOCKBUF_UNLOCK(&so->so_rcv);
894 #define nfstcp_marker_readable(so) nfstcp_readable(so, sizeof(u_int32_t))
897 nfs_copy_len(struct mbuf *mp, char *buf, int len)
899 while (len > 0 && mp != NULL) {
900 int copylen = min(len, mp->m_len);
902 bcopy(mp->m_data, buf, copylen);
911 nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag)
913 struct nfsmount *nmp = (struct nfsmount *)arg;
914 struct mbuf *mp = NULL;
921 * Don't pick any more data from the socket if we've marked the
922 * mountpoint for reconnect.
924 mtx_lock(&nmp->nm_mtx);
925 if (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) {
926 mtx_unlock(&nmp->nm_mtx);
929 mtx_unlock(&nmp->nm_mtx);
930 auio.uio_td = curthread;
931 auio.uio_segflg = UIO_SYSSPACE;
932 auio.uio_rw = UIO_READ;
934 mtx_lock(&nmp->nm_mtx);
935 if (nmp->nm_nfstcpstate.flags & NFS_TCP_EXPECT_RPCMARKER) {
938 mtx_unlock(&nmp->nm_mtx);
939 if (!nfstcp_marker_readable(so)) {
940 /* Marker is not readable */
943 auio.uio_resid = sizeof(u_int32_t);
947 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
948 error = soreceive(so, (struct sockaddr **)0, &auio,
949 &mp, (struct mbuf **)0, &rcvflg);
951 * We've already tested that the socket is readable. 2 cases
952 * here, we either read 0 bytes (client closed connection),
953 * or got some other error. In both cases, we tear down the
956 if (error || auio.uio_resid > 0) {
957 if (error && error != ECONNRESET) {
959 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
965 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
967 * Sigh. We can't do the obvious thing here (which would
968 * be to have soreceive copy the length from mbufs for us).
969 * Calling uiomove() from the context of a socket callback
970 * (even for kernel-kernel copies) leads to LORs (since
971 * we hold network locks at this point).
973 if ((resid = nfs_copy_len(mp, (char *)&len,
974 sizeof(u_int32_t)))) {
975 log(LOG_ERR, "%s (%d) from nfs server %s\n",
976 "Bad RPC HDR length",
977 (int)(sizeof(u_int32_t) - resid),
978 nmp->nm_mountp->mnt_stat.f_mntfromname);
981 len = ntohl(len) & ~0x80000000;
984 * This is SERIOUS! We are out of sync with the sender
985 * and forcing a disconnect/reconnect is all I can do.
987 if (len > NFS_MAXPACKET || len == 0) {
988 log(LOG_ERR, "%s (%d) from nfs server %s\n",
989 "impossible packet length",
991 nmp->nm_mountp->mnt_stat.f_mntfromname);
994 mtx_lock(&nmp->nm_mtx);
995 nmp->nm_nfstcpstate.rpcresid = len;
996 nmp->nm_nfstcpstate.flags &= ~(NFS_TCP_EXPECT_RPCMARKER);
997 mtx_unlock(&nmp->nm_mtx);
999 mtx_unlock(&nmp->nm_mtx);
1002 * Processed RPC marker or no RPC marker to process.
1003 * Pull in and process data.
1005 mtx_lock(&nmp->nm_mtx);
1006 if (nmp->nm_nfstcpstate.rpcresid > 0) {
1007 mtx_unlock(&nmp->nm_mtx);
1008 if (!nfstcp_readable(so, nmp->nm_nfstcpstate.rpcresid)) {
1009 /* All data not readable */
1012 auio.uio_resid = nmp->nm_nfstcpstate.rpcresid;
1013 auio.uio_iov = NULL;
1014 auio.uio_iovcnt = 0;
1016 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
1017 error = soreceive(so, (struct sockaddr **)0, &auio,
1018 &mp, (struct mbuf **)0, &rcvflg);
1019 if (error || auio.uio_resid > 0) {
1020 if (error && error != ECONNRESET) {
1022 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1025 goto mark_reconnect;
1028 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1029 mtx_lock(&nmp->nm_mtx);
1030 nmp->nm_nfstcpstate.rpcresid = 0;
1031 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
1032 mtx_unlock(&nmp->nm_mtx);
1033 /* We got the entire RPC reply. Match XIDs and wake up requestor */
1034 nfs_clnt_match_xid(so, nmp, mp);
1036 mtx_unlock(&nmp->nm_mtx);
1040 nfs_mark_for_reconnect(nmp);
1044 nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag)
1046 struct nfsmount *nmp = (struct nfsmount *)arg;
1048 struct mbuf *mp = NULL;
1049 struct mbuf *control = NULL;
1052 auio.uio_resid = 1000000;
1053 auio.uio_td = curthread;
1054 rcvflag = MSG_DONTWAIT;
1055 auio.uio_resid = 1000000000;
1057 mp = control = NULL;
1058 error = soreceive(so, NULL, &auio, &mp, &control, &rcvflag);
1062 nfs_clnt_match_xid(so, nmp, mp);
1063 } while (mp && !error);
1067 * nfs_request - goes something like this
1068 * - fill in request struct
1069 * - links it into list
1070 * - calls nfs_send() for first transmit
1071 * - calls nfs_receive() to get reply
1072 * - break down rpc header and return with nfs reply pointed to
1074 * nb: always frees up mreq mbuf list
1077 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
1078 struct thread *td, struct ucred *cred, struct mbuf **mrp,
1079 struct mbuf **mdp, caddr_t *dposp)
1081 struct mbuf *mrep, *m2;
1085 struct nfsmount *nmp;
1086 struct mbuf *m, *md, *mheadend;
1089 int error = 0, mrest_len, auth_len, auth_type;
1093 /* Reject requests while attempting a forced unmount. */
1094 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1098 nmp = VFSTONFS(vp->v_mount);
1099 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1100 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
1101 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1102 bzero(rep, sizeof(struct nfsreq));
1106 rep->r_procnum = procnum;
1107 mtx_init(&rep->r_mtx, "NFSrep lock", NULL, MTX_DEF);
1109 getmicrouptime(&now);
1110 rep->r_lastmsg = now.tv_sec -
1111 ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
1112 mrest_len = m_length(mrest, NULL);
1115 * Get the RPC header with authorization.
1117 auth_type = RPCAUTH_UNIX;
1118 if (cred->cr_ngroups < 1)
1119 panic("nfsreq nogrps");
1120 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1121 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1123 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1124 mrest, mrest_len, &mheadend, &xidp);
1127 * For stream protocols, insert a Sun RPC Record Mark.
1129 if (nmp->nm_sotype == SOCK_STREAM) {
1130 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
1131 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1132 (m->m_pkthdr.len - NFSX_UNSIGNED));
1137 if (nmp->nm_flag & NFSMNT_SOFT)
1138 rep->r_retry = nmp->nm_retry;
1140 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1141 rep->r_rtt = rep->r_rexmit = 0;
1142 if (nfs_rto_timer(procnum) != NFS_DEFAULT_TIMER)
1143 rep->r_flags = R_TIMING;
1149 * Do the client side RPC.
1151 nfsstats.rpcrequests++;
1153 * Chain request into list of outstanding requests. Be sure
1154 * to put it LAST so timer finds oldest requests first.
1156 mtx_lock(&nfs_reqq_mtx);
1157 if (TAILQ_EMPTY(&nfs_reqq))
1158 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1159 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1160 mtx_unlock(&nfs_reqq_mtx);
1163 * If backing off another request or avoiding congestion, don't
1164 * send this one now but let timer do it. If not timing a request,
1167 mtx_lock(&nmp->nm_mtx);
1168 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1169 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1170 nmp->nm_sent < nmp->nm_cwnd)) {
1171 mtx_unlock(&nmp->nm_mtx);
1172 error = nfs_sndlock(rep);
1174 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1175 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1178 mtx_lock(&nfs_reqq_mtx);
1180 * nfs_timer() could've re-transmitted the request if we ended up
1181 * blocking on nfs_send() too long, so check for R_SENT here.
1183 if (!error && (rep->r_flags & (R_SENT | R_MUSTRESEND)) == 0) {
1184 mtx_lock(&nmp->nm_mtx);
1185 nmp->nm_sent += NFS_CWNDSCALE;
1186 mtx_unlock(&nmp->nm_mtx);
1187 rep->r_flags |= R_SENT;
1189 mtx_unlock(&nfs_reqq_mtx);
1191 mtx_unlock(&nmp->nm_mtx);
1196 * Wait for the reply from our send or the timer's.
1198 if (!error || error == EPIPE)
1199 error = nfs_reply(rep);
1202 * RPC done, unlink the request.
1204 mtx_lock(&nfs_reqq_mtx);
1206 * nfs_timer() may be in the process of re-transmitting this request.
1207 * nfs_timer() drops the nfs_reqq_mtx before the pru_send() (to avoid LORs).
1208 * Wait till nfs_timer() completes the re-transmission. When the reply
1209 * comes back, it will be discarded (since the req struct for it no longer
1212 while (rep->r_flags & R_PIN_REQ) {
1213 msleep((caddr_t)&rep->r_flags, &nfs_reqq_mtx,
1214 (PZERO - 1), "nfsrxmt", 0);
1216 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1217 if (TAILQ_EMPTY(&nfs_reqq))
1218 callout_stop(&nfs_callout);
1220 * Decrement the outstanding request count.
1222 if (rep->r_flags & R_SENT) {
1223 rep->r_flags &= ~R_SENT; /* paranoia */
1224 mtx_lock(&nmp->nm_mtx);
1225 nmp->nm_sent -= NFS_CWNDSCALE;
1226 mtx_unlock(&nmp->nm_mtx);
1228 mtx_unlock(&nfs_reqq_mtx);
1231 * If there was a successful reply and a tprintf msg.
1232 * tprintf a response.
1235 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1242 * If we got interrupted by a signal in nfs_reply(), there's
1243 * a very small window where the reply could've come in before
1244 * this process got scheduled in. To handle that case, we need
1245 * to free the reply if it was delivered.
1247 if (rep->r_mrep != NULL)
1248 m_freem(rep->r_mrep);
1249 m_freem(rep->r_mreq);
1250 mtx_destroy(&rep->r_mtx);
1251 free((caddr_t)rep, M_NFSREQ);
1255 if (rep->r_mrep == NULL)
1256 panic("nfs_request: rep->r_mrep shouldn't be NULL if no error\n");
1259 * break down the rpc header and check if ok
1261 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1262 if (*tl++ == rpc_msgdenied) {
1263 if (*tl == rpc_mismatch)
1268 m_freem(rep->r_mreq);
1269 mtx_destroy(&rep->r_mtx);
1270 free((caddr_t)rep, M_NFSREQ);
1275 * Just throw away any verifyer (ie: kerberos etc).
1277 i = fxdr_unsigned(int, *tl++); /* verf type */
1278 i = fxdr_unsigned(int32_t, *tl); /* len */
1280 nfsm_adv(nfsm_rndup(i));
1281 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1284 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1286 error = fxdr_unsigned(int, *tl);
1287 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1288 error == NFSERR_TRYLATER) {
1291 waituntil = time_second + nfs3_jukebox_delay;
1292 while (time_second < waituntil) {
1293 (void) tsleep(&lbolt, PSOCK, "nqnfstry", 0);
1295 mtx_lock(&nfs_reqq_mtx);
1298 rep->r_xid = *xidp = txdr_unsigned(nfs_xid);
1299 mtx_unlock(&nfs_reqq_mtx);
1304 * If the File Handle was stale, invalidate the
1305 * lookup cache, just in case.
1307 if (error == ESTALE)
1310 * Skip wcc data on NFS errors for now. NetApp filers return corrupt
1311 * postop attrs in the wcc data for NFS err EROFS. Not sure if they
1312 * could return corrupt postop attrs for others errors.
1314 if ((nmp->nm_flag & NFSMNT_NFSV3) && !nfs_skip_wcc_data_onerr) {
1318 error |= NFSERR_RETERR;
1321 m_freem(rep->r_mreq);
1322 mtx_destroy(&rep->r_mtx);
1323 free((caddr_t)rep, M_NFSREQ);
1330 m_freem(rep->r_mreq);
1331 mtx_destroy(&rep->r_mtx);
1332 FREE((caddr_t)rep, M_NFSREQ);
1336 error = EPROTONOSUPPORT;
1338 m_freem(rep->r_mreq);
1339 mtx_destroy(&rep->r_mtx);
1340 free((caddr_t)rep, M_NFSREQ);
1346 * Scan the nfsreq list and retranmit any requests that have timed out
1347 * To avoid retransmission attempts on STREAM sockets (in the future) make
1348 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1350 * The nfs reqq lock cannot be held while we do the pru_send() because of a
1351 * lock ordering violation. The NFS client socket callback acquires
1352 * inp_lock->nfsreq mutex and pru_send acquires inp_lock. So we drop the
1353 * reqq mutex (and reacquire it after the pru_send()). The req structure
1354 * (for the rexmit) is prevented from being removed by the R_PIN_REQ flag.
1357 nfs_timer(void *arg)
1362 struct nfsmount *nmp;
1367 getmicrouptime(&now);
1368 mtx_lock(&nfs_reqq_mtx);
1369 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1371 mtx_lock(&rep->r_mtx);
1372 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
1373 mtx_unlock(&rep->r_mtx);
1377 * Terminate request if force-unmount in progress.
1378 * Note that NFS could have vfs_busy'ed the mount,
1379 * causing the unmount to wait for the mnt_lock, making
1380 * this bit of logic necessary.
1382 if (rep->r_nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1384 mtx_unlock(&rep->r_mtx);
1387 mtx_unlock(&rep->r_mtx);
1389 if (nfs_sigintr(nmp, rep, rep->r_td))
1391 mtx_lock(&nmp->nm_mtx);
1392 mtx_lock(&rep->r_mtx);
1393 if (nmp->nm_tprintf_initial_delay != 0 &&
1394 (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1395 rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1396 rep->r_lastmsg = now.tv_sec;
1398 * Pin down the request and drop locks for the acquisition
1399 * of Giant from tprintf() in nfs_down().
1401 rep->r_flags |= R_PIN_REQ;
1402 mtx_unlock(&rep->r_mtx);
1403 mtx_unlock(&nmp->nm_mtx);
1404 mtx_unlock(&nfs_reqq_mtx);
1405 nfs_down(rep, nmp, rep->r_td, "not responding",
1407 mtx_lock(&nfs_reqq_mtx);
1408 mtx_lock(&nmp->nm_mtx);
1409 mtx_lock(&rep->r_mtx);
1410 rep->r_flags &= ~R_PIN_REQ;
1411 wakeup((caddr_t)&rep->r_flags);
1413 if (rep->r_rtt >= 0) {
1415 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1416 timeo = nmp->nm_timeo;
1418 timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1419 if (nmp->nm_timeouts > 0)
1420 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1421 if (rep->r_rtt <= timeo) {
1422 mtx_unlock(&rep->r_mtx);
1423 mtx_unlock(&nmp->nm_mtx);
1426 if (nmp->nm_timeouts < NFS_NBACKOFF)
1429 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1430 nfsstats.rpctimeouts++;
1432 mtx_unlock(&rep->r_mtx);
1433 mtx_unlock(&nmp->nm_mtx);
1436 if (nmp->nm_sotype != SOCK_DGRAM) {
1437 if (++rep->r_rexmit > NFS_MAXREXMIT)
1438 rep->r_rexmit = NFS_MAXREXMIT;
1440 * For NFS/TCP, setting R_MUSTRESEND and waking up
1441 * the requester will cause the request to be
1442 * retransmitted (in nfs_reply()), re-connecting
1445 rep->r_flags |= R_MUSTRESEND;
1446 wakeup((caddr_t)rep);
1448 mtx_unlock(&rep->r_mtx);
1449 mtx_unlock(&nmp->nm_mtx);
1452 if ((so = nmp->nm_so) == NULL) {
1453 mtx_unlock(&rep->r_mtx);
1454 mtx_unlock(&nmp->nm_mtx);
1458 * If there is enough space and the window allows..
1460 * Set r_rtt to -1 in case we fail to send it now.
1463 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1464 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || (rep->r_flags & R_SENT) ||
1465 nmp->nm_sent < nmp->nm_cwnd)) {
1466 mtx_unlock(&rep->r_mtx);
1467 mtx_unlock(&nmp->nm_mtx);
1468 if ((m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
1470 * Mark the request to indicate that a XMIT is in
1471 * progress to prevent the req structure being
1472 * removed in nfs_request().
1474 mtx_lock(&rep->r_mtx);
1475 rep->r_flags |= R_PIN_REQ;
1476 mtx_unlock(&rep->r_mtx);
1477 mtx_unlock(&nfs_reqq_mtx);
1479 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1480 error = (*so->so_proto->pr_usrreqs->pru_send)
1481 (so, 0, m, NULL, NULL, curthread);
1483 error = (*so->so_proto->pr_usrreqs->pru_send)
1484 (so, 0, m, nmp->nm_nam, NULL,
1487 mtx_lock(&nfs_reqq_mtx);
1488 mtx_lock(&nmp->nm_mtx);
1489 mtx_lock(&rep->r_mtx);
1490 rep->r_flags &= ~R_PIN_REQ;
1491 wakeup((caddr_t)&rep->r_flags);
1493 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1495 rep->r_flags |= R_RESENDERR;
1498 * Iff first send, start timing
1499 * else turn timing off, backoff timer
1500 * and divide congestion window by 2.
1502 rep->r_flags &= ~R_RESENDERR;
1503 if (rep->r_flags & R_SENT) {
1504 rep->r_flags &= ~R_TIMING;
1505 if (++rep->r_rexmit > NFS_MAXREXMIT)
1506 rep->r_rexmit = NFS_MAXREXMIT;
1508 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1509 nmp->nm_cwnd = NFS_CWNDSCALE;
1510 nfsstats.rpcretries++;
1512 rep->r_flags |= R_SENT;
1513 nmp->nm_sent += NFS_CWNDSCALE;
1517 mtx_unlock(&rep->r_mtx);
1518 mtx_unlock(&nmp->nm_mtx);
1521 mtx_unlock(&rep->r_mtx);
1522 mtx_unlock(&nmp->nm_mtx);
1525 mtx_unlock(&nfs_reqq_mtx);
1526 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1530 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1531 * wait for all requests to complete. This is used by forced unmounts
1532 * to terminate any outstanding RPCs.
1535 nfs_nmcancelreqs(nmp)
1536 struct nfsmount *nmp;
1541 mtx_lock(&nfs_reqq_mtx);
1542 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1543 mtx_lock(&req->r_mtx);
1544 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1545 (req->r_flags & R_SOFTTERM)) {
1546 mtx_unlock(&req->r_mtx);
1550 mtx_unlock(&req->r_mtx);
1552 mtx_unlock(&nfs_reqq_mtx);
1554 for (i = 0; i < 30; i++) {
1555 mtx_lock(&nfs_reqq_mtx);
1556 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1557 if (nmp == req->r_nmp)
1560 mtx_unlock(&nfs_reqq_mtx);
1563 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1569 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1570 * The nm_send count is decremented now to avoid deadlocks when the process in
1571 * soreceive() hasn't yet managed to send its own request.
1575 nfs_softterm(struct nfsreq *rep)
1577 KASSERT(mtx_owned(&rep->r_mtx), ("NFS req lock not owned !"));
1578 rep->r_flags |= R_SOFTTERM;
1579 if (rep->r_flags & R_SENT) {
1580 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1581 rep->r_flags &= ~R_SENT;
1584 * Request terminated, wakeup the blocked process, so that we
1585 * can return EINTR back.
1587 wakeup((caddr_t)rep);
1591 * Any signal that can interrupt an NFS operation in an intr mount
1592 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1594 int nfs_sig_set[] = {
1604 * Check to see if one of the signals in our subset is pending on
1605 * the process (in an intr mount).
1608 nfs_sig_pending(sigset_t set)
1612 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
1613 if (SIGISMEMBER(set, nfs_sig_set[i]))
1619 * The set/restore sigmask functions are used to (temporarily) overwrite
1620 * the process p_sigmask during an RPC call (for example). These are also
1621 * used in other places in the NFS client that might tsleep().
1624 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
1632 td = curthread; /* XXX */
1634 /* Remove the NFS set of signals from newset */
1636 mtx_lock(&p->p_sigacts->ps_mtx);
1637 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
1639 * But make sure we leave the ones already masked
1640 * by the process, ie. remove the signal from the
1641 * temporary signalmask only if it wasn't already
1644 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
1645 !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
1646 SIGDELSET(newset, nfs_sig_set[i]);
1648 mtx_unlock(&p->p_sigacts->ps_mtx);
1650 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1654 nfs_restore_sigmask(struct thread *td, sigset_t *set)
1657 td = curthread; /* XXX */
1658 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1662 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1663 * old one after msleep() returns.
1666 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1672 if ((priority & PCATCH) == 0)
1673 return msleep(ident, mtx, priority, wmesg, timo);
1675 td = curthread; /* XXX */
1676 nfs_set_sigmask(td, &oldset);
1677 error = msleep(ident, mtx, priority, wmesg, timo);
1678 nfs_restore_sigmask(td, &oldset);
1684 * Test for a termination condition pending on the process.
1685 * This is used for NFSMNT_INT mounts.
1688 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1694 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1695 return nfs4_sigintr(nmp, rep, td);
1697 mtx_lock(&rep->r_mtx);
1698 if (rep->r_flags & R_SOFTTERM) {
1699 mtx_unlock(&rep->r_mtx);
1703 mtx_unlock(&rep->r_mtx);
1705 /* Terminate all requests while attempting a forced unmount. */
1706 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1710 if (!(nmp->nm_flag & NFSMNT_INT))
1716 tmpset = p->p_siglist;
1717 SIGSETOR(tmpset, td->td_siglist);
1718 SIGSETNAND(tmpset, td->td_sigmask);
1719 mtx_lock(&p->p_sigacts->ps_mtx);
1720 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1721 mtx_unlock(&p->p_sigacts->ps_mtx);
1722 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1723 && nfs_sig_pending(tmpset)) {
1735 * Lock a socket against others.
1736 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1737 * and also to avoid race conditions between the processes with nfs requests
1738 * in progress when a reconnect is necessary.
1741 nfs_sndlock(struct nfsreq *rep)
1743 int *statep = &rep->r_nmp->nm_state;
1745 int error, slpflag = 0, slptimeo = 0;
1748 mtx_lock(&rep->r_nmp->nm_mtx);
1749 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1751 while (*statep & NFSSTA_SNDLOCK) {
1752 error = nfs_sigintr(rep->r_nmp, rep, td);
1754 mtx_unlock(&rep->r_nmp->nm_mtx);
1757 *statep |= NFSSTA_WANTSND;
1758 (void) msleep(statep, &rep->r_nmp->nm_mtx,
1759 slpflag | (PZERO - 1), "nfsndlck", slptimeo);
1760 if (slpflag == PCATCH) {
1765 *statep |= NFSSTA_SNDLOCK;
1766 mtx_unlock(&rep->r_nmp->nm_mtx);
1771 * Unlock the stream socket for others.
1774 nfs_sndunlock(struct nfsreq *rep)
1776 int *statep = &rep->r_nmp->nm_state;
1778 mtx_lock(&rep->r_nmp->nm_mtx);
1779 if ((*statep & NFSSTA_SNDLOCK) == 0)
1780 panic("nfs sndunlock");
1781 *statep &= ~NFSSTA_SNDLOCK;
1782 if (*statep & NFSSTA_WANTSND) {
1783 *statep &= ~NFSSTA_WANTSND;
1786 mtx_unlock(&rep->r_nmp->nm_mtx);
1792 * Check for badly aligned mbuf data and realign by copying the unaligned
1793 * portion of the data into a new mbuf chain and freeing the portions
1794 * of the old chain that were replaced.
1796 * We cannot simply realign the data within the existing mbuf chain
1797 * because the underlying buffers may contain other rpc commands and
1798 * we cannot afford to overwrite them.
1800 * We would prefer to avoid this situation entirely. The situation does
1801 * not occur with NFS/UDP and is supposed to only occassionally occur
1802 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1806 nfs_realign(struct mbuf **pm, int hsiz)
1809 struct mbuf *n = NULL;
1813 while ((m = *pm) != NULL) {
1814 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1815 MGET(n, M_DONTWAIT, MT_DATA);
1818 if (m->m_len >= MINCLSIZE) {
1819 MCLGET(n, M_DONTWAIT);
1820 if (n->m_ext.ext_buf == NULL) {
1831 * If n is non-NULL, loop on m copying data, then replace the
1832 * portion of the chain that had to be realigned.
1835 ++nfs_realign_count;
1837 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1849 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1853 p = td ? td->td_proc : NULL;
1855 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1858 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1864 nfs_down(rep, nmp, td, msg, error, flags)
1866 struct nfsmount *nmp;
1873 mtx_lock(&nmp->nm_mtx);
1874 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1875 nmp->nm_state |= NFSSTA_TIMEO;
1876 mtx_unlock(&nmp->nm_mtx);
1877 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1880 mtx_unlock(&nmp->nm_mtx);
1881 #ifdef NFSSTA_LOCKTIMEO
1882 mtx_lock(&nmp->nm_mtx);
1883 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1884 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1885 mtx_unlock(&nmp->nm_mtx);
1886 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1889 mtx_unlock(&nmp->nm_mtx);
1892 mtx_lock(&rep->r_mtx);
1893 rep->r_flags |= R_TPRINTFMSG;
1894 mtx_unlock(&rep->r_mtx);
1896 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1900 nfs_up(rep, nmp, td, msg, flags)
1902 struct nfsmount *nmp;
1907 if (nmp == NULL || rep == NULL)
1909 mtx_lock(&rep->r_mtx);
1910 if ((rep->r_flags & R_TPRINTFMSG) != 0) {
1911 mtx_unlock(&rep->r_mtx);
1912 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1914 mtx_unlock(&rep->r_mtx);
1916 mtx_lock(&nmp->nm_mtx);
1917 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1918 nmp->nm_state &= ~NFSSTA_TIMEO;
1919 mtx_unlock(&nmp->nm_mtx);
1920 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1923 mtx_unlock(&nmp->nm_mtx);
1925 #ifdef NFSSTA_LOCKTIMEO
1926 mtx_lock(&nmp->nm_mtx);
1927 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1928 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1929 mtx_unlock(&nmp->nm_mtx);
1930 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1933 mtx_unlock(&nmp->nm_mtx);