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;
81 extern struct mtx nfs_xid_mtx;
83 static int nfs_realign_test;
84 static int nfs_realign_count;
85 static int nfs_bufpackets = 4;
86 static int nfs_reconnects;
87 static int nfs3_jukebox_delay = 10;
88 static int nfs_skip_wcc_data_onerr = 1;
90 SYSCTL_DECL(_vfs_nfs);
92 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
93 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
94 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
95 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
96 "number of times the nfs client has had to reconnect");
97 SYSCTL_INT(_vfs_nfs, OID_AUTO, nfs3_jukebox_delay, CTLFLAG_RW, &nfs3_jukebox_delay, 0,
98 "number of seconds to delay a retry after receiving EJUKEBOX");
99 SYSCTL_INT(_vfs_nfs, OID_AUTO, skip_wcc_data_onerr, CTLFLAG_RW, &nfs_skip_wcc_data_onerr, 0, "");
102 * There is a congestion window for outstanding rpcs maintained per mount
103 * point. The cwnd size is adjusted in roughly the way that:
104 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
105 * SIGCOMM '88". ACM, August 1988.
106 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
107 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
108 * of rpcs is in progress.
109 * (The sent count and cwnd are scaled for integer arith.)
110 * Variants of "slow start" were tried and were found to be too much of a
111 * performance hit (ave. rtt 3 times larger),
112 * I suspect due to the large rtt that nfs rpcs have.
114 #define NFS_CWNDSCALE 256
115 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
116 #define NFS_NBACKOFF 8
117 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
118 struct callout nfs_callout;
120 static int nfs_msg(struct thread *, const char *, const char *, int);
121 static int nfs_realign(struct mbuf **pm, int hsiz);
122 static int nfs_reply(struct nfsreq *);
123 static void nfs_softterm(struct nfsreq *rep);
124 static int nfs_reconnect(struct nfsreq *rep);
125 static void nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag);
126 static void nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag);
128 extern struct mtx nfs_reqq_mtx;
134 static enum nfs_rto_timer_t nfs_proct[NFS_NPROCS] = {
135 NFS_DEFAULT_TIMER, /* NULL */
136 NFS_GETATTR_TIMER, /* GETATTR */
137 NFS_DEFAULT_TIMER, /* SETATTR */
138 NFS_LOOKUP_TIMER, /* LOOKUP */
139 NFS_GETATTR_TIMER, /* ACCESS */
140 NFS_READ_TIMER, /* READLINK */
141 NFS_READ_TIMER, /* READ */
142 NFS_WRITE_TIMER, /* WRITE */
143 NFS_DEFAULT_TIMER, /* CREATE */
144 NFS_DEFAULT_TIMER, /* MKDIR */
145 NFS_DEFAULT_TIMER, /* SYMLINK */
146 NFS_DEFAULT_TIMER, /* MKNOD */
147 NFS_DEFAULT_TIMER, /* REMOVE */
148 NFS_DEFAULT_TIMER, /* RMDIR */
149 NFS_DEFAULT_TIMER, /* RENAME */
150 NFS_DEFAULT_TIMER, /* LINK */
151 NFS_READ_TIMER, /* READDIR */
152 NFS_READ_TIMER, /* READDIRPLUS */
153 NFS_DEFAULT_TIMER, /* FSSTAT */
154 NFS_DEFAULT_TIMER, /* FSINFO */
155 NFS_DEFAULT_TIMER, /* PATHCONF */
156 NFS_DEFAULT_TIMER, /* COMMIT */
157 NFS_DEFAULT_TIMER, /* NOOP */
161 * Choose the correct RTT timer for this NFS procedure.
163 static inline enum nfs_rto_timer_t
164 nfs_rto_timer(u_int32_t procnum)
166 return nfs_proct[procnum];
170 * Initialize the RTT estimator state for a new mount point.
173 nfs_init_rtt(struct nfsmount *nmp)
177 for (i = 0; i < NFS_MAX_TIMER; i++)
178 nmp->nm_srtt[i] = NFS_INITRTT;
179 for (i = 0; i < NFS_MAX_TIMER; i++)
180 nmp->nm_sdrtt[i] = 0;
184 * Update a mount point's RTT estimator state using data from the
187 * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
189 * NB: Since the timer resolution of NFS_HZ is so course, it can often
190 * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
191 * between N + dt and N + 2 - dt ticks, add 1 before calculating the
195 nfs_update_rtt(struct nfsreq *rep)
197 int t1 = rep->r_rtt + 1;
198 int index = nfs_rto_timer(rep->r_procnum) - 1;
199 int *srtt = &rep->r_nmp->nm_srtt[index];
200 int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
211 * Estimate RTO for an NFS RPC sent via an unreliable datagram.
213 * Use the mean and mean deviation of RTT for the appropriate type
214 * of RPC for the frequent RPCs and a default for the others.
215 * The justification for doing "other" this way is that these RPCs
216 * happen so infrequently that timer est. would probably be stale.
217 * Also, since many of these RPCs are non-idempotent, a conservative
218 * timeout is desired.
220 * getattr, lookup - A+2D
225 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
227 enum nfs_rto_timer_t timer = nfs_rto_timer(procnum);
228 int index = timer - 1;
232 case NFS_GETATTR_TIMER:
233 case NFS_LOOKUP_TIMER:
234 rto = ((nmp->nm_srtt[index] + 3) >> 2) +
235 ((nmp->nm_sdrtt[index] + 1) >> 1);
238 case NFS_WRITE_TIMER:
239 rto = ((nmp->nm_srtt[index] + 7) >> 3) +
240 (nmp->nm_sdrtt[index] + 1);
247 if (rto < NFS_MINRTO)
249 else if (rto > NFS_MAXRTO)
257 * Initialize sockets and congestion for a new NFS connection.
258 * We do not free the sockaddr if error.
261 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
264 int error, rcvreserve, sndreserve;
266 struct sockaddr *saddr;
267 struct thread *td = &thread0; /* only used for socreate and sobind */
269 if (nmp->nm_sotype == SOCK_STREAM) {
270 mtx_lock(&nmp->nm_mtx);
271 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
272 nmp->nm_nfstcpstate.rpcresid = 0;
273 mtx_unlock(&nmp->nm_mtx);
277 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
278 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
282 nmp->nm_soflags = so->so_proto->pr_flags;
285 * Some servers require that the client port be a reserved port number.
287 if (nmp->nm_flag & NFSMNT_RESVPORT) {
290 struct sockaddr_in6 ssin;
293 bzero(&sopt, sizeof sopt);
294 switch(saddr->sa_family) {
296 sopt.sopt_level = IPPROTO_IP;
297 sopt.sopt_name = IP_PORTRANGE;
298 ip = IP_PORTRANGE_LOW;
299 ip2 = IP_PORTRANGE_DEFAULT;
300 len = sizeof (struct sockaddr_in);
304 sopt.sopt_level = IPPROTO_IPV6;
305 sopt.sopt_name = IPV6_PORTRANGE;
306 ip = IPV6_PORTRANGE_LOW;
307 ip2 = IPV6_PORTRANGE_DEFAULT;
308 len = sizeof (struct sockaddr_in6);
314 sa = (struct sockaddr *)&ssin;
317 sa->sa_family = saddr->sa_family;
318 sopt.sopt_dir = SOPT_SET;
319 sopt.sopt_val = (void *)&ip;
320 sopt.sopt_valsize = sizeof(ip);
321 error = sosetopt(so, &sopt);
324 error = sobind(so, sa, td);
328 error = sosetopt(so, &sopt);
335 * Protocols that do not require connections may be optionally left
336 * unconnected for servers that reply from a port other than NFS_PORT.
338 mtx_lock(&nmp->nm_mtx);
339 if (nmp->nm_flag & NFSMNT_NOCONN) {
340 if (nmp->nm_soflags & PR_CONNREQUIRED) {
342 mtx_unlock(&nmp->nm_mtx);
345 mtx_unlock(&nmp->nm_mtx);
347 mtx_unlock(&nmp->nm_mtx);
348 error = soconnect(so, nmp->nm_nam, td);
353 * Wait for the connection to complete. Cribbed from the
354 * connect system call but with the wait timing out so
355 * that interruptible mounts don't hang here for a long time.
358 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
359 (void) msleep(&so->so_timeo, SOCK_MTX(so),
360 PSOCK, "nfscon", 2 * hz);
361 if ((so->so_state & SS_ISCONNECTING) &&
362 so->so_error == 0 && rep &&
363 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
364 so->so_state &= ~SS_ISCONNECTING;
370 error = so->so_error;
377 so->so_rcv.sb_timeo = 12 * hz;
378 if (nmp->nm_sotype == SOCK_STREAM)
379 so->so_snd.sb_timeo = 1 * hz; /* 1s snd timeout for NFS/TCP */
381 so->so_snd.sb_timeo = 5 * hz;
384 * Get buffer reservation size from sysctl, but impose reasonable
387 pktscale = nfs_bufpackets;
392 mtx_lock(&nmp->nm_mtx);
393 if (nmp->nm_sotype == SOCK_DGRAM) {
394 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
395 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
396 NFS_MAXPKTHDR) * pktscale;
397 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
398 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
399 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
400 NFS_MAXPKTHDR) * pktscale;
402 if (nmp->nm_sotype != SOCK_STREAM)
403 panic("nfscon sotype");
404 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
408 bzero(&sopt, sizeof sopt);
409 sopt.sopt_dir = SOPT_SET;
410 sopt.sopt_level = SOL_SOCKET;
411 sopt.sopt_name = SO_KEEPALIVE;
412 sopt.sopt_val = &val;
413 sopt.sopt_valsize = sizeof val;
415 mtx_unlock(&nmp->nm_mtx);
417 mtx_lock(&nmp->nm_mtx);
419 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
423 bzero(&sopt, sizeof sopt);
424 sopt.sopt_dir = SOPT_SET;
425 sopt.sopt_level = IPPROTO_TCP;
426 sopt.sopt_name = TCP_NODELAY;
427 sopt.sopt_val = &val;
428 sopt.sopt_valsize = sizeof val;
430 mtx_unlock(&nmp->nm_mtx);
432 mtx_lock(&nmp->nm_mtx);
434 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
435 sizeof (u_int32_t)) * pktscale;
436 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
437 sizeof (u_int32_t)) * pktscale;
439 mtx_unlock(&nmp->nm_mtx);
440 error = soreserve(so, sndreserve, rcvreserve);
443 SOCKBUF_LOCK(&so->so_rcv);
444 so->so_rcv.sb_flags |= SB_NOINTR;
445 so->so_upcallarg = (caddr_t)nmp;
446 if (so->so_type == SOCK_STREAM)
447 so->so_upcall = nfs_clnt_tcp_soupcall;
449 so->so_upcall = nfs_clnt_udp_soupcall;
450 so->so_rcv.sb_flags |= SB_UPCALL;
451 SOCKBUF_UNLOCK(&so->so_rcv);
452 SOCKBUF_LOCK(&so->so_snd);
453 so->so_snd.sb_flags |= SB_NOINTR;
454 SOCKBUF_UNLOCK(&so->so_snd);
456 mtx_lock(&nmp->nm_mtx);
457 /* Initialize other non-zero congestion variables */
459 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
461 nmp->nm_timeouts = 0;
462 mtx_unlock(&nmp->nm_mtx);
471 nfs_wakup_reconnectors(struct nfsmount *nmp)
473 KASSERT(mtx_owned(&nmp->nm_mtx), ("NFS mnt lock not owned !"));
474 if (--nmp->nm_nfstcpstate.sock_send_inprog == 0 &&
475 (nmp->nm_nfstcpstate.flags & NFS_TCP_WAIT_WRITE_DRAIN)) {
476 nmp->nm_nfstcpstate.flags &= ~NFS_TCP_WAIT_WRITE_DRAIN;
477 wakeup((caddr_t)&nmp->nm_nfstcpstate.sock_send_inprog);
483 * Called when a connection is broken on a reliable protocol.
484 * - clean up the old socket
485 * - nfs_connect() again
486 * - set R_MUSTRESEND for all outstanding requests on mount point
487 * If this fails the mount point is DEAD!
488 * nb: Must be called with the nfs_sndlock() set on the mount point.
491 nfs_reconnect(struct nfsreq *rep)
494 struct nfsmount *nmp = rep->r_nmp;
498 KASSERT(mtx_owned(&nmp->nm_mtx), ("NFS mnt lock not owned !"));
499 if (nmp->nm_flag & NFSMNT_INT)
502 * Wait for any pending writes to this socket to drain (or timeout).
504 while (nmp->nm_nfstcpstate.sock_send_inprog > 0) {
505 nmp->nm_nfstcpstate.flags |= NFS_TCP_WAIT_WRITE_DRAIN;
506 error = msleep((caddr_t)&nmp->nm_nfstcpstate.sock_send_inprog,
507 &nmp->nm_mtx, slpflag | (PZERO - 1), "nfscon", 0);
510 * Grab the nfs_connect_lock to serialize connects.
511 * After grabbing the nfs_connect_lock, check if a reconnect is necessary or
512 * if someone else beat us to the connect !
514 error = nfs_connect_lock(rep);
517 if ((nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) == 0)
520 mtx_unlock(&nmp->nm_mtx);
524 while ((error = nfs_connect(nmp, rep)) != 0) {
525 if (error == ERESTART)
527 if (error == EIO || error == EINTR) {
528 mtx_lock(&nmp->nm_mtx);
531 (void) tsleep(&lbolt, PSOCK, "nfscon", 0);
535 * Clear the FORCE_RECONNECT flag only after the connect
536 * succeeds. To prevent races between multiple processes
537 * waiting on the mountpoint where the connection is being
538 * torn down. The first one to acquire the sndlock will
539 * retry the connection. The others block on the sndlock
540 * until the connection is established successfully, and
541 * then re-transmit the request.
543 mtx_lock(&nmp->nm_mtx);
544 nmp->nm_nfstcpstate.flags &= ~NFS_TCP_FORCE_RECONNECT;
545 nmp->nm_nfstcpstate.rpcresid = 0;
546 mtx_unlock(&nmp->nm_mtx);
549 * Loop through outstanding request list and fix up all requests
552 mtx_lock(&nfs_reqq_mtx);
553 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
554 if (rp->r_nmp == nmp) {
555 mtx_lock(&rp->r_mtx);
556 rp->r_flags |= R_MUSTRESEND;
557 mtx_unlock(&rp->r_mtx);
560 mtx_unlock(&nfs_reqq_mtx);
561 mtx_lock(&nmp->nm_mtx);
563 nfs_connect_unlock(rep);
564 mtx_unlock(&nmp->nm_mtx);
569 * NFS disconnect. Clean up and unlink.
572 nfs_disconnect(struct nfsmount *nmp)
576 mtx_lock(&nmp->nm_mtx);
580 mtx_unlock(&nmp->nm_mtx);
581 SOCKBUF_LOCK(&so->so_rcv);
582 so->so_upcallarg = NULL;
583 so->so_upcall = NULL;
584 so->so_rcv.sb_flags &= ~SB_UPCALL;
585 SOCKBUF_UNLOCK(&so->so_rcv);
586 soshutdown(so, SHUT_WR);
589 mtx_unlock(&nmp->nm_mtx);
593 nfs_safedisconnect(struct nfsmount *nmp)
595 struct nfsreq dummyreq;
597 bzero(&dummyreq, sizeof(dummyreq));
598 dummyreq.r_nmp = nmp;
603 * This is the nfs send routine. For connection based socket types, it
604 * must be called with an nfs_sndlock() on the socket.
605 * - return EINTR if the RPC is terminated, 0 otherwise
606 * - set R_MUSTRESEND if the send fails for any reason
607 * - do any cleanup required by recoverable socket errors (?)
610 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
613 struct sockaddr *sendnam;
614 int error, error2, soflags, flags;
616 KASSERT(rep, ("nfs_send: called with rep == NULL"));
618 error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
623 mtx_lock(&rep->r_nmp->nm_mtx);
624 mtx_lock(&rep->r_mtx);
625 if ((so = rep->r_nmp->nm_so) == NULL) {
626 rep->r_flags |= R_MUSTRESEND;
627 mtx_unlock(&rep->r_mtx);
628 mtx_unlock(&rep->r_nmp->nm_mtx);
632 rep->r_flags &= ~R_MUSTRESEND;
633 soflags = rep->r_nmp->nm_soflags;
634 mtx_unlock(&rep->r_mtx);
635 mtx_unlock(&rep->r_nmp->nm_mtx);
637 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
641 if (so->so_type == SOCK_SEQPACKET)
646 error = sosend(so, sendnam, 0, top, 0, flags, curthread /*XXX*/);
647 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
649 mtx_lock(&rep->r_mtx);
650 rep->r_flags |= R_MUSTRESEND;
651 mtx_unlock(&rep->r_mtx);
656 * Don't report EPIPE errors on nfs sockets.
657 * These can be due to idle tcp mounts which will be closed by
658 * netapp, solaris, etc. if left idle too long.
660 if (error != EPIPE) {
661 log(LOG_INFO, "nfs send error %d for server %s\n",
663 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
666 * Deal with errors for the client side.
668 error2 = NFS_SIGREP(rep);
672 mtx_lock(&rep->r_mtx);
673 rep->r_flags |= R_MUSTRESEND;
674 mtx_unlock(&rep->r_mtx);
678 * Handle any recoverable (soft) socket errors here. (?)
679 * Make EWOULDBLOCK a recoverable error, we'll rexmit from nfs_timer().
681 if (error != EINTR && error != ERESTART && error != EIO && error != EPIPE)
688 nfs_reply(struct nfsreq *rep)
690 register struct socket *so;
691 register struct mbuf *m;
692 int error = 0, sotype, slpflag;
693 struct nfsmount *nmp = rep->r_nmp;
695 sotype = nmp->nm_sotype;
697 * For reliable protocols, lock against other senders/receivers
698 * in case a reconnect is necessary.
700 if (sotype != SOCK_DGRAM) {
702 mtx_lock(&nmp->nm_mtx);
703 mtx_lock(&rep->r_mtx);
705 mtx_unlock(&rep->r_mtx);
706 mtx_unlock(&nmp->nm_mtx);
709 if (rep->r_flags & R_SOFTTERM) {
710 mtx_unlock(&rep->r_mtx);
711 mtx_unlock(&nmp->nm_mtx);
716 (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) {
717 mtx_unlock(&rep->r_mtx);
718 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
719 error = nfs_reconnect(rep);
724 while (rep->r_flags & R_MUSTRESEND) {
725 mtx_unlock(&rep->r_mtx);
726 nmp->nm_nfstcpstate.sock_send_inprog++;
727 mtx_unlock(&nmp->nm_mtx);
728 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
729 nfsstats.rpcretries++;
730 error = nfs_send(so, nmp->nm_nam, m, rep);
732 mtx_lock(&nmp->nm_mtx);
733 nfs_wakup_reconnectors(nmp);
734 if (!(error == EINTR || error == ERESTART)) {
735 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
736 error = nfs_reconnect(rep);
738 mtx_unlock(&nmp->nm_mtx);
743 mtx_lock(&nmp->nm_mtx);
744 nfs_wakup_reconnectors(nmp);
745 mtx_lock(&rep->r_mtx);
748 mtx_unlock(&rep->r_mtx);
749 mtx_unlock(&nmp->nm_mtx);
752 mtx_lock(&nmp->nm_mtx);
753 if (nmp->nm_flag & NFSMNT_INT)
755 mtx_unlock(&nmp->nm_mtx);
756 mtx_lock(&rep->r_mtx);
757 while ((rep->r_mrep == NULL) && (error == 0) &&
758 ((rep->r_flags & R_SOFTTERM) == 0) &&
759 ((sotype == SOCK_DGRAM) || ((rep->r_flags & R_MUSTRESEND) == 0)))
760 error = msleep((caddr_t)rep, &rep->r_mtx,
761 slpflag | (PZERO - 1), "nfsreq", 0);
762 if (error == EINTR || error == ERESTART) {
763 /* NFS operations aren't restartable. Map ERESTART to EINTR */
764 mtx_unlock(&rep->r_mtx);
767 if (rep->r_flags & R_SOFTTERM) {
768 /* Request was terminated because we exceeded the retries (soft mount) */
769 mtx_unlock(&rep->r_mtx);
772 mtx_unlock(&rep->r_mtx);
773 if (sotype == SOCK_STREAM) {
774 mtx_lock(&nmp->nm_mtx);
775 mtx_lock(&rep->r_mtx);
776 if (((nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) ||
777 (rep->r_flags & R_MUSTRESEND))) {
778 mtx_unlock(&rep->r_mtx);
779 mtx_unlock(&nmp->nm_mtx);
782 mtx_unlock(&rep->r_mtx);
783 mtx_unlock(&nmp->nm_mtx);
791 * Make nfs_realign() non-blocking. Also make nfsm_dissect() nonblocking.
794 nfs_clnt_match_xid(struct socket *so,
795 struct nfsmount *nmp,
805 * Search for any mbufs that are not a multiple of 4 bytes long
806 * or with m_data not longword aligned.
807 * These could cause pointer alignment problems, so copy them to
808 * well aligned mbufs.
810 if (nfs_realign(&mrep, 5 * NFSX_UNSIGNED) == ENOMEM) {
812 nfsstats.rpcinvalid++;
817 * Get the xid and check that it is an rpc reply
820 dpos = mtod(md, caddr_t);
821 tl = nfsm_dissect_nonblock(u_int32_t *, 2*NFSX_UNSIGNED);
823 if (*tl != rpc_reply) {
826 nfsstats.rpcinvalid++;
830 mtx_lock(&nfs_reqq_mtx);
832 * Loop through the request list to match up the reply
833 * Iff no match, just drop the datagram
835 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
836 mtx_lock(&nmp->nm_mtx);
837 mtx_lock(&rep->r_mtx);
838 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
844 * Update congestion window.
845 * Do the additive increase of
848 if (nmp->nm_cwnd <= nmp->nm_sent) {
850 (NFS_CWNDSCALE * NFS_CWNDSCALE +
851 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
852 if (nmp->nm_cwnd > NFS_MAXCWND)
853 nmp->nm_cwnd = NFS_MAXCWND;
855 if (rep->r_flags & R_SENT) {
856 rep->r_flags &= ~R_SENT;
857 nmp->nm_sent -= NFS_CWNDSCALE;
859 if (rep->r_flags & R_TIMING)
861 nmp->nm_timeouts = 0;
862 wakeup((caddr_t)rep);
863 mtx_unlock(&rep->r_mtx);
864 mtx_unlock(&nmp->nm_mtx);
867 mtx_unlock(&rep->r_mtx);
868 mtx_unlock(&nmp->nm_mtx);
871 * If not matched to a request, drop it.
872 * If it's mine, wake up requestor.
875 nfsstats.rpcunexpected++;
878 mtx_unlock(&nfs_reqq_mtx);
882 nfs_mark_for_reconnect(struct nfsmount *nmp)
886 mtx_lock(&nmp->nm_mtx);
887 nmp->nm_nfstcpstate.flags |= NFS_TCP_FORCE_RECONNECT;
888 mtx_unlock(&nmp->nm_mtx);
890 * Wakeup all processes that are waiting for replies
891 * on this mount point. One of them does the reconnect.
893 mtx_lock(&nfs_reqq_mtx);
894 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
895 if (rp->r_nmp == nmp) {
896 mtx_lock(&rp->r_mtx);
897 rp->r_flags |= R_MUSTRESEND;
899 mtx_unlock(&rp->r_mtx);
902 mtx_unlock(&nfs_reqq_mtx);
906 nfstcp_readable(struct socket *so, int bytes)
910 SOCKBUF_LOCK(&so->so_rcv);
911 retval = (so->so_rcv.sb_cc >= (bytes) ||
912 (so->so_rcv.sb_state & SBS_CANTRCVMORE) ||
914 SOCKBUF_UNLOCK(&so->so_rcv);
918 #define nfstcp_marker_readable(so) nfstcp_readable(so, sizeof(u_int32_t))
921 nfs_copy_len(struct mbuf *mp, char *buf, int len)
923 while (len > 0 && mp != NULL) {
924 int copylen = min(len, mp->m_len);
926 bcopy(mp->m_data, buf, copylen);
935 nfs_clnt_tcp_soupcall(struct socket *so, void *arg, int waitflag)
937 struct nfsmount *nmp = (struct nfsmount *)arg;
938 struct mbuf *mp = NULL;
945 * Don't pick any more data from the socket if we've marked the
946 * mountpoint for reconnect.
948 mtx_lock(&nmp->nm_mtx);
949 if (nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT) {
950 mtx_unlock(&nmp->nm_mtx);
953 mtx_unlock(&nmp->nm_mtx);
954 auio.uio_td = curthread;
955 auio.uio_segflg = UIO_SYSSPACE;
956 auio.uio_rw = UIO_READ;
958 mtx_lock(&nmp->nm_mtx);
959 if (nmp->nm_nfstcpstate.flags & NFS_TCP_EXPECT_RPCMARKER) {
962 mtx_unlock(&nmp->nm_mtx);
963 if (!nfstcp_marker_readable(so)) {
964 /* Marker is not readable */
967 auio.uio_resid = sizeof(u_int32_t);
971 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
972 error = soreceive(so, (struct sockaddr **)0, &auio,
973 &mp, (struct mbuf **)0, &rcvflg);
975 * We've already tested that the socket is readable. 2 cases
976 * here, we either read 0 bytes (client closed connection),
977 * or got some other error. In both cases, we tear down the
980 if (error || auio.uio_resid > 0) {
981 if (error && error != ECONNRESET) {
983 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
989 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
991 * Sigh. We can't do the obvious thing here (which would
992 * be to have soreceive copy the length from mbufs for us).
993 * Calling uiomove() from the context of a socket callback
994 * (even for kernel-kernel copies) leads to LORs (since
995 * we hold network locks at this point).
997 if ((resid = nfs_copy_len(mp, (char *)&len,
998 sizeof(u_int32_t)))) {
999 log(LOG_ERR, "%s (%d) from nfs server %s\n",
1000 "Bad RPC HDR length",
1001 (int)(sizeof(u_int32_t) - resid),
1002 nmp->nm_mountp->mnt_stat.f_mntfromname);
1003 goto mark_reconnect;
1005 len = ntohl(len) & ~0x80000000;
1008 * This is SERIOUS! We are out of sync with the sender
1009 * and forcing a disconnect/reconnect is all I can do.
1011 if (len > NFS_MAXPACKET || len == 0) {
1012 log(LOG_ERR, "%s (%d) from nfs server %s\n",
1013 "impossible packet length",
1015 nmp->nm_mountp->mnt_stat.f_mntfromname);
1016 goto mark_reconnect;
1018 mtx_lock(&nmp->nm_mtx);
1019 nmp->nm_nfstcpstate.rpcresid = len;
1020 nmp->nm_nfstcpstate.flags &= ~(NFS_TCP_EXPECT_RPCMARKER);
1021 mtx_unlock(&nmp->nm_mtx);
1023 mtx_unlock(&nmp->nm_mtx);
1026 * Processed RPC marker or no RPC marker to process.
1027 * Pull in and process data.
1029 mtx_lock(&nmp->nm_mtx);
1030 if (nmp->nm_nfstcpstate.rpcresid > 0) {
1031 mtx_unlock(&nmp->nm_mtx);
1032 if (!nfstcp_readable(so, nmp->nm_nfstcpstate.rpcresid)) {
1033 /* All data not readable */
1036 auio.uio_resid = nmp->nm_nfstcpstate.rpcresid;
1037 auio.uio_iov = NULL;
1038 auio.uio_iovcnt = 0;
1040 rcvflg = (MSG_DONTWAIT | MSG_SOCALLBCK);
1041 error = soreceive(so, (struct sockaddr **)0, &auio,
1042 &mp, (struct mbuf **)0, &rcvflg);
1043 if (error || auio.uio_resid > 0) {
1044 if (error && error != ECONNRESET) {
1046 "nfs/tcp clnt: Error %d reading socket, tearing down TCP connection\n",
1049 goto mark_reconnect;
1052 panic("nfs_clnt_tcp_soupcall: Got empty mbuf chain from sorecv\n");
1053 mtx_lock(&nmp->nm_mtx);
1054 nmp->nm_nfstcpstate.rpcresid = 0;
1055 nmp->nm_nfstcpstate.flags |= NFS_TCP_EXPECT_RPCMARKER;
1056 mtx_unlock(&nmp->nm_mtx);
1057 /* We got the entire RPC reply. Match XIDs and wake up requestor */
1058 nfs_clnt_match_xid(so, nmp, mp);
1060 mtx_unlock(&nmp->nm_mtx);
1064 nfs_mark_for_reconnect(nmp);
1068 nfs_clnt_udp_soupcall(struct socket *so, void *arg, int waitflag)
1070 struct nfsmount *nmp = (struct nfsmount *)arg;
1072 struct mbuf *mp = NULL;
1073 struct mbuf *control = NULL;
1076 auio.uio_resid = 1000000;
1077 auio.uio_td = curthread;
1078 rcvflag = MSG_DONTWAIT;
1079 auio.uio_resid = 1000000000;
1081 mp = control = NULL;
1082 error = soreceive(so, NULL, &auio, &mp, &control, &rcvflag);
1086 nfs_clnt_match_xid(so, nmp, mp);
1087 } while (mp && !error);
1091 * nfs_request - goes something like this
1092 * - fill in request struct
1093 * - links it into list
1094 * - calls nfs_send() for first transmit
1095 * - calls nfs_receive() to get reply
1096 * - break down rpc header and return with nfs reply pointed to
1098 * nb: always frees up mreq mbuf list
1101 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
1102 struct thread *td, struct ucred *cred, struct mbuf **mrp,
1103 struct mbuf **mdp, caddr_t *dposp)
1105 struct mbuf *mrep, *m2;
1109 struct nfsmount *nmp;
1110 struct mbuf *m, *md, *mheadend;
1113 int error = 0, mrest_len, auth_len, auth_type;
1117 /* Reject requests while attempting a forced unmount. */
1118 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1122 nmp = VFSTONFS(vp->v_mount);
1123 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1124 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
1125 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
1126 bzero(rep, sizeof(struct nfsreq));
1130 rep->r_procnum = procnum;
1131 mtx_init(&rep->r_mtx, "NFSrep lock", NULL, MTX_DEF);
1133 getmicrouptime(&now);
1134 rep->r_lastmsg = now.tv_sec -
1135 ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
1136 mrest_len = m_length(mrest, NULL);
1139 * Get the RPC header with authorization.
1141 auth_type = RPCAUTH_UNIX;
1142 if (cred->cr_ngroups < 1)
1143 panic("nfsreq nogrps");
1144 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1145 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1147 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1148 mrest, mrest_len, &mheadend, &xidp);
1151 * For stream protocols, insert a Sun RPC Record Mark.
1153 if (nmp->nm_sotype == SOCK_STREAM) {
1154 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
1155 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1156 (m->m_pkthdr.len - NFSX_UNSIGNED));
1161 if (nmp->nm_flag & NFSMNT_SOFT)
1162 rep->r_retry = nmp->nm_retry;
1164 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1165 rep->r_rtt = rep->r_rexmit = 0;
1166 if (nfs_rto_timer(procnum) != NFS_DEFAULT_TIMER)
1167 rep->r_flags = R_TIMING;
1173 * Do the client side RPC.
1175 nfsstats.rpcrequests++;
1177 * Chain request into list of outstanding requests. Be sure
1178 * to put it LAST so timer finds oldest requests first.
1180 mtx_lock(&nfs_reqq_mtx);
1181 if (TAILQ_EMPTY(&nfs_reqq))
1182 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1183 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1184 mtx_unlock(&nfs_reqq_mtx);
1187 * If backing off another request or avoiding congestion, don't
1188 * send this one now but let timer do it. If not timing a request,
1191 mtx_lock(&nmp->nm_mtx);
1193 (((nmp->nm_sotype == SOCK_STREAM) && !(nmp->nm_nfstcpstate.flags & NFS_TCP_FORCE_RECONNECT)) ||
1194 (nmp->nm_flag & NFSMNT_DUMBTIMR) || nmp->nm_sent < nmp->nm_cwnd)) {
1195 if (nmp->nm_sotype == SOCK_STREAM)
1196 nmp->nm_nfstcpstate.sock_send_inprog++;
1197 mtx_unlock(&nmp->nm_mtx);
1198 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1199 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1200 mtx_lock(&nmp->nm_mtx);
1201 mtx_lock(&rep->r_mtx);
1203 * nfs_timer() could've re-transmitted the request if we ended up
1204 * blocking on nfs_send() too long, so check for R_SENT here.
1206 if (!error && (rep->r_flags & (R_SENT | R_MUSTRESEND)) == 0) {
1207 nmp->nm_sent += NFS_CWNDSCALE;
1208 rep->r_flags |= R_SENT;
1210 mtx_unlock(&rep->r_mtx);
1211 if (nmp->nm_sotype == SOCK_STREAM)
1212 nfs_wakup_reconnectors(rep->r_nmp);
1213 mtx_unlock(&nmp->nm_mtx);
1215 mtx_unlock(&nmp->nm_mtx);
1220 * Wait for the reply from our send or the timer's.
1222 if (!error || error == EPIPE)
1223 error = nfs_reply(rep);
1226 * nfs_timer() may be in the process of re-transmitting this request.
1227 * nfs_timer() drops the nfs_reqq_mtx before the pru_send() (to avoid LORs).
1228 * Wait till nfs_timer() completes the re-transmission. When the reply
1229 * comes back, it will be discarded (since the req struct for it no longer
1232 wait_for_pinned_req:
1233 mtx_lock(&rep->r_mtx);
1234 while (rep->r_flags & R_PIN_REQ) {
1235 msleep((caddr_t)&rep->r_flags, &rep->r_mtx,
1236 (PZERO - 1), "nfsrxmt", 0);
1238 mtx_unlock(&rep->r_mtx);
1240 mtx_lock(&nfs_reqq_mtx);
1241 /* Have to check for R_PIN_REQ after grabbing wlock again */
1242 mtx_lock(&rep->r_mtx);
1243 if (rep->r_flags & R_PIN_REQ) {
1244 mtx_unlock(&rep->r_mtx);
1245 mtx_unlock(&nfs_reqq_mtx);
1246 goto wait_for_pinned_req;
1248 mtx_unlock(&rep->r_mtx);
1249 /* RPC done (timer not active, request not pinned), unlink the request */
1250 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1251 if (TAILQ_EMPTY(&nfs_reqq))
1252 callout_stop(&nfs_callout);
1253 mtx_unlock(&nfs_reqq_mtx);
1256 * Decrement the outstanding request count.
1258 mtx_lock(&rep->r_mtx);
1259 if (rep->r_flags & R_SENT) {
1260 rep->r_flags &= ~R_SENT; /* paranoia */
1261 mtx_unlock(&rep->r_mtx);
1262 mtx_lock(&nmp->nm_mtx);
1263 nmp->nm_sent -= NFS_CWNDSCALE;
1264 mtx_unlock(&nmp->nm_mtx);
1266 mtx_unlock(&rep->r_mtx);
1269 * If there was a successful reply and a tprintf msg.
1270 * tprintf a response.
1273 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1280 * If we got interrupted by a signal in nfs_reply(), there's
1281 * a very small window where the reply could've come in before
1282 * this process got scheduled in. To handle that case, we need
1283 * to free the reply if it was delivered.
1285 if (rep->r_mrep != NULL)
1286 m_freem(rep->r_mrep);
1287 m_freem(rep->r_mreq);
1288 mtx_destroy(&rep->r_mtx);
1289 free((caddr_t)rep, M_NFSREQ);
1293 if (rep->r_mrep == NULL)
1294 panic("nfs_request: rep->r_mrep shouldn't be NULL if no error\n");
1297 * break down the rpc header and check if ok
1299 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1300 if (*tl++ == rpc_msgdenied) {
1301 if (*tl == rpc_mismatch)
1306 m_freem(rep->r_mreq);
1307 mtx_destroy(&rep->r_mtx);
1308 free((caddr_t)rep, M_NFSREQ);
1313 * Just throw away any verifyer (ie: kerberos etc).
1315 i = fxdr_unsigned(int, *tl++); /* verf type */
1316 i = fxdr_unsigned(int32_t, *tl); /* len */
1318 nfsm_adv(nfsm_rndup(i));
1319 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1322 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1324 error = fxdr_unsigned(int, *tl);
1325 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1326 error == NFSERR_TRYLATER) {
1329 waituntil = time_second + nfs3_jukebox_delay;
1330 while (time_second < waituntil) {
1331 (void) tsleep(&lbolt, PSOCK, "nqnfstry", 0);
1333 mtx_lock(&nfs_xid_mtx);
1336 rep->r_xid = *xidp = txdr_unsigned(nfs_xid);
1337 mtx_unlock(&nfs_xid_mtx);
1342 * If the File Handle was stale, invalidate the
1343 * lookup cache, just in case.
1345 if (error == ESTALE)
1348 * Skip wcc data on NFS errors for now. NetApp filers return corrupt
1349 * postop attrs in the wcc data for NFS err EROFS. Not sure if they
1350 * could return corrupt postop attrs for others errors.
1352 if ((nmp->nm_flag & NFSMNT_NFSV3) && !nfs_skip_wcc_data_onerr) {
1356 error |= NFSERR_RETERR;
1359 m_freem(rep->r_mreq);
1360 mtx_destroy(&rep->r_mtx);
1361 free((caddr_t)rep, M_NFSREQ);
1368 m_freem(rep->r_mreq);
1369 mtx_destroy(&rep->r_mtx);
1370 FREE((caddr_t)rep, M_NFSREQ);
1374 error = EPROTONOSUPPORT;
1376 m_freem(rep->r_mreq);
1377 mtx_destroy(&rep->r_mtx);
1378 free((caddr_t)rep, M_NFSREQ);
1384 * Scan the nfsreq list and retranmit any requests that have timed out
1385 * To avoid retransmission attempts on STREAM sockets (in the future) make
1386 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1388 * The nfs reqq lock cannot be held while we do the pru_send() because of a
1389 * lock ordering violation. The NFS client socket callback acquires
1390 * inp_lock->nfsreq mutex and pru_send acquires inp_lock. So we drop the
1391 * reqq mutex (and reacquire it after the pru_send()). The req structure
1392 * (for the rexmit) is prevented from being removed by the R_PIN_REQ flag.
1395 nfs_timer(void *arg)
1400 struct nfsmount *nmp;
1405 getmicrouptime(&now);
1406 mtx_lock(&nfs_reqq_mtx);
1407 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1409 mtx_lock(&rep->r_mtx);
1410 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
1411 mtx_unlock(&rep->r_mtx);
1415 * Terminate request if force-unmount in progress.
1416 * Note that NFS could have vfs_busy'ed the mount,
1417 * causing the unmount to wait for the mnt_lock, making
1418 * this bit of logic necessary.
1420 if (rep->r_nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) {
1422 mtx_unlock(&rep->r_mtx);
1425 mtx_unlock(&rep->r_mtx);
1427 if (nfs_sigintr(nmp, rep, rep->r_td))
1429 mtx_lock(&nmp->nm_mtx);
1430 mtx_lock(&rep->r_mtx);
1431 if (nmp->nm_tprintf_initial_delay != 0 &&
1432 (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1433 rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1434 rep->r_lastmsg = now.tv_sec;
1436 * Pin down the request and drop locks for the acquisition
1437 * of Giant from tprintf() in nfs_down().
1439 rep->r_flags |= R_PIN_REQ;
1440 mtx_unlock(&rep->r_mtx);
1441 mtx_unlock(&nmp->nm_mtx);
1442 mtx_unlock(&nfs_reqq_mtx);
1443 nfs_down(rep, nmp, rep->r_td, "not responding",
1445 mtx_lock(&nfs_reqq_mtx);
1446 mtx_lock(&nmp->nm_mtx);
1447 mtx_lock(&rep->r_mtx);
1448 rep->r_flags &= ~R_PIN_REQ;
1449 wakeup((caddr_t)&rep->r_flags);
1451 if (rep->r_rtt >= 0) {
1453 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1454 timeo = nmp->nm_timeo;
1456 timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1457 if (nmp->nm_timeouts > 0)
1458 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1459 if (rep->r_rtt <= timeo) {
1460 mtx_unlock(&rep->r_mtx);
1461 mtx_unlock(&nmp->nm_mtx);
1464 if (nmp->nm_timeouts < NFS_NBACKOFF)
1467 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1468 nfsstats.rpctimeouts++;
1470 mtx_unlock(&rep->r_mtx);
1471 mtx_unlock(&nmp->nm_mtx);
1474 if (nmp->nm_sotype != SOCK_DGRAM) {
1475 if (++rep->r_rexmit > NFS_MAXREXMIT)
1476 rep->r_rexmit = NFS_MAXREXMIT;
1478 * For NFS/TCP, setting R_MUSTRESEND and waking up
1479 * the requester will cause the request to be
1480 * retransmitted (in nfs_reply()), re-connecting
1483 rep->r_flags |= R_MUSTRESEND;
1484 wakeup((caddr_t)rep);
1486 mtx_unlock(&rep->r_mtx);
1487 mtx_unlock(&nmp->nm_mtx);
1490 if ((so = nmp->nm_so) == NULL) {
1491 mtx_unlock(&rep->r_mtx);
1492 mtx_unlock(&nmp->nm_mtx);
1496 * If there is enough space and the window allows..
1498 * Set r_rtt to -1 in case we fail to send it now.
1501 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1502 ((nmp->nm_flag & NFSMNT_DUMBTIMR) || (rep->r_flags & R_SENT) ||
1503 nmp->nm_sent < nmp->nm_cwnd)) {
1504 mtx_unlock(&rep->r_mtx);
1505 mtx_unlock(&nmp->nm_mtx);
1506 if ((m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
1508 * Mark the request to indicate that a XMIT is in
1509 * progress to prevent the req structure being
1510 * removed in nfs_request().
1512 mtx_lock(&rep->r_mtx);
1513 rep->r_flags |= R_PIN_REQ;
1514 mtx_unlock(&rep->r_mtx);
1515 mtx_unlock(&nfs_reqq_mtx);
1516 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1517 error = (*so->so_proto->pr_usrreqs->pru_send)
1518 (so, 0, m, NULL, NULL, curthread);
1520 error = (*so->so_proto->pr_usrreqs->pru_send)
1521 (so, 0, m, nmp->nm_nam, NULL,
1523 mtx_lock(&nfs_reqq_mtx);
1524 mtx_lock(&nmp->nm_mtx);
1525 mtx_lock(&rep->r_mtx);
1526 rep->r_flags &= ~R_PIN_REQ;
1527 wakeup((caddr_t)&rep->r_flags);
1529 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1531 rep->r_flags |= R_RESENDERR;
1534 * Iff first send, start timing
1535 * else turn timing off, backoff timer
1536 * and divide congestion window by 2.
1538 rep->r_flags &= ~R_RESENDERR;
1539 if (rep->r_flags & R_SENT) {
1540 rep->r_flags &= ~R_TIMING;
1541 if (++rep->r_rexmit > NFS_MAXREXMIT)
1542 rep->r_rexmit = NFS_MAXREXMIT;
1544 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1545 nmp->nm_cwnd = NFS_CWNDSCALE;
1546 nfsstats.rpcretries++;
1548 rep->r_flags |= R_SENT;
1549 nmp->nm_sent += NFS_CWNDSCALE;
1553 mtx_unlock(&rep->r_mtx);
1554 mtx_unlock(&nmp->nm_mtx);
1557 mtx_unlock(&rep->r_mtx);
1558 mtx_unlock(&nmp->nm_mtx);
1561 mtx_unlock(&nfs_reqq_mtx);
1562 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1566 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1567 * wait for all requests to complete. This is used by forced unmounts
1568 * to terminate any outstanding RPCs.
1571 nfs_nmcancelreqs(nmp)
1572 struct nfsmount *nmp;
1577 mtx_lock(&nfs_reqq_mtx);
1578 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1579 mtx_lock(&req->r_mtx);
1580 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1581 (req->r_flags & R_SOFTTERM)) {
1582 mtx_unlock(&req->r_mtx);
1586 mtx_unlock(&req->r_mtx);
1588 mtx_unlock(&nfs_reqq_mtx);
1590 for (i = 0; i < 30; i++) {
1591 mtx_lock(&nfs_reqq_mtx);
1592 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1593 if (nmp == req->r_nmp)
1596 mtx_unlock(&nfs_reqq_mtx);
1599 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1605 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1606 * The nm_send count is decremented now to avoid deadlocks when the process in
1607 * soreceive() hasn't yet managed to send its own request.
1611 nfs_softterm(struct nfsreq *rep)
1613 KASSERT(mtx_owned(&rep->r_mtx), ("NFS req lock not owned !"));
1614 rep->r_flags |= R_SOFTTERM;
1615 if (rep->r_flags & R_SENT) {
1616 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1617 rep->r_flags &= ~R_SENT;
1620 * Request terminated, wakeup the blocked process, so that we
1621 * can return EINTR back.
1623 wakeup((caddr_t)rep);
1627 * Any signal that can interrupt an NFS operation in an intr mount
1628 * should be added to this set. SIGSTOP and SIGKILL cannot be masked.
1630 int nfs_sig_set[] = {
1640 * Check to see if one of the signals in our subset is pending on
1641 * the process (in an intr mount).
1644 nfs_sig_pending(sigset_t set)
1648 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++)
1649 if (SIGISMEMBER(set, nfs_sig_set[i]))
1655 * The set/restore sigmask functions are used to (temporarily) overwrite
1656 * the process p_sigmask during an RPC call (for example). These are also
1657 * used in other places in the NFS client that might tsleep().
1660 nfs_set_sigmask(struct thread *td, sigset_t *oldset)
1668 td = curthread; /* XXX */
1670 /* Remove the NFS set of signals from newset */
1672 mtx_lock(&p->p_sigacts->ps_mtx);
1673 for (i = 0 ; i < sizeof(nfs_sig_set)/sizeof(int) ; i++) {
1675 * But make sure we leave the ones already masked
1676 * by the process, ie. remove the signal from the
1677 * temporary signalmask only if it wasn't already
1680 if (!SIGISMEMBER(td->td_sigmask, nfs_sig_set[i]) &&
1681 !SIGISMEMBER(p->p_sigacts->ps_sigignore, nfs_sig_set[i]))
1682 SIGDELSET(newset, nfs_sig_set[i]);
1684 mtx_unlock(&p->p_sigacts->ps_mtx);
1686 kern_sigprocmask(td, SIG_SETMASK, &newset, oldset, 0);
1690 nfs_restore_sigmask(struct thread *td, sigset_t *set)
1693 td = curthread; /* XXX */
1694 kern_sigprocmask(td, SIG_SETMASK, set, NULL, 0);
1698 * NFS wrapper to msleep(), that shoves a new p_sigmask and restores the
1699 * old one after msleep() returns.
1702 nfs_msleep(struct thread *td, void *ident, struct mtx *mtx, int priority, char *wmesg, int timo)
1708 if ((priority & PCATCH) == 0)
1709 return msleep(ident, mtx, priority, wmesg, timo);
1711 td = curthread; /* XXX */
1712 nfs_set_sigmask(td, &oldset);
1713 error = msleep(ident, mtx, priority, wmesg, timo);
1714 nfs_restore_sigmask(td, &oldset);
1720 * Test for a termination condition pending on the process.
1721 * This is used for NFSMNT_INT mounts.
1724 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1729 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1730 return nfs4_sigintr(nmp, rep, td);
1732 mtx_lock(&rep->r_mtx);
1733 if (rep->r_flags & R_SOFTTERM) {
1734 mtx_unlock(&rep->r_mtx);
1737 mtx_unlock(&rep->r_mtx);
1739 /* Terminate all requests while attempting a forced unmount. */
1740 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1742 if (!(nmp->nm_flag & NFSMNT_INT))
1748 tmpset = p->p_siglist;
1749 SIGSETOR(tmpset, td->td_siglist);
1750 SIGSETNAND(tmpset, td->td_sigmask);
1751 mtx_lock(&p->p_sigacts->ps_mtx);
1752 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1753 mtx_unlock(&p->p_sigacts->ps_mtx);
1754 if ((SIGNOTEMPTY(p->p_siglist) || SIGNOTEMPTY(td->td_siglist))
1755 && nfs_sig_pending(tmpset)) {
1764 * Lock a socket against others.
1765 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1766 * and also to avoid race conditions between the processes with nfs requests
1767 * in progress when a reconnect is necessary.
1770 nfs_connect_lock(struct nfsreq *rep)
1772 int *statep = &rep->r_nmp->nm_state;
1774 int error, slpflag = 0, slptimeo = 0;
1777 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1779 while (*statep & NFSSTA_SNDLOCK) {
1780 error = nfs_sigintr(rep->r_nmp, rep, td);
1784 *statep |= NFSSTA_WANTSND;
1785 (void) msleep(statep, &rep->r_nmp->nm_mtx,
1786 slpflag | (PZERO - 1), "nfsndlck", slptimeo);
1787 if (slpflag == PCATCH) {
1792 *statep |= NFSSTA_SNDLOCK;
1797 * Unlock the stream socket for others.
1800 nfs_connect_unlock(struct nfsreq *rep)
1802 int *statep = &rep->r_nmp->nm_state;
1804 if ((*statep & NFSSTA_SNDLOCK) == 0)
1805 panic("nfs sndunlock");
1806 *statep &= ~NFSSTA_SNDLOCK;
1807 if (*statep & NFSSTA_WANTSND) {
1808 *statep &= ~NFSSTA_WANTSND;
1816 * Check for badly aligned mbuf data and realign by copying the unaligned
1817 * portion of the data into a new mbuf chain and freeing the portions
1818 * of the old chain that were replaced.
1820 * We cannot simply realign the data within the existing mbuf chain
1821 * because the underlying buffers may contain other rpc commands and
1822 * we cannot afford to overwrite them.
1824 * We would prefer to avoid this situation entirely. The situation does
1825 * not occur with NFS/UDP and is supposed to only occassionally occur
1826 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1830 nfs_realign(struct mbuf **pm, int hsiz)
1833 struct mbuf *n = NULL;
1837 while ((m = *pm) != NULL) {
1838 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1839 MGET(n, M_DONTWAIT, MT_DATA);
1842 if (m->m_len >= MINCLSIZE) {
1843 MCLGET(n, M_DONTWAIT);
1844 if (n->m_ext.ext_buf == NULL) {
1855 * If n is non-NULL, loop on m copying data, then replace the
1856 * portion of the chain that had to be realigned.
1859 ++nfs_realign_count;
1861 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1873 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1877 p = td ? td->td_proc : NULL;
1879 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1882 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1888 nfs_down(rep, nmp, td, msg, error, flags)
1890 struct nfsmount *nmp;
1897 mtx_lock(&nmp->nm_mtx);
1898 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1899 nmp->nm_state |= NFSSTA_TIMEO;
1900 mtx_unlock(&nmp->nm_mtx);
1901 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1904 mtx_unlock(&nmp->nm_mtx);
1905 #ifdef NFSSTA_LOCKTIMEO
1906 mtx_lock(&nmp->nm_mtx);
1907 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1908 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1909 mtx_unlock(&nmp->nm_mtx);
1910 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1913 mtx_unlock(&nmp->nm_mtx);
1916 mtx_lock(&rep->r_mtx);
1917 rep->r_flags |= R_TPRINTFMSG;
1918 mtx_unlock(&rep->r_mtx);
1920 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1924 nfs_up(rep, nmp, td, msg, flags)
1926 struct nfsmount *nmp;
1931 if (nmp == NULL || rep == NULL)
1933 mtx_lock(&rep->r_mtx);
1934 if ((rep->r_flags & R_TPRINTFMSG) != 0) {
1935 mtx_unlock(&rep->r_mtx);
1936 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1938 mtx_unlock(&rep->r_mtx);
1940 mtx_lock(&nmp->nm_mtx);
1941 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1942 nmp->nm_state &= ~NFSSTA_TIMEO;
1943 mtx_unlock(&nmp->nm_mtx);
1944 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1947 mtx_unlock(&nmp->nm_mtx);
1949 #ifdef NFSSTA_LOCKTIMEO
1950 mtx_lock(&nmp->nm_mtx);
1951 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1952 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1953 mtx_unlock(&nmp->nm_mtx);
1954 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1957 mtx_unlock(&nmp->nm_mtx);