xref: /original-bsd/sys/nfs/nfs_socket.c (revision d4dfefc4)
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.41 (Berkeley) 11/03/92
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) printf("rcv err=%d\n",error);
692 		if (error) {
693 
694 			/*
695 			 * Ignore routing errors on connectionless protocols??
696 			 */
697 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
698 				nmp->nm_so->so_error = 0;
699 				continue;
700 			}
701 			return (error);
702 		}
703 		if (nam)
704 			m_freem(nam);
705 
706 		/*
707 		 * Get the xid and check that it is an rpc reply
708 		 */
709 		md = mrep;
710 		dpos = mtod(md, caddr_t);
711 		nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
712 		rxid = *tl++;
713 		if (*tl != rpc_reply) {
714 			if (nmp->nm_flag & NFSMNT_NQNFS) {
715 				if (nqnfs_callback(nmp, mrep, md, dpos))
716 					nfsstats.rpcinvalid++;
717 			} else {
718 				nfsstats.rpcinvalid++;
719 				m_freem(mrep);
720 			}
721 nfsmout:
722 			continue;
723 		}
724 
725 		/*
726 		 * Loop through the request list to match up the reply
727 		 * Iff no match, just drop the datagram
728 		 */
729 		rep = nfsreqh.r_next;
730 		while (rep != &nfsreqh) {
731 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
732 				/* Found it.. */
733 				rep->r_mrep = mrep;
734 				rep->r_md = md;
735 				rep->r_dpos = dpos;
736 				if (nfsrtton) {
737 					struct rttl *rt;
738 
739 					rt = &nfsrtt.rttl[nfsrtt.pos];
740 					rt->proc = rep->r_procnum;
741 					rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
742 					rt->sent = nmp->nm_sent;
743 					rt->cwnd = nmp->nm_cwnd;
744 					rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
745 					rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
746 					rt->fsid = nmp->nm_mountp->mnt_stat.f_fsid;
747 					rt->tstamp = time;
748 					if (rep->r_flags & R_TIMING)
749 						rt->rtt = rep->r_rtt;
750 					else
751 						rt->rtt = 1000000;
752 					nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
753 				}
754 				/*
755 				 * Update congestion window.
756 				 * Do the additive increase of
757 				 * one rpc/rtt.
758 				 */
759 				if (nmp->nm_cwnd <= nmp->nm_sent) {
760 					nmp->nm_cwnd +=
761 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
762 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
763 					if (nmp->nm_cwnd > NFS_MAXCWND)
764 						nmp->nm_cwnd = NFS_MAXCWND;
765 				}
766 				rep->r_flags &= ~R_SENT;
767 				nmp->nm_sent -= NFS_CWNDSCALE;
768 				/*
769 				 * Update rtt using a gain of 0.125 on the mean
770 				 * and a gain of 0.25 on the deviation.
771 				 */
772 				if (rep->r_flags & R_TIMING) {
773 					/*
774 					 * Since the timer resolution of
775 					 * NFS_HZ is so course, it can often
776 					 * result in r_rtt == 0. Since
777 					 * r_rtt == N means that the actual
778 					 * rtt is between N+dt and N+2-dt ticks,
779 					 * add 1.
780 					 */
781 					t1 = rep->r_rtt + 1;
782 					t1 -= (NFS_SRTT(rep) >> 3);
783 					NFS_SRTT(rep) += t1;
784 					if (t1 < 0)
785 						t1 = -t1;
786 					t1 -= (NFS_SDRTT(rep) >> 2);
787 					NFS_SDRTT(rep) += t1;
788 				}
789 				nmp->nm_timeouts = 0;
790 				break;
791 			}
792 			rep = rep->r_next;
793 		}
794 		/*
795 		 * If not matched to a request, drop it.
796 		 * If it's mine, get out.
797 		 */
798 		if (rep == &nfsreqh) {
799 			nfsstats.rpcunexpected++;
800 			m_freem(mrep);
801 		} else if (rep == myrep) {
802 			if (rep->r_mrep == NULL)
803 				panic("nfsreply nil");
804 			return (0);
805 		}
806 	}
807 }
808 
809 /*
810  * nfs_request - goes something like this
811  *	- fill in request struct
812  *	- links it into list
813  *	- calls nfs_send() for first transmit
814  *	- calls nfs_receive() to get reply
815  *	- break down rpc header and return with nfs reply pointed to
816  *	  by mrep or error
817  * nb: always frees up mreq mbuf list
818  */
819 nfs_request(vp, mrest, procnum, procp, cred, mrp, mdp, dposp)
820 	struct vnode *vp;
821 	struct mbuf *mrest;
822 	int procnum;
823 	struct proc *procp;
824 	struct ucred *cred;
825 	struct mbuf **mrp;
826 	struct mbuf **mdp;
827 	caddr_t *dposp;
828 {
829 	register struct mbuf *m, *mrep;
830 	register struct nfsreq *rep;
831 	register u_long *tl;
832 	register int i;
833 	struct nfsmount *nmp;
834 	struct mbuf *md, *mheadend;
835 	struct nfsreq *reph;
836 	struct nfsnode *tp, *np;
837 	time_t reqtime, waituntil;
838 	caddr_t dpos, cp2;
839 	int t1, nqlflag, cachable, s, error = 0, mrest_len, auth_len, auth_type;
840 	int trylater_delay = NQ_TRYLATERDEL, trylater_cnt = 0, failed_auth = 0;
841 	u_long xid;
842 	u_quad_t frev;
843 	char *auth_str;
844 
845 	nmp = VFSTONFS(vp->v_mount);
846 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
847 	rep->r_nmp = nmp;
848 	rep->r_vp = vp;
849 	rep->r_procp = procp;
850 	rep->r_procnum = procnum;
851 	i = 0;
852 	m = mrest;
853 	while (m) {
854 		i += m->m_len;
855 		m = m->m_next;
856 	}
857 	mrest_len = i;
858 
859 	/*
860 	 * Get the RPC header with authorization.
861 	 */
862 kerbauth:
863 	auth_str = (char *)0;
864 	if (nmp->nm_flag & NFSMNT_KERB) {
865 		if (failed_auth) {
866 			error = nfs_getauth(nmp, rep, cred, &auth_type,
867 				&auth_str, &auth_len);
868 			if (error) {
869 				free((caddr_t)rep, M_NFSREQ);
870 				m_freem(mrest);
871 				return (error);
872 			}
873 		} else {
874 			auth_type = RPCAUTH_UNIX;
875 			auth_len = 5 * NFSX_UNSIGNED;
876 		}
877 	} else {
878 		auth_type = RPCAUTH_UNIX;
879 		if (cred->cr_ngroups < 1)
880 			panic("nfsreq nogrps");
881 		auth_len = ((((cred->cr_ngroups - 1) > nmp->nm_numgrps) ?
882 			nmp->nm_numgrps : (cred->cr_ngroups - 1)) << 2) +
883 			5 * NFSX_UNSIGNED;
884 	}
885 	m = nfsm_rpchead(cred, (nmp->nm_flag & NFSMNT_NQNFS), procnum,
886 	     auth_type, auth_len, auth_str, mrest, mrest_len, &mheadend, &xid);
887 	if (auth_str)
888 		free(auth_str, M_TEMP);
889 
890 	/*
891 	 * For stream protocols, insert a Sun RPC Record Mark.
892 	 */
893 	if (nmp->nm_sotype == SOCK_STREAM) {
894 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
895 		*mtod(m, u_long *) = htonl(0x80000000 |
896 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
897 	}
898 	rep->r_mreq = m;
899 	rep->r_xid = xid;
900 tryagain:
901 	if (nmp->nm_flag & NFSMNT_SOFT)
902 		rep->r_retry = nmp->nm_retry;
903 	else
904 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
905 	rep->r_rtt = rep->r_rexmit = 0;
906 	if (proct[procnum] > 0)
907 		rep->r_flags = R_TIMING;
908 	else
909 		rep->r_flags = 0;
910 	rep->r_mrep = NULL;
911 
912 	/*
913 	 * Do the client side RPC.
914 	 */
915 	nfsstats.rpcrequests++;
916 	/*
917 	 * Chain request into list of outstanding requests. Be sure
918 	 * to put it LAST so timer finds oldest requests first.
919 	 */
920 	s = splsoftclock();
921 	reph = &nfsreqh;
922 	reph->r_prev->r_next = rep;
923 	rep->r_prev = reph->r_prev;
924 	reph->r_prev = rep;
925 	rep->r_next = reph;
926 
927 	/* Get send time for nqnfs */
928 	reqtime = time.tv_sec;
929 
930 	/*
931 	 * If backing off another request or avoiding congestion, don't
932 	 * send this one now but let timer do it. If not timing a request,
933 	 * do it now.
934 	 */
935 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
936 		(nmp->nm_flag & NFSMNT_DUMBTIMR) ||
937 		nmp->nm_sent < nmp->nm_cwnd)) {
938 		splx(s);
939 		if (nmp->nm_soflags & PR_CONNREQUIRED)
940 			error = nfs_sndlock(&nmp->nm_flag, rep);
941 		if (!error) {
942 			m = m_copym(m, 0, M_COPYALL, M_WAIT);
943 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
944 			if (nmp->nm_soflags & PR_CONNREQUIRED)
945 				nfs_sndunlock(&nmp->nm_flag);
946 		}
947 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
948 			nmp->nm_sent += NFS_CWNDSCALE;
949 			rep->r_flags |= R_SENT;
950 		}
951 	} else {
952 		splx(s);
953 		rep->r_rtt = -1;
954 	}
955 
956 	/*
957 	 * Wait for the reply from our send or the timer's.
958 	 */
959 	if (!error || error == EPIPE)
960 		error = nfs_reply(rep);
961 
962 	/*
963 	 * RPC done, unlink the request.
964 	 */
965 	s = splsoftclock();
966 	rep->r_prev->r_next = rep->r_next;
967 	rep->r_next->r_prev = rep->r_prev;
968 	splx(s);
969 
970 	/*
971 	 * Decrement the outstanding request count.
972 	 */
973 	if (rep->r_flags & R_SENT) {
974 		rep->r_flags &= ~R_SENT;	/* paranoia */
975 		nmp->nm_sent -= NFS_CWNDSCALE;
976 	}
977 
978 	/*
979 	 * If there was a successful reply and a tprintf msg.
980 	 * tprintf a response.
981 	 */
982 	if (!error && (rep->r_flags & R_TPRINTFMSG))
983 		nfs_msg(rep->r_procp, nmp->nm_mountp->mnt_stat.f_mntfromname,
984 		    "is alive again");
985 	mrep = rep->r_mrep;
986 	md = rep->r_md;
987 	dpos = rep->r_dpos;
988 	if (error) {
989 		m_freem(rep->r_mreq);
990 		free((caddr_t)rep, M_NFSREQ);
991 		return (error);
992 	}
993 
994 	/*
995 	 * break down the rpc header and check if ok
996 	 */
997 	nfsm_dissect(tl, u_long *, 3*NFSX_UNSIGNED);
998 	if (*tl++ == rpc_msgdenied) {
999 		if (*tl == rpc_mismatch)
1000 			error = EOPNOTSUPP;
1001 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
1002 			if (*tl == rpc_rejectedcred && failed_auth == 0) {
1003 				failed_auth++;
1004 				mheadend->m_next = (struct mbuf *)0;
1005 				m_freem(mrep);
1006 				m_freem(rep->r_mreq);
1007 				goto kerbauth;
1008 			} else
1009 				error = EAUTH;
1010 		} else
1011 			error = EACCES;
1012 		m_freem(mrep);
1013 		m_freem(rep->r_mreq);
1014 		free((caddr_t)rep, M_NFSREQ);
1015 		return (error);
1016 	}
1017 
1018 	/*
1019 	 * skip over the auth_verf, someday we may want to cache auth_short's
1020 	 * for nfs_reqhead(), but for now just dump it
1021 	 */
1022 	if (*++tl != 0) {
1023 		i = nfsm_rndup(fxdr_unsigned(long, *tl));
1024 		nfsm_adv(i);
1025 	}
1026 	nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1027 	/* 0 == ok */
1028 	if (*tl == 0) {
1029 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1030 		if (*tl != 0) {
1031 			error = fxdr_unsigned(int, *tl);
1032 			m_freem(mrep);
1033 			if ((nmp->nm_flag & NFSMNT_NQNFS) &&
1034 			    error == NQNFS_TRYLATER) {
1035 				error = 0;
1036 				waituntil = time.tv_sec + trylater_delay;
1037 				while (time.tv_sec < waituntil)
1038 					(void) tsleep((caddr_t)&lbolt,
1039 						PSOCK, "nqnfstry", 0);
1040 				trylater_delay *= nfs_backoff[trylater_cnt];
1041 				if (trylater_cnt < 7)
1042 					trylater_cnt++;
1043 				goto tryagain;
1044 			}
1045 
1046 			/*
1047 			 * If the File Handle was stale, invalidate the
1048 			 * lookup cache, just in case.
1049 			 */
1050 			if (error == ESTALE)
1051 				cache_purge(vp);
1052 			m_freem(rep->r_mreq);
1053 			free((caddr_t)rep, M_NFSREQ);
1054 			return (error);
1055 		}
1056 
1057 		/*
1058 		 * For nqnfs, get any lease in reply
1059 		 */
1060 		if (nmp->nm_flag & NFSMNT_NQNFS) {
1061 			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1062 			if (*tl) {
1063 				np = VTONFS(vp);
1064 				nqlflag = fxdr_unsigned(int, *tl);
1065 				nfsm_dissect(tl, u_long *, 4*NFSX_UNSIGNED);
1066 				cachable = fxdr_unsigned(int, *tl++);
1067 				reqtime += fxdr_unsigned(int, *tl++);
1068 				if (reqtime > time.tv_sec) {
1069 				    fxdr_hyper(tl, &frev);
1070 				    nqnfs_clientlease(nmp, np, nqlflag,
1071 					cachable, reqtime, frev);
1072 				}
1073 			}
1074 		}
1075 		*mrp = mrep;
1076 		*mdp = md;
1077 		*dposp = dpos;
1078 		m_freem(rep->r_mreq);
1079 		FREE((caddr_t)rep, M_NFSREQ);
1080 		return (0);
1081 	}
1082 	m_freem(mrep);
1083 	m_freem(rep->r_mreq);
1084 	free((caddr_t)rep, M_NFSREQ);
1085 	error = EPROTONOSUPPORT;
1086 nfsmout:
1087 	return (error);
1088 }
1089 
1090 /*
1091  * Generate the rpc reply header
1092  * siz arg. is used to decide if adding a cluster is worthwhile
1093  */
1094 nfs_rephead(siz, nd, err, cache, frev, mrq, mbp, bposp)
1095 	int siz;
1096 	struct nfsd *nd;
1097 	int err;
1098 	int cache;
1099 	u_quad_t *frev;
1100 	struct mbuf **mrq;
1101 	struct mbuf **mbp;
1102 	caddr_t *bposp;
1103 {
1104 	register u_long *tl;
1105 	register struct mbuf *mreq;
1106 	caddr_t bpos;
1107 	struct mbuf *mb, *mb2;
1108 
1109 	MGETHDR(mreq, M_WAIT, MT_DATA);
1110 	mb = mreq;
1111 	/*
1112 	 * If this is a big reply, use a cluster else
1113 	 * try and leave leading space for the lower level headers.
1114 	 */
1115 	siz += RPC_REPLYSIZ;
1116 	if (siz >= MINCLSIZE) {
1117 		MCLGET(mreq, M_WAIT);
1118 	} else
1119 		mreq->m_data += max_hdr;
1120 	tl = mtod(mreq, u_long *);
1121 	mreq->m_len = 6*NFSX_UNSIGNED;
1122 	bpos = ((caddr_t)tl)+mreq->m_len;
1123 	*tl++ = nd->nd_retxid;
1124 	*tl++ = rpc_reply;
1125 	if (err == ERPCMISMATCH || err == NQNFS_AUTHERR) {
1126 		*tl++ = rpc_msgdenied;
1127 		if (err == NQNFS_AUTHERR) {
1128 			*tl++ = rpc_autherr;
1129 			*tl = rpc_rejectedcred;
1130 			mreq->m_len -= NFSX_UNSIGNED;
1131 			bpos -= NFSX_UNSIGNED;
1132 		} else {
1133 			*tl++ = rpc_mismatch;
1134 			*tl++ = txdr_unsigned(2);
1135 			*tl = txdr_unsigned(2);
1136 		}
1137 	} else {
1138 		*tl++ = rpc_msgaccepted;
1139 		*tl++ = 0;
1140 		*tl++ = 0;
1141 		switch (err) {
1142 		case EPROGUNAVAIL:
1143 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
1144 			break;
1145 		case EPROGMISMATCH:
1146 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
1147 			nfsm_build(tl, u_long *, 2*NFSX_UNSIGNED);
1148 			*tl++ = txdr_unsigned(2);
1149 			*tl = txdr_unsigned(2);	/* someday 3 */
1150 			break;
1151 		case EPROCUNAVAIL:
1152 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
1153 			break;
1154 		default:
1155 			*tl = 0;
1156 			if (err != VNOVAL) {
1157 				nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1158 				if (err)
1159 					*tl = txdr_unsigned(nfsrv_errmap[err - 1]);
1160 				else
1161 					*tl = 0;
1162 			}
1163 			break;
1164 		};
1165 	}
1166 
1167 	/*
1168 	 * For nqnfs, piggyback lease as requested.
1169 	 */
1170 	if (nd->nd_nqlflag != NQL_NOVAL && err == 0) {
1171 		if (nd->nd_nqlflag) {
1172 			nfsm_build(tl, u_long *, 5*NFSX_UNSIGNED);
1173 			*tl++ = txdr_unsigned(nd->nd_nqlflag);
1174 			*tl++ = txdr_unsigned(cache);
1175 			*tl++ = txdr_unsigned(nd->nd_duration);
1176 			txdr_hyper(frev, tl);
1177 		} else {
1178 			if (nd->nd_nqlflag != 0)
1179 				panic("nqreph");
1180 			nfsm_build(tl, u_long *, NFSX_UNSIGNED);
1181 			*tl = 0;
1182 		}
1183 	}
1184 	*mrq = mreq;
1185 	*mbp = mb;
1186 	*bposp = bpos;
1187 	if (err != 0 && err != VNOVAL)
1188 		nfsstats.srvrpc_errs++;
1189 	return (0);
1190 }
1191 
1192 /*
1193  * Nfs timer routine
1194  * Scan the nfsreq list and retranmit any requests that have timed out
1195  * To avoid retransmission attempts on STREAM sockets (in the future) make
1196  * sure to set the r_retry field to 0 (implies nm_retry == 0).
1197  */
1198 void
1199 nfs_timer(arg)
1200 	void *arg;
1201 {
1202 	register struct nfsreq *rep;
1203 	register struct mbuf *m;
1204 	register struct socket *so;
1205 	register struct nfsmount *nmp;
1206 	register int timeo;
1207 	static long lasttime = 0;
1208 	int s, error;
1209 
1210 	s = splnet();
1211 	for (rep = nfsreqh.r_next; rep != &nfsreqh; rep = rep->r_next) {
1212 		nmp = rep->r_nmp;
1213 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
1214 			continue;
1215 		if (nfs_sigintr(nmp, rep, rep->r_procp)) {
1216 			rep->r_flags |= R_SOFTTERM;
1217 			continue;
1218 		}
1219 		if (rep->r_rtt >= 0) {
1220 			rep->r_rtt++;
1221 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
1222 				timeo = nmp->nm_timeo;
1223 			else
1224 				timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
1225 			if (nmp->nm_timeouts > 0)
1226 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
1227 			if (rep->r_rtt <= timeo)
1228 				continue;
1229 			if (nmp->nm_timeouts < 8)
1230 				nmp->nm_timeouts++;
1231 		}
1232 		/*
1233 		 * Check for server not responding
1234 		 */
1235 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1236 		     rep->r_rexmit > nmp->nm_deadthresh) {
1237 			nfs_msg(rep->r_procp,
1238 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
1239 			    "not responding");
1240 			rep->r_flags |= R_TPRINTFMSG;
1241 		}
1242 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
1243 			nfsstats.rpctimeouts++;
1244 			rep->r_flags |= R_SOFTTERM;
1245 			continue;
1246 		}
1247 		if (nmp->nm_sotype != SOCK_DGRAM) {
1248 			if (++rep->r_rexmit > NFS_MAXREXMIT)
1249 				rep->r_rexmit = NFS_MAXREXMIT;
1250 			continue;
1251 		}
1252 		if ((so = nmp->nm_so) == NULL)
1253 			continue;
1254 
1255 		/*
1256 		 * If there is enough space and the window allows..
1257 		 *	Resend it
1258 		 * Set r_rtt to -1 in case we fail to send it now.
1259 		 */
1260 		rep->r_rtt = -1;
1261 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1262 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
1263 		    (rep->r_flags & R_SENT) ||
1264 		    nmp->nm_sent < nmp->nm_cwnd) &&
1265 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1266 			if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1267 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1268 			    (struct mbuf *)0, (struct mbuf *)0);
1269 			else
1270 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1271 			    nmp->nm_nam, (struct mbuf *)0);
1272 			if (error) {
1273 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1274 					so->so_error = 0;
1275 			} else {
1276 				/*
1277 				 * Iff first send, start timing
1278 				 * else turn timing off, backoff timer
1279 				 * and divide congestion window by 2.
1280 				 */
1281 				if (rep->r_flags & R_SENT) {
1282 					rep->r_flags &= ~R_TIMING;
1283 					if (++rep->r_rexmit > NFS_MAXREXMIT)
1284 						rep->r_rexmit = NFS_MAXREXMIT;
1285 					nmp->nm_cwnd >>= 1;
1286 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
1287 						nmp->nm_cwnd = NFS_CWNDSCALE;
1288 					nfsstats.rpcretries++;
1289 				} else {
1290 					rep->r_flags |= R_SENT;
1291 					nmp->nm_sent += NFS_CWNDSCALE;
1292 				}
1293 				rep->r_rtt = 0;
1294 			}
1295 		}
1296 	}
1297 
1298 	/*
1299 	 * Call the nqnfs server timer once a second to handle leases.
1300 	 */
1301 	if (lasttime != time.tv_sec) {
1302 		lasttime = time.tv_sec;
1303 		nqnfs_serverd();
1304 	}
1305 	splx(s);
1306 	timeout(nfs_timer, (caddr_t)0, hz/NFS_HZ);
1307 }
1308 
1309 /*
1310  * Test for a termination condition pending on the process.
1311  * This is used for NFSMNT_INT mounts.
1312  */
1313 nfs_sigintr(nmp, rep, p)
1314 	struct nfsmount *nmp;
1315 	struct nfsreq *rep;
1316 	register struct proc *p;
1317 {
1318 
1319 	if (rep && (rep->r_flags & R_SOFTTERM))
1320 		return (EINTR);
1321 	if (!(nmp->nm_flag & NFSMNT_INT))
1322 		return (0);
1323 	if (p && p->p_sig && (((p->p_sig &~ p->p_sigmask) &~ p->p_sigignore) &
1324 	    NFSINT_SIGMASK))
1325 		return (EINTR);
1326 	return (0);
1327 }
1328 
1329 /*
1330  * Lock a socket against others.
1331  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1332  * and also to avoid race conditions between the processes with nfs requests
1333  * in progress when a reconnect is necessary.
1334  */
1335 nfs_sndlock(flagp, rep)
1336 	register int *flagp;
1337 	struct nfsreq *rep;
1338 {
1339 	struct proc *p;
1340 
1341 	if (rep)
1342 		p = rep->r_procp;
1343 	else
1344 		p = (struct proc *)0;
1345 	while (*flagp & NFSMNT_SNDLOCK) {
1346 		if (nfs_sigintr(rep->r_nmp, rep, p))
1347 			return (EINTR);
1348 		*flagp |= NFSMNT_WANTSND;
1349 		(void) tsleep((caddr_t)flagp, PZERO-1, "nfsndlck", 0);
1350 	}
1351 	*flagp |= NFSMNT_SNDLOCK;
1352 	return (0);
1353 }
1354 
1355 /*
1356  * Unlock the stream socket for others.
1357  */
1358 void
1359 nfs_sndunlock(flagp)
1360 	register int *flagp;
1361 {
1362 
1363 	if ((*flagp & NFSMNT_SNDLOCK) == 0)
1364 		panic("nfs sndunlock");
1365 	*flagp &= ~NFSMNT_SNDLOCK;
1366 	if (*flagp & NFSMNT_WANTSND) {
1367 		*flagp &= ~NFSMNT_WANTSND;
1368 		wakeup((caddr_t)flagp);
1369 	}
1370 }
1371 
1372 nfs_rcvlock(rep)
1373 	register struct nfsreq *rep;
1374 {
1375 	register int *flagp = &rep->r_nmp->nm_flag;
1376 
1377 	while (*flagp & NFSMNT_RCVLOCK) {
1378 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_procp))
1379 			return (EINTR);
1380 		*flagp |= NFSMNT_WANTRCV;
1381 		(void) tsleep((caddr_t)flagp, PZERO-1, "nfsrcvlck", 0);
1382 	}
1383 	*flagp |= NFSMNT_RCVLOCK;
1384 	return (0);
1385 }
1386 
1387 /*
1388  * Unlock the stream socket for others.
1389  */
1390 void
1391 nfs_rcvunlock(flagp)
1392 	register int *flagp;
1393 {
1394 
1395 	if ((*flagp & NFSMNT_RCVLOCK) == 0)
1396 		panic("nfs rcvunlock");
1397 	*flagp &= ~NFSMNT_RCVLOCK;
1398 	if (*flagp & NFSMNT_WANTRCV) {
1399 		*flagp &= ~NFSMNT_WANTRCV;
1400 		wakeup((caddr_t)flagp);
1401 	}
1402 }
1403 
1404 /*
1405  * Check for badly aligned mbuf data areas and
1406  * realign data in an mbuf list by copying the data areas up, as required.
1407  */
1408 void
1409 nfs_realign(m, hsiz)
1410 	register struct mbuf *m;
1411 	int hsiz;
1412 {
1413 	register struct mbuf *m2;
1414 	register int siz, mlen, olen;
1415 	register caddr_t tcp, fcp;
1416 	struct mbuf *mnew;
1417 
1418 	while (m) {
1419 	    /*
1420 	     * This never happens for UDP, rarely happens for TCP
1421 	     * but frequently happens for iso transport.
1422 	     */
1423 	    if ((m->m_len & 0x3) || (mtod(m, int) & 0x3)) {
1424 		olen = m->m_len;
1425 		fcp = mtod(m, caddr_t);
1426 		m->m_flags &= ~M_PKTHDR;
1427 		if (m->m_flags & M_EXT)
1428 			m->m_data = m->m_ext.ext_buf;
1429 		else
1430 			m->m_data = m->m_dat;
1431 		m->m_len = 0;
1432 		tcp = mtod(m, caddr_t);
1433 		mnew = m;
1434 		m2 = m->m_next;
1435 
1436 		/*
1437 		 * If possible, only put the first invariant part
1438 		 * of the RPC header in the first mbuf.
1439 		 */
1440 		if (olen <= hsiz)
1441 			mlen = hsiz;
1442 		else
1443 			mlen = M_TRAILINGSPACE(m);
1444 
1445 		/*
1446 		 * Loop through the mbuf list consolidating data.
1447 		 */
1448 		while (m) {
1449 			while (olen > 0) {
1450 				if (mlen == 0) {
1451 					m2->m_flags &= ~M_PKTHDR;
1452 					if (m2->m_flags & M_EXT)
1453 						m2->m_data = m2->m_ext.ext_buf;
1454 					else
1455 						m2->m_data = m2->m_dat;
1456 					m2->m_len = 0;
1457 					mlen = M_TRAILINGSPACE(m2);
1458 					tcp = mtod(m2, caddr_t);
1459 					mnew = m2;
1460 					m2 = m2->m_next;
1461 				}
1462 				siz = min(mlen, olen);
1463 				if (tcp != fcp)
1464 					bcopy(fcp, tcp, siz);
1465 				mnew->m_len += siz;
1466 				mlen -= siz;
1467 				olen -= siz;
1468 				tcp += siz;
1469 				fcp += siz;
1470 			}
1471 			m = m->m_next;
1472 			if (m) {
1473 				olen = m->m_len;
1474 				fcp = mtod(m, caddr_t);
1475 			}
1476 		}
1477 
1478 		/*
1479 		 * Finally, set m_len == 0 for any trailing mbufs that have
1480 		 * been copied out of.
1481 		 */
1482 		while (m2) {
1483 			m2->m_len = 0;
1484 			m2 = m2->m_next;
1485 		}
1486 		return;
1487 	    }
1488 	    m = m->m_next;
1489 	}
1490 }
1491 
1492 /*
1493  * Socket upcall routine for the nfsd sockets.
1494  * The caddr_t arg is a pointer to the "struct nfssvc_sock".
1495  * Essentially do as much as possible non-blocking, else punt and it will
1496  * be called with M_WAIT from an nfsd.
1497  */
1498 void
1499 nfsrv_rcv(so, arg, waitflag)
1500 	struct socket *so;
1501 	caddr_t arg;
1502 	int waitflag;
1503 {
1504 	register struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
1505 	register struct mbuf *m;
1506 	struct mbuf *mp, *nam;
1507 	struct uio auio;
1508 	int flags, error;
1509 
1510 	if ((slp->ns_flag & SLP_VALID) == 0)
1511 		return;
1512 #ifdef notdef
1513 	/*
1514 	 * Define this to test for nfsds handling this under heavy load.
1515 	 */
1516 	if (waitflag == M_DONTWAIT) {
1517 		slp->ns_flag |= SLP_NEEDQ; goto dorecs;
1518 	}
1519 #endif
1520 	auio.uio_procp = NULL;
1521 	if (so->so_type == SOCK_STREAM) {
1522 		/*
1523 		 * If there are already records on the queue, defer soreceive()
1524 		 * to an nfsd so that there is feedback to the TCP layer that
1525 		 * the nfs servers are heavily loaded.
1526 		 */
1527 		if (slp->ns_rec && waitflag == M_DONTWAIT) {
1528 			slp->ns_flag |= SLP_NEEDQ;
1529 			goto dorecs;
1530 		}
1531 
1532 		/*
1533 		 * Do soreceive().
1534 		 */
1535 		auio.uio_resid = 1000000000;
1536 		flags = MSG_DONTWAIT;
1537 		error = soreceive(so, &nam, &auio, &mp, (struct mbuf **)0, &flags);
1538 		if (error || mp == (struct mbuf *)0) {
1539 			if (error == EWOULDBLOCK)
1540 				slp->ns_flag |= SLP_NEEDQ;
1541 			else
1542 				slp->ns_flag |= SLP_DISCONN;
1543 			goto dorecs;
1544 		}
1545 		m = mp;
1546 		if (slp->ns_rawend) {
1547 			slp->ns_rawend->m_next = m;
1548 			slp->ns_cc += 1000000000 - auio.uio_resid;
1549 		} else {
1550 			slp->ns_raw = m;
1551 			slp->ns_cc = 1000000000 - auio.uio_resid;
1552 		}
1553 		while (m->m_next)
1554 			m = m->m_next;
1555 		slp->ns_rawend = m;
1556 
1557 		/*
1558 		 * Now try and parse record(s) out of the raw stream data.
1559 		 */
1560 		if (error = nfsrv_getstream(slp, waitflag)) {
1561 			if (error == EPERM)
1562 				slp->ns_flag |= SLP_DISCONN;
1563 			else
1564 				slp->ns_flag |= SLP_NEEDQ;
1565 		}
1566 	} else {
1567 		do {
1568 			auio.uio_resid = 1000000000;
1569 			flags = MSG_DONTWAIT;
1570 			error = soreceive(so, &nam, &auio, &mp,
1571 						(struct mbuf **)0, &flags);
1572 			if (mp) {
1573 				nfs_realign(mp, 10 * NFSX_UNSIGNED);
1574 				if (nam) {
1575 					m = nam;
1576 					m->m_next = mp;
1577 				} else
1578 					m = mp;
1579 				if (slp->ns_recend)
1580 					slp->ns_recend->m_nextpkt = m;
1581 				else
1582 					slp->ns_rec = m;
1583 				slp->ns_recend = m;
1584 				m->m_nextpkt = (struct mbuf *)0;
1585 			}
1586 			if (error) {
1587 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
1588 					&& error != EWOULDBLOCK) {
1589 					slp->ns_flag |= SLP_DISCONN;
1590 					goto dorecs;
1591 				}
1592 			}
1593 		} while (mp);
1594 	}
1595 
1596 	/*
1597 	 * Now try and process the request records, non-blocking.
1598 	 */
1599 dorecs:
1600 	if (waitflag == M_DONTWAIT &&
1601 		(slp->ns_rec || (slp->ns_flag & (SLP_NEEDQ | SLP_DISCONN))))
1602 		nfsrv_wakenfsd(slp);
1603 }
1604 
1605 /*
1606  * Try and extract an RPC request from the mbuf data list received on a
1607  * stream socket. The "waitflag" argument indicates whether or not it
1608  * can sleep.
1609  */
1610 nfsrv_getstream(slp, waitflag)
1611 	register struct nfssvc_sock *slp;
1612 	int waitflag;
1613 {
1614 	register struct mbuf *m;
1615 	register char *cp1, *cp2;
1616 	register int len;
1617 	struct mbuf *om, *m2, *recm;
1618 	u_long recmark;
1619 
1620 	if (slp->ns_flag & SLP_GETSTREAM)
1621 		panic("nfs getstream");
1622 	slp->ns_flag |= SLP_GETSTREAM;
1623 	for (;;) {
1624 	    if (slp->ns_reclen == 0) {
1625 		if (slp->ns_cc < NFSX_UNSIGNED) {
1626 			slp->ns_flag &= ~SLP_GETSTREAM;
1627 			return (0);
1628 		}
1629 		m = slp->ns_raw;
1630 		if (m->m_len >= NFSX_UNSIGNED) {
1631 			bcopy(mtod(m, caddr_t), (caddr_t)&recmark, NFSX_UNSIGNED);
1632 			m->m_data += NFSX_UNSIGNED;
1633 			m->m_len -= NFSX_UNSIGNED;
1634 		} else {
1635 			cp1 = (caddr_t)&recmark;
1636 			cp2 = mtod(m, caddr_t);
1637 			while (cp1 < ((caddr_t)&recmark) + NFSX_UNSIGNED) {
1638 				while (m->m_len == 0) {
1639 					m = m->m_next;
1640 					cp2 = mtod(m, caddr_t);
1641 				}
1642 				*cp1++ = *cp2++;
1643 				m->m_data++;
1644 				m->m_len--;
1645 			}
1646 		}
1647 		slp->ns_cc -= NFSX_UNSIGNED;
1648 		slp->ns_reclen = ntohl(recmark) & ~0x80000000;
1649 		if (slp->ns_reclen < NFS_MINPACKET || slp->ns_reclen > NFS_MAXPACKET) {
1650 			slp->ns_flag &= ~SLP_GETSTREAM;
1651 			return (EPERM);
1652 		}
1653 	    }
1654 
1655 	    /*
1656 	     * Now get the record part.
1657 	     */
1658 	    if (slp->ns_cc == slp->ns_reclen) {
1659 		recm = slp->ns_raw;
1660 		slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
1661 		slp->ns_cc = slp->ns_reclen = 0;
1662 	    } else if (slp->ns_cc > slp->ns_reclen) {
1663 		len = 0;
1664 		m = slp->ns_raw;
1665 		om = (struct mbuf *)0;
1666 		while (len < slp->ns_reclen) {
1667 			if ((len + m->m_len) > slp->ns_reclen) {
1668 				m2 = m_copym(m, 0, slp->ns_reclen - len,
1669 					waitflag);
1670 				if (m2) {
1671 					if (om) {
1672 						om->m_next = m2;
1673 						recm = slp->ns_raw;
1674 					} else
1675 						recm = m2;
1676 					m->m_data += slp->ns_reclen - len;
1677 					m->m_len -= slp->ns_reclen - len;
1678 					len = slp->ns_reclen;
1679 				} else {
1680 					slp->ns_flag &= ~SLP_GETSTREAM;
1681 					return (EWOULDBLOCK);
1682 				}
1683 			} else if ((len + m->m_len) == slp->ns_reclen) {
1684 				om = m;
1685 				len += m->m_len;
1686 				m = m->m_next;
1687 				recm = slp->ns_raw;
1688 				om->m_next = (struct mbuf *)0;
1689 			} else {
1690 				om = m;
1691 				len += m->m_len;
1692 				m = m->m_next;
1693 			}
1694 		}
1695 		slp->ns_raw = m;
1696 		slp->ns_cc -= len;
1697 		slp->ns_reclen = 0;
1698 	    } else {
1699 		slp->ns_flag &= ~SLP_GETSTREAM;
1700 		return (0);
1701 	    }
1702 	    nfs_realign(recm, 10 * NFSX_UNSIGNED);
1703 	    if (slp->ns_recend)
1704 		slp->ns_recend->m_nextpkt = recm;
1705 	    else
1706 		slp->ns_rec = recm;
1707 	    slp->ns_recend = recm;
1708 	}
1709 }
1710 
1711 /*
1712  * Parse an RPC header.
1713  */
1714 nfsrv_dorec(slp, nd)
1715 	register struct nfssvc_sock *slp;
1716 	register struct nfsd *nd;
1717 {
1718 	register struct mbuf *m;
1719 	int error;
1720 
1721 	if ((slp->ns_flag & SLP_VALID) == 0 ||
1722 	    (m = slp->ns_rec) == (struct mbuf *)0)
1723 		return (ENOBUFS);
1724 	if (slp->ns_rec = m->m_nextpkt)
1725 		m->m_nextpkt = (struct mbuf *)0;
1726 	else
1727 		slp->ns_recend = (struct mbuf *)0;
1728 	if (m->m_type == MT_SONAME) {
1729 		nd->nd_nam = m;
1730 		nd->nd_md = nd->nd_mrep = m->m_next;
1731 		m->m_next = (struct mbuf *)0;
1732 	} else {
1733 		nd->nd_nam = (struct mbuf *)0;
1734 		nd->nd_md = nd->nd_mrep = m;
1735 	}
1736 	nd->nd_dpos = mtod(nd->nd_md, caddr_t);
1737 	if (error = nfs_getreq(nd, TRUE)) {
1738 		m_freem(nd->nd_nam);
1739 		return (error);
1740 	}
1741 	return (0);
1742 }
1743 
1744 /*
1745  * Parse an RPC request
1746  * - verify it
1747  * - fill in the cred struct.
1748  */
1749 nfs_getreq(nd, has_header)
1750 	register struct nfsd *nd;
1751 	int has_header;
1752 {
1753 	register int len, i;
1754 	register u_long *tl;
1755 	register long t1;
1756 	struct uio uio;
1757 	struct iovec iov;
1758 	caddr_t dpos, cp2;
1759 	u_long nfsvers, auth_type;
1760 	int error = 0, nqnfs = 0;
1761 	struct mbuf *mrep, *md;
1762 
1763 	mrep = nd->nd_mrep;
1764 	md = nd->nd_md;
1765 	dpos = nd->nd_dpos;
1766 	if (has_header) {
1767 		nfsm_dissect(tl, u_long *, 10*NFSX_UNSIGNED);
1768 		nd->nd_retxid = *tl++;
1769 		if (*tl++ != rpc_call) {
1770 			m_freem(mrep);
1771 			return (EBADRPC);
1772 		}
1773 	} else {
1774 		nfsm_dissect(tl, u_long *, 8*NFSX_UNSIGNED);
1775 	}
1776 	nd->nd_repstat = 0;
1777 	if (*tl++ != rpc_vers) {
1778 		nd->nd_repstat = ERPCMISMATCH;
1779 		nd->nd_procnum = NFSPROC_NOOP;
1780 		return (0);
1781 	}
1782 	nfsvers = nfs_vers;
1783 	if (*tl != nfs_prog) {
1784 		if (*tl == nqnfs_prog) {
1785 			nqnfs++;
1786 			nfsvers = nqnfs_vers;
1787 		} else {
1788 			nd->nd_repstat = EPROGUNAVAIL;
1789 			nd->nd_procnum = NFSPROC_NOOP;
1790 			return (0);
1791 		}
1792 	}
1793 	tl++;
1794 	if (*tl++ != nfsvers) {
1795 		nd->nd_repstat = EPROGMISMATCH;
1796 		nd->nd_procnum = NFSPROC_NOOP;
1797 		return (0);
1798 	}
1799 	nd->nd_procnum = fxdr_unsigned(u_long, *tl++);
1800 	if (nd->nd_procnum == NFSPROC_NULL)
1801 		return (0);
1802 	if (nd->nd_procnum >= NFS_NPROCS ||
1803 		(!nqnfs && nd->nd_procnum > NFSPROC_STATFS) ||
1804 		(*tl != rpc_auth_unix && *tl != rpc_auth_kerb)) {
1805 		nd->nd_repstat = EPROCUNAVAIL;
1806 		nd->nd_procnum = NFSPROC_NOOP;
1807 		return (0);
1808 	}
1809 	auth_type = *tl++;
1810 	len = fxdr_unsigned(int, *tl++);
1811 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
1812 		m_freem(mrep);
1813 		return (EBADRPC);
1814 	}
1815 
1816 	/*
1817 	 * Handle auth_unix or auth_kerb.
1818 	 */
1819 	if (auth_type == rpc_auth_unix) {
1820 		len = fxdr_unsigned(int, *++tl);
1821 		if (len < 0 || len > NFS_MAXNAMLEN) {
1822 			m_freem(mrep);
1823 			return (EBADRPC);
1824 		}
1825 		nfsm_adv(nfsm_rndup(len));
1826 		nfsm_dissect(tl, u_long *, 3*NFSX_UNSIGNED);
1827 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1828 		nd->nd_cr.cr_gid = fxdr_unsigned(gid_t, *tl++);
1829 		len = fxdr_unsigned(int, *tl);
1830 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
1831 			m_freem(mrep);
1832 			return (EBADRPC);
1833 		}
1834 		nfsm_dissect(tl, u_long *, (len + 2)*NFSX_UNSIGNED);
1835 		for (i = 1; i <= len; i++)
1836 			if (i < NGROUPS)
1837 				nd->nd_cr.cr_groups[i] = fxdr_unsigned(gid_t, *tl++);
1838 			else
1839 				tl++;
1840 		nd->nd_cr.cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
1841 	} else if (auth_type == rpc_auth_kerb) {
1842 		nd->nd_cr.cr_uid = fxdr_unsigned(uid_t, *tl++);
1843 		nd->nd_authlen = fxdr_unsigned(int, *tl);
1844 		iov.iov_len = uio.uio_resid = nfsm_rndup(nd->nd_authlen);
1845 		if (uio.uio_resid > (len - 2*NFSX_UNSIGNED)) {
1846 			m_freem(mrep);
1847 			return (EBADRPC);
1848 		}
1849 		uio.uio_offset = 0;
1850 		uio.uio_iov = &iov;
1851 		uio.uio_iovcnt = 1;
1852 		uio.uio_segflg = UIO_SYSSPACE;
1853 		iov.iov_base = (caddr_t)nd->nd_authstr;
1854 		nfsm_mtouio(&uio, uio.uio_resid);
1855 		nfsm_dissect(tl, u_long *, 2*NFSX_UNSIGNED);
1856 		nd->nd_flag |= NFSD_NEEDAUTH;
1857 	}
1858 
1859 	/*
1860 	 * Do we have any use for the verifier.
1861 	 * According to the "Remote Procedure Call Protocol Spec." it
1862 	 * should be AUTH_NULL, but some clients make it AUTH_UNIX?
1863 	 * For now, just skip over it
1864 	 */
1865 	len = fxdr_unsigned(int, *++tl);
1866 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
1867 		m_freem(mrep);
1868 		return (EBADRPC);
1869 	}
1870 	if (len > 0) {
1871 		nfsm_adv(nfsm_rndup(len));
1872 	}
1873 
1874 	/*
1875 	 * For nqnfs, get piggybacked lease request.
1876 	 */
1877 	if (nqnfs && nd->nd_procnum != NQNFSPROC_EVICTED) {
1878 		nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1879 		nd->nd_nqlflag = fxdr_unsigned(int, *tl);
1880 		if (nd->nd_nqlflag) {
1881 			nfsm_dissect(tl, u_long *, NFSX_UNSIGNED);
1882 			nd->nd_duration = fxdr_unsigned(int, *tl);
1883 		} else
1884 			nd->nd_duration = NQ_MINLEASE;
1885 	} else {
1886 		nd->nd_nqlflag = NQL_NOVAL;
1887 		nd->nd_duration = NQ_MINLEASE;
1888 	}
1889 	nd->nd_md = md;
1890 	nd->nd_dpos = dpos;
1891 	return (0);
1892 nfsmout:
1893 	return (error);
1894 }
1895 
1896 /*
1897  * Search for a sleeping nfsd and wake it up.
1898  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
1899  * running nfsds will go look for the work in the nfssvc_sock list.
1900  */
1901 void
1902 nfsrv_wakenfsd(slp)
1903 	struct nfssvc_sock *slp;
1904 {
1905 	register struct nfsd *nd = nfsd_head.nd_next;
1906 
1907 	if ((slp->ns_flag & SLP_VALID) == 0)
1908 		return;
1909 	while (nd != (struct nfsd *)&nfsd_head) {
1910 		if (nd->nd_flag & NFSD_WAITING) {
1911 			nd->nd_flag &= ~NFSD_WAITING;
1912 			if (nd->nd_slp)
1913 				panic("nfsd wakeup");
1914 			slp->ns_sref++;
1915 			nd->nd_slp = slp;
1916 			wakeup((caddr_t)nd);
1917 			return;
1918 		}
1919 		nd = nd->nd_next;
1920 	}
1921 	slp->ns_flag |= SLP_DOREC;
1922 	nfsd_head.nd_flag |= NFSD_CHECKSLP;
1923 }
1924 
1925 nfs_msg(p, server, msg)
1926 	struct proc *p;
1927 	char *server, *msg;
1928 {
1929 	tpr_t tpr;
1930 
1931 	if (p)
1932 		tpr = tprintf_open(p);
1933 	else
1934 		tpr = NULL;
1935 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
1936 	tprintf_close(tpr);
1937 }
1938