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