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