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