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