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 <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
55 #include <sys/protosw.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sysctl.h>
60 #include <sys/syslog.h>
61 #include <sys/vnode.h>
63 #include <netinet/in.h>
64 #include <netinet/tcp.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>
78 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
79 * Use the mean and mean deviation of rtt for the appropriate type of rpc
80 * for the frequent rpcs and a default for the others.
81 * The justification for doing "other" this way is that these rpcs
82 * happen so infrequently that timer est. would probably be stale.
83 * Also, since many of these rpcs are
84 * non-idempotent, a conservative timeout is desired.
85 * getattr, lookup - A+2D
89 #define NFS_RTO(n, t) \
90 ((t) == 0 ? (n)->nm_timeo : \
92 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
93 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
94 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
95 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
98 * Defines which timer to use for the procnum.
105 static int proct[NFS_NPROCS] = {
106 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
109 static int nfs_realign_test;
110 static int nfs_realign_count;
111 static int nfs_bufpackets = 4;
113 SYSCTL_DECL(_vfs_nfs);
115 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
116 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
117 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
121 * There is a congestion window for outstanding rpcs maintained per mount
122 * point. The cwnd size is adjusted in roughly the way that:
123 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
124 * SIGCOMM '88". ACM, August 1988.
125 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
126 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
127 * of rpcs is in progress.
128 * (The sent count and cwnd are scaled for integer arith.)
129 * Variants of "slow start" were tried and were found to be too much of a
130 * performance hit (ave. rtt 3 times larger),
131 * I suspect due to the large rtt that nfs rpcs have.
133 #define NFS_CWNDSCALE 256
134 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
135 #define NFS_NBACKOFF 8
136 static int nfs_backoff[NFS_NBACKOFF] = { 2, 4, 8, 16, 32, 64, 128, 256, };
137 struct callout_handle nfs_timer_handle;
139 static int nfs_msg(struct thread *, char *, char *);
140 static int nfs_rcvlock(struct nfsreq *);
141 static void nfs_rcvunlock(struct nfsreq *);
142 static void nfs_realign(struct mbuf **pm, int hsiz);
143 static int nfs_receive(struct nfsreq *rep, struct sockaddr **aname,
145 static int nfs_reply(struct nfsreq *);
146 static void nfs_softterm(struct nfsreq *rep);
147 static int nfs_reconnect(struct nfsreq *rep);
150 * Initialize sockets and congestion for a new NFS connection.
151 * We do not free the sockaddr if error.
154 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
157 int s, error, rcvreserve, sndreserve;
159 struct sockaddr *saddr;
160 struct sockaddr_in *sin;
161 struct thread *td = &thread0; /* only used for socreate and sobind */
163 nmp->nm_so = (struct socket *)0;
165 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
166 nmp->nm_soproto, nmp->nm_cred, td);
170 nmp->nm_soflags = so->so_proto->pr_flags;
173 * Some servers require that the client port be a reserved port number.
175 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
178 struct sockaddr_in ssin;
180 bzero(&sopt, sizeof sopt);
181 ip = IP_PORTRANGE_LOW;
182 sopt.sopt_dir = SOPT_SET;
183 sopt.sopt_level = IPPROTO_IP;
184 sopt.sopt_name = IP_PORTRANGE;
185 sopt.sopt_val = (void *)&ip;
186 sopt.sopt_valsize = sizeof(ip);
188 error = sosetopt(so, &sopt);
191 bzero(&ssin, sizeof ssin);
193 sin->sin_len = sizeof (struct sockaddr_in);
194 sin->sin_family = AF_INET;
195 sin->sin_addr.s_addr = INADDR_ANY;
196 sin->sin_port = htons(0);
197 error = sobind(so, (struct sockaddr *)sin, td);
200 bzero(&sopt, sizeof sopt);
201 ip = IP_PORTRANGE_DEFAULT;
202 sopt.sopt_dir = SOPT_SET;
203 sopt.sopt_level = IPPROTO_IP;
204 sopt.sopt_name = IP_PORTRANGE;
205 sopt.sopt_val = (void *)&ip;
206 sopt.sopt_valsize = sizeof(ip);
208 error = sosetopt(so, &sopt);
214 * Protocols that do not require connections may be optionally left
215 * unconnected for servers that reply from a port other than NFS_PORT.
217 if (nmp->nm_flag & NFSMNT_NOCONN) {
218 if (nmp->nm_soflags & PR_CONNREQUIRED) {
223 error = soconnect(so, nmp->nm_nam, td);
228 * Wait for the connection to complete. Cribbed from the
229 * connect system call but with the wait timing out so
230 * that interruptible mounts don't hang here for a long time.
233 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
234 (void) tsleep((caddr_t)&so->so_timeo,
235 PSOCK, "nfscon", 2 * hz);
236 if ((so->so_state & SS_ISCONNECTING) &&
237 so->so_error == 0 && rep &&
238 (error = nfs_sigintr(nmp, rep,
239 (rep->r_td ? rep->r_td->td_proc : NULL))) != 0){
240 so->so_state &= ~SS_ISCONNECTING;
246 error = so->so_error;
253 so->so_rcv.sb_timeo = 5 * hz;
254 so->so_snd.sb_timeo = 5 * hz;
257 * Get buffer reservation size from sysctl, but impose reasonable
260 pktscale = nfs_bufpackets;
266 if (nmp->nm_sotype == SOCK_DGRAM) {
267 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
268 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
269 NFS_MAXPKTHDR) * pktscale;
270 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
271 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
272 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
273 NFS_MAXPKTHDR) * pktscale;
275 if (nmp->nm_sotype != SOCK_STREAM)
276 panic("nfscon sotype");
277 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
281 bzero(&sopt, sizeof sopt);
282 sopt.sopt_level = SOL_SOCKET;
283 sopt.sopt_name = SO_KEEPALIVE;
284 sopt.sopt_val = &val;
285 sopt.sopt_valsize = sizeof val;
289 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
293 bzero(&sopt, sizeof sopt);
294 sopt.sopt_level = IPPROTO_TCP;
295 sopt.sopt_name = TCP_NODELAY;
296 sopt.sopt_val = &val;
297 sopt.sopt_valsize = sizeof val;
301 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
302 sizeof (u_int32_t)) * pktscale;
303 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
304 sizeof (u_int32_t)) * pktscale;
306 error = soreserve(so, sndreserve, rcvreserve);
309 so->so_rcv.sb_flags |= SB_NOINTR;
310 so->so_snd.sb_flags |= SB_NOINTR;
312 /* Initialize other non-zero congestion variables */
313 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
314 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
315 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
316 nmp->nm_sdrtt[3] = 0;
317 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
319 nmp->nm_timeouts = 0;
329 * Called when a connection is broken on a reliable protocol.
330 * - clean up the old socket
331 * - nfs_connect() again
332 * - set R_MUSTRESEND for all outstanding requests on mount point
333 * If this fails the mount point is DEAD!
334 * nb: Must be called with the nfs_sndlock() set on the mount point.
337 nfs_reconnect(struct nfsreq *rep)
340 struct nfsmount *nmp = rep->r_nmp;
344 while ((error = nfs_connect(nmp, rep)) != 0) {
345 if (error == EINTR || error == ERESTART)
347 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
351 * Loop through outstanding request list and fix up all requests
354 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
355 if (rp->r_nmp == nmp)
356 rp->r_flags |= R_MUSTRESEND;
362 * NFS disconnect. Clean up and unlink.
365 nfs_disconnect(struct nfsmount *nmp)
371 nmp->nm_so = (struct socket *)0;
378 nfs_safedisconnect(struct nfsmount *nmp)
380 struct nfsreq dummyreq;
382 bzero(&dummyreq, sizeof(dummyreq));
383 dummyreq.r_nmp = nmp;
384 nfs_rcvlock(&dummyreq);
386 nfs_rcvunlock(&dummyreq);
390 * This is the nfs send routine. For connection based socket types, it
391 * must be called with an nfs_sndlock() on the socket.
392 * - return EINTR if the RPC is terminated, 0 otherwise
393 * - set R_MUSTRESEND if the send fails for any reason
394 * - do any cleanup required by recoverable socket errors (?)
397 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
400 struct sockaddr *sendnam;
401 int error, soflags, flags;
403 KASSERT(rep, ("nfs_send: called with rep == NULL"));
405 if (rep->r_flags & R_SOFTTERM) {
409 if ((so = rep->r_nmp->nm_so) == NULL) {
410 rep->r_flags |= R_MUSTRESEND;
414 rep->r_flags &= ~R_MUSTRESEND;
415 soflags = rep->r_nmp->nm_soflags;
417 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
418 sendnam = (struct sockaddr *)0;
421 if (so->so_type == SOCK_SEQPACKET)
426 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
427 flags, curthread /*XXX*/);
428 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
430 rep->r_flags |= R_MUSTRESEND;
434 log(LOG_INFO, "nfs send error %d for server %s\n", error,
435 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
437 * Deal with errors for the client side.
439 if (rep->r_flags & R_SOFTTERM)
442 rep->r_flags |= R_MUSTRESEND;
445 * Handle any recoverable (soft) socket errors here. (?)
447 if (error != EINTR && error != ERESTART &&
448 error != EWOULDBLOCK && error != EPIPE)
455 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
456 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
457 * Mark and consolidate the data into a new mbuf list.
458 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
460 * For SOCK_STREAM we must be very careful to read an entire record once
461 * we have read any of it, even if the system call has been interrupted.
464 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
470 struct mbuf *control;
472 struct sockaddr **getnam;
473 int error, sotype, rcvflg;
474 struct thread *td = curthread; /* XXX */
477 * Set up arguments for soreceive()
479 *mp = (struct mbuf *)0;
480 *aname = (struct sockaddr *)0;
481 sotype = rep->r_nmp->nm_sotype;
484 * For reliable protocols, lock against other senders/receivers
485 * in case a reconnect is necessary.
486 * For SOCK_STREAM, first get the Record Mark to find out how much
487 * more there is to get.
488 * We must lock the socket against other receivers
489 * until we have an entire rpc request/reply.
491 if (sotype != SOCK_DGRAM) {
492 error = nfs_sndlock(rep);
497 * Check for fatal errors and resending request.
500 * Ugh: If a reconnect attempt just happened, nm_so
501 * would have changed. NULL indicates a failed
502 * attempt that has essentially shut down this
505 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
509 so = rep->r_nmp->nm_so;
511 error = nfs_reconnect(rep);
518 while (rep->r_flags & R_MUSTRESEND) {
519 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
520 nfsstats.rpcretries++;
521 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
523 if (error == EINTR || error == ERESTART ||
524 (error = nfs_reconnect(rep)) != 0) {
532 if (sotype == SOCK_STREAM) {
533 aio.iov_base = (caddr_t) &len;
534 aio.iov_len = sizeof(u_int32_t);
537 auio.uio_segflg = UIO_SYSSPACE;
538 auio.uio_rw = UIO_READ;
540 auio.uio_resid = sizeof(u_int32_t);
543 rcvflg = MSG_WAITALL;
544 error = so->so_proto->pr_usrreqs->pru_soreceive
545 (so, (struct sockaddr **)0, &auio,
546 (struct mbuf **)0, (struct mbuf **)0,
548 if (error == EWOULDBLOCK && rep) {
549 if (rep->r_flags & R_SOFTTERM)
552 } while (error == EWOULDBLOCK);
553 if (!error && auio.uio_resid > 0) {
555 * Don't log a 0 byte receive; it means
556 * that the socket has been closed, and
557 * can happen during normal operation
558 * (forcible unmount or Solaris server).
560 if (auio.uio_resid != sizeof (u_int32_t))
562 "short receive (%d/%d) from nfs server %s\n",
563 (int)(sizeof(u_int32_t) - auio.uio_resid),
564 (int)sizeof(u_int32_t),
565 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
570 len = ntohl(len) & ~0x80000000;
572 * This is SERIOUS! We are out of sync with the sender
573 * and forcing a disconnect/reconnect is all I can do.
575 if (len > NFS_MAXPACKET) {
576 log(LOG_ERR, "%s (%d) from nfs server %s\n",
577 "impossible packet length",
579 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
583 auio.uio_resid = len;
585 rcvflg = MSG_WAITALL;
586 error = so->so_proto->pr_usrreqs->pru_soreceive
587 (so, (struct sockaddr **)0,
588 &auio, mp, (struct mbuf **)0, &rcvflg);
589 } while (error == EWOULDBLOCK || error == EINTR ||
591 if (!error && auio.uio_resid > 0) {
592 if (len != auio.uio_resid)
594 "short receive (%d/%d) from nfs server %s\n",
595 len - auio.uio_resid, len,
596 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
601 * NB: Since uio_resid is big, MSG_WAITALL is ignored
602 * and soreceive() will return when it has either a
603 * control msg or a data msg.
604 * We have no use for control msg., but must grab them
605 * and then throw them away so we know what is going
608 auio.uio_resid = len = 100000000; /* Anything Big */
612 error = so->so_proto->pr_usrreqs->pru_soreceive
613 (so, (struct sockaddr **)0,
614 &auio, mp, &control, &rcvflg);
617 if (error == EWOULDBLOCK && rep) {
618 if (rep->r_flags & R_SOFTTERM)
621 } while (error == EWOULDBLOCK ||
622 (!error && *mp == NULL && control));
623 if ((rcvflg & MSG_EOR) == 0)
625 if (!error && *mp == NULL)
627 len -= auio.uio_resid;
630 if (error && error != EINTR && error != ERESTART) {
632 *mp = (struct mbuf *)0;
635 "receive error %d from nfs server %s\n",
637 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
638 error = nfs_sndlock(rep);
640 error = nfs_reconnect(rep);
647 if ((so = rep->r_nmp->nm_so) == NULL)
649 if (so->so_state & SS_ISCONNECTED)
650 getnam = (struct sockaddr **)0;
653 auio.uio_resid = len = 1000000;
657 error = so->so_proto->pr_usrreqs->pru_soreceive
658 (so, getnam, &auio, mp,
659 (struct mbuf **)0, &rcvflg);
660 if (error == EWOULDBLOCK &&
661 (rep->r_flags & R_SOFTTERM))
663 } while (error == EWOULDBLOCK);
664 len -= auio.uio_resid;
668 *mp = (struct mbuf *)0;
671 * Search for any mbufs that are not a multiple of 4 bytes long
672 * or with m_data not longword aligned.
673 * These could cause pointer alignment problems, so copy them to
674 * well aligned mbufs.
676 nfs_realign(mp, 5 * NFSX_UNSIGNED);
681 * Implement receipt of reply on a socket.
682 * We must search through the list of received datagrams matching them
683 * with outstanding requests using the xid, until ours is found.
687 nfs_reply(struct nfsreq *myrep)
690 struct nfsmount *nmp = myrep->r_nmp;
692 struct mbuf *mrep, *md;
693 struct sockaddr *nam;
699 * Loop around until we get our own reply
703 * Lock against other receivers so that I don't get stuck in
704 * sbwait() after someone else has received my reply for me.
705 * Also necessary for connection based protocols to avoid
706 * race conditions during a reconnect.
707 * If nfs_rcvlock() returns EALREADY, that means that
708 * the reply has already been recieved by another
709 * process and we can return immediately. In this
710 * case, the lock is not taken to avoid races with
713 error = nfs_rcvlock(myrep);
714 if (error == EALREADY)
719 * Get the next Rpc reply off the socket
721 error = nfs_receive(myrep, &nam, &mrep);
722 nfs_rcvunlock(myrep);
726 * Ignore routing errors on connectionless protocols??
728 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
729 nmp->nm_so->so_error = 0;
730 if (myrep->r_flags & R_GETONEREP)
740 * Get the xid and check that it is an rpc reply
743 dpos = mtod(md, caddr_t);
744 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
746 if (*tl != rpc_reply) {
747 nfsstats.rpcinvalid++;
750 if (myrep->r_flags & R_GETONEREP)
756 * Loop through the request list to match up the reply
757 * Iff no match, just drop the datagram
759 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
760 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
766 * Update congestion window.
767 * Do the additive increase of
770 if (nmp->nm_cwnd <= nmp->nm_sent) {
772 (NFS_CWNDSCALE * NFS_CWNDSCALE +
773 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
774 if (nmp->nm_cwnd > NFS_MAXCWND)
775 nmp->nm_cwnd = NFS_MAXCWND;
777 if (rep->r_flags & R_SENT) {
778 rep->r_flags &= ~R_SENT;
779 nmp->nm_sent -= NFS_CWNDSCALE;
782 * Update rtt using a gain of 0.125 on the mean
783 * and a gain of 0.25 on the deviation.
785 if (rep->r_flags & R_TIMING) {
787 * Since the timer resolution of
788 * NFS_HZ is so course, it can often
789 * result in r_rtt == 0. Since
790 * r_rtt == N means that the actual
791 * rtt is between N+dt and N+2-dt ticks,
795 t1 -= (NFS_SRTT(rep) >> 3);
799 t1 -= (NFS_SDRTT(rep) >> 2);
800 NFS_SDRTT(rep) += t1;
802 nmp->nm_timeouts = 0;
807 * If not matched to a request, drop it.
808 * If it's mine, get out.
811 nfsstats.rpcunexpected++;
813 } else if (rep == myrep) {
814 if (rep->r_mrep == NULL)
815 panic("nfsreply nil");
818 if (myrep->r_flags & R_GETONEREP)
824 * nfs_request - goes something like this
825 * - fill in request struct
826 * - links it into list
827 * - calls nfs_send() for first transmit
828 * - calls nfs_receive() to get reply
829 * - break down rpc header and return with nfs reply pointed to
831 * nb: always frees up mreq mbuf list
833 /* XXX overloaded before */
834 #define NQ_TRYLATERDEL 15 /* Initial try later delay (sec) */
837 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
838 struct thread *td, struct ucred *cred, struct mbuf **mrp,
839 struct mbuf **mdp, caddr_t *dposp)
841 struct mbuf *mrep, *m2;
845 struct nfsmount *nmp;
846 struct mbuf *m, *md, *mheadend;
849 int s, error = 0, mrest_len, auth_len, auth_type;
850 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
853 /* Reject requests while attempting a forced unmount. */
854 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
858 nmp = VFSTONFS(vp->v_mount);
859 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
863 rep->r_procnum = procnum;
873 * Get the RPC header with authorization.
875 auth_type = RPCAUTH_UNIX;
876 if (cred->cr_ngroups < 1)
877 panic("nfsreq nogrps");
878 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
879 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
881 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
882 mrest, mrest_len, &mheadend, &xid);
885 * For stream protocols, insert a Sun RPC Record Mark.
887 if (nmp->nm_sotype == SOCK_STREAM) {
888 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
889 *mtod(m, u_int32_t *) = htonl(0x80000000 |
890 (m->m_pkthdr.len - NFSX_UNSIGNED));
895 if (nmp->nm_flag & NFSMNT_SOFT)
896 rep->r_retry = nmp->nm_retry;
898 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
899 rep->r_rtt = rep->r_rexmit = 0;
900 if (proct[procnum] > 0)
901 rep->r_flags = R_TIMING;
907 * Do the client side RPC.
909 nfsstats.rpcrequests++;
911 * Chain request into list of outstanding requests. Be sure
912 * to put it LAST so timer finds oldest requests first.
915 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
918 * If backing off another request or avoiding congestion, don't
919 * send this one now but let timer do it. If not timing a request,
922 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
923 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
924 nmp->nm_sent < nmp->nm_cwnd)) {
926 if (nmp->nm_soflags & PR_CONNREQUIRED)
927 error = nfs_sndlock(rep);
929 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
930 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
931 if (nmp->nm_soflags & PR_CONNREQUIRED)
934 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
935 nmp->nm_sent += NFS_CWNDSCALE;
936 rep->r_flags |= R_SENT;
944 * Wait for the reply from our send or the timer's.
946 if (!error || error == EPIPE)
947 error = nfs_reply(rep);
950 * RPC done, unlink the request.
953 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
957 * Decrement the outstanding request count.
959 if (rep->r_flags & R_SENT) {
960 rep->r_flags &= ~R_SENT; /* paranoia */
961 nmp->nm_sent -= NFS_CWNDSCALE;
965 * If there was a successful reply and a tprintf msg.
966 * tprintf a response.
968 if (!error && (rep->r_flags & R_TPRINTFMSG))
969 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
975 m_freem(rep->r_mreq);
976 free((caddr_t)rep, M_NFSREQ);
981 * break down the rpc header and check if ok
983 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
984 if (*tl++ == rpc_msgdenied) {
985 if (*tl == rpc_mismatch)
990 m_freem(rep->r_mreq);
991 free((caddr_t)rep, M_NFSREQ);
996 * Just throw away any verifyer (ie: kerberos etc).
998 i = fxdr_unsigned(int, *tl++); /* verf type */
999 i = fxdr_unsigned(int32_t, *tl); /* len */
1001 nfsm_adv(nfsm_rndup(i));
1002 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1005 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1007 error = fxdr_unsigned(int, *tl);
1008 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1009 error == NFSERR_TRYLATER) {
1012 waituntil = time_second + trylater_delay;
1013 while (time_second < waituntil)
1014 (void) tsleep((caddr_t)&lbolt,
1015 PSOCK, "nqnfstry", 0);
1016 trylater_delay *= nfs_backoff[trylater_cnt];
1017 if (trylater_cnt < NFS_NBACKOFF - 1)
1023 * If the File Handle was stale, invalidate the
1024 * lookup cache, just in case.
1026 if (error == ESTALE)
1028 if (nmp->nm_flag & NFSMNT_NFSV3) {
1032 error |= NFSERR_RETERR;
1035 m_freem(rep->r_mreq);
1036 free((caddr_t)rep, M_NFSREQ);
1043 m_freem(rep->r_mreq);
1044 FREE((caddr_t)rep, M_NFSREQ);
1048 error = EPROTONOSUPPORT;
1050 m_freem(rep->r_mreq);
1051 free((caddr_t)rep, M_NFSREQ);
1057 * Scan the nfsreq list and retranmit any requests that have timed out
1058 * To avoid retransmission attempts on STREAM sockets (in the future) make
1059 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1062 nfs_timer(void *arg)
1067 struct nfsmount *nmp;
1072 td = &thread0; /* XXX for credentials, may break if sleep */
1074 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1076 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1078 if (nfs_sigintr(nmp, rep,
1079 (rep->r_td ? rep->r_td->td_proc : NULL))) {
1083 if (rep->r_rtt >= 0) {
1085 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1086 timeo = nmp->nm_timeo;
1088 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1089 if (nmp->nm_timeouts > 0)
1090 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1091 if (rep->r_rtt <= timeo)
1093 if (nmp->nm_timeouts < NFS_NBACKOFF)
1097 * Check for server not responding
1099 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1100 rep->r_rexmit > nmp->nm_deadthresh) {
1102 sprintf(buf, "not responding %d > %d",
1103 rep->r_rexmit, nmp->nm_deadthresh);
1105 nmp->nm_mountp->mnt_stat.f_mntfromname,
1106 buf /* "not responding" */);
1107 rep->r_flags |= R_TPRINTFMSG;
1109 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1110 nfsstats.rpctimeouts++;
1114 if (nmp->nm_sotype != SOCK_DGRAM) {
1115 if (++rep->r_rexmit > NFS_MAXREXMIT)
1116 rep->r_rexmit = NFS_MAXREXMIT;
1119 if ((so = nmp->nm_so) == NULL)
1123 * If there is enough space and the window allows..
1125 * Set r_rtt to -1 in case we fail to send it now.
1128 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1129 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1130 (rep->r_flags & R_SENT) ||
1131 nmp->nm_sent < nmp->nm_cwnd) &&
1132 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1133 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1134 error = (*so->so_proto->pr_usrreqs->pru_send)
1135 (so, 0, m, (struct sockaddr *)0,
1136 (struct mbuf *)0, td);
1138 error = (*so->so_proto->pr_usrreqs->pru_send)
1139 (so, 0, m, nmp->nm_nam, (struct mbuf *)0,
1142 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1146 * Iff first send, start timing
1147 * else turn timing off, backoff timer
1148 * and divide congestion window by 2.
1150 if (rep->r_flags & R_SENT) {
1151 rep->r_flags &= ~R_TIMING;
1152 if (++rep->r_rexmit > NFS_MAXREXMIT)
1153 rep->r_rexmit = NFS_MAXREXMIT;
1155 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1156 nmp->nm_cwnd = NFS_CWNDSCALE;
1157 nfsstats.rpcretries++;
1159 rep->r_flags |= R_SENT;
1160 nmp->nm_sent += NFS_CWNDSCALE;
1167 nfs_timer_handle = timeout(nfs_timer, (void *)0, nfs_ticks);
1171 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1172 * wait for all requests to complete. This is used by forced unmounts
1173 * to terminate any outstanding RPCs.
1176 nfs_nmcancelreqs(nmp)
1177 struct nfsmount *nmp;
1183 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1184 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1185 (req->r_flags & R_SOFTTERM))
1191 for (i = 0; i < 30; i++) {
1193 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1194 if (nmp == req->r_nmp)
1200 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1206 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1207 * The nm_send count is decremented now to avoid deadlocks when the process in
1208 * soreceive() hasn't yet managed to send its own request.
1212 nfs_softterm(struct nfsreq *rep)
1215 rep->r_flags |= R_SOFTTERM;
1216 if (rep->r_flags & R_SENT) {
1217 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1218 rep->r_flags &= ~R_SENT;
1223 * Test for a termination condition pending on the process.
1224 * This is used for NFSMNT_INT mounts.
1227 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct proc *p)
1231 if (rep && (rep->r_flags & R_SOFTTERM))
1233 /* Terminate all requests while attempting a forced unmount. */
1234 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1236 if (!(nmp->nm_flag & NFSMNT_INT))
1241 tmpset = p->p_siglist;
1242 SIGSETNAND(tmpset, p->p_sigmask);
1243 SIGSETNAND(tmpset, p->p_sigignore);
1244 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1251 * Lock a socket against others.
1252 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1253 * and also to avoid race conditions between the processes with nfs requests
1254 * in progress when a reconnect is necessary.
1257 nfs_sndlock(struct nfsreq *rep)
1259 int *statep = &rep->r_nmp->nm_state;
1261 int slpflag = 0, slptimeo = 0;
1265 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1268 td = (struct thread *)0;
1269 while (*statep & NFSSTA_SNDLOCK) {
1270 if (nfs_sigintr(rep->r_nmp, rep, td ? td->td_proc : NULL))
1272 *statep |= NFSSTA_WANTSND;
1273 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1),
1274 "nfsndlck", slptimeo);
1275 if (slpflag == PCATCH) {
1280 *statep |= NFSSTA_SNDLOCK;
1285 * Unlock the stream socket for others.
1288 nfs_sndunlock(struct nfsreq *rep)
1290 int *statep = &rep->r_nmp->nm_state;
1292 if ((*statep & NFSSTA_SNDLOCK) == 0)
1293 panic("nfs sndunlock");
1294 *statep &= ~NFSSTA_SNDLOCK;
1295 if (*statep & NFSSTA_WANTSND) {
1296 *statep &= ~NFSSTA_WANTSND;
1297 wakeup((caddr_t)statep);
1302 nfs_rcvlock(struct nfsreq *rep)
1304 int *statep = &rep->r_nmp->nm_state;
1305 int slpflag, slptimeo = 0;
1307 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1311 while (*statep & NFSSTA_RCVLOCK) {
1312 if (nfs_sigintr(rep->r_nmp, rep,
1313 (rep->r_td ? rep->r_td->td_proc : NULL)))
1315 *statep |= NFSSTA_WANTRCV;
1316 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1319 * If our reply was recieved while we were sleeping,
1320 * then just return without taking the lock to avoid a
1321 * situation where a single iod could 'capture' the
1324 if (rep->r_mrep != NULL)
1326 if (slpflag == PCATCH) {
1331 /* Always fail if our request has been cancelled. */
1332 if (rep != NULL && (rep->r_flags & R_SOFTTERM))
1334 *statep |= NFSSTA_RCVLOCK;
1339 * Unlock the stream socket for others.
1342 nfs_rcvunlock(struct nfsreq *rep)
1344 int *statep = &rep->r_nmp->nm_state;
1346 if ((*statep & NFSSTA_RCVLOCK) == 0)
1347 panic("nfs rcvunlock");
1348 *statep &= ~NFSSTA_RCVLOCK;
1349 if (*statep & NFSSTA_WANTRCV) {
1350 *statep &= ~NFSSTA_WANTRCV;
1351 wakeup((caddr_t)statep);
1358 * Check for badly aligned mbuf data and realign by copying the unaligned
1359 * portion of the data into a new mbuf chain and freeing the portions
1360 * of the old chain that were replaced.
1362 * We cannot simply realign the data within the existing mbuf chain
1363 * because the underlying buffers may contain other rpc commands and
1364 * we cannot afford to overwrite them.
1366 * We would prefer to avoid this situation entirely. The situation does
1367 * not occur with NFS/UDP and is supposed to only occassionally occur
1368 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1371 nfs_realign(struct mbuf **pm, int hsiz)
1374 struct mbuf *n = NULL;
1378 while ((m = *pm) != NULL) {
1379 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1380 MGET(n, M_TRYWAIT, MT_DATA);
1381 if (m->m_len >= MINCLSIZE) {
1382 MCLGET(n, M_TRYWAIT);
1390 * If n is non-NULL, loop on m copying data, then replace the
1391 * portion of the chain that had to be realigned.
1394 ++nfs_realign_count;
1396 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1407 nfs_msg(struct thread *td, char *server, char *msg)
1410 tprintf(td ? td->td_proc : NULL, LOG_INFO,
1411 "nfs server %s: %s\n", server, msg);