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