xref: /dragonfly/sys/vfs/nfs/nfs_socket.c (revision 9f3fc534)
1 /*
2  * Copyright (c) 1989, 1991, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Rick Macklem at The University of Guelph.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)nfs_socket.c	8.5 (Berkeley) 3/30/95
37  * $FreeBSD: src/sys/nfs/nfs_socket.c,v 1.60.2.6 2003/03/26 01:44:46 alfred Exp $
38  * $DragonFly: src/sys/vfs/nfs/nfs_socket.c,v 1.45 2007/05/18 17:05:13 dillon Exp $
39  */
40 
41 /*
42  * Socket operations for use by nfs
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.h>
50 #include <sys/kernel.h>
51 #include <sys/mbuf.h>
52 #include <sys/vnode.h>
53 #include <sys/fcntl.h>
54 #include <sys/protosw.h>
55 #include <sys/resourcevar.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/socketops.h>
59 #include <sys/syslog.h>
60 #include <sys/thread.h>
61 #include <sys/tprintf.h>
62 #include <sys/sysctl.h>
63 #include <sys/signalvar.h>
64 #include <sys/signal2.h>
65 
66 #include <netinet/in.h>
67 #include <netinet/tcp.h>
68 #include <sys/thread2.h>
69 
70 #include "rpcv2.h"
71 #include "nfsproto.h"
72 #include "nfs.h"
73 #include "xdr_subs.h"
74 #include "nfsm_subs.h"
75 #include "nfsmount.h"
76 #include "nfsnode.h"
77 #include "nfsrtt.h"
78 
79 #define	TRUE	1
80 #define	FALSE	0
81 
82 /*
83  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
84  * Use the mean and mean deviation of rtt for the appropriate type of rpc
85  * for the frequent rpcs and a default for the others.
86  * The justification for doing "other" this way is that these rpcs
87  * happen so infrequently that timer est. would probably be stale.
88  * Also, since many of these rpcs are
89  * non-idempotent, a conservative timeout is desired.
90  * getattr, lookup - A+2D
91  * read, write     - A+4D
92  * other           - nm_timeo
93  */
94 #define	NFS_RTO(n, t) \
95 	((t) == 0 ? (n)->nm_timeo : \
96 	 ((t) < 3 ? \
97 	  (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
98 	  ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
99 #define	NFS_SRTT(r)	(r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
100 #define	NFS_SDRTT(r)	(r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
101 /*
102  * External data, mostly RPC constants in XDR form
103  */
104 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
105 	rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
106 	rpc_auth_kerb;
107 extern u_int32_t nfs_prog;
108 extern struct nfsstats nfsstats;
109 extern int nfsv3_procid[NFS_NPROCS];
110 extern int nfs_ticks;
111 
112 /*
113  * Defines which timer to use for the procnum.
114  * 0 - default
115  * 1 - getattr
116  * 2 - lookup
117  * 3 - read
118  * 4 - write
119  */
120 static int proct[NFS_NPROCS] = {
121 	0, 1, 0, 2, 1, 3, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0,
122 	0, 0, 0,
123 };
124 
125 static int nfs_realign_test;
126 static int nfs_realign_count;
127 static int nfs_bufpackets = 4;
128 static int nfs_timer_raced;
129 
130 SYSCTL_DECL(_vfs_nfs);
131 
132 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_test, CTLFLAG_RW, &nfs_realign_test, 0, "");
133 SYSCTL_INT(_vfs_nfs, OID_AUTO, realign_count, CTLFLAG_RW, &nfs_realign_count, 0, "");
134 SYSCTL_INT(_vfs_nfs, OID_AUTO, bufpackets, CTLFLAG_RW, &nfs_bufpackets, 0, "");
135 
136 
137 /*
138  * There is a congestion window for outstanding rpcs maintained per mount
139  * point. The cwnd size is adjusted in roughly the way that:
140  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
141  * SIGCOMM '88". ACM, August 1988.
142  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
143  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
144  * of rpcs is in progress.
145  * (The sent count and cwnd are scaled for integer arith.)
146  * Variants of "slow start" were tried and were found to be too much of a
147  * performance hit (ave. rtt 3 times larger),
148  * I suspect due to the large rtt that nfs rpcs have.
149  */
150 #define	NFS_CWNDSCALE	256
151 #define	NFS_MAXCWND	(NFS_CWNDSCALE * 32)
152 static int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
153 int nfsrtton = 0;
154 struct nfsrtt nfsrtt;
155 struct callout	nfs_timer_handle;
156 
157 static int	nfs_msg (struct thread *,char *,char *);
158 static int	nfs_rcvlock (struct nfsreq *);
159 static void	nfs_rcvunlock (struct nfsreq *);
160 static void	nfs_realign (struct mbuf **pm, int hsiz);
161 static int	nfs_receive (struct nfsreq *rep, struct sockaddr **aname,
162 				 struct mbuf **mp);
163 static void	nfs_softterm (struct nfsreq *rep);
164 static int	nfs_reconnect (struct nfsreq *rep);
165 #ifndef NFS_NOSERVER
166 static int	nfsrv_getstream (struct nfssvc_sock *, int, int *);
167 
168 int (*nfsrv3_procs[NFS_NPROCS]) (struct nfsrv_descript *nd,
169 				    struct nfssvc_sock *slp,
170 				    struct thread *td,
171 				    struct mbuf **mreqp) = {
172 	nfsrv_null,
173 	nfsrv_getattr,
174 	nfsrv_setattr,
175 	nfsrv_lookup,
176 	nfsrv3_access,
177 	nfsrv_readlink,
178 	nfsrv_read,
179 	nfsrv_write,
180 	nfsrv_create,
181 	nfsrv_mkdir,
182 	nfsrv_symlink,
183 	nfsrv_mknod,
184 	nfsrv_remove,
185 	nfsrv_rmdir,
186 	nfsrv_rename,
187 	nfsrv_link,
188 	nfsrv_readdir,
189 	nfsrv_readdirplus,
190 	nfsrv_statfs,
191 	nfsrv_fsinfo,
192 	nfsrv_pathconf,
193 	nfsrv_commit,
194 	nfsrv_noop,
195 	nfsrv_noop,
196 	nfsrv_noop,
197 	nfsrv_noop
198 };
199 #endif /* NFS_NOSERVER */
200 
201 /*
202  * Initialize sockets and congestion for a new NFS connection.
203  * We do not free the sockaddr if error.
204  */
205 int
206 nfs_connect(struct nfsmount *nmp, struct nfsreq *rep)
207 {
208 	struct socket *so;
209 	int error, rcvreserve, sndreserve;
210 	int pktscale;
211 	struct sockaddr *saddr;
212 	struct sockaddr_in *sin;
213 	struct thread *td = &thread0; /* only used for socreate and sobind */
214 
215 	nmp->nm_so = NULL;
216 	saddr = nmp->nm_nam;
217 	error = socreate(saddr->sa_family, &nmp->nm_so, nmp->nm_sotype,
218 		nmp->nm_soproto, td);
219 	if (error)
220 		goto bad;
221 	so = nmp->nm_so;
222 	nmp->nm_soflags = so->so_proto->pr_flags;
223 
224 	/*
225 	 * Some servers require that the client port be a reserved port number.
226 	 */
227 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
228 		struct sockopt sopt;
229 		int ip;
230 		struct sockaddr_in ssin;
231 
232 		bzero(&sopt, sizeof sopt);
233 		ip = IP_PORTRANGE_LOW;
234 		sopt.sopt_level = IPPROTO_IP;
235 		sopt.sopt_name = IP_PORTRANGE;
236 		sopt.sopt_val = (void *)&ip;
237 		sopt.sopt_valsize = sizeof(ip);
238 		sopt.sopt_td = NULL;
239 		error = sosetopt(so, &sopt);
240 		if (error)
241 			goto bad;
242 		bzero(&ssin, sizeof ssin);
243 		sin = &ssin;
244 		sin->sin_len = sizeof (struct sockaddr_in);
245 		sin->sin_family = AF_INET;
246 		sin->sin_addr.s_addr = INADDR_ANY;
247 		sin->sin_port = htons(0);
248 		error = sobind(so, (struct sockaddr *)sin, td);
249 		if (error)
250 			goto bad;
251 		bzero(&sopt, sizeof sopt);
252 		ip = IP_PORTRANGE_DEFAULT;
253 		sopt.sopt_level = IPPROTO_IP;
254 		sopt.sopt_name = IP_PORTRANGE;
255 		sopt.sopt_val = (void *)&ip;
256 		sopt.sopt_valsize = sizeof(ip);
257 		sopt.sopt_td = NULL;
258 		error = sosetopt(so, &sopt);
259 		if (error)
260 			goto bad;
261 	}
262 
263 	/*
264 	 * Protocols that do not require connections may be optionally left
265 	 * unconnected for servers that reply from a port other than NFS_PORT.
266 	 */
267 	if (nmp->nm_flag & NFSMNT_NOCONN) {
268 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
269 			error = ENOTCONN;
270 			goto bad;
271 		}
272 	} else {
273 		error = soconnect(so, nmp->nm_nam, td);
274 		if (error)
275 			goto bad;
276 
277 		/*
278 		 * Wait for the connection to complete. Cribbed from the
279 		 * connect system call but with the wait timing out so
280 		 * that interruptible mounts don't hang here for a long time.
281 		 */
282 		crit_enter();
283 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
284 			(void) tsleep((caddr_t)&so->so_timeo, 0,
285 				"nfscon", 2 * hz);
286 			if ((so->so_state & SS_ISCONNECTING) &&
287 			    so->so_error == 0 && rep &&
288 			    (error = nfs_sigintr(nmp, rep, rep->r_td)) != 0){
289 				so->so_state &= ~SS_ISCONNECTING;
290 				crit_exit();
291 				goto bad;
292 			}
293 		}
294 		if (so->so_error) {
295 			error = so->so_error;
296 			so->so_error = 0;
297 			crit_exit();
298 			goto bad;
299 		}
300 		crit_exit();
301 	}
302 	so->so_rcv.ssb_timeo = (5 * hz);
303 	so->so_snd.ssb_timeo = (5 * hz);
304 
305 	/*
306 	 * Get buffer reservation size from sysctl, but impose reasonable
307 	 * limits.
308 	 */
309 	pktscale = nfs_bufpackets;
310 	if (pktscale < 2)
311 		pktscale = 2;
312 	if (pktscale > 64)
313 		pktscale = 64;
314 
315 	if (nmp->nm_sotype == SOCK_DGRAM) {
316 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
317 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
318 		    NFS_MAXPKTHDR) * pktscale;
319 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
320 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * pktscale;
321 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
322 		    NFS_MAXPKTHDR) * pktscale;
323 	} else {
324 		if (nmp->nm_sotype != SOCK_STREAM)
325 			panic("nfscon sotype");
326 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
327 			struct sockopt sopt;
328 			int val;
329 
330 			bzero(&sopt, sizeof sopt);
331 			sopt.sopt_level = SOL_SOCKET;
332 			sopt.sopt_name = SO_KEEPALIVE;
333 			sopt.sopt_val = &val;
334 			sopt.sopt_valsize = sizeof val;
335 			val = 1;
336 			sosetopt(so, &sopt);
337 		}
338 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
339 			struct sockopt sopt;
340 			int val;
341 
342 			bzero(&sopt, sizeof sopt);
343 			sopt.sopt_level = IPPROTO_TCP;
344 			sopt.sopt_name = TCP_NODELAY;
345 			sopt.sopt_val = &val;
346 			sopt.sopt_valsize = sizeof val;
347 			val = 1;
348 			sosetopt(so, &sopt);
349 		}
350 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
351 		    sizeof (u_int32_t)) * pktscale;
352 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
353 		    sizeof (u_int32_t)) * pktscale;
354 	}
355 	error = soreserve(so, sndreserve, rcvreserve,
356 			  &td->td_proc->p_rlimit[RLIMIT_SBSIZE]);
357 	if (error)
358 		goto bad;
359 	so->so_rcv.ssb_flags |= SSB_NOINTR;
360 	so->so_snd.ssb_flags |= SSB_NOINTR;
361 
362 	/* Initialize other non-zero congestion variables */
363 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] =
364 		nmp->nm_srtt[3] = (NFS_TIMEO << 3);
365 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
366 		nmp->nm_sdrtt[3] = 0;
367 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
368 	nmp->nm_sent = 0;
369 	nmp->nm_timeouts = 0;
370 	return (0);
371 
372 bad:
373 	nfs_disconnect(nmp);
374 	return (error);
375 }
376 
377 /*
378  * Reconnect routine:
379  * Called when a connection is broken on a reliable protocol.
380  * - clean up the old socket
381  * - nfs_connect() again
382  * - set R_MUSTRESEND for all outstanding requests on mount point
383  * If this fails the mount point is DEAD!
384  * nb: Must be called with the nfs_sndlock() set on the mount point.
385  */
386 static int
387 nfs_reconnect(struct nfsreq *rep)
388 {
389 	struct nfsreq *rp;
390 	struct nfsmount *nmp = rep->r_nmp;
391 	int error;
392 
393 	nfs_disconnect(nmp);
394 	while ((error = nfs_connect(nmp, rep)) != 0) {
395 		if (error == EINTR || error == ERESTART)
396 			return (EINTR);
397 		(void) tsleep((caddr_t)&lbolt, 0, "nfscon", 0);
398 	}
399 
400 	/*
401 	 * Loop through outstanding request list and fix up all requests
402 	 * on old socket.
403 	 */
404 	crit_enter();
405 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
406 		if (rp->r_nmp == nmp)
407 			rp->r_flags |= R_MUSTRESEND;
408 	}
409 	crit_exit();
410 	return (0);
411 }
412 
413 /*
414  * NFS disconnect. Clean up and unlink.
415  */
416 void
417 nfs_disconnect(struct nfsmount *nmp)
418 {
419 	struct socket *so;
420 
421 	if (nmp->nm_so) {
422 		so = nmp->nm_so;
423 		nmp->nm_so = NULL;
424 		soshutdown(so, SHUT_RDWR);
425 		soclose(so, FNONBLOCK);
426 	}
427 }
428 
429 void
430 nfs_safedisconnect(struct nfsmount *nmp)
431 {
432 	struct nfsreq dummyreq;
433 
434 	bzero(&dummyreq, sizeof(dummyreq));
435 	dummyreq.r_nmp = nmp;
436 	dummyreq.r_td = NULL;
437 	nfs_rcvlock(&dummyreq);
438 	nfs_disconnect(nmp);
439 	nfs_rcvunlock(&dummyreq);
440 }
441 
442 /*
443  * This is the nfs send routine. For connection based socket types, it
444  * must be called with an nfs_sndlock() on the socket.
445  * "rep == NULL" indicates that it has been called from a server.
446  * For the client side:
447  * - return EINTR if the RPC is terminated, 0 otherwise
448  * - set R_MUSTRESEND if the send fails for any reason
449  * - do any cleanup required by recoverable socket errors (?)
450  * For the server side:
451  * - return EINTR or ERESTART if interrupted by a signal
452  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
453  * - do any cleanup required by recoverable socket errors (?)
454  */
455 int
456 nfs_send(struct socket *so, struct sockaddr *nam, struct mbuf *top,
457 	 struct nfsreq *rep)
458 {
459 	struct sockaddr *sendnam;
460 	int error, soflags, flags;
461 
462 	if (rep) {
463 		if (rep->r_flags & R_SOFTTERM) {
464 			m_freem(top);
465 			return (EINTR);
466 		}
467 		if ((so = rep->r_nmp->nm_so) == NULL) {
468 			rep->r_flags |= R_MUSTRESEND;
469 			m_freem(top);
470 			return (0);
471 		}
472 		rep->r_flags &= ~R_MUSTRESEND;
473 		soflags = rep->r_nmp->nm_soflags;
474 	} else
475 		soflags = so->so_proto->pr_flags;
476 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
477 		sendnam = NULL;
478 	else
479 		sendnam = nam;
480 	if (so->so_type == SOCK_SEQPACKET)
481 		flags = MSG_EOR;
482 	else
483 		flags = 0;
484 
485 	error = so_pru_sosend(so, sendnam, NULL, top, NULL, flags,
486 	    curthread /*XXX*/);
487 	/*
488 	 * ENOBUFS for dgram sockets is transient and non fatal.
489 	 * No need to log, and no need to break a soft mount.
490 	 */
491 	if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
492 		error = 0;
493 		if (rep)		/* do backoff retransmit on client */
494 			rep->r_flags |= R_MUSTRESEND;
495 	}
496 
497 	if (error) {
498 		if (rep) {
499 			log(LOG_INFO, "nfs send error %d for server %s\n",error,
500 			    rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
501 			/*
502 			 * Deal with errors for the client side.
503 			 */
504 			if (rep->r_flags & R_SOFTTERM)
505 				error = EINTR;
506 			else
507 				rep->r_flags |= R_MUSTRESEND;
508 		} else
509 			log(LOG_INFO, "nfsd send error %d\n", error);
510 
511 		/*
512 		 * Handle any recoverable (soft) socket errors here. (?)
513 		 */
514 		if (error != EINTR && error != ERESTART &&
515 			error != EWOULDBLOCK && error != EPIPE)
516 			error = 0;
517 	}
518 	return (error);
519 }
520 
521 /*
522  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
523  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
524  * Mark and consolidate the data into a new mbuf list.
525  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
526  *     small mbufs.
527  * For SOCK_STREAM we must be very careful to read an entire record once
528  * we have read any of it, even if the system call has been interrupted.
529  */
530 static int
531 nfs_receive(struct nfsreq *rep, struct sockaddr **aname, struct mbuf **mp)
532 {
533 	struct socket *so;
534 	struct sockbuf sio;
535 	struct uio auio;
536 	struct iovec aio;
537 	struct mbuf *m;
538 	struct mbuf *control;
539 	u_int32_t len;
540 	struct sockaddr **getnam;
541 	int error, sotype, rcvflg;
542 	struct thread *td = curthread;	/* XXX */
543 
544 	/*
545 	 * Set up arguments for soreceive()
546 	 */
547 	*mp = NULL;
548 	*aname = NULL;
549 	sotype = rep->r_nmp->nm_sotype;
550 
551 	/*
552 	 * For reliable protocols, lock against other senders/receivers
553 	 * in case a reconnect is necessary.
554 	 * For SOCK_STREAM, first get the Record Mark to find out how much
555 	 * more there is to get.
556 	 * We must lock the socket against other receivers
557 	 * until we have an entire rpc request/reply.
558 	 */
559 	if (sotype != SOCK_DGRAM) {
560 		error = nfs_sndlock(rep);
561 		if (error)
562 			return (error);
563 tryagain:
564 		/*
565 		 * Check for fatal errors and resending request.
566 		 */
567 		/*
568 		 * Ugh: If a reconnect attempt just happened, nm_so
569 		 * would have changed. NULL indicates a failed
570 		 * attempt that has essentially shut down this
571 		 * mount point.
572 		 */
573 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
574 			nfs_sndunlock(rep);
575 			return (EINTR);
576 		}
577 		so = rep->r_nmp->nm_so;
578 		if (!so) {
579 			error = nfs_reconnect(rep);
580 			if (error) {
581 				nfs_sndunlock(rep);
582 				return (error);
583 			}
584 			goto tryagain;
585 		}
586 		while (rep->r_flags & R_MUSTRESEND) {
587 			m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_WAIT);
588 			nfsstats.rpcretries++;
589 			error = nfs_send(so, rep->r_nmp->nm_nam, m, rep);
590 			if (error) {
591 				if (error == EINTR || error == ERESTART ||
592 				    (error = nfs_reconnect(rep)) != 0) {
593 					nfs_sndunlock(rep);
594 					return (error);
595 				}
596 				goto tryagain;
597 			}
598 		}
599 		nfs_sndunlock(rep);
600 		if (sotype == SOCK_STREAM) {
601 			/*
602 			 * Get the length marker from the stream
603 			 */
604 			aio.iov_base = (caddr_t)&len;
605 			aio.iov_len = sizeof(u_int32_t);
606 			auio.uio_iov = &aio;
607 			auio.uio_iovcnt = 1;
608 			auio.uio_segflg = UIO_SYSSPACE;
609 			auio.uio_rw = UIO_READ;
610 			auio.uio_offset = 0;
611 			auio.uio_resid = sizeof(u_int32_t);
612 			auio.uio_td = td;
613 			do {
614 			   rcvflg = MSG_WAITALL;
615 			   error = so_pru_soreceive(so, NULL, &auio, NULL,
616 						    NULL, &rcvflg);
617 			   if (error == EWOULDBLOCK && rep) {
618 				if (rep->r_flags & R_SOFTTERM)
619 					return (EINTR);
620 			   }
621 			} while (error == EWOULDBLOCK);
622 
623 			if (error == 0 && auio.uio_resid > 0) {
624 			    /*
625 			     * Only log short packets if not EOF
626 			     */
627 			    if (auio.uio_resid != sizeof(u_int32_t))
628 			    log(LOG_INFO,
629 				 "short receive (%d/%d) from nfs server %s\n",
630 				 (int)(sizeof(u_int32_t) - auio.uio_resid),
631 				 (int)sizeof(u_int32_t),
632 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
633 			    error = EPIPE;
634 			}
635 			if (error)
636 				goto errout;
637 			len = ntohl(len) & ~0x80000000;
638 			/*
639 			 * This is SERIOUS! We are out of sync with the sender
640 			 * and forcing a disconnect/reconnect is all I can do.
641 			 */
642 			if (len > NFS_MAXPACKET) {
643 			    log(LOG_ERR, "%s (%d) from nfs server %s\n",
644 				"impossible packet length",
645 				len,
646 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
647 			    error = EFBIG;
648 			    goto errout;
649 			}
650 
651 			/*
652 			 * Get the rest of the packet as an mbuf chain
653 			 */
654 			sbinit(&sio, len);
655 			do {
656 			    rcvflg = MSG_WAITALL;
657 			    error = so_pru_soreceive(so, NULL, NULL, &sio,
658 						     NULL, &rcvflg);
659 			} while (error == EWOULDBLOCK || error == EINTR ||
660 				 error == ERESTART);
661 			if (error == 0 && sio.sb_cc != len) {
662 			    if (sio.sb_cc != 0)
663 			    log(LOG_INFO,
664 				"short receive (%d/%d) from nfs server %s\n",
665 				len - auio.uio_resid, len,
666 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
667 			    error = EPIPE;
668 			}
669 			*mp = sio.sb_mb;
670 		} else {
671 			/*
672 			 * Non-stream, so get the whole packet by not
673 			 * specifying MSG_WAITALL and by specifying a large
674 			 * length.
675 			 *
676 			 * We have no use for control msg., but must grab them
677 			 * and then throw them away so we know what is going
678 			 * on.
679 			 */
680 			sbinit(&sio, 100000000);
681 			do {
682 			    rcvflg = 0;
683 			    error =  so_pru_soreceive(so, NULL, NULL, &sio,
684 						      &control, &rcvflg);
685 			    if (control)
686 				m_freem(control);
687 			    if (error == EWOULDBLOCK && rep) {
688 				if (rep->r_flags & R_SOFTTERM) {
689 					m_freem(sio.sb_mb);
690 					return (EINTR);
691 				}
692 			    }
693 			} while (error == EWOULDBLOCK ||
694 				 (error == 0 && sio.sb_mb == NULL && control));
695 			if ((rcvflg & MSG_EOR) == 0)
696 				kprintf("Egad!!\n");
697 			if (error == 0 && sio.sb_mb == NULL)
698 				error = EPIPE;
699 			len = sio.sb_cc;
700 			*mp = sio.sb_mb;
701 		}
702 errout:
703 		if (error && error != EINTR && error != ERESTART) {
704 			m_freem(*mp);
705 			*mp = NULL;
706 			if (error != EPIPE) {
707 				log(LOG_INFO,
708 				    "receive error %d from nfs server %s\n",
709 				    error,
710 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
711 			}
712 			error = nfs_sndlock(rep);
713 			if (!error) {
714 				error = nfs_reconnect(rep);
715 				if (!error)
716 					goto tryagain;
717 				else
718 					nfs_sndunlock(rep);
719 			}
720 		}
721 	} else {
722 		if ((so = rep->r_nmp->nm_so) == NULL)
723 			return (EACCES);
724 		if (so->so_state & SS_ISCONNECTED)
725 			getnam = NULL;
726 		else
727 			getnam = aname;
728 		sbinit(&sio, 100000000);
729 		do {
730 			rcvflg = 0;
731 			error =  so_pru_soreceive(so, getnam, NULL, &sio,
732 						  NULL, &rcvflg);
733 			if (error == EWOULDBLOCK &&
734 			    (rep->r_flags & R_SOFTTERM)) {
735 				m_freem(sio.sb_mb);
736 				return (EINTR);
737 			}
738 		} while (error == EWOULDBLOCK);
739 		len = sio.sb_cc;
740 		*mp = sio.sb_mb;
741 	}
742 	if (error) {
743 		m_freem(*mp);
744 		*mp = NULL;
745 	}
746 	/*
747 	 * Search for any mbufs that are not a multiple of 4 bytes long
748 	 * or with m_data not longword aligned.
749 	 * These could cause pointer alignment problems, so copy them to
750 	 * well aligned mbufs.
751 	 */
752 	nfs_realign(mp, 5 * NFSX_UNSIGNED);
753 	return (error);
754 }
755 
756 /*
757  * Implement receipt of reply on a socket.
758  * We must search through the list of received datagrams matching them
759  * with outstanding requests using the xid, until ours is found.
760  */
761 /* ARGSUSED */
762 int
763 nfs_reply(struct nfsreq *myrep)
764 {
765 	struct nfsreq *rep;
766 	struct nfsmount *nmp = myrep->r_nmp;
767 	int32_t t1;
768 	struct mbuf *mrep, *md;
769 	struct sockaddr *nam;
770 	u_int32_t rxid, *tl;
771 	caddr_t dpos, cp2;
772 	int error;
773 
774 	/*
775 	 * Loop around until we get our own reply
776 	 */
777 	for (;;) {
778 		/*
779 		 * Lock against other receivers so that I don't get stuck in
780 		 * sbwait() after someone else has received my reply for me.
781 		 * Also necessary for connection based protocols to avoid
782 		 * race conditions during a reconnect.
783 		 * If nfs_rcvlock() returns EALREADY, that means that
784 		 * the reply has already been recieved by another
785 		 * process and we can return immediately.  In this
786 		 * case, the lock is not taken to avoid races with
787 		 * other processes.
788 		 */
789 		error = nfs_rcvlock(myrep);
790 		if (error == EALREADY)
791 			return (0);
792 		if (error)
793 			return (error);
794 		/*
795 		 * Get the next Rpc reply off the socket
796 		 */
797 		error = nfs_receive(myrep, &nam, &mrep);
798 		nfs_rcvunlock(myrep);
799 		if (error) {
800 			/*
801 			 * Ignore routing errors on connectionless protocols??
802 			 */
803 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
804 				nmp->nm_so->so_error = 0;
805 				if (myrep->r_flags & R_GETONEREP)
806 					return (0);
807 				continue;
808 			}
809 			return (error);
810 		}
811 		if (nam)
812 			FREE(nam, M_SONAME);
813 
814 		/*
815 		 * Get the xid and check that it is an rpc reply
816 		 */
817 		md = mrep;
818 		dpos = mtod(md, caddr_t);
819 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
820 		rxid = *tl++;
821 		if (*tl != rpc_reply) {
822 			nfsstats.rpcinvalid++;
823 			m_freem(mrep);
824 nfsmout:
825 			if (myrep->r_flags & R_GETONEREP)
826 				return (0);
827 			continue;
828 		}
829 
830 		/*
831 		 * Loop through the request list to match up the reply
832 		 * Iff no match, just drop the datagram.  On match, set
833 		 * r_mrep atomically to prevent the timer from messing
834 		 * around with the request after we have exited the critical
835 		 * section.
836 		 */
837 		crit_enter();
838 		TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
839 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
840 				rep->r_mrep = mrep;
841 				break;
842 			}
843 		}
844 		crit_exit();
845 
846 		/*
847 		 * Fill in the rest of the reply if we found a match.
848 		 */
849 		if (rep) {
850 			rep->r_md = md;
851 			rep->r_dpos = dpos;
852 			if (nfsrtton) {
853 				struct rttl *rt;
854 
855 				rt = &nfsrtt.rttl[nfsrtt.pos];
856 				rt->proc = rep->r_procnum;
857 				rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
858 				rt->sent = nmp->nm_sent;
859 				rt->cwnd = nmp->nm_cwnd;
860 				rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
861 				rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
862 				rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
863 				getmicrotime(&rt->tstamp);
864 				if (rep->r_flags & R_TIMING)
865 					rt->rtt = rep->r_rtt;
866 				else
867 					rt->rtt = 1000000;
868 				nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
869 			}
870 			/*
871 			 * Update congestion window.
872 			 * Do the additive increase of
873 			 * one rpc/rtt.
874 			 */
875 			if (nmp->nm_cwnd <= nmp->nm_sent) {
876 				nmp->nm_cwnd +=
877 				   (NFS_CWNDSCALE * NFS_CWNDSCALE +
878 				   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
879 				if (nmp->nm_cwnd > NFS_MAXCWND)
880 					nmp->nm_cwnd = NFS_MAXCWND;
881 			}
882 			crit_enter();	/* nfs_timer interlock for nm_sent */
883 			if (rep->r_flags & R_SENT) {
884 				rep->r_flags &= ~R_SENT;
885 				nmp->nm_sent -= NFS_CWNDSCALE;
886 			}
887 			crit_exit();
888 			/*
889 			 * Update rtt using a gain of 0.125 on the mean
890 			 * and a gain of 0.25 on the deviation.
891 			 */
892 			if (rep->r_flags & R_TIMING) {
893 				/*
894 				 * Since the timer resolution of
895 				 * NFS_HZ is so course, it can often
896 				 * result in r_rtt == 0. Since
897 				 * r_rtt == N means that the actual
898 				 * rtt is between N+dt and N+2-dt ticks,
899 				 * add 1.
900 				 */
901 				t1 = rep->r_rtt + 1;
902 				t1 -= (NFS_SRTT(rep) >> 3);
903 				NFS_SRTT(rep) += t1;
904 				if (t1 < 0)
905 					t1 = -t1;
906 				t1 -= (NFS_SDRTT(rep) >> 2);
907 				NFS_SDRTT(rep) += t1;
908 			}
909 			nmp->nm_timeouts = 0;
910 		}
911 		/*
912 		 * If not matched to a request, drop it.
913 		 * If it's mine, get out.
914 		 */
915 		if (rep == NULL) {
916 			nfsstats.rpcunexpected++;
917 			m_freem(mrep);
918 		} else if (rep == myrep) {
919 			if (rep->r_mrep == NULL)
920 				panic("nfsreply nil");
921 			return (0);
922 		}
923 		if (myrep->r_flags & R_GETONEREP)
924 			return (0);
925 	}
926 }
927 
928 /*
929  * nfs_request - goes something like this
930  *	- fill in request struct
931  *	- links it into list
932  *	- calls nfs_send() for first transmit
933  *	- calls nfs_receive() to get reply
934  *	- break down rpc header and return with nfs reply pointed to
935  *	  by mrep or error
936  * nb: always frees up mreq mbuf list
937  */
938 int
939 nfs_request(struct vnode *vp, struct mbuf *mrest, int procnum,
940 	    struct thread *td, struct ucred *cred, struct mbuf **mrp,
941 	    struct mbuf **mdp, caddr_t *dposp)
942 {
943 	struct mbuf *mrep, *m2;
944 	struct nfsreq *rep;
945 	u_int32_t *tl;
946 	int i;
947 	struct nfsmount *nmp;
948 	struct mbuf *m, *md, *mheadend;
949 	char nickv[RPCX_NICKVERF];
950 	time_t waituntil;
951 	caddr_t dpos, cp2;
952 	int t1, error = 0, mrest_len, auth_len, auth_type;
953 	int trylater_delay = 15, trylater_cnt = 0, failed_auth = 0;
954 	int verf_len, verf_type;
955 	u_int32_t xid;
956 	char *auth_str, *verf_str;
957 	NFSKERBKEY_T key;		/* save session key */
958 
959 	/* Reject requests while attempting a forced unmount. */
960 	if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
961 		m_freem(mrest);
962 		return (ESTALE);
963 	}
964 	nmp = VFSTONFS(vp->v_mount);
965 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
966 	rep->r_nmp = nmp;
967 	rep->r_vp = vp;
968 	rep->r_td = td;
969 	rep->r_procnum = procnum;
970 	rep->r_mreq = NULL;
971 	i = 0;
972 	m = mrest;
973 	while (m) {
974 		i += m->m_len;
975 		m = m->m_next;
976 	}
977 	mrest_len = i;
978 
979 	/*
980 	 * Get the RPC header with authorization.
981 	 */
982 kerbauth:
983 	verf_str = auth_str = NULL;
984 	if (nmp->nm_flag & NFSMNT_KERB) {
985 		verf_str = nickv;
986 		verf_len = sizeof (nickv);
987 		auth_type = RPCAUTH_KERB4;
988 		bzero((caddr_t)key, sizeof (key));
989 		if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
990 			&auth_len, verf_str, verf_len)) {
991 			error = nfs_getauth(nmp, rep, cred, &auth_str,
992 				&auth_len, verf_str, &verf_len, key);
993 			if (error) {
994 				kfree((caddr_t)rep, M_NFSREQ);
995 				m_freem(mrest);
996 				return (error);
997 			}
998 		}
999 	} else {
1000 		auth_type = RPCAUTH_UNIX;
1001 		if (cred->cr_ngroups < 1)
1002 			panic("nfsreq nogrps");
1003 		auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
1004 			nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
1005 			5 * NFSX_UNSIGNED;
1006 	}
1007 	m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
1008 	     auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
1009 	if (auth_str)
1010 		kfree(auth_str, M_TEMP);
1011 
1012 	/*
1013 	 * For stream protocols, insert a Sun RPC Record Mark.
1014 	 */
1015 	if (nmp->nm_sotype == SOCK_STREAM) {
1016 		M_PREPEND(m, NFSX_UNSIGNED, MB_WAIT);
1017 		if (m == NULL) {
1018 			kfree(rep, M_NFSREQ);
1019 			return (ENOBUFS);
1020 		}
1021 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
1022 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
1023 	}
1024 	rep->r_mreq = m;
1025 	rep->r_xid = xid;
1026 tryagain:
1027 	if (nmp->nm_flag & NFSMNT_SOFT)
1028 		rep->r_retry = nmp->nm_retry;
1029 	else
1030 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
1031 	rep->r_rtt = rep->r_rexmit = 0;
1032 	if (proct[procnum] > 0)
1033 		rep->r_flags = R_TIMING | R_MASKTIMER;
1034 	else
1035 		rep->r_flags = R_MASKTIMER;
1036 	rep->r_mrep = NULL;
1037 
1038 	/*
1039 	 * Do the client side RPC.
1040 	 */
1041 	nfsstats.rpcrequests++;
1042 
1043 	/*
1044 	 * Chain request into list of outstanding requests. Be sure
1045 	 * to put it LAST so timer finds oldest requests first.  Note
1046 	 * that R_MASKTIMER is set at the moment to prevent any timer
1047 	 * action on this request while we are still doing processing on
1048 	 * it below.  splsoftclock() primarily protects nm_sent.  Note
1049 	 * that we may block in this code so there is no atomicy guarentee.
1050 	 */
1051 	crit_enter();
1052 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
1053 
1054 	/*
1055 	 * If backing off another request or avoiding congestion, don't
1056 	 * send this one now but let timer do it.  If not timing a request,
1057 	 * do it now.
1058 	 *
1059 	 * Even though the timer will not mess with our request there is
1060 	 * still the possibility that we will race a reply (which clears
1061 	 * R_SENT), especially on localhost connections, so be very careful
1062 	 * when setting R_SENT.  We could set R_SENT prior to calling
1063 	 * nfs_send() but why bother if the response occurs that quickly?
1064 	 */
1065 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
1066 	    (nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1067 	    nmp->nm_sent < nmp->nm_cwnd)) {
1068 		if (nmp->nm_soflags & PR_CONNREQUIRED)
1069 			error = nfs_sndlock(rep);
1070 		if (!error) {
1071 			m2 = m_copym(m, 0, M_COPYALL, MB_WAIT);
1072 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m2, rep);
1073 			if (nmp->nm_soflags & PR_CONNREQUIRED)
1074 				nfs_sndunlock(rep);
1075 		}
1076 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0 &&
1077 		    rep->r_mrep == NULL) {
1078 			KASSERT((rep->r_flags & R_SENT) == 0,
1079 				("R_SENT ASSERT %p", rep));
1080 			nmp->nm_sent += NFS_CWNDSCALE;
1081 			rep->r_flags |= R_SENT;
1082 		}
1083 	} else {
1084 		rep->r_rtt = -1;
1085 	}
1086 
1087 	/*
1088 	 * Let the timer do what it will with the request, then
1089 	 * wait for the reply from our send or the timer's.
1090 	 */
1091 	if (!error || error == EPIPE) {
1092 		rep->r_flags &= ~R_MASKTIMER;
1093 		crit_exit();
1094 		error = nfs_reply(rep);
1095 		crit_enter();
1096 	}
1097 
1098 	/*
1099 	 * RPC done, unlink the request, but don't rip it out from under
1100 	 * the callout timer.
1101 	 */
1102 	while (rep->r_flags & R_LOCKED) {
1103 		nfs_timer_raced = 1;
1104 		tsleep(&nfs_timer_raced, 0, "nfstrac", 0);
1105 	}
1106 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
1107 
1108 	/*
1109 	 * Decrement the outstanding request count.
1110 	 */
1111 	if (rep->r_flags & R_SENT) {
1112 		rep->r_flags &= ~R_SENT;
1113 		nmp->nm_sent -= NFS_CWNDSCALE;
1114 	}
1115 	crit_exit();
1116 
1117 	/*
1118 	 * If there was a successful reply and a tprintf msg.
1119 	 * tprintf a response.
1120 	 */
1121 	if (!error && (rep->r_flags & R_TPRINTFMSG))
1122 		nfs_msg(rep->r_td, nmp->nm_mountp->mnt_stat.f_mntfromname,
1123 		    "is alive again");
1124 	mrep = rep->r_mrep;
1125 	md = rep->r_md;
1126 	dpos = rep->r_dpos;
1127 	if (error) {
1128 		m_freem(rep->r_mreq);
1129 		kfree((caddr_t)rep, M_NFSREQ);
1130 		return (error);
1131 	}
1132 
1133 	/*
1134 	 * break down the rpc header and check if ok
1135 	 */
1136 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1137 	if (*tl++ == rpc_msgdenied) {
1138 		if (*tl == rpc_mismatch)
1139 			error = EOPNOTSUPP;
1140 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1141 			if (!failed_auth) {
1142 				failed_auth++;
1143 				mheadend->m_next = NULL;
1144 				m_freem(mrep);
1145 				m_freem(rep->r_mreq);
1146 				goto kerbauth;
1147 			} else
1148 				error = EAUTH;
1149 		} else
1150 			error = EACCES;
1151 		m_freem(mrep);
1152 		m_freem(rep->r_mreq);
1153 		kfree((caddr_t)rep, M_NFSREQ);
1154 		return (error);
1155 	}
1156 
1157 	/*
1158 	 * Grab any Kerberos verifier, otherwise just throw it away.
1159 	 */
1160 	verf_type = fxdr_unsigned(int, *tl++);
1161 	i = fxdr_unsigned(int32_t, *tl);
1162 	if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
1163 		error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
1164 		if (error)
1165 			goto nfsmout;
1166 	} else if (i > 0)
1167 		nfsm_adv(nfsm_rndup(i));
1168 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1169 	/* 0 == ok */
1170 	if (*tl == 0) {
1171 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
1172 		if (*tl != 0) {
1173 			error = fxdr_unsigned(int, *tl);
1174 			if ((nmp->nm_flag & NFSMNT_NFSV3) &&
1175 				error == NFSERR_TRYLATER) {
1176 				m_freem(mrep);
1177 				error = 0;
1178 				waituntil = time_second + trylater_delay;
1179 				while (time_second < waituntil)
1180 					(void) tsleep((caddr_t)&lbolt,
1181 						0, "nqnfstry", 0);
1182 				trylater_delay *= nfs_backoff[trylater_cnt];
1183 				if (trylater_cnt < 7)
1184 					trylater_cnt++;
1185 				goto tryagain;
1186 			}
1187 
1188 			/*
1189 			 * If the File Handle was stale, invalidate the
1190 			 * lookup cache, just in case.
1191 			 *
1192 			 * To avoid namecache<->vnode deadlocks we must
1193 			 * release the vnode lock if we hold it.
1194 			 */
1195 			if (error == ESTALE) {
1196 				int ltype;
1197 
1198 				ltype = lockstatus(&vp->v_lock, curthread);
1199 				if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1200 					lockmgr(&vp->v_lock, LK_RELEASE);
1201 				cache_inval_vp(vp, CINV_CHILDREN);
1202 				if (ltype == LK_EXCLUSIVE || ltype == LK_SHARED)
1203 					lockmgr(&vp->v_lock, ltype);
1204 			}
1205 			if (nmp->nm_flag & NFSMNT_NFSV3) {
1206 				*mrp = mrep;
1207 				*mdp = md;
1208 				*dposp = dpos;
1209 				error |= NFSERR_RETERR;
1210 			} else
1211 				m_freem(mrep);
1212 			m_freem(rep->r_mreq);
1213 			kfree((caddr_t)rep, M_NFSREQ);
1214 			return (error);
1215 		}
1216 
1217 		*mrp = mrep;
1218 		*mdp = md;
1219 		*dposp = dpos;
1220 		m_freem(rep->r_mreq);
1221 		FREE((caddr_t)rep, M_NFSREQ);
1222 		return (0);
1223 	}
1224 	m_freem(mrep);
1225 	error = EPROTONOSUPPORT;
1226 nfsmout:
1227 	m_freem(rep->r_mreq);
1228 	kfree((caddr_t)rep, M_NFSREQ);
1229 	return (error);
1230 }
1231 
1232 #ifndef NFS_NOSERVER
1233 /*
1234  * Generate the rpc reply header
1235  * siz arg. is used to decide if adding a cluster is worthwhile
1236  */
1237 int
1238 nfs_rephead(int siz, struct nfsrv_descript *nd, struct nfssvc_sock *slp,
1239 	    int err, struct mbuf **mrq, struct mbuf **mbp, caddr_t *bposp)
1240 {
1241 	u_int32_t *tl;
1242 	struct mbuf *mreq;
1243 	caddr_t bpos;
1244 	struct mbuf *mb, *mb2;
1245 
1246 	siz += RPC_REPLYSIZ;
1247 	mb = mreq = m_getl(max_hdr + siz, MB_WAIT, MT_DATA, M_PKTHDR, NULL);
1248 	mreq->m_pkthdr.len = 0;
1249 	/*
1250 	 * If this is not a cluster, try and leave leading space
1251 	 * for the lower level headers.
1252 	 */
1253 	if ((max_hdr + siz) < MINCLSIZE)
1254 		mreq->m_data += max_hdr;
1255 	tl = mtod(mreq, u_int32_t *);
1256 	mreq->m_len = 6 * NFSX_UNSIGNED;
1257 	bpos = ((caddr_t)tl) + mreq->m_len;
1258 	*tl++ = txdr_unsigned(nd->nd_retxid);
1259 	*tl++ = rpc_reply;
1260 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
1261 		*tl++ = rpc_msgdenied;
1262 		if (err & NFSERR_AUTHERR) {
1263 			*tl++ = rpc_autherr;
1264 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
1265 			mreq->m_len -= NFSX_UNSIGNED;
1266 			bpos -= NFSX_UNSIGNED;
1267 		} else {
1268 			*tl++ = rpc_mismatch;
1269 			*tl++ = txdr_unsigned(RPC_VER2);
1270 			*tl = txdr_unsigned(RPC_VER2);
1271 		}
1272 	} else {
1273 		*tl++ = rpc_msgaccepted;
1274 
1275 		/*
1276 		 * For Kerberos authentication, we must send the nickname
1277 		 * verifier back, otherwise just RPCAUTH_NULL.
1278 		 */
1279 		if (nd->nd_flag & ND_KERBFULL) {
1280 		    struct nfsuid *nuidp;
1281 		    struct timeval ktvin, ktvout;
1282 
1283 		    for (nuidp = NUIDHASH(slp, nd->nd_cr.cr_uid)->lh_first;
1284 			nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1285 			if (nuidp->nu_cr.cr_uid == nd->nd_cr.cr_uid &&
1286 			    (!nd->nd_nam2 || netaddr_match(NU_NETFAM(nuidp),
1287 			     &nuidp->nu_haddr, nd->nd_nam2)))
1288 			    break;
1289 		    }
1290 		    if (nuidp) {
1291 			ktvin.tv_sec =
1292 			    txdr_unsigned(nuidp->nu_timestamp.tv_sec - 1);
1293 			ktvin.tv_usec =
1294 			    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1295 
1296 			/*
1297 			 * Encrypt the timestamp in ecb mode using the
1298 			 * session key.
1299 			 */
1300 #ifdef NFSKERB
1301 			XXX
1302 #endif
1303 
1304 			*tl++ = rpc_auth_kerb;
1305 			*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
1306 			*tl = ktvout.tv_sec;
1307 			nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1308 			*tl++ = ktvout.tv_usec;
1309 			*tl++ = txdr_unsigned(nuidp->nu_cr.cr_uid);
1310 		    } else {
1311 			*tl++ = 0;
1312 			*tl++ = 0;
1313 		    }
1314 		} else {
1315 			*tl++ = 0;
1316 			*tl++ = 0;
1317 		}
1318 		switch (err) {
1319 		case EPROGUNAVAIL:
1320 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
1321 			break;
1322 		case EPROGMISMATCH:
1323 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
1324 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1325 			*tl++ = txdr_unsigned(2);
1326 			*tl = txdr_unsigned(3);
1327 			break;
1328 		case EPROCUNAVAIL:
1329 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
1330 			break;
1331 		case EBADRPC:
1332 			*tl = txdr_unsigned(RPC_GARBAGE);
1333 			break;
1334 		default:
1335 			*tl = 0;
1336 			if (err != NFSERR_RETVOID) {
1337 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1338 				if (err)
1339 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
1340 				else
1341 				    *tl = 0;
1342 			}
1343 			break;
1344 		};
1345 	}
1346 
1347 	if (mrq != NULL)
1348 	    *mrq = mreq;
1349 	*mbp = mb;
1350 	*bposp = bpos;
1351 	if (err != 0 && err != NFSERR_RETVOID)
1352 		nfsstats.srvrpc_errs++;
1353 	return (0);
1354 }
1355 
1356 
1357 #endif /* NFS_NOSERVER */
1358 /*
1359  * Nfs timer routine
1360  * Scan the nfsreq list and retranmit any requests that have timed out
1361  * To avoid retransmission attempts on STREAM sockets (in the future) make
1362  * sure to set the r_retry field to 0 (implies nm_retry == 0).
1363  */
1364 void
1365 nfs_timer(void *arg /* never used */)
1366 {
1367 	struct nfsreq *rep;
1368 	struct mbuf *m;
1369 	struct socket *so;
1370 	struct nfsmount *nmp;
1371 	int timeo;
1372 	int error;
1373 #ifndef NFS_NOSERVER
1374 	struct nfssvc_sock *slp;
1375 	u_quad_t cur_usec;
1376 #endif /* NFS_NOSERVER */
1377 	struct thread *td = &thread0; /* XXX for credentials, will break if sleep */
1378 
1379 	crit_enter();
1380 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
1381 		nmp = rep->r_nmp;
1382 		if (rep->r_mrep || (rep->r_flags & (R_SOFTTERM|R_MASKTIMER)))
1383 			continue;
1384 		rep->r_flags |= R_LOCKED;
1385 		if (nfs_sigintr(nmp, rep, rep->r_td)) {
1386 			nfs_softterm(rep);
1387 			goto skip;
1388 		}
1389 		if (rep->r_rtt >= 0) {
1390 			rep->r_rtt++;
1391 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1392 				timeo = nmp->nm_timeo;
1393 			else
1394 				timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1395 			if (nmp->nm_timeouts > 0)
1396 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1397 			if (rep->r_rtt <= timeo)
1398 				goto skip;
1399 			if (nmp->nm_timeouts < 8)
1400 				nmp->nm_timeouts++;
1401 		}
1402 		/*
1403 		 * Check for server not responding
1404 		 */
1405 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1406 		     rep->r_rexmit > nmp->nm_deadthresh) {
1407 			nfs_msg(rep->r_td,
1408 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
1409 			    "not responding");
1410 			rep->r_flags |= R_TPRINTFMSG;
1411 		}
1412 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
1413 			nfsstats.rpctimeouts++;
1414 			nfs_softterm(rep);
1415 			goto skip;
1416 		}
1417 		if (nmp->nm_sotype != SOCK_DGRAM) {
1418 			if (++rep->r_rexmit > NFS_MAXREXMIT)
1419 				rep->r_rexmit = NFS_MAXREXMIT;
1420 			goto skip;
1421 		}
1422 		if ((so = nmp->nm_so) == NULL)
1423 			goto skip;
1424 
1425 		/*
1426 		 * If there is enough space and the window allows..
1427 		 *	Resend it
1428 		 * Set r_rtt to -1 in case we fail to send it now.
1429 		 */
1430 		rep->r_rtt = -1;
1431 		if (ssb_space(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1432 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1433 		    (rep->r_flags & R_SENT) ||
1434 		    nmp->nm_sent < nmp->nm_cwnd) &&
1435 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, MB_DONTWAIT))){
1436 			if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1437 			    error = so_pru_send(so, 0, m, NULL, NULL, td);
1438 			else
1439 			    error = so_pru_send(so, 0, m, nmp->nm_nam,
1440 			        NULL, td);
1441 			if (error) {
1442 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1443 					so->so_error = 0;
1444 			} else if (rep->r_mrep == NULL) {
1445 				/*
1446 				 * Iff first send, start timing
1447 				 * else turn timing off, backoff timer
1448 				 * and divide congestion window by 2.
1449 				 *
1450 				 * It is possible for the so_pru_send() to
1451 				 * block and for us to race a reply so we
1452 				 * only do this if the reply field has not
1453 				 * been filled in.  R_LOCKED will prevent
1454 				 * the request from being ripped out from under
1455 				 * us entirely.
1456 				 */
1457 				if (rep->r_flags & R_SENT) {
1458 					rep->r_flags &= ~R_TIMING;
1459 					if (++rep->r_rexmit > NFS_MAXREXMIT)
1460 						rep->r_rexmit = NFS_MAXREXMIT;
1461 					nmp->nm_cwnd >>= 1;
1462 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
1463 						nmp->nm_cwnd = NFS_CWNDSCALE;
1464 					nfsstats.rpcretries++;
1465 				} else {
1466 					rep->r_flags |= R_SENT;
1467 					nmp->nm_sent += NFS_CWNDSCALE;
1468 				}
1469 				rep->r_rtt = 0;
1470 			}
1471 		}
1472 skip:
1473 		rep->r_flags &= ~R_LOCKED;
1474 	}
1475 #ifndef NFS_NOSERVER
1476 
1477 	/*
1478 	 * Scan the write gathering queues for writes that need to be
1479 	 * completed now.
1480 	 */
1481 	cur_usec = nfs_curusec();
1482 	TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
1483 	    if (slp->ns_tq.lh_first && slp->ns_tq.lh_first->nd_time<=cur_usec)
1484 		nfsrv_wakenfsd(slp, 1);
1485 	}
1486 #endif /* NFS_NOSERVER */
1487 
1488 	/*
1489 	 * Due to possible blocking, a client operation may be waiting for
1490 	 * us to finish processing this request so it can remove it.
1491 	 */
1492 	if (nfs_timer_raced) {
1493 		nfs_timer_raced = 0;
1494 		wakeup(&nfs_timer_raced);
1495 	}
1496 	crit_exit();
1497 	callout_reset(&nfs_timer_handle, nfs_ticks, nfs_timer, NULL);
1498 }
1499 
1500 /*
1501  * Mark all of an nfs mount's outstanding requests with R_SOFTTERM and
1502  * wait for all requests to complete. This is used by forced unmounts
1503  * to terminate any outstanding RPCs.
1504  */
1505 int
1506 nfs_nmcancelreqs(struct nfsmount *nmp)
1507 {
1508 	struct nfsreq *req;
1509 	int i;
1510 
1511 	crit_enter();
1512 	TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1513 		if (nmp != req->r_nmp || req->r_mrep != NULL ||
1514 		    (req->r_flags & R_SOFTTERM)) {
1515 			continue;
1516 		}
1517 		nfs_softterm(req);
1518 	}
1519 	crit_exit();
1520 
1521 	for (i = 0; i < 30; i++) {
1522 		crit_enter();
1523 		TAILQ_FOREACH(req, &nfs_reqq, r_chain) {
1524 			if (nmp == req->r_nmp)
1525 				break;
1526 		}
1527 		crit_exit();
1528 		if (req == NULL)
1529 			return (0);
1530 		tsleep(&lbolt, 0, "nfscancel", 0);
1531 	}
1532 	return (EBUSY);
1533 }
1534 
1535 /*
1536  * Flag a request as being about to terminate (due to NFSMNT_INT/NFSMNT_SOFT).
1537  * The nm_send count is decremented now to avoid deadlocks when the process in
1538  * soreceive() hasn't yet managed to send its own request.
1539  *
1540  * This routine must be called at splsoftclock() to protect r_flags and
1541  * nm_sent.
1542  */
1543 
1544 static void
1545 nfs_softterm(struct nfsreq *rep)
1546 {
1547 	rep->r_flags |= R_SOFTTERM;
1548 
1549 	if (rep->r_flags & R_SENT) {
1550 		rep->r_nmp->nm_sent -= NFS_CWNDSCALE;
1551 		rep->r_flags &= ~R_SENT;
1552 	}
1553 }
1554 
1555 /*
1556  * Test for a termination condition pending on the process.
1557  * This is used for NFSMNT_INT mounts.
1558  */
1559 int
1560 nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct thread *td)
1561 {
1562 	sigset_t tmpset;
1563 	struct proc *p;
1564 	struct lwp *lp;
1565 
1566 	if (rep && (rep->r_flags & R_SOFTTERM))
1567 		return (EINTR);
1568 	/* Terminate all requests while attempting a forced unmount. */
1569 	if (nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF)
1570 		return (EINTR);
1571 	if (!(nmp->nm_flag & NFSMNT_INT))
1572 		return (0);
1573 	/* td might be NULL YYY */
1574 	if (td == NULL || (p = td->td_proc) == NULL)
1575 		return (0);
1576 
1577 	lp = td->td_lwp;
1578 	tmpset = lwp_sigpend(lp);
1579 	SIGSETNAND(tmpset, lp->lwp_sigmask);
1580 	SIGSETNAND(tmpset, p->p_sigignore);
1581 	if (SIGNOTEMPTY(tmpset) && NFSINT_SIGMASK(tmpset))
1582 		return (EINTR);
1583 
1584 	return (0);
1585 }
1586 
1587 /*
1588  * Lock a socket against others.
1589  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1590  * and also to avoid race conditions between the processes with nfs requests
1591  * in progress when a reconnect is necessary.
1592  */
1593 int
1594 nfs_sndlock(struct nfsreq *rep)
1595 {
1596 	int *statep = &rep->r_nmp->nm_state;
1597 	struct thread *td;
1598 	int slptimeo;
1599 	int slpflag;
1600 	int error;
1601 
1602 	slpflag = 0;
1603 	slptimeo = 0;
1604 	td = rep->r_td;
1605 	if (rep->r_nmp->nm_flag & NFSMNT_INT)
1606 		slpflag = PCATCH;
1607 
1608 	error = 0;
1609 	crit_enter();
1610 	while (*statep & NFSSTA_SNDLOCK) {
1611 		*statep |= NFSSTA_WANTSND;
1612 		if (nfs_sigintr(rep->r_nmp, rep, td)) {
1613 			error = EINTR;
1614 			break;
1615 		}
1616 		tsleep((caddr_t)statep, slpflag, "nfsndlck", slptimeo);
1617 		if (slpflag == PCATCH) {
1618 			slpflag = 0;
1619 			slptimeo = 2 * hz;
1620 		}
1621 	}
1622 	/* Always fail if our request has been cancelled. */
1623 	if ((rep->r_flags & R_SOFTTERM))
1624 		error = EINTR;
1625 	if (error == 0)
1626 		*statep |= NFSSTA_SNDLOCK;
1627 	crit_exit();
1628 	return (error);
1629 }
1630 
1631 /*
1632  * Unlock the stream socket for others.
1633  */
1634 void
1635 nfs_sndunlock(struct nfsreq *rep)
1636 {
1637 	int *statep = &rep->r_nmp->nm_state;
1638 
1639 	if ((*statep & NFSSTA_SNDLOCK) == 0)
1640 		panic("nfs sndunlock");
1641 	crit_enter();
1642 	*statep &= ~NFSSTA_SNDLOCK;
1643 	if (*statep & NFSSTA_WANTSND) {
1644 		*statep &= ~NFSSTA_WANTSND;
1645 		wakeup((caddr_t)statep);
1646 	}
1647 	crit_exit();
1648 }
1649 
1650 static int
1651 nfs_rcvlock(struct nfsreq *rep)
1652 {
1653 	int *statep = &rep->r_nmp->nm_state;
1654 	int slpflag;
1655 	int slptimeo;
1656 	int error;
1657 
1658 	/*
1659 	 * Unconditionally check for completion in case another nfsiod
1660 	 * get the packet while the caller was blocked, before the caller
1661 	 * called us.  Packet reception is handled by mainline code which
1662 	 * is protected by the BGL at the moment.
1663 	 *
1664 	 * We do not strictly need the second check just before the
1665 	 * tsleep(), but it's good defensive programming.
1666 	 */
1667 	if (rep->r_mrep != NULL)
1668 		return (EALREADY);
1669 
1670 	if (rep->r_nmp->nm_flag & NFSMNT_INT)
1671 		slpflag = PCATCH;
1672 	else
1673 		slpflag = 0;
1674 	slptimeo = 0;
1675 	error = 0;
1676 	crit_enter();
1677 	while (*statep & NFSSTA_RCVLOCK) {
1678 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_td)) {
1679 			error = EINTR;
1680 			break;
1681 		}
1682 		if (rep->r_mrep != NULL) {
1683 			error = EALREADY;
1684 			break;
1685 		}
1686 		*statep |= NFSSTA_WANTRCV;
1687 		tsleep((caddr_t)statep, slpflag, "nfsrcvlk", slptimeo);
1688 		/*
1689 		 * If our reply was recieved while we were sleeping,
1690 		 * then just return without taking the lock to avoid a
1691 		 * situation where a single iod could 'capture' the
1692 		 * recieve lock.
1693 		 */
1694 		if (rep->r_mrep != NULL) {
1695 			error = EALREADY;
1696 			break;
1697 		}
1698 		if (slpflag == PCATCH) {
1699 			slpflag = 0;
1700 			slptimeo = 2 * hz;
1701 		}
1702 	}
1703 	if (error == 0) {
1704 		*statep |= NFSSTA_RCVLOCK;
1705 		rep->r_nmp->nm_rcvlock_td = curthread;	/* DEBUGGING */
1706 	}
1707 	crit_exit();
1708 	return (error);
1709 }
1710 
1711 /*
1712  * Unlock the stream socket for others.
1713  */
1714 static void
1715 nfs_rcvunlock(struct nfsreq *rep)
1716 {
1717 	int *statep = &rep->r_nmp->nm_state;
1718 
1719 	if ((*statep & NFSSTA_RCVLOCK) == 0)
1720 		panic("nfs rcvunlock");
1721 	crit_enter();
1722 	rep->r_nmp->nm_rcvlock_td = (void *)-1;	/* DEBUGGING */
1723 	*statep &= ~NFSSTA_RCVLOCK;
1724 	if (*statep & NFSSTA_WANTRCV) {
1725 		*statep &= ~NFSSTA_WANTRCV;
1726 		wakeup((caddr_t)statep);
1727 	}
1728 	crit_exit();
1729 }
1730 
1731 /*
1732  *	nfs_realign:
1733  *
1734  *	Check for badly aligned mbuf data and realign by copying the unaligned
1735  *	portion of the data into a new mbuf chain and freeing the portions
1736  *	of the old chain that were replaced.
1737  *
1738  *	We cannot simply realign the data within the existing mbuf chain
1739  *	because the underlying buffers may contain other rpc commands and
1740  *	we cannot afford to overwrite them.
1741  *
1742  *	We would prefer to avoid this situation entirely.  The situation does
1743  *	not occur with NFS/UDP and is supposed to only occassionally occur
1744  *	with TCP.  Use vfs.nfs.realign_count and realign_test to check this.
1745  */
1746 static void
1747 nfs_realign(struct mbuf **pm, int hsiz)
1748 {
1749 	struct mbuf *m;
1750 	struct mbuf *n = NULL;
1751 	int off = 0;
1752 
1753 	++nfs_realign_test;
1754 
1755 	while ((m = *pm) != NULL) {
1756 		if ((m->m_len & 0x3) || (mtod(m, intptr_t) & 0x3)) {
1757 			n = m_getl(m->m_len, MB_WAIT, MT_DATA, 0, NULL);
1758 			n->m_len = 0;
1759 			break;
1760 		}
1761 		pm = &m->m_next;
1762 	}
1763 
1764 	/*
1765 	 * If n is non-NULL, loop on m copying data, then replace the
1766 	 * portion of the chain that had to be realigned.
1767 	 */
1768 	if (n != NULL) {
1769 		++nfs_realign_count;
1770 		while (m) {
1771 			m_copyback(n, off, m->m_len, mtod(m, caddr_t));
1772 			off += m->m_len;
1773 			m = m->m_next;
1774 		}
1775 		m_freem(*pm);
1776 		*pm = n;
1777 	}
1778 }
1779 
1780 #ifndef NFS_NOSERVER
1781 
1782 /*
1783  * Parse an RPC request
1784  * - verify it
1785  * - fill in the cred struct.
1786  */
1787 int
1788 nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
1789 {
1790 	int len, i;
1791 	u_int32_t *tl;
1792 	int32_t t1;
1793 	struct uio uio;
1794 	struct iovec iov;
1795 	caddr_t dpos, cp2, cp;
1796 	u_int32_t nfsvers, auth_type;
1797 	uid_t nickuid;
1798 	int error = 0, ticklen;
1799 	struct mbuf *mrep, *md;
1800 	struct nfsuid *nuidp;
1801 	struct timeval tvin, tvout;
1802 #if 0				/* until encrypted keys are implemented */
1803 	NFSKERBKEYSCHED_T keys;	/* stores key schedule */
1804 #endif
1805 
1806 	mrep = nd->nd_mrep;
1807 	md = nd->nd_md;
1808 	dpos = nd->nd_dpos;
1809 	if (has_header) {
1810 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
1811 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
1812 		if (*tl++ != rpc_call) {
1813 			m_freem(mrep);
1814 			return (EBADRPC);
1815 		}
1816 	} else
1817 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
1818 	nd->nd_repstat = 0;
1819 	nd->nd_flag = 0;
1820 	if (*tl++ != rpc_vers) {
1821 		nd->nd_repstat = ERPCMISMATCH;
1822 		nd->nd_procnum = NFSPROC_NOOP;
1823 		return (0);
1824 	}
1825 	if (*tl != nfs_prog) {
1826 		nd->nd_repstat = EPROGUNAVAIL;
1827 		nd->nd_procnum = NFSPROC_NOOP;
1828 		return (0);
1829 	}
1830 	tl++;
1831 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
1832 	if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
1833 		nd->nd_repstat = EPROGMISMATCH;
1834 		nd->nd_procnum = NFSPROC_NOOP;
1835 		return (0);
1836 	}
1837 	if (nfsvers == NFS_VER3)
1838 		nd->nd_flag = ND_NFSV3;
1839 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
1840 	if (nd->nd_procnum == NFSPROC_NULL)
1841 		return (0);
1842 	if (nd->nd_procnum >= NFS_NPROCS ||
1843 		(nd->nd_procnum >= NQNFSPROC_GETLEASE) ||
1844 		(!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
1845 		nd->nd_repstat = EPROCUNAVAIL;
1846 		nd->nd_procnum = NFSPROC_NOOP;
1847 		return (0);
1848 	}
1849 	if ((nd->nd_flag & ND_NFSV3) == 0)
1850 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
1851 	auth_type = *tl++;
1852 	len = fxdr_unsigned(int, *tl++);
1853 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
1854 		m_freem(mrep);
1855 		return (EBADRPC);
1856 	}
1857 
1858 	nd->nd_flag &= ~ND_KERBAUTH;
1859 	/*
1860 	 * Handle auth_unix or auth_kerb.
1861 	 */
1862 	if (auth_type == rpc_auth_unix) {
1863 		len = fxdr_unsigned(int, *++tl);
1864 		if (len < 0 || len > NFS_MAXNAMLEN) {
1865 			m_freem(mrep);
1866 			return (EBADRPC);
1867 		}
1868 		nfsm_adv(nfsm_rndup(len));
1869 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1870 		bzero((caddr_t)&nd->nd_cr, sizeof (struct ucred));
1871 		nd->nd_cr.cr_ref = 1;
1872 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1873 		nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1874 		len = fxdr_unsigned(int, *tl);
1875 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1876 			m_freem(mrep);
1877 			return (EBADRPC);
1878 		}
1879 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
1880 		for (i = 1; i <= len; i++)
1881 		    if (i < NGROUPS)
1882 			nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1883 		    else
1884 			tl++;
1885 		nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
1886 		if (nd->nd_cr.cr_ngroups > 1)
1887 		    nfsrvw_sort(nd->nd_cr.cr_groups, nd->nd_cr.cr_ngroups);
1888 		len = fxdr_unsigned(int, *++tl);
1889 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
1890 			m_freem(mrep);
1891 			return (EBADRPC);
1892 		}
1893 		if (len > 0)
1894 			nfsm_adv(nfsm_rndup(len));
1895 	} else if (auth_type == rpc_auth_kerb) {
1896 		switch (fxdr_unsigned(int, *tl++)) {
1897 		case RPCAKN_FULLNAME:
1898 			ticklen = fxdr_unsigned(int, *tl);
1899 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
1900 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
1901 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
1902 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
1903 				m_freem(mrep);
1904 				return (EBADRPC);
1905 			}
1906 			uio.uio_offset = 0;
1907 			uio.uio_iov = &iov;
1908 			uio.uio_iovcnt = 1;
1909 			uio.uio_segflg = UIO_SYSSPACE;
1910 			iov.iov_base = (caddr_t)&nfsd->nfsd_authstr[4];
1911 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
1912 			nfsm_mtouio(&uio, uio.uio_resid);
1913 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1914 			if (*tl++ != rpc_auth_kerb ||
1915 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
1916 				kprintf("Bad kerb verifier\n");
1917 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1918 				nd->nd_procnum = NFSPROC_NOOP;
1919 				return (0);
1920 			}
1921 			nfsm_dissect(cp, caddr_t, 4 * NFSX_UNSIGNED);
1922 			tl = (u_int32_t *)cp;
1923 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
1924 				kprintf("Not fullname kerb verifier\n");
1925 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1926 				nd->nd_procnum = NFSPROC_NOOP;
1927 				return (0);
1928 			}
1929 			cp += NFSX_UNSIGNED;
1930 			bcopy(cp, nfsd->nfsd_verfstr, 3 * NFSX_UNSIGNED);
1931 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
1932 			nd->nd_flag |= ND_KERBFULL;
1933 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
1934 			break;
1935 		case RPCAKN_NICKNAME:
1936 			if (len != 2 * NFSX_UNSIGNED) {
1937 				kprintf("Kerb nickname short\n");
1938 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
1939 				nd->nd_procnum = NFSPROC_NOOP;
1940 				return (0);
1941 			}
1942 			nickuid = fxdr_unsigned(uid_t, *tl);
1943 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
1944 			if (*tl++ != rpc_auth_kerb ||
1945 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
1946 				kprintf("Kerb nick verifier bad\n");
1947 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
1948 				nd->nd_procnum = NFSPROC_NOOP;
1949 				return (0);
1950 			}
1951 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1952 			tvin.tv_sec = *tl++;
1953 			tvin.tv_usec = *tl;
1954 
1955 			for (nuidp = NUIDHASH(nfsd->nfsd_slp,nickuid)->lh_first;
1956 			    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
1957 				if (nuidp->nu_cr.cr_uid == nickuid &&
1958 				    (!nd->nd_nam2 ||
1959 				     netaddr_match(NU_NETFAM(nuidp),
1960 				      &nuidp->nu_haddr, nd->nd_nam2)))
1961 					break;
1962 			}
1963 			if (!nuidp) {
1964 				nd->nd_repstat =
1965 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
1966 				nd->nd_procnum = NFSPROC_NOOP;
1967 				return (0);
1968 			}
1969 
1970 			/*
1971 			 * Now, decrypt the timestamp using the session key
1972 			 * and validate it.
1973 			 */
1974 #ifdef NFSKERB
1975 			XXX
1976 #endif
1977 
1978 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
1979 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
1980 			if (nuidp->nu_expire < time_second ||
1981 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
1982 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
1983 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
1984 				nuidp->nu_expire = 0;
1985 				nd->nd_repstat =
1986 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
1987 				nd->nd_procnum = NFSPROC_NOOP;
1988 				return (0);
1989 			}
1990 			nfsrv_setcred(&nuidp->nu_cr, &nd->nd_cr);
1991 			nd->nd_flag |= ND_KERBNICK;
1992 		};
1993 	} else {
1994 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
1995 		nd->nd_procnum = NFSPROC_NOOP;
1996 		return (0);
1997 	}
1998 
1999 	nd->nd_md = md;
2000 	nd->nd_dpos = dpos;
2001 	return (0);
2002 nfsmout:
2003 	return (error);
2004 }
2005 
2006 #endif
2007 
2008 /*
2009  * Send a message to the originating process's terminal.  The thread and/or
2010  * process may be NULL.  YYY the thread should not be NULL but there may
2011  * still be some uio_td's that are still being passed as NULL through to
2012  * nfsm_request().
2013  */
2014 static int
2015 nfs_msg(struct thread *td, char *server, char *msg)
2016 {
2017 	tpr_t tpr;
2018 
2019 	if (td && td->td_proc)
2020 		tpr = tprintf_open(td->td_proc);
2021 	else
2022 		tpr = NULL;
2023 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
2024 	tprintf_close(tpr);
2025 	return (0);
2026 }
2027 
2028 #ifndef NFS_NOSERVER
2029 /*
2030  * Socket upcall routine for the nfsd sockets.
2031  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
2032  * Essentially do as much as possible non-blocking, else punt and it will
2033  * be called with MB_WAIT from an nfsd.
2034  */
2035 void
2036 nfsrv_rcv(struct socket *so, void *arg, int waitflag)
2037 {
2038 	struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
2039 	struct mbuf *m;
2040 	struct sockaddr *nam;
2041 	struct sockbuf sio;
2042 	int flags, error;
2043 	int nparallel_wakeup = 0;
2044 
2045 	if ((slp->ns_flag & SLP_VALID) == 0)
2046 		return;
2047 
2048 	/*
2049 	 * Do not allow an infinite number of completed RPC records to build
2050 	 * up before we stop reading data from the socket.  Otherwise we could
2051 	 * end up holding onto an unreasonable number of mbufs for requests
2052 	 * waiting for service.
2053 	 *
2054 	 * This should give pretty good feedback to the TCP
2055 	 * layer and prevents a memory crunch for other protocols.
2056 	 *
2057 	 * Note that the same service socket can be dispatched to several
2058 	 * nfs servers simultaniously.
2059 	 *
2060 	 * the tcp protocol callback calls us with MB_DONTWAIT.
2061 	 * nfsd calls us with MB_WAIT (typically).
2062 	 */
2063 	if (waitflag == MB_DONTWAIT && slp->ns_numrec >= nfsd_waiting / 2 + 1) {
2064 		slp->ns_flag |= SLP_NEEDQ;
2065 		goto dorecs;
2066 	}
2067 
2068 	/*
2069 	 * Handle protocol specifics to parse an RPC request.  We always
2070 	 * pull from the socket using non-blocking I/O.
2071 	 */
2072 	if (so->so_type == SOCK_STREAM) {
2073 		/*
2074 		 * The data has to be read in an orderly fashion from a TCP
2075 		 * stream, unlike a UDP socket.  It is possible for soreceive
2076 		 * and/or nfsrv_getstream() to block, so make sure only one
2077 		 * entity is messing around with the TCP stream at any given
2078 		 * moment.  The receive sockbuf's lock in soreceive is not
2079 		 * sufficient.
2080 		 *
2081 		 * Note that this procedure can be called from any number of
2082 		 * NFS severs *OR* can be upcalled directly from a TCP
2083 		 * protocol thread.
2084 		 */
2085 		if (slp->ns_flag & SLP_GETSTREAM) {
2086 			slp->ns_flag |= SLP_NEEDQ;
2087 			goto dorecs;
2088 		}
2089 		slp->ns_flag |= SLP_GETSTREAM;
2090 
2091 		/*
2092 		 * Do soreceive().  Pull out as much data as possible without
2093 		 * blocking.
2094 		 */
2095 		sbinit(&sio, 1000000000);
2096 		flags = MSG_DONTWAIT;
2097 		error = so_pru_soreceive(so, &nam, NULL, &sio, NULL, &flags);
2098 		if (error || sio.sb_mb == NULL) {
2099 			if (error == EWOULDBLOCK)
2100 				slp->ns_flag |= SLP_NEEDQ;
2101 			else
2102 				slp->ns_flag |= SLP_DISCONN;
2103 			slp->ns_flag &= ~SLP_GETSTREAM;
2104 			goto dorecs;
2105 		}
2106 		m = sio.sb_mb;
2107 		if (slp->ns_rawend) {
2108 			slp->ns_rawend->m_next = m;
2109 			slp->ns_cc += sio.sb_cc;
2110 		} else {
2111 			slp->ns_raw = m;
2112 			slp->ns_cc = sio.sb_cc;
2113 		}
2114 		while (m->m_next)
2115 			m = m->m_next;
2116 		slp->ns_rawend = m;
2117 
2118 		/*
2119 		 * Now try and parse as many record(s) as we can out of the
2120 		 * raw stream data.
2121 		 */
2122 		error = nfsrv_getstream(slp, waitflag, &nparallel_wakeup);
2123 		if (error) {
2124 			if (error == EPERM)
2125 				slp->ns_flag |= SLP_DISCONN;
2126 			else
2127 				slp->ns_flag |= SLP_NEEDQ;
2128 		}
2129 		slp->ns_flag &= ~SLP_GETSTREAM;
2130 	} else {
2131 		/*
2132 		 * For UDP soreceive typically pulls just one packet, loop
2133 		 * to get the whole batch.
2134 		 */
2135 		do {
2136 			sbinit(&sio, 1000000000);
2137 			flags = MSG_DONTWAIT;
2138 			error = so_pru_soreceive(so, &nam, NULL, &sio,
2139 						 NULL, &flags);
2140 			if (sio.sb_mb) {
2141 				struct nfsrv_rec *rec;
2142 				int mf = (waitflag & MB_DONTWAIT) ?
2143 					    M_NOWAIT : M_WAITOK;
2144 				rec = kmalloc(sizeof(struct nfsrv_rec),
2145 					     M_NFSRVDESC, mf);
2146 				if (!rec) {
2147 					if (nam)
2148 						FREE(nam, M_SONAME);
2149 					m_freem(sio.sb_mb);
2150 					continue;
2151 				}
2152 				nfs_realign(&sio.sb_mb, 10 * NFSX_UNSIGNED);
2153 				rec->nr_address = nam;
2154 				rec->nr_packet = sio.sb_mb;
2155 				STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2156 				++slp->ns_numrec;
2157 				++nparallel_wakeup;
2158 			}
2159 			if (error) {
2160 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
2161 					&& error != EWOULDBLOCK) {
2162 					slp->ns_flag |= SLP_DISCONN;
2163 					goto dorecs;
2164 				}
2165 			}
2166 		} while (sio.sb_mb);
2167 	}
2168 
2169 	/*
2170 	 * If we were upcalled from the tcp protocol layer and we have
2171 	 * fully parsed records ready to go, or there is new data pending,
2172 	 * or something went wrong, try to wake up an nfsd thread to deal
2173 	 * with it.
2174 	 */
2175 dorecs:
2176 	if (waitflag == MB_DONTWAIT && (slp->ns_numrec > 0
2177 	     || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN)))) {
2178 		nfsrv_wakenfsd(slp, nparallel_wakeup);
2179 	}
2180 }
2181 
2182 /*
2183  * Try and extract an RPC request from the mbuf data list received on a
2184  * stream socket. The "waitflag" argument indicates whether or not it
2185  * can sleep.
2186  */
2187 static int
2188 nfsrv_getstream(struct nfssvc_sock *slp, int waitflag, int *countp)
2189 {
2190 	struct mbuf *m, **mpp;
2191 	char *cp1, *cp2;
2192 	int len;
2193 	struct mbuf *om, *m2, *recm;
2194 	u_int32_t recmark;
2195 
2196 	for (;;) {
2197 	    if (slp->ns_reclen == 0) {
2198 		if (slp->ns_cc < NFSX_UNSIGNED)
2199 			return (0);
2200 		m = slp->ns_raw;
2201 		if (m->m_len >= NFSX_UNSIGNED) {
2202 			bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
2203 			m->m_data += NFSX_UNSIGNED;
2204 			m->m_len -= NFSX_UNSIGNED;
2205 		} else {
2206 			cp1 = (caddr_t)&recmark;
2207 			cp2 = mtod(m, caddr_t);
2208 			while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
2209 				while (m->m_len == 0) {
2210 					m = m->m_next;
2211 					cp2 = mtod(m, caddr_t);
2212 				}
2213 				*cp1++ = *cp2++;
2214 				m->m_data++;
2215 				m->m_len--;
2216 			}
2217 		}
2218 		slp->ns_cc -= NFSX_UNSIGNED;
2219 		recmark = ntohl(recmark);
2220 		slp->ns_reclen = recmark & ~0x80000000;
2221 		if (recmark & 0x80000000)
2222 			slp->ns_flag |= SLP_LASTFRAG;
2223 		else
2224 			slp->ns_flag &= ~SLP_LASTFRAG;
2225 		if (slp->ns_reclen > NFS_MAXPACKET || slp->ns_reclen <= 0) {
2226 			log(LOG_ERR, "%s (%d) from nfs client\n",
2227 			    "impossible packet length",
2228 			    slp->ns_reclen);
2229 			return (EPERM);
2230 		}
2231 	    }
2232 
2233 	    /*
2234 	     * Now get the record part.
2235 	     *
2236 	     * Note that slp->ns_reclen may be 0.  Linux sometimes
2237 	     * generates 0-length RPCs
2238 	     */
2239 	    recm = NULL;
2240 	    if (slp->ns_cc == slp->ns_reclen) {
2241 		recm = slp->ns_raw;
2242 		slp->ns_raw = slp->ns_rawend = NULL;
2243 		slp->ns_cc = slp->ns_reclen = 0;
2244 	    } else if (slp->ns_cc > slp->ns_reclen) {
2245 		len = 0;
2246 		m = slp->ns_raw;
2247 		om = NULL;
2248 
2249 		while (len < slp->ns_reclen) {
2250 			if ((len + m->m_len) > slp->ns_reclen) {
2251 				m2 = m_copym(m, 0, slp->ns_reclen - len,
2252 					waitflag);
2253 				if (m2) {
2254 					if (om) {
2255 						om->m_next = m2;
2256 						recm = slp->ns_raw;
2257 					} else
2258 						recm = m2;
2259 					m->m_data += slp->ns_reclen - len;
2260 					m->m_len -= slp->ns_reclen - len;
2261 					len = slp->ns_reclen;
2262 				} else {
2263 					return (EWOULDBLOCK);
2264 				}
2265 			} else if ((len + m->m_len) == slp->ns_reclen) {
2266 				om = m;
2267 				len += m->m_len;
2268 				m = m->m_next;
2269 				recm = slp->ns_raw;
2270 				om->m_next = NULL;
2271 			} else {
2272 				om = m;
2273 				len += m->m_len;
2274 				m = m->m_next;
2275 			}
2276 		}
2277 		slp->ns_raw = m;
2278 		slp->ns_cc -= len;
2279 		slp->ns_reclen = 0;
2280 	    } else {
2281 		return (0);
2282 	    }
2283 
2284 	    /*
2285 	     * Accumulate the fragments into a record.
2286 	     */
2287 	    mpp = &slp->ns_frag;
2288 	    while (*mpp)
2289 		mpp = &((*mpp)->m_next);
2290 	    *mpp = recm;
2291 	    if (slp->ns_flag & SLP_LASTFRAG) {
2292 		struct nfsrv_rec *rec;
2293 		int mf = (waitflag & MB_DONTWAIT) ? M_NOWAIT : M_WAITOK;
2294 		rec = kmalloc(sizeof(struct nfsrv_rec), M_NFSRVDESC, mf);
2295 		if (!rec) {
2296 		    m_freem(slp->ns_frag);
2297 		} else {
2298 		    nfs_realign(&slp->ns_frag, 10 * NFSX_UNSIGNED);
2299 		    rec->nr_address = NULL;
2300 		    rec->nr_packet = slp->ns_frag;
2301 		    STAILQ_INSERT_TAIL(&slp->ns_rec, rec, nr_link);
2302 		    ++slp->ns_numrec;
2303 		    ++*countp;
2304 		}
2305 		slp->ns_frag = NULL;
2306 	    }
2307 	}
2308 }
2309 
2310 /*
2311  * Parse an RPC header.
2312  */
2313 int
2314 nfsrv_dorec(struct nfssvc_sock *slp, struct nfsd *nfsd,
2315 	    struct nfsrv_descript **ndp)
2316 {
2317 	struct nfsrv_rec *rec;
2318 	struct mbuf *m;
2319 	struct sockaddr *nam;
2320 	struct nfsrv_descript *nd;
2321 	int error;
2322 
2323 	*ndp = NULL;
2324 	if ((slp->ns_flag & SLP_VALID) == 0 || !STAILQ_FIRST(&slp->ns_rec))
2325 		return (ENOBUFS);
2326 	rec = STAILQ_FIRST(&slp->ns_rec);
2327 	STAILQ_REMOVE_HEAD(&slp->ns_rec, nr_link);
2328 	KKASSERT(slp->ns_numrec > 0);
2329 	--slp->ns_numrec;
2330 	nam = rec->nr_address;
2331 	m = rec->nr_packet;
2332 	kfree(rec, M_NFSRVDESC);
2333 	MALLOC(nd, struct nfsrv_descript *, sizeof (struct nfsrv_descript),
2334 		M_NFSRVDESC, M_WAITOK);
2335 	nd->nd_md = nd->nd_mrep = m;
2336 	nd->nd_nam2 = nam;
2337 	nd->nd_dpos = mtod(m, caddr_t);
2338 	error = nfs_getreq(nd, nfsd, TRUE);
2339 	if (error) {
2340 		if (nam) {
2341 			FREE(nam, M_SONAME);
2342 		}
2343 		kfree((caddr_t)nd, M_NFSRVDESC);
2344 		return (error);
2345 	}
2346 	*ndp = nd;
2347 	nfsd->nfsd_nd = nd;
2348 	return (0);
2349 }
2350 
2351 /*
2352  * Try to assign service sockets to nfsd threads based on the number
2353  * of new rpc requests that have been queued on the service socket.
2354  *
2355  * If no nfsd's are available or additonal requests are pending, set the
2356  * NFSD_CHECKSLP flag so that one of the running nfsds will go look for
2357  * the work in the nfssvc_sock list when it is finished processing its
2358  * current work.  This flag is only cleared when an nfsd can not find
2359  * any new work to perform.
2360  */
2361 void
2362 nfsrv_wakenfsd(struct nfssvc_sock *slp, int nparallel)
2363 {
2364 	struct nfsd *nd;
2365 
2366 	if ((slp->ns_flag & SLP_VALID) == 0)
2367 		return;
2368 	if (nparallel <= 1)
2369 		nparallel = 1;
2370 	TAILQ_FOREACH(nd, &nfsd_head, nfsd_chain) {
2371 		if (nd->nfsd_flag & NFSD_WAITING) {
2372 			nd->nfsd_flag &= ~NFSD_WAITING;
2373 			if (nd->nfsd_slp)
2374 				panic("nfsd wakeup");
2375 			slp->ns_sref++;
2376 			nd->nfsd_slp = slp;
2377 			wakeup((caddr_t)nd);
2378 			if (--nparallel == 0)
2379 				break;
2380 		}
2381 	}
2382 	if (nparallel) {
2383 		slp->ns_flag |= SLP_DOREC;
2384 		nfsd_head_flag |= NFSD_CHECKSLP;
2385 	}
2386 }
2387 #endif /* NFS_NOSERVER */
2388