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