Home | History | Annotate | Line # | Download | only in nfs
nfs_socket.c revision 1.23
      1 /*	$NetBSD: nfs_socket.c,v 1.23 1996/02/09 21:48:29 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	@(#)nfs_socket.c	8.3 (Berkeley) 1/12/94
     39  */
     40 
     41 /*
     42  * Socket operations for use by nfs
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/proc.h>
     48 #include <sys/mount.h>
     49 #include <sys/kernel.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/vnode.h>
     52 #include <sys/domain.h>
     53 #include <sys/protosw.h>
     54 #include <sys/socket.h>
     55 #include <sys/socketvar.h>
     56 #include <sys/syslog.h>
     57 #include <sys/tprintf.h>
     58 #include <sys/namei.h>
     59 
     60 #include <netinet/in.h>
     61 #include <netinet/tcp.h>
     62 #include <nfs/rpcv2.h>
     63 #include <nfs/nfsv2.h>
     64 #include <nfs/nfs.h>
     65 #include <nfs/xdr_subs.h>
     66 #include <nfs/nfsm_subs.h>
     67 #include <nfs/nfsmount.h>
     68 #include <nfs/nfsnode.h>
     69 #include <nfs/nfsrtt.h>
     70 #include <nfs/nqnfs.h>
     71 #include <nfs/nfs_var.h>
     72 
     73 #define	TRUE	1
     74 #define	FALSE	0
     75 
     76 /*
     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
     85  * read, write     - A+4D
     86  * other           - nm_timeo
     87  */
     88 #define	NFS_RTO(n, t) \
     89 	((t) == 0 ? (n)->nm_timeo : \
     90 	 ((t) < 3 ? \
     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]
     95 /*
     96  * External data, mostly RPC constants in XDR form
     97  */
     98 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
     99 	rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr, rpc_rejectedcred,
    100 	rpc_auth_kerb;
    101 extern u_int32_t nfs_prog, nfs_vers, nqnfs_prog, nqnfs_vers;
    102 extern time_t nqnfsstarttime;
    103 extern int nonidempotent[NFS_NPROCS];
    104 
    105 /*
    106  * Maps errno values to nfs error numbers.
    107  * Use NFSERR_IO as the catch all for ones not specifically defined in
    108  * RFC 1094.
    109  */
    110 static int nfsrv_errmap[ELAST] = {
    111   NFSERR_PERM,	NFSERR_NOENT,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    112   NFSERR_NXIO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    113   NFSERR_IO,	NFSERR_IO,	NFSERR_ACCES,	NFSERR_IO,	NFSERR_IO,
    114   NFSERR_IO,	NFSERR_EXIST,	NFSERR_IO,	NFSERR_NODEV,	NFSERR_NOTDIR,
    115   NFSERR_ISDIR,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    116   NFSERR_IO,	NFSERR_FBIG,	NFSERR_NOSPC,	NFSERR_IO,	NFSERR_ROFS,
    117   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    118   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    119   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    120   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    121   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    122   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    123   NFSERR_IO,	NFSERR_IO,	NFSERR_NAMETOL,	NFSERR_IO,	NFSERR_IO,
    124   NFSERR_NOTEMPTY, NFSERR_IO,	NFSERR_IO,	NFSERR_DQUOT,	NFSERR_STALE,
    125   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    126   NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,	NFSERR_IO,
    127   NFSERR_IO,
    128 };
    129 
    130 /*
    131  * Defines which timer to use for the procnum.
    132  * 0 - default
    133  * 1 - getattr
    134  * 2 - lookup
    135  * 3 - read
    136  * 4 - write
    137  */
    138 static int proct[NFS_NPROCS] = {
    139 	0, 1, 0, 0, 2, 3, 3, 0, 4, 0, 0, 0, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0, 0,
    140 };
    141 
    142 /*
    143  * There is a congestion window for outstanding rpcs maintained per mount
    144  * point. The cwnd size is adjusted in roughly the way that:
    145  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
    146  * SIGCOMM '88". ACM, August 1988.
    147  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
    148  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
    149  * of rpcs is in progress.
    150  * (The sent count and cwnd are scaled for integer arith.)
    151  * Variants of "slow start" were tried and were found to be too much of a
    152  * performance hit (ave. rtt 3 times larger),
    153  * I suspect due to the large rtt that nfs rpcs have.
    154  */
    155 #define	NFS_CWNDSCALE	256
    156 #define	NFS_MAXCWND	(NFS_CWNDSCALE * 32)
    157 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
    158 int nfsrtton = 0;
    159 struct nfsrtt nfsrtt;
    160 
    161 /*
    162  * Initialize sockets and congestion for a new NFS connection.
    163  * We do not free the sockaddr if error.
    164  */
    165 int
    166 nfs_connect(nmp, rep)
    167 	register struct nfsmount *nmp;
    168 	struct nfsreq *rep;
    169 {
    170 	register struct socket *so;
    171 	int s, error, rcvreserve, sndreserve;
    172 	struct sockaddr *saddr;
    173 	struct sockaddr_in *sin;
    174 	struct mbuf *m;
    175 	u_int16_t tport;
    176 
    177 	nmp->nm_so = (struct socket *)0;
    178 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
    179 	error = socreate(saddr->sa_family,
    180 			 &nmp->nm_so, nmp->nm_sotype, nmp->nm_soproto);
    181 	if (error)
    182 		goto bad;
    183 	so = nmp->nm_so;
    184 	nmp->nm_soflags = so->so_proto->pr_flags;
    185 
    186 	/*
    187 	 * Some servers require that the client port be a reserved port number.
    188 	 */
    189 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    190 		MGET(m, M_WAIT, MT_SONAME);
    191 		sin = mtod(m, struct sockaddr_in *);
    192 		sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
    193 		sin->sin_family = AF_INET;
    194 		sin->sin_addr.s_addr = INADDR_ANY;
    195 		tport = IPPORT_RESERVED - 1;
    196 		sin->sin_port = htons(tport);
    197 		while ((error = sobind(so, m)) == EADDRINUSE &&
    198 		       --tport > IPPORT_RESERVED / 2)
    199 			sin->sin_port = htons(tport);
    200 		m_freem(m);
    201 		if (error)
    202 			goto bad;
    203 	}
    204 
    205 	/*
    206 	 * Protocols that do not require connections may be optionally left
    207 	 * unconnected for servers that reply from a port other than NFS_PORT.
    208 	 */
    209 	if (nmp->nm_flag & NFSMNT_NOCONN) {
    210 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
    211 			error = ENOTCONN;
    212 			goto bad;
    213 		}
    214 	} else {
    215 		if ((error = soconnect(so, nmp->nm_nam)) != 0)
    216 			goto bad;
    217 
    218 		/*
    219 		 * Wait for the connection to complete. Cribbed from the
    220 		 * connect system call but with the wait timing out so
    221 		 * that interruptible mounts don't hang here for a long time.
    222 		 */
    223 		s = splsoftnet();
    224 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    225 			(void) tsleep((caddr_t)&so->so_timeo, PSOCK,
    226 				"nfscon", 2 * hz);
    227 			if ((so->so_state & SS_ISCONNECTING) &&
    228 			    so->so_error == 0 && rep &&
    229 			    (error = nfs_sigintr(nmp, rep, rep->r_procp))) {
    230 				so->so_state &= ~SS_ISCONNECTING;
    231 				splx(s);
    232 				goto bad;
    233 			}
    234 		}
    235 		if (so->so_error) {
    236 			error = so->so_error;
    237 			so->so_error = 0;
    238 			splx(s);
    239 			goto bad;
    240 		}
    241 		splx(s);
    242 	}
    243 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
    244 		so->so_rcv.sb_timeo = (5 * hz);
    245 		so->so_snd.sb_timeo = (5 * hz);
    246 	} else {
    247 		so->so_rcv.sb_timeo = 0;
    248 		so->so_snd.sb_timeo = 0;
    249 	}
    250 	if (nmp->nm_sotype == SOCK_DGRAM) {
    251 		sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR;
    252 		rcvreserve = nmp->nm_rsize + NFS_MAXPKTHDR;
    253 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
    254 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    255 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR) * 2;
    256 	} else {
    257 		if (nmp->nm_sotype != SOCK_STREAM)
    258 			panic("nfscon sotype");
    259 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    260 			MGET(m, M_WAIT, MT_SOOPTS);
    261 			*mtod(m, int32_t *) = 1;
    262 			m->m_len = sizeof(int32_t);
    263 			sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    264 		}
    265 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
    266 			MGET(m, M_WAIT, MT_SOOPTS);
    267 			*mtod(m, int32_t *) = 1;
    268 			m->m_len = sizeof(int32_t);
    269 			sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    270 		}
    271 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
    272 		    sizeof (u_int32_t)) * 2;
    273 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
    274 		    sizeof (u_int32_t)) * 2;
    275 	}
    276 	if ((error = soreserve(so, sndreserve, rcvreserve)) != 0)
    277 		goto bad;
    278 	so->so_rcv.sb_flags |= SB_NOINTR;
    279 	so->so_snd.sb_flags |= SB_NOINTR;
    280 
    281 	/* Initialize other non-zero congestion variables */
    282 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
    283 		nmp->nm_srtt[4] = (NFS_TIMEO << 3);
    284 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
    285 		nmp->nm_sdrtt[3] = nmp->nm_sdrtt[4] = 0;
    286 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
    287 	nmp->nm_sent = 0;
    288 	nmp->nm_timeouts = 0;
    289 	return (0);
    290 
    291 bad:
    292 	nfs_disconnect(nmp);
    293 	return (error);
    294 }
    295 
    296 /*
    297  * Reconnect routine:
    298  * Called when a connection is broken on a reliable protocol.
    299  * - clean up the old socket
    300  * - nfs_connect() again
    301  * - set R_MUSTRESEND for all outstanding requests on mount point
    302  * If this fails the mount point is DEAD!
    303  * nb: Must be called with the nfs_sndlock() set on the mount point.
    304  */
    305 int
    306 nfs_reconnect(rep)
    307 	register struct nfsreq *rep;
    308 {
    309 	register struct nfsreq *rp;
    310 	register struct nfsmount *nmp = rep->r_nmp;
    311 	int error;
    312 
    313 	nfs_disconnect(nmp);
    314 	while ((error = nfs_connect(nmp, rep)) != 0) {
    315 		if (error == EINTR || error == ERESTART)
    316 			return (EINTR);
    317 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
    318 	}
    319 
    320 	/*
    321 	 * Loop through outstanding request list and fix up all requests
    322 	 * on old socket.
    323 	 */
    324 	for (rp = nfs_reqq.tqh_first; rp != 0; rp = rp->r_chain.tqe_next) {
    325 		if (rp->r_nmp == nmp)
    326 			rp->r_flags |= R_MUSTRESEND;
    327 	}
    328 	return (0);
    329 }
    330 
    331 /*
    332  * NFS disconnect. Clean up and unlink.
    333  */
    334 void
    335 nfs_disconnect(nmp)
    336 	register struct nfsmount *nmp;
    337 {
    338 	register struct socket *so;
    339 
    340 	if (nmp->nm_so) {
    341 		so = nmp->nm_so;
    342 		nmp->nm_so = (struct socket *)0;
    343 		soshutdown(so, 2);
    344 		soclose(so);
    345 	}
    346 }
    347 
    348 /*
    349  * This is the nfs send routine. For connection based socket types, it
    350  * must be called with an nfs_sndlock() on the socket.
    351  * "rep == NULL" indicates that it has been called from a server.
    352  * For the client side:
    353  * - return EINTR if the RPC is terminated, 0 otherwise
    354  * - set R_MUSTRESEND if the send fails for any reason
    355  * - do any cleanup required by recoverable socket errors (???)
    356  * For the server side:
    357  * - return EINTR or ERESTART if interrupted by a signal
    358  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
    359  * - do any cleanup required by recoverable socket errors (???)
    360  */
    361 int
    362 nfs_send(so, nam, top, rep)
    363 	register struct socket *so;
    364 	struct mbuf *nam;
    365 	register struct mbuf *top;
    366 	struct nfsreq *rep;
    367 {
    368 	struct mbuf *sendnam;
    369 	int error, soflags, flags;
    370 
    371 	if (rep) {
    372 		if (rep->r_flags & R_SOFTTERM) {
    373 			m_freem(top);
    374 			return (EINTR);
    375 		}
    376 		if ((so = rep->r_nmp->nm_so) == NULL) {
    377 			rep->r_flags |= R_MUSTRESEND;
    378 			m_freem(top);
    379 			return (0);
    380 		}
    381 		rep->r_flags &= ~R_MUSTRESEND;
    382 		soflags = rep->r_nmp->nm_soflags;
    383 	} else
    384 		soflags = so->so_proto->pr_flags;
    385 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
    386 		sendnam = (struct mbuf *)0;
    387 	else
    388 		sendnam = nam;
    389 	if (so->so_type == SOCK_SEQPACKET)
    390 		flags = MSG_EOR;
    391 	else
    392 		flags = 0;
    393 
    394 	error = sosend(so, sendnam, (struct uio *)0, top,
    395 		(struct mbuf *)0, flags);
    396 	if (error) {
    397 		if (rep) {
    398 			log(LOG_INFO, "nfs send error %d for server %s\n",error,
    399 			    rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    400 			/*
    401 			 * Deal with errors for the client side.
    402 			 */
    403 			if (rep->r_flags & R_SOFTTERM)
    404 				error = EINTR;
    405 			else
    406 				rep->r_flags |= R_MUSTRESEND;
    407 		} else
    408 			log(LOG_INFO, "nfsd send error %d\n", error);
    409 
    410 		/*
    411 		 * Handle any recoverable (soft) socket errors here. (???)
    412 		 */
    413 		if (error != EINTR && error != ERESTART &&
    414 			error != EWOULDBLOCK && error != EPIPE)
    415 			error = 0;
    416 	}
    417 	return (error);
    418 }
    419 
    420 #ifdef NFSCLIENT
    421 /*
    422  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
    423  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
    424  * Mark and consolidate the data into a new mbuf list.
    425  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
    426  *     small mbufs.
    427  * For SOCK_STREAM we must be very careful to read an entire record once
    428  * we have read any of it, even if the system call has been interrupted.
    429  */
    430 int
    431 nfs_receive(rep, aname, mp)
    432 	register struct nfsreq *rep;
    433 	struct mbuf **aname;
    434 	struct mbuf **mp;
    435 {
    436 	register struct socket *so;
    437 	struct uio auio;
    438 	struct iovec aio;
    439 	register struct mbuf *m;
    440 	struct mbuf *control;
    441 	u_int32_t len;
    442 	struct mbuf **getnam;
    443 	int error, sotype, rcvflg;
    444 	struct proc *p = curproc;	/* XXX */
    445 
    446 	/*
    447 	 * Set up arguments for soreceive()
    448 	 */
    449 	*mp = (struct mbuf *)0;
    450 	*aname = (struct mbuf *)0;
    451 	sotype = rep->r_nmp->nm_sotype;
    452 
    453 	/*
    454 	 * For reliable protocols, lock against other senders/receivers
    455 	 * in case a reconnect is necessary.
    456 	 * For SOCK_STREAM, first get the Record Mark to find out how much
    457 	 * more there is to get.
    458 	 * We must lock the socket against other receivers
    459 	 * until we have an entire rpc request/reply.
    460 	 */
    461 	if (sotype != SOCK_DGRAM) {
    462 		if ((error = nfs_sndlock(&rep->r_nmp->nm_flag, rep)) != 0)
    463 			return (error);
    464 tryagain:
    465 		/*
    466 		 * Check for fatal errors and resending request.
    467 		 */
    468 		/*
    469 		 * Ugh: If a reconnect attempt just happened, nm_so
    470 		 * would have changed. NULL indicates a failed
    471 		 * attempt that has essentially shut down this
    472 		 * mount point.
    473 		 */
    474 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
    475 			nfs_sndunlock(&rep->r_nmp->nm_flag);
    476 			return (EINTR);
    477 		}
    478 		if ((so = rep->r_nmp->nm_so) == NULL) {
    479 			if ((error = nfs_reconnect(rep)) != 0) {
    480 				nfs_sndunlock(&rep->r_nmp->nm_flag);
    481 				return (error);
    482 			}
    483 			goto tryagain;
    484 		}
    485 		while (rep->r_flags & R_MUSTRESEND) {
    486 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
    487 			nfsstats.rpcretries++;
    488 			error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
    489 			if (error) {
    490 				if (error == EINTR || error == ERESTART ||
    491 				    (error = nfs_reconnect(rep))) {
    492 					nfs_sndunlock(&rep->r_nmp->nm_flag);
    493 					return (error);
    494 				}
    495 				goto tryagain;
    496 			}
    497 		}
    498 		nfs_sndunlock(&rep->r_nmp->nm_flag);
    499 		if (sotype == SOCK_STREAM) {
    500 			aio.iov_base = (caddr_t) &len;
    501 			aio.iov_len = sizeof(u_int32_t);
    502 			auio.uio_iov = &aio;
    503 			auio.uio_iovcnt = 1;
    504 			auio.uio_segflg = UIO_SYSSPACE;
    505 			auio.uio_rw = UIO_READ;
    506 			auio.uio_offset = 0;
    507 			auio.uio_resid = sizeof(u_int32_t);
    508 			auio.uio_procp = p;
    509 			do {
    510 			   rcvflg = MSG_WAITALL;
    511 			   error = soreceive(so, (struct mbuf **)0, &auio,
    512 				(struct mbuf **)0, (struct mbuf **)0, &rcvflg);
    513 			   if (error == EWOULDBLOCK && rep) {
    514 				if (rep->r_flags & R_SOFTTERM)
    515 					return (EINTR);
    516 			   }
    517 			} while (error == EWOULDBLOCK);
    518 			if (!error && auio.uio_resid > 0) {
    519 			    log(LOG_INFO,
    520 				 "short receive (%d/%d) from nfs server %s\n",
    521 				 sizeof(u_int32_t) - auio.uio_resid,
    522 				 sizeof(u_int32_t),
    523 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    524 			    error = EPIPE;
    525 			}
    526 			if (error)
    527 				goto errout;
    528 			len = ntohl(len) & ~0x80000000;
    529 			/*
    530 			 * This is SERIOUS! We are out of sync with the sender
    531 			 * and forcing a disconnect/reconnect is all I can do.
    532 			 */
    533 			if (len > NFS_MAXPACKET) {
    534 			    log(LOG_ERR, "%s (%d) from nfs server %s\n",
    535 				"impossible packet length",
    536 				len,
    537 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    538 			    error = EFBIG;
    539 			    goto errout;
    540 			}
    541 			auio.uio_resid = len;
    542 			do {
    543 			    rcvflg = MSG_WAITALL;
    544 			    error =  soreceive(so, (struct mbuf **)0,
    545 				&auio, mp, (struct mbuf **)0, &rcvflg);
    546 			} while (error == EWOULDBLOCK || error == EINTR ||
    547 				 error == ERESTART);
    548 			if (!error && auio.uio_resid > 0) {
    549 			    log(LOG_INFO,
    550 				"short receive (%d/%d) from nfs server %s\n",
    551 				len - auio.uio_resid, len,
    552 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    553 			    error = EPIPE;
    554 			}
    555 		} else {
    556 			/*
    557 			 * NB: Since uio_resid is big, MSG_WAITALL is ignored
    558 			 * and soreceive() will return when it has either a
    559 			 * control msg or a data msg.
    560 			 * We have no use for control msg., but must grab them
    561 			 * and then throw them away so we know what is going
    562 			 * on.
    563 			 */
    564 			auio.uio_resid = len = 100000000; /* Anything Big */
    565 			auio.uio_procp = p;
    566 			do {
    567 			    rcvflg = 0;
    568 			    error =  soreceive(so, (struct mbuf **)0,
    569 				&auio, mp, &control, &rcvflg);
    570 			    if (control)
    571 				m_freem(control);
    572 			    if (error == EWOULDBLOCK && rep) {
    573 				if (rep->r_flags & R_SOFTTERM)
    574 					return (EINTR);
    575 			    }
    576 			} while (error == EWOULDBLOCK ||
    577 				 (!error && *mp == NULL && control));
    578 			if ((rcvflg & MSG_EOR) == 0)
    579 				printf("Egad!!\n");
    580 			if (!error && *mp == NULL)
    581 				error = EPIPE;
    582 			len -= auio.uio_resid;
    583 		}
    584 errout:
    585 		if (error && error != EINTR && error != ERESTART) {
    586 			m_freem(*mp);
    587 			*mp = (struct mbuf *)0;
    588 			if (error != EPIPE)
    589 				log(LOG_INFO,
    590 				    "receive error %d from nfs server %s\n",
    591 				    error,
    592 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    593 			error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
    594 			if (!error)
    595 				error = nfs_reconnect(rep);
    596 			if (!error)
    597 				goto tryagain;
    598 		}
    599 	} else {
    600 		if ((so = rep->r_nmp->nm_so) == NULL)
    601 			return (EACCES);
    602 		if (so->so_state & SS_ISCONNECTED)
    603 			getnam = (struct mbuf **)0;
    604 		else
    605 			getnam = aname;
    606 		auio.uio_resid = len = 1000000;
    607 		auio.uio_procp = p;
    608 		do {
    609 			rcvflg = 0;
    610 			error =  soreceive(so, getnam, &auio, mp,
    611 				(struct mbuf **)0, &rcvflg);
    612 			if (error == EWOULDBLOCK &&
    613 			    (rep->r_flags & R_SOFTTERM))
    614 				return (EINTR);
    615 		} while (error == EWOULDBLOCK);
    616 		len -= auio.uio_resid;
    617 	}
    618 	if (error) {
    619 		m_freem(*mp);
    620 		*mp = (struct mbuf *)0;
    621 	}
    622 	/*
    623 	 * Search for any mbufs that are not a multiple of 4 bytes long
    624 	 * or with m_data not longword aligned.
    625 	 * These could cause pointer alignment problems, so copy them to
    626 	 * well aligned mbufs.
    627 	 */
    628 	nfs_realign(*mp, 5 * NFSX_UNSIGNED);
    629 	return (error);
    630 }
    631 
    632 /*
    633  * Implement receipt of reply on a socket.
    634  * We must search through the list of received datagrams matching them
    635  * with outstanding requests using the xid, until ours is found.
    636  */
    637 /* ARGSUSED */
    638 int
    639 nfs_reply(myrep)
    640 	struct nfsreq *myrep;
    641 {
    642 	register struct nfsreq *rep;
    643 	register struct nfsmount *nmp = myrep->r_nmp;
    644 	register int32_t t1;
    645 	struct mbuf *mrep, *nam, *md;
    646 	u_int32_t rxid, *tl;
    647 	caddr_t dpos, cp2;
    648 	int error;
    649 
    650 	/*
    651 	 * Loop around until we get our own reply
    652 	 */
    653 	for (;;) {
    654 		/*
    655 		 * Lock against other receivers so that I don't get stuck in
    656 		 * sbwait() after someone else has received my reply for me.
    657 		 * Also necessary for connection based protocols to avoid
    658 		 * race conditions during a reconnect.
    659 		 */
    660 		if ((error = nfs_rcvlock(myrep)) != 0)
    661 			return (error);
    662 		/* Already received, bye bye */
    663 		if (myrep->r_mrep != NULL) {
    664 			nfs_rcvunlock(&nmp->nm_flag);
    665 			return (0);
    666 		}
    667 		/*
    668 		 * Get the next Rpc reply off the socket
    669 		 */
    670 		error = nfs_receive(myrep, &nam, &mrep);
    671 		nfs_rcvunlock(&nmp->nm_flag);
    672 		if (error) {
    673 
    674 			/*
    675 			 * Ignore routing errors on connectionless protocols??
    676 			 */
    677 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
    678 				nmp->nm_so->so_error = 0;
    679 				if (myrep->r_flags & R_GETONEREP)
    680 					return (0);
    681 				continue;
    682 			}
    683 			return (error);
    684 		}
    685 		if (nam)
    686 			m_freem(nam);
    687 
    688 		/*
    689 		 * Get the xid and check that it is an rpc reply
    690 		 */
    691 		md = mrep;
    692 		dpos = mtod(md, caddr_t);
    693 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    694 		rxid = *tl++;
    695 		if (*tl != rpc_reply) {
    696 			if (nmp->nm_flag & NFSMNT_NQNFS) {
    697 				if (nqnfs_callback(nmp, mrep, md, dpos))
    698 					nfsstats.rpcinvalid++;
    699 			} else {
    700 				nfsstats.rpcinvalid++;
    701 				m_freem(mrep);
    702 			}
    703 nfsmout:
    704 			if (myrep->r_flags & R_GETONEREP)
    705 				return (0);
    706 			continue;
    707 		}
    708 
    709 		/*
    710 		 * Loop through the request list to match up the reply
    711 		 * Iff no match, just drop the datagram
    712 		 */
    713 		for (rep = nfs_reqq.tqh_first; rep != 0;
    714 		    rep = rep->r_chain.tqe_next) {
    715 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
    716 				/* Found it.. */
    717 				rep->r_mrep = mrep;
    718 				rep->r_md = md;
    719 				rep->r_dpos = dpos;
    720 				if (nfsrtton) {
    721 					struct rttl *rt;
    722 
    723 					rt = &nfsrtt.rttl[nfsrtt.pos];
    724 					rt->proc = rep->r_procnum;
    725 					rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
    726 					rt->sent = nmp->nm_sent;
    727 					rt->cwnd = nmp->nm_cwnd;
    728 					rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
    729 					rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
    730 					rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
    731 					rt->tstamp = time;
    732 					if (rep->r_flags & R_TIMING)
    733 						rt->rtt = rep->r_rtt;
    734 					else
    735 						rt->rtt = 1000000;
    736 					nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
    737 				}
    738 				/*
    739 				 * Update congestion window.
    740 				 * Do the additive increase of
    741 				 * one rpc/rtt.
    742 				 */
    743 				if (nmp->nm_cwnd <= nmp->nm_sent) {
    744 					nmp->nm_cwnd +=
    745 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
    746 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
    747 					if (nmp->nm_cwnd > NFS_MAXCWND)
    748 						nmp->nm_cwnd = NFS_MAXCWND;
    749 				}
    750 				rep->r_flags &= ~R_SENT;
    751 				nmp->nm_sent -= NFS_CWNDSCALE;
    752 				/*
    753 				 * Update rtt using a gain of 0.125 on the mean
    754 				 * and a gain of 0.25 on the deviation.
    755 				 */
    756 				if (rep->r_flags & R_TIMING) {
    757 					/*
    758 					 * Since the timer resolution of
    759 					 * NFS_HZ is so course, it can often
    760 					 * result in r_rtt == 0. Since
    761 					 * r_rtt == N means that the actual
    762 					 * rtt is between N+dt and N+2-dt ticks,
    763 					 * add 1.
    764 					 */
    765 					t1 = rep->r_rtt + 1;
    766 					t1 -= (NFS_SRTT(rep) >> 3);
    767 					NFS_SRTT(rep) += t1;
    768 					if (t1 < 0)
    769 						t1 = -t1;
    770 					t1 -= (NFS_SDRTT(rep) >> 2);
    771 					NFS_SDRTT(rep) += t1;
    772 				}
    773 				nmp->nm_timeouts = 0;
    774 				break;
    775 			}
    776 		}
    777 		/*
    778 		 * If not matched to a request, drop it.
    779 		 * If it's mine, get out.
    780 		 */
    781 		if (rep == 0) {
    782 			nfsstats.rpcunexpected++;
    783 			m_freem(mrep);
    784 		} else if (rep == myrep) {
    785 			if (rep->r_mrep == NULL)
    786 				panic("nfsreply nil");
    787 			return (0);
    788 		}
    789 		if (myrep->r_flags & R_GETONEREP)
    790 			return (0);
    791 	}
    792 }
    793 
    794 /*
    795  * nfs_request - goes something like this
    796  *	- fill in request struct
    797  *	- links it into list
    798  *	- calls nfs_send() for first transmit
    799  *	- calls nfs_receive() to get reply
    800  *	- break down rpc header and return with nfs reply pointed to
    801  *	  by mrep or error
    802  * nb: always frees up mreq mbuf list
    803  */
    804 int
    805 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
    806 	struct vnode *vp;
    807 	struct mbuf *mrest;
    808 	int procnum;
    809 	struct proc *procp;
    810 	struct ucred *cred;
    811 	struct mbuf **mrp;
    812 	struct mbuf **mdp;
    813 	caddr_t *dposp;
    814 {
    815 	register struct mbuf *m, *mrep;
    816 	register struct nfsreq *rep;
    817 	register u_int32_t *tl;
    818 	register int i;
    819 	struct nfsmount *nmp;
    820 	struct mbuf *md, *mheadend;
    821 	struct nfsnode *np;
    822 	time_t reqtime, waituntil;
    823 	caddr_t dpos, cp2;
    824 	int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
    825 	int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
    826 	u_int32_t xid;
    827 	u_quad_t frev;
    828 	char *auth_str;
    829 
    830 	nmp = VFSTONFS(vp->v_mount);
    831 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
    832 	rep->r_nmp = nmp;
    833 	rep->r_vp = vp;
    834 	rep->r_procp = procp;
    835 	rep->r_procnum = procnum;
    836 	i = 0;
    837 	m = mrest;
    838 	while (m) {
    839 		i += m->m_len;
    840 		m = m->m_next;
    841 	}
    842 	mrest_len = i;
    843 
    844 	/*
    845 	 * Get the RPC header with authorization.
    846 	 */
    847 kerbauth:
    848 	auth_str = (char *)0;
    849 	if (nmp->nm_flag & NFSMNT_KERB) {
    850 		if (failed_auth) {
    851 			error = nfs_getauth(nmp, rep, cred, &auth_type,
    852 				&auth_str, &auth_len);
    853 			if (error) {
    854 				free((caddr_t)rep, M_NFSREQ);
    855 				m_freem(mrest);
    856 				return (error);
    857 			}
    858 		} else {
    859 			auth_type = RPCAUTH_UNIX;
    860 			auth_len = 5 * NFSX_UNSIGNED;
    861 		}
    862 	} else {
    863 		auth_type = RPCAUTH_UNIX;
    864 		auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ?
    865 			nmp->nm_numgrps : cred->cr_ngroups) << 2) +
    866 			5 * NFSX_UNSIGNED;
    867 	}
    868 	m = nfsm_rpchead(cred, (nmp->nm_flag & NFSMNT_NQNFS), procnum,
    869 	     auth_type, auth_len, auth_str, mrest, mrest_len, &mheadend, &xid);
    870 	if (auth_str)
    871 		free(auth_str, M_TEMP);
    872 
    873 	/*
    874 	 * For stream protocols, insert a Sun RPC Record Mark.
    875 	 */
    876 	if (nmp->nm_sotype == SOCK_STREAM) {
    877 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    878 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
    879 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
    880 	}
    881 	rep->r_mreq = m;
    882 	rep->r_xid = xid;
    883 tryagain:
    884 	if (nmp->nm_flag & NFSMNT_SOFT)
    885 		rep->r_retry = nmp->nm_retry;
    886 	else
    887 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
    888 	rep->r_rtt = rep->r_rexmit = 0;
    889 	if (proct[procnum] > 0)
    890 		rep->r_flags = R_TIMING;
    891 	else
    892 		rep->r_flags = 0;
    893 	rep->r_mrep = NULL;
    894 
    895 	/*
    896 	 * Do the client side RPC.
    897 	 */
    898 	nfsstats.rpcrequests++;
    899 	/*
    900 	 * Chain request into list of outstanding requests. Be sure
    901 	 * to put it LAST so timer finds oldest requests first.
    902 	 */
    903 	s = splsoftclock();
    904 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
    905 
    906 	/* Get send time for nqnfs */
    907 	reqtime = time.tv_sec;
    908 
    909 	/*
    910 	 * If backing off another request or avoiding congestion, don't
    911 	 * send this one now but let timer do it. If not timing a request,
    912 	 * do it now.
    913 	 */
    914 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
    915 		(nmp->nm_flag & NFSMNT_DUMBTIMR) ||
    916 		nmp->nm_sent < nmp->nm_cwnd)) {
    917 		splx(s);
    918 		if (nmp->nm_soflags & PR_CONNREQUIRED)
    919 			error = nfs_sndlock(&nmp->nm_flag, rep);
    920 		if (!error) {
    921 			m = m_copym(m, 0, M_COPYALL, M_WAIT);
    922 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
    923 			if (nmp->nm_soflags & PR_CONNREQUIRED)
    924 				nfs_sndunlock(&nmp->nm_flag);
    925 		}
    926 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
    927 			nmp->nm_sent += NFS_CWNDSCALE;
    928 			rep->r_flags |= R_SENT;
    929 		}
    930 	} else {
    931 		splx(s);
    932 		rep->r_rtt = -1;
    933 	}
    934 
    935 	/*
    936 	 * Wait for the reply from our send or the timer's.
    937 	 */
    938 	if (!error || error == EPIPE)
    939 		error = nfs_reply(rep);
    940 
    941 	/*
    942 	 * RPC done, unlink the request.
    943 	 */
    944 	s = splsoftclock();
    945 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
    946 	splx(s);
    947 
    948 	/*
    949 	 * Decrement the outstanding request count.
    950 	 */
    951 	if (rep->r_flags & R_SENT) {
    952 		rep->r_flags &= ~R_SENT;	/* paranoia */
    953 		nmp->nm_sent -= NFS_CWNDSCALE;
    954 	}
    955 
    956 	/*
    957 	 * If there was a successful reply and a tprintf msg.
    958 	 * tprintf a response.
    959 	 */
    960 	if (!error && (rep->r_flags & R_TPRINTFMSG))
    961 		nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
    962 		    "is alive again");
    963 	mrep = rep->r_mrep;
    964 	md = rep->r_md;
    965 	dpos = rep->r_dpos;
    966 	if (error) {
    967 		m_freem(rep->r_mreq);
    968 		free((caddr_t)rep, M_NFSREQ);
    969 		return (error);
    970 	}
    971 
    972 	/*
    973 	 * break down the rpc header and check if ok
    974 	 */
    975 	nfsm_dissect(tl, u_int32_t *, 3*NFSX_UNSIGNED);
    976 	if (*tl++ == rpc_msgdenied) {
    977 		if (*tl == rpc_mismatch)
    978 			error = EOPNOTSUPP;
    979 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
    980 			if (*tl == rpc_rejectedcred && failed_auth == 0) {
    981 				failed_auth++;
    982 				mheadend->m_next = (struct mbuf *)0;
    983 				m_freem(mrep);
    984 				m_freem(rep->r_mreq);
    985 				goto kerbauth;
    986 			} else
    987 				error = EAUTH;
    988 		} else
    989 			error = EACCES;
    990 		m_freem(mrep);
    991 		m_freem(rep->r_mreq);
    992 		free((caddr_t)rep, M_NFSREQ);
    993 		return (error);
    994 	}
    995 
    996 	/*
    997 	 * skip over the auth_verf, someday we may want to cache auth_short's
    998 	 * for nfs_reqhead(), but for now just dump it
    999 	 */
   1000 	if (*++tl != 0) {
   1001 		i = nfsm_rndup(fxdr_unsigned(int32_t, *tl));
   1002 		nfsm_adv(i);
   1003 	}
   1004 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1005 	/* 0 == ok */
   1006 	if (*tl == 0) {
   1007 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1008 		if (*tl != 0) {
   1009 			error = fxdr_unsigned(int, *tl);
   1010 			m_freem(mrep);
   1011 			if ((nmp->nm_flag & NFSMNT_NQNFS) &&
   1012 			    error == NQNFS_TRYLATER) {
   1013 				error = 0;
   1014 				waituntil = time.tv_sec + trylater_delay;
   1015 				while (time.tv_sec < waituntil)
   1016 					(void) tsleep((caddr_t)&lbolt,
   1017 						PSOCK, "nqnfstry", 0);
   1018 				trylater_delay *= nfs_backoff[trylater_cnt];
   1019 				if (trylater_cnt < 7)
   1020 					trylater_cnt++;
   1021 				goto tryagain;
   1022 			}
   1023 
   1024 			/*
   1025 			 * If the File Handle was stale, invalidate the
   1026 			 * lookup cache, just in case.
   1027 			 */
   1028 			if (error == ESTALE)
   1029 				cache_purge(vp);
   1030 			m_freem(rep->r_mreq);
   1031 			free((caddr_t)rep, M_NFSREQ);
   1032 			return (error);
   1033 		}
   1034 
   1035 		/*
   1036 		 * For nqnfs, get any lease in reply
   1037 		 */
   1038 		if (nmp->nm_flag & NFSMNT_NQNFS) {
   1039 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1040 			if (*tl) {
   1041 				np = VTONFS(vp);
   1042 				nqlflag = fxdr_unsigned(int, *tl);
   1043 				nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
   1044 				cachable = fxdr_unsigned(int, *tl++);
   1045 				reqtime += fxdr_unsigned(int, *tl++);
   1046 				if (reqtime > time.tv_sec) {
   1047 				    fxdr_hyper(tl, &frev);
   1048 				    nqnfs_clientlease(nmp, np, nqlflag,
   1049 					cachable, reqtime, frev);
   1050 				}
   1051 			}
   1052 		}
   1053 		*mrp = mrep;
   1054 		*mdp = md;
   1055 		*dposp = dpos;
   1056 		m_freem(rep->r_mreq);
   1057 		FREE((caddr_t)rep, M_NFSREQ);
   1058 		return (0);
   1059 	}
   1060 	m_freem(mrep);
   1061 	m_freem(rep->r_mreq);
   1062 	free((caddr_t)rep, M_NFSREQ);
   1063 	error = EPROTONOSUPPORT;
   1064 nfsmout:
   1065 	return (error);
   1066 }
   1067 #endif /* NFSCLIENT */
   1068 
   1069 /*
   1070  * Generate the rpc reply header
   1071  * siz arg. is used to decide if adding a cluster is worthwhile
   1072  */
   1073 int
   1074 nfs_rephead(siz, nd, err, cache, frev, mrq, mbp, bposp)
   1075 	int siz;
   1076 	struct nfsd *nd;
   1077 	int err;
   1078 	int cache;
   1079 	u_quad_t *frev;
   1080 	struct mbuf **mrq;
   1081 	struct mbuf **mbp;
   1082 	caddr_t *bposp;
   1083 {
   1084 	register u_int32_t *tl;
   1085 	register struct mbuf *mreq;
   1086 	caddr_t bpos;
   1087 	struct mbuf *mb, *mb2;
   1088 
   1089 	MGETHDR(mreq, M_WAIT, MT_DATA);
   1090 	mb = mreq;
   1091 	/*
   1092 	 * If this is a big reply, use a cluster else
   1093 	 * try and leave leading space for the lower level headers.
   1094 	 */
   1095 	siz += RPC_REPLYSIZ;
   1096 	if (siz >= MINCLSIZE) {
   1097 		MCLGET(mreq, M_WAIT);
   1098 	} else
   1099 		mreq->m_data += max_hdr;
   1100 	tl = mtod(mreq, u_int32_t *);
   1101 	mreq->m_len = 6*NFSX_UNSIGNED;
   1102 	bpos = ((caddr_t)tl)+mreq->m_len;
   1103 	*tl++ = txdr_unsigned(nd->nd_retxid);
   1104 	*tl++ = rpc_reply;
   1105 	if (err == ERPCMISMATCH || err == NQNFS_AUTHERR) {
   1106 		*tl++ = rpc_msgdenied;
   1107 		if (err == NQNFS_AUTHERR) {
   1108 			*tl++ = rpc_autherr;
   1109 			*tl = rpc_rejectedcred;
   1110 			mreq->m_len -= NFSX_UNSIGNED;
   1111 			bpos -= NFSX_UNSIGNED;
   1112 		} else {
   1113 			*tl++ = rpc_mismatch;
   1114 			*tl++ = txdr_unsigned(2);
   1115 			*tl = txdr_unsigned(2);
   1116 		}
   1117 	} else {
   1118 		*tl++ = rpc_msgaccepted;
   1119 		*tl++ = 0;
   1120 		*tl++ = 0;
   1121 		switch (err) {
   1122 		case EPROGUNAVAIL:
   1123 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
   1124 			break;
   1125 		case EPROGMISMATCH:
   1126 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
   1127 			nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
   1128 			*tl++ = txdr_unsigned(2);
   1129 			*tl = txdr_unsigned(2);	/* someday 3 */
   1130 			break;
   1131 		case EPROCUNAVAIL:
   1132 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
   1133 			break;
   1134 		default:
   1135 			*tl = 0;
   1136 			if (err != VNOVAL) {
   1137 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1138 				if (err)
   1139 					*tl = txdr_unsigned(nfsrv_errmap[err - 1]);
   1140 				else
   1141 					*tl = 0;
   1142 			}
   1143 			break;
   1144 		};
   1145 	}
   1146 
   1147 	/*
   1148 	 * For nqnfs, piggyback lease as requested.
   1149 	 */
   1150 	if (nd->nd_nqlflag != NQL_NOVAL && err == 0) {
   1151 		if (nd->nd_nqlflag) {
   1152 			nfsm_build(tl, u_int32_t *, 5*NFSX_UNSIGNED);
   1153 			*tl++ = txdr_unsigned(nd->nd_nqlflag);
   1154 			*tl++ = txdr_unsigned(cache);
   1155 			*tl++ = txdr_unsigned(nd->nd_duration);
   1156 			txdr_hyper(frev, tl);
   1157 		} else {
   1158 			if (nd->nd_nqlflag != 0)
   1159 				panic("nqreph");
   1160 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1161 			*tl = 0;
   1162 		}
   1163 	}
   1164 	*mrq = mreq;
   1165 	*mbp = mb;
   1166 	*bposp = bpos;
   1167 	if (err != 0 && err != VNOVAL)
   1168 		nfsstats.srvrpc_errs++;
   1169 	return (0);
   1170 }
   1171 
   1172 /*
   1173  * Nfs timer routine
   1174  * Scan the nfsreq list and retranmit any requests that have timed out
   1175  * To avoid retransmission attempts on STREAM sockets (in the future) make
   1176  * sure to set the r_retry field to 0 (implies nm_retry == 0).
   1177  */
   1178 void
   1179 nfs_timer(arg)
   1180 	void *arg;
   1181 {
   1182 	register struct nfsreq *rep;
   1183 	register struct mbuf *m;
   1184 	register struct socket *so;
   1185 	register struct nfsmount *nmp;
   1186 	register int timeo;
   1187 #ifdef NFSSERVER
   1188 	static long lasttime = 0;
   1189 #endif
   1190 	int s, error;
   1191 
   1192 	s = splsoftnet();
   1193 	for (rep = nfs_reqq.tqh_first; rep != 0; rep = rep->r_chain.tqe_next) {
   1194 		nmp = rep->r_nmp;
   1195 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
   1196 			continue;
   1197 		if (nfs_sigintr(nmp, rep, rep->r_procp)) {
   1198 			rep->r_flags |= R_SOFTTERM;
   1199 			continue;
   1200 		}
   1201 		if (rep->r_rtt >= 0) {
   1202 			rep->r_rtt++;
   1203 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
   1204 				timeo = nmp->nm_timeo;
   1205 			else
   1206 				timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
   1207 			if (nmp->nm_timeouts > 0)
   1208 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
   1209 			if (rep->r_rtt <= timeo)
   1210 				continue;
   1211 			if (nmp->nm_timeouts < 8)
   1212 				nmp->nm_timeouts++;
   1213 		}
   1214 		/*
   1215 		 * Check for server not responding
   1216 		 */
   1217 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
   1218 		     rep->r_rexmit > nmp->nm_deadthresh) {
   1219 			nfs_msg(rep->r_procp,
   1220 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
   1221 			    "not responding");
   1222 			rep->r_flags |= R_TPRINTFMSG;
   1223 		}
   1224 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
   1225 			nfsstats.rpctimeouts++;
   1226 			rep->r_flags |= R_SOFTTERM;
   1227 			continue;
   1228 		}
   1229 		if (nmp->nm_sotype != SOCK_DGRAM) {
   1230 			if (++rep->r_rexmit > NFS_MAXREXMIT)
   1231 				rep->r_rexmit = NFS_MAXREXMIT;
   1232 			continue;
   1233 		}
   1234 		if ((so = nmp->nm_so) == NULL)
   1235 			continue;
   1236 
   1237 		/*
   1238 		 * If there is enough space and the window allows..
   1239 		 *	Resend it
   1240 		 * Set r_rtt to -1 in case we fail to send it now.
   1241 		 */
   1242 		rep->r_rtt = -1;
   1243 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
   1244 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
   1245 		    (rep->r_flags & R_SENT) ||
   1246 		    nmp->nm_sent < nmp->nm_cwnd) &&
   1247 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
   1248 			if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
   1249 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1250 			    (struct mbuf *)0, (struct mbuf *)0);
   1251 			else
   1252 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1253 			    nmp->nm_nam, (struct mbuf *)0);
   1254 			if (error) {
   1255 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
   1256 					so->so_error = 0;
   1257 			} else {
   1258 				/*
   1259 				 * Iff first send, start timing
   1260 				 * else turn timing off, backoff timer
   1261 				 * and divide congestion window by 2.
   1262 				 */
   1263 				if (rep->r_flags & R_SENT) {
   1264 					rep->r_flags &= ~R_TIMING;
   1265 					if (++rep->r_rexmit > NFS_MAXREXMIT)
   1266 						rep->r_rexmit = NFS_MAXREXMIT;
   1267 					nmp->nm_cwnd >>= 1;
   1268 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
   1269 						nmp->nm_cwnd = NFS_CWNDSCALE;
   1270 					nfsstats.rpcretries++;
   1271 				} else {
   1272 					rep->r_flags |= R_SENT;
   1273 					nmp->nm_sent += NFS_CWNDSCALE;
   1274 				}
   1275 				rep->r_rtt = 0;
   1276 			}
   1277 		}
   1278 	}
   1279 
   1280 #ifdef NFSSERVER
   1281 	/*
   1282 	 * Call the nqnfs server timer once a second to handle leases.
   1283 	 */
   1284 	if (lasttime != time.tv_sec) {
   1285 		lasttime = time.tv_sec;
   1286 		nqnfs_serverd();
   1287 	}
   1288 #endif /* NFSSERVER */
   1289 	splx(s);
   1290 	timeout(nfs_timer, (void *)0, hz / NFS_HZ);
   1291 }
   1292 
   1293 /*
   1294  * Test for a termination condition pending on the process.
   1295  * This is used for NFSMNT_INT mounts.
   1296  */
   1297 int
   1298 nfs_sigintr(nmp, rep, p)
   1299 	struct nfsmount *nmp;
   1300 	struct nfsreq *rep;
   1301 	register struct proc *p;
   1302 {
   1303 
   1304 	if (rep && (rep->r_flags & R_SOFTTERM))
   1305 		return (EINTR);
   1306 	if (!(nmp->nm_flag & NFSMNT_INT))
   1307 		return (0);
   1308 	if (p && p->p_siglist &&
   1309 	    (((p->p_siglist & ~p->p_sigmask) & ~p->p_sigignore) &
   1310 	    NFSINT_SIGMASK))
   1311 		return (EINTR);
   1312 	return (0);
   1313 }
   1314 
   1315 /*
   1316  * Lock a socket against others.
   1317  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
   1318  * and also to avoid race conditions between the processes with nfs requests
   1319  * in progress when a reconnect is necessary.
   1320  */
   1321 int
   1322 nfs_sndlock(flagp, rep)
   1323 	register int *flagp;
   1324 	struct nfsreq *rep;
   1325 {
   1326 	struct proc *p;
   1327 	int slpflag = 0, slptimeo = 0;
   1328 
   1329 	if (rep) {
   1330 		p = rep->r_procp;
   1331 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
   1332 			slpflag = PCATCH;
   1333 	} else
   1334 		p = (struct proc *)0;
   1335 	while (*flagp & NFSMNT_SNDLOCK) {
   1336 		if (nfs_sigintr(rep->r_nmp, rep, p))
   1337 			return (EINTR);
   1338 		*flagp |= NFSMNT_WANTSND;
   1339 		(void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
   1340 			slptimeo);
   1341 		if (slpflag == PCATCH) {
   1342 			slpflag = 0;
   1343 			slptimeo = 2 * hz;
   1344 		}
   1345 	}
   1346 	*flagp |= NFSMNT_SNDLOCK;
   1347 	return (0);
   1348 }
   1349 
   1350 /*
   1351  * Unlock the stream socket for others.
   1352  */
   1353 void
   1354 nfs_sndunlock(flagp)
   1355 	register int *flagp;
   1356 {
   1357 
   1358 	if ((*flagp & NFSMNT_SNDLOCK) == 0)
   1359 		panic("nfs sndunlock");
   1360 	*flagp &= ~NFSMNT_SNDLOCK;
   1361 	if (*flagp & NFSMNT_WANTSND) {
   1362 		*flagp &= ~NFSMNT_WANTSND;
   1363 		wakeup((caddr_t)flagp);
   1364 	}
   1365 }
   1366 
   1367 int
   1368 nfs_rcvlock(rep)
   1369 	register struct nfsreq *rep;
   1370 {
   1371 	register int *flagp = &rep->r_nmp->nm_flag;
   1372 	int slpflag, slptimeo = 0;
   1373 
   1374 	if (*flagp & NFSMNT_INT)
   1375 		slpflag = PCATCH;
   1376 	else
   1377 		slpflag = 0;
   1378 	while (*flagp & NFSMNT_RCVLOCK) {
   1379 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
   1380 			return (EINTR);
   1381 		*flagp |= NFSMNT_WANTRCV;
   1382 		(void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
   1383 			slptimeo);
   1384 		if (slpflag == PCATCH) {
   1385 			slpflag = 0;
   1386 			slptimeo = 2 * hz;
   1387 		}
   1388 	}
   1389 	*flagp |= NFSMNT_RCVLOCK;
   1390 	return (0);
   1391 }
   1392 
   1393 /*
   1394  * Unlock the stream socket for others.
   1395  */
   1396 void
   1397 nfs_rcvunlock(flagp)
   1398 	register int *flagp;
   1399 {
   1400 
   1401 	if ((*flagp & NFSMNT_RCVLOCK) == 0)
   1402 		panic("nfs rcvunlock");
   1403 	*flagp &= ~NFSMNT_RCVLOCK;
   1404 	if (*flagp & NFSMNT_WANTRCV) {
   1405 		*flagp &= ~NFSMNT_WANTRCV;
   1406 		wakeup((caddr_t)flagp);
   1407 	}
   1408 }
   1409 
   1410 /*
   1411  * Check for badly aligned mbuf data areas and
   1412  * realign data in an mbuf list by copying the data areas up, as required.
   1413  */
   1414 void
   1415 nfs_realign(m, hsiz)
   1416 	register struct mbuf *m;
   1417 	int hsiz;
   1418 {
   1419 	register struct mbuf *m2;
   1420 	register int siz, mlen, olen;
   1421 	register caddr_t tcp, fcp;
   1422 	struct mbuf *mnew;
   1423 
   1424 	while (m) {
   1425 	    /*
   1426 	     * This never happens for UDP, rarely happens for TCP
   1427 	     * but frequently happens for iso transport.
   1428 	     */
   1429 	    if ((m->m_len & 0x3) || (mtod(m, long) & 0x3)) {
   1430 		olen = m->m_len;
   1431 		fcp = mtod(m, caddr_t);
   1432 		if ((long)fcp & 0x3) {
   1433 			m->m_flags &= ~M_PKTHDR;
   1434 			if (m->m_flags & M_EXT)
   1435 				m->m_data = m->m_ext.ext_buf +
   1436 					((m->m_ext.ext_size - olen) & ~0x3);
   1437 			else
   1438 				m->m_data = m->m_dat;
   1439 		}
   1440 		m->m_len = 0;
   1441 		tcp = mtod(m, caddr_t);
   1442 		mnew = m;
   1443 		m2 = m->m_next;
   1444 
   1445 		/*
   1446 		 * If possible, only put the first invariant part
   1447 		 * of the RPC header in the first mbuf.
   1448 		 */
   1449 		mlen = M_TRAILINGSPACE(m);
   1450 		if (olen <= hsiz && mlen > hsiz)
   1451 			mlen = hsiz;
   1452 
   1453 		/*
   1454 		 * Loop through the mbuf list consolidating data.
   1455 		 */
   1456 		while (m) {
   1457 			while (olen > 0) {
   1458 				if (mlen == 0) {
   1459 					m2->m_flags &= ~M_PKTHDR;
   1460 					if (m2->m_flags & M_EXT)
   1461 						m2->m_data = m2->m_ext.ext_buf;
   1462 					else
   1463 						m2->m_data = m2->m_dat;
   1464 					m2->m_len = 0;
   1465 					mlen = M_TRAILINGSPACE(m2);
   1466 					tcp = mtod(m2, caddr_t);
   1467 					mnew = m2;
   1468 					m2 = m2->m_next;
   1469 				}
   1470 				siz = min(mlen, olen);
   1471 				if (tcp != fcp)
   1472 					bcopy(fcp, tcp, siz);
   1473 				mnew->m_len += siz;
   1474 				mlen -= siz;
   1475 				olen -= siz;
   1476 				tcp += siz;
   1477 				fcp += siz;
   1478 			}
   1479 			m = m->m_next;
   1480 			if (m) {
   1481 				olen = m->m_len;
   1482 				fcp = mtod(m, caddr_t);
   1483 			}
   1484 		}
   1485 
   1486 		/*
   1487 		 * Finally, set m_len == 0 for any trailing mbufs that have
   1488 		 * been copied out of.
   1489 		 */
   1490 		while (m2) {
   1491 			m2->m_len = 0;
   1492 			m2 = m2->m_next;
   1493 		}
   1494 		return;
   1495 	    }
   1496 	    m = m->m_next;
   1497 	}
   1498 }
   1499 
   1500 /*
   1501  * Parse an RPC request
   1502  * - verify it
   1503  * - fill in the cred struct.
   1504  */
   1505 int
   1506 nfs_getreq(nd, has_header)
   1507 	register struct nfsd *nd;
   1508 	int has_header;
   1509 {
   1510 	register int len, i;
   1511 	register u_int32_t *tl;
   1512 	register int32_t t1;
   1513 	struct uio uio;
   1514 	struct iovec iov;
   1515 	caddr_t dpos, cp2;
   1516 	u_int32_t nfsvers, auth_type;
   1517 	int error = 0, nqnfs = 0;
   1518 	struct mbuf *mrep, *md;
   1519 
   1520 	mrep = nd->nd_mrep;
   1521 	md = nd->nd_md;
   1522 	dpos = nd->nd_dpos;
   1523 	if (has_header) {
   1524 		nfsm_dissect(tl, u_int32_t *, 10*NFSX_UNSIGNED);
   1525 		nd->nd_retxid = fxdr_unsigned(u_int32_t , *tl++);
   1526 		if (*tl++ != rpc_call) {
   1527 			m_freem(mrep);
   1528 			return (EBADRPC);
   1529 		}
   1530 	} else {
   1531 		nfsm_dissect(tl, u_int32_t *, 8*NFSX_UNSIGNED);
   1532 	}
   1533 	nd->nd_repstat = 0;
   1534 	if (*tl++ != rpc_vers) {
   1535 		nd->nd_repstat = ERPCMISMATCH;
   1536 		nd->nd_procnum = NFSPROC_NOOP;
   1537 		return (0);
   1538 	}
   1539 	nfsvers = nfs_vers;
   1540 	if (*tl != nfs_prog) {
   1541 		if (*tl == nqnfs_prog) {
   1542 			nqnfs++;
   1543 			nfsvers = nqnfs_vers;
   1544 		} else {
   1545 			nd->nd_repstat = EPROGUNAVAIL;
   1546 			nd->nd_procnum = NFSPROC_NOOP;
   1547 			return (0);
   1548 		}
   1549 	}
   1550 	tl++;
   1551 	if (*tl++ != nfsvers) {
   1552 		nd->nd_repstat = EPROGMISMATCH;
   1553 		nd->nd_procnum = NFSPROC_NOOP;
   1554 		return (0);
   1555 	}
   1556 	nd->nd_procnum = fxdr_unsigned(u_int32_t , *tl++);
   1557 	if (nd->nd_procnum == NFSPROC_NULL)
   1558 		return (0);
   1559 	if (nd->nd_procnum >= NFS_NPROCS ||
   1560 		(!nqnfs && nd->nd_procnum > NFSPROC_STATFS) ||
   1561 		(*tl != rpc_auth_unix && *tl != rpc_auth_kerb)) {
   1562 		nd->nd_repstat = EPROCUNAVAIL;
   1563 		nd->nd_procnum = NFSPROC_NOOP;
   1564 		return (0);
   1565 	}
   1566 	auth_type = *tl++;
   1567 	len = fxdr_unsigned(int, *tl++);
   1568 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1569 		m_freem(mrep);
   1570 		return (EBADRPC);
   1571 	}
   1572 
   1573 	/*
   1574 	 * Handle auth_unix or auth_kerb.
   1575 	 */
   1576 	if (auth_type == rpc_auth_unix) {
   1577 		len = fxdr_unsigned(int, *++tl);
   1578 		if (len < 0 || len > NFS_MAXNAMLEN) {
   1579 			m_freem(mrep);
   1580 			return (EBADRPC);
   1581 		}
   1582 		nfsm_adv(nfsm_rndup(len));
   1583 		nfsm_dissect(tl, u_int32_t *, 3*NFSX_UNSIGNED);
   1584 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
   1585 		nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
   1586 		len = fxdr_unsigned(int, *tl);
   1587 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
   1588 			m_freem(mrep);
   1589 			return (EBADRPC);
   1590 		}
   1591 		nfsm_dissect(tl, u_int32_t *, (len + 2)*NFSX_UNSIGNED);
   1592 		for (i = 0; i < len; i++)
   1593 			if (i < NGROUPS)
   1594 				nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
   1595 			else
   1596 				tl++;
   1597 		nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
   1598 	} else if (auth_type == rpc_auth_kerb) {
   1599 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
   1600 		nd->nd_authlen = fxdr_unsigned(int, *tl);
   1601 		uio.uio_resid = nfsm_rndup(nd->nd_authlen);
   1602 		if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
   1603 			m_freem(mrep);
   1604 			return (EBADRPC);
   1605 		}
   1606 		uio.uio_offset = 0;
   1607 		uio.uio_iov = &iov;
   1608 		uio.uio_iovcnt = 1;
   1609 		uio.uio_segflg = UIO_SYSSPACE;
   1610 		iov.iov_base = (caddr_t)nd->nd_authstr;
   1611 		iov.iov_len = RPCAUTH_MAXSIZ;
   1612 		nfsm_mtouio(&uio, uio.uio_resid);
   1613 		nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1614 		nd->nd_flag |= NFSD_NEEDAUTH;
   1615 	}
   1616 
   1617 	/*
   1618 	 * Do we have any use for the verifier.
   1619 	 * According to the "Remote Procedure Call Protocol Spec." it
   1620 	 * should be AUTH_NULL, but some clients make it AUTH_UNIX?
   1621 	 * For now, just skip over it
   1622 	 */
   1623 	len = fxdr_unsigned(int, *++tl);
   1624 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1625 		m_freem(mrep);
   1626 		return (EBADRPC);
   1627 	}
   1628 	if (len > 0) {
   1629 		nfsm_adv(nfsm_rndup(len));
   1630 	}
   1631 
   1632 	/*
   1633 	 * For nqnfs, get piggybacked lease request.
   1634 	 */
   1635 	if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
   1636 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1637 		nd->nd_nqlflag = fxdr_unsigned(int, *tl);
   1638 		if (nd->nd_nqlflag) {
   1639 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1640 			nd->nd_duration = fxdr_unsigned(int, *tl);
   1641 		} else
   1642 			nd->nd_duration = NQ_MINLEASE;
   1643 	} else {
   1644 		nd->nd_nqlflag = NQL_NOVAL;
   1645 		nd->nd_duration = NQ_MINLEASE;
   1646 	}
   1647 	nd->nd_md = md;
   1648 	nd->nd_dpos = dpos;
   1649 	return (0);
   1650 nfsmout:
   1651 	return (error);
   1652 }
   1653 
   1654 void
   1655 nfs_msg(p, server, msg)
   1656 	struct proc *p;
   1657 	char *server, *msg;
   1658 {
   1659 	tpr_t tpr;
   1660 
   1661 	if (p)
   1662 		tpr = tprintf_open(p);
   1663 	else
   1664 		tpr = NULL;
   1665 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
   1666 	tprintf_close(tpr);
   1667 }
   1668 
   1669 #ifdef NFSSERVER
   1670 int (*nfsrv_procs[NFS_NPROCS]) __P((struct nfsd *, struct mbuf *, struct mbuf *,
   1671 				    caddr_t, struct ucred *, struct mbuf *,
   1672 				    struct mbuf **)) = {
   1673 	nfsrv_null,
   1674 	nfsrv_getattr,
   1675 	nfsrv_setattr,
   1676 	nfsrv_noop,
   1677 	nfsrv_lookup,
   1678 	nfsrv_readlink,
   1679 	nfsrv_read,
   1680 	nfsrv_noop,
   1681 	nfsrv_write,
   1682 	nfsrv_create,
   1683 	nfsrv_remove,
   1684 	nfsrv_rename,
   1685 	nfsrv_link,
   1686 	nfsrv_symlink,
   1687 	nfsrv_mkdir,
   1688 	nfsrv_rmdir,
   1689 	nfsrv_readdir,
   1690 	nfsrv_statfs,
   1691 	nqnfsrv_readdirlook,
   1692 	nqnfsrv_getlease,
   1693 	nqnfsrv_vacated,
   1694 	nfsrv_noop,
   1695 	nqnfsrv_access,
   1696 };
   1697 
   1698 /*
   1699  * Socket upcall routine for the nfsd sockets.
   1700  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
   1701  * Essentially do as much as possible non-blocking, else punt and it will
   1702  * be called with M_WAIT from an nfsd.
   1703  */
   1704 void
   1705 nfsrv_rcv(so, arg, waitflag)
   1706 	struct socket *so;
   1707 	caddr_t arg;
   1708 	int waitflag;
   1709 {
   1710 	register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
   1711 	register struct mbuf *m;
   1712 	struct mbuf *mp, *nam;
   1713 	struct uio auio;
   1714 	int flags, error;
   1715 
   1716 	if ((slp->ns_flag & SLP_VALID) == 0)
   1717 		return;
   1718 #ifdef notdef
   1719 	/*
   1720 	 * Define this to test for nfsds handling this under heavy load.
   1721 	 */
   1722 	if (waitflag == M_DONTWAIT) {
   1723 		slp->ns_flag |= SLP_NEEDQ; goto dorecs;
   1724 	}
   1725 #endif
   1726 	auio.uio_procp = NULL;
   1727 	if (so->so_type == SOCK_STREAM) {
   1728 		/*
   1729 		 * If there are already records on the queue, defer soreceive()
   1730 		 * to an nfsd so that there is feedback to the TCP layer that
   1731 		 * the nfs servers are heavily loaded.
   1732 		 */
   1733 		if (slp->ns_rec && waitflag == M_DONTWAIT) {
   1734 			slp->ns_flag |= SLP_NEEDQ;
   1735 			goto dorecs;
   1736 		}
   1737 
   1738 		/*
   1739 		 * Do soreceive().
   1740 		 */
   1741 		auio.uio_resid = 1000000000;
   1742 		flags = MSG_DONTWAIT;
   1743 		error = soreceive(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
   1744 		if (error || mp == (struct mbuf *)0) {
   1745 			if (error == EWOULDBLOCK)
   1746 				slp->ns_flag |= SLP_NEEDQ;
   1747 			else
   1748 				slp->ns_flag |= SLP_DISCONN;
   1749 			goto dorecs;
   1750 		}
   1751 		m = mp;
   1752 		if (slp->ns_rawend) {
   1753 			slp->ns_rawend->m_next = m;
   1754 			slp->ns_cc += 1000000000 - auio.uio_resid;
   1755 		} else {
   1756 			slp->ns_raw = m;
   1757 			slp->ns_cc = 1000000000 - auio.uio_resid;
   1758 		}
   1759 		while (m->m_next)
   1760 			m = m->m_next;
   1761 		slp->ns_rawend = m;
   1762 
   1763 		/*
   1764 		 * Now try and parse record(s) out of the raw stream data.
   1765 		 */
   1766 		if ((error = nfsrv_getstream(slp, waitflag)) != 0) {
   1767 			if (error == EPERM)
   1768 				slp->ns_flag |= SLP_DISCONN;
   1769 			else
   1770 				slp->ns_flag |= SLP_NEEDQ;
   1771 		}
   1772 	} else {
   1773 		do {
   1774 			auio.uio_resid = 1000000000;
   1775 			flags = MSG_DONTWAIT;
   1776 			error = soreceive(so, &nam, &auio, &mp,
   1777 						(struct mbuf **)0, &flags);
   1778 			if (mp) {
   1779 				nfs_realign(mp, 10 * NFSX_UNSIGNED);
   1780 				if (nam) {
   1781 					m = nam;
   1782 					m->m_next = mp;
   1783 				} else
   1784 					m = mp;
   1785 				if (slp->ns_recend)
   1786 					slp->ns_recend->m_nextpkt = m;
   1787 				else
   1788 					slp->ns_rec = m;
   1789 				slp->ns_recend = m;
   1790 				m->m_nextpkt = (struct mbuf *)0;
   1791 			}
   1792 			if (error) {
   1793 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
   1794 					&& error != EWOULDBLOCK) {
   1795 					slp->ns_flag |= SLP_DISCONN;
   1796 					goto dorecs;
   1797 				}
   1798 			}
   1799 		} while (mp);
   1800 	}
   1801 
   1802 	/*
   1803 	 * Now try and process the request records, non-blocking.
   1804 	 */
   1805 dorecs:
   1806 	if (waitflag == M_DONTWAIT &&
   1807 		(slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
   1808 		nfsrv_wakenfsd(slp);
   1809 }
   1810 
   1811 /*
   1812  * Try and extract an RPC request from the mbuf data list received on a
   1813  * stream socket. The "waitflag" argument indicates whether or not it
   1814  * can sleep.
   1815  */
   1816 int
   1817 nfsrv_getstream(slp, waitflag)
   1818 	register struct nfssvc_sock *slp;
   1819 	int waitflag;
   1820 {
   1821 	register struct mbuf *m;
   1822 	register char *cp1, *cp2;
   1823 	register int len;
   1824 	struct mbuf *om, *m2, *recm = NULL;
   1825 	u_long recmark;
   1826 
   1827 	if (slp->ns_flag & SLP_GETSTREAM)
   1828 		panic("nfs getstream");
   1829 	slp->ns_flag |= SLP_GETSTREAM;
   1830 	for (;;) {
   1831 	    if (slp->ns_reclen == 0) {
   1832 		if (slp->ns_cc < NFSX_UNSIGNED) {
   1833 			slp->ns_flag &= ~SLP_GETSTREAM;
   1834 			return (0);
   1835 		}
   1836 		m = slp->ns_raw;
   1837 		if (m->m_len >= NFSX_UNSIGNED) {
   1838 			bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
   1839 			m->m_data += NFSX_UNSIGNED;
   1840 			m->m_len -= NFSX_UNSIGNED;
   1841 		} else {
   1842 			cp1 = (caddr_t)&recmark;
   1843 			cp2 = mtod(m, caddr_t);
   1844 			while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
   1845 				while (m->m_len == 0) {
   1846 					m = m->m_next;
   1847 					cp2 = mtod(m, caddr_t);
   1848 				}
   1849 				*cp1++ = *cp2++;
   1850 				m->m_data++;
   1851 				m->m_len--;
   1852 			}
   1853 		}
   1854 		slp->ns_cc -= NFSX_UNSIGNED;
   1855 		slp->ns_reclen = ntohl(recmark) & ~0x80000000;
   1856 		if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
   1857 			slp->ns_flag &= ~SLP_GETSTREAM;
   1858 			return (EPERM);
   1859 		}
   1860 	    }
   1861 
   1862 	    /*
   1863 	     * Now get the record part.
   1864 	     */
   1865 	    if (slp->ns_cc == slp->ns_reclen) {
   1866 		recm = slp->ns_raw;
   1867 		slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
   1868 		slp->ns_cc = slp->ns_reclen = 0;
   1869 	    } else if (slp->ns_cc > slp->ns_reclen) {
   1870 		len = 0;
   1871 		m = slp->ns_raw;
   1872 		om = (struct mbuf *)0;
   1873 		while (len < slp->ns_reclen) {
   1874 			if ((len + m->m_len) > slp->ns_reclen) {
   1875 				m2 = m_copym(m, 0, slp->ns_reclen - len,
   1876 					waitflag);
   1877 				if (m2) {
   1878 					if (om) {
   1879 						om->m_next = m2;
   1880 						recm = slp->ns_raw;
   1881 					} else
   1882 						recm = m2;
   1883 					m->m_data += slp->ns_reclen - len;
   1884 					m->m_len -= slp->ns_reclen - len;
   1885 					len = slp->ns_reclen;
   1886 				} else {
   1887 					slp->ns_flag &= ~SLP_GETSTREAM;
   1888 					return (EWOULDBLOCK);
   1889 				}
   1890 			} else if ((len + m->m_len) == slp->ns_reclen) {
   1891 				om = m;
   1892 				len += m->m_len;
   1893 				m = m->m_next;
   1894 				recm = slp->ns_raw;
   1895 				om->m_next = (struct mbuf *)0;
   1896 			} else {
   1897 				om = m;
   1898 				len += m->m_len;
   1899 				m = m->m_next;
   1900 			}
   1901 		}
   1902 		slp->ns_raw = m;
   1903 		slp->ns_cc -= len;
   1904 		slp->ns_reclen = 0;
   1905 	    } else {
   1906 		slp->ns_flag &= ~SLP_GETSTREAM;
   1907 		return (0);
   1908 	    }
   1909 	    nfs_realign(recm, 10 * NFSX_UNSIGNED);
   1910 	    if (slp->ns_recend)
   1911 		slp->ns_recend->m_nextpkt = recm;
   1912 	    else
   1913 		slp->ns_rec = recm;
   1914 	    slp->ns_recend = recm;
   1915 	}
   1916 }
   1917 
   1918 /*
   1919  * Parse an RPC header.
   1920  */
   1921 int
   1922 nfsrv_dorec(slp, nd)
   1923 	register struct nfssvc_sock *slp;
   1924 	register struct nfsd *nd;
   1925 {
   1926 	register struct mbuf *m;
   1927 	int error;
   1928 
   1929 	if ((slp->ns_flag & SLP_VALID) == 0 ||
   1930 	    (m = slp->ns_rec) == (struct mbuf *)0)
   1931 		return (ENOBUFS);
   1932 	if ((slp->ns_rec = m->m_nextpkt) != NULL)
   1933 		m->m_nextpkt = (struct mbuf *)0;
   1934 	else
   1935 		slp->ns_recend = (struct mbuf *)0;
   1936 	if (m->m_type == MT_SONAME) {
   1937 		nd->nd_nam = m;
   1938 		nd->nd_md = nd->nd_mrep = m->m_next;
   1939 		m->m_next = (struct mbuf *)0;
   1940 	} else {
   1941 		nd->nd_nam = (struct mbuf *)0;
   1942 		nd->nd_md = nd->nd_mrep = m;
   1943 	}
   1944 	nd->nd_dpos = mtod(nd->nd_md, caddr_t);
   1945 	if ((error = nfs_getreq(nd, TRUE)) != 0) {
   1946 		m_freem(nd->nd_nam);
   1947 		return (error);
   1948 	}
   1949 	return (0);
   1950 }
   1951 
   1952 /*
   1953  * Search for a sleeping nfsd and wake it up.
   1954  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
   1955  * running nfsds will go look for the work in the nfssvc_sock list.
   1956  */
   1957 void
   1958 nfsrv_wakenfsd(slp)
   1959 	struct nfssvc_sock *slp;
   1960 {
   1961 	register struct nfsd *nd;
   1962 
   1963 	if ((slp->ns_flag & SLP_VALID) == 0)
   1964 		return;
   1965 	for (nd = nfsd_head.tqh_first; nd != 0; nd = nd->nd_chain.tqe_next) {
   1966 		if (nd->nd_flag & NFSD_WAITING) {
   1967 			nd->nd_flag &= ~NFSD_WAITING;
   1968 			if (nd->nd_slp)
   1969 				panic("nfsd wakeup");
   1970 			slp->ns_sref++;
   1971 			nd->nd_slp = slp;
   1972 			wakeup((caddr_t)nd);
   1973 			return;
   1974 		}
   1975 	}
   1976 	slp->ns_flag |= SLP_DOREC;
   1977 	nfsd_head_flag |= NFSD_CHECKSLP;
   1978 }
   1979 #endif /* NFSSERVER */
   1980