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