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