xref: /openbsd-src/sys/nfs/nfs_socket.c (revision f933361f20df4def12f9bc8391f68d6bfad3bb75)
1 /*	$OpenBSD: nfs_socket.c,v 1.116 2017/05/17 08:59:05 mpi Exp $	*/
2 /*	$NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1991, 1993, 1995
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_socket.c	8.5 (Berkeley) 3/30/95
36  */
37 
38 /*
39  * Socket operations for use by nfs
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/mount.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/vnode.h>
49 #include <sys/domain.h>
50 #include <sys/protosw.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/syslog.h>
55 #include <sys/tprintf.h>
56 #include <sys/namei.h>
57 #include <sys/pool.h>
58 #include <sys/queue.h>
59 
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/nfs.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/nfsm_subs.h>
68 #include <nfs/nfsmount.h>
69 #include <nfs/nfs_var.h>
70 
71 /* External data, mostly RPC constants in XDR form. */
72 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
73 	rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr;
74 extern u_int32_t nfs_prog;
75 extern struct nfsstats nfsstats;
76 extern int nfsv3_procid[NFS_NPROCS];
77 extern int nfs_ticks;
78 
79 extern struct pool nfsrv_descript_pl;
80 
81 /*
82  * There is a congestion window for outstanding rpcs maintained per mount
83  * point. The cwnd size is adjusted in roughly the way that:
84  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
85  * SIGCOMM '88". ACM, August 1988.
86  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
87  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
88  * of rpcs is in progress.
89  * (The sent count and cwnd are scaled for integer arith.)
90  * Variants of "slow start" were tried and were found to be too much of a
91  * performance hit (ave. rtt 3 times larger),
92  * I suspect due to the large rtt that nfs rpcs have.
93  */
94 #define	NFS_CWNDSCALE	256
95 #define	NFS_MAXCWND	(NFS_CWNDSCALE * 32)
96 int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256 };
97 
98 /* RTT estimator */
99 enum nfs_rto_timers nfs_ptimers[NFS_NPROCS] = {
100 	NFS_DEFAULT_TIMER,	/* NULL */
101 	NFS_GETATTR_TIMER,	/* GETATTR */
102 	NFS_DEFAULT_TIMER,	/* SETATTR */
103 	NFS_LOOKUP_TIMER,	/* LOOKUP */
104 	NFS_GETATTR_TIMER,	/* ACCESS */
105 	NFS_READ_TIMER,		/* READLINK */
106 	NFS_READ_TIMER,		/* READ */
107 	NFS_WRITE_TIMER,	/* WRITE */
108 	NFS_DEFAULT_TIMER,	/* CREATE */
109 	NFS_DEFAULT_TIMER,	/* MKDIR */
110 	NFS_DEFAULT_TIMER,	/* SYMLINK */
111 	NFS_DEFAULT_TIMER,	/* MKNOD */
112 	NFS_DEFAULT_TIMER,	/* REMOVE */
113 	NFS_DEFAULT_TIMER,	/* RMDIR */
114 	NFS_DEFAULT_TIMER,	/* RENAME */
115 	NFS_DEFAULT_TIMER,	/* LINK */
116 	NFS_READ_TIMER,		/* READDIR */
117 	NFS_READ_TIMER,		/* READDIRPLUS */
118 	NFS_DEFAULT_TIMER,	/* FSSTAT */
119 	NFS_DEFAULT_TIMER,	/* FSINFO */
120 	NFS_DEFAULT_TIMER,	/* PATHCONF */
121 	NFS_DEFAULT_TIMER,	/* COMMIT */
122 	NFS_DEFAULT_TIMER,	/* NOOP */
123 };
124 
125 void nfs_init_rtt(struct nfsmount *);
126 void nfs_update_rtt(struct nfsreq *);
127 int  nfs_estimate_rto(struct nfsmount *, u_int32_t procnum);
128 
129 void nfs_realign(struct mbuf **, int);
130 void nfs_realign_fixup(struct mbuf *, struct mbuf *, unsigned int *);
131 
132 int nfs_rcvlock(struct nfsreq *);
133 int nfs_receive(struct nfsreq *, struct mbuf **, struct mbuf **);
134 int nfs_reconnect(struct nfsreq *);
135 int nfs_reply(struct nfsreq *);
136 void nfs_msg(struct nfsreq *, char *);
137 void nfs_rcvunlock(int *);
138 
139 int nfsrv_getstream(struct nfssvc_sock *, int);
140 
141 unsigned int nfs_realign_test = 0;
142 unsigned int nfs_realign_count = 0;
143 
144 /* Initialize the RTT estimator state for a new mount point. */
145 void
146 nfs_init_rtt(struct nfsmount *nmp)
147 {
148 	int i;
149 
150 	for (i = 0; i < NFS_MAX_TIMER; i++)
151 		nmp->nm_srtt[i] = NFS_INITRTT;
152 	for (i = 0; i < NFS_MAX_TIMER; i++)
153 		nmp->nm_sdrtt[i] = 0;
154 }
155 
156 /*
157  * Update a mount point's RTT estimator state using data from the
158  * passed-in request.
159  *
160  * Use a gain of 0.125 on the mean and a gain of 0.25 on the deviation.
161  *
162  * NB: Since the timer resolution of NFS_HZ is so course, it can often
163  * result in r_rtt == 0. Since r_rtt == N means that the actual RTT is
164  * between N + dt and N + 2 - dt ticks, add 1 before calculating the
165  * update values.
166  */
167 void
168 nfs_update_rtt(struct nfsreq *rep)
169 {
170 	int t1 = rep->r_rtt + 1;
171 	int index = nfs_ptimers[rep->r_procnum] - 1;
172 	int *srtt = &rep->r_nmp->nm_srtt[index];
173 	int *sdrtt = &rep->r_nmp->nm_sdrtt[index];
174 
175 	t1 -= *srtt >> 3;
176 	*srtt += t1;
177 	if (t1 < 0)
178 		t1 = -t1;
179 	t1 -= *sdrtt >> 2;
180 	*sdrtt += t1;
181 }
182 
183 /*
184  * Estimate RTO for an NFS RPC sent via an unreliable datagram.
185  *
186  * Use the mean and mean deviation of RTT for the appropriate type
187  * of RPC for the frequent RPCs and a default for the others.
188  * The justification for doing "other" this way is that these RPCs
189  * happen so infrequently that timer est. would probably be stale.
190  * Also, since many of these RPCs are non-idempotent, a conservative
191  * timeout is desired.
192  *
193  * getattr, lookup - A+2D
194  * read, write     - A+4D
195  * other           - nm_timeo
196  */
197 int
198 nfs_estimate_rto(struct nfsmount *nmp, u_int32_t procnum)
199 {
200 	enum nfs_rto_timers timer = nfs_ptimers[procnum];
201 	int index = timer - 1;
202 	int rto;
203 
204 	switch (timer) {
205 	case NFS_GETATTR_TIMER:
206 	case NFS_LOOKUP_TIMER:
207 		rto = ((nmp->nm_srtt[index] + 3) >> 2) +
208 				((nmp->nm_sdrtt[index] + 1) >> 1);
209 		break;
210 	case NFS_READ_TIMER:
211 	case NFS_WRITE_TIMER:
212 		rto = ((nmp->nm_srtt[index] + 7) >> 3) +
213 				(nmp->nm_sdrtt[index] + 1);
214 		break;
215 	default:
216 		rto = nmp->nm_timeo;
217 		return (rto);
218 	}
219 
220 	if (rto < NFS_MINRTO)
221 		rto = NFS_MINRTO;
222 	else if (rto > NFS_MAXRTO)
223 		rto = NFS_MAXRTO;
224 
225 	return (rto);
226 }
227 
228 
229 
230 /*
231  * Initialize sockets and congestion for a new NFS connection.
232  * We do not free the sockaddr if error.
233  */
234 int
235 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
236 {
237 	struct socket *so;
238 	int s, error, rcvreserve, sndreserve;
239 	struct sockaddr *saddr;
240 	struct sockaddr_in *sin;
241 	struct mbuf *m;
242 
243 	nmp->nm_so = NULL;
244 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
245 	error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
246 		nmp->nm_soproto);
247 	if (error)
248 		goto bad;
249 	so = nmp->nm_so;
250 	nmp->nm_soflags = so->so_proto->pr_flags;
251 
252 	/*
253 	 * Some servers require that the client port be a reserved port number.
254 	 * We always allocate a reserved port, as this prevents filehandle
255 	 * disclosure through UDP port capture.
256 	 */
257 	if (saddr->sa_family == AF_INET) {
258 		struct mbuf *mopt;
259 		int *ip;
260 
261 		MGET(mopt, M_WAIT, MT_SOOPTS);
262 		mopt->m_len = sizeof(int);
263 		ip = mtod(mopt, int *);
264 		*ip = IP_PORTRANGE_LOW;
265 		error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
266 		if (error)
267 			goto bad;
268 
269 		MGET(m, M_WAIT, MT_SONAME);
270 		sin = mtod(m, struct sockaddr_in *);
271 		memset(sin, 0, sizeof(*sin));
272 		sin->sin_len = m->m_len = sizeof(struct sockaddr_in);
273 		sin->sin_family = AF_INET;
274 		sin->sin_addr.s_addr = INADDR_ANY;
275 		sin->sin_port = htons(0);
276 		error = sobind(so, m, &proc0);
277 		m_freem(m);
278 		if (error)
279 			goto bad;
280 
281 		MGET(mopt, M_WAIT, MT_SOOPTS);
282 		mopt->m_len = sizeof(int);
283 		ip = mtod(mopt, int *);
284 		*ip = IP_PORTRANGE_DEFAULT;
285 		error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, mopt);
286 		if (error)
287 			goto bad;
288 	}
289 
290 	/*
291 	 * Protocols that do not require connections may be optionally left
292 	 * unconnected for servers that reply from a port other than NFS_PORT.
293 	 */
294 	if (nmp->nm_flag & NFSMNT_NOCONN) {
295 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
296 			error = ENOTCONN;
297 			goto bad;
298 		}
299 	} else {
300 		error = soconnect(so, nmp->nm_nam);
301 		if (error)
302 			goto bad;
303 
304 		/*
305 		 * Wait for the connection to complete. Cribbed from the
306 		 * connect system call but with the wait timing out so
307 		 * that interruptible mounts don't hang here for a long time.
308 		 */
309 		s = solock(so);
310 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
311 			sosleep(so, &so->so_timeo, PSOCK, "nfscon", 2 * hz);
312 			if ((so->so_state & SS_ISCONNECTING) &&
313 			    so->so_error == 0 && rep &&
314 			    (error = nfs_sigintr(nmp, rep, rep->r_procp)) != 0){
315 				so->so_state &= ~SS_ISCONNECTING;
316 				sounlock(s);
317 				goto bad;
318 			}
319 		}
320 		if (so->so_error) {
321 			error = so->so_error;
322 			so->so_error = 0;
323 			sounlock(s);
324 			goto bad;
325 		}
326 		sounlock(s);
327 	}
328 	/*
329 	 * Always set receive timeout to detect server crash and reconnect.
330 	 * Otherwise, we can get stuck in soreceive forever.
331 	 */
332 	so->so_rcv.sb_timeo = (5 * hz);
333 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT))
334 		so->so_snd.sb_timeo = (5 * hz);
335 	else
336 		so->so_snd.sb_timeo = 0;
337 	if (nmp->nm_sotype == SOCK_DGRAM) {
338 		sndreserve = nmp->nm_wsize + NFS_MAXPKTHDR;
339 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
340 		    NFS_MAXPKTHDR) * 2;
341 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
342 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
343 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
344 		    NFS_MAXPKTHDR) * 2;
345 	} else {
346 		if (nmp->nm_sotype != SOCK_STREAM)
347 			panic("nfscon sotype");
348 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
349 			MGET(m, M_WAIT, MT_SOOPTS);
350 			*mtod(m, int32_t *) = 1;
351 			m->m_len = sizeof(int32_t);
352 			sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
353 		}
354 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
355 			MGET(m, M_WAIT, MT_SOOPTS);
356 			*mtod(m, int32_t *) = 1;
357 			m->m_len = sizeof(int32_t);
358 			sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
359 		}
360 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
361 		    sizeof (u_int32_t)) * 2;
362 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
363 		    sizeof (u_int32_t)) * 2;
364 	}
365 	error = soreserve(so, sndreserve, rcvreserve);
366 	if (error)
367 		goto bad;
368 	so->so_rcv.sb_flags |= SB_NOINTR;
369 	so->so_snd.sb_flags |= SB_NOINTR;
370 
371 	/* Initialize other non-zero congestion variables */
372 	nfs_init_rtt(nmp);
373 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
374 	nmp->nm_sent = 0;
375 	nmp->nm_timeouts = 0;
376 	return (0);
377 
378 bad:
379 	nfs_disconnect(nmp);
380 	return (error);
381 }
382 
383 /*
384  * Reconnect routine:
385  * Called when a connection is broken on a reliable protocol.
386  * - clean up the old socket
387  * - nfs_connect() again
388  * - set R_MUSTRESEND for all outstanding requests on mount point
389  * If this fails the mount point is DEAD!
390  * nb: Must be called with the nfs_sndlock() set on the mount point.
391  */
392 int
393 nfs_reconnect(struct nfsreq *rep)
394 {
395 	struct nfsreq *rp;
396 	struct nfsmount *nmp = rep->r_nmp;
397 	int error;
398 
399 	nfs_disconnect(nmp);
400 	while ((error = nfs_connect(nmp, rep)) != 0) {
401 		if (error == EINTR || error == ERESTART)
402 			return (EINTR);
403 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsrecon", 0);
404 	}
405 
406 	/*
407 	 * Loop through outstanding request list and fix up all requests
408 	 * on old socket.
409 	 */
410 	TAILQ_FOREACH(rp, &nmp->nm_reqsq, r_chain) {
411 		rp->r_flags |= R_MUSTRESEND;
412 		rp->r_rexmit = 0;
413 	}
414 	return (0);
415 }
416 
417 /*
418  * NFS disconnect. Clean up and unlink.
419  */
420 void
421 nfs_disconnect(struct nfsmount *nmp)
422 {
423 	struct socket *so;
424 
425 	if (nmp->nm_so) {
426 		so = nmp->nm_so;
427 		nmp->nm_so = NULL;
428 		soshutdown(so, SHUT_RDWR);
429 		soclose(so);
430 	}
431 }
432 
433 /*
434  * This is the nfs send routine. For connection based socket types, it
435  * must be called with an nfs_sndlock() on the socket.
436  * "rep == NULL" indicates that it has been called from a server.
437  * For the client side:
438  * - return EINTR if the RPC is terminated, 0 otherwise
439  * - set R_MUSTRESEND if the send fails for any reason
440  * - do any cleanup required by recoverable socket errors (???)
441  * For the server side:
442  * - return EINTR or ERESTART if interrupted by a signal
443  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
444  * - do any cleanup required by recoverable socket errors (???)
445  */
446 int
447 nfs_send(struct socket *so, struct mbuf *nam, struct mbuf *top,
448     struct nfsreq *rep)
449 {
450 	struct mbuf *sendnam;
451 	int error, soflags, flags;
452 
453 	if (rep) {
454 		if (rep->r_flags & R_SOFTTERM) {
455 			m_freem(top);
456 			return (EINTR);
457 		}
458 		if ((so = rep->r_nmp->nm_so) == NULL) {
459 			rep->r_flags |= R_MUSTRESEND;
460 			m_freem(top);
461 			return (0);
462 		}
463 		rep->r_flags &= ~R_MUSTRESEND;
464 		soflags = rep->r_nmp->nm_soflags;
465 	} else
466 		soflags = so->so_proto->pr_flags;
467 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
468 		sendnam = NULL;
469 	else
470 		sendnam = nam;
471 	if (so->so_type == SOCK_SEQPACKET)
472 		flags = MSG_EOR;
473 	else
474 		flags = 0;
475 
476 	error = sosend(so, sendnam, NULL, top, NULL, flags);
477 	if (error) {
478 		if (rep) {
479 			/*
480 			 * Deal with errors for the client side.
481 			 */
482 			if (rep->r_flags & R_SOFTTERM)
483 				error = EINTR;
484 			else
485 				rep->r_flags |= R_MUSTRESEND;
486 		}
487 
488 		/*
489 		 * Handle any recoverable (soft) socket errors here. (???)
490 		 */
491 		if (error != EINTR && error != ERESTART &&
492 			error != EWOULDBLOCK && error != EPIPE)
493 			error = 0;
494 	}
495 	return (error);
496 }
497 
498 #ifdef NFSCLIENT
499 /*
500  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
501  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
502  * Mark and consolidate the data into a new mbuf list.
503  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
504  *     small mbufs.
505  * For SOCK_STREAM we must be very careful to read an entire record once
506  * we have read any of it, even if the system call has been interrupted.
507  */
508 int
509 nfs_receive(struct nfsreq *rep, struct mbuf **aname, struct mbuf **mp)
510 {
511 	struct socket *so;
512 	struct uio auio;
513 	struct iovec aio;
514 	struct mbuf *m;
515 	struct mbuf *control;
516 	u_int32_t len;
517 	struct mbuf **getnam;
518 	int error, sotype, rcvflg;
519 	struct proc *p = curproc;	/* XXX */
520 
521 	/*
522 	 * Set up arguments for soreceive()
523 	 */
524 	*mp = NULL;
525 	*aname = NULL;
526 	sotype = rep->r_nmp->nm_sotype;
527 
528 	/*
529 	 * For reliable protocols, lock against other senders/receivers
530 	 * in case a reconnect is necessary.
531 	 * For SOCK_STREAM, first get the Record Mark to find out how much
532 	 * more there is to get.
533 	 * We must lock the socket against other receivers
534 	 * until we have an entire rpc request/reply.
535 	 */
536 	if (sotype != SOCK_DGRAM) {
537 		error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
538 		if (error)
539 			return (error);
540 tryagain:
541 		/*
542 		 * Check for fatal errors and resending request.
543 		 */
544 		/*
545 		 * Ugh: If a reconnect attempt just happened, nm_so
546 		 * would have changed. NULL indicates a failed
547 		 * attempt that has essentially shut down this
548 		 * mount point.
549 		 */
550 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
551 			nfs_sndunlock(&rep->r_nmp->nm_flag);
552 			return (EINTR);
553 		}
554 		so = rep->r_nmp->nm_so;
555 		if (!so) {
556 			error = nfs_reconnect(rep);
557 			if (error) {
558 				nfs_sndunlock(&rep->r_nmp->nm_flag);
559 				return (error);
560 			}
561 			goto tryagain;
562 		}
563 		while (rep->r_flags & R_MUSTRESEND) {
564 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
565 			nfsstats.rpcretries++;
566 			rep->r_rtt = 0;
567 			rep->r_flags &= ~R_TIMING;
568 			error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
569 			if (error) {
570 				if (error == EINTR || error == ERESTART ||
571 				    (error = nfs_reconnect(rep)) != 0) {
572 					nfs_sndunlock(&rep->r_nmp->nm_flag);
573 					return (error);
574 				}
575 				goto tryagain;
576 			}
577 		}
578 		nfs_sndunlock(&rep->r_nmp->nm_flag);
579 		if (sotype == SOCK_STREAM) {
580 			aio.iov_base = (caddr_t) &len;
581 			aio.iov_len = sizeof(u_int32_t);
582 			auio.uio_iov = &aio;
583 			auio.uio_iovcnt = 1;
584 			auio.uio_segflg = UIO_SYSSPACE;
585 			auio.uio_rw = UIO_READ;
586 			auio.uio_offset = 0;
587 			auio.uio_resid = sizeof(u_int32_t);
588 			auio.uio_procp = p;
589 			do {
590 			   rcvflg = MSG_WAITALL;
591 			   error = soreceive(so, NULL, &auio, NULL, NULL,
592 			       &rcvflg, 0);
593 			   if (error == EWOULDBLOCK && rep) {
594 				if (rep->r_flags & R_SOFTTERM)
595 					return (EINTR);
596 				/*
597 				 * looks like the server died after it
598 				 * received the request, make sure
599 				 * that we will retransmit and we
600 				 * don't get stuck here forever.
601 				 */
602 				if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
603 					nfsstats.rpctimeouts++;
604 					error = EPIPE;
605 				}
606 			   }
607 			} while (error == EWOULDBLOCK);
608 			if (!error && auio.uio_resid > 0) {
609 			    log(LOG_INFO,
610 				 "short receive (%zu/%zu) from nfs server %s\n",
611 				 sizeof(u_int32_t) - auio.uio_resid,
612 				 sizeof(u_int32_t),
613 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
614 			    error = EPIPE;
615 			}
616 			if (error)
617 				goto errout;
618 
619 			len = ntohl(len) & ~0x80000000;
620 			/*
621 			 * This is SERIOUS! We are out of sync with the sender
622 			 * and forcing a disconnect/reconnect is all I can do.
623 			 */
624 			if (len > NFS_MAXPACKET) {
625 			    log(LOG_ERR, "%s (%u) from nfs server %s\n",
626 				"impossible packet length",
627 				len,
628 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
629 			    error = EFBIG;
630 			    goto errout;
631 			}
632 			auio.uio_resid = len;
633 			do {
634 			    rcvflg = MSG_WAITALL;
635 			    error =  soreceive(so, NULL, &auio, mp, NULL,
636 			        &rcvflg, 0);
637 			} while (error == EWOULDBLOCK || error == EINTR ||
638 				 error == ERESTART);
639 			if (!error && auio.uio_resid > 0) {
640 			    log(LOG_INFO,
641 				"short receive (%zu/%u) from nfs server %s\n",
642 				len - auio.uio_resid, len,
643 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
644 			    error = EPIPE;
645 			}
646 		} else {
647 			/*
648 			 * NB: Since uio_resid is big, MSG_WAITALL is ignored
649 			 * and soreceive() will return when it has either a
650 			 * control msg or a data msg.
651 			 * We have no use for control msg., but must grab them
652 			 * and then throw them away so we know what is going
653 			 * on.
654 			 */
655 			auio.uio_resid = len = 100000000; /* Anything Big */
656 			auio.uio_procp = p;
657 			do {
658 			    rcvflg = 0;
659 			    error =  soreceive(so, NULL, &auio, mp, &control,
660 			        &rcvflg, 0);
661 			    m_freem(control);
662 			    if (error == EWOULDBLOCK && rep) {
663 				if (rep->r_flags & R_SOFTTERM)
664 					return (EINTR);
665 			    }
666 			} while (error == EWOULDBLOCK ||
667 				 (!error && *mp == NULL && control));
668 			if ((rcvflg & MSG_EOR) == 0)
669 				printf("Egad!!\n");
670 			if (!error && *mp == NULL)
671 				error = EPIPE;
672 			len -= auio.uio_resid;
673 		}
674 errout:
675 		if (error && error != EINTR && error != ERESTART) {
676 			m_freem(*mp);
677 			*mp = NULL;
678 			if (error != EPIPE)
679 				log(LOG_INFO,
680 				    "receive error %d from nfs server %s\n",
681 				    error,
682 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
683 			error = nfs_sndlock(&rep->r_nmp->nm_flag, rep);
684 			if (!error) {
685 				error = nfs_reconnect(rep);
686 				if (!error)
687 					goto tryagain;
688 				nfs_sndunlock(&rep->r_nmp->nm_flag);
689 			}
690 		}
691 	} else {
692 		if ((so = rep->r_nmp->nm_so) == NULL)
693 			return (EACCES);
694 		if (so->so_state & SS_ISCONNECTED)
695 			getnam = NULL;
696 		else
697 			getnam = aname;
698 		auio.uio_resid = len = 1000000;
699 		auio.uio_procp = p;
700 		do {
701 			rcvflg = 0;
702 			error =  soreceive(so, getnam, &auio, mp, NULL,
703 			    &rcvflg, 0);
704 			if (error == EWOULDBLOCK &&
705 			    (rep->r_flags & R_SOFTTERM))
706 				return (EINTR);
707 		} while (error == EWOULDBLOCK);
708 		len -= auio.uio_resid;
709 	}
710 	if (error) {
711 		m_freem(*mp);
712 		*mp = NULL;
713 	}
714 	/*
715 	 * Search for any mbufs that are not a multiple of 4 bytes long
716 	 * or with m_data not longword aligned.
717 	 * These could cause pointer alignment problems, so copy them to
718 	 * well aligned mbufs.
719 	 */
720 	nfs_realign(mp, 5 * NFSX_UNSIGNED);
721 	return (error);
722 }
723 
724 /*
725  * Implement receipt of reply on a socket.
726  * We must search through the list of received datagrams matching them
727  * with outstanding requests using the xid, until ours is found.
728  */
729 int
730 nfs_reply(struct nfsreq *myrep)
731 {
732 	struct nfsreq *rep;
733 	struct nfsmount *nmp = myrep->r_nmp;
734 	struct nfsm_info	info;
735 	struct mbuf *nam;
736 	u_int32_t rxid, *tl, t1;
737 	caddr_t cp2;
738 	int error;
739 
740 	/*
741 	 * Loop around until we get our own reply
742 	 */
743 	for (;;) {
744 		/*
745 		 * Lock against other receivers so that I don't get stuck in
746 		 * sbwait() after someone else has received my reply for me.
747 		 * Also necessary for connection based protocols to avoid
748 		 * race conditions during a reconnect.
749 		 */
750 		error = nfs_rcvlock(myrep);
751 		if (error)
752 			return (error == EALREADY ? 0 : error);
753 
754 		/*
755 		 * Get the next Rpc reply off the socket
756 		 */
757 		error = nfs_receive(myrep, &nam, &info.nmi_mrep);
758 		nfs_rcvunlock(&nmp->nm_flag);
759 		if (error) {
760 
761 			/*
762 			 * Ignore routing errors on connectionless protocols??
763 			 */
764 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
765 				if (nmp->nm_so)
766 					nmp->nm_so->so_error = 0;
767 				continue;
768 			}
769 			return (error);
770 		}
771 		m_freem(nam);
772 
773 		/*
774 		 * Get the xid and check that it is an rpc reply
775 		 */
776 		info.nmi_md = info.nmi_mrep;
777 		info.nmi_dpos = mtod(info.nmi_md, caddr_t);
778 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
779 		rxid = *tl++;
780 		if (*tl != rpc_reply) {
781 			nfsstats.rpcinvalid++;
782 			m_freem(info.nmi_mrep);
783 nfsmout:
784 			continue;
785 		}
786 
787 		/*
788 		 * Loop through the request list to match up the reply
789 		 * Iff no match, just drop the datagram
790 		 */
791 		TAILQ_FOREACH(rep, &nmp->nm_reqsq, r_chain) {
792 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
793 				/* Found it.. */
794 				rep->r_mrep = info.nmi_mrep;
795 				rep->r_md = info.nmi_md;
796 				rep->r_dpos = info.nmi_dpos;
797 
798 				/*
799 				 * Update congestion window.
800 				 * Do the additive increase of
801 				 * one rpc/rtt.
802 				 */
803 				if (nmp->nm_cwnd <= nmp->nm_sent) {
804 					nmp->nm_cwnd +=
805 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
806 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
807 					if (nmp->nm_cwnd > NFS_MAXCWND)
808 						nmp->nm_cwnd = NFS_MAXCWND;
809 				}
810 				rep->r_flags &= ~R_SENT;
811 				nmp->nm_sent -= NFS_CWNDSCALE;
812 
813 				if (rep->r_flags & R_TIMING)
814 					nfs_update_rtt(rep);
815 
816 				nmp->nm_timeouts = 0;
817 				break;
818 			}
819 		}
820 		/*
821 		 * If not matched to a request, drop it.
822 		 * If it's mine, get out.
823 		 */
824 		if (rep == 0) {
825 			nfsstats.rpcunexpected++;
826 			m_freem(info.nmi_mrep);
827 		} else if (rep == myrep) {
828 			if (rep->r_mrep == NULL)
829 				panic("nfsreply nil");
830 			return (0);
831 		}
832 	}
833 }
834 
835 /*
836  * nfs_request - goes something like this
837  *	- fill in request struct
838  *	- links it into list
839  *	- calls nfs_send() for first transmit
840  *	- calls nfs_receive() to get reply
841  *	- break down rpc header and return with nfs reply pointed to
842  *	  by mrep or error
843  * nb: always frees up mreq mbuf list
844  */
845 int
846 nfs_request(struct vnode *vp, int procnum, struct nfsm_info *infop)
847 {
848 	struct mbuf *m;
849 	u_int32_t *tl;
850 	struct nfsmount *nmp;
851 	struct timeval tv;
852 	caddr_t cp2;
853 	int t1, i, error = 0;
854 	int trylater_delay;
855 	struct nfsreq *rep;
856 	int  mrest_len;
857 	struct nfsm_info	info;
858 
859 	rep = pool_get(&nfsreqpl, PR_WAITOK);
860 	rep->r_nmp = VFSTONFS(vp->v_mount);
861 	rep->r_vp = vp;
862 	rep->r_procp = infop->nmi_procp;
863 	rep->r_procnum = procnum;
864 
865 	mrest_len = 0;
866 	m = infop->nmi_mreq;
867 	while (m) {
868 		mrest_len += m->m_len;
869 		m = m->m_next;
870 	}
871 
872 	/* empty mbuf for AUTH_UNIX header */
873 	rep->r_mreq = m_gethdr(M_WAIT, MT_DATA);
874 	rep->r_mreq->m_next = infop->nmi_mreq;
875 	rep->r_mreq->m_pkthdr.len = mrest_len;
876 
877 	trylater_delay = NFS_MINTIMEO;
878 
879 	nmp = rep->r_nmp;
880 
881 	/* Get the RPC header with authorization. */
882 	nfsm_rpchead(rep, infop->nmi_cred, RPCAUTH_UNIX);
883 	m = rep->r_mreq;
884 
885 	/*
886 	 * For stream protocols, insert a Sun RPC Record Mark.
887 	 */
888 	if (nmp->nm_sotype == SOCK_STREAM) {
889 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
890 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
891 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
892 	}
893 
894 tryagain:
895 	rep->r_rtt = rep->r_rexmit = 0;
896 	if (nfs_ptimers[rep->r_procnum] != NFS_DEFAULT_TIMER)
897 		rep->r_flags = R_TIMING;
898 	else
899 		rep->r_flags = 0;
900 	rep->r_mrep = NULL;
901 
902 	/*
903 	 * Do the client side RPC.
904 	 */
905 	nfsstats.rpcrequests++;
906 	/*
907 	 * Chain request into list of outstanding requests. Be sure
908 	 * to put it LAST so timer finds oldest requests first.
909 	 */
910 	if (TAILQ_EMPTY(&nmp->nm_reqsq))
911 		timeout_add(&nmp->nm_rtimeout, nfs_ticks);
912 	TAILQ_INSERT_TAIL(&nmp->nm_reqsq, rep, r_chain);
913 
914 	/*
915 	 * If backing off another request or avoiding congestion, don't
916 	 * send this one now but let timer do it. If not timing a request,
917 	 * do it now.
918 	 */
919 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
920 		(nmp->nm_flag & NFSMNT_DUMBTIMR) ||
921 		nmp->nm_sent < nmp->nm_cwnd)) {
922 		if (nmp->nm_soflags & PR_CONNREQUIRED)
923 			error = nfs_sndlock(&nmp->nm_flag, rep);
924 		if (!error) {
925 			error = nfs_send(nmp->nm_so, nmp->nm_nam,
926 					m_copym(m, 0, M_COPYALL, M_WAIT),
927 					rep);
928 			if (nmp->nm_soflags & PR_CONNREQUIRED)
929 				nfs_sndunlock(&nmp->nm_flag);
930 		}
931 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
932 			nmp->nm_sent += NFS_CWNDSCALE;
933 			rep->r_flags |= R_SENT;
934 		}
935 	} else {
936 		rep->r_rtt = -1;
937 	}
938 
939 	/*
940 	 * Wait for the reply from our send or the timer's.
941 	 */
942 	if (!error || error == EPIPE)
943 		error = nfs_reply(rep);
944 
945 	/*
946 	 * RPC done, unlink the request.
947 	 */
948 	TAILQ_REMOVE(&nmp->nm_reqsq, rep, r_chain);
949 	if (TAILQ_EMPTY(&nmp->nm_reqsq))
950 		timeout_del(&nmp->nm_rtimeout);
951 
952 	/*
953 	 * Decrement the outstanding request count.
954 	 */
955 	if (rep->r_flags & R_SENT) {
956 		rep->r_flags &= ~R_SENT;	/* paranoia */
957 		nmp->nm_sent -= NFS_CWNDSCALE;
958 	}
959 
960 	/*
961 	 * If there was a successful reply and a tprintf msg.
962 	 * tprintf a response.
963 	 */
964 	if (!error && (rep->r_flags & R_TPRINTFMSG))
965 		nfs_msg(rep, "is alive again");
966 	info.nmi_mrep = rep->r_mrep;
967 	info.nmi_md = rep->r_md;
968 	info.nmi_dpos = rep->r_dpos;
969 	if (error) {
970 		infop->nmi_mrep = NULL;
971 		goto nfsmout1;
972 	}
973 
974 	/*
975 	 * break down the rpc header and check if ok
976 	 */
977 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
978 	if (*tl++ == rpc_msgdenied) {
979 		if (*tl == rpc_mismatch)
980 			error = EOPNOTSUPP;
981 		else
982 			error = EACCES;	/* Should be EAUTH. */
983 		infop->nmi_mrep = NULL;
984 		goto nfsmout1;
985 	}
986 
987 	/*
988 	 * Since we only support RPCAUTH_UNIX atm we step over the
989 	 * reply verifer type, and in the (error) case that there really
990 	 * is any data in it, we advance over it.
991 	 */
992 	tl++;			/* Step over verifer type */
993 	i = fxdr_unsigned(int32_t, *tl);
994 	if (i > 0)
995 		nfsm_adv(nfsm_rndup(i));	/* Should not happen */
996 
997 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
998 	/* 0 == ok */
999 	if (*tl == 0) {
1000 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1001 		if (*tl != 0) {
1002 			error = fxdr_unsigned(int, *tl);
1003 			if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1004 			    error == NFSERR_TRYLATER) {
1005 				m_freem(info.nmi_mrep);
1006 				error = 0;
1007 				tv.tv_sec = trylater_delay;
1008 				tv.tv_usec = 0;
1009 				tsleep(&tv, PSOCK, "nfsretry", tvtohz(&tv));
1010 				trylater_delay *= NFS_TIMEOUTMUL;
1011 				if (trylater_delay > NFS_MAXTIMEO)
1012 					trylater_delay = NFS_MAXTIMEO;
1013 
1014 				goto tryagain;
1015 			}
1016 
1017 			/*
1018 			 * If the File Handle was stale, invalidate the
1019 			 * lookup cache, just in case.
1020 			 */
1021 			if (error == ESTALE)
1022 				cache_purge(rep->r_vp);
1023 		}
1024 		goto nfsmout;
1025 	}
1026 
1027 	error = EPROTONOSUPPORT;
1028 
1029 nfsmout:
1030 	infop->nmi_mrep = info.nmi_mrep;
1031 	infop->nmi_md = info.nmi_md;
1032 	infop->nmi_dpos = info.nmi_dpos;
1033 nfsmout1:
1034 	m_freem(rep->r_mreq);
1035 	pool_put(&nfsreqpl, rep);
1036 	return (error);
1037 }
1038 #endif /* NFSCLIENT */
1039 
1040 /*
1041  * Generate the rpc reply header
1042  * siz arg. is used to decide if adding a cluster is worthwhile
1043  */
1044 int
1045 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
1046     int err, struct mbuf **mrq, struct mbuf **mbp)
1047 {
1048 	u_int32_t *tl;
1049 	struct mbuf *mreq;
1050 	struct mbuf *mb;
1051 
1052 	MGETHDR(mreq, M_WAIT, MT_DATA);
1053 	mb = mreq;
1054 	/*
1055 	 * If this is a big reply, use a cluster else
1056 	 * try and leave leading space for the lower level headers.
1057 	 */
1058 	siz += RPC_REPLYSIZ;
1059 	if (siz >= MHLEN - max_hdr) {
1060 		MCLGET(mreq, M_WAIT);
1061 	} else
1062 		mreq->m_data += max_hdr;
1063 	tl = mtod(mreq, u_int32_t *);
1064 	mreq->m_len = 6 * NFSX_UNSIGNED;
1065 	*tl++ = txdr_unsigned(nd->nd_retxid);
1066 	*tl++ = rpc_reply;
1067 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1068 		*tl++ = rpc_msgdenied;
1069 		if (err & NFSERR_AUTHERR) {
1070 			*tl++ = rpc_autherr;
1071 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1072 			mreq->m_len -= NFSX_UNSIGNED;
1073 		} else {
1074 			*tl++ = rpc_mismatch;
1075 			*tl++ = txdr_unsigned(RPC_VER2);
1076 			*tl = txdr_unsigned(RPC_VER2);
1077 		}
1078 	} else {
1079 		*tl++ = rpc_msgaccepted;
1080 
1081 		/* AUTH_UNIX requires RPCAUTH_NULL. */
1082 		*tl++ = 0;
1083 		*tl++ = 0;
1084 
1085 		switch (err) {
1086 		case EPROGUNAVAIL:
1087 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
1088 			break;
1089 		case EPROGMISMATCH:
1090 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
1091 			tl = nfsm_build(&mb, 2 * NFSX_UNSIGNED);
1092 			*tl++ = txdr_unsigned(NFS_VER2);
1093 			*tl = txdr_unsigned(NFS_VER3);
1094 			break;
1095 		case EPROCUNAVAIL:
1096 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
1097 			break;
1098 		case EBADRPC:
1099 			*tl = txdr_unsigned(RPC_GARBAGE);
1100 			break;
1101 		default:
1102 			*tl = 0;
1103 			if (err != NFSERR_RETVOID) {
1104 				tl = nfsm_build(&mb, NFSX_UNSIGNED);
1105 				if (err)
1106 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1107 				else
1108 				    *tl = 0;
1109 			}
1110 			break;
1111 		};
1112 	}
1113 
1114 	*mrq = mreq;
1115 	if (mbp != NULL)
1116 		*mbp = mb;
1117 	if (err != 0 && err != NFSERR_RETVOID)
1118 		nfsstats.srvrpc_errs++;
1119 	return (0);
1120 }
1121 
1122 /*
1123  * nfs timer routine
1124  * Scan the nfsreq list and retranmit any requests that have timed out.
1125  */
1126 void
1127 nfs_timer(void *arg)
1128 {
1129 	struct nfsmount *nmp = arg;
1130 	struct nfsreq *rep;
1131 	struct mbuf *m;
1132 	struct socket *so;
1133 	int timeo, s, error;
1134 
1135 	NET_LOCK(s);
1136 	TAILQ_FOREACH(rep, &nmp->nm_reqsq, r_chain) {
1137 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1138 			continue;
1139 		if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1140 			rep->r_flags |= R_SOFTTERM;
1141 			continue;
1142 		}
1143 		if (rep->r_rtt >= 0) {
1144 			rep->r_rtt++;
1145 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1146 				timeo = nmp->nm_timeo;
1147 			else
1148 				timeo = nfs_estimate_rto(nmp, rep->r_procnum);
1149 			if (nmp->nm_timeouts > 0)
1150 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1151 			if (rep->r_rtt <= timeo)
1152 				continue;
1153 			if (nmp->nm_timeouts < nitems(nfs_backoff))
1154 				nmp->nm_timeouts++;
1155 		}
1156 
1157 		/* Check for server not responding. */
1158 		if ((rep->r_flags & R_TPRINTFMSG) == 0 && rep->r_rexmit > 4) {
1159 			nfs_msg(rep, "not responding");
1160 			rep->r_flags |= R_TPRINTFMSG;
1161 		}
1162 		if (rep->r_rexmit >= nmp->nm_retry) {	/* too many */
1163 			nfsstats.rpctimeouts++;
1164 			rep->r_flags |= R_SOFTTERM;
1165 			continue;
1166 		}
1167 		if (nmp->nm_sotype != SOCK_DGRAM) {
1168 			if (++rep->r_rexmit > NFS_MAXREXMIT)
1169 				rep->r_rexmit = NFS_MAXREXMIT;
1170 			continue;
1171 		}
1172 
1173 		if ((so = nmp->nm_so) == NULL)
1174 			continue;
1175 
1176 		/*
1177 		 * If there is enough space and the window allows..
1178 		 *	Resend it
1179 		 * Set r_rtt to -1 in case we fail to send it now.
1180 		 */
1181 		rep->r_rtt = -1;
1182 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1183 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1184 		    (rep->r_flags & R_SENT) ||
1185 		    nmp->nm_sent < nmp->nm_cwnd) &&
1186 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1187 			if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1188 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1189 			    NULL, NULL, curproc);
1190 			else
1191 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1192 			    nmp->nm_nam, NULL, curproc);
1193 			if (error) {
1194 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1195 					so->so_error = 0;
1196 			} else {
1197 				/*
1198 				 * Iff first send, start timing
1199 				 * else turn timing off, backoff timer
1200 				 * and divide congestion window by 2.
1201 				 */
1202 				if (rep->r_flags & R_SENT) {
1203 					rep->r_flags &= ~R_TIMING;
1204 					if (++rep->r_rexmit > NFS_MAXREXMIT)
1205 						rep->r_rexmit = NFS_MAXREXMIT;
1206 					nmp->nm_cwnd >>= 1;
1207 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
1208 						nmp->nm_cwnd = NFS_CWNDSCALE;
1209 					nfsstats.rpcretries++;
1210 				} else {
1211 					rep->r_flags |= R_SENT;
1212 					nmp->nm_sent += NFS_CWNDSCALE;
1213 				}
1214 				rep->r_rtt = 0;
1215 			}
1216 		}
1217 	}
1218 	NET_UNLOCK(s);
1219 	timeout_add(&nmp->nm_rtimeout, nfs_ticks);
1220 }
1221 
1222 /*
1223  * Test for a termination condition pending on the process.
1224  * This is used for NFSMNT_INT mounts.
1225  */
1226 int
1227 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct proc *p)
1228 {
1229 
1230 	if (rep && (rep->r_flags & R_SOFTTERM))
1231 		return (EINTR);
1232 	if (!(nmp->nm_flag & NFSMNT_INT))
1233 		return (0);
1234 	if (p && p->p_siglist &&
1235 	    (((p->p_siglist & ~p->p_sigmask) &
1236 	    ~p->p_p->ps_sigacts->ps_sigignore) & NFSINT_SIGMASK))
1237 		return (EINTR);
1238 	return (0);
1239 }
1240 
1241 /*
1242  * Lock a socket against others.
1243  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1244  * and also to avoid race conditions between the processes with nfs requests
1245  * in progress when a reconnect is necessary.
1246  */
1247 int
1248 nfs_sndlock(int *flagp, struct nfsreq *rep)
1249 {
1250 	struct proc *p;
1251 	int slpflag = 0, slptimeo = 0;
1252 
1253 	if (rep) {
1254 		p = rep->r_procp;
1255 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
1256 			slpflag = PCATCH;
1257 	} else
1258 		p = NULL;
1259 	while (*flagp & NFSMNT_SNDLOCK) {
1260 		if (rep && nfs_sigintr(rep->r_nmp, rep, p))
1261 			return (EINTR);
1262 		*flagp |= NFSMNT_WANTSND;
1263 		(void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsndlck",
1264 			slptimeo);
1265 		if (slpflag == PCATCH) {
1266 			slpflag = 0;
1267 			slptimeo = 2 * hz;
1268 		}
1269 	}
1270 	*flagp |= NFSMNT_SNDLOCK;
1271 	return (0);
1272 }
1273 
1274 /*
1275  * Unlock the stream socket for others.
1276  */
1277 void
1278 nfs_sndunlock(int *flagp)
1279 {
1280 
1281 	if ((*flagp & NFSMNT_SNDLOCK) == 0)
1282 		panic("nfs sndunlock");
1283 	*flagp &= ~NFSMNT_SNDLOCK;
1284 	if (*flagp & NFSMNT_WANTSND) {
1285 		*flagp &= ~NFSMNT_WANTSND;
1286 		wakeup((caddr_t)flagp);
1287 	}
1288 }
1289 
1290 int
1291 nfs_rcvlock(struct nfsreq *rep)
1292 {
1293 	int *flagp = &rep->r_nmp->nm_flag;
1294 	int slpflag, slptimeo = 0;
1295 
1296 	if (*flagp & NFSMNT_INT)
1297 		slpflag = PCATCH;
1298 	else
1299 		slpflag = 0;
1300 
1301 	while (*flagp & NFSMNT_RCVLOCK) {
1302 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
1303 			return (EINTR);
1304 		*flagp |= NFSMNT_WANTRCV;
1305 		(void) tsleep((caddr_t)flagp, slpflag | (PZERO - 1), "nfsrcvlk",
1306 			slptimeo);
1307 		if (rep->r_mrep != NULL) {
1308 			/*
1309 			 * Don't take the lock if our reply has been received
1310 			 * while we where sleeping.
1311 			 */
1312 			 return (EALREADY);
1313 		}
1314 		if (slpflag == PCATCH) {
1315 			slpflag = 0;
1316 			slptimeo = 2 * hz;
1317 		}
1318 	}
1319 	*flagp |= NFSMNT_RCVLOCK;
1320 	return (0);
1321 }
1322 
1323 /*
1324  * Unlock the stream socket for others.
1325  */
1326 void
1327 nfs_rcvunlock(int *flagp)
1328 {
1329 
1330 	if ((*flagp & NFSMNT_RCVLOCK) == 0)
1331 		panic("nfs rcvunlock");
1332 	*flagp &= ~NFSMNT_RCVLOCK;
1333 	if (*flagp & NFSMNT_WANTRCV) {
1334 		*flagp &= ~NFSMNT_WANTRCV;
1335 		wakeup((caddr_t)flagp);
1336 	}
1337 }
1338 
1339 /*
1340  * Auxiliary routine to align the length of mbuf copies made with m_copyback().
1341  */
1342 void
1343 nfs_realign_fixup(struct mbuf *m, struct mbuf *n, unsigned int *off)
1344 {
1345 	size_t padding;
1346 
1347 	/*
1348 	 * The maximum number of bytes that m_copyback() places in a mbuf is
1349 	 * always an aligned quantity, so realign happens at the chain's tail.
1350 	 */
1351 	while (n->m_next != NULL)
1352 		n = n->m_next;
1353 
1354 	/*
1355 	 * Pad from the next elements in the source chain. Loop until the
1356 	 * destination chain is aligned, or the end of the source is reached.
1357 	 */
1358 	do {
1359 		m = m->m_next;
1360 		if (m == NULL)
1361 			return;
1362 
1363 		padding = min(ALIGN(n->m_len) - n->m_len, m->m_len);
1364 		if (padding > M_TRAILINGSPACE(n))
1365 			panic("nfs_realign_fixup: no memory to pad to");
1366 
1367 		bcopy(mtod(m, void *), mtod(n, char *) + n->m_len, padding);
1368 
1369 		n->m_len += padding;
1370 		m_adj(m, padding);
1371 		*off += padding;
1372 
1373 	} while (!ALIGNED_POINTER(n->m_len, void *));
1374 }
1375 
1376 /*
1377  * The NFS RPC parsing code uses the data address and the length of mbuf
1378  * structures to calculate on-memory addresses. This function makes sure these
1379  * parameters are correctly aligned.
1380  */
1381 void
1382 nfs_realign(struct mbuf **pm, int hsiz)
1383 {
1384 	struct mbuf *m;
1385 	struct mbuf *n = NULL;
1386 	unsigned int off = 0;
1387 
1388 	++nfs_realign_test;
1389 	while ((m = *pm) != NULL) {
1390 		if (!ALIGNED_POINTER(m->m_data, void *) ||
1391 		    !ALIGNED_POINTER(m->m_len,  void *)) {
1392 			MGET(n, M_WAIT, MT_DATA);
1393 #define ALIGN_POINTER(n) ((u_int)(((n) + sizeof(void *)) & ~sizeof(void *)))
1394 			if (ALIGN_POINTER(m->m_len) >= MINCLSIZE) {
1395 				MCLGET(n, M_WAIT);
1396 			}
1397 			n->m_len = 0;
1398 			break;
1399 		}
1400 		pm = &m->m_next;
1401 	}
1402 	/*
1403 	 * If n is non-NULL, loop on m copying data, then replace the
1404 	 * portion of the chain that had to be realigned.
1405 	 */
1406 	if (n != NULL) {
1407 		++nfs_realign_count;
1408 		while (m) {
1409 			m_copyback(n, off, m->m_len, mtod(m, caddr_t), M_WAIT);
1410 
1411 			/*
1412 			 * If an unaligned amount of memory was copied, fix up
1413 			 * the last mbuf created by m_copyback().
1414 			 */
1415 			if (!ALIGNED_POINTER(m->m_len, void *))
1416 				nfs_realign_fixup(m, n, &off);
1417 
1418 			off += m->m_len;
1419 			m = m->m_next;
1420 		}
1421 		m_freem(*pm);
1422 		*pm = n;
1423 	}
1424 }
1425 
1426 
1427 /*
1428  * Parse an RPC request
1429  * - verify it
1430  * - fill in the cred struct.
1431  */
1432 int
1433 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
1434 {
1435 	int len, i;
1436 	u_int32_t *tl;
1437 	int32_t t1;
1438 	caddr_t cp2;
1439 	u_int32_t nfsvers, auth_type;
1440 	int error = 0;
1441 	struct nfsm_info	info;
1442 
1443 	info.nmi_mrep = nd->nd_mrep;
1444 	info.nmi_md = nd->nd_md;
1445 	info.nmi_dpos = nd->nd_dpos;
1446 	if (has_header) {
1447 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1448 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1449 		if (*tl++ != rpc_call) {
1450 			m_freem(info.nmi_mrep);
1451 			return (EBADRPC);
1452 		}
1453 	} else
1454 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1455 	nd->nd_repstat = 0;
1456 	nd->nd_flag = 0;
1457 	if (*tl++ != rpc_vers) {
1458 		nd->nd_repstat = ERPCMISMATCH;
1459 		nd->nd_procnum = NFSPROC_NOOP;
1460 		return (0);
1461 	}
1462 	if (*tl != nfs_prog) {
1463 		nd->nd_repstat = EPROGUNAVAIL;
1464 		nd->nd_procnum = NFSPROC_NOOP;
1465 		return (0);
1466 	}
1467 	tl++;
1468 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1469 	if (nfsvers != NFS_VER2 && nfsvers != NFS_VER3) {
1470 		nd->nd_repstat = EPROGMISMATCH;
1471 		nd->nd_procnum = NFSPROC_NOOP;
1472 		return (0);
1473 	}
1474 	if (nfsvers == NFS_VER3)
1475 		nd->nd_flag = ND_NFSV3;
1476 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1477 	if (nd->nd_procnum == NFSPROC_NULL)
1478 		return (0);
1479 	if (nd->nd_procnum >= NFS_NPROCS ||
1480 		(nd->nd_procnum > NFSPROC_COMMIT) ||
1481 		(!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1482 		nd->nd_repstat = EPROCUNAVAIL;
1483 		nd->nd_procnum = NFSPROC_NOOP;
1484 		return (0);
1485 	}
1486 	if ((nd->nd_flag & ND_NFSV3) == 0)
1487 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1488 	auth_type = *tl++;
1489 	len = fxdr_unsigned(int, *tl++);
1490 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
1491 		m_freem(info.nmi_mrep);
1492 		return (EBADRPC);
1493 	}
1494 
1495 	/* Handle auth_unix */
1496 	if (auth_type == rpc_auth_unix) {
1497 		len = fxdr_unsigned(int, *++tl);
1498 		if (len < 0 || len > NFS_MAXNAMLEN) {
1499 			m_freem(info.nmi_mrep);
1500 			return (EBADRPC);
1501 		}
1502 		nfsm_adv(nfsm_rndup(len));
1503 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1504 		memset(&nd->nd_cr, 0, sizeof (struct ucred));
1505 		nd->nd_cr.cr_ref = 1;
1506 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1507 		nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1508 		len = fxdr_unsigned(int, *tl);
1509 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1510 			m_freem(info.nmi_mrep);
1511 			return (EBADRPC);
1512 		}
1513 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1514 		for (i = 0; i < len; i++)
1515 		    if (i < NGROUPS_MAX)
1516 			nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1517 		    else
1518 			tl++;
1519 		nd->nd_cr.cr_ngroups = (len > NGROUPS_MAX) ? NGROUPS_MAX : len;
1520 		len = fxdr_unsigned(int, *++tl);
1521 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
1522 			m_freem(info.nmi_mrep);
1523 			return (EBADRPC);
1524 		}
1525 		if (len > 0)
1526 			nfsm_adv(nfsm_rndup(len));
1527 	} else {
1528 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1529 		nd->nd_procnum = NFSPROC_NOOP;
1530 		return (0);
1531 	}
1532 
1533 	nd->nd_md = info.nmi_md;
1534 	nd->nd_dpos = info.nmi_dpos;
1535 	return (0);
1536 nfsmout:
1537 	return (error);
1538 }
1539 
1540 void
1541 nfs_msg(struct nfsreq *rep, char *msg)
1542 {
1543 	tpr_t tpr;
1544 
1545 	if (rep->r_procp)
1546 		tpr = tprintf_open(rep->r_procp);
1547 	else
1548 		tpr = NULL;
1549 
1550 	tprintf(tpr, "nfs server %s: %s\n",
1551 	    rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname, msg);
1552 	tprintf_close(tpr);
1553 }
1554 
1555 #ifdef NFSSERVER
1556 /*
1557  * Socket upcall routine for the nfsd sockets.
1558  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
1559  * Essentially do as much as possible non-blocking, else punt and it will
1560  * be called with M_WAIT from an nfsd.
1561  */
1562 void
1563 nfsrv_rcv(struct socket *so, caddr_t arg, int waitflag)
1564 {
1565 	struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
1566 	struct mbuf *m;
1567 	struct mbuf *mp, *nam;
1568 	struct uio auio;
1569 	int flags, error;
1570 
1571 	if ((slp->ns_flag & SLP_VALID) == 0)
1572 		return;
1573 
1574 	/* Defer soreceive() to an nfsd. */
1575 	if (waitflag == M_DONTWAIT) {
1576 		slp->ns_flag |= SLP_NEEDQ;
1577 		goto dorecs;
1578 	}
1579 
1580 	auio.uio_procp = NULL;
1581 	if (so->so_type == SOCK_STREAM) {
1582 		/*
1583 		 * Do soreceive().
1584 		 */
1585 		auio.uio_resid = 1000000000;
1586 		flags = MSG_DONTWAIT;
1587 		error = soreceive(so, &nam, &auio, &mp, NULL,
1588 		    &flags, 0);
1589 		if (error || mp == NULL) {
1590 			if (error == EWOULDBLOCK)
1591 				slp->ns_flag |= SLP_NEEDQ;
1592 			else
1593 				slp->ns_flag |= SLP_DISCONN;
1594 			goto dorecs;
1595 		}
1596 		m = mp;
1597 		if (slp->ns_rawend) {
1598 			slp->ns_rawend->m_next = m;
1599 			slp->ns_cc += 1000000000 - auio.uio_resid;
1600 		} else {
1601 			slp->ns_raw = m;
1602 			slp->ns_cc = 1000000000 - auio.uio_resid;
1603 		}
1604 		while (m->m_next)
1605 			m = m->m_next;
1606 		slp->ns_rawend = m;
1607 
1608 		/*
1609 		 * Now try and parse record(s) out of the raw stream data.
1610 		 */
1611 		error = nfsrv_getstream(slp, waitflag);
1612 		if (error) {
1613 			if (error == EPERM)
1614 				slp->ns_flag |= SLP_DISCONN;
1615 			else
1616 				slp->ns_flag |= SLP_NEEDQ;
1617 		}
1618 	} else {
1619 		do {
1620 			auio.uio_resid = 1000000000;
1621 			flags = MSG_DONTWAIT;
1622 			error = soreceive(so, &nam, &auio, &mp,
1623 			    NULL, &flags, 0);
1624 			if (mp) {
1625 				if (nam) {
1626 					m = nam;
1627 					m->m_next = mp;
1628 				} else
1629 					m = mp;
1630 				if (slp->ns_recend)
1631 					slp->ns_recend->m_nextpkt = m;
1632 				else
1633 					slp->ns_rec = m;
1634 				slp->ns_recend = m;
1635 				m->m_nextpkt = NULL;
1636 			}
1637 			if (error) {
1638 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
1639 					&& error != EWOULDBLOCK) {
1640 					slp->ns_flag |= SLP_DISCONN;
1641 					goto dorecs;
1642 				}
1643 			}
1644 		} while (mp);
1645 	}
1646 
1647 	/*
1648 	 * Now try and process the request records, non-blocking.
1649 	 */
1650 dorecs:
1651 	if (waitflag == M_DONTWAIT &&
1652 		(slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
1653 		nfsrv_wakenfsd(slp);
1654 }
1655 
1656 /*
1657  * Try and extract an RPC request from the mbuf data list received on a
1658  * stream socket. The "waitflag" argument indicates whether or not it
1659  * can sleep.
1660  */
1661 int
1662 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag)
1663 {
1664 	struct mbuf *m, **mpp;
1665 	char *cp1, *cp2;
1666 	int len;
1667 	struct mbuf *om, *m2, *recm;
1668 	u_int32_t recmark;
1669 
1670 	if (slp->ns_flag & SLP_GETSTREAM)
1671 		return (0);
1672 	slp->ns_flag |= SLP_GETSTREAM;
1673 	for (;;) {
1674 	    if (slp->ns_reclen == 0) {
1675 		if (slp->ns_cc < NFSX_UNSIGNED) {
1676 			slp->ns_flag &= ~SLP_GETSTREAM;
1677 			return (0);
1678 		}
1679 		m = slp->ns_raw;
1680 		if (m->m_len >= NFSX_UNSIGNED) {
1681 			bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
1682 			m->m_data += NFSX_UNSIGNED;
1683 			m->m_len -= NFSX_UNSIGNED;
1684 		} else {
1685 			cp1 = (caddr_t)&recmark;
1686 			cp2 = mtod(m, caddr_t);
1687 			while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
1688 				while (m->m_len == 0) {
1689 					m = m->m_next;
1690 					cp2 = mtod(m, caddr_t);
1691 				}
1692 				*cp1++ = *cp2++;
1693 				m->m_data++;
1694 				m->m_len--;
1695 			}
1696 		}
1697 		slp->ns_cc -= NFSX_UNSIGNED;
1698 		recmark = ntohl(recmark);
1699 		slp->ns_reclen = recmark & ~0x80000000;
1700 		if (recmark & 0x80000000)
1701 			slp->ns_flag |= SLP_LASTFRAG;
1702 		else
1703 			slp->ns_flag &= ~SLP_LASTFRAG;
1704 		if (slp->ns_reclen > NFS_MAXPACKET) {
1705 			slp->ns_flag &= ~SLP_GETSTREAM;
1706 			return (EPERM);
1707 		}
1708 	    }
1709 
1710 	    /*
1711 	     * Now get the record part.
1712 	     */
1713 	    recm = NULL;
1714 	    if (slp->ns_cc == slp->ns_reclen) {
1715 		recm = slp->ns_raw;
1716 		slp->ns_raw = slp->ns_rawend = NULL;
1717 		slp->ns_cc = slp->ns_reclen = 0;
1718 	    } else if (slp->ns_cc > slp->ns_reclen) {
1719 		len = 0;
1720 		m = slp->ns_raw;
1721 		om = NULL;
1722 		while (len < slp->ns_reclen) {
1723 			if ((len + m->m_len) > slp->ns_reclen) {
1724 				m2 = m_copym(m, 0, slp->ns_reclen - len,
1725 					waitflag);
1726 				if (m2) {
1727 					if (om) {
1728 						om->m_next = m2;
1729 						recm = slp->ns_raw;
1730 					} else
1731 						recm = m2;
1732 					m->m_data += slp->ns_reclen - len;
1733 					m->m_len -= slp->ns_reclen - len;
1734 					len = slp->ns_reclen;
1735 				} else {
1736 					slp->ns_flag &= ~SLP_GETSTREAM;
1737 					return (EWOULDBLOCK);
1738 				}
1739 			} else if ((len + m->m_len) == slp->ns_reclen) {
1740 				om = m;
1741 				len += m->m_len;
1742 				m = m->m_next;
1743 				recm = slp->ns_raw;
1744 				om->m_next = NULL;
1745 			} else {
1746 				om = m;
1747 				len += m->m_len;
1748 				m = m->m_next;
1749 			}
1750 		}
1751 		slp->ns_raw = m;
1752 		slp->ns_cc -= len;
1753 		slp->ns_reclen = 0;
1754 	    } else {
1755 		slp->ns_flag &= ~SLP_GETSTREAM;
1756 		return (0);
1757 	    }
1758 
1759 	    /*
1760 	     * Accumulate the fragments into a record.
1761 	     */
1762 	    mpp = &slp->ns_frag;
1763 	    while (*mpp)
1764 		mpp = &((*mpp)->m_next);
1765 	    *mpp = recm;
1766 	    if (slp->ns_flag & SLP_LASTFRAG) {
1767 		if (slp->ns_recend)
1768 		    slp->ns_recend->m_nextpkt = slp->ns_frag;
1769 		else
1770 		    slp->ns_rec = slp->ns_frag;
1771 		slp->ns_recend = slp->ns_frag;
1772 		slp->ns_frag = NULL;
1773 	    }
1774 	}
1775 }
1776 
1777 /*
1778  * Parse an RPC header.
1779  */
1780 int
1781 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
1782     struct nfsrv_descript **ndp)
1783 {
1784 	struct mbuf *m, *nam;
1785 	struct nfsrv_descript *nd;
1786 	int error;
1787 
1788 	*ndp = NULL;
1789 	if ((slp->ns_flag & SLP_VALID) == 0 ||
1790 	    (m = slp->ns_rec) == NULL)
1791 		return (ENOBUFS);
1792 	slp->ns_rec = m->m_nextpkt;
1793 	if (slp->ns_rec)
1794 		m->m_nextpkt = NULL;
1795 	else
1796 		slp->ns_recend = NULL;
1797 	if (m->m_type == MT_SONAME) {
1798 		nam = m;
1799 		m = m->m_next;
1800 		nam->m_next = NULL;
1801 	} else
1802 		nam = NULL;
1803 	nd = pool_get(&nfsrv_descript_pl, PR_WAITOK);
1804 	nfs_realign(&m, 10 * NFSX_UNSIGNED);
1805 	nd->nd_md = nd->nd_mrep = m;
1806 	nd->nd_nam2 = nam;
1807 	nd->nd_dpos = mtod(m, caddr_t);
1808 	error = nfs_getreq(nd, nfsd, 1);
1809 	if (error) {
1810 		m_freem(nam);
1811 		pool_put(&nfsrv_descript_pl, nd);
1812 		return (error);
1813 	}
1814 	*ndp = nd;
1815 	nfsd->nfsd_nd = nd;
1816 	return (0);
1817 }
1818 
1819 
1820 /*
1821  * Search for a sleeping nfsd and wake it up.
1822  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
1823  * running nfsds will go look for the work in the nfssvc_sock list.
1824  */
1825 void
1826 nfsrv_wakenfsd(struct nfssvc_sock *slp)
1827 {
1828 	struct nfsd	*nfsd;
1829 
1830 	if ((slp->ns_flag & SLP_VALID) == 0)
1831 		return;
1832 
1833 	TAILQ_FOREACH(nfsd, &nfsd_head, nfsd_chain) {
1834 		if (nfsd->nfsd_flag & NFSD_WAITING) {
1835 			nfsd->nfsd_flag &= ~NFSD_WAITING;
1836 			if (nfsd->nfsd_slp)
1837 				panic("nfsd wakeup");
1838 			slp->ns_sref++;
1839 			nfsd->nfsd_slp = slp;
1840 			wakeup_one(nfsd);
1841 			return;
1842 		}
1843 	}
1844 
1845 	slp->ns_flag |= SLP_DOREC;
1846 	nfsd_head_flag |= NFSD_CHECKSLP;
1847 }
1848 #endif /* NFSSERVER */
1849