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