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