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