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