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