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