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/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/vnode.h>
61 #include <netinet/in.h>
62 #include <netinet/tcp.h>
64 #include <rpc/rpcclnt.h>
66 #include <nfs/rpcv2.h>
67 #include <nfs/nfsproto.h>
68 #include <nfsclient/nfs.h>
69 #include <nfs/xdr_subs.h>
70 #include <nfsclient/nfsm_subs.h>
71 #include <nfsclient/nfsmount.h>
72 #include <nfsclient/nfsnode.h>
74 #include <nfs4client/nfs4.h>
80 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
81 * Use the mean and mean deviation of rtt for the appropriate type of rpc
82 * for the frequent rpcs and a default for the others.
83 * The justification for doing "other" this way is that these rpcs
84 * happen so infrequently that timer est. would probably be stale.
85 * Also, since many of these rpcs are
86 * non-idempotent, a conservative timeout is desired.
87 * getattr, lookup - A+2D
91 #define NFS_RTO(n, t) \
92 ((t) == 0 ? (n)->nm_timeo : \
94 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
95 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
96 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
97 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
100 * Defines which timer to use for the procnum.
107 static int proct[NFS_NPROCS] = {
108 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
111 static int nfs_realign_test;
112 static int nfs_realign_count;
113 static int nfs_bufpackets = 4;
114 static int nfs_reconnects;
116 SYSCTL_DECL(_vfs_nfs);
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
120 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
121 SYSCTL_INT(_vfs_nfs, OID_AUTO, reconnects, CTLFLAG_RD, &nfs_reconnects, 0,
122 "number of times the nfs client has had to reconnect");
126 * There is a congestion window for outstanding rpcs maintained per mount
127 * point. The cwnd size is adjusted in roughly the way that:
128 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
129 * SIGCOMM '88". ACM, August 1988.
130 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
131 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
132 * of rpcs is in progress.
133 * (The sent count and cwnd are scaled for integer arith.)
134 * Variants of "slow start" were tried and were found to be too much of a
135 * performance hit (ave. rtt 3 times larger),
136 * I suspect due to the large rtt that nfs rpcs have.
138 #define NFS_CWNDSCALE 256
139 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
140 #define NFS_NBACKOFF 8
141 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
142 struct callout nfs_callout;
144 static int nfs_msg(struct thread *, const char *, const char *, int);
145 static int nfs_rcvlock(struct nfsreq *);
146 static void nfs_rcvunlock(struct nfsreq *);
147 static void nfs_realign(struct mbuf **pm, int hsiz);
148 static int nfs_receive(struct nfsreq *rep, struct sockaddr **aname,
150 static int nfs_reply(struct nfsreq *);
151 static void nfs_softterm(struct nfsreq *rep);
152 static int nfs_reconnect(struct nfsreq *rep);
155 * Initialize sockets and congestion for a new NFS connection.
156 * We do not free the sockaddr if error.
159 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
162 int error, rcvreserve, sndreserve;
164 struct sockaddr *saddr;
165 struct thread *td = &thread0; /* only used for socreate and sobind */
171 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
172 nmp->nm_soproto, nmp->nm_mountp->mnt_cred, td);
176 nmp->nm_soflags = so->so_proto->pr_flags;
179 * Some servers require that the client port be a reserved port number.
181 if (nmp->nm_flag & NFSMNT_RESVPORT) {
184 struct sockaddr_in6 ssin;
187 bzero(&sopt, sizeof sopt);
188 switch(saddr->sa_family) {
190 sopt.sopt_level = IPPROTO_IP;
191 sopt.sopt_name = IP_PORTRANGE;
192 ip = IP_PORTRANGE_LOW;
193 ip2 = IP_PORTRANGE_DEFAULT;
194 len = sizeof (struct sockaddr_in);
198 sopt.sopt_level = IPPROTO_IPV6;
199 sopt.sopt_name = IPV6_PORTRANGE;
200 ip = IPV6_PORTRANGE_LOW;
201 ip2 = IPV6_PORTRANGE_DEFAULT;
202 len = sizeof (struct sockaddr_in6);
208 sa = (struct sockaddr *)&ssin;
211 sa->sa_family = saddr->sa_family;
212 sopt.sopt_dir = SOPT_SET;
213 sopt.sopt_val = (void *)&ip;
214 sopt.sopt_valsize = sizeof(ip);
215 error = sosetopt(so, &sopt);
218 error = sobind(so, sa, td);
222 error = sosetopt(so, &sopt);
229 * Protocols that do not require connections may be optionally left
230 * unconnected for servers that reply from a port other than NFS_PORT.
232 if (nmp->nm_flag & NFSMNT_NOCONN) {
233 if (nmp->nm_soflags & PR_CONNREQUIRED) {
238 error = soconnect(so, nmp->nm_nam, td);
243 * Wait for the connection to complete. Cribbed from the
244 * connect system call but with the wait timing out so
245 * that interruptible mounts don't hang here for a long time.
248 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
249 (void) msleep(&so->so_timeo, SOCK_MTX(so),
250 PSOCK, "nfscon", 2 * hz);
251 if ((so->so_state & SS_ISCONNECTING) &&
252 so->so_error == 0 && rep &&
253 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
254 so->so_state &= ~SS_ISCONNECTING;
260 error = so->so_error;
267 so->so_rcv.sb_timeo = 12 * hz;
268 so->so_snd.sb_timeo = 5 * hz;
271 * Get buffer reservation size from sysctl, but impose reasonable
274 pktscale = nfs_bufpackets;
280 if (nmp->nm_sotype == SOCK_DGRAM) {
281 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
282 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
283 NFS_MAXPKTHDR) * pktscale;
284 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
285 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
286 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
287 NFS_MAXPKTHDR) * pktscale;
289 if (nmp->nm_sotype != SOCK_STREAM)
290 panic("nfscon sotype");
291 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
295 bzero(&sopt, sizeof sopt);
296 sopt.sopt_dir = SOPT_SET;
297 sopt.sopt_level = SOL_SOCKET;
298 sopt.sopt_name = SO_KEEPALIVE;
299 sopt.sopt_val = &val;
300 sopt.sopt_valsize = sizeof val;
304 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
308 bzero(&sopt, sizeof sopt);
309 sopt.sopt_dir = SOPT_SET;
310 sopt.sopt_level = IPPROTO_TCP;
311 sopt.sopt_name = TCP_NODELAY;
312 sopt.sopt_val = &val;
313 sopt.sopt_valsize = sizeof val;
317 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
318 sizeof (u_int32_t)) * pktscale;
319 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
320 sizeof (u_int32_t)) * pktscale;
322 error = soreserve(so, sndreserve, rcvreserve);
325 SOCKBUF_LOCK(&so->so_rcv);
326 so->so_rcv.sb_flags |= SB_NOINTR;
327 SOCKBUF_UNLOCK(&so->so_rcv);
328 SOCKBUF_LOCK(&so->so_snd);
329 so->so_snd.sb_flags |= SB_NOINTR;
330 SOCKBUF_UNLOCK(&so->so_snd);
332 /* Initialize other non-zero congestion variables */
333 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
334 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
335 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
336 nmp->nm_sdrtt[3] = 0;
337 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
339 nmp->nm_timeouts = 0;
349 * Called when a connection is broken on a reliable protocol.
350 * - clean up the old socket
351 * - nfs_connect() again
352 * - set R_MUSTRESEND for all outstanding requests on mount point
353 * If this fails the mount point is DEAD!
354 * nb: Must be called with the nfs_sndlock() set on the mount point.
357 nfs_reconnect(struct nfsreq *rep)
360 struct nfsmount *nmp = rep->r_nmp;
365 while ((error = nfs_connect(nmp, rep)) != 0) {
366 if (error == ERESTART)
368 if (error == EIO || error == EINTR)
370 (void) tsleep(&lbolt, PSOCK, "nfscon", 0);
374 * Loop through outstanding request list and fix up all requests
377 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
378 if (rp->r_nmp == nmp)
379 rp->r_flags |= R_MUSTRESEND;
385 * NFS disconnect. Clean up and unlink.
388 nfs_disconnect(struct nfsmount *nmp)
397 soshutdown(so, SHUT_RDWR);
403 nfs_safedisconnect(struct nfsmount *nmp)
405 struct nfsreq dummyreq;
407 bzero(&dummyreq, sizeof(dummyreq));
408 dummyreq.r_nmp = nmp;
409 nfs_rcvlock(&dummyreq);
411 nfs_rcvunlock(&dummyreq);
415 * This is the nfs send routine. For connection based socket types, it
416 * must be called with an nfs_sndlock() on the socket.
417 * - return EINTR if the RPC is terminated, 0 otherwise
418 * - set R_MUSTRESEND if the send fails for any reason
419 * - do any cleanup required by recoverable socket errors (?)
422 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
425 struct sockaddr *sendnam;
426 int error, error2, soflags, flags;
430 KASSERT(rep, ("nfs_send: called with rep == NULL"));
432 error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
437 if ((so = rep->r_nmp->nm_so) == NULL) {
438 rep->r_flags |= R_MUSTRESEND;
442 rep->r_flags &= ~R_MUSTRESEND;
443 soflags = rep->r_nmp->nm_soflags;
445 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
449 if (so->so_type == SOCK_SEQPACKET)
454 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
455 flags, curthread /*XXX*/);
456 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
458 rep->r_flags |= R_MUSTRESEND;
463 * Don't report EPIPE errors on nfs sockets.
464 * These can be due to idle tcp mounts which will be closed by
465 * netapp, solaris, etc. if left idle too long.
467 if (error != EPIPE) {
468 log(LOG_INFO, "nfs send error %d for server %s\n",
470 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
473 * Deal with errors for the client side.
475 error2 = NFS_SIGREP(rep);
479 rep->r_flags |= R_MUSTRESEND;
482 * Handle any recoverable (soft) socket errors here. (?)
484 if (error != EINTR && error != ERESTART && error != EIO &&
485 error != EWOULDBLOCK && error != EPIPE)
492 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
493 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
494 * Mark and consolidate the data into a new mbuf list.
495 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
497 * For SOCK_STREAM we must be very careful to read an entire record once
498 * we have read any of it, even if the system call has been interrupted.
501 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
507 struct mbuf *control;
509 struct sockaddr **getnam;
510 int error, error2, sotype, rcvflg;
511 struct thread *td = curthread; /* XXX */
516 * Set up arguments for soreceive()
520 sotype = rep->r_nmp->nm_sotype;
523 * For reliable protocols, lock against other senders/receivers
524 * in case a reconnect is necessary.
525 * For SOCK_STREAM, first get the Record Mark to find out how much
526 * more there is to get.
527 * We must lock the socket against other receivers
528 * until we have an entire rpc request/reply.
530 if (sotype != SOCK_DGRAM) {
531 error = nfs_sndlock(rep);
536 * Check for fatal errors and resending request.
539 * Ugh: If a reconnect attempt just happened, nm_so
540 * would have changed. NULL indicates a failed
541 * attempt that has essentially shut down this
544 if (rep->r_mrep || (error = NFS_SIGREP(rep)) != 0) {
546 return (error == 0 ? EINTR : error);
548 so = rep->r_nmp->nm_so;
550 error = nfs_reconnect(rep);
557 while (rep->r_flags & R_MUSTRESEND) {
558 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
559 nfsstats.rpcretries++;
560 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
562 if (error == EINTR || error == ERESTART ||
564 (error = nfs_reconnect(rep)) != 0) {
572 if (sotype == SOCK_STREAM) {
573 aio.iov_base = (caddr_t) &len;
574 aio.iov_len = sizeof(u_int32_t);
577 auio.uio_segflg = UIO_SYSSPACE;
578 auio.uio_rw = UIO_READ;
580 auio.uio_resid = sizeof(u_int32_t);
583 rcvflg = MSG_WAITALL;
584 error = so->so_proto->pr_usrreqs->pru_soreceive
585 (so, NULL, &auio, NULL, NULL, &rcvflg);
586 if (error == EWOULDBLOCK) {
587 error2 = NFS_SIGREP(rep);
592 if (!error && auio.uio_resid > 0) {
594 * Don't log a 0 byte receive; it means
595 * that the socket has been closed, and
596 * can happen during normal operation
597 * (forcible unmount or Solaris server).
599 if (auio.uio_resid != sizeof (u_int32_t))
601 "short receive (%d/%d) from nfs server %s\n",
602 (int)(sizeof(u_int32_t) - auio.uio_resid),
603 (int)sizeof(u_int32_t),
604 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
609 len = ntohl(len) & ~0x80000000;
611 * This is SERIOUS! We are out of sync with the sender
612 * and forcing a disconnect/reconnect is all I can do.
614 if (len > NFS_MAXPACKET) {
615 log(LOG_ERR, "%s (%d) from nfs server %s\n",
616 "impossible packet length",
618 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
622 auio.uio_resid = len;
624 rcvflg = MSG_WAITALL;
625 error = so->so_proto->pr_usrreqs->pru_soreceive
627 &auio, mp, NULL, &rcvflg);
629 if (!error && auio.uio_resid > 0) {
630 if (len != auio.uio_resid)
632 "short receive (%d/%d) from nfs server %s\n",
633 len - auio.uio_resid, len,
634 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
639 * NB: Since uio_resid is big, MSG_WAITALL is ignored
640 * and soreceive() will return when it has either a
641 * control msg or a data msg.
642 * We have no use for control msg., but must grab them
643 * and then throw them away so we know what is going
646 auio.uio_resid = len = 100000000; /* Anything Big */
650 error = so->so_proto->pr_usrreqs->pru_soreceive
652 &auio, mp, &control, &rcvflg);
655 if (error == EWOULDBLOCK && rep) {
656 error2 = NFS_SIGREP(rep);
660 } while (!error && *mp == NULL && control);
661 if ((rcvflg & MSG_EOR) == 0)
663 if (!error && *mp == NULL)
665 len -= auio.uio_resid;
668 if (error && error != EINTR && error != EIO &&
672 if (error != EPIPE && error != EWOULDBLOCK)
674 "receive error %d from nfs server %s\n",
676 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
677 error = nfs_sndlock(rep);
679 error = nfs_reconnect(rep);
688 * We may have failed while rebinding the datagram socket
689 * so attempt a rebind here.
691 if ((so = rep->r_nmp->nm_so) == NULL) {
692 error = nfs_sndlock(rep);
694 error = nfs_reconnect(rep);
699 so = rep->r_nmp->nm_so;
701 if (so->so_state & SS_ISCONNECTED)
705 auio.uio_resid = len = 1000000;
709 error = so->so_proto->pr_usrreqs->pru_soreceive
710 (so, getnam, &auio, mp,
713 error2 = NFS_SIGREP(rep);
720 error2 = nfs_sndlock(rep);
722 error2 = nfs_reconnect(rep);
726 so = rep->r_nmp->nm_so;
732 } while (error == EWOULDBLOCK);
734 len -= auio.uio_resid;
741 * Search for any mbufs that are not a multiple of 4 bytes long
742 * or with m_data not longword aligned.
743 * These could cause pointer alignment problems, so copy them to
744 * well aligned mbufs.
746 nfs_realign(mp, 5 * NFSX_UNSIGNED);
751 * Implement receipt of reply on a socket.
752 * We must search through the list of received datagrams matching them
753 * with outstanding requests using the xid, until ours is found.
757 nfs_reply(struct nfsreq *myrep)
760 struct nfsmount *nmp = myrep->r_nmp;
762 struct mbuf *mrep, *md;
763 struct sockaddr *nam;
769 * Loop around until we get our own reply
773 * Lock against other receivers so that I don't get stuck in
774 * sbwait() after someone else has received my reply for me.
775 * Also necessary for connection based protocols to avoid
776 * race conditions during a reconnect.
777 * If nfs_rcvlock() returns EALREADY, that means that
778 * the reply has already been recieved by another
779 * process and we can return immediately. In this
780 * case, the lock is not taken to avoid races with
783 error = nfs_rcvlock(myrep);
784 if (error == EALREADY)
789 * Get the next Rpc reply off the socket
791 error = nfs_receive(myrep, &nam, &mrep);
792 nfs_rcvunlock(myrep);
796 * Ignore routing errors on connectionless protocols??
798 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
799 nmp->nm_so->so_error = 0;
800 if (myrep->r_flags & R_GETONEREP)
810 * Get the xid and check that it is an rpc reply
813 dpos = mtod(md, caddr_t);
814 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
816 if (*tl != rpc_reply) {
817 nfsstats.rpcinvalid++;
820 if (myrep->r_flags & R_GETONEREP)
826 * Loop through the request list to match up the reply
827 * Iff no match, just drop the datagram
829 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
830 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
836 * Update congestion window.
837 * Do the additive increase of
840 if (nmp->nm_cwnd <= nmp->nm_sent) {
842 (NFS_CWNDSCALE * NFS_CWNDSCALE +
843 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
844 if (nmp->nm_cwnd > NFS_MAXCWND)
845 nmp->nm_cwnd = NFS_MAXCWND;
847 if (rep->r_flags & R_SENT) {
848 rep->r_flags &= ~R_SENT;
849 nmp->nm_sent -= NFS_CWNDSCALE;
852 * Update rtt using a gain of 0.125 on the mean
853 * and a gain of 0.25 on the deviation.
855 if (rep->r_flags & R_TIMING) {
857 * Since the timer resolution of
858 * NFS_HZ is so course, it can often
859 * result in r_rtt == 0. Since
860 * r_rtt == N means that the actual
861 * rtt is between N+dt and N+2-dt ticks,
865 t1 -= (NFS_SRTT(rep) >> 3);
869 t1 -= (NFS_SDRTT(rep) >> 2);
870 NFS_SDRTT(rep) += t1;
872 nmp->nm_timeouts = 0;
877 * If not matched to a request, drop it.
878 * If it's mine, get out.
881 nfsstats.rpcunexpected++;
883 } else if (rep == myrep) {
884 if (rep->r_mrep == NULL)
885 panic("nfsreply nil");
888 if (myrep->r_flags & R_GETONEREP)
894 * nfs_request - goes something like this
895 * - fill in request struct
896 * - links it into list
897 * - calls nfs_send() for first transmit
898 * - calls nfs_receive() to get reply
899 * - break down rpc header and return with nfs reply pointed to
901 * nb: always frees up mreq mbuf list
903 /* XXX overloaded before */
904 #define NQ_TRYLATERDEL 15 /* Initial try later delay (sec) */
907 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
908 struct thread *td, struct ucred *cred, struct mbuf **mrp,
909 struct mbuf **mdp, caddr_t *dposp)
911 struct mbuf *mrep, *m2;
915 struct nfsmount *nmp;
916 struct mbuf *m, *md, *mheadend;
919 int s, error = 0, mrest_len, auth_len, auth_type;
920 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
924 /* Reject requests while attempting a forced unmount. */
925 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
929 nmp = VFSTONFS(vp->v_mount);
930 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
931 return nfs4_request(vp, mrest, procnum, td, cred, mrp, mdp, dposp);
932 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
936 rep->r_procnum = procnum;
938 getmicrouptime(&now);
939 rep->r_lastmsg = now.tv_sec -
940 ((nmp->nm_tprintf_delay) - (nmp->nm_tprintf_initial_delay));
941 mrest_len = m_length(mrest, NULL);
944 * Get the RPC header with authorization.
946 auth_type = RPCAUTH_UNIX;
947 if (cred->cr_ngroups < 1)
948 panic("nfsreq nogrps");
949 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
950 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
952 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
953 mrest, mrest_len, &mheadend, &xid);
956 * For stream protocols, insert a Sun RPC Record Mark.
958 if (nmp->nm_sotype == SOCK_STREAM) {
959 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
960 *mtod(m, u_int32_t *) = htonl(0x80000000 |
961 (m->m_pkthdr.len - NFSX_UNSIGNED));
966 if (nmp->nm_flag & NFSMNT_SOFT)
967 rep->r_retry = nmp->nm_retry;
969 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
970 rep->r_rtt = rep->r_rexmit = 0;
971 if (proct[procnum] > 0)
972 rep->r_flags = R_TIMING;
978 * Do the client side RPC.
980 nfsstats.rpcrequests++;
982 * Chain request into list of outstanding requests. Be sure
983 * to put it LAST so timer finds oldest requests first.
986 if (TAILQ_EMPTY(&nfs_reqq))
987 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
988 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
991 * If backing off another request or avoiding congestion, don't
992 * send this one now but let timer do it. If not timing a request,
995 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
996 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
997 nmp->nm_sent < nmp->nm_cwnd)) {
999 error = nfs_sndlock(rep);
1001 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
1002 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1005 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1006 nmp->nm_sent += NFS_CWNDSCALE;
1007 rep->r_flags |= R_SENT;
1015 * Wait for the reply from our send or the timer's.
1017 if (!error || error == EPIPE)
1018 error = nfs_reply(rep);
1021 * RPC done, unlink the request.
1024 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1025 if (TAILQ_EMPTY(&nfs_reqq))
1026 callout_stop(&nfs_callout);
1030 * Decrement the outstanding request count.
1032 if (rep->r_flags & R_SENT) {
1033 rep->r_flags &= ~R_SENT; /* paranoia */
1034 nmp->nm_sent -= NFS_CWNDSCALE;
1038 * If there was a successful reply and a tprintf msg.
1039 * tprintf a response.
1042 nfs_up(rep, nmp, rep->r_td, "is alive again", NFSSTA_TIMEO);
1047 m_freem(rep->r_mreq);
1048 free((caddr_t)rep, M_NFSREQ);
1053 * break down the rpc header and check if ok
1055 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
1056 if (*tl++ == rpc_msgdenied) {
1057 if (*tl == rpc_mismatch)
1062 m_freem(rep->r_mreq);
1063 free((caddr_t)rep, M_NFSREQ);
1068 * Just throw away any verifyer (ie: kerberos etc).
1070 i = fxdr_unsigned(int, *tl++); /* verf type */
1071 i = fxdr_unsigned(int32_t, *tl); /* len */
1073 nfsm_adv(nfsm_rndup(i));
1074 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1077 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1079 error = fxdr_unsigned(int, *tl);
1080 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1081 error == NFSERR_TRYLATER) {
1084 waituntil = time_second + trylater_delay;
1085 while (time_second < waituntil)
1086 (void) tsleep(&lbolt,
1087 PSOCK, "nqnfstry", 0);
1088 trylater_delay *= nfs_backoff[trylater_cnt];
1089 if (trylater_cnt < NFS_NBACKOFF - 1)
1095 * If the File Handle was stale, invalidate the
1096 * lookup cache, just in case.
1098 if (error == ESTALE)
1100 if (nmp->nm_flag & NFSMNT_NFSV3) {
1104 error |= NFSERR_RETERR;
1107 m_freem(rep->r_mreq);
1108 free((caddr_t)rep, M_NFSREQ);
1115 m_freem(rep->r_mreq);
1116 FREE((caddr_t)rep, M_NFSREQ);
1120 error = EPROTONOSUPPORT;
1122 m_freem(rep->r_mreq);
1123 free((caddr_t)rep, M_NFSREQ);
1129 * Scan the nfsreq list and retranmit any requests that have timed out
1130 * To avoid retransmission attempts on STREAM sockets (in the future) make
1131 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1134 nfs_timer(void *arg)
1139 struct nfsmount *nmp;
1145 getmicrouptime(&now);
1146 td = &thread0; /* XXX for credentials, may break if sleep */
1148 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1150 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1152 if (nfs_sigintr(nmp, rep, rep->r_td))
1154 if (nmp->nm_tprintf_initial_delay != 0 &&
1155 (rep->r_rexmit > 2 || (rep->r_flags & R_RESENDERR)) &&
1156 rep->r_lastmsg + nmp->nm_tprintf_delay < now.tv_sec) {
1157 rep->r_lastmsg = now.tv_sec;
1158 nfs_down(rep, nmp, rep->r_td, "not responding",
1161 if (!(nmp->nm_state & NFSSTA_MOUNTED)) {
1162 /* we're not yet completely mounted and */
1163 /* we can't complete an RPC, so we fail */
1164 nfsstats.rpctimeouts++;
1170 if (rep->r_rtt >= 0) {
1172 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1173 timeo = nmp->nm_timeo;
1175 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1176 if (nmp->nm_timeouts > 0)
1177 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1178 if (rep->r_rtt <= timeo)
1180 if (nmp->nm_timeouts < NFS_NBACKOFF)
1183 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1184 nfsstats.rpctimeouts++;
1188 if (nmp->nm_sotype != SOCK_DGRAM) {
1189 if (++rep->r_rexmit > NFS_MAXREXMIT)
1190 rep->r_rexmit = NFS_MAXREXMIT;
1193 if ((so = nmp->nm_so) == NULL)
1197 * If there is enough space and the window allows..
1199 * Set r_rtt to -1 in case we fail to send it now.
1202 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1203 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1204 (rep->r_flags & R_SENT) ||
1205 nmp->nm_sent < nmp->nm_cwnd) &&
1206 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1207 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1208 error = (*so->so_proto->pr_usrreqs->pru_send)
1209 (so, 0, m, NULL, NULL, td);
1211 error = (*so->so_proto->pr_usrreqs->pru_send)
1212 (so, 0, m, nmp->nm_nam, NULL, td);
1214 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1216 rep->r_flags |= R_RESENDERR;
1219 * Iff first send, start timing
1220 * else turn timing off, backoff timer
1221 * and divide congestion window by 2.
1223 rep->r_flags &= ~R_RESENDERR;
1224 if (rep->r_flags & R_SENT) {
1225 rep->r_flags &= ~R_TIMING;
1226 if (++rep->r_rexmit > NFS_MAXREXMIT)
1227 rep->r_rexmit = NFS_MAXREXMIT;
1229 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1230 nmp->nm_cwnd = NFS_CWNDSCALE;
1231 nfsstats.rpcretries++;
1233 rep->r_flags |= R_SENT;
1234 nmp->nm_sent += NFS_CWNDSCALE;
1241 callout_reset(&nfs_callout, nfs_ticks, nfs_timer, NULL);
1245 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1246 * wait for all requests to complete. This is used by forced unmounts
1247 * to terminate any outstanding RPCs.
1250 nfs_nmcancelreqs(nmp)
1251 struct nfsmount *nmp;
1257 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1258 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1259 (req->r_flags & R_SOFTTERM))
1265 for (i = 0; i < 30; i++) {
1267 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1268 if (nmp == req->r_nmp)
1274 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1280 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1281 * The nm_send count is decremented now to avoid deadlocks when the process in
1282 * soreceive() hasn't yet managed to send its own request.
1286 nfs_softterm(struct nfsreq *rep)
1289 rep->r_flags |= R_SOFTTERM;
1290 if (rep->r_flags & R_SENT) {
1291 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1292 rep->r_flags &= ~R_SENT;
1297 * Test for a termination condition pending on the process.
1298 * This is used for NFSMNT_INT mounts.
1301 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1306 if ((nmp->nm_flag & NFSMNT_NFSV4) != 0)
1307 return nfs4_sigintr(nmp, rep, td);
1308 if (rep && (rep->r_flags & R_SOFTTERM))
1310 /* Terminate all requests while attempting a forced unmount. */
1311 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1313 if (!(nmp->nm_flag & NFSMNT_INT))
1320 tmpset = p->p_siglist;
1321 SIGSETNAND(tmpset, td->td_sigmask);
1322 mtx_lock(&p->p_sigacts->ps_mtx);
1323 SIGSETNAND(tmpset, p->p_sigacts->ps_sigignore);
1324 mtx_unlock(&p->p_sigacts->ps_mtx);
1325 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset)) {
1335 * Lock a socket against others.
1336 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1337 * and also to avoid race conditions between the processes with nfs requests
1338 * in progress when a reconnect is necessary.
1341 nfs_sndlock(struct nfsreq *rep)
1343 int *statep = &rep->r_nmp->nm_state;
1345 int error, slpflag = 0, slptimeo = 0;
1348 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1350 while (*statep & NFSSTA_SNDLOCK) {
1351 error = nfs_sigintr(rep->r_nmp, rep, td);
1354 *statep |= NFSSTA_WANTSND;
1355 (void) tsleep(statep, slpflag | (PZERO - 1),
1356 "nfsndlck", slptimeo);
1357 if (slpflag == PCATCH) {
1362 *statep |= NFSSTA_SNDLOCK;
1367 * Unlock the stream socket for others.
1370 nfs_sndunlock(struct nfsreq *rep)
1372 int *statep = &rep->r_nmp->nm_state;
1374 if ((*statep & NFSSTA_SNDLOCK) == 0)
1375 panic("nfs sndunlock");
1376 *statep &= ~NFSSTA_SNDLOCK;
1377 if (*statep & NFSSTA_WANTSND) {
1378 *statep &= ~NFSSTA_WANTSND;
1384 nfs_rcvlock(struct nfsreq *rep)
1386 int *statep = &rep->r_nmp->nm_state;
1387 int error, slpflag, slptimeo = 0;
1389 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1393 while (*statep & NFSSTA_RCVLOCK) {
1394 error = nfs_sigintr(rep->r_nmp, rep, rep->r_td);
1397 *statep |= NFSSTA_WANTRCV;
1398 (void) tsleep(statep, slpflag | (PZERO - 1), "nfsrcvlk",
1401 * If our reply was recieved while we were sleeping,
1402 * then just return without taking the lock to avoid a
1403 * situation where a single iod could 'capture' the
1406 if (rep->r_mrep != NULL)
1408 if (slpflag == PCATCH) {
1413 /* Always fail if our request has been cancelled. */
1414 if (rep != NULL && (error = NFS_SIGREP(rep)) != 0)
1416 *statep |= NFSSTA_RCVLOCK;
1421 * Unlock the stream socket for others.
1424 nfs_rcvunlock(struct nfsreq *rep)
1426 int *statep = &rep->r_nmp->nm_state;
1428 if ((*statep & NFSSTA_RCVLOCK) == 0)
1429 panic("nfs rcvunlock");
1430 *statep &= ~NFSSTA_RCVLOCK;
1431 if (*statep & NFSSTA_WANTRCV) {
1432 *statep &= ~NFSSTA_WANTRCV;
1440 * Check for badly aligned mbuf data and realign by copying the unaligned
1441 * portion of the data into a new mbuf chain and freeing the portions
1442 * of the old chain that were replaced.
1444 * We cannot simply realign the data within the existing mbuf chain
1445 * because the underlying buffers may contain other rpc commands and
1446 * we cannot afford to overwrite them.
1448 * We would prefer to avoid this situation entirely. The situation does
1449 * not occur with NFS/UDP and is supposed to only occassionally occur
1450 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1453 nfs_realign(struct mbuf **pm, int hsiz)
1456 struct mbuf *n = NULL;
1460 while ((m = *pm) != NULL) {
1461 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1462 MGET(n, M_TRYWAIT, MT_DATA);
1463 if (m->m_len >= MINCLSIZE) {
1464 MCLGET(n, M_TRYWAIT);
1472 * If n is non-NULL, loop on m copying data, then replace the
1473 * portion of the chain that had to be realigned.
1476 ++nfs_realign_count;
1478 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1489 nfs_msg(struct thread *td, const char *server, const char *msg, int error)
1493 p = td ? td->td_proc : NULL;
1495 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
1498 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
1504 nfs_down(rep, nmp, td, msg, error, flags)
1506 struct nfsmount *nmp;
1514 if ((flags & NFSSTA_TIMEO) && !(nmp->nm_state & NFSSTA_TIMEO)) {
1515 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1517 nmp->nm_state |= NFSSTA_TIMEO;
1519 #ifdef NFSSTA_LOCKTIMEO
1520 if ((flags & NFSSTA_LOCKTIMEO) && !(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1521 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1523 nmp->nm_state |= NFSSTA_LOCKTIMEO;
1527 rep->r_flags |= R_TPRINTFMSG;
1528 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
1532 nfs_up(rep, nmp, td, msg, flags)
1534 struct nfsmount *nmp;
1541 if ((rep == NULL) || (rep->r_flags & R_TPRINTFMSG) != 0)
1542 nfs_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
1543 if ((flags & NFSSTA_TIMEO) && (nmp->nm_state & NFSSTA_TIMEO)) {
1544 nmp->nm_state &= ~NFSSTA_TIMEO;
1545 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
1548 #ifdef NFSSTA_LOCKTIMEO
1549 if ((flags & NFSSTA_LOCKTIMEO) && (nmp->nm_state & NFSSTA_LOCKTIMEO)) {
1550 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
1551 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,