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, rep->r_td)) != 0) {
239 so->so_state &= ~SS_ISCONNECTING;
245 error = so->so_error;
252 so->so_rcv.sb_timeo = 5 * hz;
253 so->so_snd.sb_timeo = 5 * hz;
256 * Get buffer reservation size from sysctl, but impose reasonable
259 pktscale = nfs_bufpackets;
265 if (nmp->nm_sotype == SOCK_DGRAM) {
266 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
267 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
268 NFS_MAXPKTHDR) * pktscale;
269 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
270 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
271 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
272 NFS_MAXPKTHDR) * pktscale;
274 if (nmp->nm_sotype != SOCK_STREAM)
275 panic("nfscon sotype");
276 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
280 bzero(&sopt, sizeof sopt);
281 sopt.sopt_level = SOL_SOCKET;
282 sopt.sopt_name = SO_KEEPALIVE;
283 sopt.sopt_val = &val;
284 sopt.sopt_valsize = sizeof val;
288 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
292 bzero(&sopt, sizeof sopt);
293 sopt.sopt_level = IPPROTO_TCP;
294 sopt.sopt_name = TCP_NODELAY;
295 sopt.sopt_val = &val;
296 sopt.sopt_valsize = sizeof val;
300 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
301 sizeof (u_int32_t)) * pktscale;
302 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
303 sizeof (u_int32_t)) * pktscale;
305 error = soreserve(so, sndreserve, rcvreserve);
308 so->so_rcv.sb_flags |= SB_NOINTR;
309 so->so_snd.sb_flags |= SB_NOINTR;
311 /* Initialize other non-zero congestion variables */
312 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
313 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
314 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
315 nmp->nm_sdrtt[3] = 0;
316 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
318 nmp->nm_timeouts = 0;
328 * Called when a connection is broken on a reliable protocol.
329 * - clean up the old socket
330 * - nfs_connect() again
331 * - set R_MUSTRESEND for all outstanding requests on mount point
332 * If this fails the mount point is DEAD!
333 * nb: Must be called with the nfs_sndlock() set on the mount point.
336 nfs_reconnect(struct nfsreq *rep)
339 struct nfsmount *nmp = rep->r_nmp;
343 while ((error = nfs_connect(nmp, rep)) != 0) {
344 if (error == EINTR || error == ERESTART)
346 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
350 * Loop through outstanding request list and fix up all requests
353 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
354 if (rp->r_nmp == nmp)
355 rp->r_flags |= R_MUSTRESEND;
361 * NFS disconnect. Clean up and unlink.
364 nfs_disconnect(struct nfsmount *nmp)
370 nmp->nm_so = (struct socket *)0;
377 nfs_safedisconnect(struct nfsmount *nmp)
379 struct nfsreq dummyreq;
381 bzero(&dummyreq, sizeof(dummyreq));
382 dummyreq.r_nmp = nmp;
383 nfs_rcvlock(&dummyreq);
385 nfs_rcvunlock(&dummyreq);
389 * This is the nfs send routine. For connection based socket types, it
390 * must be called with an nfs_sndlock() on the socket.
391 * - return EINTR if the RPC is terminated, 0 otherwise
392 * - set R_MUSTRESEND if the send fails for any reason
393 * - do any cleanup required by recoverable socket errors (?)
396 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
399 struct sockaddr *sendnam;
400 int error, soflags, flags;
402 KASSERT(rep, ("nfs_send: called with rep == NULL"));
404 if (rep->r_flags & R_SOFTTERM) {
408 if ((so = rep->r_nmp->nm_so) == NULL) {
409 rep->r_flags |= R_MUSTRESEND;
413 rep->r_flags &= ~R_MUSTRESEND;
414 soflags = rep->r_nmp->nm_soflags;
416 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
417 sendnam = (struct sockaddr *)0;
420 if (so->so_type == SOCK_SEQPACKET)
425 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
426 flags, curthread /*XXX*/);
427 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
429 rep->r_flags |= R_MUSTRESEND;
433 log(LOG_INFO, "nfs send error %d for server %s\n", error,
434 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
436 * Deal with errors for the client side.
438 if (rep->r_flags & R_SOFTTERM)
441 rep->r_flags |= R_MUSTRESEND;
444 * Handle any recoverable (soft) socket errors here. (?)
446 if (error != EINTR && error != ERESTART &&
447 error != EWOULDBLOCK && error != EPIPE)
454 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
455 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
456 * Mark and consolidate the data into a new mbuf list.
457 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
459 * For SOCK_STREAM we must be very careful to read an entire record once
460 * we have read any of it, even if the system call has been interrupted.
463 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
469 struct mbuf *control;
471 struct sockaddr **getnam;
472 int error, sotype, rcvflg;
473 struct thread *td = curthread; /* XXX */
476 * Set up arguments for soreceive()
478 *mp = (struct mbuf *)0;
479 *aname = (struct sockaddr *)0;
480 sotype = rep->r_nmp->nm_sotype;
483 * For reliable protocols, lock against other senders/receivers
484 * in case a reconnect is necessary.
485 * For SOCK_STREAM, first get the Record Mark to find out how much
486 * more there is to get.
487 * We must lock the socket against other receivers
488 * until we have an entire rpc request/reply.
490 if (sotype != SOCK_DGRAM) {
491 error = nfs_sndlock(rep);
496 * Check for fatal errors and resending request.
499 * Ugh: If a reconnect attempt just happened, nm_so
500 * would have changed. NULL indicates a failed
501 * attempt that has essentially shut down this
504 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
508 so = rep->r_nmp->nm_so;
510 error = nfs_reconnect(rep);
517 while (rep->r_flags & R_MUSTRESEND) {
518 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
519 nfsstats.rpcretries++;
520 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
522 if (error == EINTR || error == ERESTART ||
523 (error = nfs_reconnect(rep)) != 0) {
531 if (sotype == SOCK_STREAM) {
532 aio.iov_base = (caddr_t) &len;
533 aio.iov_len = sizeof(u_int32_t);
536 auio.uio_segflg = UIO_SYSSPACE;
537 auio.uio_rw = UIO_READ;
539 auio.uio_resid = sizeof(u_int32_t);
542 rcvflg = MSG_WAITALL;
543 error = so->so_proto->pr_usrreqs->pru_soreceive
544 (so, (struct sockaddr **)0, &auio,
545 (struct mbuf **)0, (struct mbuf **)0,
547 if (error == EWOULDBLOCK && rep) {
548 if (rep->r_flags & R_SOFTTERM)
551 } while (error == EWOULDBLOCK);
552 if (!error && auio.uio_resid > 0) {
554 * Don't log a 0 byte receive; it means
555 * that the socket has been closed, and
556 * can happen during normal operation
557 * (forcible unmount or Solaris server).
559 if (auio.uio_resid != sizeof (u_int32_t))
561 "short receive (%d/%d) from nfs server %s\n",
562 (int)(sizeof(u_int32_t) - auio.uio_resid),
563 (int)sizeof(u_int32_t),
564 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
569 len = ntohl(len) & ~0x80000000;
571 * This is SERIOUS! We are out of sync with the sender
572 * and forcing a disconnect/reconnect is all I can do.
574 if (len > NFS_MAXPACKET) {
575 log(LOG_ERR, "%s (%d) from nfs server %s\n",
576 "impossible packet length",
578 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
582 auio.uio_resid = len;
584 rcvflg = MSG_WAITALL;
585 error = so->so_proto->pr_usrreqs->pru_soreceive
586 (so, (struct sockaddr **)0,
587 &auio, mp, (struct mbuf **)0, &rcvflg);
588 } while (error == EWOULDBLOCK || error == EINTR ||
590 if (!error && auio.uio_resid > 0) {
591 if (len != auio.uio_resid)
593 "short receive (%d/%d) from nfs server %s\n",
594 len - auio.uio_resid, len,
595 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
600 * NB: Since uio_resid is big, MSG_WAITALL is ignored
601 * and soreceive() will return when it has either a
602 * control msg or a data msg.
603 * We have no use for control msg., but must grab them
604 * and then throw them away so we know what is going
607 auio.uio_resid = len = 100000000; /* Anything Big */
611 error = so->so_proto->pr_usrreqs->pru_soreceive
612 (so, (struct sockaddr **)0,
613 &auio, mp, &control, &rcvflg);
616 if (error == EWOULDBLOCK && rep) {
617 if (rep->r_flags & R_SOFTTERM)
620 } while (error == EWOULDBLOCK ||
621 (!error && *mp == NULL && control));
622 if ((rcvflg & MSG_EOR) == 0)
624 if (!error && *mp == NULL)
626 len -= auio.uio_resid;
629 if (error && error != EINTR && error != ERESTART) {
631 *mp = (struct mbuf *)0;
634 "receive error %d from nfs server %s\n",
636 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
637 error = nfs_sndlock(rep);
639 error = nfs_reconnect(rep);
646 if ((so = rep->r_nmp->nm_so) == NULL)
648 if (so->so_state & SS_ISCONNECTED)
649 getnam = (struct sockaddr **)0;
652 auio.uio_resid = len = 1000000;
656 error = so->so_proto->pr_usrreqs->pru_soreceive
657 (so, getnam, &auio, mp,
658 (struct mbuf **)0, &rcvflg);
659 if (error == EWOULDBLOCK &&
660 (rep->r_flags & R_SOFTTERM))
662 } while (error == EWOULDBLOCK);
663 len -= auio.uio_resid;
667 *mp = (struct mbuf *)0;
670 * Search for any mbufs that are not a multiple of 4 bytes long
671 * or with m_data not longword aligned.
672 * These could cause pointer alignment problems, so copy them to
673 * well aligned mbufs.
675 nfs_realign(mp, 5 * NFSX_UNSIGNED);
680 * Implement receipt of reply on a socket.
681 * We must search through the list of received datagrams matching them
682 * with outstanding requests using the xid, until ours is found.
686 nfs_reply(struct nfsreq *myrep)
689 struct nfsmount *nmp = myrep->r_nmp;
691 struct mbuf *mrep, *md;
692 struct sockaddr *nam;
698 * Loop around until we get our own reply
702 * Lock against other receivers so that I don't get stuck in
703 * sbwait() after someone else has received my reply for me.
704 * Also necessary for connection based protocols to avoid
705 * race conditions during a reconnect.
706 * If nfs_rcvlock() returns EALREADY, that means that
707 * the reply has already been recieved by another
708 * process and we can return immediately. In this
709 * case, the lock is not taken to avoid races with
712 error = nfs_rcvlock(myrep);
713 if (error == EALREADY)
718 * Get the next Rpc reply off the socket
720 error = nfs_receive(myrep, &nam, &mrep);
721 nfs_rcvunlock(myrep);
725 * Ignore routing errors on connectionless protocols??
727 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
728 nmp->nm_so->so_error = 0;
729 if (myrep->r_flags & R_GETONEREP)
739 * Get the xid and check that it is an rpc reply
742 dpos = mtod(md, caddr_t);
743 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
745 if (*tl != rpc_reply) {
746 nfsstats.rpcinvalid++;
749 if (myrep->r_flags & R_GETONEREP)
755 * Loop through the request list to match up the reply
756 * Iff no match, just drop the datagram
758 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
759 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
765 * Update congestion window.
766 * Do the additive increase of
769 if (nmp->nm_cwnd <= nmp->nm_sent) {
771 (NFS_CWNDSCALE * NFS_CWNDSCALE +
772 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
773 if (nmp->nm_cwnd > NFS_MAXCWND)
774 nmp->nm_cwnd = NFS_MAXCWND;
776 if (rep->r_flags & R_SENT) {
777 rep->r_flags &= ~R_SENT;
778 nmp->nm_sent -= NFS_CWNDSCALE;
781 * Update rtt using a gain of 0.125 on the mean
782 * and a gain of 0.25 on the deviation.
784 if (rep->r_flags & R_TIMING) {
786 * Since the timer resolution of
787 * NFS_HZ is so course, it can often
788 * result in r_rtt == 0. Since
789 * r_rtt == N means that the actual
790 * rtt is between N+dt and N+2-dt ticks,
794 t1 -= (NFS_SRTT(rep) >> 3);
798 t1 -= (NFS_SDRTT(rep) >> 2);
799 NFS_SDRTT(rep) += t1;
801 nmp->nm_timeouts = 0;
806 * If not matched to a request, drop it.
807 * If it's mine, get out.
810 nfsstats.rpcunexpected++;
812 } else if (rep == myrep) {
813 if (rep->r_mrep == NULL)
814 panic("nfsreply nil");
817 if (myrep->r_flags & R_GETONEREP)
823 * nfs_request - goes something like this
824 * - fill in request struct
825 * - links it into list
826 * - calls nfs_send() for first transmit
827 * - calls nfs_receive() to get reply
828 * - break down rpc header and return with nfs reply pointed to
830 * nb: always frees up mreq mbuf list
832 /* XXX overloaded before */
833 #define NQ_TRYLATERDEL 15 /* Initial try later delay (sec) */
836 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
837 struct thread *td, struct ucred *cred, struct mbuf **mrp,
838 struct mbuf **mdp, caddr_t *dposp)
840 struct mbuf *mrep, *m2;
844 struct nfsmount *nmp;
845 struct mbuf *m, *md, *mheadend;
848 int s, error = 0, mrest_len, auth_len, auth_type;
849 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
852 /* Reject requests while attempting a forced unmount. */
853 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
857 nmp = VFSTONFS(vp->v_mount);
858 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
862 rep->r_procnum = procnum;
872 * Get the RPC header with authorization.
874 auth_type = RPCAUTH_UNIX;
875 if (cred->cr_ngroups < 1)
876 panic("nfsreq nogrps");
877 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
878 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
880 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
881 mrest, mrest_len, &mheadend, &xid);
884 * For stream protocols, insert a Sun RPC Record Mark.
886 if (nmp->nm_sotype == SOCK_STREAM) {
887 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
888 *mtod(m, u_int32_t *) = htonl(0x80000000 |
889 (m->m_pkthdr.len - NFSX_UNSIGNED));
894 if (nmp->nm_flag & NFSMNT_SOFT)
895 rep->r_retry = nmp->nm_retry;
897 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
898 rep->r_rtt = rep->r_rexmit = 0;
899 if (proct[procnum] > 0)
900 rep->r_flags = R_TIMING;
906 * Do the client side RPC.
908 nfsstats.rpcrequests++;
910 * Chain request into list of outstanding requests. Be sure
911 * to put it LAST so timer finds oldest requests first.
914 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
917 * If backing off another request or avoiding congestion, don't
918 * send this one now but let timer do it. If not timing a request,
921 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
922 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
923 nmp->nm_sent < nmp->nm_cwnd)) {
925 if (nmp->nm_soflags & PR_CONNREQUIRED)
926 error = nfs_sndlock(rep);
928 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
929 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
930 if (nmp->nm_soflags & PR_CONNREQUIRED)
933 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
934 nmp->nm_sent += NFS_CWNDSCALE;
935 rep->r_flags |= R_SENT;
943 * Wait for the reply from our send or the timer's.
945 if (!error || error == EPIPE)
946 error = nfs_reply(rep);
949 * RPC done, unlink the request.
952 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
956 * Decrement the outstanding request count.
958 if (rep->r_flags & R_SENT) {
959 rep->r_flags &= ~R_SENT; /* paranoia */
960 nmp->nm_sent -= NFS_CWNDSCALE;
964 * If there was a successful reply and a tprintf msg.
965 * tprintf a response.
967 if (!error && (rep->r_flags & R_TPRINTFMSG))
968 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
974 m_freem(rep->r_mreq);
975 free((caddr_t)rep, M_NFSREQ);
980 * break down the rpc header and check if ok
982 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
983 if (*tl++ == rpc_msgdenied) {
984 if (*tl == rpc_mismatch)
989 m_freem(rep->r_mreq);
990 free((caddr_t)rep, M_NFSREQ);
995 * Just throw away any verifyer (ie: kerberos etc).
997 i = fxdr_unsigned(int, *tl++); /* verf type */
998 i = fxdr_unsigned(int32_t, *tl); /* len */
1000 nfsm_adv(nfsm_rndup(i));
1001 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1004 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1006 error = fxdr_unsigned(int, *tl);
1007 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1008 error == NFSERR_TRYLATER) {
1011 waituntil = time_second + trylater_delay;
1012 while (time_second < waituntil)
1013 (void) tsleep((caddr_t)&lbolt,
1014 PSOCK, "nqnfstry", 0);
1015 trylater_delay *= nfs_backoff[trylater_cnt];
1016 if (trylater_cnt < NFS_NBACKOFF - 1)
1022 * If the File Handle was stale, invalidate the
1023 * lookup cache, just in case.
1025 if (error == ESTALE)
1027 if (nmp->nm_flag & NFSMNT_NFSV3) {
1031 error |= NFSERR_RETERR;
1034 m_freem(rep->r_mreq);
1035 free((caddr_t)rep, M_NFSREQ);
1042 m_freem(rep->r_mreq);
1043 FREE((caddr_t)rep, M_NFSREQ);
1047 error = EPROTONOSUPPORT;
1049 m_freem(rep->r_mreq);
1050 free((caddr_t)rep, M_NFSREQ);
1056 * Scan the nfsreq list and retranmit any requests that have timed out
1057 * To avoid retransmission attempts on STREAM sockets (in the future) make
1058 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1061 nfs_timer(void *arg)
1066 struct nfsmount *nmp;
1071 td = &thread0; /* XXX for credentials, may break if sleep */
1073 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1075 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1077 if (nfs_sigintr(nmp, rep, rep->r_td)) {
1081 if (rep->r_rtt >= 0) {
1083 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1084 timeo = nmp->nm_timeo;
1086 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1087 if (nmp->nm_timeouts > 0)
1088 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1089 if (rep->r_rtt <= timeo)
1091 if (nmp->nm_timeouts < NFS_NBACKOFF)
1095 * Check for server not responding
1097 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1098 rep->r_rexmit > nmp->nm_deadthresh) {
1100 sprintf(buf, "not responding %d > %d",
1101 rep->r_rexmit, nmp->nm_deadthresh);
1103 nmp->nm_mountp->mnt_stat.f_mntfromname,
1104 buf /* "not responding" */);
1105 rep->r_flags |= R_TPRINTFMSG;
1107 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1108 nfsstats.rpctimeouts++;
1112 if (nmp->nm_sotype != SOCK_DGRAM) {
1113 if (++rep->r_rexmit > NFS_MAXREXMIT)
1114 rep->r_rexmit = NFS_MAXREXMIT;
1117 if ((so = nmp->nm_so) == NULL)
1121 * If there is enough space and the window allows..
1123 * Set r_rtt to -1 in case we fail to send it now.
1126 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1127 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1128 (rep->r_flags & R_SENT) ||
1129 nmp->nm_sent < nmp->nm_cwnd) &&
1130 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1131 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1132 error = (*so->so_proto->pr_usrreqs->pru_send)
1133 (so, 0, m, (struct sockaddr *)0,
1134 (struct mbuf *)0, td);
1136 error = (*so->so_proto->pr_usrreqs->pru_send)
1137 (so, 0, m, nmp->nm_nam, (struct mbuf *)0,
1140 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1144 * Iff first send, start timing
1145 * else turn timing off, backoff timer
1146 * and divide congestion window by 2.
1148 if (rep->r_flags & R_SENT) {
1149 rep->r_flags &= ~R_TIMING;
1150 if (++rep->r_rexmit > NFS_MAXREXMIT)
1151 rep->r_rexmit = NFS_MAXREXMIT;
1153 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1154 nmp->nm_cwnd = NFS_CWNDSCALE;
1155 nfsstats.rpcretries++;
1157 rep->r_flags |= R_SENT;
1158 nmp->nm_sent += NFS_CWNDSCALE;
1165 nfs_timer_handle = timeout(nfs_timer, (void *)0, nfs_ticks);
1169 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1170 * wait for all requests to complete. This is used by forced unmounts
1171 * to terminate any outstanding RPCs.
1174 nfs_nmcancelreqs(nmp)
1175 struct nfsmount *nmp;
1181 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1182 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1183 (req->r_flags & R_SOFTTERM))
1189 for (i = 0; i < 30; i++) {
1191 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1192 if (nmp == req->r_nmp)
1198 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1204 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1205 * The nm_send count is decremented now to avoid deadlocks when the process in
1206 * soreceive() hasn't yet managed to send its own request.
1210 nfs_softterm(struct nfsreq *rep)
1213 rep->r_flags |= R_SOFTTERM;
1214 if (rep->r_flags & R_SENT) {
1215 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1216 rep->r_flags &= ~R_SENT;
1221 * Test for a termination condition pending on the process.
1222 * This is used for NFSMNT_INT mounts.
1225 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1230 if (rep && (rep->r_flags & R_SOFTTERM))
1232 /* Terminate all requests while attempting a forced unmount. */
1233 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1235 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))
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, rep->r_td))
1314 *statep |= NFSSTA_WANTRCV;
1315 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1318 * If our reply was recieved while we were sleeping,
1319 * then just return without taking the lock to avoid a
1320 * situation where a single iod could 'capture' the
1323 if (rep->r_mrep != NULL)
1325 if (slpflag == PCATCH) {
1330 /* Always fail if our request has been cancelled. */
1331 if (rep != NULL && (rep->r_flags & R_SOFTTERM))
1333 *statep |= NFSSTA_RCVLOCK;
1338 * Unlock the stream socket for others.
1341 nfs_rcvunlock(struct nfsreq *rep)
1343 int *statep = &rep->r_nmp->nm_state;
1345 if ((*statep & NFSSTA_RCVLOCK) == 0)
1346 panic("nfs rcvunlock");
1347 *statep &= ~NFSSTA_RCVLOCK;
1348 if (*statep & NFSSTA_WANTRCV) {
1349 *statep &= ~NFSSTA_WANTRCV;
1350 wakeup((caddr_t)statep);
1357 * Check for badly aligned mbuf data and realign by copying the unaligned
1358 * portion of the data into a new mbuf chain and freeing the portions
1359 * of the old chain that were replaced.
1361 * We cannot simply realign the data within the existing mbuf chain
1362 * because the underlying buffers may contain other rpc commands and
1363 * we cannot afford to overwrite them.
1365 * We would prefer to avoid this situation entirely. The situation does
1366 * not occur with NFS/UDP and is supposed to only occassionally occur
1367 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1370 nfs_realign(struct mbuf **pm, int hsiz)
1373 struct mbuf *n = NULL;
1377 while ((m = *pm) != NULL) {
1378 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1379 MGET(n, M_TRYWAIT, MT_DATA);
1380 if (m->m_len >= MINCLSIZE) {
1381 MCLGET(n, M_TRYWAIT);
1389 * If n is non-NULL, loop on m copying data, then replace the
1390 * portion of the chain that had to be realigned.
1393 ++nfs_realign_count;
1395 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1406 nfs_msg(struct thread *td, char *server, char *msg)
1409 tprintf(td ? td->td_proc : NULL, LOG_INFO,
1410 "nfs server %s: %s\n", server, msg);