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