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 $
41 * Socket operations for use by nfs
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/kernel.h>
51 #include <sys/vnode.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/syslog.h>
56 #include <sys/tprintf.h>
57 #include <sys/sysctl.h>
58 #include <sys/signalvar.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsmount.h>
69 #include <nfs/nfsnode.h>
70 #include <nfs/nfsrtt.h>
71 #include <nfs/nqnfs.h>
77 * Estimate rto for an nfs rpc sent via. an unreliable datagram.
78 * Use the mean and mean deviation of rtt for the appropriate type of rpc
79 * for the frequent rpcs and a default for the others.
80 * The justification for doing "other" this way is that these rpcs
81 * happen so infrequently that timer est. would probably be stale.
82 * Also, since many of these rpcs are
83 * non-idempotent, a conservative timeout is desired.
84 * getattr, lookup - A+2D
88 #define NFS_RTO(n, t) \
89 ((t) == 0 ? (n)->nm_timeo : \
91 (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
92 ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
93 #define NFS_SRTT(r) (r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
94 #define NFS_SDRTT(r) (r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
96 * External data, mostly RPC constants in XDR form
98 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
99 rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
101 extern u_int32_t nfs_prog, nqnfs_prog;
102 extern time_t nqnfsstarttime;
103 extern struct nfsstats nfsstats;
104 extern int nfsv3_procid[NFS_NPROCS];
105 extern int nfs_ticks;
108 * Defines which timer to use for the procnum.
115 static int proct[NFS_NPROCS] = {
116 0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
120 static int nfs_realign_test;
121 static int nfs_realign_count;
122 static int nfs_bufpackets = 4;
124 SYSCTL_DECL(_vfs_nfs);
126 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
127 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
128 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
132 * There is a congestion window for outstanding rpcs maintained per mount
133 * point. The cwnd size is adjusted in roughly the way that:
134 * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
135 * SIGCOMM '88". ACM, August 1988.
136 * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
137 * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
138 * of rpcs is in progress.
139 * (The sent count and cwnd are scaled for integer arith.)
140 * Variants of "slow start" were tried and were found to be too much of a
141 * performance hit (ave. rtt 3 times larger),
142 * I suspect due to the large rtt that nfs rpcs have.
144 #define NFS_CWNDSCALE 256
145 #define NFS_MAXCWND (NFS_CWNDSCALE * 32)
146 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
148 struct nfsrtt nfsrtt;
149 struct callout_handle nfs_timer_handle;
151 static int nfs_msg __P((struct proc *,char *,char *));
152 static int nfs_rcvlock __P((struct nfsreq *));
153 static void nfs_rcvunlock __P((struct nfsreq *));
154 static void nfs_realign __P((struct mbuf **pm, int hsiz));
155 static int nfs_receive __P((struct nfsreq *rep, struct sockaddr **aname,
157 static void nfs_softterm __P((struct nfsreq *rep));
158 static int nfs_reconnect __P((struct nfsreq *rep));
160 static int nfsrv_getstream __P((struct nfssvc_sock *,int));
162 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *nd,
163 struct nfssvc_sock *slp,
165 struct mbuf **mreqp)) = {
193 #endif /* NFS_NOSERVER */
196 * Initialize sockets and congestion for a new NFS connection.
197 * We do not free the sockaddr if error.
200 nfs_connect(nmp, rep)
201 register struct nfsmount *nmp;
204 register struct socket *so;
205 int s, error, rcvreserve, sndreserve;
207 struct sockaddr *saddr;
208 struct sockaddr_in *sin;
209 struct proc *p = &proc0; /* only used for socreate and sobind */
211 nmp->nm_so = (struct socket *)0;
213 error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
218 nmp->nm_soflags = so->so_proto->pr_flags;
221 * Some servers require that the client port be a reserved port number.
223 if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
226 struct sockaddr_in ssin;
228 bzero(&sopt, sizeof sopt);
229 ip = IP_PORTRANGE_LOW;
230 sopt.sopt_dir = SOPT_SET;
231 sopt.sopt_level = IPPROTO_IP;
232 sopt.sopt_name = IP_PORTRANGE;
233 sopt.sopt_val = (void *)&ip;
234 sopt.sopt_valsize = sizeof(ip);
236 error = sosetopt(so, &sopt);
239 bzero(&ssin, sizeof ssin);
241 sin->sin_len = sizeof (struct sockaddr_in);
242 sin->sin_family = AF_INET;
243 sin->sin_addr.s_addr = INADDR_ANY;
244 sin->sin_port = htons(0);
245 error = sobind(so, (struct sockaddr *)sin, p);
248 bzero(&sopt, sizeof sopt);
249 ip = IP_PORTRANGE_DEFAULT;
250 sopt.sopt_dir = SOPT_SET;
251 sopt.sopt_level = IPPROTO_IP;
252 sopt.sopt_name = IP_PORTRANGE;
253 sopt.sopt_val = (void *)&ip;
254 sopt.sopt_valsize = sizeof(ip);
256 error = sosetopt(so, &sopt);
262 * Protocols that do not require connections may be optionally left
263 * unconnected for servers that reply from a port other than NFS_PORT.
265 if (nmp->nm_flag & NFSMNT_NOCONN) {
266 if (nmp->nm_soflags & PR_CONNREQUIRED) {
271 error = soconnect(so, nmp->nm_nam, p);
276 * Wait for the connection to complete. Cribbed from the
277 * connect system call but with the wait timing out so
278 * that interruptible mounts don't hang here for a long time.
281 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
282 (void) tsleep((caddr_t)&so->so_timeo, PSOCK,
284 if ((so->so_state & SS_ISCONNECTING) &&
285 so->so_error == 0 && rep &&
286 (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
287 so->so_state &= ~SS_ISCONNECTING;
293 error = so->so_error;
300 so->so_rcv.sb_timeo = (5 * hz);
301 so->so_snd.sb_timeo = (5 * hz);
304 * Get buffer reservation size from sysctl, but impose reasonable
307 pktscale = nfs_bufpackets;
313 if (nmp->nm_sotype == SOCK_DGRAM) {
314 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
315 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
316 NFS_MAXPKTHDR) * pktscale;
317 } else if (nmp->nm_sotype == SOCK_SEQPACKET) {
318 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
319 rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
320 NFS_MAXPKTHDR) * pktscale;
322 if (nmp->nm_sotype != SOCK_STREAM)
323 panic("nfscon sotype");
324 if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
328 bzero(&sopt, sizeof sopt);
329 sopt.sopt_level = SOL_SOCKET;
330 sopt.sopt_name = SO_KEEPALIVE;
331 sopt.sopt_val = &val;
332 sopt.sopt_valsize = sizeof val;
336 if (so->so_proto->pr_protocol == IPPROTO_TCP) {
340 bzero(&sopt, sizeof sopt);
341 sopt.sopt_level = IPPROTO_TCP;
342 sopt.sopt_name = TCP_NODELAY;
343 sopt.sopt_val = &val;
344 sopt.sopt_valsize = sizeof val;
348 sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
349 sizeof (u_int32_t)) * pktscale;
350 rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
351 sizeof (u_int32_t)) * pktscale;
353 error = soreserve(so, sndreserve, rcvreserve);
356 so->so_rcv.sb_flags |= SB_NOINTR;
357 so->so_snd.sb_flags |= SB_NOINTR;
359 /* Initialize other non-zero congestion variables */
360 nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
361 nmp->nm_srtt[3] = (NFS_TIMEO << 3);
362 nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
363 nmp->nm_sdrtt[3] = 0;
364 nmp->nm_cwnd = NFS_MAXCWND / 2; /* Initial send window */
366 nmp->nm_timeouts = 0;
376 * Called when a connection is broken on a reliable protocol.
377 * - clean up the old socket
378 * - nfs_connect() again
379 * - set R_MUSTRESEND for all outstanding requests on mount point
380 * If this fails the mount point is DEAD!
381 * nb: Must be called with the nfs_sndlock() set on the mount point.
385 register struct nfsreq *rep;
387 register struct nfsreq *rp;
388 register struct nfsmount *nmp = rep->r_nmp;
392 while ((error = nfs_connect(nmp, rep)) != 0) {
393 if (error == EINTR || error == ERESTART)
395 (void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
399 * Loop through outstanding request list and fix up all requests
402 for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) {
403 if (rp->r_nmp == nmp)
404 rp->r_flags |= R_MUSTRESEND;
410 * NFS disconnect. Clean up and unlink.
414 register struct nfsmount *nmp;
416 register struct socket *so;
420 nmp->nm_so = (struct socket *)0;
427 nfs_safedisconnect(nmp)
428 struct nfsmount *nmp;
430 struct nfsreq dummyreq;
432 bzero(&dummyreq, sizeof(dummyreq));
433 dummyreq.r_nmp = nmp;
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(so, nam, top, rep)
454 register struct socket *so;
455 struct sockaddr *nam;
456 register struct mbuf *top;
459 struct sockaddr *sendnam;
460 int error, soflags, flags;
463 if (rep->r_flags & R_SOFTTERM) {
467 if ((so = rep->r_nmp->nm_so) == NULL) {
468 rep->r_flags |= R_MUSTRESEND;
472 rep->r_flags &= ~R_MUSTRESEND;
473 soflags = rep->r_nmp->nm_soflags;
475 soflags = so->so_proto->pr_flags;
476 if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
477 sendnam = (struct sockaddr *)0;
480 if (so->so_type == SOCK_SEQPACKET)
485 error = so->so_proto->pr_usrreqs->pru_sosend(so, sendnam, 0, top, 0,
486 flags, curproc /*XXX*/);
488 * ENOBUFS for dgram sockets is transient and non fatal.
489 * No need to log, and no need to break a soft mount.
491 if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
493 if (rep) /* do backoff retransmit on client */
494 rep->r_flags |= R_MUSTRESEND;
499 log(LOG_INFO, "nfs send error %d for server %s\n",error,
500 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
502 * Deal with errors for the client side.
504 if (rep->r_flags & R_SOFTTERM)
507 rep->r_flags |= R_MUSTRESEND;
509 log(LOG_INFO, "nfsd send error %d\n", error);
512 * Handle any recoverable (soft) socket errors here. (?)
514 if (error != EINTR && error != ERESTART &&
515 error != EWOULDBLOCK && error != EPIPE)
522 * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
523 * done by soreceive(), but for SOCK_STREAM we must deal with the Record
524 * Mark and consolidate the data into a new mbuf list.
525 * nb: Sometimes TCP passes the data up to soreceive() in long lists of
527 * For SOCK_STREAM we must be very careful to read an entire record once
528 * we have read any of it, even if the system call has been interrupted.
531 nfs_receive(rep, aname, mp)
532 register struct nfsreq *rep;
533 struct sockaddr **aname;
536 register struct socket *so;
539 register struct mbuf *m;
540 struct mbuf *control;
542 struct sockaddr **getnam;
543 int error, sotype, rcvflg;
544 struct proc *p = curproc; /* XXX */
547 * Set up arguments for soreceive()
549 *mp = (struct mbuf *)0;
550 *aname = (struct sockaddr *)0;
551 sotype = rep->r_nmp->nm_sotype;
554 * For reliable protocols, lock against other senders/receivers
555 * in case a reconnect is necessary.
556 * For SOCK_STREAM, first get the Record Mark to find out how much
557 * more there is to get.
558 * We must lock the socket against other receivers
559 * until we have an entire rpc request/reply.
561 if (sotype != SOCK_DGRAM) {
562 error = nfs_sndlock(rep);
567 * Check for fatal errors and resending request.
570 * Ugh: If a reconnect attempt just happened, nm_so
571 * would have changed. NULL indicates a failed
572 * attempt that has essentially shut down this
575 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
579 so = rep->r_nmp->nm_so;
581 error = nfs_reconnect(rep);
588 while (rep->r_flags & R_MUSTRESEND) {
589 m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
590 nfsstats.rpcretries++;
591 error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
593 if (error == EINTR || error == ERESTART ||
594 (error = nfs_reconnect(rep)) != 0) {
602 if (sotype == SOCK_STREAM) {
603 aio.iov_base = (caddr_t) &len;
604 aio.iov_len = sizeof(u_int32_t);
607 auio.uio_segflg = UIO_SYSSPACE;
608 auio.uio_rw = UIO_READ;
610 auio.uio_resid = sizeof(u_int32_t);
613 rcvflg = MSG_WAITALL;
614 error = so->so_proto->pr_usrreqs->pru_soreceive
615 (so, (struct sockaddr **)0, &auio,
616 (struct mbuf **)0, (struct mbuf **)0,
618 if (error == EWOULDBLOCK && rep) {
619 if (rep->r_flags & R_SOFTTERM)
622 } while (error == EWOULDBLOCK);
623 if (!error && auio.uio_resid > 0) {
625 * Don't log a 0 byte receive; it means
626 * that the socket has been closed, and
627 * can happen during normal operation
628 * (forcible unmount or Solaris server).
630 if (auio.uio_resid != sizeof (u_int32_t))
632 "short receive (%d/%d) from nfs server %s\n",
633 (int)(sizeof(u_int32_t) - auio.uio_resid),
634 (int)sizeof(u_int32_t),
635 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
640 len = ntohl(len) & ~0x80000000;
642 * This is SERIOUS! We are out of sync with the sender
643 * and forcing a disconnect/reconnect is all I can do.
645 if (len > NFS_MAXPACKET) {
646 log(LOG_ERR, "%s (%d) from nfs server %s\n",
647 "impossible packet length",
649 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
653 auio.uio_resid = len;
655 rcvflg = MSG_WAITALL;
656 error = so->so_proto->pr_usrreqs->pru_soreceive
657 (so, (struct sockaddr **)0,
658 &auio, mp, (struct mbuf **)0, &rcvflg);
659 } while (error == EWOULDBLOCK || error == EINTR ||
661 if (!error && auio.uio_resid > 0) {
662 if (len != auio.uio_resid)
664 "short receive (%d/%d) from nfs server %s\n",
665 len - auio.uio_resid, len,
666 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
671 * NB: Since uio_resid is big, MSG_WAITALL is ignored
672 * and soreceive() will return when it has either a
673 * control msg or a data msg.
674 * We have no use for control msg., but must grab them
675 * and then throw them away so we know what is going
678 auio.uio_resid = len = 100000000; /* Anything Big */
682 error = so->so_proto->pr_usrreqs->pru_soreceive
683 (so, (struct sockaddr **)0,
684 &auio, mp, &control, &rcvflg);
687 if (error == EWOULDBLOCK && rep) {
688 if (rep->r_flags & R_SOFTTERM)
691 } while (error == EWOULDBLOCK ||
692 (!error && *mp == NULL && control));
693 if ((rcvflg & MSG_EOR) == 0)
695 if (!error && *mp == NULL)
697 len -= auio.uio_resid;
700 if (error && error != EINTR && error != ERESTART) {
702 *mp = (struct mbuf *)0;
705 "receive error %d from nfs server %s\n",
707 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
708 error = nfs_sndlock(rep);
710 error = nfs_reconnect(rep);
718 if ((so = rep->r_nmp->nm_so) == NULL)
720 if (so->so_state & SS_ISCONNECTED)
721 getnam = (struct sockaddr **)0;
724 auio.uio_resid = len = 1000000;
728 error = so->so_proto->pr_usrreqs->pru_soreceive
729 (so, getnam, &auio, mp,
730 (struct mbuf **)0, &rcvflg);
731 if (error == EWOULDBLOCK &&
732 (rep->r_flags & R_SOFTTERM))
734 } while (error == EWOULDBLOCK);
735 len -= auio.uio_resid;
739 *mp = (struct mbuf *)0;
742 * Search for any mbufs that are not a multiple of 4 bytes long
743 * or with m_data not longword aligned.
744 * These could cause pointer alignment problems, so copy them to
745 * well aligned mbufs.
747 nfs_realign(mp, 5 * NFSX_UNSIGNED);
752 * Implement receipt of reply on a socket.
753 * We must search through the list of received datagrams matching them
754 * with outstanding requests using the xid, until ours is found.
759 struct nfsreq *myrep;
761 register struct nfsreq *rep;
762 register struct nfsmount *nmp = myrep->r_nmp;
764 struct mbuf *mrep, *md;
765 struct sockaddr *nam;
771 * Loop around until we get our own reply
775 * Lock against other receivers so that I don't get stuck in
776 * sbwait() after someone else has received my reply for me.
777 * Also necessary for connection based protocols to avoid
778 * race conditions during a reconnect.
779 * If nfs_rcvlock() returns EALREADY, that means that
780 * the reply has already been recieved by another
781 * process and we can return immediately. In this
782 * case, the lock is not taken to avoid races with
785 error = nfs_rcvlock(myrep);
786 if (error == EALREADY)
791 * Get the next Rpc reply off the socket
793 error = nfs_receive(myrep, &nam, &mrep);
794 nfs_rcvunlock(myrep);
798 * Ignore routing errors on connectionless protocols??
800 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
801 nmp->nm_so->so_error = 0;
802 if (myrep->r_flags & R_GETONEREP)
812 * Get the xid and check that it is an rpc reply
815 dpos = mtod(md, caddr_t);
816 nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
818 if (*tl != rpc_reply) {
820 if (nmp->nm_flag & NFSMNT_NQNFS) {
821 if (nqnfs_callback(nmp, mrep, md, dpos))
822 nfsstats.rpcinvalid++;
824 nfsstats.rpcinvalid++;
828 nfsstats.rpcinvalid++;
832 if (myrep->r_flags & R_GETONEREP)
838 * Loop through the request list to match up the reply
839 * Iff no match, just drop the datagram
841 for (rep = nfs_reqq.tqh_first; rep != 0;
842 rep = rep->r_chain.tqe_next) {
843 if (rep->r_mrep == NULL && rxid == rep->r_xid) {
851 rt = &nfsrtt.rttl[nfsrtt.pos];
852 rt->proc = rep->r_procnum;
853 rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
854 rt->sent = nmp->nm_sent;
855 rt->cwnd = nmp->nm_cwnd;
856 rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
857 rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
858 rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
859 getmicrotime(&rt->tstamp);
860 if (rep->r_flags & R_TIMING)
861 rt->rtt = rep->r_rtt;
864 nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
867 * Update congestion window.
868 * Do the additive increase of
871 if (nmp->nm_cwnd <= nmp->nm_sent) {
873 (NFS_CWNDSCALE * NFS_CWNDSCALE +
874 (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
875 if (nmp->nm_cwnd > NFS_MAXCWND)
876 nmp->nm_cwnd = NFS_MAXCWND;
878 if (rep->r_flags & R_SENT) {
879 rep->r_flags &= ~R_SENT;
880 nmp->nm_sent -= NFS_CWNDSCALE;
883 * Update rtt using a gain of 0.125 on the mean
884 * and a gain of 0.25 on the deviation.
886 if (rep->r_flags & R_TIMING) {
888 * Since the timer resolution of
889 * NFS_HZ is so course, it can often
890 * result in r_rtt == 0. Since
891 * r_rtt == N means that the actual
892 * rtt is between N+dt and N+2-dt ticks,
896 t1 -= (NFS_SRTT(rep) >> 3);
900 t1 -= (NFS_SDRTT(rep) >> 2);
901 NFS_SDRTT(rep) += t1;
903 nmp->nm_timeouts = 0;
908 * If not matched to a request, drop it.
909 * If it's mine, get out.
912 nfsstats.rpcunexpected++;
914 } else if (rep == myrep) {
915 if (rep->r_mrep == NULL)
916 panic("nfsreply nil");
919 if (myrep->r_flags & R_GETONEREP)
925 * nfs_request - goes something like this
926 * - fill in request struct
927 * - links it into list
928 * - calls nfs_send() for first transmit
929 * - calls nfs_receive() to get reply
930 * - break down rpc header and return with nfs reply pointed to
932 * nb: always frees up mreq mbuf list
935 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
945 register struct mbuf *mrep, *m2;
946 register struct nfsreq *rep;
947 register u_int32_t *tl;
949 struct nfsmount *nmp;
950 struct mbuf *m, *md, *mheadend;
952 char nickv[RPCX_NICKVERF];
953 time_t reqtime, waituntil;
955 int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
956 int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
957 int verf_len, verf_type;
960 char *auth_str, *verf_str;
961 NFSKERBKEY_T key; /* save session key */
963 /* Reject requests while attempting a forced unmount. */
964 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
968 nmp = VFSTONFS(vp->v_mount);
969 MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
972 rep->r_procp = procp;
973 rep->r_procnum = procnum;
983 * Get the RPC header with authorization.
986 verf_str = auth_str = (char *)0;
987 if (nmp->nm_flag & NFSMNT_KERB) {
989 verf_len = sizeof (nickv);
990 auth_type = RPCAUTH_KERB4;
991 bzero((caddr_t)key, sizeof (key));
992 if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
993 &auth_len, verf_str, verf_len)) {
994 error = nfs_getauth(nmp, rep, cred, &auth_str,
995 &auth_len, verf_str, &verf_len, key);
997 free((caddr_t)rep, M_NFSREQ);
1003 auth_type = RPCAUTH_UNIX;
1004 if (cred->cr_ngroups < 1)
1005 panic("nfsreq nogrps");
1006 auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1007 nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1010 m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1011 auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1013 free(auth_str, M_TEMP);
1016 * For stream protocols, insert a Sun RPC Record Mark.
1018 if (nmp->nm_sotype == SOCK_STREAM) {
1019 M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
1020 *mtod(m, u_int32_t *) = htonl(0x80000000 |
1021 (m->m_pkthdr.len - NFSX_UNSIGNED));
1026 if (nmp->nm_flag & NFSMNT_SOFT)
1027 rep->r_retry = nmp->nm_retry;
1029 rep->r_retry = NFS_MAXREXMIT + 1; /* past clip limit */
1030 rep->r_rtt = rep->r_rexmit = 0;
1031 if (proct[procnum] > 0)
1032 rep->r_flags = R_TIMING;
1038 * Do the client side RPC.
1040 nfsstats.rpcrequests++;
1042 * Chain request into list of outstanding requests. Be sure
1043 * to put it LAST so timer finds oldest requests first.
1046 TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1048 /* Get send time for nqnfs */
1049 reqtime = time_second;
1052 * If backing off another request or avoiding congestion, don't
1053 * send this one now but let timer do it. If not timing a request,
1056 if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1057 (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1058 nmp->nm_sent < nmp->nm_cwnd)) {
1060 if (nmp->nm_soflags & PR_CONNREQUIRED)
1061 error = nfs_sndlock(rep);
1063 m2 = m_copym(m, 0, M_COPYALL, M_WAIT);
1064 error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1065 if (nmp->nm_soflags & PR_CONNREQUIRED)
1068 if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
1069 nmp->nm_sent += NFS_CWNDSCALE;
1070 rep->r_flags |= R_SENT;
1078 * Wait for the reply from our send or the timer's.
1080 if (!error || error == EPIPE)
1081 error = nfs_reply(rep);
1084 * RPC done, unlink the request.
1087 TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1091 * Decrement the outstanding request count.
1093 if (rep->r_flags & R_SENT) {
1094 rep->r_flags &= ~R_SENT; /* paranoia */
1095 nmp->nm_sent -= NFS_CWNDSCALE;
1099 * If there was a successful reply and a tprintf msg.
1100 * tprintf a response.
1102 if (!error && (rep->r_flags & R_TPRINTFMSG))
1103 nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
1109 m_freem(rep->r_mreq);
1110 free((caddr_t)rep, M_NFSREQ);
1115 * break down the rpc header and check if ok
1117 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1118 if (*tl++ == rpc_msgdenied) {
1119 if (*tl == rpc_mismatch)
1121 else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1124 mheadend->m_next = (struct mbuf *)0;
1126 m_freem(rep->r_mreq);
1133 m_freem(rep->r_mreq);
1134 free((caddr_t)rep, M_NFSREQ);
1139 * Grab any Kerberos verifier, otherwise just throw it away.
1141 verf_type = fxdr_unsigned(int, *tl++);
1142 i = fxdr_unsigned(int32_t, *tl);
1143 if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1144 error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1148 nfsm_adv(nfsm_rndup(i));
1149 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1152 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1154 error = fxdr_unsigned(int, *tl);
1155 if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1156 error == NFSERR_TRYLATER) {
1159 waituntil = time_second + trylater_delay;
1160 while (time_second < waituntil)
1161 (void) tsleep((caddr_t)&lbolt,
1162 PSOCK, "nqnfstry", 0);
1163 trylater_delay *= nfs_backoff[trylater_cnt];
1164 if (trylater_cnt < 7)
1170 * If the File Handle was stale, invalidate the
1171 * lookup cache, just in case.
1173 if (error == ESTALE)
1175 if (nmp->nm_flag & NFSMNT_NFSV3) {
1179 error |= NFSERR_RETERR;
1182 m_freem(rep->r_mreq);
1183 free((caddr_t)rep, M_NFSREQ);
1188 * For nqnfs, get any lease in reply
1190 if (nmp->nm_flag & NFSMNT_NQNFS) {
1191 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1194 nqlflag = fxdr_unsigned(int, *tl);
1195 nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
1196 cachable = fxdr_unsigned(int, *tl++);
1197 reqtime += fxdr_unsigned(int, *tl++);
1198 if (reqtime > time_second) {
1199 frev = fxdr_hyper(tl);
1200 nqnfs_clientlease(nmp, np, nqlflag,
1201 cachable, reqtime, frev);
1208 m_freem(rep->r_mreq);
1209 FREE((caddr_t)rep, M_NFSREQ);
1213 error = EPROTONOSUPPORT;
1215 m_freem(rep->r_mreq);
1216 free((caddr_t)rep, M_NFSREQ);
1220 #ifndef NFS_NOSERVER
1222 * Generate the rpc reply header
1223 * siz arg. is used to decide if adding a cluster is worthwhile
1226 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
1228 struct nfsrv_descript *nd;
1229 struct nfssvc_sock *slp;
1237 register u_int32_t *tl;
1238 register struct mbuf *mreq;
1240 struct mbuf *mb, *mb2;
1242 MGETHDR(mreq, M_WAIT, MT_DATA);
1245 * If this is a big reply, use a cluster else
1246 * try and leave leading space for the lower level headers.
1248 siz += RPC_REPLYSIZ;
1249 if ((max_hdr + siz) >= MINCLSIZE) {
1250 MCLGET(mreq, M_WAIT);
1252 mreq->m_data += max_hdr;
1253 tl = mtod(mreq, u_int32_t *);
1254 mreq->m_len = 6 * NFSX_UNSIGNED;
1255 bpos = ((caddr_t)tl) + mreq->m_len;
1256 *tl++ = txdr_unsigned(nd->nd_retxid);
1258 if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1259 *tl++ = rpc_msgdenied;
1260 if (err & NFSERR_AUTHERR) {
1261 *tl++ = rpc_autherr;
1262 *tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1263 mreq->m_len -= NFSX_UNSIGNED;
1264 bpos -= NFSX_UNSIGNED;
1266 *tl++ = rpc_mismatch;
1267 *tl++ = txdr_unsigned(RPC_VER2);
1268 *tl = txdr_unsigned(RPC_VER2);
1271 *tl++ = rpc_msgaccepted;
1274 * For Kerberos authentication, we must send the nickname
1275 * verifier back, otherwise just RPCAUTH_NULL.
1277 if (nd->nd_flag & ND_KERBFULL) {
1278 register struct nfsuid *nuidp;
1279 struct timeval ktvin, ktvout;
1281 for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1282 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1283 if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1284 (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1285 &nuidp->nu_haddr, nd->nd_nam2)))
1290 txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1292 txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1295 * Encrypt the timestamp in ecb mode using the
1302 *tl++ = rpc_auth_kerb;
1303 *tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1304 *tl = ktvout.tv_sec;
1305 nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1306 *tl++ = ktvout.tv_usec;
1307 *tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1318 *tl = txdr_unsigned(RPC_PROGUNAVAIL);
1321 *tl = txdr_unsigned(RPC_PROGMISMATCH);
1322 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1323 if (nd->nd_flag & ND_NQNFS) {
1324 *tl++ = txdr_unsigned(3);
1325 *tl = txdr_unsigned(3);
1327 *tl++ = txdr_unsigned(2);
1328 *tl = txdr_unsigned(3);
1332 *tl = txdr_unsigned(RPC_PROCUNAVAIL);
1335 *tl = txdr_unsigned(RPC_GARBAGE);
1339 if (err != NFSERR_RETVOID) {
1340 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1342 *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1351 * For nqnfs, piggyback lease as requested.
1353 if ((nd->nd_flag & ND_NQNFS) && err == 0) {
1354 if (nd->nd_flag & ND_LEASE) {
1355 nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
1356 *tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
1357 *tl++ = txdr_unsigned(cache);
1358 *tl++ = txdr_unsigned(nd->nd_duration);
1359 txdr_hyper(*frev, tl);
1361 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1369 if (err != 0 && err != NFSERR_RETVOID)
1370 nfsstats.srvrpc_errs++;
1375 #endif /* NFS_NOSERVER */
1378 * Scan the nfsreq list and retranmit any requests that have timed out
1379 * To avoid retransmission attempts on STREAM sockets (in the future) make
1380 * sure to set the r_retry field to 0 (implies nm_retry == 0).
1384 void *arg; /* never used */
1386 register struct nfsreq *rep;
1387 register struct mbuf *m;
1388 register struct socket *so;
1389 register struct nfsmount *nmp;
1392 #ifndef NFS_NOSERVER
1393 static long lasttime = 0;
1394 register struct nfssvc_sock *slp;
1396 #endif /* NFS_NOSERVER */
1397 struct proc *p = &proc0; /* XXX for credentials, will break if sleep */
1400 for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
1402 if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1404 if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1408 if (rep->r_rtt >= 0) {
1410 if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1411 timeo = nmp->nm_timeo;
1413 timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1414 if (nmp->nm_timeouts > 0)
1415 timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1416 if (rep->r_rtt <= timeo)
1418 if (nmp->nm_timeouts < 8)
1422 * Check for server not responding
1424 if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1425 rep->r_rexmit > nmp->nm_deadthresh) {
1426 nfs_msg(rep->r_procp,
1427 nmp->nm_mountp->mnt_stat.f_mntfromname,
1429 rep->r_flags |= R_TPRINTFMSG;
1431 if (rep->r_rexmit >= rep->r_retry) { /* too many */
1432 nfsstats.rpctimeouts++;
1436 if (nmp->nm_sotype != SOCK_DGRAM) {
1437 if (++rep->r_rexmit > NFS_MAXREXMIT)
1438 rep->r_rexmit = NFS_MAXREXMIT;
1441 if ((so = nmp->nm_so) == NULL)
1445 * If there is enough space and the window allows..
1447 * Set r_rtt to -1 in case we fail to send it now.
1450 if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1451 ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1452 (rep->r_flags & R_SENT) ||
1453 nmp->nm_sent < nmp->nm_cwnd) &&
1454 (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1455 if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1456 error = (*so->so_proto->pr_usrreqs->pru_send)
1457 (so, 0, m, (struct sockaddr *)0,
1458 (struct mbuf *)0, p);
1460 error = (*so->so_proto->pr_usrreqs->pru_send)
1461 (so, 0, m, nmp->nm_nam, (struct mbuf *)0,
1464 if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1468 * Iff first send, start timing
1469 * else turn timing off, backoff timer
1470 * and divide congestion window by 2.
1472 if (rep->r_flags & R_SENT) {
1473 rep->r_flags &= ~R_TIMING;
1474 if (++rep->r_rexmit > NFS_MAXREXMIT)
1475 rep->r_rexmit = NFS_MAXREXMIT;
1477 if (nmp->nm_cwnd < NFS_CWNDSCALE)
1478 nmp->nm_cwnd = NFS_CWNDSCALE;
1479 nfsstats.rpcretries++;
1481 rep->r_flags |= R_SENT;
1482 nmp->nm_sent += NFS_CWNDSCALE;
1488 #ifndef NFS_NOSERVER
1490 * Call the nqnfs server timer once a second to handle leases.
1492 if (lasttime != time_second) {
1493 lasttime = time_second;
1498 * Scan the write gathering queues for writes that need to be
1501 cur_usec = nfs_curusec();
1502 for (slp = nfssvc_sockhead.tqh_first; slp != 0;
1503 slp = slp->ns_chain.tqe_next) {
1504 if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1505 nfsrv_wakenfsd(slp);
1507 #endif /* NFS_NOSERVER */
1509 nfs_timer_handle = timeout(nfs_timer, (void *)0, nfs_ticks);
1513 * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1514 * wait for all requests to complete. This is used by forced unmounts
1515 * to terminate any outstanding RPCs.
1518 nfs_nmcancelreqs(nmp)
1519 struct nfsmount *nmp;
1525 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1526 if (nmp != req->r_nmp || req->r_mrep != NULL ||
1527 (req->r_flags & R_SOFTTERM))
1533 for (i = 0; i < 30; i++) {
1535 TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1536 if (nmp == req->r_nmp)
1542 tsleep(&lbolt, PSOCK, "nfscancel", 0);
1548 * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1549 * The nm_send count is decremented now to avoid deadlocks when the process in
1550 * soreceive() hasn't yet managed to send its own request.
1557 rep->r_flags |= R_SOFTTERM;
1559 if (rep->r_flags & R_SENT) {
1560 rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1561 rep->r_flags &= ~R_SENT;
1566 * Test for a termination condition pending on the process.
1567 * This is used for NFSMNT_INT mounts.
1570 nfs_sigintr(nmp, rep, p)
1571 struct nfsmount *nmp;
1573 register struct proc *p;
1577 if (rep && (rep->r_flags & R_SOFTTERM))
1579 /* Terminate all requests while attempting a forced unmount. */
1580 if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1582 if (!(nmp->nm_flag & NFSMNT_INT))
1587 tmpset = p->p_siglist;
1588 SIGSETNAND(tmpset, p->p_sigmask);
1589 SIGSETNAND(tmpset, p->p_sigignore);
1590 if (SIGNOTEMPTY(p->p_siglist) && NFSINT_SIGMASK(tmpset))
1597 * Lock a socket against others.
1598 * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1599 * and also to avoid race conditions between the processes with nfs requests
1600 * in progress when a reconnect is necessary.
1606 register int *statep = &rep->r_nmp->nm_state;
1608 int slpflag = 0, slptimeo = 0;
1611 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1613 while (*statep & NFSSTA_SNDLOCK) {
1614 if (nfs_sigintr(rep->r_nmp, rep, p))
1616 *statep |= NFSSTA_WANTSND;
1617 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1),
1618 "nfsndlck", slptimeo);
1619 if (slpflag == PCATCH) {
1624 /* Always fail if our request has been cancelled. */
1625 if ((rep->r_flags & R_SOFTTERM))
1627 *statep |= NFSSTA_SNDLOCK;
1632 * Unlock the stream socket for others.
1638 register int *statep = &rep->r_nmp->nm_state;
1640 if ((*statep & NFSSTA_SNDLOCK) == 0)
1641 panic("nfs sndunlock");
1642 *statep &= ~NFSSTA_SNDLOCK;
1643 if (*statep & NFSSTA_WANTSND) {
1644 *statep &= ~NFSSTA_WANTSND;
1645 wakeup((caddr_t)statep);
1651 register struct nfsreq *rep;
1653 register int *statep = &rep->r_nmp->nm_state;
1654 int slpflag, slptimeo = 0;
1656 if (rep->r_nmp->nm_flag & NFSMNT_INT)
1660 while (*statep & NFSSTA_RCVLOCK) {
1661 if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
1663 *statep |= NFSSTA_WANTRCV;
1664 (void) tsleep((caddr_t)statep, slpflag | (PZERO - 1), "nfsrcvlk",
1667 * If our reply was recieved while we were sleeping,
1668 * then just return without taking the lock to avoid a
1669 * situation where a single iod could 'capture' the
1672 if (rep->r_mrep != NULL)
1674 if (slpflag == PCATCH) {
1679 *statep |= NFSSTA_RCVLOCK;
1684 * Unlock the stream socket for others.
1688 register struct nfsreq *rep;
1690 register int *statep = &rep->r_nmp->nm_state;
1692 if ((*statep & NFSSTA_RCVLOCK) == 0)
1693 panic("nfs rcvunlock");
1694 *statep &= ~NFSSTA_RCVLOCK;
1695 if (*statep & NFSSTA_WANTRCV) {
1696 *statep &= ~NFSSTA_WANTRCV;
1697 wakeup((caddr_t)statep);
1704 * Check for badly aligned mbuf data and realign by copying the unaligned
1705 * portion of the data into a new mbuf chain and freeing the portions
1706 * of the old chain that were replaced.
1708 * We cannot simply realign the data within the existing mbuf chain
1709 * because the underlying buffers may contain other rpc commands and
1710 * we cannot afford to overwrite them.
1712 * We would prefer to avoid this situation entirely. The situation does
1713 * not occur with NFS/UDP and is supposed to only occassionally occur
1714 * with TCP. Use vfs.nfs.realign_count and realign_test to check this.
1717 nfs_realign(pm, hsiz)
1718 register struct mbuf **pm;
1722 struct mbuf *n = NULL;
1727 while ((m = *pm) != NULL) {
1728 if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1729 MGET(n, M_WAIT, MT_DATA);
1730 if (m->m_len >= MINCLSIZE) {
1740 * If n is non-NULL, loop on m copying data, then replace the
1741 * portion of the chain that had to be realigned.
1744 ++nfs_realign_count;
1746 m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1755 #ifndef NFS_NOSERVER
1758 * Parse an RPC request
1760 * - fill in the cred struct.
1763 nfs_getreq(nd, nfsd, has_header)
1764 register struct nfsrv_descript *nd;
1768 register int len, i;
1769 register u_int32_t *tl;
1770 register int32_t t1;
1773 caddr_t dpos, cp2, cp;
1774 u_int32_t nfsvers, auth_type;
1776 int error = 0, nqnfs = 0, ticklen;
1777 struct mbuf *mrep, *md;
1778 register struct nfsuid *nuidp;
1779 struct timeval tvin, tvout;
1780 #if 0 /* until encrypted keys are implemented */
1781 NFSKERBKEYSCHED_T keys; /* stores key schedule */
1788 nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1789 nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1790 if (*tl++ != rpc_call) {
1795 nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1798 if (*tl++ != rpc_vers) {
1799 nd->nd_repstat = ERPCMISMATCH;
1800 nd->nd_procnum = NFSPROC_NOOP;
1803 if (*tl != nfs_prog) {
1804 if (*tl == nqnfs_prog)
1807 nd->nd_repstat = EPROGUNAVAIL;
1808 nd->nd_procnum = NFSPROC_NOOP;
1813 nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1814 if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
1815 (nfsvers != NQNFS_VER3 && nqnfs)) {
1816 nd->nd_repstat = EPROGMISMATCH;
1817 nd->nd_procnum = NFSPROC_NOOP;
1821 nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
1822 else if (nfsvers == NFS_VER3)
1823 nd->nd_flag = ND_NFSV3;
1824 nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1825 if (nd->nd_procnum == NFSPROC_NULL)
1827 if (nd->nd_procnum >= NFS_NPROCS ||
1828 (!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1829 (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1830 nd->nd_repstat = EPROCUNAVAIL;
1831 nd->nd_procnum = NFSPROC_NOOP;
1834 if ((nd->nd_flag & ND_NFSV3) == 0)
1835 nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1837 len = fxdr_unsigned(int, *tl++);
1838 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1843 nd->nd_flag &= ~ND_KERBAUTH;
1845 * Handle auth_unix or auth_kerb.
1847 if (auth_type == rpc_auth_unix) {
1848 len = fxdr_unsigned(int, *++tl);
1849 if (len < 0 || len > NFS_MAXNAMLEN) {
1853 nfsm_adv(nfsm_rndup(len));
1854 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1855 bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1856 nd->nd_cr.cr_ref = 1;
1857 nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1858 nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1859 len = fxdr_unsigned(int, *tl);
1860 if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1864 nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1865 for (i = 1; i <= len; i++)
1867 nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1870 nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
1871 if (nd->nd_cr.cr_ngroups > 1)
1872 nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1873 len = fxdr_unsigned(int, *++tl);
1874 if (len < 0 || len > RPCAUTH_MAXSIZ) {
1879 nfsm_adv(nfsm_rndup(len));
1880 } else if (auth_type == rpc_auth_kerb) {
1881 switch (fxdr_unsigned(int, *tl++)) {
1882 case RPCAKN_FULLNAME:
1883 ticklen = fxdr_unsigned(int, *tl);
1884 *((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1885 uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1886 nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1887 if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1894 uio.uio_segflg = UIO_SYSSPACE;
1895 iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1896 iov.iov_len = RPCAUTH_MAXSIZ - 4;
1897 nfsm_mtouio(&uio, uio.uio_resid);
1898 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1899 if (*tl++ != rpc_auth_kerb ||
1900 fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1901 printf("Bad kerb verifier\n");
1902 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1903 nd->nd_procnum = NFSPROC_NOOP;
1906 nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1907 tl = (u_int32_t *)cp;
1908 if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1909 printf("Not fullname kerb verifier\n");
1910 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1911 nd->nd_procnum = NFSPROC_NOOP;
1914 cp += NFSX_UNSIGNED;
1915 bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1916 nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1917 nd->nd_flag |= ND_KERBFULL;
1918 nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1920 case RPCAKN_NICKNAME:
1921 if (len != 2 * NFSX_UNSIGNED) {
1922 printf("Kerb nickname short\n");
1923 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1924 nd->nd_procnum = NFSPROC_NOOP;
1927 nickuid = fxdr_unsigned(uid_t, *tl);
1928 nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1929 if (*tl++ != rpc_auth_kerb ||
1930 fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1931 printf("Kerb nick verifier bad\n");
1932 nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1933 nd->nd_procnum = NFSPROC_NOOP;
1936 nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1937 tvin.tv_sec = *tl++;
1940 for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
1941 nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1942 if (nuidp->nu_cr.cr_uid == nickuid &&
1944 netaddr_match(NU_NETFAM(nuidp),
1945 &nuidp->nu_haddr, nd->nd_nam2)))
1950 (NFSERR_AUTHERR|AUTH_REJECTCRED);
1951 nd->nd_procnum = NFSPROC_NOOP;
1956 * Now, decrypt the timestamp using the session key
1963 tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1964 tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1965 if (nuidp->nu_expire < time_second ||
1966 nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1967 (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1968 nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1969 nuidp->nu_expire = 0;
1971 (NFSERR_AUTHERR|AUTH_REJECTVERF);
1972 nd->nd_procnum = NFSPROC_NOOP;
1975 nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1976 nd->nd_flag |= ND_KERBNICK;
1979 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1980 nd->nd_procnum = NFSPROC_NOOP;
1985 * For nqnfs, get piggybacked lease request.
1987 if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
1988 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1989 nd->nd_flag |= fxdr_unsigned(int, *tl);
1990 if (nd->nd_flag & ND_LEASE) {
1991 nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1992 nd->nd_duration = fxdr_unsigned(int32_t, *tl);
1994 nd->nd_duration = NQ_MINLEASE;
1996 nd->nd_duration = NQ_MINLEASE;
2007 nfs_msg(p, server, msg)
2014 tpr = tprintf_open(p);
2017 tprintf(tpr, "nfs server %s: %s\n", server, msg);
2022 #ifndef NFS_NOSERVER
2024 * Socket upcall routine for the nfsd sockets.
2025 * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2026 * Essentially do as much as possible non-blocking, else punt and it will
2027 * be called with M_WAIT from an nfsd.
2030 nfsrv_rcv(so, arg, waitflag)
2035 register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2036 register struct mbuf *m;
2038 struct sockaddr *nam;
2042 if ((slp->ns_flag & SLP_VALID) == 0)
2046 * Define this to test for nfsds handling this under heavy load.
2048 if (waitflag == M_DONTWAIT) {
2049 slp->ns_flag |= SLP_NEEDQ; goto dorecs;
2052 auio.uio_procp = NULL;
2053 if (so->so_type == SOCK_STREAM) {
2055 * If there are already records on the queue, defer soreceive()
2056 * to an nfsd so that there is feedback to the TCP layer that
2057 * the nfs servers are heavily loaded.
2059 if (STAILQ_FIRST(&slp->ns_rec) && waitflag == M_DONTWAIT) {
2060 slp->ns_flag |= SLP_NEEDQ;
2067 auio.uio_resid = 1000000000;
2068 flags = MSG_DONTWAIT;
2069 error = so->so_proto->pr_usrreqs->pru_soreceive
2070 (so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
2071 if (error || mp == (struct mbuf *)0) {
2072 if (error == EWOULDBLOCK)
2073 slp->ns_flag |= SLP_NEEDQ;
2075 slp->ns_flag |= SLP_DISCONN;
2079 if (slp->ns_rawend) {
2080 slp->ns_rawend->m_next = m;
2081 slp->ns_cc += 1000000000 - auio.uio_resid;
2084 slp->ns_cc = 1000000000 - auio.uio_resid;
2091 * Now try and parse record(s) out of the raw stream data.
2093 error = nfsrv_getstream(slp, waitflag);
2096 slp->ns_flag |= SLP_DISCONN;
2098 slp->ns_flag |= SLP_NEEDQ;
2102 auio.uio_resid = 1000000000;
2103 flags = MSG_DONTWAIT;
2104 error = so->so_proto->pr_usrreqs->pru_soreceive
2105 (so, &nam, &auio, &mp,
2106 (struct mbuf **)0, &flags);
2108 struct nfsrv_rec *rec;
2109 rec = malloc(sizeof(struct nfsrv_rec),
2110 M_NFSRVDESC, waitflag);
2113 FREE(nam, M_SONAME);
2117 nfs_realign(&mp, 10 * NFSX_UNSIGNED);
2118 rec->nr_address = nam;
2119 rec->nr_packet = mp;
2120 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2123 if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2124 && error != EWOULDBLOCK) {
2125 slp->ns_flag |= SLP_DISCONN;
2133 * Now try and process the request records, non-blocking.
2136 if (waitflag == M_DONTWAIT &&
2137 (STAILQ_FIRST(&slp->ns_rec)
2138 || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
2139 nfsrv_wakenfsd(slp);
2143 * Try and extract an RPC request from the mbuf data list received on a
2144 * stream socket. The "waitflag" argument indicates whether or not it
2148 nfsrv_getstream(slp, waitflag)
2149 register struct nfssvc_sock *slp;
2152 register struct mbuf *m, **mpp;
2153 register char *cp1, *cp2;
2155 struct mbuf *om, *m2, *recm;
2158 if (slp->ns_flag & SLP_GETSTREAM)
2159 panic("nfs getstream");
2160 slp->ns_flag |= SLP_GETSTREAM;
2162 if (slp->ns_reclen == 0) {
2163 if (slp->ns_cc < NFSX_UNSIGNED) {
2164 slp->ns_flag &= ~SLP_GETSTREAM;
2168 if (m->m_len >= NFSX_UNSIGNED) {
2169 bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2170 m->m_data += NFSX_UNSIGNED;
2171 m->m_len -= NFSX_UNSIGNED;
2173 cp1 = (caddr_t)&recmark;
2174 cp2 = mtod(m, caddr_t);
2175 while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2176 while (m->m_len == 0) {
2178 cp2 = mtod(m, caddr_t);
2185 slp->ns_cc -= NFSX_UNSIGNED;
2186 recmark = ntohl(recmark);
2187 slp->ns_reclen = recmark & ~0x80000000;
2188 if (recmark & 0x80000000)
2189 slp->ns_flag |= SLP_LASTFRAG;
2191 slp->ns_flag &= ~SLP_LASTFRAG;
2192 if (slp->ns_reclen > NFS_MAXPACKET) {
2193 slp->ns_flag &= ~SLP_GETSTREAM;
2199 * Now get the record part.
2201 * Note that slp->ns_reclen may be 0. Linux sometimes
2202 * generates 0-length RPCs
2205 if (slp->ns_cc == slp->ns_reclen) {
2207 slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
2208 slp->ns_cc = slp->ns_reclen = 0;
2209 } else if (slp->ns_cc > slp->ns_reclen) {
2212 om = (struct mbuf *)0;
2214 while (len < slp->ns_reclen) {
2215 if ((len + m->m_len) > slp->ns_reclen) {
2216 m2 = m_copym(m, 0, slp->ns_reclen - len,
2224 m->m_data += slp->ns_reclen - len;
2225 m->m_len -= slp->ns_reclen - len;
2226 len = slp->ns_reclen;
2228 slp->ns_flag &= ~SLP_GETSTREAM;
2229 return (EWOULDBLOCK);
2231 } else if ((len + m->m_len) == slp->ns_reclen) {
2236 om->m_next = (struct mbuf *)0;
2247 slp->ns_flag &= ~SLP_GETSTREAM;
2252 * Accumulate the fragments into a record.
2254 mpp = &slp->ns_frag;
2256 mpp = &((*mpp)->m_next);
2258 if (slp->ns_flag & SLP_LASTFRAG) {
2259 struct nfsrv_rec *rec;
2260 rec = malloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, waitflag);
2262 m_freem(slp->ns_frag);
2264 nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2265 rec->nr_address = (struct sockaddr *)0;
2266 rec->nr_packet = slp->ns_frag;
2267 STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2269 slp->ns_frag = (struct mbuf *)0;
2275 * Parse an RPC header.
2278 nfsrv_dorec(slp, nfsd, ndp)
2279 register struct nfssvc_sock *slp;
2281 struct nfsrv_descript **ndp;
2283 struct nfsrv_rec *rec;
2284 register struct mbuf *m;
2285 struct sockaddr *nam;
2286 register struct nfsrv_descript *nd;
2290 if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2292 rec = STAILQ_FIRST(&slp->ns_rec);
2293 STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
2294 nam = rec->nr_address;
2296 free(rec, M_NFSRVDESC);
2297 MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2298 M_NFSRVDESC, M_WAITOK);
2299 nd->nd_md = nd->nd_mrep = m;
2301 nd->nd_dpos = mtod(m, caddr_t);
2302 error = nfs_getreq(nd, nfsd, TRUE);
2305 FREE(nam, M_SONAME);
2307 free((caddr_t)nd, M_NFSRVDESC);
2316 * Search for a sleeping nfsd and wake it up.
2317 * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
2318 * running nfsds will go look for the work in the nfssvc_sock list.
2322 struct nfssvc_sock *slp;
2324 register struct nfsd *nd;
2326 if ((slp->ns_flag & SLP_VALID) == 0)
2328 for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nfsd_chain.tqe_next) {
2329 if (nd->nfsd_flag & NFSD_WAITING) {
2330 nd->nfsd_flag &= ~NFSD_WAITING;
2332 panic("nfsd wakeup");
2335 wakeup((caddr_t)nd);
2339 slp->ns_flag |= SLP_DOREC;
2340 nfsd_head_flag |= NFSD_CHECKSLP;
2342 #endif /* NFS_NOSERVER */