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