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