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