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.
234 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
235 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
237 if ((so->so_state & SS_ISCONNECTING) &&
238 so->so_error == 0 && rep &&
239 (error = nfs_sigintr(nmp, rep,
240 (rep->r_td ? rep->r_td->td_proc : NULL))) != 0){
241 so->so_state &= ~SS_ISCONNECTING;
249 error = so->so_error;
256 so->so_rcv.sb_timeo = 5 * hz;
257 so->so_snd.sb_timeo = 5 * hz;
260 * Get buffer reservation size from sysctl, but impose reasonable
263 pktscale = nfs_bufpackets;
269 if (nmp->nm_sotype == SOCK_DGRAM) {
270 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
271 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
272 NFS_MAXPKTHDR) * pktscale;
273 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
274 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
275 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
276 NFS_MAXPKTHDR) * pktscale;
278 if (nmp->nm_sotype != SOCK_STREAM)
279 panic("nfscon sotype");
280 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
284 bzero(&sopt, sizeof sopt);
285 sopt.sopt_level = SOL_SOCKET;
286 sopt.sopt_name = SO_KEEPALIVE;
287 sopt.sopt_val = &val;
288 sopt.sopt_valsize = sizeof val;
292 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
296 bzero(&sopt, sizeof sopt);
297 sopt.sopt_level = IPPROTO_TCP;
298 sopt.sopt_name = TCP_NODELAY;
299 sopt.sopt_val = &val;
300 sopt.sopt_valsize = sizeof val;
304 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
305 sizeof (u_int32_t)) * pktscale;
306 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
307 sizeof (u_int32_t)) * pktscale;
309 error = soreserve(so, sndreserve, rcvreserve);
312 so->so_rcv.sb_flags |= SB_NOINTR;
313 so->so_snd.sb_flags |= SB_NOINTR;
315 /* Initialize other non-zero congestion variables */
316 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
317 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
318 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
319 nmp->nm_sdrtt[3] = 0;
320 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
322 nmp->nm_timeouts = 0;
332 * Called when a connection is broken on a reliable protocol.
333 * - clean up the old socket
334 * - nfs_connect() again
335 * - set R_MUSTRESEND for all outstanding requests on mount point
336 * If this fails the mount point is DEAD!
337 * nb: Must be called with the nfs_sndlock() set on the mount point.
340 nfs_reconnect(struct nfsreq *rep)
343 struct nfsmount *nmp = rep->r_nmp;
347 while ((error = nfs_connect(nmp, rep)) != 0) {
348 if (error == EINTR || error == ERESTART)
350 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
354 * Loop through outstanding request list and fix up all requests
357 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
358 if (rp->r_nmp == nmp)
359 rp->r_flags |= R_MUSTRESEND;
365 * NFS disconnect. Clean up and unlink.
368 nfs_disconnect(struct nfsmount *nmp)
374 nmp->nm_so = (struct socket *)0;
381 nfs_safedisconnect(struct nfsmount *nmp)
383 struct nfsreq dummyreq;
385 bzero(&dummyreq, sizeof(dummyreq));
386 dummyreq.r_nmp = nmp;
387 nfs_rcvlock(&dummyreq);
389 nfs_rcvunlock(&dummyreq);
393 * This is the nfs send routine. For connection based socket types, it
394 * must be called with an nfs_sndlock() on the socket.
395 * - return EINTR if the RPC is terminated, 0 otherwise
396 * - set R_MUSTRESEND if the send fails for any reason
397 * - do any cleanup required by recoverable socket errors (?)
400 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
403 struct sockaddr *sendnam;
404 int error, soflags, flags;
406 KASSERT(rep, ("nfs_send: called with rep == NULL"));
408 if (rep->r_flags & R_SOFTTERM) {
412 if ((so = rep->r_nmp->nm_so) == NULL) {
413 rep->r_flags |= R_MUSTRESEND;
417 rep->r_flags &= ~R_MUSTRESEND;
418 soflags = rep->r_nmp->nm_soflags;
421 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED)) {
423 sendnam = (struct sockaddr *)0;
428 if (so->so_type == SOCK_SEQPACKET)
433 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
434 flags, curthread /*XXX*/);
435 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
437 rep->r_flags |= R_MUSTRESEND;
441 log(LOG_INFO, "nfs send error %d for server %s\n", error,
442 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
444 * Deal with errors for the client side.
446 if (rep->r_flags & R_SOFTTERM)
449 rep->r_flags |= R_MUSTRESEND;
452 * Handle any recoverable (soft) socket errors here. (?)
454 if (error != EINTR && error != ERESTART &&
455 error != EWOULDBLOCK && error != EPIPE)
462 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
463 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
464 * Mark and consolidate the data into a new mbuf list.
465 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
467 * For SOCK_STREAM we must be very careful to read an entire record once
468 * we have read any of it, even if the system call has been interrupted.
471 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
477 struct mbuf *control;
479 struct sockaddr **getnam;
480 int error, sotype, rcvflg;
481 struct thread *td = curthread; /* XXX */
484 * Set up arguments for soreceive()
486 *mp = (struct mbuf *)0;
487 *aname = (struct sockaddr *)0;
488 sotype = rep->r_nmp->nm_sotype;
491 * For reliable protocols, lock against other senders/receivers
492 * in case a reconnect is necessary.
493 * For SOCK_STREAM, first get the Record Mark to find out how much
494 * more there is to get.
495 * We must lock the socket against other receivers
496 * until we have an entire rpc request/reply.
498 if (sotype != SOCK_DGRAM) {
499 error = nfs_sndlock(rep);
504 * Check for fatal errors and resending request.
507 * Ugh: If a reconnect attempt just happened, nm_so
508 * would have changed. NULL indicates a failed
509 * attempt that has essentially shut down this
512 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
516 so = rep->r_nmp->nm_so;
518 error = nfs_reconnect(rep);
525 while (rep->r_flags & R_MUSTRESEND) {
526 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_TRYWAIT);
527 nfsstats.rpcretries++;
528 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
530 if (error == EINTR || error == ERESTART ||
531 (error = nfs_reconnect(rep)) != 0) {
539 if (sotype == SOCK_STREAM) {
540 aio.iov_base = (caddr_t) &len;
541 aio.iov_len = sizeof(u_int32_t);
544 auio.uio_segflg = UIO_SYSSPACE;
545 auio.uio_rw = UIO_READ;
547 auio.uio_resid = sizeof(u_int32_t);
550 rcvflg = MSG_WAITALL;
551 error = so->so_proto->pr_usrreqs->pru_soreceive
552 (so, (struct sockaddr **)0, &auio,
553 (struct mbuf **)0, (struct mbuf **)0,
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
594 (so, (struct sockaddr **)0,
595 &auio, mp, (struct mbuf **)0, &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
620 (so, (struct sockaddr **)0,
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) {
639 *mp = (struct mbuf *)0;
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);
654 if ((so = rep->r_nmp->nm_so) == NULL)
657 if (so->so_state & SS_ISCONNECTED) {
659 getnam = (struct sockaddr **)0;
664 auio.uio_resid = len = 1000000;
668 error = so->so_proto->pr_usrreqs->pru_soreceive
669 (so, getnam, &auio, mp,
670 (struct mbuf **)0, &rcvflg);
671 if (error == EWOULDBLOCK &&
672 (rep->r_flags & R_SOFTTERM))
674 } while (error == EWOULDBLOCK);
675 len -= auio.uio_resid;
679 *mp = (struct mbuf *)0;
682 * Search for any mbufs that are not a multiple of 4 bytes long
683 * or with m_data not longword aligned.
684 * These could cause pointer alignment problems, so copy them to
685 * well aligned mbufs.
687 nfs_realign(mp, 5 * NFSX_UNSIGNED);
692 * Implement receipt of reply on a socket.
693 * We must search through the list of received datagrams matching them
694 * with outstanding requests using the xid, until ours is found.
698 nfs_reply(struct nfsreq *myrep)
701 struct nfsmount *nmp = myrep->r_nmp;
703 struct mbuf *mrep, *md;
704 struct sockaddr *nam;
710 * Loop around until we get our own reply
714 * Lock against other receivers so that I don't get stuck in
715 * sbwait() after someone else has received my reply for me.
716 * Also necessary for connection based protocols to avoid
717 * race conditions during a reconnect.
718 * If nfs_rcvlock() returns EALREADY, that means that
719 * the reply has already been recieved by another
720 * process and we can return immediately. In this
721 * case, the lock is not taken to avoid races with
724 error = nfs_rcvlock(myrep);
725 if (error == EALREADY)
730 * Get the next Rpc reply off the socket
732 error = nfs_receive(myrep, &nam, &mrep);
733 nfs_rcvunlock(myrep);
737 * Ignore routing errors on connectionless protocols??
739 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
740 nmp->nm_so->so_error = 0;
741 if (myrep->r_flags & R_GETONEREP)
751 * Get the xid and check that it is an rpc reply
754 dpos = mtod(md, caddr_t);
755 tl = nfsm_dissect(u_int32_t *, 2 * NFSX_UNSIGNED);
757 if (*tl != rpc_reply) {
758 nfsstats.rpcinvalid++;
761 if (myrep->r_flags & R_GETONEREP)
767 * Loop through the request list to match up the reply
768 * Iff no match, just drop the datagram
770 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
771 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
777 * Update congestion window.
778 * Do the additive increase of
781 if (nmp->nm_cwnd <= nmp->nm_sent) {
783 (NFS_CWNDSCALE * NFS_CWNDSCALE +
784 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
785 if (nmp->nm_cwnd > NFS_MAXCWND)
786 nmp->nm_cwnd = NFS_MAXCWND;
788 if (rep->r_flags & R_SENT) {
789 rep->r_flags &= ~R_SENT;
790 nmp->nm_sent -= NFS_CWNDSCALE;
793 * Update rtt using a gain of 0.125 on the mean
794 * and a gain of 0.25 on the deviation.
796 if (rep->r_flags & R_TIMING) {
798 * Since the timer resolution of
799 * NFS_HZ is so course, it can often
800 * result in r_rtt == 0. Since
801 * r_rtt == N means that the actual
802 * rtt is between N+dt and N+2-dt ticks,
806 t1 -= (NFS_SRTT(rep) >> 3);
810 t1 -= (NFS_SDRTT(rep) >> 2);
811 NFS_SDRTT(rep) += t1;
813 nmp->nm_timeouts = 0;
818 * If not matched to a request, drop it.
819 * If it's mine, get out.
822 nfsstats.rpcunexpected++;
824 } else if (rep == myrep) {
825 if (rep->r_mrep == NULL)
826 panic("nfsreply nil");
829 if (myrep->r_flags & R_GETONEREP)
835 * nfs_request - goes something like this
836 * - fill in request struct
837 * - links it into list
838 * - calls nfs_send() for first transmit
839 * - calls nfs_receive() to get reply
840 * - break down rpc header and return with nfs reply pointed to
842 * nb: always frees up mreq mbuf list
844 /* XXX overloaded before */
845 #define NQ_TRYLATERDEL 15 /* Initial try later delay (sec) */
848 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
849 struct thread *td, struct ucred *cred, struct mbuf **mrp,
850 struct mbuf **mdp, caddr_t *dposp)
852 struct mbuf *mrep, *m2;
856 struct nfsmount *nmp;
857 struct mbuf *m, *md, *mheadend;
860 int s, error = 0, mrest_len, auth_len, auth_type;
861 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0;
864 /* Reject requests while attempting a forced unmount. */
865 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
869 nmp = VFSTONFS(vp->v_mount);
870 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
874 rep->r_procnum = procnum;
884 * Get the RPC header with authorization.
886 auth_type = RPCAUTH_UNIX;
887 if (cred->cr_ngroups < 1)
888 panic("nfsreq nogrps");
889 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
890 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
892 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
893 mrest, mrest_len, &mheadend, &xid);
896 * For stream protocols, insert a Sun RPC Record Mark.
898 if (nmp->nm_sotype == SOCK_STREAM) {
899 M_PREPEND(m, NFSX_UNSIGNED, M_TRYWAIT);
900 *mtod(m, u_int32_t *) = htonl(0x80000000 |
901 (m->m_pkthdr.len - NFSX_UNSIGNED));
906 if (nmp->nm_flag & NFSMNT_SOFT)
907 rep->r_retry = nmp->nm_retry;
909 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
910 rep->r_rtt = rep->r_rexmit = 0;
911 if (proct[procnum] > 0)
912 rep->r_flags = R_TIMING;
918 * Do the client side RPC.
920 nfsstats.rpcrequests++;
922 * Chain request into list of outstanding requests. Be sure
923 * to put it LAST so timer finds oldest requests first.
926 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
929 * If backing off another request or avoiding congestion, don't
930 * send this one now but let timer do it. If not timing a request,
933 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
934 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
935 nmp->nm_sent < nmp->nm_cwnd)) {
937 if (nmp->nm_soflags & PR_CONNREQUIRED)
938 error = nfs_sndlock(rep);
940 m2 = m_copym(m, 0, M_COPYALL, M_TRYWAIT);
941 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
942 if (nmp->nm_soflags & PR_CONNREQUIRED)
945 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
946 nmp->nm_sent += NFS_CWNDSCALE;
947 rep->r_flags |= R_SENT;
955 * Wait for the reply from our send or the timer's.
957 if (!error || error == EPIPE)
958 error = nfs_reply(rep);
961 * RPC done, unlink the request.
964 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
968 * Decrement the outstanding request count.
970 if (rep->r_flags & R_SENT) {
971 rep->r_flags &= ~R_SENT; /* paranoia */
972 nmp->nm_sent -= NFS_CWNDSCALE;
976 * If there was a successful reply and a tprintf msg.
977 * tprintf a response.
979 if (!error && (rep->r_flags & R_TPRINTFMSG))
980 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
986 m_freem(rep->r_mreq);
987 free((caddr_t)rep, M_NFSREQ);
992 * break down the rpc header and check if ok
994 tl = nfsm_dissect(u_int32_t *, 3 * NFSX_UNSIGNED);
995 if (*tl++ == rpc_msgdenied) {
996 if (*tl == rpc_mismatch)
1001 m_freem(rep->r_mreq);
1002 free((caddr_t)rep, M_NFSREQ);
1007 * Just throw away any verifyer (ie: kerberos etc).
1009 i = fxdr_unsigned(int, *tl++); /* verf type */
1010 i = fxdr_unsigned(int32_t, *tl); /* len */
1012 nfsm_adv(nfsm_rndup(i));
1013 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1016 tl = nfsm_dissect(u_int32_t *, NFSX_UNSIGNED);
1018 error = fxdr_unsigned(int, *tl);
1019 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1020 error == NFSERR_TRYLATER) {
1023 waituntil = time_second + trylater_delay;
1024 while (time_second < waituntil)
1025 (void) tsleep((caddr_t)&lbolt,
1026 PSOCK, "nqnfstry", 0);
1027 trylater_delay *= nfs_backoff[trylater_cnt];
1028 if (trylater_cnt < NFS_NBACKOFF - 1)
1034 * If the File Handle was stale, invalidate the
1035 * lookup cache, just in case.
1037 if (error == ESTALE)
1039 if (nmp->nm_flag & NFSMNT_NFSV3) {
1043 error |= NFSERR_RETERR;
1046 m_freem(rep->r_mreq);
1047 free((caddr_t)rep, M_NFSREQ);
1054 m_freem(rep->r_mreq);
1055 FREE((caddr_t)rep, M_NFSREQ);
1059 error = EPROTONOSUPPORT;
1061 m_freem(rep->r_mreq);
1062 free((caddr_t)rep, M_NFSREQ);
1068 * Scan the nfsreq list and retranmit any requests that have timed out
1069 * To avoid retransmission attempts on STREAM sockets (in the future) make
1070 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1073 nfs_timer(void *arg)
1078 struct nfsmount *nmp;
1083 td = &thread0; /* XXX for credentials, may break if sleep */
1085 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1087 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1089 if (nfs_sigintr(nmp, rep,
1090 (rep->r_td ? rep->r_td->td_proc : NULL))) {
1094 if (rep->r_rtt >= 0) {
1096 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1097 timeo = nmp->nm_timeo;
1099 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1100 if (nmp->nm_timeouts > 0)
1101 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1102 if (rep->r_rtt <= timeo)
1104 if (nmp->nm_timeouts < NFS_NBACKOFF)
1108 * Check for server not responding
1110 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1111 rep->r_rexmit > nmp->nm_deadthresh) {
1113 sprintf(buf, "not responding %d > %d",
1114 rep->r_rexmit, nmp->nm_deadthresh);
1116 nmp->nm_mountp->mnt_stat.f_mntfromname,
1117 buf /* "not responding" */);
1118 rep->r_flags |= R_TPRINTFMSG;
1120 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1121 nfsstats.rpctimeouts++;
1125 if (nmp->nm_sotype != SOCK_DGRAM) {
1126 if (++rep->r_rexmit > NFS_MAXREXMIT)
1127 rep->r_rexmit = NFS_MAXREXMIT;
1130 if ((so = nmp->nm_so) == NULL)
1134 * If there is enough space and the window allows..
1136 * Set r_rtt to -1 in case we fail to send it now.
1139 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1140 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1141 (rep->r_flags & R_SENT) ||
1142 nmp->nm_sent < nmp->nm_cwnd) &&
1143 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1144 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1145 error = (*so->so_proto->pr_usrreqs->pru_send)
1146 (so, 0, m, (struct sockaddr *)0,
1147 (struct mbuf *)0, td);
1149 error = (*so->so_proto->pr_usrreqs->pru_send)
1150 (so, 0, m, nmp->nm_nam, (struct mbuf *)0,
1153 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1157 * Iff first send, start timing
1158 * else turn timing off, backoff timer
1159 * and divide congestion window by 2.
1161 if (rep->r_flags & R_SENT) {
1162 rep->r_flags &= ~R_TIMING;
1163 if (++rep->r_rexmit > NFS_MAXREXMIT)
1164 rep->r_rexmit = NFS_MAXREXMIT;
1166 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1167 nmp->nm_cwnd = NFS_CWNDSCALE;
1168 nfsstats.rpcretries++;
1170 rep->r_flags |= R_SENT;
1171 nmp->nm_sent += NFS_CWNDSCALE;
1178 nfs_timer_handle = timeout(nfs_timer, (void *)0, nfs_ticks);
1182 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1183 * wait for all requests to complete. This is used by forced unmounts
1184 * to terminate any outstanding RPCs.
1187 nfs_nmcancelreqs(nmp)
1188 struct nfsmount *nmp;
1194 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1195 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1196 (req->r_flags & R_SOFTTERM))
1202 for (i = 0; i < 30; i++) {
1204 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1205 if (nmp == req->r_nmp)
1211 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1217 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1218 * The nm_send count is decremented now to avoid deadlocks when the process in
1219 * soreceive() hasn't yet managed to send its own request.
1223 nfs_softterm(struct nfsreq *rep)
1226 rep->r_flags |= R_SOFTTERM;
1227 if (rep->r_flags & R_SENT) {
1228 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1229 rep->r_flags &= ~R_SENT;
1234 * Test for a termination condition pending on the process.
1235 * This is used for NFSMNT_INT mounts.
1238 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct proc *p)
1242 if (rep && (rep->r_flags & R_SOFTTERM))
1244 /* Terminate all requests while attempting a forced unmount. */
1245 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1247 if (!(nmp->nm_flag & NFSMNT_INT))
1252 tmpset = p->p_siglist;
1253 SIGSETNAND(tmpset, p->p_sigmask);
1254 SIGSETNAND(tmpset, p->p_sigignore);
1255 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1262 * Lock a socket against others.
1263 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1264 * and also to avoid race conditions between the processes with nfs requests
1265 * in progress when a reconnect is necessary.
1268 nfs_sndlock(struct nfsreq *rep)
1270 int *statep = &rep->r_nmp->nm_state;
1272 int slpflag = 0, slptimeo = 0;
1276 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1279 td = (struct thread *)0;
1280 while (*statep & NFSSTA_SNDLOCK) {
1281 if (nfs_sigintr(rep->r_nmp, rep, td ? td->td_proc : NULL))
1283 *statep |= NFSSTA_WANTSND;
1284 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1),
1285 "nfsndlck", slptimeo);
1286 if (slpflag == PCATCH) {
1291 *statep |= NFSSTA_SNDLOCK;
1296 * Unlock the stream socket for others.
1299 nfs_sndunlock(struct nfsreq *rep)
1301 int *statep = &rep->r_nmp->nm_state;
1303 if ((*statep & NFSSTA_SNDLOCK) == 0)
1304 panic("nfs sndunlock");
1305 *statep &= ~NFSSTA_SNDLOCK;
1306 if (*statep & NFSSTA_WANTSND) {
1307 *statep &= ~NFSSTA_WANTSND;
1308 wakeup((caddr_t)statep);
1313 nfs_rcvlock(struct nfsreq *rep)
1315 int *statep = &rep->r_nmp->nm_state;
1316 int slpflag, slptimeo = 0;
1318 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1322 while (*statep & NFSSTA_RCVLOCK) {
1323 if (nfs_sigintr(rep->r_nmp, rep,
1324 (rep->r_td ? rep->r_td->td_proc : NULL)))
1326 *statep |= NFSSTA_WANTRCV;
1327 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1330 * If our reply was recieved while we were sleeping,
1331 * then just return without taking the lock to avoid a
1332 * situation where a single iod could 'capture' the
1335 if (rep->r_mrep != NULL)
1337 if (slpflag == PCATCH) {
1342 /* Always fail if our request has been cancelled. */
1343 if (rep != NULL && (rep->r_flags & R_SOFTTERM))
1345 *statep |= NFSSTA_RCVLOCK;
1350 * Unlock the stream socket for others.
1353 nfs_rcvunlock(struct nfsreq *rep)
1355 int *statep = &rep->r_nmp->nm_state;
1357 if ((*statep & NFSSTA_RCVLOCK) == 0)
1358 panic("nfs rcvunlock");
1359 *statep &= ~NFSSTA_RCVLOCK;
1360 if (*statep & NFSSTA_WANTRCV) {
1361 *statep &= ~NFSSTA_WANTRCV;
1362 wakeup((caddr_t)statep);
1369 * Check for badly aligned mbuf data and realign by copying the unaligned
1370 * portion of the data into a new mbuf chain and freeing the portions
1371 * of the old chain that were replaced.
1373 * We cannot simply realign the data within the existing mbuf chain
1374 * because the underlying buffers may contain other rpc commands and
1375 * we cannot afford to overwrite them.
1377 * We would prefer to avoid this situation entirely. The situation does
1378 * not occur with NFS/UDP and is supposed to only occassionally occur
1379 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1382 nfs_realign(struct mbuf **pm, int hsiz)
1385 struct mbuf *n = NULL;
1389 while ((m = *pm) != NULL) {
1390 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1391 MGET(n, M_TRYWAIT, MT_DATA);
1392 if (m->m_len >= MINCLSIZE) {
1393 MCLGET(n, M_TRYWAIT);
1401 * If n is non-NULL, loop on m copying data, then replace the
1402 * portion of the chain that had to be realigned.
1405 ++nfs_realign_count;
1407 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1418 nfs_msg(struct thread *td, char *server, char *msg)
1421 tprintf(td ? td->td_proc : NULL, LOG_INFO,
1422 "nfs server %s: %s\n", server, msg);