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