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