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