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