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