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;
88 SYSCTL_DECL(_vfs_nfs);
90 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
91 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
94 "number of times the nfs client has had to reconnect");
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
96 "number of seconds to delay a retry after receiving EJUKEBOX");
99 * There is a congestion window for outstanding rpcs maintained per mount
100 * point. The cwnd size is adjusted in roughly the way that:
101 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
102 * SIGCOMM '88". ACM, August 1988.
103 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
104 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
105 * of rpcs is in progress.
106 * (The sent count and cwnd are scaled for integer arith.)
107 * Variants of "slow start" were tried and were found to be too much of a
108 * performance hit (ave. rtt 3 times larger),
109 * I suspect due to the large rtt that nfs rpcs have.
111 #define NFS_CWNDSCALE 256
112 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
113 #define NFS_NBACKOFF 8
114 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
115 struct callout nfs_callout;
117 static int nfs_msg(struct thread *, const char *, const char *, int);
118 static int nfs_realign(struct mbuf **pm, int hsiz);
119 static int nfs_reply(struct nfsreq *);
120 static void nfs_softterm(struct nfsreq *rep);
121 static int nfs_reconnect(struct nfsreq *rep);
122 static void nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag);
123 static void nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag);
125 extern struct mtx nfs_reqq_mtx;
131 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
132 NFS_DEFAULT_TIMER, /* NULL */
133 NFS_GETATTR_TIMER, /* GETATTR */
134 NFS_DEFAULT_TIMER, /* SETATTR */
135 NFS_LOOKUP_TIMER, /* LOOKUP */
136 NFS_GETATTR_TIMER, /* ACCESS */
137 NFS_READ_TIMER, /* READLINK */
138 NFS_READ_TIMER, /* READ */
139 NFS_WRITE_TIMER, /* WRITE */
140 NFS_DEFAULT_TIMER, /* CREATE */
141 NFS_DEFAULT_TIMER, /* MKDIR */
142 NFS_DEFAULT_TIMER, /* SYMLINK */
143 NFS_DEFAULT_TIMER, /* MKNOD */
144 NFS_DEFAULT_TIMER, /* REMOVE */
145 NFS_DEFAULT_TIMER, /* RMDIR */
146 NFS_DEFAULT_TIMER, /* RENAME */
147 NFS_DEFAULT_TIMER, /* LINK */
148 NFS_READ_TIMER, /* READDIR */
149 NFS_READ_TIMER, /* READDIRPLUS */
150 NFS_DEFAULT_TIMER, /* FSSTAT */
151 NFS_DEFAULT_TIMER, /* FSINFO */
152 NFS_DEFAULT_TIMER, /* PATHCONF */
153 NFS_DEFAULT_TIMER, /* COMMIT */
154 NFS_DEFAULT_TIMER, /* NOOP */
158 * Choose the correct RTT timer for this NFS procedure.
160 static inline enum nfs_rto_timer_t
161 nfs_rto_timer(u_int32_t procnum)
163 return nfs_proct[procnum];
167 * Initialize the RTT estimator state for a new mount point.
170 nfs_init_rtt(struct nfsmount *nmp)
174 for (i = 0; i < NFS_MAX_TIMER; i++)
175 nmp->nm_srtt[i] = NFS_INITRTT;
176 for (i = 0; i < NFS_MAX_TIMER; i++)
177 nmp->nm_sdrtt[i] = 0;
181 * Update a mount point's RTT estimator state using data from the
184 * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
186 * NB: Since the timer resolution of NFS_HZ is so course, it can often
187 * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
188 * between N + dt and N + 2 - dt ticks, add 1 before calculating the
192 nfs_update_rtt(struct nfsreq *rep)
194 int t1 = rep->r_rtt + 1;
195 int index = nfs_rto_timer(rep->r_procnum) - 1;
196 int *srtt = &rep->r_nmp->nm_srtt[index];
197 int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
208 * Estimate RTO for an NFS RPC sent via an unreliable datagram.
210 * Use the mean and mean deviation of RTT for the appropriate type
211 * of RPC for the frequent RPCs and a default for the others.
212 * The justification for doing "other" this way is that these RPCs
213 * happen so infrequently that timer est. would probably be stale.
214 * Also, since many of these RPCs are non-idempotent, a conservative
215 * timeout is desired.
217 * getattr, lookup - A+2D
222 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
224 enum nfs_rto_timer_t timer = nfs_rto_timer(procnum);
225 int index = timer - 1;
229 case NFS_GETATTR_TIMER:
230 case NFS_LOOKUP_TIMER:
231 rto = ((nmp->nm_srtt[index] + 3) >> 2) +
232 ((nmp->nm_sdrtt[index] + 1) >> 1);
235 case NFS_WRITE_TIMER:
236 rto = ((nmp->nm_srtt[index] + 7) >> 3) +
237 (nmp->nm_sdrtt[index] + 1);
244 if (rto < NFS_MINRTO)
246 else if (rto > NFS_MAXRTO)
254 * Initialize sockets and congestion for a new NFS connection.
255 * We do not free the sockaddr if error.
258 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
261 int error, rcvreserve, sndreserve;
263 struct sockaddr *saddr;
264 struct thread *td = &thread0; /* only used for socreate and sobind */
268 if (nmp->nm_sotype == SOCK_STREAM) {
269 mtx_lock(&nmp->nm_mtx);
270 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
271 nmp->nm_nfstcpstate.rpcresid = 0;
272 mtx_unlock(&nmp->nm_mtx);
276 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
277 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
281 nmp->nm_soflags = so->so_proto->pr_flags;
284 * Some servers require that the client port be a reserved port number.
286 if (nmp->nm_flag & NFSMNT_RESVPORT) {
289 struct sockaddr_in6 ssin;
292 bzero(&sopt, sizeof sopt);
293 switch(saddr->sa_family) {
295 sopt.sopt_level = IPPROTO_IP;
296 sopt.sopt_name = IP_PORTRANGE;
297 ip = IP_PORTRANGE_LOW;
298 ip2 = IP_PORTRANGE_DEFAULT;
299 len = sizeof (struct sockaddr_in);
303 sopt.sopt_level = IPPROTO_IPV6;
304 sopt.sopt_name = IPV6_PORTRANGE;
305 ip = IPV6_PORTRANGE_LOW;
306 ip2 = IPV6_PORTRANGE_DEFAULT;
307 len = sizeof (struct sockaddr_in6);
313 sa = (struct sockaddr *)&ssin;
316 sa->sa_family = saddr->sa_family;
317 sopt.sopt_dir = SOPT_SET;
318 sopt.sopt_val = (void *)&ip;
319 sopt.sopt_valsize = sizeof(ip);
320 error = sosetopt(so, &sopt);
323 error = sobind(so, sa, td);
327 error = sosetopt(so, &sopt);
334 * Protocols that do not require connections may be optionally left
335 * unconnected for servers that reply from a port other than NFS_PORT.
337 mtx_lock(&nmp->nm_mtx);
338 if (nmp->nm_flag & NFSMNT_NOCONN) {
339 if (nmp->nm_soflags & PR_CONNREQUIRED) {
341 mtx_unlock(&nmp->nm_mtx);
344 mtx_unlock(&nmp->nm_mtx);
346 mtx_unlock(&nmp->nm_mtx);
347 error = soconnect(so, nmp->nm_nam, td);
352 * Wait for the connection to complete. Cribbed from the
353 * connect system call but with the wait timing out so
354 * that interruptible mounts don't hang here for a long time.
357 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
358 (void) msleep(&so->so_timeo, SOCK_MTX(so),
359 PSOCK, "nfscon", 2 * hz);
360 if ((so->so_state & SS_ISCONNECTING) &&
361 so->so_error == 0 && rep &&
362 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
363 so->so_state &= ~SS_ISCONNECTING;
369 error = so->so_error;
376 so->so_rcv.sb_timeo = 12 * hz;
377 so->so_snd.sb_timeo = 5 * hz;
380 * Get buffer reservation size from sysctl, but impose reasonable
383 pktscale = nfs_bufpackets;
388 mtx_lock(&nmp->nm_mtx);
389 if (nmp->nm_sotype == SOCK_DGRAM) {
390 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
391 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
392 NFS_MAXPKTHDR) * pktscale;
393 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
394 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
395 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
396 NFS_MAXPKTHDR) * pktscale;
398 if (nmp->nm_sotype != SOCK_STREAM)
399 panic("nfscon sotype");
400 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
404 bzero(&sopt, sizeof sopt);
405 sopt.sopt_dir = SOPT_SET;
406 sopt.sopt_level = SOL_SOCKET;
407 sopt.sopt_name = SO_KEEPALIVE;
408 sopt.sopt_val = &val;
409 sopt.sopt_valsize = sizeof val;
411 mtx_unlock(&nmp->nm_mtx);
413 mtx_lock(&nmp->nm_mtx);
415 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
419 bzero(&sopt, sizeof sopt);
420 sopt.sopt_dir = SOPT_SET;
421 sopt.sopt_level = IPPROTO_TCP;
422 sopt.sopt_name = TCP_NODELAY;
423 sopt.sopt_val = &val;
424 sopt.sopt_valsize = sizeof val;
426 mtx_unlock(&nmp->nm_mtx);
428 mtx_lock(&nmp->nm_mtx);
430 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
431 sizeof (u_int32_t)) * pktscale;
432 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
433 sizeof (u_int32_t)) * pktscale;
435 mtx_unlock(&nmp->nm_mtx);
436 error = soreserve(so, sndreserve, rcvreserve);
439 SOCKBUF_LOCK(&so->so_rcv);
440 so->so_rcv.sb_flags |= SB_NOINTR;
441 so->so_upcallarg = (caddr_t)nmp;
442 if (so->so_type == SOCK_STREAM)
443 so->so_upcall = nfs_clnt_tcp_soupcall;
445 so->so_upcall = nfs_clnt_udp_soupcall;
446 so->so_rcv.sb_flags |= SB_UPCALL;
447 SOCKBUF_UNLOCK(&so->so_rcv);
448 SOCKBUF_LOCK(&so->so_snd);
449 so->so_snd.sb_flags |= SB_NOINTR;
450 SOCKBUF_UNLOCK(&so->so_snd);
452 mtx_lock(&nmp->nm_mtx);
453 /* Initialize other non-zero congestion variables */
455 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
457 nmp->nm_timeouts = 0;
458 mtx_unlock(&nmp->nm_mtx);
470 * Called when a connection is broken on a reliable protocol.
471 * - clean up the old socket
472 * - nfs_connect() again
473 * - set R_MUSTRESEND for all outstanding requests on mount point
474 * If this fails the mount point is DEAD!
475 * nb: Must be called with the nfs_sndlock() set on the mount point.
478 nfs_reconnect(struct nfsreq *rep)
481 struct nfsmount *nmp = rep->r_nmp;
486 while ((error = nfs_connect(nmp, rep)) != 0) {
487 if (error == ERESTART)
489 if (error == EIO || error == EINTR)
492 (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 = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
610 flags, curthread /*XXX*/);
611 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
613 mtx_lock(&rep->r_mtx);
614 rep->r_flags |= R_MUSTRESEND;
615 mtx_unlock(&rep->r_mtx);
620 * Don't report EPIPE errors on nfs sockets.
621 * These can be due to idle tcp mounts which will be closed by
622 * netapp, solaris, etc. if left idle too long.
624 if (error != EPIPE) {
625 log(LOG_INFO, "nfs send error %d for server %s\n",
627 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
630 * Deal with errors for the client side.
632 error2 = NFS_SIGREP(rep);
636 mtx_lock(&rep->r_mtx);
637 rep->r_flags |= R_MUSTRESEND;
638 mtx_unlock(&rep->r_mtx);
642 * Handle any recoverable (soft) socket errors here. (?)
644 if (error != EINTR && error != ERESTART && error != EIO &&
645 error != EWOULDBLOCK && error != EPIPE)
654 nfs_reply(struct nfsreq *rep)
656 register struct socket *so;
657 register struct mbuf *m;
658 int error = 0, sotype, slpflag;
662 sotype = rep->r_nmp->nm_sotype;
664 * For reliable protocols, lock against other senders/receivers
665 * in case a reconnect is necessary.
667 if (sotype != SOCK_DGRAM) {
668 error = nfs_sndlock(rep);
672 mtx_lock(&rep->r_nmp->nm_mtx);
673 mtx_lock(&rep->r_mtx);
675 mtx_unlock(&rep->r_mtx);
676 mtx_unlock(&rep->r_nmp->nm_mtx);
681 if (rep->r_flags & R_SOFTTERM) {
682 mtx_unlock(&rep->r_mtx);
683 mtx_unlock(&rep->r_nmp->nm_mtx);
688 so = rep->r_nmp->nm_so;
690 (rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) {
691 mtx_unlock(&rep->r_mtx);
692 mtx_unlock(&rep->r_nmp->nm_mtx);
693 error = nfs_reconnect(rep);
700 while (rep->r_flags & R_MUSTRESEND) {
701 mtx_unlock(&rep->r_mtx);
702 mtx_unlock(&rep->r_nmp->nm_mtx);
703 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
704 nfsstats.rpcretries++;
705 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
707 if (error == EINTR || error == ERESTART ||
708 (error = nfs_reconnect(rep)) != 0) {
714 mtx_lock(&rep->r_nmp->nm_mtx);
715 mtx_lock(&rep->r_mtx);
717 mtx_unlock(&rep->r_nmp->nm_mtx);
718 mtx_unlock(&rep->r_mtx);
722 mtx_lock(&rep->r_nmp->nm_mtx);
723 if (rep->r_nmp->nm_flag & NFSMNT_INT)
725 mtx_unlock(&rep->r_nmp->nm_mtx);
726 mtx_lock(&rep->r_mtx);
727 while ((rep->r_mrep == NULL) && (error == 0) &&
728 ((rep->r_flags & R_SOFTTERM) == 0) &&
729 ((sotype == SOCK_DGRAM) || ((rep->r_flags & R_MUSTRESEND) == 0)))
730 error = msleep((caddr_t)rep, &rep->r_mtx,
731 slpflag | (PZERO - 1), "nfsreq", 0);
732 if (error == EINTR || error == ERESTART) {
733 /* NFS operations aren't restartable. Map ERESTART to EINTR */
735 mtx_unlock(&rep->r_mtx);
738 if (rep->r_flags & R_SOFTTERM) {
739 /* Request was terminated because we exceeded the retries (soft mount) */
741 mtx_unlock(&rep->r_mtx);
744 mtx_unlock(&rep->r_mtx);
745 if (sotype == SOCK_STREAM) {
746 mtx_lock(&rep->r_nmp->nm_mtx);
747 mtx_lock(&rep->r_mtx);
748 if (((rep->r_nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) ||
749 (rep->r_flags & R_MUSTRESEND))) {
750 mtx_unlock(&rep->r_mtx);
751 mtx_unlock(&rep->r_nmp->nm_mtx);
752 error = nfs_sndlock(rep);
757 mtx_unlock(&rep->r_mtx);
758 mtx_unlock(&rep->r_nmp->nm_mtx);
768 * Make nfs_realign() non-blocking. Also make nfsm_dissect() nonblocking.
771 nfs_clnt_match_xid(struct socket *so,
772 struct nfsmount *nmp,
782 * Search for any mbufs that are not a multiple of 4 bytes long
783 * or with m_data not longword aligned.
784 * These could cause pointer alignment problems, so copy them to
785 * well aligned mbufs.
787 if (nfs_realign(&mrep, 5 * NFSX_UNSIGNED) == ENOMEM) {
789 nfsstats.rpcinvalid++;
794 * Get the xid and check that it is an rpc reply
797 dpos = mtod(md, caddr_t);
798 tl = nfsm_dissect_nonblock(u_int32_t *, 2*NFSX_UNSIGNED);
800 if (*tl != rpc_reply) {
803 nfsstats.rpcinvalid++;
807 mtx_lock(&nfs_reqq_mtx);
809 * Loop through the request list to match up the reply
810 * Iff no match, just drop the datagram
812 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
813 mtx_lock(&nmp->nm_mtx);
814 mtx_lock(&rep->r_mtx);
815 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
821 * Update congestion window.
822 * Do the additive increase of
825 if (nmp->nm_cwnd <= nmp->nm_sent) {
827 (NFS_CWNDSCALE * NFS_CWNDSCALE +
828 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
829 if (nmp->nm_cwnd > NFS_MAXCWND)
830 nmp->nm_cwnd = NFS_MAXCWND;
832 if (rep->r_flags & R_SENT) {
833 rep->r_flags &= ~R_SENT;
834 nmp->nm_sent -= NFS_CWNDSCALE;
836 if (rep->r_flags & R_TIMING)
838 nmp->nm_timeouts = 0;
839 wakeup((caddr_t)rep);
840 mtx_unlock(&rep->r_mtx);
841 mtx_unlock(&nmp->nm_mtx);
844 mtx_unlock(&rep->r_mtx);
845 mtx_unlock(&nmp->nm_mtx);
848 * If not matched to a request, drop it.
849 * If it's mine, wake up requestor.
852 nfsstats.rpcunexpected++;
855 mtx_unlock(&nfs_reqq_mtx);
859 nfs_mark_for_reconnect(struct nfsmount *nmp)
863 mtx_lock(&nmp->nm_mtx);
864 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
865 mtx_unlock(&nmp->nm_mtx);
867 * Wakeup all processes that are waiting for replies
868 * on this mount point. One of them does the reconnect.
870 mtx_lock(&nfs_reqq_mtx);
871 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
872 if (rp->r_nmp == nmp) {
873 mtx_lock(&rp->r_mtx);
874 rp->r_flags |= R_MUSTRESEND;
876 mtx_unlock(&rp->r_mtx);
879 mtx_unlock(&nfs_reqq_mtx);
883 nfstcp_readable(struct socket *so, int bytes)
887 SOCKBUF_LOCK(&so->so_rcv);
888 retval = (so->so_rcv.sb_cc >= (bytes) ||
889 (so->so_rcv.sb_state & SBS_CANTRCVMORE) ||
891 SOCKBUF_UNLOCK(&so->so_rcv);
895 #define nfstcp_marker_readable(so) nfstcp_readable(so, sizeof(u_int32_t))
898 nfs_copy_len(struct mbuf *mp, char *buf, int len)
900 while (len > 0 && mp != NULL) {
901 int copylen = min(len, mp->m_len);
903 bcopy(mp->m_data, buf, copylen);
912 nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag)
914 struct nfsmount *nmp = (struct nfsmount *)arg;
915 struct mbuf *mp = NULL;
922 * Don't pick any more data from the socket if we've marked the
923 * mountpoint for reconnect.
925 mtx_lock(&nmp->nm_mtx);
926 if (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) {
927 mtx_unlock(&nmp->nm_mtx);
930 mtx_unlock(&nmp->nm_mtx);
931 auio.uio_td = curthread;
932 auio.uio_segflg = UIO_SYSSPACE;
933 auio.uio_rw = UIO_READ;
935 mtx_lock(&nmp->nm_mtx);
936 if (nmp->nm_nfstcpstate.flags & NFS_TCP_EXPECT_RPCMARKER) {
939 mtx_unlock(&nmp->nm_mtx);
940 if (!nfstcp_marker_readable(so)) {
941 /* Marker is not readable */
944 auio.uio_resid = sizeof(u_int32_t);
948 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
949 error = so->so_proto->pr_usrreqs->pru_soreceive
950 (so, (struct sockaddr **)0,
951 &auio, &mp, (struct mbuf **)0, &rcvflg);
953 * We've already tested that the socket is readable. 2 cases
954 * here, we either read 0 bytes (client closed connection),
955 * or got some other error. In both cases, we tear down the
958 if (error || auio.uio_resid > 0) {
959 if (error && error != ECONNRESET) {
961 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
967 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
969 * Sigh. We can't do the obvious thing here (which would
970 * be to have soreceive copy the length from mbufs for us).
971 * Calling uiomove() from the context of a socket callback
972 * (even for kernel-kernel copies) leads to LORs (since
973 * we hold network locks at this point).
975 if ((resid = nfs_copy_len(mp, (char *)&len,
976 sizeof(u_int32_t)))) {
977 log(LOG_ERR, "%s (%d) from nfs server %s\n",
978 "Bad RPC HDR length",
979 (int)(sizeof(u_int32_t) - resid),
980 nmp->nm_mountp->mnt_stat.f_mntfromname);
983 len = ntohl(len) & ~0x80000000;
986 * This is SERIOUS! We are out of sync with the sender
987 * and forcing a disconnect/reconnect is all I can do.
989 if (len > NFS_MAXPACKET || len == 0) {
990 log(LOG_ERR, "%s (%d) from nfs server %s\n",
991 "impossible packet length",
993 nmp->nm_mountp->mnt_stat.f_mntfromname);
996 mtx_lock(&nmp->nm_mtx);
997 nmp->nm_nfstcpstate.rpcresid = len;
998 nmp->nm_nfstcpstate.flags &= ~(NFS_TCP_EXPECT_RPCMARKER);
999 mtx_unlock(&nmp->nm_mtx);
1001 mtx_unlock(&nmp->nm_mtx);
1004 * Processed RPC marker or no RPC marker to process.
1005 * Pull in and process data.
1007 mtx_lock(&nmp->nm_mtx);
1008 if (nmp->nm_nfstcpstate.rpcresid > 0) {
1009 mtx_unlock(&nmp->nm_mtx);
1010 if (!nfstcp_readable(so, nmp->nm_nfstcpstate.rpcresid)) {
1011 /* All data not readable */
1014 auio.uio_resid = nmp->nm_nfstcpstate.rpcresid;
1015 auio.uio_iov = NULL;
1016 auio.uio_iovcnt = 0;
1018 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
1019 error = so->so_proto->pr_usrreqs->pru_soreceive
1020 (so, (struct sockaddr **)0,
1021 &auio, &mp, (struct mbuf **)0, &rcvflg);
1022 if (error || auio.uio_resid > 0) {
1023 if (error && error != ECONNRESET) {
1025 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1028 goto mark_reconnect;
1031 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1032 mtx_lock(&nmp->nm_mtx);
1033 nmp->nm_nfstcpstate.rpcresid = 0;
1034 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
1035 mtx_unlock(&nmp->nm_mtx);
1036 /* We got the entire RPC reply. Match XIDs and wake up requestor */
1037 nfs_clnt_match_xid(so, nmp, mp);
1039 mtx_unlock(&nmp->nm_mtx);
1043 nfs_mark_for_reconnect(nmp);
1047 nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag)
1049 struct nfsmount *nmp = (struct nfsmount *)arg;
1051 struct mbuf *mp = NULL;
1052 struct mbuf *control = NULL;
1055 auio.uio_resid = 1000000;
1056 auio.uio_td = curthread;
1057 rcvflag = MSG_DONTWAIT;
1058 auio.uio_resid = 1000000000;
1060 mp = control = NULL;
1061 error = so->so_proto->pr_usrreqs->pru_soreceive(so,
1063 &control, &rcvflag);
1067 nfs_clnt_match_xid(so, nmp, mp);
1068 } while (mp && !error);
1072 * nfs_request - goes something like this
1073 * - fill in request struct
1074 * - links it into list
1075 * - calls nfs_send() for first transmit
1076 * - calls nfs_receive() to get reply
1077 * - break down rpc header and return with nfs reply pointed to
1079 * nb: always frees up mreq mbuf list
1082 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
1083 struct thread *td, struct ucred *cred, struct mbuf **mrp,
1084 struct mbuf **mdp, caddr_t *dposp)
1086 struct mbuf *mrep, *m2;
1090 struct nfsmount *nmp;
1091 struct mbuf *m, *md, *mheadend;
1094 int error = 0, mrest_len, auth_len, auth_type;
1098 /* Reject requests while attempting a forced unmount. */
1099 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1103 nmp = VFSTONFS(vp->v_mount);
1104 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1105 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
1106 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1107 bzero(rep, sizeof(struct nfsreq));
1111 rep->r_procnum = procnum;
1112 mtx_init(&rep->r_mtx, "NFSrep lock", NULL, MTX_DEF);
1114 getmicrouptime(&now);
1115 rep->r_lastmsg = now.tv_sec -
1116 ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
1117 mrest_len = m_length(mrest, NULL);
1120 * Get the RPC header with authorization.
1122 auth_type = RPCAUTH_UNIX;
1123 if (cred->cr_ngroups < 1)
1124 panic("nfsreq nogrps");
1125 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1126 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1128 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1129 mrest, mrest_len, &mheadend, &xidp);
1132 * For stream protocols, insert a Sun RPC Record Mark.
1134 if (nmp->nm_sotype == SOCK_STREAM) {
1135 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
1136 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1137 (m->m_pkthdr.len - NFSX_UNSIGNED));
1142 if (nmp->nm_flag & NFSMNT_SOFT)
1143 rep->r_retry = nmp->nm_retry;
1145 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1146 rep->r_rtt = rep->r_rexmit = 0;
1147 if (nfs_rto_timer(procnum) != NFS_DEFAULT_TIMER)
1148 rep->r_flags = R_TIMING;
1154 * Do the client side RPC.
1156 nfsstats.rpcrequests++;
1158 * Chain request into list of outstanding requests. Be sure
1159 * to put it LAST so timer finds oldest requests first.
1161 mtx_lock(&nfs_reqq_mtx);
1162 if (TAILQ_EMPTY(&nfs_reqq))
1163 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1164 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1165 mtx_unlock(&nfs_reqq_mtx);
1168 * If backing off another request or avoiding congestion, don't
1169 * send this one now but let timer do it. If not timing a request,
1172 mtx_lock(&nmp->nm_mtx);
1173 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1174 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1175 nmp->nm_sent < nmp->nm_cwnd)) {
1176 mtx_unlock(&nmp->nm_mtx);
1177 error = nfs_sndlock(rep);
1179 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1180 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1183 mtx_lock(&nfs_reqq_mtx);
1185 * nfs_timer() could've re-transmitted the request if we ended up
1186 * blocking on nfs_send() too long, so check for R_SENT here.
1188 if (!error && (rep->r_flags & (R_SENT | R_MUSTRESEND)) == 0) {
1189 mtx_lock(&nmp->nm_mtx);
1190 nmp->nm_sent += NFS_CWNDSCALE;
1191 mtx_unlock(&nmp->nm_mtx);
1192 rep->r_flags |= R_SENT;
1194 mtx_unlock(&nfs_reqq_mtx);
1196 mtx_unlock(&nmp->nm_mtx);
1201 * Wait for the reply from our send or the timer's.
1203 if (!error || error == EPIPE)
1204 error = nfs_reply(rep);
1207 * RPC done, unlink the request.
1209 mtx_lock(&nfs_reqq_mtx);
1211 * nfs_timer() may be in the process of re-transmitting this request.
1212 * nfs_timer() drops the nfs_reqq_mtx before the pru_send() (to avoid LORs).
1213 * Wait till nfs_timer() completes the re-transmission. When the reply
1214 * comes back, it will be discarded (since the req struct for it no longer
1217 while (rep->r_flags & R_REXMIT_INPROG) {
1218 msleep((caddr_t)&rep->r_flags, &nfs_reqq_mtx,
1219 (PZERO - 1), "nfsrxmt", 0);
1221 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1222 if (TAILQ_EMPTY(&nfs_reqq))
1223 callout_stop(&nfs_callout);
1225 * Decrement the outstanding request count.
1227 if (rep->r_flags & R_SENT) {
1228 rep->r_flags &= ~R_SENT; /* paranoia */
1229 mtx_lock(&nmp->nm_mtx);
1230 nmp->nm_sent -= NFS_CWNDSCALE;
1231 mtx_unlock(&nmp->nm_mtx);
1233 mtx_unlock(&nfs_reqq_mtx);
1236 * If there was a successful reply and a tprintf msg.
1237 * tprintf a response.
1241 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1249 * If we got interrupted by a signal in nfs_reply(), there's
1250 * a very small window where the reply could've come in before
1251 * this process got scheduled in. To handle that case, we need
1252 * to free the reply if it was delivered.
1254 if (rep->r_mrep != NULL)
1255 m_freem(rep->r_mrep);
1256 m_freem(rep->r_mreq);
1257 mtx_destroy(&rep->r_mtx);
1258 free((caddr_t)rep, M_NFSREQ);
1262 if (rep->r_mrep == NULL)
1263 panic("nfs_request: rep->r_mrep shouldn't be NULL if no error\n");
1266 * break down the rpc header and check if ok
1268 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1269 if (*tl++ == rpc_msgdenied) {
1270 if (*tl == rpc_mismatch)
1275 m_freem(rep->r_mreq);
1276 mtx_destroy(&rep->r_mtx);
1277 free((caddr_t)rep, M_NFSREQ);
1282 * Just throw away any verifyer (ie: kerberos etc).
1284 i = fxdr_unsigned(int, *tl++); /* verf type */
1285 i = fxdr_unsigned(int32_t, *tl); /* len */
1287 nfsm_adv(nfsm_rndup(i));
1288 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1291 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1293 error = fxdr_unsigned(int, *tl);
1294 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1295 error == NFSERR_TRYLATER) {
1298 waituntil = time_second + nfs3_jukebox_delay;
1299 while (time_second < waituntil) {
1301 (void) tsleep(&lbolt, PSOCK, "nqnfstry", 0);
1304 mtx_lock(&nfs_reqq_mtx);
1307 rep->r_xid = *xidp = txdr_unsigned(nfs_xid);
1308 mtx_unlock(&nfs_reqq_mtx);
1313 * If the File Handle was stale, invalidate the
1314 * lookup cache, just in case.
1316 if (error == ESTALE)
1318 if (nmp->nm_flag & NFSMNT_NFSV3) {
1322 error |= NFSERR_RETERR;
1325 m_freem(rep->r_mreq);
1326 mtx_destroy(&rep->r_mtx);
1327 free((caddr_t)rep, M_NFSREQ);
1334 m_freem(rep->r_mreq);
1335 mtx_destroy(&rep->r_mtx);
1336 FREE((caddr_t)rep, M_NFSREQ);
1340 error = EPROTONOSUPPORT;
1342 m_freem(rep->r_mreq);
1343 mtx_destroy(&rep->r_mtx);
1344 free((caddr_t)rep, M_NFSREQ);
1350 * Scan the nfsreq list and retranmit any requests that have timed out
1351 * To avoid retransmission attempts on STREAM sockets (in the future) make
1352 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1354 * The nfs reqq lock cannot be held while we do the pru_send() because of a
1355 * lock ordering violation. The NFS client socket callback acquires
1356 * inp_lock->nfsreq mutex and pru_send acquires inp_lock. So we drop the
1357 * reqq mutex (and reacquire it after the pru_send()). The req structure
1358 * (for the rexmit) is prevented from being removed by the R_REXMIT_INPROG flag.
1361 nfs_timer(void *arg)
1366 struct nfsmount *nmp;
1371 getmicrouptime(&now);
1372 mtx_lock(&Giant); /* nfs_down -> tprintf */
1373 mtx_lock(&nfs_reqq_mtx);
1374 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1376 mtx_lock(&rep->r_mtx);
1377 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
1378 mtx_unlock(&rep->r_mtx);
1381 mtx_unlock(&rep->r_mtx);
1382 if (nfs_sigintr(nmp, rep, rep->r_td))
1384 mtx_lock(&nmp->nm_mtx);
1385 mtx_lock(&rep->r_mtx);
1386 if (nmp->nm_tprintf_initial_delay != 0 &&
1387 (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1388 rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1389 mtx_unlock(&rep->r_mtx);
1390 mtx_unlock(&nmp->nm_mtx);
1391 rep->r_lastmsg = now.tv_sec;
1392 nfs_down(rep, nmp, rep->r_td, "not responding",
1394 mtx_lock(&nmp->nm_mtx);
1395 mtx_lock(&rep->r_mtx);
1397 if (rep->r_rtt >= 0) {
1399 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1400 timeo = nmp->nm_timeo;
1402 timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1403 if (nmp->nm_timeouts > 0)
1404 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1405 if (rep->r_rtt <= timeo) {
1406 mtx_unlock(&rep->r_mtx);
1407 mtx_unlock(&nmp->nm_mtx);
1410 if (nmp->nm_timeouts < NFS_NBACKOFF)
1413 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1414 nfsstats.rpctimeouts++;
1416 mtx_unlock(&rep->r_mtx);
1417 mtx_unlock(&nmp->nm_mtx);
1420 if (nmp->nm_sotype != SOCK_DGRAM) {
1421 if (++rep->r_rexmit > NFS_MAXREXMIT)
1422 rep->r_rexmit = NFS_MAXREXMIT;
1424 * For NFS/TCP, setting R_MUSTRESEND and waking up
1425 * the requester will cause the request to be
1426 * retransmitted (in nfs_reply()), re-connecting
1429 rep->r_flags |= R_MUSTRESEND;
1430 wakeup((caddr_t)rep);
1432 mtx_unlock(&rep->r_mtx);
1433 mtx_unlock(&nmp->nm_mtx);
1436 if ((so = nmp->nm_so) == NULL) {
1437 mtx_unlock(&rep->r_mtx);
1438 mtx_unlock(&nmp->nm_mtx);
1442 * If there is enough space and the window allows..
1444 * Set r_rtt to -1 in case we fail to send it now.
1447 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1448 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || (rep->r_flags & R_SENT) ||
1449 nmp->nm_sent < nmp->nm_cwnd)) {
1450 mtx_unlock(&rep->r_mtx);
1451 mtx_unlock(&nmp->nm_mtx);
1452 if ((m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
1454 * Mark the request to indicate that a XMIT is in
1455 * progress to prevent the req structure being
1456 * removed in nfs_request().
1458 mtx_lock(&rep->r_mtx);
1459 rep->r_flags |= R_REXMIT_INPROG;
1460 mtx_unlock(&rep->r_mtx);
1461 mtx_unlock(&nfs_reqq_mtx);
1463 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1464 error = (*so->so_proto->pr_usrreqs->pru_send)
1465 (so, 0, m, NULL, NULL, curthread);
1467 error = (*so->so_proto->pr_usrreqs->pru_send)
1468 (so, 0, m, nmp->nm_nam, NULL,
1471 mtx_lock(&nfs_reqq_mtx);
1472 mtx_lock(&nmp->nm_mtx);
1473 mtx_lock(&rep->r_mtx);
1474 rep->r_flags &= ~R_REXMIT_INPROG;
1475 wakeup((caddr_t)&rep->r_flags);
1477 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1479 rep->r_flags |= R_RESENDERR;
1482 * Iff first send, start timing
1483 * else turn timing off, backoff timer
1484 * and divide congestion window by 2.
1486 rep->r_flags &= ~R_RESENDERR;
1487 if (rep->r_flags & R_SENT) {
1488 rep->r_flags &= ~R_TIMING;
1489 if (++rep->r_rexmit > NFS_MAXREXMIT)
1490 rep->r_rexmit = NFS_MAXREXMIT;
1492 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1493 nmp->nm_cwnd = NFS_CWNDSCALE;
1494 nfsstats.rpcretries++;
1496 rep->r_flags |= R_SENT;
1497 nmp->nm_sent += NFS_CWNDSCALE;
1501 mtx_unlock(&rep->r_mtx);
1502 mtx_unlock(&nmp->nm_mtx);
1505 mtx_unlock(&rep->r_mtx);
1506 mtx_unlock(&nmp->nm_mtx);
1509 mtx_unlock(&nfs_reqq_mtx);
1510 mtx_unlock(&Giant); /* nfs_down -> tprintf */
1511 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1515 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1516 * wait for all requests to complete. This is used by forced unmounts
1517 * to terminate any outstanding RPCs.
1520 nfs_nmcancelreqs(nmp)
1521 struct nfsmount *nmp;
1526 mtx_lock(&nfs_reqq_mtx);
1527 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1528 mtx_lock(&req->r_mtx);
1529 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1530 (req->r_flags & R_SOFTTERM)) {
1531 mtx_unlock(&req->r_mtx);
1535 mtx_unlock(&req->r_mtx);
1537 mtx_unlock(&nfs_reqq_mtx);
1539 for (i = 0; i < 30; i++) {
1540 mtx_lock(&nfs_reqq_mtx);
1541 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1542 if (nmp == req->r_nmp)
1545 mtx_unlock(&nfs_reqq_mtx);
1549 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1556 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1557 * The nm_send count is decremented now to avoid deadlocks when the process in
1558 * soreceive() hasn't yet managed to send its own request.
1562 nfs_softterm(struct nfsreq *rep)
1564 KASSERT(mtx_owned(&rep->r_mtx), ("NFS req lock not owned !"));
1565 rep->r_flags |= R_SOFTTERM;
1566 if (rep->r_flags & R_SENT) {
1567 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1568 rep->r_flags &= ~R_SENT;
1571 * Request terminated, wakeup the blocked process, so that we
1572 * can return EINTR back.
1574 wakeup((caddr_t)rep);
1578 * Any signal that can interrupt an NFS operation in an intr mount
1579 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1581 int nfs_sig_set[] = {
1591 * Check to see if one of the signals in our subset is pending on
1592 * the process (in an intr mount).
1595 nfs_sig_pending(sigset_t set)
1599 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
1600 if (SIGISMEMBER(set, nfs_sig_set[i]))
1606 * The set/restore sigmask functions are used to (temporarily) overwrite
1607 * the process p_sigmask during an RPC call (for example). These are also
1608 * used in other places in the NFS client that might tsleep().
1611 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
1619 td = curthread; /* XXX */
1621 /* Remove the NFS set of signals from newset */
1623 mtx_lock(&p->p_sigacts->ps_mtx);
1624 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
1626 * But make sure we leave the ones already masked
1627 * by the process, ie. remove the signal from the
1628 * temporary signalmask only if it wasn't already
1631 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
1632 !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
1633 SIGDELSET(newset, nfs_sig_set[i]);
1635 mtx_unlock(&p->p_sigacts->ps_mtx);
1637 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1641 nfs_restore_sigmask(struct thread *td, sigset_t *set)
1644 td = curthread; /* XXX */
1645 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1649 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1650 * old one after msleep() returns.
1653 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1659 if ((priority & PCATCH) == 0)
1660 return msleep(ident, mtx, priority, wmesg, timo);
1662 td = curthread; /* XXX */
1663 nfs_set_sigmask(td, &oldset);
1664 error = msleep(ident, mtx, priority, wmesg, timo);
1665 nfs_restore_sigmask(td, &oldset);
1671 * Test for a termination condition pending on the process.
1672 * This is used for NFSMNT_INT mounts.
1675 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1681 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1682 return nfs4_sigintr(nmp, rep, td);
1684 mtx_lock(&rep->r_mtx);
1685 if (rep->r_flags & R_SOFTTERM) {
1686 mtx_unlock(&rep->r_mtx);
1690 mtx_unlock(&rep->r_mtx);
1692 /* Terminate all requests while attempting a forced unmount. */
1693 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1697 if (!(nmp->nm_flag & NFSMNT_INT))
1703 tmpset = p->p_siglist;
1704 SIGSETNAND(tmpset, td->td_sigmask);
1705 mtx_lock(&p->p_sigacts->ps_mtx);
1706 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1707 mtx_unlock(&p->p_sigacts->ps_mtx);
1708 if (SIGNOTEMPTY(p->p_siglist) && nfs_sig_pending(tmpset)) {
1720 * Lock a socket against others.
1721 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1722 * and also to avoid race conditions between the processes with nfs requests
1723 * in progress when a reconnect is necessary.
1726 nfs_sndlock(struct nfsreq *rep)
1728 int *statep = &rep->r_nmp->nm_state;
1730 int error, slpflag = 0, slptimeo = 0;
1733 mtx_lock(&rep->r_nmp->nm_mtx);
1734 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1736 while (*statep & NFSSTA_SNDLOCK) {
1737 error = nfs_sigintr(rep->r_nmp, rep, td);
1739 mtx_unlock(&rep->r_nmp->nm_mtx);
1742 *statep |= NFSSTA_WANTSND;
1743 (void) msleep(statep, &rep->r_nmp->nm_mtx,
1744 slpflag | (PZERO - 1), "nfsndlck", slptimeo);
1745 if (slpflag == PCATCH) {
1750 *statep |= NFSSTA_SNDLOCK;
1751 mtx_unlock(&rep->r_nmp->nm_mtx);
1756 * Unlock the stream socket for others.
1759 nfs_sndunlock(struct nfsreq *rep)
1761 int *statep = &rep->r_nmp->nm_state;
1763 mtx_lock(&rep->r_nmp->nm_mtx);
1764 if ((*statep & NFSSTA_SNDLOCK) == 0)
1765 panic("nfs sndunlock");
1766 *statep &= ~NFSSTA_SNDLOCK;
1767 if (*statep & NFSSTA_WANTSND) {
1768 *statep &= ~NFSSTA_WANTSND;
1771 mtx_unlock(&rep->r_nmp->nm_mtx);
1777 * Check for badly aligned mbuf data and realign by copying the unaligned
1778 * portion of the data into a new mbuf chain and freeing the portions
1779 * of the old chain that were replaced.
1781 * We cannot simply realign the data within the existing mbuf chain
1782 * because the underlying buffers may contain other rpc commands and
1783 * we cannot afford to overwrite them.
1785 * We would prefer to avoid this situation entirely. The situation does
1786 * not occur with NFS/UDP and is supposed to only occassionally occur
1787 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1791 nfs_realign(struct mbuf **pm, int hsiz)
1794 struct mbuf *n = NULL;
1798 while ((m = *pm) != NULL) {
1799 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1800 MGET(n, M_DONTWAIT, MT_DATA);
1803 if (m->m_len >= MINCLSIZE) {
1804 MCLGET(n, M_DONTWAIT);
1805 if (n->m_ext.ext_buf == NULL) {
1816 * If n is non-NULL, loop on m copying data, then replace the
1817 * portion of the chain that had to be realigned.
1820 ++nfs_realign_count;
1822 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1834 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1838 GIANT_REQUIRED; /* tprintf */
1840 p = td ? td->td_proc : NULL;
1842 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1845 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1851 nfs_down(rep, nmp, td, msg, error, flags)
1853 struct nfsmount *nmp;
1859 GIANT_REQUIRED; /* nfs_msg */
1863 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1864 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1866 nmp->nm_state |= NFSSTA_TIMEO;
1868 #ifdef NFSSTA_LOCKTIMEO
1869 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1870 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1872 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1875 mtx_lock(&rep->r_mtx);
1877 rep->r_flags |= R_TPRINTFMSG;
1878 mtx_unlock(&rep->r_mtx);
1879 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1883 nfs_up(rep, nmp, td, msg, flags)
1885 struct nfsmount *nmp;
1891 GIANT_REQUIRED; /* nfs_msg */
1895 mtx_lock(&rep->r_mtx);
1896 if ((rep == NULL) || (rep->r_flags & R_TPRINTFMSG) != 0)
1897 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1898 mtx_unlock(&rep->r_mtx);
1899 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1900 nmp->nm_state &= ~NFSSTA_TIMEO;
1901 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1904 #ifdef NFSSTA_LOCKTIMEO
1905 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1906 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1907 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,