Home | History | Annotate | Line # | Download | only in nfs
nfs_socket.c revision 1.104
      1 /*	$NetBSD: nfs_socket.c,v 1.104 2004/05/10 10:40:42 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_socket.c	8.5 (Berkeley) 3/30/95
     35  */
     36 
     37 /*
     38  * Socket operations for use by nfs
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.104 2004/05/10 10:40:42 yamt Exp $");
     43 
     44 #include "fs_nfs.h"
     45 #include "opt_nfs.h"
     46 #include "opt_nfsserver.h"
     47 #include "opt_mbuftrace.h"
     48 #include "opt_inet.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/callout.h>
     53 #include <sys/proc.h>
     54 #include <sys/mount.h>
     55 #include <sys/kernel.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/vnode.h>
     58 #include <sys/domain.h>
     59 #include <sys/protosw.h>
     60 #include <sys/socket.h>
     61 #include <sys/socketvar.h>
     62 #include <sys/syslog.h>
     63 #include <sys/tprintf.h>
     64 #include <sys/namei.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 
     68 #include <netinet/in.h>
     69 #include <netinet/tcp.h>
     70 
     71 #include <nfs/rpcv2.h>
     72 #include <nfs/nfsproto.h>
     73 #include <nfs/nfs.h>
     74 #include <nfs/xdr_subs.h>
     75 #include <nfs/nfsm_subs.h>
     76 #include <nfs/nfsmount.h>
     77 #include <nfs/nfsnode.h>
     78 #include <nfs/nfsrtt.h>
     79 #include <nfs/nqnfs.h>
     80 #include <nfs/nfs_var.h>
     81 
     82 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
     83 #ifdef MBUFTRACE
     84 struct mowner nfs_mowner = { "nfs" };
     85 #endif
     86 
     87 /*
     88  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
     89  * Use the mean and mean deviation of rtt for the appropriate type of rpc
     90  * for the frequent rpcs and a default for the others.
     91  * The justification for doing "other" this way is that these rpcs
     92  * happen so infrequently that timer est. would probably be stale.
     93  * Also, since many of these rpcs are
     94  * non-idempotent, a conservative timeout is desired.
     95  * getattr, lookup - A+2D
     96  * read, write     - A+4D
     97  * other           - nm_timeo
     98  */
     99 #define	NFS_RTO(n, t) \
    100 	((t) == 0 ? (n)->nm_timeo : \
    101 	 ((t) < 3 ? \
    102 	  (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
    103 	  ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
    104 #define	NFS_SRTT(r)	(r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
    105 #define	NFS_SDRTT(r)	(r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
    106 /*
    107  * External data, mostly RPC constants in XDR form
    108  */
    109 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
    110 	rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
    111 	rpc_auth_kerb;
    112 extern u_int32_t nfs_prog, nqnfs_prog;
    113 extern time_t nqnfsstarttime;
    114 extern const int nfsv3_procid[NFS_NPROCS];
    115 extern int nfs_ticks;
    116 
    117 /*
    118  * Defines which timer to use for the procnum.
    119  * 0 - default
    120  * 1 - getattr
    121  * 2 - lookup
    122  * 3 - read
    123  * 4 - write
    124  */
    125 static const int proct[NFS_NPROCS] = {
    126 	0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
    127 	0, 0, 0,
    128 };
    129 
    130 /*
    131  * There is a congestion window for outstanding rpcs maintained per mount
    132  * point. The cwnd size is adjusted in roughly the way that:
    133  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
    134  * SIGCOMM '88". ACM, August 1988.
    135  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
    136  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
    137  * of rpcs is in progress.
    138  * (The sent count and cwnd are scaled for integer arith.)
    139  * Variants of "slow start" were tried and were found to be too much of a
    140  * performance hit (ave. rtt 3 times larger),
    141  * I suspect due to the large rtt that nfs rpcs have.
    142  */
    143 #define	NFS_CWNDSCALE	256
    144 #define	NFS_MAXCWND	(NFS_CWNDSCALE * 32)
    145 static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
    146 int nfsrtton = 0;
    147 struct nfsrtt nfsrtt;
    148 struct nfsreqhead nfs_reqq;
    149 
    150 struct callout nfs_timer_ch = CALLOUT_INITIALIZER_SETFUNC(nfs_timer, NULL);
    151 
    152 /*
    153  * Initialize sockets and congestion for a new NFS connection.
    154  * We do not free the sockaddr if error.
    155  */
    156 int
    157 nfs_connect(nmp, rep)
    158 	struct nfsmount *nmp;
    159 	struct nfsreq *rep;
    160 {
    161 	struct socket *so;
    162 	int s, error, rcvreserve, sndreserve;
    163 	struct sockaddr *saddr;
    164 	struct sockaddr_in *sin;
    165 #ifdef INET6
    166 	struct sockaddr_in6 *sin6;
    167 #endif
    168 	struct mbuf *m;
    169 
    170 	nmp->nm_so = (struct socket *)0;
    171 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
    172 	error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
    173 		nmp->nm_soproto);
    174 	if (error)
    175 		goto bad;
    176 	so = nmp->nm_so;
    177 #ifdef MBUFTRACE
    178 	so->so_mowner = &nfs_mowner;
    179 	so->so_rcv.sb_mowner = &nfs_mowner;
    180 	so->so_snd.sb_mowner = &nfs_mowner;
    181 #endif
    182 	nmp->nm_soflags = so->so_proto->pr_flags;
    183 
    184 	/*
    185 	 * Some servers require that the client port be a reserved port number.
    186 	 */
    187 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    188 		m = m_get(M_WAIT, MT_SOOPTS);
    189 		MCLAIM(m, so->so_mowner);
    190 		*mtod(m, int32_t *) = IP_PORTRANGE_LOW;
    191 		m->m_len = sizeof(int32_t);
    192 		if ((error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, m)))
    193 			goto bad;
    194 		m = m_get(M_WAIT, MT_SONAME);
    195 		MCLAIM(m, so->so_mowner);
    196 		sin = mtod(m, struct sockaddr_in *);
    197 		sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
    198 		sin->sin_family = AF_INET;
    199 		sin->sin_addr.s_addr = INADDR_ANY;
    200 		sin->sin_port = 0;
    201 		error = sobind(so, m, &proc0);
    202 		m_freem(m);
    203 		if (error)
    204 			goto bad;
    205 	}
    206 #ifdef INET6
    207 	if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    208 		m = m_get(M_WAIT, MT_SOOPTS);
    209 		MCLAIM(m, so->so_mowner);
    210 		*mtod(m, int32_t *) = IPV6_PORTRANGE_LOW;
    211 		m->m_len = sizeof(int32_t);
    212 		if ((error = sosetopt(so, IPPROTO_IPV6, IPV6_PORTRANGE, m)))
    213 			goto bad;
    214 		m = m_get(M_WAIT, MT_SONAME);
    215 		MCLAIM(m, so->so_mowner);
    216 		sin6 = mtod(m, struct sockaddr_in6 *);
    217 		sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6);
    218 		sin6->sin6_family = AF_INET6;
    219 		sin6->sin6_addr = in6addr_any;
    220 		sin6->sin6_port = 0;
    221 		error = sobind(so, m, &proc0);
    222 		m_freem(m);
    223 		if (error)
    224 			goto bad;
    225 	}
    226 #endif
    227 
    228 	/*
    229 	 * Protocols that do not require connections may be optionally left
    230 	 * unconnected for servers that reply from a port other than NFS_PORT.
    231 	 */
    232 	if (nmp->nm_flag & NFSMNT_NOCONN) {
    233 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
    234 			error = ENOTCONN;
    235 			goto bad;
    236 		}
    237 	} else {
    238 		error = soconnect(so, nmp->nm_nam);
    239 		if (error)
    240 			goto bad;
    241 
    242 		/*
    243 		 * Wait for the connection to complete. Cribbed from the
    244 		 * connect system call but with the wait timing out so
    245 		 * that interruptible mounts don't hang here for a long time.
    246 		 */
    247 		s = splsoftnet();
    248 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    249 			(void) tsleep((caddr_t)&so->so_timeo, PSOCK,
    250 				"nfscn1", 2 * hz);
    251 			if ((so->so_state & SS_ISCONNECTING) &&
    252 			    so->so_error == 0 && rep &&
    253 			    (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
    254 				so->so_state &= ~SS_ISCONNECTING;
    255 				splx(s);
    256 				goto bad;
    257 			}
    258 		}
    259 		if (so->so_error) {
    260 			error = so->so_error;
    261 			so->so_error = 0;
    262 			splx(s);
    263 			goto bad;
    264 		}
    265 		splx(s);
    266 	}
    267 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
    268 		so->so_rcv.sb_timeo = (5 * hz);
    269 		so->so_snd.sb_timeo = (5 * hz);
    270 	} else {
    271 		so->so_rcv.sb_timeo = 0;
    272 		so->so_snd.sb_timeo = 0;
    273 	}
    274 	if (nmp->nm_sotype == SOCK_DGRAM) {
    275 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    276 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    277 		    NFS_MAXPKTHDR) * 2;
    278 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
    279 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    280 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    281 		    NFS_MAXPKTHDR) * 2;
    282 	} else {
    283 		if (nmp->nm_sotype != SOCK_STREAM)
    284 			panic("nfscon sotype");
    285 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    286 			m = m_get(M_WAIT, MT_SOOPTS);
    287 			MCLAIM(m, so->so_mowner);
    288 			*mtod(m, int32_t *) = 1;
    289 			m->m_len = sizeof(int32_t);
    290 			sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    291 		}
    292 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
    293 			m = m_get(M_WAIT, MT_SOOPTS);
    294 			MCLAIM(m, so->so_mowner);
    295 			*mtod(m, int32_t *) = 1;
    296 			m->m_len = sizeof(int32_t);
    297 			sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    298 		}
    299 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
    300 		    sizeof (u_int32_t)) * 2;
    301 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
    302 		    sizeof (u_int32_t)) * 2;
    303 	}
    304 	error = soreserve(so, sndreserve, rcvreserve);
    305 	if (error)
    306 		goto bad;
    307 	so->so_rcv.sb_flags |= SB_NOINTR;
    308 	so->so_snd.sb_flags |= SB_NOINTR;
    309 
    310 	/* Initialize other non-zero congestion variables */
    311 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
    312 		NFS_TIMEO << 3;
    313 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
    314 		nmp->nm_sdrtt[3] = 0;
    315 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
    316 	nmp->nm_sent = 0;
    317 	nmp->nm_timeouts = 0;
    318 	return (0);
    319 
    320 bad:
    321 	nfs_disconnect(nmp);
    322 	return (error);
    323 }
    324 
    325 /*
    326  * Reconnect routine:
    327  * Called when a connection is broken on a reliable protocol.
    328  * - clean up the old socket
    329  * - nfs_connect() again
    330  * - set R_MUSTRESEND for all outstanding requests on mount point
    331  * If this fails the mount point is DEAD!
    332  * nb: Must be called with the nfs_sndlock() set on the mount point.
    333  */
    334 int
    335 nfs_reconnect(rep)
    336 	struct nfsreq *rep;
    337 {
    338 	struct nfsreq *rp;
    339 	struct nfsmount *nmp = rep->r_nmp;
    340 	int error;
    341 
    342 	nfs_disconnect(nmp);
    343 	while ((error = nfs_connect(nmp, rep)) != 0) {
    344 		if (error == EINTR || error == ERESTART)
    345 			return (EINTR);
    346 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfscn2", 0);
    347 	}
    348 
    349 	/*
    350 	 * Loop through outstanding request list and fix up all requests
    351 	 * on old socket.
    352 	 */
    353 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
    354 		if (rp->r_nmp == nmp && (rp->r_flags & R_MUSTRESEND) == 0)
    355 			rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
    356 	}
    357 	return (0);
    358 }
    359 
    360 /*
    361  * NFS disconnect. Clean up and unlink.
    362  */
    363 void
    364 nfs_disconnect(nmp)
    365 	struct nfsmount *nmp;
    366 {
    367 	struct socket *so;
    368 	int drain = 0;
    369 
    370 	if (nmp->nm_so) {
    371 		so = nmp->nm_so;
    372 		nmp->nm_so = (struct socket *)0;
    373 		soshutdown(so, 2);
    374 		drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
    375 		if (drain) {
    376 			/*
    377 			 * soshutdown() above should wake up the current
    378 			 * listener.
    379 			 * Now wake up those waiting for the receive lock, and
    380 			 * wait for them to go away unhappy, to prevent *nmp
    381 			 * from evaporating while they're sleeping.
    382 			 */
    383 			while (nmp->nm_waiters > 0) {
    384 				wakeup (&nmp->nm_iflag);
    385 				(void) tsleep(&nmp->nm_waiters, PVFS,
    386 				    "nfsdis", 0);
    387 			}
    388 		}
    389 		soclose(so);
    390 	}
    391 #ifdef DIAGNOSTIC
    392 	if (drain && (nmp->nm_waiters > 0))
    393 		panic("nfs_disconnect: waiters left after drain?");
    394 #endif
    395 }
    396 
    397 void
    398 nfs_safedisconnect(nmp)
    399 	struct nfsmount *nmp;
    400 {
    401 	struct nfsreq dummyreq;
    402 
    403 	memset(&dummyreq, 0, sizeof(dummyreq));
    404 	dummyreq.r_nmp = nmp;
    405 	nfs_rcvlock(&dummyreq); /* XXX ignored error return */
    406 	nfs_disconnect(nmp);
    407 	nfs_rcvunlock(nmp);
    408 }
    409 
    410 /*
    411  * This is the nfs send routine. For connection based socket types, it
    412  * must be called with an nfs_sndlock() on the socket.
    413  * "rep == NULL" indicates that it has been called from a server.
    414  * For the client side:
    415  * - return EINTR if the RPC is terminated, 0 otherwise
    416  * - set R_MUSTRESEND if the send fails for any reason
    417  * - do any cleanup required by recoverable socket errors (? ? ?)
    418  * For the server side:
    419  * - return EINTR or ERESTART if interrupted by a signal
    420  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
    421  * - do any cleanup required by recoverable socket errors (? ? ?)
    422  */
    423 int
    424 nfs_send(so, nam, top, rep)
    425 	struct socket *so;
    426 	struct mbuf *nam;
    427 	struct mbuf *top;
    428 	struct nfsreq *rep;
    429 {
    430 	struct mbuf *sendnam;
    431 	int error, soflags, flags;
    432 
    433 	if (rep) {
    434 		if (rep->r_flags & R_SOFTTERM) {
    435 			m_freem(top);
    436 			return (EINTR);
    437 		}
    438 		if ((so = rep->r_nmp->nm_so) == NULL) {
    439 			rep->r_flags |= R_MUSTRESEND;
    440 			m_freem(top);
    441 			return (0);
    442 		}
    443 		rep->r_flags &= ~R_MUSTRESEND;
    444 		soflags = rep->r_nmp->nm_soflags;
    445 	} else
    446 		soflags = so->so_proto->pr_flags;
    447 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
    448 		sendnam = (struct mbuf *)0;
    449 	else
    450 		sendnam = nam;
    451 	if (so->so_type == SOCK_SEQPACKET)
    452 		flags = MSG_EOR;
    453 	else
    454 		flags = 0;
    455 
    456 	error = (*so->so_send)(so, sendnam, (struct uio *)0, top,
    457 		(struct mbuf *)0, flags);
    458 	if (error) {
    459 		if (rep) {
    460 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
    461 				/*
    462 				 * We're too fast for the network/driver,
    463 				 * and UDP isn't flowcontrolled.
    464 				 * We need to resend. This is not fatal,
    465 				 * just try again.
    466 				 *
    467 				 * Could be smarter here by doing some sort
    468 				 * of a backoff, but this is rare.
    469 				 */
    470 				rep->r_flags |= R_MUSTRESEND;
    471 			} else {
    472 				if (error != EPIPE)
    473 					log(LOG_INFO,
    474 					    "nfs send error %d for %s\n",
    475 					    error,
    476 					    rep->r_nmp->nm_mountp->
    477 						    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);
    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_fsidx;
    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, rexmitp)
    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 	int *rexmitp;
    923 {
    924 	struct mbuf *m, *mrep;
    925 	struct nfsreq *rep;
    926 	u_int32_t *tl;
    927 	int i;
    928 	struct nfsmount *nmp;
    929 	struct mbuf *md, *mheadend;
    930 	char nickv[RPCX_NICKVERF];
    931 	time_t reqtime, waituntil;
    932 	caddr_t dpos, cp2;
    933 	int t1, s, error = 0, mrest_len, auth_len, auth_type;
    934 	int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
    935 	int verf_len, verf_type;
    936 	u_int32_t xid;
    937 	char *auth_str, *verf_str;
    938 	NFSKERBKEY_T key;		/* save session key */
    939 	struct ucred acred;
    940 #ifndef NFS_V2_ONLY
    941 	int nqlflag, cachable;
    942 	u_quad_t frev;
    943 #endif
    944 
    945 	if (rexmitp != NULL)
    946 		*rexmitp = 0;
    947 
    948 	KASSERT(cred != NULL);
    949 	nmp = VFSTONFS(np->n_vnode->v_mount);
    950 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
    951 	rep->r_nmp = nmp;
    952 	rep->r_procp = procp;
    953 	rep->r_procnum = procnum;
    954 	i = 0;
    955 	m = mrest;
    956 	while (m) {
    957 		i += m->m_len;
    958 		m = m->m_next;
    959 	}
    960 	mrest_len = i;
    961 
    962 	/*
    963 	 * Get the RPC header with authorization.
    964 	 */
    965 kerbauth:
    966 	verf_str = auth_str = (char *)0;
    967 	if (nmp->nm_flag & NFSMNT_KERB) {
    968 		verf_str = nickv;
    969 		verf_len = sizeof (nickv);
    970 		auth_type = RPCAUTH_KERB4;
    971 		memset((caddr_t)key, 0, sizeof (key));
    972 		if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
    973 			&auth_len, verf_str, verf_len)) {
    974 			error = nfs_getauth(nmp, rep, cred, &auth_str,
    975 				&auth_len, verf_str, &verf_len, key);
    976 			if (error) {
    977 				free((caddr_t)rep, M_NFSREQ);
    978 				m_freem(mrest);
    979 				return (error);
    980 			}
    981 		}
    982 	} else {
    983 		switch (procnum) {
    984 		case NFSPROC_READ:
    985 		case NFSPROC_WRITE:
    986 		case NFSPROC_COMMIT:
    987 			acred.cr_uid = np->n_vattr->va_uid;
    988 			acred.cr_gid = np->n_vattr->va_gid;
    989 			acred.cr_ngroups = 0;
    990 			acred.cr_ref = 2;	/* Just to be safe.. */
    991 			cred = &acred;
    992 			break;
    993 		}
    994 		auth_type = RPCAUTH_UNIX;
    995 		auth_len = (((cred->cr_ngroups > nmp->nm_numgrps) ?
    996 			nmp->nm_numgrps : cred->cr_ngroups) << 2) +
    997 			5 * NFSX_UNSIGNED;
    998 	}
    999 	m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
   1000 	     auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
   1001 	if (auth_str)
   1002 		free(auth_str, M_TEMP);
   1003 
   1004 	/*
   1005 	 * For stream protocols, insert a Sun RPC Record Mark.
   1006 	 */
   1007 	if (nmp->nm_sotype == SOCK_STREAM) {
   1008 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
   1009 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
   1010 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
   1011 	}
   1012 	rep->r_mreq = m;
   1013 	rep->r_xid = xid;
   1014 tryagain:
   1015 	if (nmp->nm_flag & NFSMNT_SOFT)
   1016 		rep->r_retry = nmp->nm_retry;
   1017 	else
   1018 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
   1019 	rep->r_rtt = rep->r_rexmit = 0;
   1020 	if (proct[procnum] > 0)
   1021 		rep->r_flags = R_TIMING;
   1022 	else
   1023 		rep->r_flags = 0;
   1024 	rep->r_mrep = NULL;
   1025 
   1026 	/*
   1027 	 * Do the client side RPC.
   1028 	 */
   1029 	nfsstats.rpcrequests++;
   1030 	/*
   1031 	 * Chain request into list of outstanding requests. Be sure
   1032 	 * to put it LAST so timer finds oldest requests first.
   1033 	 */
   1034 	s = splsoftnet();
   1035 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
   1036 
   1037 	/* Get send time for nqnfs */
   1038 	reqtime = time.tv_sec;
   1039 
   1040 	/*
   1041 	 * If backing off another request or avoiding congestion, don't
   1042 	 * send this one now but let timer do it. If not timing a request,
   1043 	 * do it now.
   1044 	 */
   1045 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
   1046 		(nmp->nm_flag & NFSMNT_DUMBTIMR) ||
   1047 		nmp->nm_sent < nmp->nm_cwnd)) {
   1048 		splx(s);
   1049 		if (nmp->nm_soflags & PR_CONNREQUIRED)
   1050 			error = nfs_sndlock(&nmp->nm_iflag, rep);
   1051 		if (!error) {
   1052 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
   1053 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
   1054 			if (nmp->nm_soflags & PR_CONNREQUIRED)
   1055 				nfs_sndunlock(&nmp->nm_iflag);
   1056 		}
   1057 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
   1058 			nmp->nm_sent += NFS_CWNDSCALE;
   1059 			rep->r_flags |= R_SENT;
   1060 		}
   1061 	} else {
   1062 		splx(s);
   1063 		rep->r_rtt = -1;
   1064 	}
   1065 
   1066 	/*
   1067 	 * Wait for the reply from our send or the timer's.
   1068 	 */
   1069 	if (!error || error == EPIPE)
   1070 		error = nfs_reply(rep);
   1071 
   1072 	/*
   1073 	 * RPC done, unlink the request.
   1074 	 */
   1075 	s = splsoftnet();
   1076 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
   1077 	splx(s);
   1078 
   1079 	/*
   1080 	 * Decrement the outstanding request count.
   1081 	 */
   1082 	if (rep->r_flags & R_SENT) {
   1083 		rep->r_flags &= ~R_SENT;	/* paranoia */
   1084 		nmp->nm_sent -= NFS_CWNDSCALE;
   1085 	}
   1086 
   1087 	if (rexmitp != NULL) {
   1088 		int rexmit;
   1089 
   1090 		if (nmp->nm_sotype != SOCK_DGRAM)
   1091 			rexmit = (rep->r_flags & R_REXMITTED) != 0;
   1092 		else
   1093 			rexmit = rep->r_rexmit;
   1094 		*rexmitp = rexmit;
   1095 	}
   1096 
   1097 	/*
   1098 	 * If there was a successful reply and a tprintf msg.
   1099 	 * tprintf a response.
   1100 	 */
   1101 	if (!error && (rep->r_flags & R_TPRINTFMSG))
   1102 		nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
   1103 		    "is alive again");
   1104 	mrep = rep->r_mrep;
   1105 	md = rep->r_md;
   1106 	dpos = rep->r_dpos;
   1107 	if (error) {
   1108 		m_freem(rep->r_mreq);
   1109 		free((caddr_t)rep, M_NFSREQ);
   1110 		return (error);
   1111 	}
   1112 
   1113 	/*
   1114 	 * break down the rpc header and check if ok
   1115 	 */
   1116 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1117 	if (*tl++ == rpc_msgdenied) {
   1118 		if (*tl == rpc_mismatch)
   1119 			error = EOPNOTSUPP;
   1120 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
   1121 			if (!failed_auth) {
   1122 				failed_auth++;
   1123 				mheadend->m_next = (struct mbuf *)0;
   1124 				m_freem(mrep);
   1125 				m_freem(rep->r_mreq);
   1126 				goto kerbauth;
   1127 			} else
   1128 				error = EAUTH;
   1129 		} else
   1130 			error = EACCES;
   1131 		m_freem(mrep);
   1132 		m_freem(rep->r_mreq);
   1133 		free((caddr_t)rep, M_NFSREQ);
   1134 		return (error);
   1135 	}
   1136 
   1137 	/*
   1138 	 * Grab any Kerberos verifier, otherwise just throw it away.
   1139 	 */
   1140 	verf_type = fxdr_unsigned(int, *tl++);
   1141 	i = fxdr_unsigned(int32_t, *tl);
   1142 	if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
   1143 		error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
   1144 		if (error)
   1145 			goto nfsmout;
   1146 	} else if (i > 0)
   1147 		nfsm_adv(nfsm_rndup(i));
   1148 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1149 	/* 0 == ok */
   1150 	if (*tl == 0) {
   1151 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1152 		if (*tl != 0) {
   1153 			error = fxdr_unsigned(int, *tl);
   1154 			if ((nmp->nm_flag & NFSMNT_NFSV3) &&
   1155 				error == NFSERR_TRYLATER) {
   1156 				m_freem(mrep);
   1157 				error = 0;
   1158 				waituntil = time.tv_sec + trylater_delay;
   1159 				while (time.tv_sec < waituntil)
   1160 					(void) tsleep((caddr_t)&lbolt,
   1161 						PSOCK, "nqnfstry", 0);
   1162 				trylater_delay *= NFS_TRYLATERDELMUL;
   1163 				if (trylater_delay > NFS_TRYLATERDELMAX)
   1164 					trylater_delay = NFS_TRYLATERDELMAX;
   1165 				/*
   1166 				 * RFC1813:
   1167 				 * The client should wait and then try
   1168 				 * the request with a new RPC transaction ID.
   1169 				 */
   1170 				nfs_renewxid(rep);
   1171 				goto tryagain;
   1172 			}
   1173 
   1174 			/*
   1175 			 * If the File Handle was stale, invalidate the
   1176 			 * lookup cache, just in case.
   1177 			 */
   1178 			if (error == ESTALE)
   1179 				cache_purge(NFSTOV(np));
   1180 			if (nmp->nm_flag & NFSMNT_NFSV3) {
   1181 				*mrp = mrep;
   1182 				*mdp = md;
   1183 				*dposp = dpos;
   1184 				error |= NFSERR_RETERR;
   1185 			} else
   1186 				m_freem(mrep);
   1187 			m_freem(rep->r_mreq);
   1188 			free((caddr_t)rep, M_NFSREQ);
   1189 			return (error);
   1190 		}
   1191 
   1192 #ifndef NFS_V2_ONLY
   1193 		/*
   1194 		 * For nqnfs, get any lease in reply
   1195 		 */
   1196 		if (nmp->nm_flag & NFSMNT_NQNFS) {
   1197 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1198 			if (*tl) {
   1199 				nqlflag = fxdr_unsigned(int, *tl);
   1200 				nfsm_dissect(tl, u_int32_t *, 4*NFSX_UNSIGNED);
   1201 				cachable = fxdr_unsigned(int, *tl++);
   1202 				reqtime += fxdr_unsigned(int, *tl++);
   1203 				if (reqtime > time.tv_sec) {
   1204 				    frev = fxdr_hyper(tl);
   1205 				    nqnfs_clientlease(nmp, np, nqlflag,
   1206 					cachable, reqtime, frev);
   1207 				}
   1208 			}
   1209 		}
   1210 #endif
   1211 		*mrp = mrep;
   1212 		*mdp = md;
   1213 		*dposp = dpos;
   1214 		m_freem(rep->r_mreq);
   1215 		FREE((caddr_t)rep, M_NFSREQ);
   1216 		return (0);
   1217 	}
   1218 	m_freem(mrep);
   1219 	error = EPROTONOSUPPORT;
   1220 nfsmout:
   1221 	m_freem(rep->r_mreq);
   1222 	free((caddr_t)rep, M_NFSREQ);
   1223 	return (error);
   1224 }
   1225 #endif /* NFS */
   1226 
   1227 /*
   1228  * Generate the rpc reply header
   1229  * siz arg. is used to decide if adding a cluster is worthwhile
   1230  */
   1231 int
   1232 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
   1233 	int siz;
   1234 	struct nfsrv_descript *nd;
   1235 	struct nfssvc_sock *slp;
   1236 	int err;
   1237 	int cache;
   1238 	u_quad_t *frev;
   1239 	struct mbuf **mrq;
   1240 	struct mbuf **mbp;
   1241 	caddr_t *bposp;
   1242 {
   1243 	u_int32_t *tl;
   1244 	struct mbuf *mreq;
   1245 	caddr_t bpos;
   1246 	struct mbuf *mb;
   1247 
   1248 	mreq = m_gethdr(M_WAIT, MT_DATA);
   1249 	MCLAIM(mreq, &nfs_mowner);
   1250 	mb = mreq;
   1251 	/*
   1252 	 * If this is a big reply, use a cluster else
   1253 	 * try and leave leading space for the lower level headers.
   1254 	 */
   1255 	siz += RPC_REPLYSIZ;
   1256 	if (siz >= max_datalen) {
   1257 		m_clget(mreq, M_WAIT);
   1258 	} else
   1259 		mreq->m_data += max_hdr;
   1260 	tl = mtod(mreq, u_int32_t *);
   1261 	mreq->m_len = 6 * NFSX_UNSIGNED;
   1262 	bpos = ((caddr_t)tl) + mreq->m_len;
   1263 	*tl++ = txdr_unsigned(nd->nd_retxid);
   1264 	*tl++ = rpc_reply;
   1265 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
   1266 		*tl++ = rpc_msgdenied;
   1267 		if (err & NFSERR_AUTHERR) {
   1268 			*tl++ = rpc_autherr;
   1269 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
   1270 			mreq->m_len -= NFSX_UNSIGNED;
   1271 			bpos -= NFSX_UNSIGNED;
   1272 		} else {
   1273 			*tl++ = rpc_mismatch;
   1274 			*tl++ = txdr_unsigned(RPC_VER2);
   1275 			*tl = txdr_unsigned(RPC_VER2);
   1276 		}
   1277 	} else {
   1278 		*tl++ = rpc_msgaccepted;
   1279 
   1280 		/*
   1281 		 * For Kerberos authentication, we must send the nickname
   1282 		 * verifier back, otherwise just RPCAUTH_NULL.
   1283 		 */
   1284 		if (nd->nd_flag & ND_KERBFULL) {
   1285 			struct nfsuid *nuidp;
   1286 			struct timeval ktvin, ktvout;
   1287 
   1288 			LIST_FOREACH(nuidp, NUIDHASH(slp, nd->nd_cr.cr_uid),
   1289 			    nu_hash) {
   1290 				if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
   1291 				    (!nd->nd_nam2 || netaddr_match(
   1292 				    NU_NETFAM(nuidp), &nuidp->nu_haddr,
   1293 				    nd->nd_nam2)))
   1294 					break;
   1295 			}
   1296 			if (nuidp) {
   1297 				ktvin.tv_sec =
   1298 				    txdr_unsigned(nuidp->nu_timestamp.tv_sec
   1299 					- 1);
   1300 				ktvin.tv_usec =
   1301 				    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1302 
   1303 				/*
   1304 				 * Encrypt the timestamp in ecb mode using the
   1305 				 * session key.
   1306 				 */
   1307 #ifdef NFSKERB
   1308 				XXX
   1309 #endif
   1310 
   1311 				*tl++ = rpc_auth_kerb;
   1312 				*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
   1313 				*tl = ktvout.tv_sec;
   1314 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1315 				*tl++ = ktvout.tv_usec;
   1316 				*tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
   1317 			} else {
   1318 				*tl++ = 0;
   1319 				*tl++ = 0;
   1320 			}
   1321 		} else {
   1322 			*tl++ = 0;
   1323 			*tl++ = 0;
   1324 		}
   1325 		switch (err) {
   1326 		case EPROGUNAVAIL:
   1327 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
   1328 			break;
   1329 		case EPROGMISMATCH:
   1330 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
   1331 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1332 			if (nd->nd_flag & ND_NQNFS) {
   1333 				*tl++ = txdr_unsigned(3);
   1334 				*tl = txdr_unsigned(3);
   1335 			} else {
   1336 				*tl++ = txdr_unsigned(2);
   1337 				*tl = txdr_unsigned(3);
   1338 			}
   1339 			break;
   1340 		case EPROCUNAVAIL:
   1341 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
   1342 			break;
   1343 		case EBADRPC:
   1344 			*tl = txdr_unsigned(RPC_GARBAGE);
   1345 			break;
   1346 		default:
   1347 			*tl = 0;
   1348 			if (err != NFSERR_RETVOID) {
   1349 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1350 				if (err)
   1351 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
   1352 				else
   1353 				    *tl = 0;
   1354 			}
   1355 			break;
   1356 		};
   1357 	}
   1358 
   1359 	/*
   1360 	 * For nqnfs, piggyback lease as requested.
   1361 	 */
   1362 	if ((nd->nd_flag & ND_NQNFS) && err == 0) {
   1363 		if (nd->nd_flag & ND_LEASE) {
   1364 			nfsm_build(tl, u_int32_t *, 5 * NFSX_UNSIGNED);
   1365 			*tl++ = txdr_unsigned(nd->nd_flag & ND_LEASE);
   1366 			*tl++ = txdr_unsigned(cache);
   1367 			*tl++ = txdr_unsigned(nd->nd_duration);
   1368 			txdr_hyper(*frev, tl);
   1369 		} else {
   1370 			nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1371 			*tl = 0;
   1372 		}
   1373 	}
   1374 	if (mrq != NULL)
   1375 		*mrq = mreq;
   1376 	*mbp = mb;
   1377 	*bposp = bpos;
   1378 	if (err != 0 && err != NFSERR_RETVOID)
   1379 		nfsstats.srvrpc_errs++;
   1380 	return (0);
   1381 }
   1382 
   1383 /*
   1384  * Nfs timer routine
   1385  * Scan the nfsreq list and retranmit any requests that have timed out
   1386  * To avoid retransmission attempts on STREAM sockets (in the future) make
   1387  * sure to set the r_retry field to 0 (implies nm_retry == 0).
   1388  */
   1389 void
   1390 nfs_timer(arg)
   1391 	void *arg;	/* never used */
   1392 {
   1393 	struct nfsreq *rep;
   1394 	struct mbuf *m;
   1395 	struct socket *so;
   1396 	struct nfsmount *nmp;
   1397 	int timeo;
   1398 	int s, error;
   1399 #ifdef NFSSERVER
   1400 	struct nfssvc_sock *slp;
   1401 	static long lasttime = 0;
   1402 	u_quad_t cur_usec;
   1403 #endif
   1404 
   1405 	s = splsoftnet();
   1406 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
   1407 		nmp = rep->r_nmp;
   1408 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
   1409 			continue;
   1410 		if (nfs_sigintr(nmp, rep, rep->r_procp)) {
   1411 			rep->r_flags |= R_SOFTTERM;
   1412 			continue;
   1413 		}
   1414 		if (rep->r_rtt >= 0) {
   1415 			rep->r_rtt++;
   1416 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
   1417 				timeo = nmp->nm_timeo;
   1418 			else
   1419 				timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
   1420 			if (nmp->nm_timeouts > 0)
   1421 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
   1422 			if (rep->r_rtt <= timeo)
   1423 				continue;
   1424 			if (nmp->nm_timeouts <
   1425 			    (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
   1426 				nmp->nm_timeouts++;
   1427 		}
   1428 		/*
   1429 		 * Check for server not responding
   1430 		 */
   1431 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
   1432 		     rep->r_rexmit > nmp->nm_deadthresh) {
   1433 			nfs_msg(rep->r_procp,
   1434 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
   1435 			    "not responding");
   1436 			rep->r_flags |= R_TPRINTFMSG;
   1437 		}
   1438 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
   1439 			nfsstats.rpctimeouts++;
   1440 			rep->r_flags |= R_SOFTTERM;
   1441 			continue;
   1442 		}
   1443 		if (nmp->nm_sotype != SOCK_DGRAM) {
   1444 			if (++rep->r_rexmit > NFS_MAXREXMIT)
   1445 				rep->r_rexmit = NFS_MAXREXMIT;
   1446 			continue;
   1447 		}
   1448 		if ((so = nmp->nm_so) == NULL)
   1449 			continue;
   1450 
   1451 		/*
   1452 		 * If there is enough space and the window allows..
   1453 		 *	Resend it
   1454 		 * Set r_rtt to -1 in case we fail to send it now.
   1455 		 */
   1456 		rep->r_rtt = -1;
   1457 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
   1458 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
   1459 		    (rep->r_flags & R_SENT) ||
   1460 		    nmp->nm_sent < nmp->nm_cwnd) &&
   1461 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
   1462 		        if (so->so_state & SS_ISCONNECTED)
   1463 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1464 			    (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
   1465 			else
   1466 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1467 			    nmp->nm_nam, (struct mbuf *)0, (struct proc *)0);
   1468 			if (error) {
   1469 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
   1470 #ifdef DEBUG
   1471 					printf("nfs_timer: ignoring error %d\n",
   1472 						error);
   1473 #endif
   1474 					so->so_error = 0;
   1475 				}
   1476 			} else {
   1477 				/*
   1478 				 * Iff first send, start timing
   1479 				 * else turn timing off, backoff timer
   1480 				 * and divide congestion window by 2.
   1481 				 */
   1482 				if (rep->r_flags & R_SENT) {
   1483 					rep->r_flags &= ~R_TIMING;
   1484 					if (++rep->r_rexmit > NFS_MAXREXMIT)
   1485 						rep->r_rexmit = NFS_MAXREXMIT;
   1486 					nmp->nm_cwnd >>= 1;
   1487 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
   1488 						nmp->nm_cwnd = NFS_CWNDSCALE;
   1489 					nfsstats.rpcretries++;
   1490 				} else {
   1491 					rep->r_flags |= R_SENT;
   1492 					nmp->nm_sent += NFS_CWNDSCALE;
   1493 				}
   1494 				rep->r_rtt = 0;
   1495 			}
   1496 		}
   1497 	}
   1498 
   1499 #ifdef NFSSERVER
   1500 	/*
   1501 	 * Call the nqnfs server timer once a second to handle leases.
   1502 	 */
   1503 	if (lasttime != time.tv_sec) {
   1504 		lasttime = time.tv_sec;
   1505 		nqnfs_serverd();
   1506 	}
   1507 
   1508 	/*
   1509 	 * Scan the write gathering queues for writes that need to be
   1510 	 * completed now.
   1511 	 */
   1512 	cur_usec = (u_quad_t)time.tv_sec * 1000000 + (u_quad_t)time.tv_usec;
   1513 	TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
   1514 	    if (LIST_FIRST(&slp->ns_tq) &&
   1515 		LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
   1516 		nfsrv_wakenfsd(slp);
   1517 	}
   1518 #endif /* NFSSERVER */
   1519 	splx(s);
   1520 	callout_schedule(&nfs_timer_ch, nfs_ticks);
   1521 }
   1522 
   1523 /*ARGSUSED*/
   1524 void
   1525 nfs_exit(p, v)
   1526 	struct proc *p;
   1527 	void *v;
   1528 {
   1529 	struct nfsreq *rp;
   1530 	int s = splsoftnet();
   1531 
   1532 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
   1533 		if (rp->r_procp == p)
   1534 			TAILQ_REMOVE(&nfs_reqq, rp, r_chain);
   1535 	}
   1536 	splx(s);
   1537 }
   1538 
   1539 /*
   1540  * Test for a termination condition pending on the process.
   1541  * This is used for NFSMNT_INT mounts.
   1542  */
   1543 int
   1544 nfs_sigintr(nmp, rep, p)
   1545 	struct nfsmount *nmp;
   1546 	struct nfsreq *rep;
   1547 	struct proc *p;
   1548 {
   1549 	sigset_t ss;
   1550 
   1551 	if (rep && (rep->r_flags & R_SOFTTERM))
   1552 		return (EINTR);
   1553 	if (!(nmp->nm_flag & NFSMNT_INT))
   1554 		return (0);
   1555 	if (p) {
   1556 		sigpending1(p, &ss);
   1557 #if 0
   1558 		sigminusset(&p->p_sigctx.ps_sigignore, &ss);
   1559 #endif
   1560 		if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
   1561 		    sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
   1562 		    sigismember(&ss, SIGQUIT))
   1563 			return (EINTR);
   1564 	}
   1565 	return (0);
   1566 }
   1567 
   1568 /*
   1569  * Lock a socket against others.
   1570  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
   1571  * and also to avoid race conditions between the processes with nfs requests
   1572  * in progress when a reconnect is necessary.
   1573  */
   1574 int
   1575 nfs_sndlock(flagp, rep)
   1576 	int *flagp;
   1577 	struct nfsreq *rep;
   1578 {
   1579 	struct proc *p;
   1580 	int slpflag = 0, slptimeo = 0;
   1581 
   1582 	if (rep) {
   1583 		p = rep->r_procp;
   1584 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
   1585 			slpflag = PCATCH;
   1586 	} else
   1587 		p = (struct proc *)0;
   1588 	while (*flagp & NFSMNT_SNDLOCK) {
   1589 		if (rep && nfs_sigintr(rep->r_nmp, rep, p))
   1590 			return (EINTR);
   1591 		*flagp |= NFSMNT_WANTSND;
   1592 		(void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
   1593 			slptimeo);
   1594 		if (slpflag == PCATCH) {
   1595 			slpflag = 0;
   1596 			slptimeo = 2 * hz;
   1597 		}
   1598 	}
   1599 	*flagp |= NFSMNT_SNDLOCK;
   1600 	return (0);
   1601 }
   1602 
   1603 /*
   1604  * Unlock the stream socket for others.
   1605  */
   1606 void
   1607 nfs_sndunlock(flagp)
   1608 	int *flagp;
   1609 {
   1610 
   1611 	if ((*flagp & NFSMNT_SNDLOCK) == 0)
   1612 		panic("nfs sndunlock");
   1613 	*flagp &= ~NFSMNT_SNDLOCK;
   1614 	if (*flagp & NFSMNT_WANTSND) {
   1615 		*flagp &= ~NFSMNT_WANTSND;
   1616 		wakeup((caddr_t)flagp);
   1617 	}
   1618 }
   1619 
   1620 int
   1621 nfs_rcvlock(rep)
   1622 	struct nfsreq *rep;
   1623 {
   1624 	struct nfsmount *nmp = rep->r_nmp;
   1625 	int *flagp = &nmp->nm_iflag;
   1626 	int slpflag, slptimeo = 0;
   1627 	int error = 0;
   1628 
   1629 	if (*flagp & NFSMNT_DISMNT)
   1630 		return EIO;
   1631 
   1632 	if (*flagp & NFSMNT_INT)
   1633 		slpflag = PCATCH;
   1634 	else
   1635 		slpflag = 0;
   1636 	simple_lock(&nmp->nm_slock);
   1637 	while (*flagp & NFSMNT_RCVLOCK) {
   1638 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp)) {
   1639 			error = EINTR;
   1640 			goto quit;
   1641 		}
   1642 		*flagp |= NFSMNT_WANTRCV;
   1643 		nmp->nm_waiters++;
   1644 		(void) ltsleep(flagp, slpflag | (PZERO - 1), "nfsrcvlk",
   1645 			slptimeo, &nmp->nm_slock);
   1646 		nmp->nm_waiters--;
   1647 		if (*flagp & NFSMNT_DISMNT) {
   1648 			wakeup(&nmp->nm_waiters);
   1649 			error = EIO;
   1650 			goto quit;
   1651 		}
   1652 		/* If our reply was received while we were sleeping,
   1653 		 * then just return without taking the lock to avoid a
   1654 		 * situation where a single iod could 'capture' the
   1655 		 * receive lock.
   1656 		 */
   1657 		if (rep->r_mrep != NULL) {
   1658 			error = EALREADY;
   1659 			goto quit;
   1660 		}
   1661 		if (slpflag == PCATCH) {
   1662 			slpflag = 0;
   1663 			slptimeo = 2 * hz;
   1664 		}
   1665 	}
   1666 	*flagp |= NFSMNT_RCVLOCK;
   1667 quit:
   1668 	simple_unlock(&nmp->nm_slock);
   1669 	return error;
   1670 }
   1671 
   1672 /*
   1673  * Unlock the stream socket for others.
   1674  */
   1675 void
   1676 nfs_rcvunlock(nmp)
   1677 	struct nfsmount *nmp;
   1678 {
   1679 	int *flagp = &nmp->nm_iflag;
   1680 
   1681 	simple_lock(&nmp->nm_slock);
   1682 	if ((*flagp & NFSMNT_RCVLOCK) == 0)
   1683 		panic("nfs rcvunlock");
   1684 	*flagp &= ~NFSMNT_RCVLOCK;
   1685 	if (*flagp & NFSMNT_WANTRCV) {
   1686 		*flagp &= ~NFSMNT_WANTRCV;
   1687 		wakeup((caddr_t)flagp);
   1688 	}
   1689 	simple_unlock(&nmp->nm_slock);
   1690 }
   1691 
   1692 /*
   1693  * Parse an RPC request
   1694  * - verify it
   1695  * - fill in the cred struct.
   1696  */
   1697 int
   1698 nfs_getreq(nd, nfsd, has_header)
   1699 	struct nfsrv_descript *nd;
   1700 	struct nfsd *nfsd;
   1701 	int has_header;
   1702 {
   1703 	int len, i;
   1704 	u_int32_t *tl;
   1705 	int32_t t1;
   1706 	struct uio uio;
   1707 	struct iovec iov;
   1708 	caddr_t dpos, cp2, cp;
   1709 	u_int32_t nfsvers, auth_type;
   1710 	uid_t nickuid;
   1711 	int error = 0, nqnfs = 0, ticklen;
   1712 	struct mbuf *mrep, *md;
   1713 	struct nfsuid *nuidp;
   1714 	struct timeval tvin, tvout;
   1715 
   1716 	mrep = nd->nd_mrep;
   1717 	md = nd->nd_md;
   1718 	dpos = nd->nd_dpos;
   1719 	if (has_header) {
   1720 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
   1721 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
   1722 		if (*tl++ != rpc_call) {
   1723 			m_freem(mrep);
   1724 			return (EBADRPC);
   1725 		}
   1726 	} else
   1727 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
   1728 	nd->nd_repstat = 0;
   1729 	nd->nd_flag = 0;
   1730 	if (*tl++ != rpc_vers) {
   1731 		nd->nd_repstat = ERPCMISMATCH;
   1732 		nd->nd_procnum = NFSPROC_NOOP;
   1733 		return (0);
   1734 	}
   1735 	if (*tl != nfs_prog) {
   1736 		if (*tl == nqnfs_prog)
   1737 			nqnfs++;
   1738 		else {
   1739 			nd->nd_repstat = EPROGUNAVAIL;
   1740 			nd->nd_procnum = NFSPROC_NOOP;
   1741 			return (0);
   1742 		}
   1743 	}
   1744 	tl++;
   1745 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
   1746 	if (((nfsvers < NFS_VER2 || nfsvers > NFS_VER3) && !nqnfs) ||
   1747 		(nfsvers != NQNFS_VER3 && nqnfs)) {
   1748 		nd->nd_repstat = EPROGMISMATCH;
   1749 		nd->nd_procnum = NFSPROC_NOOP;
   1750 		return (0);
   1751 	}
   1752 	if (nqnfs)
   1753 		nd->nd_flag = (ND_NFSV3 | ND_NQNFS);
   1754 	else if (nfsvers == NFS_VER3)
   1755 		nd->nd_flag = ND_NFSV3;
   1756 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
   1757 	if (nd->nd_procnum == NFSPROC_NULL)
   1758 		return (0);
   1759 	if (nd->nd_procnum >= NFS_NPROCS ||
   1760 		(!nqnfs && nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
   1761 		(!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
   1762 		nd->nd_repstat = EPROCUNAVAIL;
   1763 		nd->nd_procnum = NFSPROC_NOOP;
   1764 		return (0);
   1765 	}
   1766 	if ((nd->nd_flag & ND_NFSV3) == 0)
   1767 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
   1768 	auth_type = *tl++;
   1769 	len = fxdr_unsigned(int, *tl++);
   1770 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1771 		m_freem(mrep);
   1772 		return (EBADRPC);
   1773 	}
   1774 
   1775 	nd->nd_flag &= ~ND_KERBAUTH;
   1776 	/*
   1777 	 * Handle auth_unix or auth_kerb.
   1778 	 */
   1779 	if (auth_type == rpc_auth_unix) {
   1780 		len = fxdr_unsigned(int, *++tl);
   1781 		if (len < 0 || len > NFS_MAXNAMLEN) {
   1782 			m_freem(mrep);
   1783 			return (EBADRPC);
   1784 		}
   1785 		nfsm_adv(nfsm_rndup(len));
   1786 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1787 		memset((caddr_t)&nd->nd_cr, 0, sizeof (struct ucred));
   1788 		nd->nd_cr.cr_ref = 1;
   1789 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
   1790 		nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
   1791 		len = fxdr_unsigned(int, *tl);
   1792 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
   1793 			m_freem(mrep);
   1794 			return (EBADRPC);
   1795 		}
   1796 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
   1797 		for (i = 0; i < len; i++)
   1798 		    if (i < NGROUPS)
   1799 			nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
   1800 		    else
   1801 			tl++;
   1802 		nd->nd_cr.cr_ngroups = (len > NGROUPS) ? NGROUPS : len;
   1803 		if (nd->nd_cr.cr_ngroups > 1)
   1804 		    nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
   1805 		len = fxdr_unsigned(int, *++tl);
   1806 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1807 			m_freem(mrep);
   1808 			return (EBADRPC);
   1809 		}
   1810 		if (len > 0)
   1811 			nfsm_adv(nfsm_rndup(len));
   1812 	} else if (auth_type == rpc_auth_kerb) {
   1813 		switch (fxdr_unsigned(int, *tl++)) {
   1814 		case RPCAKN_FULLNAME:
   1815 			ticklen = fxdr_unsigned(int, *tl);
   1816 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
   1817 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
   1818 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
   1819 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
   1820 				m_freem(mrep);
   1821 				return (EBADRPC);
   1822 			}
   1823 			uio.uio_offset = 0;
   1824 			uio.uio_iov = &iov;
   1825 			uio.uio_iovcnt = 1;
   1826 			uio.uio_segflg = UIO_SYSSPACE;
   1827 			iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
   1828 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
   1829 			nfsm_mtouio(&uio, uio.uio_resid);
   1830 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1831 			if (*tl++ != rpc_auth_kerb ||
   1832 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
   1833 				printf("Bad kerb verifier\n");
   1834 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1835 				nd->nd_procnum = NFSPROC_NOOP;
   1836 				return (0);
   1837 			}
   1838 			nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
   1839 			tl = (u_int32_t *)cp;
   1840 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
   1841 				printf("Not fullname kerb verifier\n");
   1842 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1843 				nd->nd_procnum = NFSPROC_NOOP;
   1844 				return (0);
   1845 			}
   1846 			cp += NFSX_UNSIGNED;
   1847 			memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
   1848 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
   1849 			nd->nd_flag |= ND_KERBFULL;
   1850 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
   1851 			break;
   1852 		case RPCAKN_NICKNAME:
   1853 			if (len != 2 * NFSX_UNSIGNED) {
   1854 				printf("Kerb nickname short\n");
   1855 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
   1856 				nd->nd_procnum = NFSPROC_NOOP;
   1857 				return (0);
   1858 			}
   1859 			nickuid = fxdr_unsigned(uid_t, *tl);
   1860 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1861 			if (*tl++ != rpc_auth_kerb ||
   1862 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
   1863 				printf("Kerb nick verifier bad\n");
   1864 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1865 				nd->nd_procnum = NFSPROC_NOOP;
   1866 				return (0);
   1867 			}
   1868 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1869 			tvin.tv_sec = *tl++;
   1870 			tvin.tv_usec = *tl;
   1871 
   1872 			LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
   1873 			    nu_hash) {
   1874 				if (nuidp->nu_cr.cr_uid == nickuid &&
   1875 				    (!nd->nd_nam2 ||
   1876 				     netaddr_match(NU_NETFAM(nuidp),
   1877 				      &nuidp->nu_haddr, nd->nd_nam2)))
   1878 					break;
   1879 			}
   1880 			if (!nuidp) {
   1881 				nd->nd_repstat =
   1882 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
   1883 				nd->nd_procnum = NFSPROC_NOOP;
   1884 				return (0);
   1885 			}
   1886 
   1887 			/*
   1888 			 * Now, decrypt the timestamp using the session key
   1889 			 * and validate it.
   1890 			 */
   1891 #ifdef NFSKERB
   1892 			XXX
   1893 #endif
   1894 
   1895 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
   1896 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
   1897 			if (nuidp->nu_expire < time.tv_sec ||
   1898 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
   1899 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
   1900 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
   1901 				nuidp->nu_expire = 0;
   1902 				nd->nd_repstat =
   1903 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
   1904 				nd->nd_procnum = NFSPROC_NOOP;
   1905 				return (0);
   1906 			}
   1907 			nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
   1908 			nd->nd_flag |= ND_KERBNICK;
   1909 		};
   1910 	} else {
   1911 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
   1912 		nd->nd_procnum = NFSPROC_NOOP;
   1913 		return (0);
   1914 	}
   1915 
   1916 	/*
   1917 	 * For nqnfs, get piggybacked lease request.
   1918 	 */
   1919 	if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
   1920 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1921 		nd->nd_flag |= fxdr_unsigned(int, *tl);
   1922 		if (nd->nd_flag & ND_LEASE) {
   1923 			nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1924 			nd->nd_duration = fxdr_unsigned(u_int32_t, *tl);
   1925 		} else
   1926 			nd->nd_duration = NQ_MINLEASE;
   1927 	} else
   1928 		nd->nd_duration = NQ_MINLEASE;
   1929 	nd->nd_md = md;
   1930 	nd->nd_dpos = dpos;
   1931 	return (0);
   1932 nfsmout:
   1933 	return (error);
   1934 }
   1935 
   1936 int
   1937 nfs_msg(p, server, msg)
   1938 	struct proc *p;
   1939 	char *server, *msg;
   1940 {
   1941 	tpr_t tpr;
   1942 
   1943 	if (p)
   1944 		tpr = tprintf_open(p);
   1945 	else
   1946 		tpr = NULL;
   1947 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
   1948 	tprintf_close(tpr);
   1949 	return (0);
   1950 }
   1951 
   1952 #ifdef NFSSERVER
   1953 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
   1954 				    struct nfssvc_sock *, struct proc *,
   1955 				    struct mbuf **)) = {
   1956 	nfsrv_null,
   1957 	nfsrv_getattr,
   1958 	nfsrv_setattr,
   1959 	nfsrv_lookup,
   1960 	nfsrv3_access,
   1961 	nfsrv_readlink,
   1962 	nfsrv_read,
   1963 	nfsrv_write,
   1964 	nfsrv_create,
   1965 	nfsrv_mkdir,
   1966 	nfsrv_symlink,
   1967 	nfsrv_mknod,
   1968 	nfsrv_remove,
   1969 	nfsrv_rmdir,
   1970 	nfsrv_rename,
   1971 	nfsrv_link,
   1972 	nfsrv_readdir,
   1973 	nfsrv_readdirplus,
   1974 	nfsrv_statfs,
   1975 	nfsrv_fsinfo,
   1976 	nfsrv_pathconf,
   1977 	nfsrv_commit,
   1978 	nqnfsrv_getlease,
   1979 	nqnfsrv_vacated,
   1980 	nfsrv_noop,
   1981 	nfsrv_noop
   1982 };
   1983 
   1984 /*
   1985  * Socket upcall routine for the nfsd sockets.
   1986  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
   1987  * Essentially do as much as possible non-blocking, else punt and it will
   1988  * be called with M_WAIT from an nfsd.
   1989  */
   1990 void
   1991 nfsrv_rcv(so, arg, waitflag)
   1992 	struct socket *so;
   1993 	caddr_t arg;
   1994 	int waitflag;
   1995 {
   1996 	struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
   1997 	struct mbuf *m;
   1998 	struct mbuf *mp, *nam;
   1999 	struct uio auio;
   2000 	int flags, error;
   2001 
   2002 	if ((slp->ns_flag & SLP_VALID) == 0)
   2003 		return;
   2004 #ifdef notdef
   2005 	/*
   2006 	 * Define this to test for nfsds handling this under heavy load.
   2007 	 */
   2008 	if (waitflag == M_DONTWAIT) {
   2009 		slp->ns_flag |= SLP_NEEDQ; goto dorecs;
   2010 	}
   2011 #endif
   2012 	auio.uio_procp = NULL;
   2013 	if (so->so_type == SOCK_STREAM) {
   2014 		/*
   2015 		 * If there are already records on the queue, defer soreceive()
   2016 		 * to an nfsd so that there is feedback to the TCP layer that
   2017 		 * the nfs servers are heavily loaded.
   2018 		 */
   2019 		if (slp->ns_rec && waitflag == M_DONTWAIT) {
   2020 			slp->ns_flag |= SLP_NEEDQ;
   2021 			goto dorecs;
   2022 		}
   2023 
   2024 		/*
   2025 		 * Do soreceive().
   2026 		 */
   2027 		auio.uio_resid = 1000000000;
   2028 		flags = MSG_DONTWAIT;
   2029 		error = (*so->so_receive)(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
   2030 		if (error || mp == (struct mbuf *)0) {
   2031 			if (error == EWOULDBLOCK)
   2032 				slp->ns_flag |= SLP_NEEDQ;
   2033 			else
   2034 				slp->ns_flag |= SLP_DISCONN;
   2035 			goto dorecs;
   2036 		}
   2037 		m = mp;
   2038 		if (slp->ns_rawend) {
   2039 			slp->ns_rawend->m_next = m;
   2040 			slp->ns_cc += 1000000000 - auio.uio_resid;
   2041 		} else {
   2042 			slp->ns_raw = m;
   2043 			slp->ns_cc = 1000000000 - auio.uio_resid;
   2044 		}
   2045 		while (m->m_next)
   2046 			m = m->m_next;
   2047 		slp->ns_rawend = m;
   2048 
   2049 		/*
   2050 		 * Now try and parse record(s) out of the raw stream data.
   2051 		 */
   2052 		error = nfsrv_getstream(slp, waitflag);
   2053 		if (error) {
   2054 			if (error == EPERM)
   2055 				slp->ns_flag |= SLP_DISCONN;
   2056 			else
   2057 				slp->ns_flag |= SLP_NEEDQ;
   2058 		}
   2059 	} else {
   2060 		do {
   2061 			auio.uio_resid = 1000000000;
   2062 			flags = MSG_DONTWAIT;
   2063 			error = (*so->so_receive)(so, &nam, &auio, &mp,
   2064 						(struct mbuf **)0, &flags);
   2065 			if (mp) {
   2066 				if (nam) {
   2067 					m = nam;
   2068 					m->m_next = mp;
   2069 				} else
   2070 					m = mp;
   2071 				if (slp->ns_recend)
   2072 					slp->ns_recend->m_nextpkt = m;
   2073 				else
   2074 					slp->ns_rec = m;
   2075 				slp->ns_recend = m;
   2076 				m->m_nextpkt = (struct mbuf *)0;
   2077 			}
   2078 			if (error) {
   2079 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
   2080 					&& error != EWOULDBLOCK) {
   2081 					slp->ns_flag |= SLP_DISCONN;
   2082 					goto dorecs;
   2083 				}
   2084 			}
   2085 		} while (mp);
   2086 	}
   2087 
   2088 	/*
   2089 	 * Now try and process the request records, non-blocking.
   2090 	 */
   2091 dorecs:
   2092 	if (waitflag == M_DONTWAIT &&
   2093 		(slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
   2094 		nfsrv_wakenfsd(slp);
   2095 }
   2096 
   2097 /*
   2098  * Try and extract an RPC request from the mbuf data list received on a
   2099  * stream socket. The "waitflag" argument indicates whether or not it
   2100  * can sleep.
   2101  */
   2102 int
   2103 nfsrv_getstream(slp, waitflag)
   2104 	struct nfssvc_sock *slp;
   2105 	int waitflag;
   2106 {
   2107 	struct mbuf *m, **mpp;
   2108 	struct mbuf *recm;
   2109 	u_int32_t recmark;
   2110 
   2111 	if (slp->ns_flag & SLP_GETSTREAM)
   2112 		panic("nfs getstream");
   2113 	slp->ns_flag |= SLP_GETSTREAM;
   2114 	for (;;) {
   2115 		if (slp->ns_reclen == 0) {
   2116 			if (slp->ns_cc < NFSX_UNSIGNED) {
   2117 				slp->ns_flag &= ~SLP_GETSTREAM;
   2118 				return (0);
   2119 			}
   2120 			m = slp->ns_raw;
   2121 			m_copydata(m, 0, NFSX_UNSIGNED, (caddr_t)&recmark);
   2122 			m_adj(m, NFSX_UNSIGNED);
   2123 			slp->ns_cc -= NFSX_UNSIGNED;
   2124 			recmark = ntohl(recmark);
   2125 			slp->ns_reclen = recmark & ~0x80000000;
   2126 			if (recmark & 0x80000000)
   2127 				slp->ns_flag |= SLP_LASTFRAG;
   2128 			else
   2129 				slp->ns_flag &= ~SLP_LASTFRAG;
   2130 			if (slp->ns_reclen > NFS_MAXPACKET) {
   2131 				slp->ns_flag &= ~SLP_GETSTREAM;
   2132 				return (EPERM);
   2133 			}
   2134 		}
   2135 
   2136 		/*
   2137 		 * Now get the record part.
   2138 		 *
   2139 		 * Note that slp->ns_reclen may be 0.  Linux sometimes
   2140 		 * generates 0-length records.
   2141 		 */
   2142 		if (slp->ns_cc == slp->ns_reclen) {
   2143 			recm = slp->ns_raw;
   2144 			slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
   2145 			slp->ns_cc = slp->ns_reclen = 0;
   2146 		} else if (slp->ns_cc > slp->ns_reclen) {
   2147 			recm = slp->ns_raw;
   2148 			m = m_split(recm, slp->ns_reclen, waitflag);
   2149 			if (m == NULL) {
   2150 				slp->ns_flag &= ~SLP_GETSTREAM;
   2151 				return (EWOULDBLOCK);
   2152 			}
   2153 			m_claim(recm, &nfs_mowner);
   2154 			slp->ns_raw = m;
   2155 			if (m->m_next == NULL)
   2156 				slp->ns_rawend = m;
   2157 			slp->ns_cc -= slp->ns_reclen;
   2158 			slp->ns_reclen = 0;
   2159 		} else {
   2160 			slp->ns_flag &= ~SLP_GETSTREAM;
   2161 			return (0);
   2162 		}
   2163 
   2164 		/*
   2165 		 * Accumulate the fragments into a record.
   2166 		 */
   2167 		mpp = &slp->ns_frag;
   2168 		while (*mpp)
   2169 			mpp = &((*mpp)->m_next);
   2170 		*mpp = recm;
   2171 		if (slp->ns_flag & SLP_LASTFRAG) {
   2172 			if (slp->ns_recend)
   2173 				slp->ns_recend->m_nextpkt = slp->ns_frag;
   2174 			else
   2175 				slp->ns_rec = slp->ns_frag;
   2176 			slp->ns_recend = slp->ns_frag;
   2177 			slp->ns_frag = (struct mbuf *)0;
   2178 		}
   2179 	}
   2180 }
   2181 
   2182 /*
   2183  * Parse an RPC header.
   2184  */
   2185 int
   2186 nfsrv_dorec(slp, nfsd, ndp)
   2187 	struct nfssvc_sock *slp;
   2188 	struct nfsd *nfsd;
   2189 	struct nfsrv_descript **ndp;
   2190 {
   2191 	struct mbuf *m, *nam;
   2192 	struct nfsrv_descript *nd;
   2193 	int error;
   2194 
   2195 	*ndp = NULL;
   2196 	if ((slp->ns_flag & SLP_VALID) == 0 ||
   2197 	    (m = slp->ns_rec) == (struct mbuf *)0)
   2198 		return (ENOBUFS);
   2199 	slp->ns_rec = m->m_nextpkt;
   2200 	if (slp->ns_rec)
   2201 		m->m_nextpkt = (struct mbuf *)0;
   2202 	else
   2203 		slp->ns_recend = (struct mbuf *)0;
   2204 	if (m->m_type == MT_SONAME) {
   2205 		nam = m;
   2206 		m = m->m_next;
   2207 		nam->m_next = NULL;
   2208 	} else
   2209 		nam = NULL;
   2210 	nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
   2211 	nd->nd_md = nd->nd_mrep = m;
   2212 	nd->nd_nam2 = nam;
   2213 	nd->nd_dpos = mtod(m, caddr_t);
   2214 	error = nfs_getreq(nd, nfsd, TRUE);
   2215 	if (error) {
   2216 		m_freem(nam);
   2217 		pool_put(&nfs_srvdesc_pool, nd);
   2218 		return (error);
   2219 	}
   2220 	*ndp = nd;
   2221 	nfsd->nfsd_nd = nd;
   2222 	return (0);
   2223 }
   2224 
   2225 
   2226 /*
   2227  * Search for a sleeping nfsd and wake it up.
   2228  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
   2229  * running nfsds will go look for the work in the nfssvc_sock list.
   2230  */
   2231 void
   2232 nfsrv_wakenfsd(slp)
   2233 	struct nfssvc_sock *slp;
   2234 {
   2235 	struct nfsd *nd;
   2236 
   2237 	if ((slp->ns_flag & SLP_VALID) == 0)
   2238 		return;
   2239 	simple_lock(&nfsd_slock);
   2240 	if (slp->ns_flag & SLP_DOREC) {
   2241 		simple_unlock(&nfsd_slock);
   2242 		return;
   2243 	}
   2244 	nd = SLIST_FIRST(&nfsd_idle_head);
   2245 	if (nd) {
   2246 		SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
   2247 		simple_unlock(&nfsd_slock);
   2248 
   2249 		KASSERT(nd->nfsd_flag & NFSD_WAITING);
   2250 		nd->nfsd_flag &= ~NFSD_WAITING;
   2251 		if (nd->nfsd_slp)
   2252 			panic("nfsd wakeup");
   2253 		slp->ns_sref++;
   2254 		nd->nfsd_slp = slp;
   2255 		wakeup(nd);
   2256 		return;
   2257 	}
   2258 	slp->ns_flag |= SLP_DOREC;
   2259 	nfsd_head_flag |= NFSD_CHECKSLP;
   2260 	TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
   2261 	simple_unlock(&nfsd_slock);
   2262 }
   2263 #endif /* NFSSERVER */
   2264