xref: /original-bsd/sys/nfs/nfs_socket.c (revision 95a66346)
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.20 (Berkeley) 03/19/91
11  */
12 
13 /*
14  * Socket operations for use by nfs
15  */
16 
17 #include "param.h"
18 #include "proc.h"
19 #include "mount.h"
20 #include "kernel.h"
21 #include "malloc.h"
22 #include "mbuf.h"
23 #include "vnode.h"
24 #include "domain.h"
25 #include "protosw.h"
26 #include "socket.h"
27 #include "socketvar.h"
28 #include "syslog.h"
29 #include "../netinet/in.h"
30 #include "../netinet/tcp.h"
31 
32 #include "rpcv2.h"
33 #include "nfsv2.h"
34 #include "nfs.h"
35 #include "xdr_subs.h"
36 #include "nfsm_subs.h"
37 #include "nfsmount.h"
38 
39 #define	TRUE	1
40 #define	FALSE	0
41 
42 /*
43  * External data, mostly RPC constants in XDR form
44  */
45 extern u_long rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers, rpc_auth_unix,
46 	rpc_msgaccepted, rpc_call;
47 extern u_long nfs_prog, nfs_vers;
48 /* Maybe these should be bits in a u_long ?? */
49 extern int nonidempotent[NFS_NPROCS];
50 static int compressrequest[NFS_NPROCS] = {
51 	FALSE,
52 	TRUE,
53 	TRUE,
54 	FALSE,
55 	TRUE,
56 	TRUE,
57 	TRUE,
58 	FALSE,
59 	FALSE,
60 	TRUE,
61 	TRUE,
62 	TRUE,
63 	TRUE,
64 	TRUE,
65 	TRUE,
66 	TRUE,
67 	TRUE,
68 	TRUE,
69 };
70 int	nfs_sbwait();
71 void	nfs_disconnect();
72 struct mbuf *nfs_compress(), *nfs_uncompress();
73 
74 int	nfsrv_null(),
75 	nfsrv_getattr(),
76 	nfsrv_setattr(),
77 	nfsrv_lookup(),
78 	nfsrv_readlink(),
79 	nfsrv_read(),
80 	nfsrv_write(),
81 	nfsrv_create(),
82 	nfsrv_remove(),
83 	nfsrv_rename(),
84 	nfsrv_link(),
85 	nfsrv_symlink(),
86 	nfsrv_mkdir(),
87 	nfsrv_rmdir(),
88 	nfsrv_readdir(),
89 	nfsrv_statfs(),
90 	nfsrv_noop();
91 
92 int (*nfsrv_procs[NFS_NPROCS])() = {
93 	nfsrv_null,
94 	nfsrv_getattr,
95 	nfsrv_setattr,
96 	nfsrv_noop,
97 	nfsrv_lookup,
98 	nfsrv_readlink,
99 	nfsrv_read,
100 	nfsrv_noop,
101 	nfsrv_write,
102 	nfsrv_create,
103 	nfsrv_remove,
104 	nfsrv_rename,
105 	nfsrv_link,
106 	nfsrv_symlink,
107 	nfsrv_mkdir,
108 	nfsrv_rmdir,
109 	nfsrv_readdir,
110 	nfsrv_statfs,
111 };
112 
113 struct nfsreq nfsreqh;
114 int nfsrexmtthresh = NFS_FISHY;
115 int nfs_tcpnodelay = 1;
116 
117 /*
118  * Initialize sockets and congestion for a new NFS connection.
119  * We do not free the sockaddr if error.
120  */
121 nfs_connect(nmp)
122 	register struct nfsmount *nmp;
123 {
124 	register struct socket *so;
125 	int s, error;
126 	struct mbuf *m;
127 
128 	nmp->nm_so = (struct socket *)0;
129 	if (error = socreate(mtod(nmp->nm_nam, struct sockaddr *)->sa_family,
130 		&nmp->nm_so, nmp->nm_sotype, nmp->nm_soproto))
131 		goto bad;
132 	so = nmp->nm_so;
133 	nmp->nm_soflags = so->so_proto->pr_flags;
134 
135 	/*
136 	 * Protocols that do not require connections may be optionally left
137 	 * unconnected for servers that reply from a port other than NFS_PORT.
138 	 */
139 	if (nmp->nm_flag & NFSMNT_NOCONN) {
140 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
141 			error = ENOTCONN;
142 			goto bad;
143 		}
144 	} else {
145 		if (error = soconnect(so, nmp->nm_nam))
146 			goto bad;
147 
148 		/*
149 		 * Wait for the connection to complete. Cribbed from the
150 		 * connect system call but with the wait at negative prio.
151 		 */
152 		s = splnet();
153 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0)
154 			(void) tsleep((caddr_t)&so->so_timeo, PSOCK, "nfscon", 0);
155 		splx(s);
156 		if (so->so_error) {
157 			error = so->so_error;
158 			goto bad;
159 		}
160 	}
161 	if (nmp->nm_sotype == SOCK_DGRAM) {
162 		if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_SPONGY | NFSMNT_INT)) {
163 			so->so_rcv.sb_timeo = (5 * hz);
164 			so->so_snd.sb_timeo = (5 * hz);
165 		} else {
166 			so->so_rcv.sb_timeo = 0;
167 			so->so_snd.sb_timeo = 0;
168 		}
169 		if (error = soreserve(so,
170 		    min(4 * (nmp->nm_wsize + NFS_MAXPKTHDR), NFS_MAXPACKET),
171 		    min(4 * (nmp->nm_rsize + NFS_MAXPKTHDR), NFS_MAXPACKET)))
172 			goto bad;
173 	} else {
174 		if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_SPONGY | NFSMNT_INT)) {
175 			so->so_rcv.sb_timeo = (5 * hz);
176 			so->so_snd.sb_timeo = (5 * hz);
177 		} else {
178 			so->so_rcv.sb_timeo = 0;
179 			so->so_snd.sb_timeo = 0;
180 		}
181 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
182 			MGET(m, M_WAIT, MT_SOOPTS);
183 			*mtod(m, int *) = 1;
184 			m->m_len = sizeof(int);
185 			sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
186 		}
187 		if (so->so_proto->pr_domain->dom_family == AF_INET &&
188 		    so->so_proto->pr_protocol == IPPROTO_TCP &&
189 		    nfs_tcpnodelay) {
190 			MGET(m, M_WAIT, MT_SOOPTS);
191 			*mtod(m, int *) = 1;
192 			m->m_len = sizeof(int);
193 			sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
194 		}
195 		if (error = soreserve(so,
196 		    min(4 * (nmp->nm_wsize + NFS_MAXPKTHDR + sizeof(u_long)),
197 		    NFS_MAXPACKET + sizeof(u_long)),
198 		    min(4 * (nmp->nm_rsize + NFS_MAXPKTHDR + sizeof(u_long)),
199 		    NFS_MAXPACKET + sizeof(u_long))))
200 			goto bad;
201 	}
202 	so->so_rcv.sb_flags |= SB_NOINTR;
203 	so->so_snd.sb_flags |= SB_NOINTR;
204 
205 	/* Initialize other non-zero congestion variables */
206 	nmp->nm_rto = NFS_TIMEO;
207 	nmp->nm_window = 2;		    /* Initial send window */
208 	nmp->nm_ssthresh = NFS_MAXWINDOW; /* Slowstart threshold */
209 	nmp->nm_rttvar = nmp->nm_rto << 1;
210 	nmp->nm_sent = 0;
211 	nmp->nm_currexmit = 0;
212 	return (0);
213 
214 bad:
215 	nfs_disconnect(nmp);
216 	return (error);
217 }
218 
219 /*
220  * Reconnect routine:
221  * Called when a connection is broken on a reliable protocol.
222  * - clean up the old socket
223  * - nfs_connect() again
224  * - set R_MUSTRESEND for all outstanding requests on mount point
225  * If this fails the mount point is DEAD!
226  * nb: Must be called with the nfs_solock() set on the mount point.
227  */
228 nfs_reconnect(rep, nmp)
229 	register struct nfsreq *rep;
230 	register struct nfsmount *nmp;
231 {
232 	register struct nfsreq *rp;
233 	int error;
234 
235 	if (rep->r_procp)
236 		tprintf(rep->r_procp->p_session,
237 			"Nfs server %s, trying reconnect\n",
238 			nmp->nm_mountp->mnt_stat.f_mntfromname);
239 	else
240 		tprintf(NULL, "Nfs server %s, trying a reconnect\n",
241 			nmp->nm_mountp->mnt_stat.f_mntfromname);
242 	while (error = nfs_connect(nmp)) {
243 #ifdef lint
244 		error = error;
245 #endif /* lint */
246 		if ((nmp->nm_flag & NFSMNT_INT) && nfs_sigintr(rep->r_procp))
247 			return (EINTR);
248 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfscon", 0);
249 	}
250 	if (rep->r_procp)
251 		tprintf(rep->r_procp->p_session,
252 			"Nfs server %s, reconnected\n",
253 			nmp->nm_mountp->mnt_stat.f_mntfromname);
254 	else
255 		tprintf(NULL, "Nfs server %s, reconnected\n",
256 			nmp->nm_mountp->mnt_stat.f_mntfromname);
257 
258 	/*
259 	 * Loop through outstanding request list and fix up all requests
260 	 * on old socket.
261 	 */
262 	rp = nfsreqh.r_next;
263 	while (rp != &nfsreqh) {
264 		if (rp->r_nmp == nmp)
265 			rp->r_flags |= R_MUSTRESEND;
266 		rp = rp->r_next;
267 	}
268 	return (0);
269 }
270 
271 /*
272  * NFS disconnect. Clean up and unlink.
273  */
274 void
275 nfs_disconnect(nmp)
276 	register struct nfsmount *nmp;
277 {
278 	register struct socket *so;
279 
280 	if (nmp->nm_so) {
281 		so = nmp->nm_so;
282 		nmp->nm_so = (struct socket *)0;
283 		soshutdown(so, 2);
284 		soclose(so);
285 	}
286 }
287 
288 /*
289  * This is the nfs send routine. For connection based socket types, it
290  * must be called with an nfs_solock() on the socket.
291  * "rep == NULL" indicates that it has been called from a server.
292  */
293 nfs_send(so, nam, top, rep)
294 	register struct socket *so;
295 	struct mbuf *nam;
296 	register struct mbuf *top;
297 	struct nfsreq *rep;
298 {
299 	struct mbuf *sendnam;
300 	int error, soflags;
301 
302 	if (rep) {
303 		if (rep->r_flags & R_SOFTTERM) {
304 			m_freem(top);
305 			return (EINTR);
306 		}
307 		if (rep->r_nmp->nm_so == NULL &&
308 		    (error = nfs_reconnect(rep, rep->r_nmp)))
309 			return (error);
310 		rep->r_flags &= ~R_MUSTRESEND;
311 		so = rep->r_nmp->nm_so;
312 		soflags = rep->r_nmp->nm_soflags;
313 	} else
314 		soflags = so->so_proto->pr_flags;
315 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
316 		sendnam = (struct mbuf *)0;
317 	else
318 		sendnam = nam;
319 
320 	error = sosend(so, sendnam, (struct uio *)0, top,
321 		(struct mbuf *)0, 0);
322 	if (error == EWOULDBLOCK && rep) {
323 		if (rep->r_flags & R_SOFTTERM)
324 			error = EINTR;
325 		else {
326 			rep->r_flags |= R_MUSTRESEND;
327 			error = 0;
328 		}
329 	}
330 	/*
331 	 * Ignore socket errors??
332 	 */
333 	if (error && error != EINTR && error != ERESTART)
334 		error = 0;
335 	return (error);
336 }
337 
338 /*
339  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
340  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
341  * Mark and consolidate the data into a new mbuf list.
342  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
343  *     small mbufs.
344  * For SOCK_STREAM we must be very careful to read an entire record once
345  * we have read any of it, even if the system call has been interrupted.
346  */
347 nfs_receive(so, aname, mp, rep)
348 	register struct socket *so;
349 	struct mbuf **aname;
350 	struct mbuf **mp;
351 	register struct nfsreq *rep;
352 {
353 	struct uio auio;
354 	struct iovec aio;
355 	register struct mbuf *m;
356 	struct mbuf *m2, *mnew, **mbp;
357 	caddr_t fcp, tcp;
358 	u_long len;
359 	struct mbuf **getnam;
360 	int error, siz, mlen, soflags, rcvflg = MSG_WAITALL;
361 
362 	/*
363 	 * Set up arguments for soreceive()
364 	 */
365 	*mp = (struct mbuf *)0;
366 	*aname = (struct mbuf *)0;
367 	if (rep)
368 		soflags = rep->r_nmp->nm_soflags;
369 	else
370 		soflags = so->so_proto->pr_flags;
371 
372 	/*
373 	 * For reliable protocols, lock against other senders/receivers
374 	 * in case a reconnect is necessary.
375 	 * For SOCK_STREAM, first get the Record Mark to find out how much
376 	 * more there is to get.
377 	 * We must lock the socket against other receivers
378 	 * until we have an entire rpc request/reply.
379 	 */
380 	if (soflags & PR_CONNREQUIRED) {
381 tryagain:
382 		/*
383 		 * Check for fatal errors and resending request.
384 		 */
385 		if (rep) {
386 			/*
387 			 * Ugh: If a reconnect attempt just happened, nm_so
388 			 * would have changed. NULL indicates a failed
389 			 * attempt that has essentially shut down this
390 			 * mount point.
391 			 */
392 			if (rep->r_mrep || (so = rep->r_nmp->nm_so) == NULL ||
393 				(rep->r_flags & R_SOFTTERM))
394 				return (EINTR);
395 			while (rep->r_flags & R_MUSTRESEND) {
396 				m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
397 				nfsstats.rpcretries++;
398 				if (error = nfs_send(so, rep->r_nmp->nm_nam, m,
399 					rep))
400 					goto errout;
401 			}
402 		}
403 		if ((soflags & PR_ATOMIC) == 0) {
404 			aio.iov_base = (caddr_t) &len;
405 			aio.iov_len = sizeof(u_long);
406 			auio.uio_iov = &aio;
407 			auio.uio_iovcnt = 1;
408 			auio.uio_segflg = UIO_SYSSPACE;
409 			auio.uio_rw = UIO_READ;
410 			auio.uio_offset = 0;
411 			auio.uio_resid = sizeof(u_long);
412 			do {
413 			   error = soreceive(so, (struct mbuf **)0, &auio,
414 				(struct mbuf **)0, (struct mbuf **)0, &rcvflg);
415 			   if (error == EWOULDBLOCK && rep) {
416 				if (rep->r_flags & R_SOFTTERM)
417 					return (EINTR);
418 				if (rep->r_flags & R_MUSTRESEND)
419 					goto tryagain;
420 			   }
421 			} while (error == EWOULDBLOCK);
422 			if (!error && auio.uio_resid > 0)
423 				error = EPIPE;
424 			if (error)
425 				goto errout;
426 			len = ntohl(len) & ~0x80000000;
427 			/*
428 			 * This is SERIOUS! We are out of sync with the sender
429 			 * and forcing a disconnect/reconnect is all I can do.
430 			 */
431 			if (len > NFS_MAXPACKET) {
432 				error = EFBIG;
433 				goto errout;
434 			}
435 			auio.uio_resid = len;
436 			do {
437 			    error =  soreceive(so, (struct mbuf **)0,
438 				&auio, mp, (struct mbuf **)0, &rcvflg);
439 			} while (error == EWOULDBLOCK || error == EINTR ||
440 				 error == ERESTART);
441 			if (!error && auio.uio_resid > 0)
442 				error = EPIPE;
443 		} else {
444 			auio.uio_resid = len = 1000000;	/* Anything Big */
445 			do {
446 			    error =  soreceive(so, (struct mbuf **)0,
447 				&auio, mp, (struct mbuf **)0, &rcvflg);
448 			    if (error == EWOULDBLOCK && rep) {
449 				if (rep->r_flags & R_SOFTTERM)
450 					return (EINTR);
451 				if (rep->r_flags & R_MUSTRESEND)
452 					goto tryagain;
453 			    }
454 			} while (error == EWOULDBLOCK);
455 			if (!error && *mp == NULL)
456 				error = EPIPE;
457 			len -= auio.uio_resid;
458 		}
459 errout:
460 		if (error && rep && error != EINTR && error != ERESTART) {
461 			m_freem(*mp);
462 			*mp = (struct mbuf *)0;
463 			nfs_disconnect(rep->r_nmp);
464 			error = nfs_reconnect(rep, rep->r_nmp);
465 			if (!error)
466 				goto tryagain;
467 		}
468 	} else {
469 		if (so->so_state & SS_ISCONNECTED)
470 			getnam = (struct mbuf **)0;
471 		else
472 			getnam = aname;
473 		auio.uio_resid = len = 1000000;
474 		do {
475 			error =  soreceive(so, getnam, &auio, mp,
476 				(struct mbuf **)0, &rcvflg);
477 			if (error == EWOULDBLOCK && rep &&
478 			    (rep->r_flags & R_SOFTTERM))
479 				return (EINTR);
480 		} while (error == EWOULDBLOCK);
481 		len -= auio.uio_resid;
482 	}
483 	if (error) {
484 		m_freem(*mp);
485 		*mp = (struct mbuf *)0;
486 	}
487 	/*
488 	 * Search for any mbufs that are not a multiple of 4 bytes long.
489 	 * These could cause pointer alignment problems, so copy them to
490 	 * well aligned mbufs.
491 	 */
492 	m = *mp;
493 	mbp = mp;
494 	while (m) {
495 		/*
496 		 * All this for something that may never happen.
497 		 */
498 		if (m->m_next && (m->m_len & 0x3)) {
499 			printf("nfs_rcv odd length!\n");
500 			mlen = 0;
501 			while (m) {
502 				fcp = mtod(m, caddr_t);
503 				while (m->m_len > 0) {
504 					if (mlen == 0) {
505 						MGET(m2, M_WAIT, MT_DATA);
506 						if (len >= MINCLSIZE)
507 							MCLGET(m2, M_WAIT);
508 						m2->m_len = 0;
509 						mlen = M_TRAILINGSPACE(m2);
510 						tcp = mtod(m2, caddr_t);
511 						*mbp = m2;
512 						mbp = &m2->m_next;
513 					}
514 					siz = MIN(mlen, m->m_len);
515 					bcopy(fcp, tcp, siz);
516 					m2->m_len += siz;
517 					mlen -= siz;
518 					len -= siz;
519 					tcp += siz;
520 					m->m_len -= siz;
521 					fcp += siz;
522 				}
523 				MFREE(m, mnew);
524 				m = mnew;
525 			}
526 			break;
527 		}
528 		len -= m->m_len;
529 		mbp = &m->m_next;
530 		m = m->m_next;
531 	}
532 	return (error);
533 }
534 
535 /*
536  * Implement receipt of reply on a socket.
537  * We must search through the list of received datagrams matching them
538  * with outstanding requests using the xid, until ours is found.
539  */
540 /* ARGSUSED */
541 nfs_reply(nmp, myrep)
542 	struct nfsmount *nmp;
543 	struct nfsreq *myrep;
544 {
545 	register struct mbuf *m;
546 	register struct nfsreq *rep;
547 	register int error = 0;
548 	u_long rxid;
549 	struct mbuf *mp, *nam;
550 	char *cp;
551 	int cnt, xfer;
552 
553 	/*
554 	 * Loop around until we get our own reply
555 	 */
556 	for (;;) {
557 		/*
558 		 * Lock against other receivers so that I don't get stuck in
559 		 * sbwait() after someone else has received my reply for me.
560 		 * Also necessary for connection based protocols to avoid
561 		 * race conditions during a reconnect.
562 		 */
563 		nfs_solock(&nmp->nm_flag);
564 		/* Already received, bye bye */
565 		if (myrep->r_mrep != NULL) {
566 			nfs_sounlock(&nmp->nm_flag);
567 			return (0);
568 		}
569 		/*
570 		 * Get the next Rpc reply off the socket
571 		 */
572 		if (error = nfs_receive(nmp->nm_so, &nam, &mp, myrep)) {
573 			nfs_sounlock(&nmp->nm_flag);
574 
575 			/*
576 			 * Ignore routing errors on connectionless protocols??
577 			 */
578 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
579 				nmp->nm_so->so_error = 0;
580 				continue;
581 			}
582 
583 			/*
584 			 * Otherwise cleanup and return a fatal error.
585 			 */
586 			if (myrep->r_flags & R_TIMING) {
587 				myrep->r_flags &= ~R_TIMING;
588 				nmp->nm_rtt = -1;
589 			}
590 			if (myrep->r_flags & R_SENT) {
591 				myrep->r_flags &= ~R_SENT;
592 				nmp->nm_sent--;
593 			}
594 			return (error);
595 		}
596 
597 		/*
598 		 * Get the xid and check that it is an rpc reply
599 		 */
600 		m = mp;
601 		while (m && m->m_len == 0)
602 			m = m->m_next;
603 		if (m == NULL) {
604 			nfsstats.rpcinvalid++;
605 			m_freem(mp);
606 			nfs_sounlock(&nmp->nm_flag);
607 			continue;
608 		}
609 		bcopy(mtod(m, caddr_t), (caddr_t)&rxid, NFSX_UNSIGNED);
610 		/*
611 		 * Loop through the request list to match up the reply
612 		 * Iff no match, just drop the datagram
613 		 */
614 		m = mp;
615 		rep = nfsreqh.r_next;
616 		while (rep != &nfsreqh) {
617 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
618 				/* Found it.. */
619 				rep->r_mrep = m;
620 				/*
621 				 * Update timing
622 				 */
623 				if (rep->r_flags & R_TIMING) {
624 					nfs_updatetimer(rep->r_nmp);
625 					rep->r_flags &= ~R_TIMING;
626 					rep->r_nmp->nm_rtt = -1;
627 				}
628 				if (rep->r_flags & R_SENT) {
629 					rep->r_flags &= ~R_SENT;
630 					rep->r_nmp->nm_sent--;
631 				}
632 				break;
633 			}
634 			rep = rep->r_next;
635 		}
636 		nfs_sounlock(&nmp->nm_flag);
637 		if (nam)
638 			m_freem(nam);
639 		/*
640 		 * If not matched to a request, drop it.
641 		 * If it's mine, get out.
642 		 */
643 		if (rep == &nfsreqh) {
644 			nfsstats.rpcunexpected++;
645 			m_freem(m);
646 		} else if (rep == myrep)
647 			return (0);
648 	}
649 }
650 
651 /*
652  * nfs_request - goes something like this
653  *	- fill in request struct
654  *	- links it into list
655  *	- calls nfs_send() for first transmit
656  *	- calls nfs_receive() to get reply
657  *	- break down rpc header and return with nfs reply pointed to
658  *	  by mrep or error
659  * nb: always frees up mreq mbuf list
660  */
661 nfs_request(vp, mreq, xid, procnum, procp, tryhard, mp, mrp, mdp, dposp)
662 	struct vnode *vp;
663 	struct mbuf *mreq;
664 	u_long xid;
665 	int procnum;
666 	struct proc *procp;
667 	int tryhard;
668 	struct mount *mp;
669 	struct mbuf **mrp;
670 	struct mbuf **mdp;
671 	caddr_t *dposp;
672 {
673 	register struct mbuf *m, *mrep;
674 	register struct nfsreq *rep;
675 	register u_long *p;
676 	register int len;
677 	struct nfsmount *nmp;
678 	struct mbuf *md;
679 	struct nfsreq *reph;
680 	caddr_t dpos;
681 	char *cp2;
682 	int t1;
683 	int s, compressed;
684 	int error = 0;
685 
686 	nmp = VFSTONFS(mp);
687 	m = mreq;
688 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
689 	rep->r_xid = xid;
690 	rep->r_nmp = nmp;
691 	rep->r_vp = vp;
692 	rep->r_procp = procp;
693 	if ((nmp->nm_flag & NFSMNT_SOFT) ||
694 	    ((nmp->nm_flag & NFSMNT_SPONGY) && !tryhard))
695 		rep->r_retry = nmp->nm_retry;
696 	else
697 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
698 	rep->r_flags = rep->r_rexmit = 0;
699 	/*
700 	 * Three cases:
701 	 * - non-idempotent requests on SOCK_DGRAM use NFS_MINIDEMTIMEO
702 	 * - idempotent requests on SOCK_DGRAM use 0
703 	 * - Reliable transports, NFS_RELIABLETIMEO
704 	 *   Timeouts are still done on reliable transports to ensure detection
705 	 *   of excessive connection delay.
706 	 */
707 	if (nmp->nm_sotype != SOCK_DGRAM)
708 		rep->r_timerinit = -NFS_RELIABLETIMEO;
709 	else if (nonidempotent[procnum])
710 		rep->r_timerinit = -NFS_MINIDEMTIMEO;
711 	else
712 		rep->r_timerinit = 0;
713 	rep->r_timer = rep->r_timerinit;
714 	rep->r_mrep = NULL;
715 	len = 0;
716 	while (m) {
717 		len += m->m_len;
718 		m = m->m_next;
719 	}
720 	mreq->m_pkthdr.len = len;
721 	mreq->m_pkthdr.rcvif = (struct ifnet *)0;
722 	compressed = 0;
723 	m = mreq;
724 	if ((nmp->nm_flag & NFSMNT_COMPRESS) && compressrequest[procnum]) {
725 		mreq = nfs_compress(mreq);
726 		if (mreq != m) {
727 			len = mreq->m_pkthdr.len;
728 			compressed++;
729 		}
730 	}
731 	/*
732 	 * For non-atomic protocols, insert a Sun RPC Record Mark.
733 	 */
734 	if ((nmp->nm_soflags & PR_ATOMIC) == 0) {
735 		M_PREPEND(mreq, sizeof(u_long), M_WAIT);
736 		*mtod(mreq, u_long *) = htonl(0x80000000 | len);
737 	}
738 	rep->r_mreq = mreq;
739 
740 	/*
741 	 * Do the client side RPC.
742 	 */
743 	nfsstats.rpcrequests++;
744 	/*
745 	 * Chain request into list of outstanding requests. Be sure
746 	 * to put it LAST so timer finds oldest requests first.
747 	 */
748 	s = splnet();
749 	reph = &nfsreqh;
750 	reph->r_prev->r_next = rep;
751 	rep->r_prev = reph->r_prev;
752 	reph->r_prev = rep;
753 	rep->r_next = reph;
754 	/*
755 	 * If backing off another request or avoiding congestion, don't
756 	 * send this one now but let timer do it. If not timing a request,
757 	 * do it now.
758 	 */
759 	if (nmp->nm_sent <= 0 || nmp->nm_sotype != SOCK_DGRAM ||
760 	    (nmp->nm_currexmit == 0 && nmp->nm_sent < nmp->nm_window)) {
761 		nmp->nm_sent++;
762 		rep->r_flags |= R_SENT;
763 		if (nmp->nm_rtt == -1) {
764 			nmp->nm_rtt = 0;
765 			rep->r_flags |= R_TIMING;
766 		}
767 		splx(s);
768 		m = m_copym(mreq, 0, M_COPYALL, M_WAIT);
769 		if (nmp->nm_soflags & PR_CONNREQUIRED)
770 			nfs_solock(&nmp->nm_flag);
771 		error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep);
772 		if (nmp->nm_soflags & PR_CONNREQUIRED)
773 			nfs_sounlock(&nmp->nm_flag);
774 		if (error && NFSIGNORE_SOERROR(nmp->nm_soflags, error))
775 			nmp->nm_so->so_error = error = 0;
776 	} else
777 		splx(s);
778 
779 	/*
780 	 * Wait for the reply from our send or the timer's.
781 	 */
782 	if (!error)
783 		error = nfs_reply(nmp, rep);
784 
785 	/*
786 	 * RPC done, unlink the request.
787 	 */
788 	s = splnet();
789 	rep->r_prev->r_next = rep->r_next;
790 	rep->r_next->r_prev = rep->r_prev;
791 	splx(s);
792 
793 	/*
794 	 * If there was a successful reply and a tprintf msg.
795 	 * tprintf a response.
796 	 */
797 	if (!error && (rep->r_flags & R_TPRINTFMSG)) {
798 		if (rep->r_procp)
799 			tprintf(rep->r_procp->p_session,
800 				"Nfs server %s, is alive again\n",
801 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
802 		else
803 			tprintf(NULL, "Nfs server %s, is alive again\n",
804 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
805 	}
806 	m_freem(rep->r_mreq);
807 	mrep = rep->r_mrep;
808 	FREE((caddr_t)rep, M_NFSREQ);
809 	if (error)
810 		return (error);
811 
812 	if (compressed)
813 		mrep = nfs_uncompress(mrep);
814 	md = mrep;
815 	/*
816 	 * break down the rpc header and check if ok
817 	 */
818 	dpos = mtod(md, caddr_t);
819 	nfsm_disect(p, u_long *, 5*NFSX_UNSIGNED);
820 	p += 2;
821 	if (*p++ == rpc_msgdenied) {
822 		if (*p == rpc_mismatch)
823 			error = EOPNOTSUPP;
824 		else
825 			error = EACCES;
826 		m_freem(mrep);
827 		return (error);
828 	}
829 	/*
830 	 * skip over the auth_verf, someday we may want to cache auth_short's
831 	 * for nfs_reqhead(), but for now just dump it
832 	 */
833 	if (*++p != 0) {
834 		len = nfsm_rndup(fxdr_unsigned(long, *p));
835 		nfsm_adv(len);
836 	}
837 	nfsm_disect(p, u_long *, NFSX_UNSIGNED);
838 	/* 0 == ok */
839 	if (*p == 0) {
840 		nfsm_disect(p, u_long *, NFSX_UNSIGNED);
841 		if (*p != 0) {
842 			error = fxdr_unsigned(int, *p);
843 			m_freem(mrep);
844 			return (error);
845 		}
846 		*mrp = mrep;
847 		*mdp = md;
848 		*dposp = dpos;
849 		return (0);
850 	}
851 	m_freem(mrep);
852 	return (EPROTONOSUPPORT);
853 nfsmout:
854 	return (error);
855 }
856 
857 /*
858  * Get a request for the server main loop
859  * - receive a request via. nfs_soreceive()
860  * - verify it
861  * - fill in the cred struct.
862  */
863 nfs_getreq(so, prog, vers, maxproc, nam, mrp, mdp, dposp, retxid, procnum, cr,
864 	msk, mtch, wascomp)
865 	struct socket *so;
866 	u_long prog;
867 	u_long vers;
868 	int maxproc;
869 	struct mbuf **nam;
870 	struct mbuf **mrp;
871 	struct mbuf **mdp;
872 	caddr_t *dposp;
873 	u_long *retxid;
874 	u_long *procnum;
875 	register struct ucred *cr;
876 	struct mbuf *msk, *mtch;
877 	int *wascomp;
878 {
879 	register int i;
880 	register u_long *p;
881 	register long t1;
882 	caddr_t dpos, cp2;
883 	int error = 0;
884 	struct mbuf *mrep, *md;
885 	int len;
886 
887 	if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
888 		error = nfs_receive(so, nam, &mrep, (struct nfsreq *)0);
889 	} else {
890 		mrep = (struct mbuf *)0;
891 		do {
892 			if (mrep) {
893 				m_freem(*nam);
894 				m_freem(mrep);
895 			}
896 			error = nfs_receive(so, nam, &mrep, (struct nfsreq *)0);
897 		} while (!error && nfs_badnam(*nam, msk, mtch));
898 	}
899 	if (error)
900 		return (error);
901 	md = mrep;
902 	mrep = nfs_uncompress(mrep);
903 	if (mrep != md) {
904 		*wascomp = 1;
905 		md = mrep;
906 	} else
907 		*wascomp = 0;
908 	dpos = mtod(mrep, caddr_t);
909 	nfsm_disect(p, u_long *, 10*NFSX_UNSIGNED);
910 	*retxid = *p++;
911 	if (*p++ != rpc_call) {
912 		m_freem(mrep);
913 		return (ERPCMISMATCH);
914 	}
915 	if (*p++ != rpc_vers) {
916 		m_freem(mrep);
917 		return (ERPCMISMATCH);
918 	}
919 	if (*p++ != prog) {
920 		m_freem(mrep);
921 		return (EPROGUNAVAIL);
922 	}
923 	if (*p++ != vers) {
924 		m_freem(mrep);
925 		return (EPROGMISMATCH);
926 	}
927 	*procnum = fxdr_unsigned(u_long, *p++);
928 	if (*procnum == NFSPROC_NULL) {
929 		*mrp = mrep;
930 		return (0);
931 	}
932 	if (*procnum > maxproc || *p++ != rpc_auth_unix) {
933 		m_freem(mrep);
934 		return (EPROCUNAVAIL);
935 	}
936 	len = fxdr_unsigned(int, *p++);
937 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
938 		m_freem(mrep);
939 		return (EBADRPC);
940 	}
941 	len = fxdr_unsigned(int, *++p);
942 	if (len < 0 || len > NFS_MAXNAMLEN) {
943 		m_freem(mrep);
944 		return (EBADRPC);
945 	}
946 	nfsm_adv(nfsm_rndup(len));
947 	nfsm_disect(p, u_long *, 3*NFSX_UNSIGNED);
948 	cr->cr_uid = fxdr_unsigned(uid_t, *p++);
949 	cr->cr_gid = fxdr_unsigned(gid_t, *p++);
950 	len = fxdr_unsigned(int, *p);
951 	if (len < 0 || len > RPCAUTH_UNIXGIDS) {
952 		m_freem(mrep);
953 		return (EBADRPC);
954 	}
955 	nfsm_disect(p, u_long *, (len + 2)*NFSX_UNSIGNED);
956 	for (i = 1; i <= len; i++)
957 		if (i < NGROUPS)
958 			cr->cr_groups[i] = fxdr_unsigned(gid_t, *p++);
959 		else
960 			p++;
961 	cr->cr_ngroups = (len >= NGROUPS) ? NGROUPS : (len + 1);
962 	/*
963 	 * Do we have any use for the verifier.
964 	 * According to the "Remote Procedure Call Protocol Spec." it
965 	 * should be AUTH_NULL, but some clients make it AUTH_UNIX?
966 	 * For now, just skip over it
967 	 */
968 	len = fxdr_unsigned(int, *++p);
969 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
970 		m_freem(mrep);
971 		return (EBADRPC);
972 	}
973 	if (len > 0)
974 		nfsm_adv(nfsm_rndup(len));
975 	*mrp = mrep;
976 	*mdp = md;
977 	*dposp = dpos;
978 	return (0);
979 nfsmout:
980 	return (error);
981 }
982 
983 /*
984  * Generate the rpc reply header
985  * siz arg. is used to decide if adding a cluster is worthwhile
986  */
987 nfs_rephead(siz, retxid, err, mrq, mbp, bposp)
988 	int siz;
989 	u_long retxid;
990 	int err;
991 	struct mbuf **mrq;
992 	struct mbuf **mbp;
993 	caddr_t *bposp;
994 {
995 	register u_long *p;
996 	register long t1;
997 	caddr_t bpos;
998 	struct mbuf *mreq, *mb, *mb2;
999 
1000 	NFSMGETHDR(mreq);
1001 	mb = mreq;
1002 	if ((siz+RPC_REPLYSIZ) > MHLEN)
1003 		MCLGET(mreq, M_WAIT);
1004 	p = mtod(mreq, u_long *);
1005 	mreq->m_len = 6*NFSX_UNSIGNED;
1006 	bpos = ((caddr_t)p)+mreq->m_len;
1007 	*p++ = retxid;
1008 	*p++ = rpc_reply;
1009 	if (err == ERPCMISMATCH) {
1010 		*p++ = rpc_msgdenied;
1011 		*p++ = rpc_mismatch;
1012 		*p++ = txdr_unsigned(2);
1013 		*p = txdr_unsigned(2);
1014 	} else {
1015 		*p++ = rpc_msgaccepted;
1016 		*p++ = 0;
1017 		*p++ = 0;
1018 		switch (err) {
1019 		case EPROGUNAVAIL:
1020 			*p = txdr_unsigned(RPC_PROGUNAVAIL);
1021 			break;
1022 		case EPROGMISMATCH:
1023 			*p = txdr_unsigned(RPC_PROGMISMATCH);
1024 			nfsm_build(p, u_long *, 2*NFSX_UNSIGNED);
1025 			*p++ = txdr_unsigned(2);
1026 			*p = txdr_unsigned(2);	/* someday 3 */
1027 			break;
1028 		case EPROCUNAVAIL:
1029 			*p = txdr_unsigned(RPC_PROCUNAVAIL);
1030 			break;
1031 		default:
1032 			*p = 0;
1033 			if (err != VNOVAL) {
1034 				nfsm_build(p, u_long *, NFSX_UNSIGNED);
1035 				*p = txdr_unsigned(err);
1036 			}
1037 			break;
1038 		};
1039 	}
1040 	*mrq = mreq;
1041 	*mbp = mb;
1042 	*bposp = bpos;
1043 	if (err != 0 && err != VNOVAL)
1044 		nfsstats.srvrpc_errs++;
1045 	return (0);
1046 }
1047 
1048 /*
1049  * Nfs timer routine
1050  * Scan the nfsreq list and retranmit any requests that have timed out
1051  * To avoid retransmission attempts on STREAM sockets (in the future) make
1052  * sure to set the r_retry field to 0 (implies nm_retry == 0).
1053  */
1054 nfs_timer()
1055 {
1056 	register struct nfsreq *rep;
1057 	register struct mbuf *m;
1058 	register struct socket *so;
1059 	register struct nfsmount *nmp;
1060 	int s, error;
1061 
1062 	s = splnet();
1063 	for (rep = nfsreqh.r_next; rep != &nfsreqh; rep = rep->r_next) {
1064 		nmp = rep->r_nmp;
1065 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM) ||
1066 		    (so = nmp->nm_so) == NULL)
1067 			continue;
1068 		if ((nmp->nm_flag & NFSMNT_INT) && nfs_sigintr(rep->r_procp)) {
1069 			rep->r_flags |= R_SOFTTERM;
1070 			continue;
1071 		}
1072 		if (rep->r_flags & R_TIMING)	/* update rtt in mount */
1073 			nmp->nm_rtt++;
1074 		/* If not timed out */
1075 		if (++rep->r_timer < nmp->nm_rto)
1076 			continue;
1077 		/* Do backoff and save new timeout in mount */
1078 		if (rep->r_flags & R_TIMING) {
1079 			nfs_backofftimer(nmp);
1080 			rep->r_flags &= ~R_TIMING;
1081 			nmp->nm_rtt = -1;
1082 		}
1083 		if (rep->r_flags & R_SENT) {
1084 			rep->r_flags &= ~R_SENT;
1085 			nmp->nm_sent--;
1086 		}
1087 
1088 		/*
1089 		 * Check for too many retries on soft mount.
1090 		 * nb: For hard mounts, r_retry == NFS_MAXREXMIT+1
1091 		 */
1092 		if (++rep->r_rexmit > NFS_MAXREXMIT)
1093 			rep->r_rexmit = NFS_MAXREXMIT;
1094 
1095 		/*
1096 		 * Check for server not responding
1097 		 */
1098 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
1099 		     rep->r_rexmit > NFS_FISHY) {
1100 			if (rep->r_procp && rep->r_procp->p_session)
1101 				tprintf(rep->r_procp->p_session,
1102 					"Nfs server %s, not responding\n",
1103 					nmp->nm_mountp->mnt_stat.f_mntfromname);
1104 			else
1105 				tprintf(NULL,
1106 					"Nfs server %s, not responding\n",
1107 					nmp->nm_mountp->mnt_stat.f_mntfromname);
1108 			rep->r_flags |= R_TPRINTFMSG;
1109 		}
1110 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
1111 			nfsstats.rpctimeouts++;
1112 			rep->r_flags |= R_SOFTTERM;
1113 			continue;
1114 		}
1115 		if (nmp->nm_sotype != SOCK_DGRAM)
1116 			continue;
1117 
1118 		/*
1119 		 * If there is enough space and the window allows..
1120 		 *	Resend it
1121 		 */
1122 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
1123 		       nmp->nm_sent < nmp->nm_window &&
1124 		       (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
1125 			nfsstats.rpcretries++;
1126 			if ((nmp->nm_flag & NFSMNT_NOCONN) == 0)
1127 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1128 			    (caddr_t)0, (struct mbuf *)0, (struct mbuf *)0);
1129 			else
1130 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
1131 			    nmp->nm_nam, (struct mbuf *)0, (struct mbuf *)0);
1132 			if (error) {
1133 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error))
1134 					so->so_error = 0;
1135 			} else {
1136 				/*
1137 				 * We need to time the request even though we
1138 				 * are retransmitting.
1139 				 */
1140 				nmp->nm_rtt = 0;
1141 				nmp->nm_sent++;
1142 				rep->r_flags |= (R_SENT|R_TIMING);
1143 				rep->r_timer = rep->r_timerinit;
1144 			}
1145 		}
1146 	}
1147 	splx(s);
1148 	timeout(nfs_timer, (caddr_t)0, hz/NFS_HZ);
1149 }
1150 
1151 /*
1152  * NFS timer update and backoff. The "Jacobson/Karels/Karn" scheme is
1153  * used here. The timer state is held in the nfsmount structure and
1154  * a single request is used to clock the response. When successful
1155  * the rtt smoothing in nfs_updatetimer is used, when failed the backoff
1156  * is done by nfs_backofftimer. We also log failure messages in these
1157  * routines.
1158  *
1159  * Congestion variables are held in the nfshost structure which
1160  * is referenced by nfsmounts and shared per-server. This separation
1161  * makes it possible to do per-mount timing which allows varying disk
1162  * access times to be dealt with, while preserving a network oriented
1163  * congestion control scheme.
1164  *
1165  * The windowing implements the Jacobson/Karels slowstart algorithm
1166  * with adjusted scaling factors. We start with one request, then send
1167  * 4 more after each success until the ssthresh limit is reached, then
1168  * we increment at a rate proportional to the window. On failure, we
1169  * remember 3/4 the current window and clamp the send limit to 1. Note
1170  * ICMP source quench is not reflected in so->so_error so we ignore that
1171  * for now.
1172  *
1173  * NFS behaves much more like a transport protocol with these changes,
1174  * shedding the teenage pedal-to-the-metal tendencies of "other"
1175  * implementations.
1176  *
1177  * Timers and congestion avoidance by Tom Talpey, Open Software Foundation.
1178  */
1179 
1180 /*
1181  * The TCP algorithm was not forgiving enough. Because the NFS server
1182  * responds only after performing lookups/diskio/etc, we have to be
1183  * more prepared to accept a spiky variance. The TCP algorithm is:
1184  * TCP_RTO(nmp) ((((nmp)->nm_srtt >> 2) + (nmp)->nm_rttvar) >> 1)
1185  */
1186 #define NFS_RTO(nmp)	(((nmp)->nm_srtt >> 3) + (nmp)->nm_rttvar)
1187 
1188 nfs_updatetimer(nmp)
1189 	register struct nfsmount *nmp;
1190 {
1191 
1192 	/* If retransmitted, clear and return */
1193 	if (nmp->nm_rexmit || nmp->nm_currexmit) {
1194 		nmp->nm_rexmit = nmp->nm_currexmit = 0;
1195 		return;
1196 	}
1197 	/* If have a measurement, do smoothing */
1198 	if (nmp->nm_srtt) {
1199 		register short delta;
1200 		delta = nmp->nm_rtt - (nmp->nm_srtt >> 3);
1201 		if ((nmp->nm_srtt += delta) <= 0)
1202 			nmp->nm_srtt = 1;
1203 		if (delta < 0)
1204 			delta = -delta;
1205 		delta -= (nmp->nm_rttvar >> 2);
1206 		if ((nmp->nm_rttvar += delta) <= 0)
1207 			nmp->nm_rttvar = 1;
1208 	/* Else initialize */
1209 	} else {
1210 		nmp->nm_rttvar = nmp->nm_rtt << 1;
1211 		if (nmp->nm_rttvar == 0) nmp->nm_rttvar = 2;
1212 		nmp->nm_srtt = nmp->nm_rttvar << 2;
1213 	}
1214 	/* Compute new Retransmission TimeOut and clip */
1215 	nmp->nm_rto = NFS_RTO(nmp);
1216 	if (nmp->nm_rto < NFS_MINTIMEO)
1217 		nmp->nm_rto = NFS_MINTIMEO;
1218 	else if (nmp->nm_rto > NFS_MAXTIMEO)
1219 		nmp->nm_rto = NFS_MAXTIMEO;
1220 
1221 	/* Update window estimate */
1222 	if (nmp->nm_window < nmp->nm_ssthresh)	/* quickly */
1223 		nmp->nm_window += 4;
1224 	else {						/* slowly */
1225 		register long incr = ++nmp->nm_winext;
1226 		incr = (incr * incr) / nmp->nm_window;
1227 		if (incr > 0) {
1228 			nmp->nm_winext = 0;
1229 			++nmp->nm_window;
1230 		}
1231 	}
1232 	if (nmp->nm_window > NFS_MAXWINDOW)
1233 		nmp->nm_window = NFS_MAXWINDOW;
1234 }
1235 
1236 nfs_backofftimer(nmp)
1237 	register struct nfsmount *nmp;
1238 {
1239 	register unsigned long newrto;
1240 
1241 	/* Clip shift count */
1242 	if (++nmp->nm_rexmit > 8 * sizeof nmp->nm_rto)
1243 		nmp->nm_rexmit = 8 * sizeof nmp->nm_rto;
1244 	/* Back off RTO exponentially */
1245 	newrto = NFS_RTO(nmp);
1246 	newrto <<= (nmp->nm_rexmit - 1);
1247 	if (newrto == 0 || newrto > NFS_MAXTIMEO)
1248 		newrto = NFS_MAXTIMEO;
1249 	nmp->nm_rto = newrto;
1250 
1251 	/* If too many retries, message, assume a bogus RTT and re-measure */
1252 	if (nmp->nm_currexmit < nmp->nm_rexmit) {
1253 		nmp->nm_currexmit = nmp->nm_rexmit;
1254 		if (nmp->nm_currexmit >= nfsrexmtthresh) {
1255 			if (nmp->nm_currexmit == nfsrexmtthresh) {
1256 				nmp->nm_rttvar += (nmp->nm_srtt >> 2);
1257 				nmp->nm_srtt = 0;
1258 			}
1259 		}
1260 	}
1261 	/* Close down window but remember this point (3/4 current) for later */
1262 	nmp->nm_ssthresh = ((nmp->nm_window << 1) + nmp->nm_window) >> 2;
1263 	nmp->nm_window = 1;
1264 	nmp->nm_winext = 0;
1265 }
1266 
1267 /*
1268  * Test for a termination signal pending on procp.
1269  * This is used for NFSMNT_INT mounts.
1270  */
1271 nfs_sigintr(p)
1272 	register struct proc *p;
1273 {
1274 	if (p && p->p_sig && (((p->p_sig &~ p->p_sigmask) &~ p->p_sigignore) &
1275 	    NFSINT_SIGMASK))
1276 		return (1);
1277 	else
1278 		return (0);
1279 }
1280 
1281 /*
1282  * Lock a socket against others.
1283  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
1284  * and also to avoid race conditions between the processes with nfs requests
1285  * in progress when a reconnect is necessary.
1286  */
1287 nfs_solock(flagp)
1288 	register int *flagp;
1289 {
1290 
1291 	while (*flagp & NFSMNT_SCKLOCK) {
1292 		*flagp |= NFSMNT_WANTSCK;
1293 		(void) tsleep((caddr_t)flagp, PZERO-1, "nfsolck", 0);
1294 	}
1295 	*flagp |= NFSMNT_SCKLOCK;
1296 }
1297 
1298 /*
1299  * Unlock the stream socket for others.
1300  */
1301 nfs_sounlock(flagp)
1302 	register int *flagp;
1303 {
1304 
1305 	if ((*flagp & NFSMNT_SCKLOCK) == 0)
1306 		panic("nfs sounlock");
1307 	*flagp &= ~NFSMNT_SCKLOCK;
1308 	if (*flagp & NFSMNT_WANTSCK) {
1309 		*flagp &= ~NFSMNT_WANTSCK;
1310 		wakeup((caddr_t)flagp);
1311 	}
1312 }
1313 
1314 /*
1315  * This function compares two net addresses by family and returns TRUE
1316  * if they are the same.
1317  * If there is any doubt, return FALSE.
1318  */
1319 nfs_netaddr_match(nam1, nam2)
1320 	struct mbuf *nam1, *nam2;
1321 {
1322 	register struct sockaddr *saddr1, *saddr2;
1323 
1324 	saddr1 = mtod(nam1, struct sockaddr *);
1325 	saddr2 = mtod(nam2, struct sockaddr *);
1326 	if (saddr1->sa_family != saddr2->sa_family)
1327 		return (0);
1328 
1329 	/*
1330 	 * Must do each address family separately since unused fields
1331 	 * are undefined values and not always zeroed.
1332 	 */
1333 	switch (saddr1->sa_family) {
1334 	case AF_INET:
1335 		if (((struct sockaddr_in *)saddr1)->sin_addr.s_addr ==
1336 		    ((struct sockaddr_in *)saddr2)->sin_addr.s_addr)
1337 			return (1);
1338 		break;
1339 	default:
1340 		break;
1341 	};
1342 	return (0);
1343 }
1344 
1345 /*
1346  * Check the hostname fields for nfsd's mask and match fields.
1347  * By address family:
1348  * - Bitwise AND the mask with the host address field
1349  * - Compare for == with match
1350  * return TRUE if not equal
1351  */
1352 nfs_badnam(nam, msk, mtch)
1353 	register struct mbuf *nam, *msk, *mtch;
1354 {
1355 	switch (mtod(nam, struct sockaddr *)->sa_family) {
1356 	case AF_INET:
1357 		return ((mtod(nam, struct sockaddr_in *)->sin_addr.s_addr &
1358 			 mtod(msk, struct sockaddr_in *)->sin_addr.s_addr) !=
1359 			 mtod(mtch, struct sockaddr_in *)->sin_addr.s_addr);
1360 	default:
1361 		printf("nfs_badmatch, unknown sa_family\n");
1362 		return (0);
1363 	};
1364 }
1365