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