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