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