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
37 * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $
38 * $DragonFly: src/sys/vfs/nfs/nfs_socket.c,v 1.30 2005/09/17 07:43:12 dillon Exp $
42 * Socket operations for use by nfs
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <sys/protosw.h>
54 #include <sys/resourcevar.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/socketops.h>
58 #include <sys/syslog.h>
59 #include <sys/thread.h>
60 #include <sys/tprintf.h>
61 #include <sys/sysctl.h>
62 #include <sys/signalvar.h>
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
66 #include <sys/thread2.h>
72 #include "nfsm_subs.h"
82 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
83 * Use the mean and mean deviation of rtt for the appropriate type of rpc
84 * for the frequent rpcs and a default for the others.
85 * The justification for doing "other" this way is that these rpcs
86 * happen so infrequently that timer est. would probably be stale.
87 * Also, since many of these rpcs are
88 * non-idempotent, a conservative timeout is desired.
89 * getattr, lookup - A+2D
93 #define NFS_RTO(n, t) \
94 ((t) == 0 ? (n)->nm_timeo : \
96 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
97 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
98 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
99 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
101 * External data, mostly RPC constants in XDR form
103 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
104 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
106 extern u_int32_t nfs_prog, nqnfs_prog;
107 extern time_t nqnfsstarttime;
108 extern struct nfsstats nfsstats;
109 extern int nfsv3_procid[NFS_NPROCS];
110 extern int nfs_ticks;
113 * Defines which timer to use for the procnum.
120 static int proct[NFS_NPROCS] = {
121 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
125 static int nfs_realign_test;
126 static int nfs_realign_count;
127 static int nfs_bufpackets = 4;
129 SYSCTL_DECL(_vfs_nfs);
131 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
132 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
133 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
137 * There is a congestion window for outstanding rpcs maintained per mount
138 * point. The cwnd size is adjusted in roughly the way that:
139 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
140 * SIGCOMM '88". ACM, August 1988.
141 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
142 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
143 * of rpcs is in progress.
144 * (The sent count and cwnd are scaled for integer arith.)
145 * Variants of "slow start" were tried and were found to be too much of a
146 * performance hit (ave. rtt 3 times larger),
147 * I suspect due to the large rtt that nfs rpcs have.
149 #define NFS_CWNDSCALE 256
150 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
151 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
153 struct nfsrtt nfsrtt;
154 struct callout nfs_timer_handle;
156 static int nfs_msg (struct thread *,char *,char *);
157 static int nfs_rcvlock (struct nfsreq *);
158 static void nfs_rcvunlock (struct nfsreq *);
159 static void nfs_realign (struct mbuf **pm, int hsiz);
160 static int nfs_receive (struct nfsreq *rep, struct sockaddr **aname,
162 static void nfs_softterm (struct nfsreq *rep);
163 static int nfs_reconnect (struct nfsreq *rep);
165 static int nfsrv_getstream (struct nfssvc_sock *, int, int *);
167 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
168 struct nfssvc_sock *slp,
170 struct mbuf **mreqp) = {
198 #endif /* NFS_NOSERVER */
201 * Initialize sockets and congestion for a new NFS connection.
202 * We do not free the sockaddr if error.
205 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
208 int error, rcvreserve, sndreserve;
210 struct sockaddr *saddr;
211 struct sockaddr_in *sin;
212 struct thread *td = &thread0; /* only used for socreate and sobind */
214 nmp->nm_so = (struct socket *)0;
216 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
217 nmp->nm_soproto, td);
221 nmp->nm_soflags = so->so_proto->pr_flags;
224 * Some servers require that the client port be a reserved port number.
226 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
229 struct sockaddr_in ssin;
231 bzero(&sopt, sizeof sopt);
232 ip = IP_PORTRANGE_LOW;
233 sopt.sopt_level = IPPROTO_IP;
234 sopt.sopt_name = IP_PORTRANGE;
235 sopt.sopt_val = (void *)&ip;
236 sopt.sopt_valsize = sizeof(ip);
238 error = sosetopt(so, &sopt);
241 bzero(&ssin, sizeof ssin);
243 sin->sin_len = sizeof (struct sockaddr_in);
244 sin->sin_family = AF_INET;
245 sin->sin_addr.s_addr = INADDR_ANY;
246 sin->sin_port = htons(0);
247 error = sobind(so, (struct sockaddr *)sin, td);
250 bzero(&sopt, sizeof sopt);
251 ip = IP_PORTRANGE_DEFAULT;
252 sopt.sopt_level = IPPROTO_IP;
253 sopt.sopt_name = IP_PORTRANGE;
254 sopt.sopt_val = (void *)&ip;
255 sopt.sopt_valsize = sizeof(ip);
257 error = sosetopt(so, &sopt);
263 * Protocols that do not require connections may be optionally left
264 * unconnected for servers that reply from a port other than NFS_PORT.
266 if (nmp->nm_flag & NFSMNT_NOCONN) {
267 if (nmp->nm_soflags & PR_CONNREQUIRED) {
272 error = soconnect(so, nmp->nm_nam, td);
277 * Wait for the connection to complete. Cribbed from the
278 * connect system call but with the wait timing out so
279 * that interruptible mounts don't hang here for a long time.
282 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
283 (void) tsleep((caddr_t)&so->so_timeo, 0,
285 if ((so->so_state & SS_ISCONNECTING) &&
286 so->so_error == 0 && rep &&
287 (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
288 so->so_state &= ~SS_ISCONNECTING;
294 error = so->so_error;
301 so->so_rcv.sb_timeo = (5 * hz);
302 so->so_snd.sb_timeo = (5 * hz);
305 * Get buffer reservation size from sysctl, but impose reasonable
308 pktscale = nfs_bufpackets;
314 if (nmp->nm_sotype == SOCK_DGRAM) {
315 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
316 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
317 NFS_MAXPKTHDR) * pktscale;
318 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
319 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
320 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
321 NFS_MAXPKTHDR) * pktscale;
323 if (nmp->nm_sotype != SOCK_STREAM)
324 panic("nfscon sotype");
325 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
329 bzero(&sopt, sizeof sopt);
330 sopt.sopt_level = SOL_SOCKET;
331 sopt.sopt_name = SO_KEEPALIVE;
332 sopt.sopt_val = &val;
333 sopt.sopt_valsize = sizeof val;
337 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
341 bzero(&sopt, sizeof sopt);
342 sopt.sopt_level = IPPROTO_TCP;
343 sopt.sopt_name = TCP_NODELAY;
344 sopt.sopt_val = &val;
345 sopt.sopt_valsize = sizeof val;
349 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
350 sizeof (u_int32_t)) * pktscale;
351 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
352 sizeof (u_int32_t)) * pktscale;
354 error = soreserve(so, sndreserve, rcvreserve,
355 &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
358 so->so_rcv.sb_flags |= SB_NOINTR;
359 so->so_snd.sb_flags |= SB_NOINTR;
361 /* Initialize other non-zero congestion variables */
362 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
363 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
364 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
365 nmp->nm_sdrtt[3] = 0;
366 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
368 nmp->nm_timeouts = 0;
378 * Called when a connection is broken on a reliable protocol.
379 * - clean up the old socket
380 * - nfs_connect() again
381 * - set R_MUSTRESEND for all outstanding requests on mount point
382 * If this fails the mount point is DEAD!
383 * nb: Must be called with the nfs_sndlock() set on the mount point.
386 nfs_reconnect(struct nfsreq *rep)
389 struct nfsmount *nmp = rep->r_nmp;
393 while ((error = nfs_connect(nmp, rep)) != 0) {
394 if (error == EINTR || error == ERESTART)
396 (void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
400 * Loop through outstanding request list and fix up all requests
403 TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
404 if (rp->r_nmp == nmp)
405 rp->r_flags |= R_MUSTRESEND;
411 * NFS disconnect. Clean up and unlink.
414 nfs_disconnect(struct nfsmount *nmp)
420 nmp->nm_so = (struct socket *)0;
427 nfs_safedisconnect(struct nfsmount *nmp)
429 struct nfsreq dummyreq;
431 bzero(&dummyreq, sizeof(dummyreq));
432 dummyreq.r_nmp = nmp;
433 dummyreq.r_td = NULL;
434 nfs_rcvlock(&dummyreq);
436 nfs_rcvunlock(&dummyreq);
440 * This is the nfs send routine. For connection based socket types, it
441 * must be called with an nfs_sndlock() on the socket.
442 * "rep == NULL" indicates that it has been called from a server.
443 * For the client side:
444 * - return EINTR if the RPC is terminated, 0 otherwise
445 * - set R_MUSTRESEND if the send fails for any reason
446 * - do any cleanup required by recoverable socket errors (?)
447 * For the server side:
448 * - return EINTR or ERESTART if interrupted by a signal
449 * - return EPIPE if a connection is lost for connection based sockets (TCP...)
450 * - do any cleanup required by recoverable socket errors (?)
453 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
456 struct sockaddr *sendnam;
457 int error, soflags, flags;
460 if (rep->r_flags & R_SOFTTERM) {
464 if ((so = rep->r_nmp->nm_so) == NULL) {
465 rep->r_flags |= R_MUSTRESEND;
469 rep->r_flags &= ~R_MUSTRESEND;
470 soflags = rep->r_nmp->nm_soflags;
472 soflags = so->so_proto->pr_flags;
473 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
474 sendnam = (struct sockaddr *)0;
477 if (so->so_type == SOCK_SEQPACKET)
482 error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
485 * ENOBUFS for dgram sockets is transient and non fatal.
486 * No need to log, and no need to break a soft mount.
488 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
490 if (rep) /* do backoff retransmit on client */
491 rep->r_flags |= R_MUSTRESEND;
496 log(LOG_INFO, "nfs send error %d for server %s\n",error,
497 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
499 * Deal with errors for the client side.
501 if (rep->r_flags & R_SOFTTERM)
504 rep->r_flags |= R_MUSTRESEND;
506 log(LOG_INFO, "nfsd send error %d\n", error);
509 * Handle any recoverable (soft) socket errors here. (?)
511 if (error != EINTR && error != ERESTART &&
512 error != EWOULDBLOCK && error != EPIPE)
519 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
520 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
521 * Mark and consolidate the data into a new mbuf list.
522 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
524 * For SOCK_STREAM we must be very careful to read an entire record once
525 * we have read any of it, even if the system call has been interrupted.
528 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
534 struct mbuf *control;
536 struct sockaddr **getnam;
537 int error, sotype, rcvflg;
538 struct thread *td = curthread; /* XXX */
541 * Set up arguments for soreceive()
543 *mp = (struct mbuf *)0;
544 *aname = (struct sockaddr *)0;
545 sotype = rep->r_nmp->nm_sotype;
548 * For reliable protocols, lock against other senders/receivers
549 * in case a reconnect is necessary.
550 * For SOCK_STREAM, first get the Record Mark to find out how much
551 * more there is to get.
552 * We must lock the socket against other receivers
553 * until we have an entire rpc request/reply.
555 if (sotype != SOCK_DGRAM) {
556 error = nfs_sndlock(rep);
561 * Check for fatal errors and resending request.
564 * Ugh: If a reconnect attempt just happened, nm_so
565 * would have changed. NULL indicates a failed
566 * attempt that has essentially shut down this
569 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
573 so = rep->r_nmp->nm_so;
575 error = nfs_reconnect(rep);
582 while (rep->r_flags & R_MUSTRESEND) {
583 m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
584 nfsstats.rpcretries++;
585 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
587 if (error == EINTR || error == ERESTART ||
588 (error = nfs_reconnect(rep)) != 0) {
596 if (sotype == SOCK_STREAM) {
597 aio.iov_base = (caddr_t) &len;
598 aio.iov_len = sizeof(u_int32_t);
601 auio.uio_segflg = UIO_SYSSPACE;
602 auio.uio_rw = UIO_READ;
604 auio.uio_resid = sizeof(u_int32_t);
607 rcvflg = MSG_WAITALL;
608 error = so_pru_soreceive(so, NULL, &auio, NULL,
610 if (error == EWOULDBLOCK && rep) {
611 if (rep->r_flags & R_SOFTTERM)
614 } while (error == EWOULDBLOCK);
615 if (!error && auio.uio_resid > 0) {
617 * Don't log a 0 byte receive; it means
618 * that the socket has been closed, and
619 * can happen during normal operation
620 * (forcible unmount or Solaris server).
622 if (auio.uio_resid != sizeof (u_int32_t))
624 "short receive (%d/%d) from nfs server %s\n",
625 (int)(sizeof(u_int32_t) - auio.uio_resid),
626 (int)sizeof(u_int32_t),
627 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
632 len = ntohl(len) & ~0x80000000;
634 * This is SERIOUS! We are out of sync with the sender
635 * and forcing a disconnect/reconnect is all I can do.
637 if (len > NFS_MAXPACKET) {
638 log(LOG_ERR, "%s (%d) from nfs server %s\n",
639 "impossible packet length",
641 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
645 auio.uio_resid = len;
647 rcvflg = MSG_WAITALL;
648 error = so_pru_soreceive(so, NULL, &auio, mp,
650 } while (error == EWOULDBLOCK || error == EINTR ||
652 if (!error && auio.uio_resid > 0) {
653 if (len != auio.uio_resid)
655 "short receive (%d/%d) from nfs server %s\n",
656 len - auio.uio_resid, len,
657 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
662 * NB: Since uio_resid is big, MSG_WAITALL is ignored
663 * and soreceive() will return when it has either a
664 * control msg or a data msg.
665 * We have no use for control msg., but must grab them
666 * and then throw them away so we know what is going
669 auio.uio_resid = len = 100000000; /* Anything Big */
673 error = so_pru_soreceive(so, NULL, &auio, mp,
677 if (error == EWOULDBLOCK && rep) {
678 if (rep->r_flags & R_SOFTTERM)
681 } while (error == EWOULDBLOCK ||
682 (!error && *mp == NULL && control));
683 if ((rcvflg & MSG_EOR) == 0)
685 if (!error && *mp == NULL)
687 len -= auio.uio_resid;
690 if (error && error != EINTR && error != ERESTART) {
692 *mp = (struct mbuf *)0;
695 "receive error %d from nfs server %s\n",
697 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
698 error = nfs_sndlock(rep);
700 error = nfs_reconnect(rep);
708 if ((so = rep->r_nmp->nm_so) == NULL)
710 if (so->so_state & SS_ISCONNECTED)
711 getnam = (struct sockaddr **)0;
714 auio.uio_resid = len = 1000000;
718 error = so_pru_soreceive(so, getnam, &auio, mp, NULL,
720 if (error == EWOULDBLOCK &&
721 (rep->r_flags & R_SOFTTERM))
723 } while (error == EWOULDBLOCK);
724 len -= auio.uio_resid;
728 *mp = (struct mbuf *)0;
731 * Search for any mbufs that are not a multiple of 4 bytes long
732 * or with m_data not longword aligned.
733 * These could cause pointer alignment problems, so copy them to
734 * well aligned mbufs.
736 nfs_realign(mp, 5 * NFSX_UNSIGNED);
741 * Implement receipt of reply on a socket.
742 * We must search through the list of received datagrams matching them
743 * with outstanding requests using the xid, until ours is found.
747 nfs_reply(struct nfsreq *myrep)
750 struct nfsmount *nmp = myrep->r_nmp;
752 struct mbuf *mrep, *md;
753 struct sockaddr *nam;
759 * Loop around until we get our own reply
763 * Lock against other receivers so that I don't get stuck in
764 * sbwait() after someone else has received my reply for me.
765 * Also necessary for connection based protocols to avoid
766 * race conditions during a reconnect.
767 * If nfs_rcvlock() returns EALREADY, that means that
768 * the reply has already been recieved by another
769 * process and we can return immediately. In this
770 * case, the lock is not taken to avoid races with
773 error = nfs_rcvlock(myrep);
774 if (error == EALREADY)
779 * Get the next Rpc reply off the socket
781 error = nfs_receive(myrep, &nam, &mrep);
782 nfs_rcvunlock(myrep);
786 * Ignore routing errors on connectionless protocols??
788 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
789 nmp->nm_so->so_error = 0;
790 if (myrep->r_flags & R_GETONEREP)
800 * Get the xid and check that it is an rpc reply
803 dpos = mtod(md, caddr_t);
804 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
806 if (*tl != rpc_reply) {
808 if (nmp->nm_flag & NFSMNT_NQNFS) {
809 if (nqnfs_callback(nmp, mrep, md, dpos))
810 nfsstats.rpcinvalid++;
812 nfsstats.rpcinvalid++;
816 nfsstats.rpcinvalid++;
820 if (myrep->r_flags & R_GETONEREP)
826 * Loop through the request list to match up the reply
827 * Iff no match, just drop the datagram
829 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
830 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
838 rt = &nfsrtt.rttl[nfsrtt.pos];
839 rt->proc = rep->r_procnum;
840 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
841 rt->sent = nmp->nm_sent;
842 rt->cwnd = nmp->nm_cwnd;
843 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
844 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
845 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
846 getmicrotime(&rt->tstamp);
847 if (rep->r_flags & R_TIMING)
848 rt->rtt = rep->r_rtt;
851 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
854 * Update congestion window.
855 * Do the additive increase of
858 if (nmp->nm_cwnd <= nmp->nm_sent) {
860 (NFS_CWNDSCALE * NFS_CWNDSCALE +
861 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
862 if (nmp->nm_cwnd > NFS_MAXCWND)
863 nmp->nm_cwnd = NFS_MAXCWND;
865 crit_enter(); /* nfs_timer interlock*/
866 if (rep->r_flags & R_SENT) {
867 rep->r_flags &= ~R_SENT;
868 nmp->nm_sent -= NFS_CWNDSCALE;
872 * Update rtt using a gain of 0.125 on the mean
873 * and a gain of 0.25 on the deviation.
875 if (rep->r_flags & R_TIMING) {
877 * Since the timer resolution of
878 * NFS_HZ is so course, it can often
879 * result in r_rtt == 0. Since
880 * r_rtt == N means that the actual
881 * rtt is between N+dt and N+2-dt ticks,
885 t1 -= (NFS_SRTT(rep) >> 3);
889 t1 -= (NFS_SDRTT(rep) >> 2);
890 NFS_SDRTT(rep) += t1;
892 nmp->nm_timeouts = 0;
897 * If not matched to a request, drop it.
898 * If it's mine, get out.
901 nfsstats.rpcunexpected++;
903 } else if (rep == myrep) {
904 if (rep->r_mrep == NULL)
905 panic("nfsreply nil");
908 if (myrep->r_flags & R_GETONEREP)
914 * nfs_request - goes something like this
915 * - fill in request struct
916 * - links it into list
917 * - calls nfs_send() for first transmit
918 * - calls nfs_receive() to get reply
919 * - break down rpc header and return with nfs reply pointed to
921 * nb: always frees up mreq mbuf list
924 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
925 struct thread *td, struct ucred *cred, struct mbuf **mrp,
926 struct mbuf **mdp, caddr_t *dposp)
928 struct mbuf *mrep, *m2;
932 struct nfsmount *nmp;
933 struct mbuf *m, *md, *mheadend;
935 char nickv[RPCX_NICKVERF];
936 time_t reqtime, waituntil;
938 int t1, nqlflag, cachable, error = 0, mrest_len, auth_len, auth_type;
939 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
940 int verf_len, verf_type;
944 char *auth_str, *verf_str;
945 NFSKERBKEY_T key; /* save session key */
947 /* Reject requests while attempting a forced unmount. */
948 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
952 nmp = VFSTONFS(vp->v_mount);
953 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
957 rep->r_procnum = procnum;
967 * Get the RPC header with authorization.
970 verf_str = auth_str = (char *)0;
971 if (nmp->nm_flag & NFSMNT_KERB) {
973 verf_len = sizeof (nickv);
974 auth_type = RPCAUTH_KERB4;
975 bzero((caddr_t)key, sizeof (key));
976 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
977 &auth_len, verf_str, verf_len)) {
978 error = nfs_getauth(nmp, rep, cred, &auth_str,
979 &auth_len, verf_str, &verf_len, key);
981 free((caddr_t)rep, M_NFSREQ);
987 auth_type = RPCAUTH_UNIX;
988 if (cred->cr_ngroups < 1)
989 panic("nfsreq nogrps");
990 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
991 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
994 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
995 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
997 free(auth_str, M_TEMP);
1000 * For stream protocols, insert a Sun RPC Record Mark.
1002 if (nmp->nm_sotype == SOCK_STREAM) {
1003 M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
1005 free(rep, M_NFSREQ);
1008 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1009 (m->m_pkthdr.len - NFSX_UNSIGNED));
1014 if (nmp->nm_flag & NFSMNT_SOFT)
1015 rep->r_retry = nmp->nm_retry;
1017 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1018 rep->r_rtt = rep->r_rexmit = 0;
1019 if (proct[procnum] > 0)
1020 rep->r_flags = R_TIMING | R_MASKTIMER;
1022 rep->r_flags = R_MASKTIMER;
1026 * Do the client side RPC.
1028 nfsstats.rpcrequests++;
1031 * Chain request into list of outstanding requests. Be sure
1032 * to put it LAST so timer finds oldest requests first. Note
1033 * that R_MASKTIMER is set at the moment to prevent any timer
1034 * action on this request while we are still doing processing on
1035 * it below. splsoftclock() primarily protects nm_sent. Note
1036 * that we may block in this code so there is no atomicy guarentee.
1039 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1041 /* Get send time for nqnfs */
1042 reqtime = time_second;
1045 * If backing off another request or avoiding congestion, don't
1046 * send this one now but let timer do it. If not timing a request,
1049 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1050 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1051 nmp->nm_sent < nmp->nm_cwnd)) {
1052 if (nmp->nm_soflags & PR_CONNREQUIRED)
1053 error = nfs_sndlock(rep);
1055 m2 = m_copym(m, 0, M_COPYALL, MB_WAIT);
1056 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1057 if (nmp->nm_soflags & PR_CONNREQUIRED)
1060 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1061 nmp->nm_sent += NFS_CWNDSCALE;
1062 rep->r_flags |= R_SENT;
1069 * Let the timer do what it will with the request, then
1070 * wait for the reply from our send or the timer's.
1072 rep->r_flags &= ~R_MASKTIMER;
1074 if (!error || error == EPIPE)
1075 error = nfs_reply(rep);
1078 * RPC done, unlink the request.
1081 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1084 * Decrement the outstanding request count.
1086 if (rep->r_flags & R_SENT) {
1087 rep->r_flags &= ~R_SENT;
1088 nmp->nm_sent -= NFS_CWNDSCALE;
1093 * If there was a successful reply and a tprintf msg.
1094 * tprintf a response.
1096 if (!error && (rep->r_flags & R_TPRINTFMSG))
1097 nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1103 m_freem(rep->r_mreq);
1104 free((caddr_t)rep, M_NFSREQ);
1109 * break down the rpc header and check if ok
1111 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1112 if (*tl++ == rpc_msgdenied) {
1113 if (*tl == rpc_mismatch)
1115 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1118 mheadend->m_next = (struct mbuf *)0;
1120 m_freem(rep->r_mreq);
1127 m_freem(rep->r_mreq);
1128 free((caddr_t)rep, M_NFSREQ);
1133 * Grab any Kerberos verifier, otherwise just throw it away.
1135 verf_type = fxdr_unsigned(int, *tl++);
1136 i = fxdr_unsigned(int32_t, *tl);
1137 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1138 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1142 nfsm_adv(nfsm_rndup(i));
1143 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1146 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1148 error = fxdr_unsigned(int, *tl);
1149 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1150 error == NFSERR_TRYLATER) {
1153 waituntil = time_second + trylater_delay;
1154 while (time_second < waituntil)
1155 (void) tsleep((caddr_t)&lbolt,
1157 trylater_delay *= nfs_backoff[trylater_cnt];
1158 if (trylater_cnt < 7)
1164 * If the File Handle was stale, invalidate the
1165 * lookup cache, just in case.
1167 if (error == ESTALE) {
1169 cache_inval_vp(vp, CINV_CHILDREN, &retdummy);
1171 if (nmp->nm_flag & NFSMNT_NFSV3) {
1175 error |= NFSERR_RETERR;
1178 m_freem(rep->r_mreq);
1179 free((caddr_t)rep, M_NFSREQ);
1184 * For nqnfs, get any lease in reply
1186 if (nmp->nm_flag & NFSMNT_NQNFS) {
1187 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1190 nqlflag = fxdr_unsigned(int, *tl);
1191 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1192 cachable = fxdr_unsigned(int, *tl++);
1193 reqtime += fxdr_unsigned(int, *tl++);
1194 if (reqtime > time_second) {
1195 frev = fxdr_hyper(tl);
1196 nqnfs_clientlease(nmp, np, nqlflag,
1197 cachable, reqtime, frev);
1204 m_freem(rep->r_mreq);
1205 FREE((caddr_t)rep, M_NFSREQ);
1209 error = EPROTONOSUPPORT;
1211 m_freem(rep->r_mreq);
1212 free((caddr_t)rep, M_NFSREQ);
1216 #ifndef NFS_NOSERVER
1218 * Generate the rpc reply header
1219 * siz arg. is used to decide if adding a cluster is worthwhile
1222 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
1223 int err, int cache, u_quad_t *frev, struct mbuf **mrq,
1224 struct mbuf **mbp, caddr_t *bposp)
1229 struct mbuf *mb, *mb2;
1231 siz += RPC_REPLYSIZ;
1232 mb = mreq = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1233 mreq->m_pkthdr.len = 0;
1235 * If this is not a cluster, try and leave leading space
1236 * for the lower level headers.
1238 if ((max_hdr + siz) < MINCLSIZE)
1239 mreq->m_data += max_hdr;
1240 tl = mtod(mreq, u_int32_t *);
1241 mreq->m_len = 6 * NFSX_UNSIGNED;
1242 bpos = ((caddr_t)tl) + mreq->m_len;
1243 *tl++ = txdr_unsigned(nd->nd_retxid);
1245 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1246 *tl++ = rpc_msgdenied;
1247 if (err & NFSERR_AUTHERR) {
1248 *tl++ = rpc_autherr;
1249 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1250 mreq->m_len -= NFSX_UNSIGNED;
1251 bpos -= NFSX_UNSIGNED;
1253 *tl++ = rpc_mismatch;
1254 *tl++ = txdr_unsigned(RPC_VER2);
1255 *tl = txdr_unsigned(RPC_VER2);
1258 *tl++ = rpc_msgaccepted;
1261 * For Kerberos authentication, we must send the nickname
1262 * verifier back, otherwise just RPCAUTH_NULL.
1264 if (nd->nd_flag & ND_KERBFULL) {
1265 struct nfsuid *nuidp;
1266 struct timeval ktvin, ktvout;
1268 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1269 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1270 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1271 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1272 &nuidp->nu_haddr, nd->nd_nam2)))
1277 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1279 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1282 * Encrypt the timestamp in ecb mode using the
1289 *tl++ = rpc_auth_kerb;
1290 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1291 *tl = ktvout.tv_sec;
1292 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1293 *tl++ = ktvout.tv_usec;
1294 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1305 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1308 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1309 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1310 if (nd->nd_flag & ND_NQNFS) {
1311 *tl++ = txdr_unsigned(3);
1312 *tl = txdr_unsigned(3);
1314 *tl++ = txdr_unsigned(2);
1315 *tl = txdr_unsigned(3);
1319 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1322 *tl = txdr_unsigned(RPC_GARBAGE);
1326 if (err != NFSERR_RETVOID) {
1327 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1329 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1338 * For nqnfs, piggyback lease as requested.
1340 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1341 if (nd->nd_flag & ND_LEASE) {
1342 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1343 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1344 *tl++ = txdr_unsigned(cache);
1345 *tl++ = txdr_unsigned(nd->nd_duration);
1346 txdr_hyper(*frev, tl);
1348 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1356 if (err != 0 && err != NFSERR_RETVOID)
1357 nfsstats.srvrpc_errs++;
1362 #endif /* NFS_NOSERVER */
1365 * Scan the nfsreq list and retranmit any requests that have timed out
1366 * To avoid retransmission attempts on STREAM sockets (in the future) make
1367 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1370 nfs_timer(void *arg /* never used */)
1375 struct nfsmount *nmp;
1378 #ifndef NFS_NOSERVER
1379 static long lasttime = 0;
1380 struct nfssvc_sock *slp;
1382 #endif /* NFS_NOSERVER */
1383 struct thread *td = &thread0; /* XXX for credentials, will break if sleep */
1386 TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1388 if (rep->r_mrep || (rep->r_flags & (R_SOFTTERM|R_MASKTIMER)))
1390 if (nfs_sigintr(nmp, rep, rep->r_td)) {
1394 if (rep->r_rtt >= 0) {
1396 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1397 timeo = nmp->nm_timeo;
1399 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1400 if (nmp->nm_timeouts > 0)
1401 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1402 if (rep->r_rtt <= timeo)
1404 if (nmp->nm_timeouts < 8)
1408 * Check for server not responding
1410 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1411 rep->r_rexmit > nmp->nm_deadthresh) {
1413 nmp->nm_mountp->mnt_stat.f_mntfromname,
1415 rep->r_flags |= R_TPRINTFMSG;
1417 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1418 nfsstats.rpctimeouts++;
1422 if (nmp->nm_sotype != SOCK_DGRAM) {
1423 if (++rep->r_rexmit > NFS_MAXREXMIT)
1424 rep->r_rexmit = NFS_MAXREXMIT;
1427 if ((so = nmp->nm_so) == NULL)
1431 * If there is enough space and the window allows..
1433 * Set r_rtt to -1 in case we fail to send it now.
1436 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1437 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1438 (rep->r_flags & R_SENT) ||
1439 nmp->nm_sent < nmp->nm_cwnd) &&
1440 (m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
1441 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1442 error = so_pru_send(so, 0, m, (struct sockaddr *)0,
1443 (struct mbuf *)0, td);
1445 error = so_pru_send(so, 0, m, nmp->nm_nam,
1446 (struct mbuf *)0, td);
1448 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1452 * Iff first send, start timing
1453 * else turn timing off, backoff timer
1454 * and divide congestion window by 2.
1456 if (rep->r_flags & R_SENT) {
1457 rep->r_flags &= ~R_TIMING;
1458 if (++rep->r_rexmit > NFS_MAXREXMIT)
1459 rep->r_rexmit = NFS_MAXREXMIT;
1461 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1462 nmp->nm_cwnd = NFS_CWNDSCALE;
1463 nfsstats.rpcretries++;
1465 rep->r_flags |= R_SENT;
1466 nmp->nm_sent += NFS_CWNDSCALE;
1472 #ifndef NFS_NOSERVER
1474 * Call the nqnfs server timer once a second to handle leases.
1476 if (lasttime != time_second) {
1477 lasttime = time_second;
1482 * Scan the write gathering queues for writes that need to be
1485 cur_usec = nfs_curusec();
1486 TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1487 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1488 nfsrv_wakenfsd(slp, 1);
1490 #endif /* NFS_NOSERVER */
1492 callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL);
1496 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1497 * wait for all requests to complete. This is used by forced unmounts
1498 * to terminate any outstanding RPCs.
1501 nfs_nmcancelreqs(struct nfsmount *nmp)
1507 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1508 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1509 (req->r_flags & R_SOFTTERM))
1515 for (i = 0; i < 30; i++) {
1517 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1518 if (nmp == req->r_nmp)
1524 tsleep(&lbolt, 0, "nfscancel", 0);
1530 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1531 * The nm_send count is decremented now to avoid deadlocks when the process in
1532 * soreceive() hasn't yet managed to send its own request.
1534 * This routine must be called at splsoftclock() to protect r_flags and
1539 nfs_softterm(struct nfsreq *rep)
1541 rep->r_flags |= R_SOFTTERM;
1543 if (rep->r_flags & R_SENT) {
1544 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1545 rep->r_flags &= ~R_SENT;
1550 * Test for a termination condition pending on the process.
1551 * This is used for NFSMNT_INT mounts.
1554 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1559 if (rep && (rep->r_flags & R_SOFTTERM))
1561 /* Terminate all requests while attempting a forced unmount. */
1562 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1564 if (!(nmp->nm_flag & NFSMNT_INT))
1566 /* td might be NULL YYY */
1567 if (td == NULL || (p = td->td_proc) == NULL)
1570 tmpset = p->p_siglist;
1571 SIGSETNAND(tmpset, p->p_sigmask);
1572 SIGSETNAND(tmpset, p->p_sigignore);
1573 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1580 * Lock a socket against others.
1581 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1582 * and also to avoid race conditions between the processes with nfs requests
1583 * in progress when a reconnect is necessary.
1586 nfs_sndlock(struct nfsreq *rep)
1588 int *statep = &rep->r_nmp->nm_state;
1597 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1602 while (*statep & NFSSTA_SNDLOCK) {
1603 *statep |= NFSSTA_WANTSND;
1604 if (nfs_sigintr(rep->r_nmp, rep, td)) {
1608 tsleep((caddr_t)statep, slpflag, "nfsndlck", slptimeo);
1609 if (slpflag == PCATCH) {
1614 /* Always fail if our request has been cancelled. */
1615 if ((rep->r_flags & R_SOFTTERM))
1618 *statep |= NFSSTA_SNDLOCK;
1624 * Unlock the stream socket for others.
1627 nfs_sndunlock(struct nfsreq *rep)
1629 int *statep = &rep->r_nmp->nm_state;
1631 if ((*statep & NFSSTA_SNDLOCK) == 0)
1632 panic("nfs sndunlock");
1634 *statep &= ~NFSSTA_SNDLOCK;
1635 if (*statep & NFSSTA_WANTSND) {
1636 *statep &= ~NFSSTA_WANTSND;
1637 wakeup((caddr_t)statep);
1643 nfs_rcvlock(struct nfsreq *rep)
1645 int *statep = &rep->r_nmp->nm_state;
1651 * Unconditionally check for completion in case another nfsiod
1652 * get the packet while the caller was blocked, before the caller
1653 * called us. Packet reception is handled by mainline code which
1654 * is protected by the BGL at the moment.
1656 * We do not strictly need the second check just before the
1657 * tsleep(), but it's good defensive programming.
1659 if (rep->r_mrep != NULL)
1662 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1669 while (*statep & NFSSTA_RCVLOCK) {
1670 if (nfs_sigintr(rep->r_nmp, rep, rep->r_td)) {
1674 if (rep->r_mrep != NULL) {
1678 *statep |= NFSSTA_WANTRCV;
1679 tsleep((caddr_t)statep, slpflag, "nfsrcvlk", slptimeo);
1681 * If our reply was recieved while we were sleeping,
1682 * then just return without taking the lock to avoid a
1683 * situation where a single iod could 'capture' the
1686 if (rep->r_mrep != NULL) {
1690 if (slpflag == PCATCH) {
1696 *statep |= NFSSTA_RCVLOCK;
1697 rep->r_nmp->nm_rcvlock_td = curthread; /* DEBUGGING */
1704 * Unlock the stream socket for others.
1707 nfs_rcvunlock(struct nfsreq *rep)
1709 int *statep = &rep->r_nmp->nm_state;
1711 if ((*statep & NFSSTA_RCVLOCK) == 0)
1712 panic("nfs rcvunlock");
1714 rep->r_nmp->nm_rcvlock_td = (void *)-1; /* DEBUGGING */
1715 *statep &= ~NFSSTA_RCVLOCK;
1716 if (*statep & NFSSTA_WANTRCV) {
1717 *statep &= ~NFSSTA_WANTRCV;
1718 wakeup((caddr_t)statep);
1726 * Check for badly aligned mbuf data and realign by copying the unaligned
1727 * portion of the data into a new mbuf chain and freeing the portions
1728 * of the old chain that were replaced.
1730 * We cannot simply realign the data within the existing mbuf chain
1731 * because the underlying buffers may contain other rpc commands and
1732 * we cannot afford to overwrite them.
1734 * We would prefer to avoid this situation entirely. The situation does
1735 * not occur with NFS/UDP and is supposed to only occassionally occur
1736 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1739 nfs_realign(struct mbuf **pm, int hsiz)
1742 struct mbuf *n = NULL;
1747 while ((m = *pm) != NULL) {
1748 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1749 n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL);
1757 * If n is non-NULL, loop on m copying data, then replace the
1758 * portion of the chain that had to be realigned.
1761 ++nfs_realign_count;
1763 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1772 #ifndef NFS_NOSERVER
1775 * Parse an RPC request
1777 * - fill in the cred struct.
1780 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
1787 caddr_t dpos, cp2, cp;
1788 u_int32_t nfsvers, auth_type;
1790 int error = 0, nqnfs = 0, ticklen;
1791 struct mbuf *mrep, *md;
1792 struct nfsuid *nuidp;
1793 struct timeval tvin, tvout;
1794 #if 0 /* until encrypted keys are implemented */
1795 NFSKERBKEYSCHED_T keys; /* stores key schedule */
1802 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1803 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1804 if (*tl++ != rpc_call) {
1809 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1812 if (*tl++ != rpc_vers) {
1813 nd->nd_repstat = ERPCMISMATCH;
1814 nd->nd_procnum = NFSPROC_NOOP;
1817 if (*tl != nfs_prog) {
1818 if (*tl == nqnfs_prog)
1821 nd->nd_repstat = EPROGUNAVAIL;
1822 nd->nd_procnum = NFSPROC_NOOP;
1827 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1828 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1829 (nfsvers != NQNFS_VER3 && nqnfs)) {
1830 nd->nd_repstat = EPROGMISMATCH;
1831 nd->nd_procnum = NFSPROC_NOOP;
1835 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1836 else if (nfsvers == NFS_VER3)
1837 nd->nd_flag = ND_NFSV3;
1838 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1839 if (nd->nd_procnum == NFSPROC_NULL)
1841 if (nd->nd_procnum >= NFS_NPROCS ||
1842 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1843 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1844 nd->nd_repstat = EPROCUNAVAIL;
1845 nd->nd_procnum = NFSPROC_NOOP;
1848 if ((nd->nd_flag & ND_NFSV3) == 0)
1849 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1851 len = fxdr_unsigned(int, *tl++);
1852 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1857 nd->nd_flag &= ~ND_KERBAUTH;
1859 * Handle auth_unix or auth_kerb.
1861 if (auth_type == rpc_auth_unix) {
1862 len = fxdr_unsigned(int, *++tl);
1863 if (len < 0 || len > NFS_MAXNAMLEN) {
1867 nfsm_adv(nfsm_rndup(len));
1868 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1869 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1870 nd->nd_cr.cr_ref = 1;
1871 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1872 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1873 len = fxdr_unsigned(int, *tl);
1874 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1878 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1879 for (i = 1; i <= len; i++)
1881 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1884 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
1885 if (nd->nd_cr.cr_ngroups > 1)
1886 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1887 len = fxdr_unsigned(int, *++tl);
1888 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1893 nfsm_adv(nfsm_rndup(len));
1894 } else if (auth_type == rpc_auth_kerb) {
1895 switch (fxdr_unsigned(int, *tl++)) {
1896 case RPCAKN_FULLNAME:
1897 ticklen = fxdr_unsigned(int, *tl);
1898 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1899 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1900 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1901 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1908 uio.uio_segflg = UIO_SYSSPACE;
1909 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1910 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1911 nfsm_mtouio(&uio, uio.uio_resid);
1912 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1913 if (*tl++ != rpc_auth_kerb ||
1914 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1915 printf("Bad kerb verifier\n");
1916 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1917 nd->nd_procnum = NFSPROC_NOOP;
1920 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1921 tl = (u_int32_t *)cp;
1922 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1923 printf("Not fullname kerb verifier\n");
1924 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1925 nd->nd_procnum = NFSPROC_NOOP;
1928 cp += NFSX_UNSIGNED;
1929 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1930 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1931 nd->nd_flag |= ND_KERBFULL;
1932 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1934 case RPCAKN_NICKNAME:
1935 if (len != 2 * NFSX_UNSIGNED) {
1936 printf("Kerb nickname short\n");
1937 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1938 nd->nd_procnum = NFSPROC_NOOP;
1941 nickuid = fxdr_unsigned(uid_t, *tl);
1942 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1943 if (*tl++ != rpc_auth_kerb ||
1944 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1945 printf("Kerb nick verifier bad\n");
1946 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1947 nd->nd_procnum = NFSPROC_NOOP;
1950 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1951 tvin.tv_sec = *tl++;
1954 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
1955 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1956 if (nuidp->nu_cr.cr_uid == nickuid &&
1958 netaddr_match(NU_NETFAM(nuidp),
1959 &nuidp->nu_haddr, nd->nd_nam2)))
1964 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1965 nd->nd_procnum = NFSPROC_NOOP;
1970 * Now, decrypt the timestamp using the session key
1977 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1978 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1979 if (nuidp->nu_expire < time_second ||
1980 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1981 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1982 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1983 nuidp->nu_expire = 0;
1985 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1986 nd->nd_procnum = NFSPROC_NOOP;
1989 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1990 nd->nd_flag |= ND_KERBNICK;
1993 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1994 nd->nd_procnum = NFSPROC_NOOP;
1999 * For nqnfs, get piggybacked lease request.
2001 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
2002 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2003 nd->nd_flag |= fxdr_unsigned(int, *tl);
2004 if (nd->nd_flag & ND_LEASE) {
2005 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
2006 nd->nd_duration = fxdr_unsigned(int32_t, *tl);
2008 nd->nd_duration = NQ_MINLEASE;
2010 nd->nd_duration = NQ_MINLEASE;
2021 * Send a message to the originating process's terminal. The thread and/or
2022 * process may be NULL. YYY the thread should not be NULL but there may
2023 * still be some uio_td's that are still being passed as NULL through to
2027 nfs_msg(struct thread *td, char *server, char *msg)
2031 if (td && td->td_proc)
2032 tpr = tprintf_open(td->td_proc);
2035 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2040 #ifndef NFS_NOSERVER
2042 * Socket upcall routine for the nfsd sockets.
2043 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2044 * Essentially do as much as possible non-blocking, else punt and it will
2045 * be called with MB_WAIT from an nfsd.
2048 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
2050 struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2053 struct sockaddr *nam;
2056 int nparallel_wakeup = 0;
2058 if ((slp->ns_flag & SLP_VALID) == 0)
2062 * Do not allow an infinite number of completed RPC records to build
2063 * up before we stop reading data from the socket. Otherwise we could
2064 * end up holding onto an unreasonable number of mbufs for requests
2065 * waiting for service.
2067 * This should give pretty good feedback to the TCP
2068 * layer and prevents a memory crunch for other protocols.
2070 * Note that the same service socket can be dispatched to several
2071 * nfs servers simultaniously.
2073 * the tcp protocol callback calls us with MB_DONTWAIT.
2074 * nfsd calls us with MB_WAIT (typically).
2076 if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) {
2077 slp->ns_flag |= SLP_NEEDQ;
2082 * Handle protocol specifics to parse an RPC request. We always
2083 * pull from the socket using non-blocking I/O.
2086 if (so->so_type == SOCK_STREAM) {
2088 * The data has to be read in an orderly fashion from a TCP
2089 * stream, unlike a UDP socket. It is possible for soreceive
2090 * and/or nfsrv_getstream() to block, so make sure only one
2091 * entity is messing around with the TCP stream at any given
2092 * moment. The receive sockbuf's lock in soreceive is not
2095 * Note that this procedure can be called from any number of
2096 * NFS severs *OR* can be upcalled directly from a TCP
2099 if (slp->ns_flag & SLP_GETSTREAM) {
2100 slp->ns_flag |= SLP_NEEDQ;
2103 slp->ns_flag |= SLP_GETSTREAM;
2108 auio.uio_resid = 1000000000;
2109 flags = MSG_DONTWAIT;
2110 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL, &flags);
2111 if (error || mp == (struct mbuf *)0) {
2112 if (error == EWOULDBLOCK)
2113 slp->ns_flag |= SLP_NEEDQ;
2115 slp->ns_flag |= SLP_DISCONN;
2116 slp->ns_flag &= ~SLP_GETSTREAM;
2120 if (slp->ns_rawend) {
2121 slp->ns_rawend->m_next = m;
2122 slp->ns_cc += 1000000000 - auio.uio_resid;
2125 slp->ns_cc = 1000000000 - auio.uio_resid;
2132 * Now try and parse as many record(s) as we can out of the
2135 error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
2138 slp->ns_flag |= SLP_DISCONN;
2140 slp->ns_flag |= SLP_NEEDQ;
2142 slp->ns_flag &= ~SLP_GETSTREAM;
2145 * For UDP soreceive typically pulls just one packet, loop
2146 * to get the whole batch.
2149 auio.uio_resid = 1000000000;
2150 flags = MSG_DONTWAIT;
2151 error = so_pru_soreceive(so, &nam, &auio, &mp, NULL,
2154 struct nfsrv_rec *rec;
2155 int mf = (waitflag & MB_DONTWAIT) ?
2156 M_NOWAIT : M_WAITOK;
2157 rec = malloc(sizeof(struct nfsrv_rec),
2161 FREE(nam, M_SONAME);
2165 nfs_realign(&mp, 10 * NFSX_UNSIGNED);
2166 rec->nr_address = nam;
2167 rec->nr_packet = mp;
2168 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2173 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2174 && error != EWOULDBLOCK) {
2175 slp->ns_flag |= SLP_DISCONN;
2183 * If we were upcalled from the tcp protocol layer and we have
2184 * fully parsed records ready to go, or there is new data pending,
2185 * or something went wrong, try to wake up an nfsd thread to deal
2189 if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0
2190 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2191 nfsrv_wakenfsd(slp, nparallel_wakeup);
2196 * Try and extract an RPC request from the mbuf data list received on a
2197 * stream socket. The "waitflag" argument indicates whether or not it
2201 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
2203 struct mbuf *m, **mpp;
2206 struct mbuf *om, *m2, *recm;
2210 if (slp->ns_reclen == 0) {
2211 if (slp->ns_cc < NFSX_UNSIGNED)
2214 if (m->m_len >= NFSX_UNSIGNED) {
2215 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2216 m->m_data += NFSX_UNSIGNED;
2217 m->m_len -= NFSX_UNSIGNED;
2219 cp1 = (caddr_t)&recmark;
2220 cp2 = mtod(m, caddr_t);
2221 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2222 while (m->m_len == 0) {
2224 cp2 = mtod(m, caddr_t);
2231 slp->ns_cc -= NFSX_UNSIGNED;
2232 recmark = ntohl(recmark);
2233 slp->ns_reclen = recmark & ~0x80000000;
2234 if (recmark & 0x80000000)
2235 slp->ns_flag |= SLP_LASTFRAG;
2237 slp->ns_flag &= ~SLP_LASTFRAG;
2238 if (slp->ns_reclen > NFS_MAXPACKET) {
2239 log(LOG_ERR, "%s (%d) from nfs client\n",
2240 "impossible packet length",
2247 * Now get the record part.
2249 * Note that slp->ns_reclen may be 0. Linux sometimes
2250 * generates 0-length RPCs
2253 if (slp->ns_cc == slp->ns_reclen) {
2255 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2256 slp->ns_cc = slp->ns_reclen = 0;
2257 } else if (slp->ns_cc > slp->ns_reclen) {
2260 om = (struct mbuf *)0;
2262 while (len < slp->ns_reclen) {
2263 if ((len + m->m_len) > slp->ns_reclen) {
2264 m2 = m_copym(m, 0, slp->ns_reclen - len,
2272 m->m_data += slp->ns_reclen - len;
2273 m->m_len -= slp->ns_reclen - len;
2274 len = slp->ns_reclen;
2276 return (EWOULDBLOCK);
2278 } else if ((len + m->m_len) == slp->ns_reclen) {
2283 om->m_next = (struct mbuf *)0;
2298 * Accumulate the fragments into a record.
2300 mpp = &slp->ns_frag;
2302 mpp = &((*mpp)->m_next);
2304 if (slp->ns_flag & SLP_LASTFRAG) {
2305 struct nfsrv_rec *rec;
2306 int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
2307 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
2309 m_freem(slp->ns_frag);
2311 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2312 rec->nr_address = (struct sockaddr *)0;
2313 rec->nr_packet = slp->ns_frag;
2314 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2318 slp->ns_frag = (struct mbuf *)0;
2324 * Parse an RPC header.
2327 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2328 struct nfsrv_descript **ndp)
2330 struct nfsrv_rec *rec;
2332 struct sockaddr *nam;
2333 struct nfsrv_descript *nd;
2337 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2339 rec = STAILQ_FIRST(&slp->ns_rec);
2340 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
2341 KKASSERT(slp->ns_numrec > 0);
2343 nam = rec->nr_address;
2345 free(rec, M_NFSRVDESC);
2346 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2347 M_NFSRVDESC, M_WAITOK);
2348 nd->nd_md = nd->nd_mrep = m;
2350 nd->nd_dpos = mtod(m, caddr_t);
2351 error = nfs_getreq(nd, nfsd, TRUE);
2354 FREE(nam, M_SONAME);
2356 free((caddr_t)nd, M_NFSRVDESC);
2365 * Try to assign service sockets to nfsd threads based on the number
2366 * of new rpc requests that have been queued on the service socket.
2368 * If no nfsd's are available or additonal requests are pending, set the
2369 * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2370 * the work in the nfssvc_sock list when it is finished processing its
2371 * current work. This flag is only cleared when an nfsd can not find
2372 * any new work to perform.
2375 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
2379 if ((slp->ns_flag & SLP_VALID) == 0)
2383 TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
2384 if (nd->nfsd_flag & NFSD_WAITING) {
2385 nd->nfsd_flag &= ~NFSD_WAITING;
2387 panic("nfsd wakeup");
2390 wakeup((caddr_t)nd);
2391 if (--nparallel == 0)
2396 slp->ns_flag |= SLP_DOREC;
2397 nfsd_head_flag |= NFSD_CHECKSLP;
2400 #endif /* NFS_NOSERVER */