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