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