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