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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)nfs_socket.c 8.5 (Berkeley) 3/30/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * Socket operations for use by nfs
46 #include "opt_inet6.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/mutex.h>
57 #include <sys/protosw.h>
58 #include <sys/signalvar.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/sysctl.h>
62 #include <sys/syslog.h>
63 #include <sys/vnode.h>
65 #include <netinet/in.h>
66 #include <netinet/tcp.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfsproto.h>
70 #include <nfsclient/nfs.h>
71 #include <nfs/xdr_subs.h>
72 #include <nfsclient/nfsm_subs.h>
73 #include <nfsclient/nfsmount.h>
74 #include <nfsclient/nfsnode.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;
115 SYSCTL_DECL(_vfs_nfs);
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
118 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
119 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
123 * There is a congestion window for outstanding rpcs maintained per mount
124 * point. The cwnd size is adjusted in roughly the way that:
125 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
126 * SIGCOMM '88". ACM, August 1988.
127 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
128 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
129 * of rpcs is in progress.
130 * (The sent count and cwnd are scaled for integer arith.)
131 * Variants of "slow start" were tried and were found to be too much of a
132 * performance hit (ave. rtt 3 times larger),
133 * I suspect due to the large rtt that nfs rpcs have.
135 #define NFS_CWNDSCALE 256
136 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
137 #define NFS_NBACKOFF 8
138 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
139 struct callout_handle nfs_timer_handle;
141 static int nfs_msg(struct thread *, char *, char *);
142 static int nfs_rcvlock(struct nfsreq *);
143 static void nfs_rcvunlock(struct nfsreq *);
144 static void nfs_realign(struct mbuf **pm, int hsiz);
145 static int nfs_receive(struct nfsreq *rep, struct sockaddr **aname,
147 static int nfs_reply(struct nfsreq *);
148 static void nfs_softterm(struct nfsreq *rep);
149 static int nfs_reconnect(struct nfsreq *rep);
152 * Initialize sockets and congestion for a new NFS connection.
153 * We do not free the sockaddr if error.
156 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
159 int s, error, rcvreserve, sndreserve;
161 struct sockaddr *saddr;
162 struct thread *td = &thread0; /* only used for socreate and sobind */
166 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
167 nmp->nm_soproto, nmp->nm_cred, td);
171 nmp->nm_soflags = so->so_proto->pr_flags;
174 * Some servers require that the client port be a reserved port number.
176 if (nmp->nm_flag & NFSMNT_RESVPORT) {
179 struct sockaddr_in6 ssin;
182 bzero(&sopt, sizeof sopt);
183 switch(saddr->sa_family) {
185 sopt.sopt_level = IPPROTO_IP;
186 sopt.sopt_name = IP_PORTRANGE;
187 ip = IP_PORTRANGE_LOW;
188 ip2 = IP_PORTRANGE_DEFAULT;
189 len = sizeof (struct sockaddr_in);
193 sopt.sopt_level = IPPROTO_IPV6;
194 sopt.sopt_name = IPV6_PORTRANGE;
195 ip = IPV6_PORTRANGE_LOW;
196 ip2 = IPV6_PORTRANGE_DEFAULT;
197 len = sizeof (struct sockaddr_in6);
203 sa = (struct sockaddr *)&ssin;
206 sa->sa_family = saddr->sa_family;
207 sopt.sopt_dir = SOPT_SET;
208 sopt.sopt_val = (void *)&ip;
209 sopt.sopt_valsize = sizeof(ip);
210 error = sosetopt(so, &sopt);
213 error = sobind(so, sa, td);
217 error = sosetopt(so, &sopt);
224 * Protocols that do not require connections may be optionally left
225 * unconnected for servers that reply from a port other than NFS_PORT.
227 if (nmp->nm_flag & NFSMNT_NOCONN) {
228 if (nmp->nm_soflags & PR_CONNREQUIRED) {
233 error = soconnect(so, nmp->nm_nam, td);
238 * Wait for the connection to complete. Cribbed from the
239 * connect system call but with the wait timing out so
240 * that interruptible mounts don't hang here for a long time.
243 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
244 (void) tsleep((caddr_t)&so->so_timeo,
245 PSOCK, "nfscon", 2 * hz);
246 if ((so->so_state & SS_ISCONNECTING) &&
247 so->so_error == 0 && rep &&
248 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0) {
249 so->so_state &= ~SS_ISCONNECTING;
255 error = so->so_error;
262 so->so_rcv.sb_timeo = 5 * hz;
263 so->so_snd.sb_timeo = 5 * hz;
266 * Get buffer reservation size from sysctl, but impose reasonable
269 pktscale = nfs_bufpackets;
275 if (nmp->nm_sotype == SOCK_DGRAM) {
276 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
277 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
278 NFS_MAXPKTHDR) * pktscale;
279 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
280 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
281 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
282 NFS_MAXPKTHDR) * pktscale;
284 if (nmp->nm_sotype != SOCK_STREAM)
285 panic("nfscon sotype");
286 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
290 bzero(&sopt, sizeof sopt);
291 sopt.sopt_level = SOL_SOCKET;
292 sopt.sopt_name = SO_KEEPALIVE;
293 sopt.sopt_val = &val;
294 sopt.sopt_valsize = sizeof val;
298 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
302 bzero(&sopt, sizeof sopt);
303 sopt.sopt_level = IPPROTO_TCP;
304 sopt.sopt_name = TCP_NODELAY;
305 sopt.sopt_val = &val;
306 sopt.sopt_valsize = sizeof val;
310 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
311 sizeof (u_int32_t)) * pktscale;
312 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
313 sizeof (u_int32_t)) * pktscale;
315 error = soreserve(so, sndreserve, rcvreserve);
318 so->so_rcv.sb_flags |= SB_NOINTR;
319 so->so_snd.sb_flags |= SB_NOINTR;
321 /* Initialize other non-zero congestion variables */
322 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
323 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
324 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
325 nmp->nm_sdrtt[3] = 0;
326 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
328 nmp->nm_timeouts = 0;
338 * Called when a connection is broken on a reliable protocol.
339 * - clean up the old socket
340 * - nfs_connect() again
341 * - set R_MUSTRESEND for all outstanding requests on mount point
342 * If this fails the mount point is DEAD!
343 * nb: Must be called with the nfs_sndlock() set on the mount point.
346 nfs_reconnect(struct nfsreq *rep)
349 struct nfsmount *nmp = rep->r_nmp;
353 while ((error = nfs_connect(nmp, rep)) != 0) {
354 if (error == EINTR || error == ERESTART)
356 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
360 * Loop through outstanding request list and fix up all requests
363 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
364 if (rp->r_nmp == nmp)
365 rp->r_flags |= R_MUSTRESEND;
371 * NFS disconnect. Clean up and unlink.
374 nfs_disconnect(struct nfsmount *nmp)
387 nfs_safedisconnect(struct nfsmount *nmp)
389 struct nfsreq dummyreq;
391 bzero(&dummyreq, sizeof(dummyreq));
392 dummyreq.r_nmp = nmp;
393 nfs_rcvlock(&dummyreq);
395 nfs_rcvunlock(&dummyreq);
399 * This is the nfs send routine. For connection based socket types, it
400 * must be called with an nfs_sndlock() on the socket.
401 * - return EINTR if the RPC is terminated, 0 otherwise
402 * - set R_MUSTRESEND if the send fails for any reason
403 * - do any cleanup required by recoverable socket errors (?)
406 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
409 struct sockaddr *sendnam;
410 int error, soflags, flags;
412 KASSERT(rep, ("nfs_send: called with rep == NULL"));
414 if (rep->r_flags & R_SOFTTERM) {
418 if ((so = rep->r_nmp->nm_so) == NULL) {
419 rep->r_flags |= R_MUSTRESEND;
423 rep->r_flags &= ~R_MUSTRESEND;
424 soflags = rep->r_nmp->nm_soflags;
426 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
430 if (so->so_type == SOCK_SEQPACKET)
435 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
436 flags, curthread /*XXX*/);
437 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
439 rep->r_flags |= R_MUSTRESEND;
443 log(LOG_INFO, "nfs send error %d for server %s\n", error,
444 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
446 * Deal with errors for the client side.
448 if (rep->r_flags & R_SOFTTERM)
451 rep->r_flags |= R_MUSTRESEND;
454 * Handle any recoverable (soft) socket errors here. (?)
456 if (error != EINTR && error != ERESTART &&
457 error != EWOULDBLOCK && error != EPIPE)
464 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
465 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
466 * Mark and consolidate the data into a new mbuf list.
467 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
469 * For SOCK_STREAM we must be very careful to read an entire record once
470 * we have read any of it, even if the system call has been interrupted.
473 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
479 struct mbuf *control;
481 struct sockaddr **getnam;
482 int error, sotype, rcvflg;
483 struct thread *td = curthread; /* XXX */
486 * Set up arguments for soreceive()
490 sotype = rep->r_nmp->nm_sotype;
493 * For reliable protocols, lock against other senders/receivers
494 * in case a reconnect is necessary.
495 * For SOCK_STREAM, first get the Record Mark to find out how much
496 * more there is to get.
497 * We must lock the socket against other receivers
498 * until we have an entire rpc request/reply.
500 if (sotype != SOCK_DGRAM) {
501 error = nfs_sndlock(rep);
506 * Check for fatal errors and resending request.
509 * Ugh: If a reconnect attempt just happened, nm_so
510 * would have changed. NULL indicates a failed
511 * attempt that has essentially shut down this
514 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
518 so = rep->r_nmp->nm_so;
520 error = nfs_reconnect(rep);
527 while (rep->r_flags & R_MUSTRESEND) {
528 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
529 nfsstats.rpcretries++;
530 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
532 if (error == EINTR || error == ERESTART ||
533 (error = nfs_reconnect(rep)) != 0) {
541 if (sotype == SOCK_STREAM) {
542 aio.iov_base = (caddr_t) &len;
543 aio.iov_len = sizeof(u_int32_t);
546 auio.uio_segflg = UIO_SYSSPACE;
547 auio.uio_rw = UIO_READ;
549 auio.uio_resid = sizeof(u_int32_t);
552 rcvflg = MSG_WAITALL;
553 error = so->so_proto->pr_usrreqs->pru_soreceive
554 (so, NULL, &auio, NULL, NULL, &rcvflg);
555 if (error == EWOULDBLOCK && rep) {
556 if (rep->r_flags & R_SOFTTERM)
559 } while (error == EWOULDBLOCK);
560 if (!error && auio.uio_resid > 0) {
562 * Don't log a 0 byte receive; it means
563 * that the socket has been closed, and
564 * can happen during normal operation
565 * (forcible unmount or Solaris server).
567 if (auio.uio_resid != sizeof (u_int32_t))
569 "short receive (%d/%d) from nfs server %s\n",
570 (int)(sizeof(u_int32_t) - auio.uio_resid),
571 (int)sizeof(u_int32_t),
572 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
577 len = ntohl(len) & ~0x80000000;
579 * This is SERIOUS! We are out of sync with the sender
580 * and forcing a disconnect/reconnect is all I can do.
582 if (len > NFS_MAXPACKET) {
583 log(LOG_ERR, "%s (%d) from nfs server %s\n",
584 "impossible packet length",
586 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
590 auio.uio_resid = len;
592 rcvflg = MSG_WAITALL;
593 error = so->so_proto->pr_usrreqs->pru_soreceive
595 &auio, mp, NULL, &rcvflg);
596 } while (error == EWOULDBLOCK || error == EINTR ||
598 if (!error && auio.uio_resid > 0) {
599 if (len != auio.uio_resid)
601 "short receive (%d/%d) from nfs server %s\n",
602 len - auio.uio_resid, len,
603 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
608 * NB: Since uio_resid is big, MSG_WAITALL is ignored
609 * and soreceive() will return when it has either a
610 * control msg or a data msg.
611 * We have no use for control msg., but must grab them
612 * and then throw them away so we know what is going
615 auio.uio_resid = len = 100000000; /* Anything Big */
619 error = so->so_proto->pr_usrreqs->pru_soreceive
621 &auio, mp, &control, &rcvflg);
624 if (error == EWOULDBLOCK && rep) {
625 if (rep->r_flags & R_SOFTTERM)
628 } while (error == EWOULDBLOCK ||
629 (!error && *mp == NULL && control));
630 if ((rcvflg & MSG_EOR) == 0)
632 if (!error && *mp == NULL)
634 len -= auio.uio_resid;
637 if (error && error != EINTR && error != ERESTART) {
642 "receive error %d from nfs server %s\n",
644 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
645 error = nfs_sndlock(rep);
647 error = nfs_reconnect(rep);
655 if ((so = rep->r_nmp->nm_so) == NULL)
657 if (so->so_state & SS_ISCONNECTED)
661 auio.uio_resid = len = 1000000;
665 error = so->so_proto->pr_usrreqs->pru_soreceive
666 (so, getnam, &auio, mp,
668 if (error == EWOULDBLOCK &&
669 (rep->r_flags & R_SOFTTERM))
671 } while (error == EWOULDBLOCK);
672 len -= auio.uio_resid;
679 * Search for any mbufs that are not a multiple of 4 bytes long
680 * or with m_data not longword aligned.
681 * These could cause pointer alignment problems, so copy them to
682 * well aligned mbufs.
684 nfs_realign(mp, 5 * NFSX_UNSIGNED);
689 * Implement receipt of reply on a socket.
690 * We must search through the list of received datagrams matching them
691 * with outstanding requests using the xid, until ours is found.
695 nfs_reply(struct nfsreq *myrep)
698 struct nfsmount *nmp = myrep->r_nmp;
700 struct mbuf *mrep, *md;
701 struct sockaddr *nam;
707 * Loop around until we get our own reply
711 * Lock against other receivers so that I don't get stuck in
712 * sbwait() after someone else has received my reply for me.
713 * Also necessary for connection based protocols to avoid
714 * race conditions during a reconnect.
715 * If nfs_rcvlock() returns EALREADY, that means that
716 * the reply has already been recieved by another
717 * process and we can return immediately. In this
718 * case, the lock is not taken to avoid races with
721 error = nfs_rcvlock(myrep);
722 if (error == EALREADY)
727 * Get the next Rpc reply off the socket
729 error = nfs_receive(myrep, &nam, &mrep);
730 nfs_rcvunlock(myrep);
734 * Ignore routing errors on connectionless protocols??
736 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
737 nmp->nm_so->so_error = 0;
738 if (myrep->r_flags & R_GETONEREP)
748 * Get the xid and check that it is an rpc reply
751 dpos = mtod(md, caddr_t);
752 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
754 if (*tl != rpc_reply) {
755 nfsstats.rpcinvalid++;
758 if (myrep->r_flags & R_GETONEREP)
764 * Loop through the request list to match up the reply
765 * Iff no match, just drop the datagram
767 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
768 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
774 * Update congestion window.
775 * Do the additive increase of
778 if (nmp->nm_cwnd <= nmp->nm_sent) {
780 (NFS_CWNDSCALE * NFS_CWNDSCALE +
781 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
782 if (nmp->nm_cwnd > NFS_MAXCWND)
783 nmp->nm_cwnd = NFS_MAXCWND;
785 if (rep->r_flags & R_SENT) {
786 rep->r_flags &= ~R_SENT;
787 nmp->nm_sent -= NFS_CWNDSCALE;
790 * Update rtt using a gain of 0.125 on the mean
791 * and a gain of 0.25 on the deviation.
793 if (rep->r_flags & R_TIMING) {
795 * Since the timer resolution of
796 * NFS_HZ is so course, it can often
797 * result in r_rtt == 0. Since
798 * r_rtt == N means that the actual
799 * rtt is between N+dt and N+2-dt ticks,
803 t1 -= (NFS_SRTT(rep) >> 3);
807 t1 -= (NFS_SDRTT(rep) >> 2);
808 NFS_SDRTT(rep) += t1;
810 nmp->nm_timeouts = 0;
815 * If not matched to a request, drop it.
816 * If it's mine, get out.
819 nfsstats.rpcunexpected++;
821 } else if (rep == myrep) {
822 if (rep->r_mrep == NULL)
823 panic("nfsreply nil");
826 if (myrep->r_flags & R_GETONEREP)
832 * nfs_request - goes something like this
833 * - fill in request struct
834 * - links it into list
835 * - calls nfs_send() for first transmit
836 * - calls nfs_receive() to get reply
837 * - break down rpc header and return with nfs reply pointed to
839 * nb: always frees up mreq mbuf list
841 /* XXX overloaded before */
842 #define NQ_TRYLATERDEL 15 /* Initial try later delay (sec) */
845 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
846 struct thread *td, struct ucred *cred, struct mbuf **mrp,
847 struct mbuf **mdp, caddr_t *dposp)
849 struct mbuf *mrep, *m2;
853 struct nfsmount *nmp;
854 struct mbuf *m, *md, *mheadend;
857 int s, error = 0, mrest_len, auth_len, auth_type;
858 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
861 /* Reject requests while attempting a forced unmount. */
862 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
866 nmp = VFSTONFS(vp->v_mount);
867 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
871 rep->r_procnum = procnum;
881 * Get the RPC header with authorization.
883 auth_type = RPCAUTH_UNIX;
884 if (cred->cr_ngroups < 1)
885 panic("nfsreq nogrps");
886 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
887 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
889 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
890 mrest, mrest_len, &mheadend, &xid);
893 * For stream protocols, insert a Sun RPC Record Mark.
895 if (nmp->nm_sotype == SOCK_STREAM) {
896 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
897 *mtod(m, u_int32_t *) = htonl(0x80000000 |
898 (m->m_pkthdr.len - NFSX_UNSIGNED));
903 if (nmp->nm_flag & NFSMNT_SOFT)
904 rep->r_retry = nmp->nm_retry;
906 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
907 rep->r_rtt = rep->r_rexmit = 0;
908 if (proct[procnum] > 0)
909 rep->r_flags = R_TIMING;
915 * Do the client side RPC.
917 nfsstats.rpcrequests++;
919 * Chain request into list of outstanding requests. Be sure
920 * to put it LAST so timer finds oldest requests first.
923 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
926 * If backing off another request or avoiding congestion, don't
927 * send this one now but let timer do it. If not timing a request,
930 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
931 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
932 nmp->nm_sent < nmp->nm_cwnd)) {
934 if (nmp->nm_soflags & PR_CONNREQUIRED)
935 error = nfs_sndlock(rep);
937 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
938 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
939 if (nmp->nm_soflags & PR_CONNREQUIRED)
942 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
943 nmp->nm_sent += NFS_CWNDSCALE;
944 rep->r_flags |= R_SENT;
952 * Wait for the reply from our send or the timer's.
954 if (!error || error == EPIPE)
955 error = nfs_reply(rep);
958 * RPC done, unlink the request.
961 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
965 * Decrement the outstanding request count.
967 if (rep->r_flags & R_SENT) {
968 rep->r_flags &= ~R_SENT; /* paranoia */
969 nmp->nm_sent -= NFS_CWNDSCALE;
973 * If there was a successful reply and a tprintf msg.
974 * tprintf a response.
976 if (!error && (rep->r_flags & R_TPRINTFMSG))
977 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
983 m_freem(rep->r_mreq);
984 free((caddr_t)rep, M_NFSREQ);
989 * break down the rpc header and check if ok
991 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
992 if (*tl++ == rpc_msgdenied) {
993 if (*tl == rpc_mismatch)
998 m_freem(rep->r_mreq);
999 free((caddr_t)rep, M_NFSREQ);
1004 * Just throw away any verifyer (ie: kerberos etc).
1006 i = fxdr_unsigned(int, *tl++); /* verf type */
1007 i = fxdr_unsigned(int32_t, *tl); /* len */
1009 nfsm_adv(nfsm_rndup(i));
1010 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1013 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1015 error = fxdr_unsigned(int, *tl);
1016 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1017 error == NFSERR_TRYLATER) {
1020 waituntil = time_second + trylater_delay;
1021 while (time_second < waituntil)
1022 (void) tsleep((caddr_t)&lbolt,
1023 PSOCK, "nqnfstry", 0);
1024 trylater_delay *= nfs_backoff[trylater_cnt];
1025 if (trylater_cnt < NFS_NBACKOFF - 1)
1031 * If the File Handle was stale, invalidate the
1032 * lookup cache, just in case.
1034 if (error == ESTALE)
1036 if (nmp->nm_flag & NFSMNT_NFSV3) {
1040 error |= NFSERR_RETERR;
1043 m_freem(rep->r_mreq);
1044 free((caddr_t)rep, M_NFSREQ);
1051 m_freem(rep->r_mreq);
1052 FREE((caddr_t)rep, M_NFSREQ);
1056 error = EPROTONOSUPPORT;
1058 m_freem(rep->r_mreq);
1059 free((caddr_t)rep, M_NFSREQ);
1065 * Scan the nfsreq list and retranmit any requests that have timed out
1066 * To avoid retransmission attempts on STREAM sockets (in the future) make
1067 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1070 nfs_timer(void *arg)
1075 struct nfsmount *nmp;
1080 td = &thread0; /* XXX for credentials, may break if sleep */
1082 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1084 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1086 if (nfs_sigintr(nmp, rep, rep->r_td)) {
1090 if (rep->r_rtt >= 0) {
1092 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1093 timeo = nmp->nm_timeo;
1095 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1096 if (nmp->nm_timeouts > 0)
1097 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1098 if (rep->r_rtt <= timeo)
1100 if (nmp->nm_timeouts < NFS_NBACKOFF)
1104 * Check for server not responding
1106 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1107 rep->r_rexmit > nmp->nm_deadthresh) {
1109 sprintf(buf, "not responding %d > %d",
1110 rep->r_rexmit, nmp->nm_deadthresh);
1112 nmp->nm_mountp->mnt_stat.f_mntfromname,
1113 buf /* "not responding" */);
1114 rep->r_flags |= R_TPRINTFMSG;
1116 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1117 nfsstats.rpctimeouts++;
1121 if (nmp->nm_sotype != SOCK_DGRAM) {
1122 if (++rep->r_rexmit > NFS_MAXREXMIT)
1123 rep->r_rexmit = NFS_MAXREXMIT;
1126 if ((so = nmp->nm_so) == NULL)
1130 * If there is enough space and the window allows..
1132 * Set r_rtt to -1 in case we fail to send it now.
1135 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1136 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1137 (rep->r_flags & R_SENT) ||
1138 nmp->nm_sent < nmp->nm_cwnd) &&
1139 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1140 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1141 error = (*so->so_proto->pr_usrreqs->pru_send)
1142 (so, 0, m, NULL, NULL, td);
1144 error = (*so->so_proto->pr_usrreqs->pru_send)
1145 (so, 0, m, nmp->nm_nam, NULL, td);
1147 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1151 * Iff first send, start timing
1152 * else turn timing off, backoff timer
1153 * and divide congestion window by 2.
1155 if (rep->r_flags & R_SENT) {
1156 rep->r_flags &= ~R_TIMING;
1157 if (++rep->r_rexmit > NFS_MAXREXMIT)
1158 rep->r_rexmit = NFS_MAXREXMIT;
1160 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1161 nmp->nm_cwnd = NFS_CWNDSCALE;
1162 nfsstats.rpcretries++;
1164 rep->r_flags |= R_SENT;
1165 nmp->nm_sent += NFS_CWNDSCALE;
1172 nfs_timer_handle = timeout(nfs_timer, NULL, nfs_ticks);
1176 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1177 * wait for all requests to complete. This is used by forced unmounts
1178 * to terminate any outstanding RPCs.
1181 nfs_nmcancelreqs(nmp)
1182 struct nfsmount *nmp;
1188 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1189 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1190 (req->r_flags & R_SOFTTERM))
1196 for (i = 0; i < 30; i++) {
1198 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1199 if (nmp == req->r_nmp)
1205 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1211 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1212 * The nm_send count is decremented now to avoid deadlocks when the process in
1213 * soreceive() hasn't yet managed to send its own request.
1217 nfs_softterm(struct nfsreq *rep)
1220 rep->r_flags |= R_SOFTTERM;
1221 if (rep->r_flags & R_SENT) {
1222 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1223 rep->r_flags &= ~R_SENT;
1228 * Test for a termination condition pending on the process.
1229 * This is used for NFSMNT_INT mounts.
1232 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1237 if (rep && (rep->r_flags & R_SOFTTERM))
1239 /* Terminate all requests while attempting a forced unmount. */
1240 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1242 if (!(nmp->nm_flag & NFSMNT_INT))
1248 tmpset = p->p_siglist;
1249 SIGSETNAND(tmpset, p->p_sigmask);
1250 SIGSETNAND(tmpset, p->p_sigignore);
1251 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1258 * Lock a socket against others.
1259 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1260 * and also to avoid race conditions between the processes with nfs requests
1261 * in progress when a reconnect is necessary.
1264 nfs_sndlock(struct nfsreq *rep)
1266 int *statep = &rep->r_nmp->nm_state;
1268 int slpflag = 0, slptimeo = 0;
1272 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1276 while (*statep & NFSSTA_SNDLOCK) {
1277 if (nfs_sigintr(rep->r_nmp, rep, td))
1279 *statep |= NFSSTA_WANTSND;
1280 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1),
1281 "nfsndlck", slptimeo);
1282 if (slpflag == PCATCH) {
1287 *statep |= NFSSTA_SNDLOCK;
1292 * Unlock the stream socket for others.
1295 nfs_sndunlock(struct nfsreq *rep)
1297 int *statep = &rep->r_nmp->nm_state;
1299 if ((*statep & NFSSTA_SNDLOCK) == 0)
1300 panic("nfs sndunlock");
1301 *statep &= ~NFSSTA_SNDLOCK;
1302 if (*statep & NFSSTA_WANTSND) {
1303 *statep &= ~NFSSTA_WANTSND;
1304 wakeup((caddr_t)statep);
1309 nfs_rcvlock(struct nfsreq *rep)
1311 int *statep = &rep->r_nmp->nm_state;
1312 int slpflag, slptimeo = 0;
1314 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1318 while (*statep & NFSSTA_RCVLOCK) {
1319 if (nfs_sigintr(rep->r_nmp, rep, rep->r_td))
1321 *statep |= NFSSTA_WANTRCV;
1322 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1325 * If our reply was recieved while we were sleeping,
1326 * then just return without taking the lock to avoid a
1327 * situation where a single iod could 'capture' the
1330 if (rep->r_mrep != NULL)
1332 if (slpflag == PCATCH) {
1337 /* Always fail if our request has been cancelled. */
1338 if (rep != NULL && (rep->r_flags & R_SOFTTERM))
1340 *statep |= NFSSTA_RCVLOCK;
1345 * Unlock the stream socket for others.
1348 nfs_rcvunlock(struct nfsreq *rep)
1350 int *statep = &rep->r_nmp->nm_state;
1352 if ((*statep & NFSSTA_RCVLOCK) == 0)
1353 panic("nfs rcvunlock");
1354 *statep &= ~NFSSTA_RCVLOCK;
1355 if (*statep & NFSSTA_WANTRCV) {
1356 *statep &= ~NFSSTA_WANTRCV;
1357 wakeup((caddr_t)statep);
1364 * Check for badly aligned mbuf data and realign by copying the unaligned
1365 * portion of the data into a new mbuf chain and freeing the portions
1366 * of the old chain that were replaced.
1368 * We cannot simply realign the data within the existing mbuf chain
1369 * because the underlying buffers may contain other rpc commands and
1370 * we cannot afford to overwrite them.
1372 * We would prefer to avoid this situation entirely. The situation does
1373 * not occur with NFS/UDP and is supposed to only occassionally occur
1374 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1377 nfs_realign(struct mbuf **pm, int hsiz)
1380 struct mbuf *n = NULL;
1384 while ((m = *pm) != NULL) {
1385 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1386 MGET(n, M_TRYWAIT, MT_DATA);
1387 if (m->m_len >= MINCLSIZE) {
1388 MCLGET(n, M_TRYWAIT);
1396 * If n is non-NULL, loop on m copying data, then replace the
1397 * portion of the chain that had to be realigned.
1400 ++nfs_realign_count;
1402 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1413 nfs_msg(struct thread *td, char *server, char *msg)
1416 tprintf(td ? td->td_proc : NULL, LOG_INFO,
1417 "nfs server %s: %s\n", server, msg);