Home | History | Annotate | Line # | Download | only in nfs
nfs_socket.c revision 1.160
      1 /*	$NetBSD: nfs_socket.c,v 1.160 2007/07/09 21:11:30 ad 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_socket.c	8.5 (Berkeley) 3/30/95
     35  */
     36 
     37 /*
     38  * Socket operations for use by nfs
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.160 2007/07/09 21:11:30 ad Exp $");
     43 
     44 #include "fs_nfs.h"
     45 #include "opt_nfs.h"
     46 #include "opt_nfsserver.h"
     47 #include "opt_mbuftrace.h"
     48 #include "opt_inet.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/callout.h>
     53 #include <sys/proc.h>
     54 #include <sys/mount.h>
     55 #include <sys/kernel.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/vnode.h>
     58 #include <sys/domain.h>
     59 #include <sys/protosw.h>
     60 #include <sys/socket.h>
     61 #include <sys/socketvar.h>
     62 #include <sys/syslog.h>
     63 #include <sys/tprintf.h>
     64 #include <sys/namei.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/kauth.h>
     68 
     69 #include <netinet/in.h>
     70 #include <netinet/tcp.h>
     71 
     72 #include <nfs/rpcv2.h>
     73 #include <nfs/nfsproto.h>
     74 #include <nfs/nfs.h>
     75 #include <nfs/xdr_subs.h>
     76 #include <nfs/nfsm_subs.h>
     77 #include <nfs/nfsmount.h>
     78 #include <nfs/nfsnode.h>
     79 #include <nfs/nfsrtt.h>
     80 #include <nfs/nfs_var.h>
     81 
     82 MALLOC_DEFINE(M_NFSREQ, "NFS req", "NFS request header");
     83 #ifdef MBUFTRACE
     84 struct mowner nfs_mowner = MOWNER_INIT("nfs","");
     85 #endif
     86 
     87 /*
     88  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
     89  * Use the mean and mean deviation of rtt for the appropriate type of rpc
     90  * for the frequent rpcs and a default for the others.
     91  * The justification for doing "other" this way is that these rpcs
     92  * happen so infrequently that timer est. would probably be stale.
     93  * Also, since many of these rpcs are
     94  * non-idempotent, a conservative timeout is desired.
     95  * getattr, lookup - A+2D
     96  * read, write     - A+4D
     97  * other           - nm_timeo
     98  */
     99 #define	NFS_RTO(n, t) \
    100 	((t) == 0 ? (n)->nm_timeo : \
    101 	 ((t) < 3 ? \
    102 	  (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
    103 	  ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
    104 #define	NFS_SRTT(r)	(r)->r_nmp->nm_srtt[proct[(r)->r_procnum] - 1]
    105 #define	NFS_SDRTT(r)	(r)->r_nmp->nm_sdrtt[proct[(r)->r_procnum] - 1]
    106 /*
    107  * External data, mostly RPC constants in XDR form
    108  */
    109 extern u_int32_t rpc_reply, rpc_msgdenied, rpc_mismatch, rpc_vers,
    110 	rpc_auth_unix, rpc_msgaccepted, rpc_call, rpc_autherr,
    111 	rpc_auth_kerb;
    112 extern u_int32_t nfs_prog;
    113 extern const int nfsv3_procid[NFS_NPROCS];
    114 extern int nfs_ticks;
    115 
    116 /*
    117  * Defines which timer to use for the procnum.
    118  * 0 - default
    119  * 1 - getattr
    120  * 2 - lookup
    121  * 3 - read
    122  * 4 - write
    123  */
    124 static const int proct[NFS_NPROCS] = {
    125 	[NFSPROC_NULL] = 0,
    126 	[NFSPROC_GETATTR] = 1,
    127 	[NFSPROC_SETATTR] = 0,
    128 	[NFSPROC_LOOKUP] = 2,
    129 	[NFSPROC_ACCESS] = 1,
    130 	[NFSPROC_READLINK] = 3,
    131 	[NFSPROC_READ] = 3,
    132 	[NFSPROC_WRITE] = 4,
    133 	[NFSPROC_CREATE] = 0,
    134 	[NFSPROC_MKDIR] = 0,
    135 	[NFSPROC_SYMLINK] = 0,
    136 	[NFSPROC_MKNOD] = 0,
    137 	[NFSPROC_REMOVE] = 0,
    138 	[NFSPROC_RMDIR] = 0,
    139 	[NFSPROC_RENAME] = 0,
    140 	[NFSPROC_LINK] = 0,
    141 	[NFSPROC_READDIR] = 3,
    142 	[NFSPROC_READDIRPLUS] = 3,
    143 	[NFSPROC_FSSTAT] = 0,
    144 	[NFSPROC_FSINFO] = 0,
    145 	[NFSPROC_PATHCONF] = 0,
    146 	[NFSPROC_COMMIT] = 0,
    147 	[NFSPROC_NOOP] = 0,
    148 };
    149 
    150 /*
    151  * There is a congestion window for outstanding rpcs maintained per mount
    152  * point. The cwnd size is adjusted in roughly the way that:
    153  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
    154  * SIGCOMM '88". ACM, August 1988.
    155  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
    156  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
    157  * of rpcs is in progress.
    158  * (The sent count and cwnd are scaled for integer arith.)
    159  * Variants of "slow start" were tried and were found to be too much of a
    160  * performance hit (ave. rtt 3 times larger),
    161  * I suspect due to the large rtt that nfs rpcs have.
    162  */
    163 #define	NFS_CWNDSCALE	256
    164 #define	NFS_MAXCWND	(NFS_CWNDSCALE * 32)
    165 static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
    166 int nfsrtton = 0;
    167 struct nfsrtt nfsrtt;
    168 struct nfsreqhead nfs_reqq;
    169 callout_t nfs_timer_ch;
    170 
    171 static int nfs_sndlock(struct nfsmount *, struct nfsreq *);
    172 static void nfs_sndunlock(struct nfsmount *);
    173 static int nfs_rcvlock(struct nfsmount *, struct nfsreq *);
    174 static void nfs_rcvunlock(struct nfsmount *);
    175 
    176 /*
    177  * Initialize sockets and congestion for a new NFS connection.
    178  * We do not free the sockaddr if error.
    179  */
    180 int
    181 nfs_connect(nmp, rep, l)
    182 	struct nfsmount *nmp;
    183 	struct nfsreq *rep;
    184 	struct lwp *l;
    185 {
    186 	struct socket *so;
    187 	int s, error, rcvreserve, sndreserve;
    188 	struct sockaddr *saddr;
    189 	struct sockaddr_in *sin;
    190 #ifdef INET6
    191 	struct sockaddr_in6 *sin6;
    192 #endif
    193 	struct mbuf *m;
    194 
    195 	nmp->nm_so = (struct socket *)0;
    196 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
    197 	error = socreate(saddr->sa_family, &nmp->nm_so,
    198 		nmp->nm_sotype, nmp->nm_soproto, l);
    199 	if (error)
    200 		goto bad;
    201 	so = nmp->nm_so;
    202 #ifdef MBUFTRACE
    203 	so->so_mowner = &nfs_mowner;
    204 	so->so_rcv.sb_mowner = &nfs_mowner;
    205 	so->so_snd.sb_mowner = &nfs_mowner;
    206 #endif
    207 	nmp->nm_soflags = so->so_proto->pr_flags;
    208 
    209 	/*
    210 	 * Some servers require that the client port be a reserved port number.
    211 	 */
    212 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    213 		m = m_get(M_WAIT, MT_SOOPTS);
    214 		MCLAIM(m, so->so_mowner);
    215 		*mtod(m, int32_t *) = IP_PORTRANGE_LOW;
    216 		m->m_len = sizeof(int32_t);
    217 		if ((error = sosetopt(so, IPPROTO_IP, IP_PORTRANGE, m)))
    218 			goto bad;
    219 		m = m_get(M_WAIT, MT_SONAME);
    220 		MCLAIM(m, so->so_mowner);
    221 		sin = mtod(m, struct sockaddr_in *);
    222 		sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
    223 		sin->sin_family = AF_INET;
    224 		sin->sin_addr.s_addr = INADDR_ANY;
    225 		sin->sin_port = 0;
    226 		error = sobind(so, m, &lwp0);
    227 		m_freem(m);
    228 		if (error)
    229 			goto bad;
    230 	}
    231 #ifdef INET6
    232 	if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    233 		m = m_get(M_WAIT, MT_SOOPTS);
    234 		MCLAIM(m, so->so_mowner);
    235 		*mtod(m, int32_t *) = IPV6_PORTRANGE_LOW;
    236 		m->m_len = sizeof(int32_t);
    237 		if ((error = sosetopt(so, IPPROTO_IPV6, IPV6_PORTRANGE, m)))
    238 			goto bad;
    239 		m = m_get(M_WAIT, MT_SONAME);
    240 		MCLAIM(m, so->so_mowner);
    241 		sin6 = mtod(m, struct sockaddr_in6 *);
    242 		sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6);
    243 		sin6->sin6_family = AF_INET6;
    244 		sin6->sin6_addr = in6addr_any;
    245 		sin6->sin6_port = 0;
    246 		error = sobind(so, m, &lwp0);
    247 		m_freem(m);
    248 		if (error)
    249 			goto bad;
    250 	}
    251 #endif
    252 
    253 	/*
    254 	 * Protocols that do not require connections may be optionally left
    255 	 * unconnected for servers that reply from a port other than NFS_PORT.
    256 	 */
    257 	if (nmp->nm_flag & NFSMNT_NOCONN) {
    258 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
    259 			error = ENOTCONN;
    260 			goto bad;
    261 		}
    262 	} else {
    263 		error = soconnect(so, nmp->nm_nam, l);
    264 		if (error)
    265 			goto bad;
    266 
    267 		/*
    268 		 * Wait for the connection to complete. Cribbed from the
    269 		 * connect system call but with the wait timing out so
    270 		 * that interruptible mounts don't hang here for a long time.
    271 		 */
    272 		s = splsoftnet();
    273 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    274 			(void) tsleep((void *)&so->so_timeo, PSOCK,
    275 				"nfscn1", 2 * hz);
    276 			if ((so->so_state & SS_ISCONNECTING) &&
    277 			    so->so_error == 0 && rep &&
    278 			    (error = nfs_sigintr(nmp, rep, rep->r_lwp)) != 0){
    279 				so->so_state &= ~SS_ISCONNECTING;
    280 				splx(s);
    281 				goto bad;
    282 			}
    283 		}
    284 		if (so->so_error) {
    285 			error = so->so_error;
    286 			so->so_error = 0;
    287 			splx(s);
    288 			goto bad;
    289 		}
    290 		splx(s);
    291 	}
    292 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
    293 		so->so_rcv.sb_timeo = (5 * hz);
    294 		so->so_snd.sb_timeo = (5 * hz);
    295 	} else {
    296 		/*
    297 		 * enable receive timeout to detect server crash and reconnect.
    298 		 * otherwise, we can be stuck in soreceive forever.
    299 		 */
    300 		so->so_rcv.sb_timeo = (5 * hz);
    301 		so->so_snd.sb_timeo = 0;
    302 	}
    303 	if (nmp->nm_sotype == SOCK_DGRAM) {
    304 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    305 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    306 		    NFS_MAXPKTHDR) * 2;
    307 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
    308 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    309 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    310 		    NFS_MAXPKTHDR) * 2;
    311 	} else {
    312 		if (nmp->nm_sotype != SOCK_STREAM)
    313 			panic("nfscon sotype");
    314 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    315 			m = m_get(M_WAIT, MT_SOOPTS);
    316 			MCLAIM(m, so->so_mowner);
    317 			*mtod(m, int32_t *) = 1;
    318 			m->m_len = sizeof(int32_t);
    319 			sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    320 		}
    321 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
    322 			m = m_get(M_WAIT, MT_SOOPTS);
    323 			MCLAIM(m, so->so_mowner);
    324 			*mtod(m, int32_t *) = 1;
    325 			m->m_len = sizeof(int32_t);
    326 			sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    327 		}
    328 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
    329 		    sizeof (u_int32_t)) * 2;
    330 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
    331 		    sizeof (u_int32_t)) * 2;
    332 	}
    333 	error = soreserve(so, sndreserve, rcvreserve);
    334 	if (error)
    335 		goto bad;
    336 	so->so_rcv.sb_flags |= SB_NOINTR;
    337 	so->so_snd.sb_flags |= SB_NOINTR;
    338 
    339 	/* Initialize other non-zero congestion variables */
    340 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
    341 		NFS_TIMEO << 3;
    342 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
    343 		nmp->nm_sdrtt[3] = 0;
    344 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
    345 	nmp->nm_sent = 0;
    346 	nmp->nm_timeouts = 0;
    347 	return (0);
    348 
    349 bad:
    350 	nfs_disconnect(nmp);
    351 	return (error);
    352 }
    353 
    354 /*
    355  * Reconnect routine:
    356  * Called when a connection is broken on a reliable protocol.
    357  * - clean up the old socket
    358  * - nfs_connect() again
    359  * - set R_MUSTRESEND for all outstanding requests on mount point
    360  * If this fails the mount point is DEAD!
    361  * nb: Must be called with the nfs_sndlock() set on the mount point.
    362  */
    363 int
    364 nfs_reconnect(rep, l)
    365 	struct nfsreq *rep;
    366 	struct lwp *l;
    367 {
    368 	struct nfsreq *rp;
    369 	struct nfsmount *nmp = rep->r_nmp;
    370 	int error;
    371 
    372 	nfs_disconnect(nmp);
    373 	while ((error = nfs_connect(nmp, rep, l)) != 0) {
    374 		if (error == EINTR || error == ERESTART)
    375 			return (EINTR);
    376 		(void) tsleep((void *)&lbolt, PSOCK, "nfscn2", 0);
    377 	}
    378 
    379 	/*
    380 	 * Loop through outstanding request list and fix up all requests
    381 	 * on old socket.
    382 	 */
    383 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
    384 		if (rp->r_nmp == nmp) {
    385 			if ((rp->r_flags & R_MUSTRESEND) == 0)
    386 				rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
    387 			rp->r_rexmit = 0;
    388 		}
    389 	}
    390 	return (0);
    391 }
    392 
    393 /*
    394  * NFS disconnect. Clean up and unlink.
    395  */
    396 void
    397 nfs_disconnect(nmp)
    398 	struct nfsmount *nmp;
    399 {
    400 	struct socket *so;
    401 	int drain = 0;
    402 
    403 	if (nmp->nm_so) {
    404 		so = nmp->nm_so;
    405 		nmp->nm_so = (struct socket *)0;
    406 		soshutdown(so, SHUT_RDWR);
    407 		drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
    408 		if (drain) {
    409 			/*
    410 			 * soshutdown() above should wake up the current
    411 			 * listener.
    412 			 * Now wake up those waiting for the receive lock, and
    413 			 * wait for them to go away unhappy, to prevent *nmp
    414 			 * from evaporating while they're sleeping.
    415 			 */
    416 			mutex_enter(&nmp->nm_lock);
    417 			while (nmp->nm_waiters > 0) {
    418 				cv_broadcast(&nmp->nm_rcvcv);
    419 				cv_broadcast(&nmp->nm_sndcv);
    420 				cv_wait(&nmp->nm_disconcv, &nmp->nm_lock);
    421 			}
    422 			mutex_exit(&nmp->nm_lock);
    423 		}
    424 		soclose(so);
    425 	}
    426 #ifdef DIAGNOSTIC
    427 	if (drain && (nmp->nm_waiters > 0))
    428 		panic("nfs_disconnect: waiters left after drain?");
    429 #endif
    430 }
    431 
    432 void
    433 nfs_safedisconnect(nmp)
    434 	struct nfsmount *nmp;
    435 {
    436 	struct nfsreq dummyreq;
    437 
    438 	memset(&dummyreq, 0, sizeof(dummyreq));
    439 	dummyreq.r_nmp = nmp;
    440 	nfs_rcvlock(nmp, &dummyreq); /* XXX ignored error return */
    441 	nfs_disconnect(nmp);
    442 	nfs_rcvunlock(nmp);
    443 }
    444 
    445 /*
    446  * This is the nfs send routine. For connection based socket types, it
    447  * must be called with an nfs_sndlock() on the socket.
    448  * "rep == NULL" indicates that it has been called from a server.
    449  * For the client side:
    450  * - return EINTR if the RPC is terminated, 0 otherwise
    451  * - set R_MUSTRESEND if the send fails for any reason
    452  * - do any cleanup required by recoverable socket errors (? ? ?)
    453  * For the server side:
    454  * - return EINTR or ERESTART if interrupted by a signal
    455  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
    456  * - do any cleanup required by recoverable socket errors (? ? ?)
    457  */
    458 int
    459 nfs_send(so, nam, top, rep, l)
    460 	struct socket *so;
    461 	struct mbuf *nam;
    462 	struct mbuf *top;
    463 	struct nfsreq *rep;
    464 	struct lwp *l;
    465 {
    466 	struct mbuf *sendnam;
    467 	int error, soflags, flags;
    468 
    469 	/* XXX nfs_doio()/nfs_request() calls with  rep->r_lwp == NULL */
    470 	if (l == NULL && rep->r_lwp == NULL)
    471 		l = curlwp;
    472 
    473 	if (rep) {
    474 		if (rep->r_flags & R_SOFTTERM) {
    475 			m_freem(top);
    476 			return (EINTR);
    477 		}
    478 		if ((so = rep->r_nmp->nm_so) == NULL) {
    479 			rep->r_flags |= R_MUSTRESEND;
    480 			m_freem(top);
    481 			return (0);
    482 		}
    483 		rep->r_flags &= ~R_MUSTRESEND;
    484 		soflags = rep->r_nmp->nm_soflags;
    485 	} else
    486 		soflags = so->so_proto->pr_flags;
    487 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
    488 		sendnam = (struct mbuf *)0;
    489 	else
    490 		sendnam = nam;
    491 	if (so->so_type == SOCK_SEQPACKET)
    492 		flags = MSG_EOR;
    493 	else
    494 		flags = 0;
    495 
    496 	error = (*so->so_send)(so, sendnam, (struct uio *)0, top,
    497 		    (struct mbuf *)0, flags,  l);
    498 	if (error) {
    499 		if (rep) {
    500 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
    501 				/*
    502 				 * We're too fast for the network/driver,
    503 				 * and UDP isn't flowcontrolled.
    504 				 * We need to resend. This is not fatal,
    505 				 * just try again.
    506 				 *
    507 				 * Could be smarter here by doing some sort
    508 				 * of a backoff, but this is rare.
    509 				 */
    510 				rep->r_flags |= R_MUSTRESEND;
    511 			} else {
    512 				if (error != EPIPE)
    513 					log(LOG_INFO,
    514 					    "nfs send error %d for %s\n",
    515 					    error,
    516 					    rep->r_nmp->nm_mountp->
    517 						    mnt_stat.f_mntfromname);
    518 				/*
    519 				 * Deal with errors for the client side.
    520 				 */
    521 				if (rep->r_flags & R_SOFTTERM)
    522 					error = EINTR;
    523 				else
    524 					rep->r_flags |= R_MUSTRESEND;
    525 			}
    526 		} else {
    527 			/*
    528 			 * See above. This error can happen under normal
    529 			 * circumstances and the log is too noisy.
    530 			 * The error will still show up in nfsstat.
    531 			 */
    532 			if (error != ENOBUFS || so->so_type != SOCK_DGRAM)
    533 				log(LOG_INFO, "nfsd send error %d\n", error);
    534 		}
    535 
    536 		/*
    537 		 * Handle any recoverable (soft) socket errors here. (? ? ?)
    538 		 */
    539 		if (error != EINTR && error != ERESTART &&
    540 			error != EWOULDBLOCK && error != EPIPE)
    541 			error = 0;
    542 	}
    543 	return (error);
    544 }
    545 
    546 #ifdef NFS
    547 /*
    548  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
    549  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
    550  * Mark and consolidate the data into a new mbuf list.
    551  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
    552  *     small mbufs.
    553  * For SOCK_STREAM we must be very careful to read an entire record once
    554  * we have read any of it, even if the system call has been interrupted.
    555  */
    556 int
    557 nfs_receive(rep, aname, mp, l)
    558 	struct nfsreq *rep;
    559 	struct mbuf **aname;
    560 	struct mbuf **mp;
    561 	struct lwp *l;
    562 {
    563 	struct socket *so;
    564 	struct uio auio;
    565 	struct iovec aio;
    566 	struct mbuf *m;
    567 	struct mbuf *control;
    568 	u_int32_t len;
    569 	struct mbuf **getnam;
    570 	int error, sotype, rcvflg;
    571 
    572 	/*
    573 	 * Set up arguments for soreceive()
    574 	 */
    575 	*mp = (struct mbuf *)0;
    576 	*aname = (struct mbuf *)0;
    577 	sotype = rep->r_nmp->nm_sotype;
    578 
    579 	/*
    580 	 * For reliable protocols, lock against other senders/receivers
    581 	 * in case a reconnect is necessary.
    582 	 * For SOCK_STREAM, first get the Record Mark to find out how much
    583 	 * more there is to get.
    584 	 * We must lock the socket against other receivers
    585 	 * until we have an entire rpc request/reply.
    586 	 */
    587 	if (sotype != SOCK_DGRAM) {
    588 		error = nfs_sndlock(rep->r_nmp, rep);
    589 		if (error)
    590 			return (error);
    591 tryagain:
    592 		/*
    593 		 * Check for fatal errors and resending request.
    594 		 */
    595 		/*
    596 		 * Ugh: If a reconnect attempt just happened, nm_so
    597 		 * would have changed. NULL indicates a failed
    598 		 * attempt that has essentially shut down this
    599 		 * mount point.
    600 		 */
    601 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
    602 			nfs_sndunlock(rep->r_nmp);
    603 			return (EINTR);
    604 		}
    605 		so = rep->r_nmp->nm_so;
    606 		if (!so) {
    607 			error = nfs_reconnect(rep, l);
    608 			if (error) {
    609 				nfs_sndunlock(rep->r_nmp);
    610 				return (error);
    611 			}
    612 			goto tryagain;
    613 		}
    614 		while (rep->r_flags & R_MUSTRESEND) {
    615 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
    616 			nfsstats.rpcretries++;
    617 			rep->r_rtt = 0;
    618 			rep->r_flags &= ~R_TIMING;
    619 			error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l);
    620 			if (error) {
    621 				if (error == EINTR || error == ERESTART ||
    622 				    (error = nfs_reconnect(rep, l)) != 0) {
    623 					nfs_sndunlock(rep->r_nmp);
    624 					return (error);
    625 				}
    626 				goto tryagain;
    627 			}
    628 		}
    629 		nfs_sndunlock(rep->r_nmp);
    630 		if (sotype == SOCK_STREAM) {
    631 			aio.iov_base = (void *) &len;
    632 			aio.iov_len = sizeof(u_int32_t);
    633 			auio.uio_iov = &aio;
    634 			auio.uio_iovcnt = 1;
    635 			auio.uio_rw = UIO_READ;
    636 			auio.uio_offset = 0;
    637 			auio.uio_resid = sizeof(u_int32_t);
    638 			UIO_SETUP_SYSSPACE(&auio);
    639 			do {
    640 			   rcvflg = MSG_WAITALL;
    641 			   error = (*so->so_receive)(so, (struct mbuf **)0, &auio,
    642 				(struct mbuf **)0, (struct mbuf **)0, &rcvflg);
    643 			   if (error == EWOULDBLOCK && rep) {
    644 				if (rep->r_flags & R_SOFTTERM)
    645 					return (EINTR);
    646 				/*
    647 				 * if it seems that the server died after it
    648 				 * received our request, set EPIPE so that
    649 				 * we'll reconnect and retransmit requests.
    650 				 */
    651 				if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
    652 					nfsstats.rpctimeouts++;
    653 					error = EPIPE;
    654 				}
    655 			   }
    656 			} while (error == EWOULDBLOCK);
    657 			if (!error && auio.uio_resid > 0) {
    658 			    /*
    659 			     * Don't log a 0 byte receive; it means
    660 			     * that the socket has been closed, and
    661 			     * can happen during normal operation
    662 			     * (forcible unmount or Solaris server).
    663 			     */
    664 			    if (auio.uio_resid != sizeof (u_int32_t))
    665 			      log(LOG_INFO,
    666 				 "short receive (%lu/%lu) from nfs server %s\n",
    667 				 (u_long)sizeof(u_int32_t) - auio.uio_resid,
    668 				 (u_long)sizeof(u_int32_t),
    669 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    670 			    error = EPIPE;
    671 			}
    672 			if (error)
    673 				goto errout;
    674 			len = ntohl(len) & ~0x80000000;
    675 			/*
    676 			 * This is SERIOUS! We are out of sync with the sender
    677 			 * and forcing a disconnect/reconnect is all I can do.
    678 			 */
    679 			if (len > NFS_MAXPACKET) {
    680 			    log(LOG_ERR, "%s (%d) from nfs server %s\n",
    681 				"impossible packet length",
    682 				len,
    683 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    684 			    error = EFBIG;
    685 			    goto errout;
    686 			}
    687 			auio.uio_resid = len;
    688 			do {
    689 			    rcvflg = MSG_WAITALL;
    690 			    error =  (*so->so_receive)(so, (struct mbuf **)0,
    691 				&auio, mp, (struct mbuf **)0, &rcvflg);
    692 			} while (error == EWOULDBLOCK || error == EINTR ||
    693 				 error == ERESTART);
    694 			if (!error && auio.uio_resid > 0) {
    695 			    if (len != auio.uio_resid)
    696 			      log(LOG_INFO,
    697 				"short receive (%lu/%d) from nfs server %s\n",
    698 				(u_long)len - auio.uio_resid, len,
    699 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    700 			    error = EPIPE;
    701 			}
    702 		} else {
    703 			/*
    704 			 * NB: Since uio_resid is big, MSG_WAITALL is ignored
    705 			 * and soreceive() will return when it has either a
    706 			 * control msg or a data msg.
    707 			 * We have no use for control msg., but must grab them
    708 			 * and then throw them away so we know what is going
    709 			 * on.
    710 			 */
    711 			auio.uio_resid = len = 100000000; /* Anything Big */
    712 			/* not need to setup uio_vmspace */
    713 			do {
    714 			    rcvflg = 0;
    715 			    error =  (*so->so_receive)(so, (struct mbuf **)0,
    716 				&auio, mp, &control, &rcvflg);
    717 			    if (control)
    718 				m_freem(control);
    719 			    if (error == EWOULDBLOCK && rep) {
    720 				if (rep->r_flags & R_SOFTTERM)
    721 					return (EINTR);
    722 			    }
    723 			} while (error == EWOULDBLOCK ||
    724 				 (!error && *mp == NULL && control));
    725 			if ((rcvflg & MSG_EOR) == 0)
    726 				printf("Egad!!\n");
    727 			if (!error && *mp == NULL)
    728 				error = EPIPE;
    729 			len -= auio.uio_resid;
    730 		}
    731 errout:
    732 		if (error && error != EINTR && error != ERESTART) {
    733 			m_freem(*mp);
    734 			*mp = (struct mbuf *)0;
    735 			if (error != EPIPE)
    736 				log(LOG_INFO,
    737 				    "receive error %d from nfs server %s\n",
    738 				    error,
    739 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    740 			error = nfs_sndlock(rep->r_nmp, rep);
    741 			if (!error)
    742 				error = nfs_reconnect(rep, l);
    743 			if (!error)
    744 				goto tryagain;
    745 			else
    746 				nfs_sndunlock(rep->r_nmp);
    747 		}
    748 	} else {
    749 		if ((so = rep->r_nmp->nm_so) == NULL)
    750 			return (EACCES);
    751 		if (so->so_state & SS_ISCONNECTED)
    752 			getnam = (struct mbuf **)0;
    753 		else
    754 			getnam = aname;
    755 		auio.uio_resid = len = 1000000;
    756 		/* not need to setup uio_vmspace */
    757 		do {
    758 			rcvflg = 0;
    759 			error =  (*so->so_receive)(so, getnam, &auio, mp,
    760 				(struct mbuf **)0, &rcvflg);
    761 			if (error == EWOULDBLOCK &&
    762 			    (rep->r_flags & R_SOFTTERM))
    763 				return (EINTR);
    764 		} while (error == EWOULDBLOCK);
    765 		len -= auio.uio_resid;
    766 		if (!error && *mp == NULL)
    767 			error = EPIPE;
    768 	}
    769 	if (error) {
    770 		m_freem(*mp);
    771 		*mp = (struct mbuf *)0;
    772 	}
    773 	return (error);
    774 }
    775 
    776 /*
    777  * Implement receipt of reply on a socket.
    778  * We must search through the list of received datagrams matching them
    779  * with outstanding requests using the xid, until ours is found.
    780  */
    781 /* ARGSUSED */
    782 int
    783 nfs_reply(myrep, lwp)
    784 	struct nfsreq *myrep;
    785 	struct lwp *lwp;
    786 {
    787 	struct nfsreq *rep;
    788 	struct nfsmount *nmp = myrep->r_nmp;
    789 	int32_t t1;
    790 	struct mbuf *mrep, *nam, *md;
    791 	u_int32_t rxid, *tl;
    792 	char *dpos, *cp2;
    793 	int error;
    794 
    795 	/*
    796 	 * Loop around until we get our own reply
    797 	 */
    798 	for (;;) {
    799 		/*
    800 		 * Lock against other receivers so that I don't get stuck in
    801 		 * sbwait() after someone else has received my reply for me.
    802 		 * Also necessary for connection based protocols to avoid
    803 		 * race conditions during a reconnect.
    804 		 */
    805 		error = nfs_rcvlock(nmp, myrep);
    806 		if (error == EALREADY)
    807 			return (0);
    808 		if (error)
    809 			return (error);
    810 		/*
    811 		 * Get the next Rpc reply off the socket
    812 		 */
    813 
    814 		mutex_enter(&nmp->nm_lock);
    815 		nmp->nm_waiters++;
    816 		mutex_exit(&nmp->nm_lock);
    817 
    818 		error = nfs_receive(myrep, &nam, &mrep, lwp);
    819 
    820 		mutex_enter(&nmp->nm_lock);
    821 		nmp->nm_waiters--;
    822 		cv_signal(&nmp->nm_disconcv);
    823 		mutex_exit(&nmp->nm_lock);
    824 
    825 		if (error) {
    826 			nfs_rcvunlock(nmp);
    827 
    828 			if (nmp->nm_iflag & NFSMNT_DISMNT) {
    829 				/*
    830 				 * Oops, we're going away now..
    831 				 */
    832 				return error;
    833 			}
    834 			/*
    835 			 * Ignore routing errors on connectionless protocols? ?
    836 			 */
    837 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
    838 				nmp->nm_so->so_error = 0;
    839 #ifdef DEBUG
    840 				printf("nfs_reply: ignoring error %d\n", error);
    841 #endif
    842 				continue;
    843 			}
    844 			return (error);
    845 		}
    846 		if (nam)
    847 			m_freem(nam);
    848 
    849 		/*
    850 		 * Get the xid and check that it is an rpc reply
    851 		 */
    852 		md = mrep;
    853 		dpos = mtod(md, void *);
    854 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    855 		rxid = *tl++;
    856 		if (*tl != rpc_reply) {
    857 			nfsstats.rpcinvalid++;
    858 			m_freem(mrep);
    859 nfsmout:
    860 			nfs_rcvunlock(nmp);
    861 			continue;
    862 		}
    863 
    864 		/*
    865 		 * Loop through the request list to match up the reply
    866 		 * Iff no match, just drop the datagram
    867 		 */
    868 		TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
    869 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
    870 				/* Found it.. */
    871 				rep->r_mrep = mrep;
    872 				rep->r_md = md;
    873 				rep->r_dpos = dpos;
    874 				if (nfsrtton) {
    875 					struct rttl *rt;
    876 
    877 					rt = &nfsrtt.rttl[nfsrtt.pos];
    878 					rt->proc = rep->r_procnum;
    879 					rt->rto = NFS_RTO(nmp, proct[rep->r_procnum]);
    880 					rt->sent = nmp->nm_sent;
    881 					rt->cwnd = nmp->nm_cwnd;
    882 					rt->srtt = nmp->nm_srtt[proct[rep->r_procnum] - 1];
    883 					rt->sdrtt = nmp->nm_sdrtt[proct[rep->r_procnum] - 1];
    884 					rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
    885 					getmicrotime(&rt->tstamp);
    886 					if (rep->r_flags & R_TIMING)
    887 						rt->rtt = rep->r_rtt;
    888 					else
    889 						rt->rtt = 1000000;
    890 					nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
    891 				}
    892 				/*
    893 				 * Update congestion window.
    894 				 * Do the additive increase of
    895 				 * one rpc/rtt.
    896 				 */
    897 				if (nmp->nm_cwnd <= nmp->nm_sent) {
    898 					nmp->nm_cwnd +=
    899 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
    900 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
    901 					if (nmp->nm_cwnd > NFS_MAXCWND)
    902 						nmp->nm_cwnd = NFS_MAXCWND;
    903 				}
    904 				rep->r_flags &= ~R_SENT;
    905 				nmp->nm_sent -= NFS_CWNDSCALE;
    906 				/*
    907 				 * Update rtt using a gain of 0.125 on the mean
    908 				 * and a gain of 0.25 on the deviation.
    909 				 */
    910 				if (rep->r_flags & R_TIMING) {
    911 					/*
    912 					 * Since the timer resolution of
    913 					 * NFS_HZ is so course, it can often
    914 					 * result in r_rtt == 0. Since
    915 					 * r_rtt == N means that the actual
    916 					 * rtt is between N+dt and N+2-dt ticks,
    917 					 * add 1.
    918 					 */
    919 					t1 = rep->r_rtt + 1;
    920 					t1 -= (NFS_SRTT(rep) >> 3);
    921 					NFS_SRTT(rep) += t1;
    922 					if (t1 < 0)
    923 						t1 = -t1;
    924 					t1 -= (NFS_SDRTT(rep) >> 2);
    925 					NFS_SDRTT(rep) += t1;
    926 				}
    927 				nmp->nm_timeouts = 0;
    928 				break;
    929 			}
    930 		}
    931 		nfs_rcvunlock(nmp);
    932 		/*
    933 		 * If not matched to a request, drop it.
    934 		 * If it's mine, get out.
    935 		 */
    936 		if (rep == 0) {
    937 			nfsstats.rpcunexpected++;
    938 			m_freem(mrep);
    939 		} else if (rep == myrep) {
    940 			if (rep->r_mrep == NULL)
    941 				panic("nfsreply nil");
    942 			return (0);
    943 		}
    944 	}
    945 }
    946 
    947 /*
    948  * nfs_request - goes something like this
    949  *	- fill in request struct
    950  *	- links it into list
    951  *	- calls nfs_send() for first transmit
    952  *	- calls nfs_receive() to get reply
    953  *	- break down rpc header and return with nfs reply pointed to
    954  *	  by mrep or error
    955  * nb: always frees up mreq mbuf list
    956  */
    957 int
    958 nfs_request(np, mrest, procnum, lwp, cred, mrp, mdp, dposp, rexmitp)
    959 	struct nfsnode *np;
    960 	struct mbuf *mrest;
    961 	int procnum;
    962 	struct lwp *lwp;
    963 	kauth_cred_t cred;
    964 	struct mbuf **mrp;
    965 	struct mbuf **mdp;
    966 	char **dposp;
    967 	int *rexmitp;
    968 {
    969 	struct mbuf *m, *mrep;
    970 	struct nfsreq *rep;
    971 	u_int32_t *tl;
    972 	int i;
    973 	struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount);
    974 	struct mbuf *md, *mheadend;
    975 	char nickv[RPCX_NICKVERF];
    976 	time_t waituntil;
    977 	char *dpos, *cp2;
    978 	int t1, s, error = 0, mrest_len, auth_len, auth_type;
    979 	int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
    980 	int verf_len, verf_type;
    981 	u_int32_t xid;
    982 	char *auth_str, *verf_str;
    983 	NFSKERBKEY_T key;		/* save session key */
    984 	kauth_cred_t acred;
    985 	struct mbuf *mrest_backup = NULL;
    986 	kauth_cred_t origcred = NULL; /* XXX: gcc */
    987 	bool retry_cred = true;
    988 	bool use_opencred = (np->n_flag & NUSEOPENCRED) != 0;
    989 
    990 	if (rexmitp != NULL)
    991 		*rexmitp = 0;
    992 
    993 	acred = kauth_cred_alloc();
    994 
    995 tryagain_cred:
    996 	KASSERT(cred != NULL);
    997 	MALLOC(rep, struct nfsreq *, sizeof(struct nfsreq), M_NFSREQ, M_WAITOK);
    998 	rep->r_nmp = nmp;
    999 	KASSERT(lwp == NULL || lwp == curlwp);
   1000 	rep->r_lwp = lwp;
   1001 	rep->r_procnum = procnum;
   1002 	i = 0;
   1003 	m = mrest;
   1004 	while (m) {
   1005 		i += m->m_len;
   1006 		m = m->m_next;
   1007 	}
   1008 	mrest_len = i;
   1009 
   1010 	/*
   1011 	 * Get the RPC header with authorization.
   1012 	 */
   1013 kerbauth:
   1014 	verf_str = auth_str = (char *)0;
   1015 	if (nmp->nm_flag & NFSMNT_KERB) {
   1016 		verf_str = nickv;
   1017 		verf_len = sizeof (nickv);
   1018 		auth_type = RPCAUTH_KERB4;
   1019 		memset((void *)key, 0, sizeof (key));
   1020 		if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
   1021 			&auth_len, verf_str, verf_len)) {
   1022 			error = nfs_getauth(nmp, rep, cred, &auth_str,
   1023 				&auth_len, verf_str, &verf_len, key);
   1024 			if (error) {
   1025 				free((void *)rep, M_NFSREQ);
   1026 				m_freem(mrest);
   1027 				KASSERT(kauth_cred_getrefcnt(acred) == 1);
   1028 				kauth_cred_free(acred);
   1029 				return (error);
   1030 			}
   1031 		}
   1032 		retry_cred = false;
   1033 	} else {
   1034 		/* AUTH_UNIX */
   1035 		uid_t uid;
   1036 		gid_t gid;
   1037 
   1038 		/*
   1039 		 * on the most unix filesystems, permission checks are
   1040 		 * done when the file is open(2)'ed.
   1041 		 * ie. once a file is successfully open'ed,
   1042 		 * following i/o operations never fail with EACCES.
   1043 		 * we try to follow the semantics as far as possible.
   1044 		 *
   1045 		 * note that we expect that the nfs server always grant
   1046 		 * accesses by the file's owner.
   1047 		 */
   1048 		origcred = cred;
   1049 		switch (procnum) {
   1050 		case NFSPROC_READ:
   1051 		case NFSPROC_WRITE:
   1052 		case NFSPROC_COMMIT:
   1053 			uid = np->n_vattr->va_uid;
   1054 			gid = np->n_vattr->va_gid;
   1055 			if (kauth_cred_geteuid(cred) == uid &&
   1056 			    kauth_cred_getegid(cred) == gid) {
   1057 				retry_cred = false;
   1058 				break;
   1059 			}
   1060 			if (use_opencred)
   1061 				break;
   1062 			kauth_cred_setuid(acred, uid);
   1063 			kauth_cred_seteuid(acred, uid);
   1064 			kauth_cred_setsvuid(acred, uid);
   1065 			kauth_cred_setgid(acred, gid);
   1066 			kauth_cred_setegid(acred, gid);
   1067 			kauth_cred_setsvgid(acred, gid);
   1068 			cred = acred;
   1069 			break;
   1070 		default:
   1071 			retry_cred = false;
   1072 			break;
   1073 		}
   1074 		/*
   1075 		 * backup mbuf chain if we can need it later to retry.
   1076 		 *
   1077 		 * XXX maybe we can keep a direct reference to
   1078 		 * mrest without doing m_copym, but it's ...ugly.
   1079 		 */
   1080 		if (retry_cred)
   1081 			mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT);
   1082 		auth_type = RPCAUTH_UNIX;
   1083 		/* XXX elad - ngroups */
   1084 		auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ?
   1085 			nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) +
   1086 			5 * NFSX_UNSIGNED;
   1087 	}
   1088 	m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
   1089 	     auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
   1090 	if (auth_str)
   1091 		free(auth_str, M_TEMP);
   1092 
   1093 	/*
   1094 	 * For stream protocols, insert a Sun RPC Record Mark.
   1095 	 */
   1096 	if (nmp->nm_sotype == SOCK_STREAM) {
   1097 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
   1098 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
   1099 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
   1100 	}
   1101 	rep->r_mreq = m;
   1102 	rep->r_xid = xid;
   1103 tryagain:
   1104 	if (nmp->nm_flag & NFSMNT_SOFT)
   1105 		rep->r_retry = nmp->nm_retry;
   1106 	else
   1107 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
   1108 	rep->r_rtt = rep->r_rexmit = 0;
   1109 	if (proct[procnum] > 0)
   1110 		rep->r_flags = R_TIMING;
   1111 	else
   1112 		rep->r_flags = 0;
   1113 	rep->r_mrep = NULL;
   1114 
   1115 	/*
   1116 	 * Do the client side RPC.
   1117 	 */
   1118 	nfsstats.rpcrequests++;
   1119 	/*
   1120 	 * Chain request into list of outstanding requests. Be sure
   1121 	 * to put it LAST so timer finds oldest requests first.
   1122 	 */
   1123 	s = splsoftnet();
   1124 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
   1125 
   1126 	/*
   1127 	 * If backing off another request or avoiding congestion, don't
   1128 	 * send this one now but let timer do it. If not timing a request,
   1129 	 * do it now.
   1130 	 */
   1131 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
   1132 		(nmp->nm_flag & NFSMNT_DUMBTIMR) ||
   1133 		nmp->nm_sent < nmp->nm_cwnd)) {
   1134 		splx(s);
   1135 		if (nmp->nm_soflags & PR_CONNREQUIRED)
   1136 			error = nfs_sndlock(nmp, rep);
   1137 		if (!error) {
   1138 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
   1139 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
   1140 			if (nmp->nm_soflags & PR_CONNREQUIRED)
   1141 				nfs_sndunlock(nmp);
   1142 		}
   1143 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
   1144 			nmp->nm_sent += NFS_CWNDSCALE;
   1145 			rep->r_flags |= R_SENT;
   1146 		}
   1147 	} else {
   1148 		splx(s);
   1149 		rep->r_rtt = -1;
   1150 	}
   1151 
   1152 	/*
   1153 	 * Wait for the reply from our send or the timer's.
   1154 	 */
   1155 	if (!error || error == EPIPE)
   1156 		error = nfs_reply(rep, lwp);
   1157 
   1158 	/*
   1159 	 * RPC done, unlink the request.
   1160 	 */
   1161 	s = splsoftnet();
   1162 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
   1163 	splx(s);
   1164 
   1165 	/*
   1166 	 * Decrement the outstanding request count.
   1167 	 */
   1168 	if (rep->r_flags & R_SENT) {
   1169 		rep->r_flags &= ~R_SENT;	/* paranoia */
   1170 		nmp->nm_sent -= NFS_CWNDSCALE;
   1171 	}
   1172 
   1173 	if (rexmitp != NULL) {
   1174 		int rexmit;
   1175 
   1176 		if (nmp->nm_sotype != SOCK_DGRAM)
   1177 			rexmit = (rep->r_flags & R_REXMITTED) != 0;
   1178 		else
   1179 			rexmit = rep->r_rexmit;
   1180 		*rexmitp = rexmit;
   1181 	}
   1182 
   1183 	/*
   1184 	 * If there was a successful reply and a tprintf msg.
   1185 	 * tprintf a response.
   1186 	 */
   1187 	if (!error && (rep->r_flags & R_TPRINTFMSG))
   1188 		nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
   1189 		    "is alive again");
   1190 	mrep = rep->r_mrep;
   1191 	md = rep->r_md;
   1192 	dpos = rep->r_dpos;
   1193 	if (error)
   1194 		goto nfsmout;
   1195 
   1196 	/*
   1197 	 * break down the rpc header and check if ok
   1198 	 */
   1199 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1200 	if (*tl++ == rpc_msgdenied) {
   1201 		if (*tl == rpc_mismatch)
   1202 			error = EOPNOTSUPP;
   1203 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
   1204 			if (!failed_auth) {
   1205 				failed_auth++;
   1206 				mheadend->m_next = (struct mbuf *)0;
   1207 				m_freem(mrep);
   1208 				m_freem(rep->r_mreq);
   1209 				goto kerbauth;
   1210 			} else
   1211 				error = EAUTH;
   1212 		} else
   1213 			error = EACCES;
   1214 		m_freem(mrep);
   1215 		goto nfsmout;
   1216 	}
   1217 
   1218 	/*
   1219 	 * Grab any Kerberos verifier, otherwise just throw it away.
   1220 	 */
   1221 	verf_type = fxdr_unsigned(int, *tl++);
   1222 	i = fxdr_unsigned(int32_t, *tl);
   1223 	if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
   1224 		error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
   1225 		if (error)
   1226 			goto nfsmout;
   1227 	} else if (i > 0)
   1228 		nfsm_adv(nfsm_rndup(i));
   1229 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1230 	/* 0 == ok */
   1231 	if (*tl == 0) {
   1232 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
   1233 		if (*tl != 0) {
   1234 			error = fxdr_unsigned(int, *tl);
   1235 			switch (error) {
   1236 			case NFSERR_PERM:
   1237 				error = EPERM;
   1238 				break;
   1239 
   1240 			case NFSERR_NOENT:
   1241 				error = ENOENT;
   1242 				break;
   1243 
   1244 			case NFSERR_IO:
   1245 				error = EIO;
   1246 				break;
   1247 
   1248 			case NFSERR_NXIO:
   1249 				error = ENXIO;
   1250 				break;
   1251 
   1252 			case NFSERR_ACCES:
   1253 				error = EACCES;
   1254 				if (!retry_cred)
   1255 					break;
   1256 				m_freem(mrep);
   1257 				m_freem(rep->r_mreq);
   1258 				FREE(rep, M_NFSREQ);
   1259 				use_opencred = !use_opencred;
   1260 				if (mrest_backup == NULL) {
   1261 					/* m_copym failure */
   1262 					KASSERT(
   1263 					    kauth_cred_getrefcnt(acred) == 1);
   1264 					kauth_cred_free(acred);
   1265 					return ENOMEM;
   1266 				}
   1267 				mrest = mrest_backup;
   1268 				mrest_backup = NULL;
   1269 				cred = origcred;
   1270 				error = 0;
   1271 				retry_cred = false;
   1272 				goto tryagain_cred;
   1273 
   1274 			case NFSERR_EXIST:
   1275 				error = EEXIST;
   1276 				break;
   1277 
   1278 			case NFSERR_XDEV:
   1279 				error = EXDEV;
   1280 				break;
   1281 
   1282 			case NFSERR_NODEV:
   1283 				error = ENODEV;
   1284 				break;
   1285 
   1286 			case NFSERR_NOTDIR:
   1287 				error = ENOTDIR;
   1288 				break;
   1289 
   1290 			case NFSERR_ISDIR:
   1291 				error = EISDIR;
   1292 				break;
   1293 
   1294 			case NFSERR_INVAL:
   1295 				error = EINVAL;
   1296 				break;
   1297 
   1298 			case NFSERR_FBIG:
   1299 				error = EFBIG;
   1300 				break;
   1301 
   1302 			case NFSERR_NOSPC:
   1303 				error = ENOSPC;
   1304 				break;
   1305 
   1306 			case NFSERR_ROFS:
   1307 				error = EROFS;
   1308 				break;
   1309 
   1310 			case NFSERR_MLINK:
   1311 				error = EMLINK;
   1312 				break;
   1313 
   1314 			case NFSERR_TIMEDOUT:
   1315 				error = ETIMEDOUT;
   1316 				break;
   1317 
   1318 			case NFSERR_NAMETOL:
   1319 				error = ENAMETOOLONG;
   1320 				break;
   1321 
   1322 			case NFSERR_NOTEMPTY:
   1323 				error = ENOTEMPTY;
   1324 				break;
   1325 
   1326 			case NFSERR_DQUOT:
   1327 				error = EDQUOT;
   1328 				break;
   1329 
   1330 			case NFSERR_STALE:
   1331 				/*
   1332 				 * If the File Handle was stale, invalidate the
   1333 				 * lookup cache, just in case.
   1334 				 */
   1335 				error = ESTALE;
   1336 				cache_purge(NFSTOV(np));
   1337 				break;
   1338 
   1339 			case NFSERR_REMOTE:
   1340 				error = EREMOTE;
   1341 				break;
   1342 
   1343 			case NFSERR_WFLUSH:
   1344 			case NFSERR_BADHANDLE:
   1345 			case NFSERR_NOT_SYNC:
   1346 			case NFSERR_BAD_COOKIE:
   1347 				error = EINVAL;
   1348 				break;
   1349 
   1350 			case NFSERR_NOTSUPP:
   1351 				error = ENOTSUP;
   1352 				break;
   1353 
   1354 			case NFSERR_TOOSMALL:
   1355 			case NFSERR_SERVERFAULT:
   1356 			case NFSERR_BADTYPE:
   1357 				error = EINVAL;
   1358 				break;
   1359 
   1360 			case NFSERR_TRYLATER:
   1361 				if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
   1362 					break;
   1363 				m_freem(mrep);
   1364 				error = 0;
   1365 				waituntil = time_second + trylater_delay;
   1366 				while (time_second < waituntil) {
   1367 					tsleep(&lbolt, PSOCK, "nfstrylater", 0);
   1368 				}
   1369 				trylater_delay *= NFS_TRYLATERDELMUL;
   1370 				if (trylater_delay > NFS_TRYLATERDELMAX)
   1371 					trylater_delay = NFS_TRYLATERDELMAX;
   1372 				/*
   1373 				 * RFC1813:
   1374 				 * The client should wait and then try
   1375 				 * the request with a new RPC transaction ID.
   1376 				 */
   1377 				nfs_renewxid(rep);
   1378 				goto tryagain;
   1379 
   1380 			default:
   1381 #ifdef DIAGNOSTIC
   1382 				printf("Invalid rpc error code %d\n", error);
   1383 #endif
   1384 				error = EINVAL;
   1385 				break;
   1386 			}
   1387 
   1388 			if (nmp->nm_flag & NFSMNT_NFSV3) {
   1389 				*mrp = mrep;
   1390 				*mdp = md;
   1391 				*dposp = dpos;
   1392 				error |= NFSERR_RETERR;
   1393 			} else
   1394 				m_freem(mrep);
   1395 			goto nfsmout;
   1396 		}
   1397 
   1398 		/*
   1399 		 * note which credential worked to minimize number of retries.
   1400 		 */
   1401 		if (use_opencred)
   1402 			np->n_flag |= NUSEOPENCRED;
   1403 		else
   1404 			np->n_flag &= ~NUSEOPENCRED;
   1405 
   1406 		*mrp = mrep;
   1407 		*mdp = md;
   1408 		*dposp = dpos;
   1409 
   1410 		KASSERT(error == 0);
   1411 		goto nfsmout;
   1412 	}
   1413 	m_freem(mrep);
   1414 	error = EPROTONOSUPPORT;
   1415 nfsmout:
   1416 	KASSERT(kauth_cred_getrefcnt(acred) == 1);
   1417 	kauth_cred_free(acred);
   1418 	m_freem(rep->r_mreq);
   1419 	free((void *)rep, M_NFSREQ);
   1420 	m_freem(mrest_backup);
   1421 	return (error);
   1422 }
   1423 #endif /* NFS */
   1424 
   1425 /*
   1426  * Generate the rpc reply header
   1427  * siz arg. is used to decide if adding a cluster is worthwhile
   1428  */
   1429 int
   1430 nfs_rephead(siz, nd, slp, err, cache, frev, mrq, mbp, bposp)
   1431 	int siz;
   1432 	struct nfsrv_descript *nd;
   1433 	struct nfssvc_sock *slp;
   1434 	int err;
   1435 	int cache;
   1436 	u_quad_t *frev;
   1437 	struct mbuf **mrq;
   1438 	struct mbuf **mbp;
   1439 	char **bposp;
   1440 {
   1441 	u_int32_t *tl;
   1442 	struct mbuf *mreq;
   1443 	char *bpos;
   1444 	struct mbuf *mb;
   1445 
   1446 	mreq = m_gethdr(M_WAIT, MT_DATA);
   1447 	MCLAIM(mreq, &nfs_mowner);
   1448 	mb = mreq;
   1449 	/*
   1450 	 * If this is a big reply, use a cluster else
   1451 	 * try and leave leading space for the lower level headers.
   1452 	 */
   1453 	siz += RPC_REPLYSIZ;
   1454 	if (siz >= max_datalen) {
   1455 		m_clget(mreq, M_WAIT);
   1456 	} else
   1457 		mreq->m_data += max_hdr;
   1458 	tl = mtod(mreq, u_int32_t *);
   1459 	mreq->m_len = 6 * NFSX_UNSIGNED;
   1460 	bpos = ((char *)tl) + mreq->m_len;
   1461 	*tl++ = txdr_unsigned(nd->nd_retxid);
   1462 	*tl++ = rpc_reply;
   1463 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
   1464 		*tl++ = rpc_msgdenied;
   1465 		if (err & NFSERR_AUTHERR) {
   1466 			*tl++ = rpc_autherr;
   1467 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
   1468 			mreq->m_len -= NFSX_UNSIGNED;
   1469 			bpos -= NFSX_UNSIGNED;
   1470 		} else {
   1471 			*tl++ = rpc_mismatch;
   1472 			*tl++ = txdr_unsigned(RPC_VER2);
   1473 			*tl = txdr_unsigned(RPC_VER2);
   1474 		}
   1475 	} else {
   1476 		*tl++ = rpc_msgaccepted;
   1477 
   1478 		/*
   1479 		 * For Kerberos authentication, we must send the nickname
   1480 		 * verifier back, otherwise just RPCAUTH_NULL.
   1481 		 */
   1482 		if (nd->nd_flag & ND_KERBFULL) {
   1483 			struct nfsuid *nuidp;
   1484 			struct timeval ktvin, ktvout;
   1485 
   1486 			memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
   1487 
   1488 			LIST_FOREACH(nuidp,
   1489 			    NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
   1490 			    nu_hash) {
   1491 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
   1492 				kauth_cred_geteuid(nd->nd_cr) &&
   1493 				    (!nd->nd_nam2 || netaddr_match(
   1494 				    NU_NETFAM(nuidp), &nuidp->nu_haddr,
   1495 				    nd->nd_nam2)))
   1496 					break;
   1497 			}
   1498 			if (nuidp) {
   1499 				ktvin.tv_sec =
   1500 				    txdr_unsigned(nuidp->nu_timestamp.tv_sec
   1501 					- 1);
   1502 				ktvin.tv_usec =
   1503 				    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1504 
   1505 				/*
   1506 				 * Encrypt the timestamp in ecb mode using the
   1507 				 * session key.
   1508 				 */
   1509 #ifdef NFSKERB
   1510 				XXX
   1511 #endif
   1512 
   1513 				*tl++ = rpc_auth_kerb;
   1514 				*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
   1515 				*tl = ktvout.tv_sec;
   1516 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1517 				*tl++ = ktvout.tv_usec;
   1518 				*tl++ = txdr_unsigned(
   1519 				    kauth_cred_geteuid(nuidp->nu_cr));
   1520 			} else {
   1521 				*tl++ = 0;
   1522 				*tl++ = 0;
   1523 			}
   1524 		} else {
   1525 			*tl++ = 0;
   1526 			*tl++ = 0;
   1527 		}
   1528 		switch (err) {
   1529 		case EPROGUNAVAIL:
   1530 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
   1531 			break;
   1532 		case EPROGMISMATCH:
   1533 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
   1534 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1535 			*tl++ = txdr_unsigned(2);
   1536 			*tl = txdr_unsigned(3);
   1537 			break;
   1538 		case EPROCUNAVAIL:
   1539 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
   1540 			break;
   1541 		case EBADRPC:
   1542 			*tl = txdr_unsigned(RPC_GARBAGE);
   1543 			break;
   1544 		default:
   1545 			*tl = 0;
   1546 			if (err != NFSERR_RETVOID) {
   1547 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
   1548 				if (err)
   1549 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
   1550 				else
   1551 				    *tl = 0;
   1552 			}
   1553 			break;
   1554 		};
   1555 	}
   1556 
   1557 	if (mrq != NULL)
   1558 		*mrq = mreq;
   1559 	*mbp = mb;
   1560 	*bposp = bpos;
   1561 	if (err != 0 && err != NFSERR_RETVOID)
   1562 		nfsstats.srvrpc_errs++;
   1563 	return (0);
   1564 }
   1565 
   1566 /*
   1567  * Nfs timer routine
   1568  * Scan the nfsreq list and retranmit any requests that have timed out
   1569  * To avoid retransmission attempts on STREAM sockets (in the future) make
   1570  * sure to set the r_retry field to 0 (implies nm_retry == 0).
   1571  * A non-NULL argument means 'initialize'.
   1572  */
   1573 void
   1574 nfs_timer(void *arg)
   1575 {
   1576 	struct nfsreq *rep;
   1577 	struct mbuf *m;
   1578 	struct socket *so;
   1579 	struct nfsmount *nmp;
   1580 	int timeo;
   1581 	int s, error;
   1582 #ifdef NFSSERVER
   1583 	struct timeval tv;
   1584 	struct nfssvc_sock *slp;
   1585 	u_quad_t cur_usec;
   1586 #endif
   1587 
   1588 	if (arg != NULL) {
   1589 		callout_init(&nfs_timer_ch, 0);
   1590 		callout_setfunc(&nfs_timer_ch, nfs_timer, NULL);
   1591 	}
   1592 
   1593 	s = splsoftnet();
   1594 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
   1595 		nmp = rep->r_nmp;
   1596 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
   1597 			continue;
   1598 		if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
   1599 			rep->r_flags |= R_SOFTTERM;
   1600 			continue;
   1601 		}
   1602 		if (rep->r_rtt >= 0) {
   1603 			rep->r_rtt++;
   1604 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
   1605 				timeo = nmp->nm_timeo;
   1606 			else
   1607 				timeo = NFS_RTO(nmp, proct[rep->r_procnum]);
   1608 			if (nmp->nm_timeouts > 0)
   1609 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
   1610 			if (rep->r_rtt <= timeo)
   1611 				continue;
   1612 			if (nmp->nm_timeouts <
   1613 			    (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
   1614 				nmp->nm_timeouts++;
   1615 		}
   1616 		/*
   1617 		 * Check for server not responding
   1618 		 */
   1619 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
   1620 		     rep->r_rexmit > nmp->nm_deadthresh) {
   1621 			nfs_msg(rep->r_lwp,
   1622 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
   1623 			    "not responding");
   1624 			rep->r_flags |= R_TPRINTFMSG;
   1625 		}
   1626 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
   1627 			nfsstats.rpctimeouts++;
   1628 			rep->r_flags |= R_SOFTTERM;
   1629 			continue;
   1630 		}
   1631 		if (nmp->nm_sotype != SOCK_DGRAM) {
   1632 			if (++rep->r_rexmit > NFS_MAXREXMIT)
   1633 				rep->r_rexmit = NFS_MAXREXMIT;
   1634 			continue;
   1635 		}
   1636 		if ((so = nmp->nm_so) == NULL)
   1637 			continue;
   1638 
   1639 		/*
   1640 		 * If there is enough space and the window allows..
   1641 		 *	Resend it
   1642 		 * Set r_rtt to -1 in case we fail to send it now.
   1643 		 */
   1644 		rep->r_rtt = -1;
   1645 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
   1646 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
   1647 		    (rep->r_flags & R_SENT) ||
   1648 		    nmp->nm_sent < nmp->nm_cwnd) &&
   1649 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){
   1650 		        if (so->so_state & SS_ISCONNECTED)
   1651 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1652 			    (struct mbuf *)0, (struct mbuf *)0, (struct lwp *)0);
   1653 			else
   1654 			    error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, m,
   1655 			    nmp->nm_nam, (struct mbuf *)0, (struct lwp *)0);
   1656 			if (error) {
   1657 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
   1658 #ifdef DEBUG
   1659 					printf("nfs_timer: ignoring error %d\n",
   1660 						error);
   1661 #endif
   1662 					so->so_error = 0;
   1663 				}
   1664 			} else {
   1665 				/*
   1666 				 * Iff first send, start timing
   1667 				 * else turn timing off, backoff timer
   1668 				 * and divide congestion window by 2.
   1669 				 */
   1670 				if (rep->r_flags & R_SENT) {
   1671 					rep->r_flags &= ~R_TIMING;
   1672 					if (++rep->r_rexmit > NFS_MAXREXMIT)
   1673 						rep->r_rexmit = NFS_MAXREXMIT;
   1674 					nmp->nm_cwnd >>= 1;
   1675 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
   1676 						nmp->nm_cwnd = NFS_CWNDSCALE;
   1677 					nfsstats.rpcretries++;
   1678 				} else {
   1679 					rep->r_flags |= R_SENT;
   1680 					nmp->nm_sent += NFS_CWNDSCALE;
   1681 				}
   1682 				rep->r_rtt = 0;
   1683 			}
   1684 		}
   1685 	}
   1686 
   1687 #ifdef NFSSERVER
   1688 	/*
   1689 	 * Scan the write gathering queues for writes that need to be
   1690 	 * completed now.
   1691 	 */
   1692 	getmicrotime(&tv);
   1693 	cur_usec = (u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec;
   1694 	TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
   1695 	    if (LIST_FIRST(&slp->ns_tq) &&
   1696 		LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec)
   1697 		nfsrv_wakenfsd(slp);
   1698 	}
   1699 #endif /* NFSSERVER */
   1700 	splx(s);
   1701 	callout_schedule(&nfs_timer_ch, nfs_ticks);
   1702 }
   1703 
   1704 /*
   1705  * Test for a termination condition pending on the process.
   1706  * This is used for NFSMNT_INT mounts.
   1707  */
   1708 int
   1709 nfs_sigintr(nmp, rep, l)
   1710 	struct nfsmount *nmp;
   1711 	struct nfsreq *rep;
   1712 	struct lwp *l;
   1713 {
   1714 	sigset_t ss;
   1715 
   1716 	if (rep && (rep->r_flags & R_SOFTTERM))
   1717 		return (EINTR);
   1718 	if (!(nmp->nm_flag & NFSMNT_INT))
   1719 		return (0);
   1720 	if (l) {
   1721 		sigpending1(l, &ss);
   1722 #if 0
   1723 		sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
   1724 #endif
   1725 		if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
   1726 		    sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
   1727 		    sigismember(&ss, SIGQUIT))
   1728 			return (EINTR);
   1729 	}
   1730 	return (0);
   1731 }
   1732 
   1733 /*
   1734  * Lock a socket against others.
   1735  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
   1736  * and also to avoid race conditions between the processes with nfs requests
   1737  * in progress when a reconnect is necessary.
   1738  */
   1739 static int
   1740 nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
   1741 {
   1742 	struct lwp *l;
   1743 	int timeo = 0;
   1744 	bool catch = false;
   1745 	int error = 0;
   1746 
   1747 	if (rep) {
   1748 		l = rep->r_lwp;
   1749 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
   1750 			catch = true;
   1751 	} else
   1752 		l = NULL;
   1753 	mutex_enter(&nmp->nm_lock);
   1754 	while ((nmp->nm_iflag & NFSMNT_SNDLOCK) != 0) {
   1755 		if (rep && nfs_sigintr(rep->r_nmp, rep, l)) {
   1756 			error = EINTR;
   1757 			goto quit;
   1758 		}
   1759 		if (catch) {
   1760 			cv_timedwait_sig(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
   1761 		} else {
   1762 			cv_timedwait(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
   1763 		}
   1764 		if (catch) {
   1765 			catch = false;
   1766 			timeo = 2 * hz;
   1767 		}
   1768 	}
   1769 	nmp->nm_iflag |= NFSMNT_SNDLOCK;
   1770 quit:
   1771 	mutex_exit(&nmp->nm_lock);
   1772 	return error;
   1773 }
   1774 
   1775 /*
   1776  * Unlock the stream socket for others.
   1777  */
   1778 static void
   1779 nfs_sndunlock(struct nfsmount *nmp)
   1780 {
   1781 
   1782 	mutex_enter(&nmp->nm_lock);
   1783 	if ((nmp->nm_iflag & NFSMNT_SNDLOCK) == 0)
   1784 		panic("nfs sndunlock");
   1785 	nmp->nm_iflag &= ~NFSMNT_SNDLOCK;
   1786 	cv_signal(&nmp->nm_sndcv);
   1787 	mutex_exit(&nmp->nm_lock);
   1788 }
   1789 
   1790 static int
   1791 nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
   1792 {
   1793 	int *flagp = &nmp->nm_iflag;
   1794 	int slptimeo = 0;
   1795 	bool catch;
   1796 	int error = 0;
   1797 
   1798 	KASSERT(nmp == rep->r_nmp);
   1799 
   1800 	catch = (nmp->nm_flag & NFSMNT_INT) != 0;
   1801 	mutex_enter(&nmp->nm_lock);
   1802 	while (/* CONSTCOND */ true) {
   1803 		if (*flagp & NFSMNT_DISMNT) {
   1804 			cv_signal(&nmp->nm_disconcv);
   1805 			error = EIO;
   1806 			break;
   1807 		}
   1808 		/* If our reply was received while we were sleeping,
   1809 		 * then just return without taking the lock to avoid a
   1810 		 * situation where a single iod could 'capture' the
   1811 		 * receive lock.
   1812 		 */
   1813 		if (rep->r_mrep != NULL) {
   1814 			error = EALREADY;
   1815 			break;
   1816 		}
   1817 		if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
   1818 			error = EINTR;
   1819 			break;
   1820 		}
   1821 		if ((*flagp & NFSMNT_RCVLOCK) == 0) {
   1822 			*flagp |= NFSMNT_RCVLOCK;
   1823 			break;
   1824 		}
   1825 		if (catch) {
   1826 			cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
   1827 			    slptimeo);
   1828 		} else {
   1829 			cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
   1830 			    slptimeo);
   1831 		}
   1832 		if (catch) {
   1833 			catch = false;
   1834 			slptimeo = 2 * hz;
   1835 		}
   1836 	}
   1837 	mutex_exit(&nmp->nm_lock);
   1838 	return error;
   1839 }
   1840 
   1841 /*
   1842  * Unlock the stream socket for others.
   1843  */
   1844 static void
   1845 nfs_rcvunlock(struct nfsmount *nmp)
   1846 {
   1847 
   1848 	mutex_enter(&nmp->nm_lock);
   1849 	if ((nmp->nm_iflag & NFSMNT_RCVLOCK) == 0)
   1850 		panic("nfs rcvunlock");
   1851 	nmp->nm_iflag &= ~NFSMNT_RCVLOCK;
   1852 	cv_broadcast(&nmp->nm_rcvcv);
   1853 	mutex_exit(&nmp->nm_lock);
   1854 }
   1855 
   1856 /*
   1857  * Parse an RPC request
   1858  * - verify it
   1859  * - allocate and fill in the cred.
   1860  */
   1861 int
   1862 nfs_getreq(nd, nfsd, has_header)
   1863 	struct nfsrv_descript *nd;
   1864 	struct nfsd *nfsd;
   1865 	int has_header;
   1866 {
   1867 	int len, i;
   1868 	u_int32_t *tl;
   1869 	int32_t t1;
   1870 	struct uio uio;
   1871 	struct iovec iov;
   1872 	char *dpos, *cp2, *cp;
   1873 	u_int32_t nfsvers, auth_type;
   1874 	uid_t nickuid;
   1875 	int error = 0, ticklen;
   1876 	struct mbuf *mrep, *md;
   1877 	struct nfsuid *nuidp;
   1878 	struct timeval tvin, tvout;
   1879 
   1880 	memset(&tvout, 0, sizeof tvout);	/* XXX gcc */
   1881 
   1882 	KASSERT(nd->nd_cr == NULL);
   1883 	mrep = nd->nd_mrep;
   1884 	md = nd->nd_md;
   1885 	dpos = nd->nd_dpos;
   1886 	if (has_header) {
   1887 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
   1888 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
   1889 		if (*tl++ != rpc_call) {
   1890 			m_freem(mrep);
   1891 			return (EBADRPC);
   1892 		}
   1893 	} else
   1894 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
   1895 	nd->nd_repstat = 0;
   1896 	nd->nd_flag = 0;
   1897 	if (*tl++ != rpc_vers) {
   1898 		nd->nd_repstat = ERPCMISMATCH;
   1899 		nd->nd_procnum = NFSPROC_NOOP;
   1900 		return (0);
   1901 	}
   1902 	if (*tl != nfs_prog) {
   1903 		nd->nd_repstat = EPROGUNAVAIL;
   1904 		nd->nd_procnum = NFSPROC_NOOP;
   1905 		return (0);
   1906 	}
   1907 	tl++;
   1908 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
   1909 	if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
   1910 		nd->nd_repstat = EPROGMISMATCH;
   1911 		nd->nd_procnum = NFSPROC_NOOP;
   1912 		return (0);
   1913 	}
   1914 	if (nfsvers == NFS_VER3)
   1915 		nd->nd_flag = ND_NFSV3;
   1916 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
   1917 	if (nd->nd_procnum == NFSPROC_NULL)
   1918 		return (0);
   1919 	if (nd->nd_procnum > NFSPROC_COMMIT ||
   1920 	    (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
   1921 		nd->nd_repstat = EPROCUNAVAIL;
   1922 		nd->nd_procnum = NFSPROC_NOOP;
   1923 		return (0);
   1924 	}
   1925 	if ((nd->nd_flag & ND_NFSV3) == 0)
   1926 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
   1927 	auth_type = *tl++;
   1928 	len = fxdr_unsigned(int, *tl++);
   1929 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1930 		m_freem(mrep);
   1931 		return (EBADRPC);
   1932 	}
   1933 
   1934 	nd->nd_flag &= ~ND_KERBAUTH;
   1935 	/*
   1936 	 * Handle auth_unix or auth_kerb.
   1937 	 */
   1938 	if (auth_type == rpc_auth_unix) {
   1939 		uid_t uid;
   1940 		gid_t gid, *grbuf;
   1941 
   1942 		nd->nd_cr = kauth_cred_alloc();
   1943 		len = fxdr_unsigned(int, *++tl);
   1944 		if (len < 0 || len > NFS_MAXNAMLEN) {
   1945 			m_freem(mrep);
   1946 			error = EBADRPC;
   1947 			goto errout;
   1948 		}
   1949 		nfsm_adv(nfsm_rndup(len));
   1950 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1951 
   1952 		uid = fxdr_unsigned(uid_t, *tl++);
   1953 		gid = fxdr_unsigned(gid_t, *tl++);
   1954 		kauth_cred_setuid(nd->nd_cr, uid);
   1955 		kauth_cred_seteuid(nd->nd_cr, uid);
   1956 		kauth_cred_setsvuid(nd->nd_cr, uid);
   1957 		kauth_cred_setgid(nd->nd_cr, gid);
   1958 		kauth_cred_setegid(nd->nd_cr, gid);
   1959 		kauth_cred_setsvgid(nd->nd_cr, gid);
   1960 
   1961 		len = fxdr_unsigned(int, *tl);
   1962 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
   1963 			m_freem(mrep);
   1964 			error = EBADRPC;
   1965 			goto errout;
   1966 		}
   1967 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
   1968 
   1969 		grbuf = malloc(len * sizeof(gid_t), M_TEMP, M_WAITOK);
   1970 		for (i = 0; i < len; i++) {
   1971 			if (i < NGROUPS) /* XXX elad */
   1972 				grbuf[i] = fxdr_unsigned(gid_t, *tl++);
   1973 			else
   1974 				tl++;
   1975 		}
   1976 		kauth_cred_setgroups(nd->nd_cr, grbuf, min(len, NGROUPS), -1,
   1977 		    UIO_SYSSPACE);
   1978 		free(grbuf, M_TEMP);
   1979 
   1980 		len = fxdr_unsigned(int, *++tl);
   1981 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1982 			m_freem(mrep);
   1983 			error = EBADRPC;
   1984 			goto errout;
   1985 		}
   1986 		if (len > 0)
   1987 			nfsm_adv(nfsm_rndup(len));
   1988 	} else if (auth_type == rpc_auth_kerb) {
   1989 		switch (fxdr_unsigned(int, *tl++)) {
   1990 		case RPCAKN_FULLNAME:
   1991 			ticklen = fxdr_unsigned(int, *tl);
   1992 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
   1993 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
   1994 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
   1995 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
   1996 				m_freem(mrep);
   1997 				error = EBADRPC;
   1998 				goto errout;
   1999 			}
   2000 			uio.uio_offset = 0;
   2001 			uio.uio_iov = &iov;
   2002 			uio.uio_iovcnt = 1;
   2003 			UIO_SETUP_SYSSPACE(&uio);
   2004 			iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
   2005 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
   2006 			nfsm_mtouio(&uio, uio.uio_resid);
   2007 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2008 			if (*tl++ != rpc_auth_kerb ||
   2009 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
   2010 				printf("Bad kerb verifier\n");
   2011 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   2012 				nd->nd_procnum = NFSPROC_NOOP;
   2013 				return (0);
   2014 			}
   2015 			nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
   2016 			tl = (u_int32_t *)cp;
   2017 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
   2018 				printf("Not fullname kerb verifier\n");
   2019 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   2020 				nd->nd_procnum = NFSPROC_NOOP;
   2021 				return (0);
   2022 			}
   2023 			cp += NFSX_UNSIGNED;
   2024 			memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
   2025 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
   2026 			nd->nd_flag |= ND_KERBFULL;
   2027 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
   2028 			break;
   2029 		case RPCAKN_NICKNAME:
   2030 			if (len != 2 * NFSX_UNSIGNED) {
   2031 				printf("Kerb nickname short\n");
   2032 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
   2033 				nd->nd_procnum = NFSPROC_NOOP;
   2034 				return (0);
   2035 			}
   2036 			nickuid = fxdr_unsigned(uid_t, *tl);
   2037 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   2038 			if (*tl++ != rpc_auth_kerb ||
   2039 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
   2040 				printf("Kerb nick verifier bad\n");
   2041 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   2042 				nd->nd_procnum = NFSPROC_NOOP;
   2043 				return (0);
   2044 			}
   2045 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   2046 			tvin.tv_sec = *tl++;
   2047 			tvin.tv_usec = *tl;
   2048 
   2049 			LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
   2050 			    nu_hash) {
   2051 				if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
   2052 				    (!nd->nd_nam2 ||
   2053 				     netaddr_match(NU_NETFAM(nuidp),
   2054 				      &nuidp->nu_haddr, nd->nd_nam2)))
   2055 					break;
   2056 			}
   2057 			if (!nuidp) {
   2058 				nd->nd_repstat =
   2059 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
   2060 				nd->nd_procnum = NFSPROC_NOOP;
   2061 				return (0);
   2062 			}
   2063 
   2064 			/*
   2065 			 * Now, decrypt the timestamp using the session key
   2066 			 * and validate it.
   2067 			 */
   2068 #ifdef NFSKERB
   2069 			XXX
   2070 #endif
   2071 
   2072 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
   2073 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
   2074 			if (nuidp->nu_expire < time_second ||
   2075 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
   2076 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
   2077 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
   2078 				nuidp->nu_expire = 0;
   2079 				nd->nd_repstat =
   2080 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
   2081 				nd->nd_procnum = NFSPROC_NOOP;
   2082 				return (0);
   2083 			}
   2084 			kauth_cred_hold(nuidp->nu_cr);
   2085 			nd->nd_cr = nuidp->nu_cr;
   2086 			nd->nd_flag |= ND_KERBNICK;
   2087 		}
   2088 	} else {
   2089 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
   2090 		nd->nd_procnum = NFSPROC_NOOP;
   2091 		return (0);
   2092 	}
   2093 
   2094 	nd->nd_md = md;
   2095 	nd->nd_dpos = dpos;
   2096 	KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
   2097 	     || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
   2098 	return (0);
   2099 nfsmout:
   2100 errout:
   2101 	KASSERT(error != 0);
   2102 	if (nd->nd_cr != NULL) {
   2103 		kauth_cred_free(nd->nd_cr);
   2104 		nd->nd_cr = NULL;
   2105 	}
   2106 	return (error);
   2107 }
   2108 
   2109 int
   2110 nfs_msg(l, server, msg)
   2111 	struct lwp *l;
   2112 	const char *server, *msg;
   2113 {
   2114 	tpr_t tpr;
   2115 
   2116 	if (l)
   2117 		tpr = tprintf_open(l->l_proc);
   2118 	else
   2119 		tpr = NULL;
   2120 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
   2121 	tprintf_close(tpr);
   2122 	return (0);
   2123 }
   2124 
   2125 #ifdef NFSSERVER
   2126 int (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
   2127 				    struct nfssvc_sock *, struct lwp *,
   2128 				    struct mbuf **)) = {
   2129 	nfsrv_null,
   2130 	nfsrv_getattr,
   2131 	nfsrv_setattr,
   2132 	nfsrv_lookup,
   2133 	nfsrv3_access,
   2134 	nfsrv_readlink,
   2135 	nfsrv_read,
   2136 	nfsrv_write,
   2137 	nfsrv_create,
   2138 	nfsrv_mkdir,
   2139 	nfsrv_symlink,
   2140 	nfsrv_mknod,
   2141 	nfsrv_remove,
   2142 	nfsrv_rmdir,
   2143 	nfsrv_rename,
   2144 	nfsrv_link,
   2145 	nfsrv_readdir,
   2146 	nfsrv_readdirplus,
   2147 	nfsrv_statfs,
   2148 	nfsrv_fsinfo,
   2149 	nfsrv_pathconf,
   2150 	nfsrv_commit,
   2151 	nfsrv_noop
   2152 };
   2153 
   2154 /*
   2155  * Socket upcall routine for the nfsd sockets.
   2156  * The void *arg is a pointer to the "struct nfssvc_sock".
   2157  * Essentially do as much as possible non-blocking, else punt and it will
   2158  * be called with M_WAIT from an nfsd.
   2159  */
   2160 void
   2161 nfsrv_rcv(so, arg, waitflag)
   2162 	struct socket *so;
   2163 	void *arg;
   2164 	int waitflag;
   2165 {
   2166 	struct nfssvc_sock *slp = (struct nfssvc_sock *)arg;
   2167 	struct mbuf *m;
   2168 	struct mbuf *mp, *nam;
   2169 	struct uio auio;
   2170 	int flags, error;
   2171 	int setflags = 0;
   2172 
   2173 	error = nfsdsock_lock(slp, (waitflag != M_DONTWAIT));
   2174 	if (error) {
   2175 		setflags |= SLP_NEEDQ;
   2176 		goto dorecs_unlocked;
   2177 	}
   2178 
   2179 	KASSERT(so == slp->ns_so);
   2180 #define NFS_TEST_HEAVY
   2181 #ifdef NFS_TEST_HEAVY
   2182 	/*
   2183 	 * Define this to test for nfsds handling this under heavy load.
   2184 	 *
   2185 	 * XXX it isn't safe to call so_receive from so_upcall context.
   2186 	 */
   2187 	if (waitflag == M_DONTWAIT) {
   2188 		setflags |= SLP_NEEDQ;
   2189 		goto dorecs;
   2190 	}
   2191 #endif
   2192 	mutex_enter(&slp->ns_lock);
   2193 	slp->ns_flag &= ~SLP_NEEDQ;
   2194 	mutex_exit(&slp->ns_lock);
   2195 	if (so->so_type == SOCK_STREAM) {
   2196 #ifndef NFS_TEST_HEAVY
   2197 		/*
   2198 		 * If there are already records on the queue, defer soreceive()
   2199 		 * to an nfsd so that there is feedback to the TCP layer that
   2200 		 * the nfs servers are heavily loaded.
   2201 		 */
   2202 		if (slp->ns_rec && waitflag == M_DONTWAIT) {
   2203 			setflags |= SLP_NEEDQ;
   2204 			goto dorecs;
   2205 		}
   2206 #endif
   2207 
   2208 		/*
   2209 		 * Do soreceive().
   2210 		 */
   2211 		auio.uio_resid = 1000000000;
   2212 		/* not need to setup uio_vmspace */
   2213 		flags = MSG_DONTWAIT;
   2214 		error = (*so->so_receive)(so, &nam, &auio, &mp, NULL, &flags);
   2215 		if (error || mp == NULL) {
   2216 			if (error == EWOULDBLOCK)
   2217 				setflags |= SLP_NEEDQ;
   2218 			else
   2219 				setflags |= SLP_DISCONN;
   2220 			goto dorecs;
   2221 		}
   2222 		m = mp;
   2223 		m_claimm(m, &nfs_mowner);
   2224 		if (slp->ns_rawend) {
   2225 			slp->ns_rawend->m_next = m;
   2226 			slp->ns_cc += 1000000000 - auio.uio_resid;
   2227 		} else {
   2228 			slp->ns_raw = m;
   2229 			slp->ns_cc = 1000000000 - auio.uio_resid;
   2230 		}
   2231 		while (m->m_next)
   2232 			m = m->m_next;
   2233 		slp->ns_rawend = m;
   2234 
   2235 		/*
   2236 		 * Now try and parse record(s) out of the raw stream data.
   2237 		 */
   2238 		error = nfsrv_getstream(slp, waitflag);
   2239 		if (error) {
   2240 			if (error == EPERM)
   2241 				setflags |= SLP_DISCONN;
   2242 			else
   2243 				setflags |= SLP_NEEDQ;
   2244 		}
   2245 	} else {
   2246 		do {
   2247 			auio.uio_resid = 1000000000;
   2248 			/* not need to setup uio_vmspace */
   2249 			flags = MSG_DONTWAIT;
   2250 			error = (*so->so_receive)(so, &nam, &auio, &mp, NULL,
   2251 			    &flags);
   2252 			if (mp) {
   2253 				if (nam) {
   2254 					m = nam;
   2255 					m->m_next = mp;
   2256 				} else
   2257 					m = mp;
   2258 				m_claimm(m, &nfs_mowner);
   2259 				if (slp->ns_recend)
   2260 					slp->ns_recend->m_nextpkt = m;
   2261 				else
   2262 					slp->ns_rec = m;
   2263 				slp->ns_recend = m;
   2264 				m->m_nextpkt = (struct mbuf *)0;
   2265 			}
   2266 			if (error) {
   2267 				if ((so->so_proto->pr_flags & PR_CONNREQUIRED)
   2268 				    && error != EWOULDBLOCK) {
   2269 					setflags |= SLP_DISCONN;
   2270 					goto dorecs;
   2271 				}
   2272 			}
   2273 		} while (mp);
   2274 	}
   2275 dorecs:
   2276 	nfsdsock_unlock(slp);
   2277 
   2278 dorecs_unlocked:
   2279 	/*
   2280 	 * Now try and process the request records, non-blocking.
   2281 	 */
   2282 	if (setflags) {
   2283 		mutex_enter(&slp->ns_lock);
   2284 		slp->ns_flag |= setflags;
   2285 		mutex_exit(&slp->ns_lock);
   2286 	}
   2287 	if (waitflag == M_DONTWAIT &&
   2288 	    (slp->ns_rec || (slp->ns_flag & (SLP_DISCONN | SLP_NEEDQ)) != 0)) {
   2289 		nfsrv_wakenfsd(slp);
   2290 	}
   2291 }
   2292 
   2293 int
   2294 nfsdsock_lock(struct nfssvc_sock *slp, bool waitok)
   2295 {
   2296 
   2297 	mutex_enter(&slp->ns_lock);
   2298 	while ((~slp->ns_flag & (SLP_BUSY|SLP_VALID)) == 0) {
   2299 		if (!waitok) {
   2300 			mutex_exit(&slp->ns_lock);
   2301 			return EWOULDBLOCK;
   2302 		}
   2303 		cv_wait(&slp->ns_cv, &slp->ns_lock);
   2304 	}
   2305 	if ((slp->ns_flag & SLP_VALID) == 0) {
   2306 		mutex_exit(&slp->ns_lock);
   2307 		return EINVAL;
   2308 	}
   2309 	KASSERT((slp->ns_flag & SLP_BUSY) == 0);
   2310 	slp->ns_flag |= SLP_BUSY;
   2311 	mutex_exit(&slp->ns_lock);
   2312 
   2313 	return 0;
   2314 }
   2315 
   2316 void
   2317 nfsdsock_unlock(struct nfssvc_sock *slp)
   2318 {
   2319 
   2320 	mutex_enter(&slp->ns_lock);
   2321 	KASSERT((slp->ns_flag & SLP_BUSY) != 0);
   2322 	cv_broadcast(&slp->ns_cv);
   2323 	slp->ns_flag &= ~SLP_BUSY;
   2324 	mutex_exit(&slp->ns_lock);
   2325 }
   2326 
   2327 int
   2328 nfsdsock_drain(struct nfssvc_sock *slp)
   2329 {
   2330 	int error = 0;
   2331 
   2332 	mutex_enter(&slp->ns_lock);
   2333 	if ((slp->ns_flag & SLP_VALID) == 0) {
   2334 		error = EINVAL;
   2335 		goto done;
   2336 	}
   2337 	slp->ns_flag &= ~SLP_VALID;
   2338 	while ((slp->ns_flag & SLP_BUSY) != 0) {
   2339 		cv_wait(&slp->ns_cv, &slp->ns_lock);
   2340 	}
   2341 done:
   2342 	mutex_exit(&slp->ns_lock);
   2343 
   2344 	return error;
   2345 }
   2346 
   2347 /*
   2348  * Try and extract an RPC request from the mbuf data list received on a
   2349  * stream socket. The "waitflag" argument indicates whether or not it
   2350  * can sleep.
   2351  */
   2352 int
   2353 nfsrv_getstream(slp, waitflag)
   2354 	struct nfssvc_sock *slp;
   2355 	int waitflag;
   2356 {
   2357 	struct mbuf *m, **mpp;
   2358 	struct mbuf *recm;
   2359 	u_int32_t recmark;
   2360 	int error = 0;
   2361 
   2362 	KASSERT((slp->ns_flag & SLP_BUSY) != 0);
   2363 	for (;;) {
   2364 		if (slp->ns_reclen == 0) {
   2365 			if (slp->ns_cc < NFSX_UNSIGNED) {
   2366 				break;
   2367 			}
   2368 			m = slp->ns_raw;
   2369 			m_copydata(m, 0, NFSX_UNSIGNED, (void *)&recmark);
   2370 			m_adj(m, NFSX_UNSIGNED);
   2371 			slp->ns_cc -= NFSX_UNSIGNED;
   2372 			recmark = ntohl(recmark);
   2373 			slp->ns_reclen = recmark & ~0x80000000;
   2374 			if (recmark & 0x80000000)
   2375 				slp->ns_flag |= SLP_LASTFRAG;
   2376 			else
   2377 				slp->ns_flag &= ~SLP_LASTFRAG;
   2378 			if (slp->ns_reclen > NFS_MAXPACKET) {
   2379 				error = EPERM;
   2380 				break;
   2381 			}
   2382 		}
   2383 
   2384 		/*
   2385 		 * Now get the record part.
   2386 		 *
   2387 		 * Note that slp->ns_reclen may be 0.  Linux sometimes
   2388 		 * generates 0-length records.
   2389 		 */
   2390 		if (slp->ns_cc == slp->ns_reclen) {
   2391 			recm = slp->ns_raw;
   2392 			slp->ns_raw = slp->ns_rawend = (struct mbuf *)0;
   2393 			slp->ns_cc = slp->ns_reclen = 0;
   2394 		} else if (slp->ns_cc > slp->ns_reclen) {
   2395 			recm = slp->ns_raw;
   2396 			m = m_split(recm, slp->ns_reclen, waitflag);
   2397 			if (m == NULL) {
   2398 				error = EWOULDBLOCK;
   2399 				break;
   2400 			}
   2401 			m_claimm(recm, &nfs_mowner);
   2402 			slp->ns_raw = m;
   2403 			if (m->m_next == NULL)
   2404 				slp->ns_rawend = m;
   2405 			slp->ns_cc -= slp->ns_reclen;
   2406 			slp->ns_reclen = 0;
   2407 		} else {
   2408 			break;
   2409 		}
   2410 
   2411 		/*
   2412 		 * Accumulate the fragments into a record.
   2413 		 */
   2414 		mpp = &slp->ns_frag;
   2415 		while (*mpp)
   2416 			mpp = &((*mpp)->m_next);
   2417 		*mpp = recm;
   2418 		if (slp->ns_flag & SLP_LASTFRAG) {
   2419 			if (slp->ns_recend)
   2420 				slp->ns_recend->m_nextpkt = slp->ns_frag;
   2421 			else
   2422 				slp->ns_rec = slp->ns_frag;
   2423 			slp->ns_recend = slp->ns_frag;
   2424 			slp->ns_frag = (struct mbuf *)0;
   2425 		}
   2426 	}
   2427 
   2428 	return error;
   2429 }
   2430 
   2431 /*
   2432  * Parse an RPC header.
   2433  */
   2434 int
   2435 nfsrv_dorec(slp, nfsd, ndp)
   2436 	struct nfssvc_sock *slp;
   2437 	struct nfsd *nfsd;
   2438 	struct nfsrv_descript **ndp;
   2439 {
   2440 	struct mbuf *m, *nam;
   2441 	struct nfsrv_descript *nd;
   2442 	int error;
   2443 
   2444 	*ndp = NULL;
   2445 
   2446 	if (nfsdsock_lock(slp, true)) {
   2447 		return ENOBUFS;
   2448 	}
   2449 	m = slp->ns_rec;
   2450 	if (m == NULL) {
   2451 		nfsdsock_unlock(slp);
   2452 		return ENOBUFS;
   2453 	}
   2454 	slp->ns_rec = m->m_nextpkt;
   2455 	if (slp->ns_rec)
   2456 		m->m_nextpkt = NULL;
   2457 	else
   2458 		slp->ns_recend = NULL;
   2459 	nfsdsock_unlock(slp);
   2460 
   2461 	if (m->m_type == MT_SONAME) {
   2462 		nam = m;
   2463 		m = m->m_next;
   2464 		nam->m_next = NULL;
   2465 	} else
   2466 		nam = NULL;
   2467 	nd = nfsdreq_alloc();
   2468 	nd->nd_md = nd->nd_mrep = m;
   2469 	nd->nd_nam2 = nam;
   2470 	nd->nd_dpos = mtod(m, void *);
   2471 	error = nfs_getreq(nd, nfsd, true);
   2472 	if (error) {
   2473 		m_freem(nam);
   2474 		nfsdreq_free(nd);
   2475 		return (error);
   2476 	}
   2477 	*ndp = nd;
   2478 	nfsd->nfsd_nd = nd;
   2479 	return (0);
   2480 }
   2481 
   2482 /*
   2483  * Search for a sleeping nfsd and wake it up.
   2484  * SIDE EFFECT: If none found, set NFSD_CHECKSLP flag, so that one of the
   2485  * running nfsds will go look for the work in the nfssvc_sock list.
   2486  */
   2487 void
   2488 nfsrv_wakenfsd(slp)
   2489 	struct nfssvc_sock *slp;
   2490 {
   2491 	struct nfsd *nd;
   2492 
   2493 	if ((slp->ns_flag & SLP_VALID) == 0)
   2494 		return;
   2495 	mutex_enter(&nfsd_lock);
   2496 	if (slp->ns_flag & SLP_DOREC) {
   2497 		mutex_exit(&nfsd_lock);
   2498 		return;
   2499 	}
   2500 	nd = SLIST_FIRST(&nfsd_idle_head);
   2501 	if (nd) {
   2502 		SLIST_REMOVE_HEAD(&nfsd_idle_head, nfsd_idle);
   2503 		if (nd->nfsd_slp)
   2504 			panic("nfsd wakeup");
   2505 		slp->ns_sref++;
   2506 		KASSERT(slp->ns_sref > 0);
   2507 		nd->nfsd_slp = slp;
   2508 		cv_signal(&nd->nfsd_cv);
   2509 	} else {
   2510 		slp->ns_flag |= SLP_DOREC;
   2511 		nfsd_head_flag |= NFSD_CHECKSLP;
   2512 		TAILQ_INSERT_TAIL(&nfssvc_sockpending, slp, ns_pending);
   2513 	}
   2514 	mutex_exit(&nfsd_lock);
   2515 }
   2516 
   2517 int
   2518 nfsdsock_sendreply(struct nfssvc_sock *slp, struct nfsrv_descript *nd)
   2519 {
   2520 	int error;
   2521 
   2522 	if (nd->nd_mrep != NULL) {
   2523 		m_freem(nd->nd_mrep);
   2524 		nd->nd_mrep = NULL;
   2525 	}
   2526 
   2527 	mutex_enter(&slp->ns_lock);
   2528 	if ((slp->ns_flag & SLP_SENDING) != 0) {
   2529 		SIMPLEQ_INSERT_TAIL(&slp->ns_sendq, nd, nd_sendq);
   2530 		mutex_exit(&slp->ns_lock);
   2531 		return 0;
   2532 	}
   2533 	KASSERT(SIMPLEQ_EMPTY(&slp->ns_sendq));
   2534 	slp->ns_flag |= SLP_SENDING;
   2535 	mutex_exit(&slp->ns_lock);
   2536 
   2537 again:
   2538 	error = nfs_send(slp->ns_so, nd->nd_nam2, nd->nd_mreq, NULL, curlwp);
   2539 	if (nd->nd_nam2) {
   2540 		m_free(nd->nd_nam2);
   2541 	}
   2542 	nfsdreq_free(nd);
   2543 
   2544 	mutex_enter(&slp->ns_lock);
   2545 	KASSERT((slp->ns_flag & SLP_SENDING) != 0);
   2546 	nd = SIMPLEQ_FIRST(&slp->ns_sendq);
   2547 	if (nd != NULL) {
   2548 		SIMPLEQ_REMOVE_HEAD(&slp->ns_sendq, nd_sendq);
   2549 		mutex_exit(&slp->ns_lock);
   2550 		goto again;
   2551 	}
   2552 	slp->ns_flag &= ~SLP_SENDING;
   2553 	mutex_exit(&slp->ns_lock);
   2554 
   2555 	return error;
   2556 }
   2557 #endif /* NFSSERVER */
   2558 
   2559 #if defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY))
   2560 static struct pool nfs_srvdesc_pool;
   2561 
   2562 void
   2563 nfsdreq_init(void)
   2564 {
   2565 
   2566 	pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
   2567 	    0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
   2568 }
   2569 
   2570 struct nfsrv_descript *
   2571 nfsdreq_alloc(void)
   2572 {
   2573 	struct nfsrv_descript *nd;
   2574 
   2575 	nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
   2576 	nd->nd_cr = NULL;
   2577 	return nd;
   2578 }
   2579 
   2580 void
   2581 nfsdreq_free(struct nfsrv_descript *nd)
   2582 {
   2583 	kauth_cred_t cr;
   2584 
   2585 	cr = nd->nd_cr;
   2586 	if (cr != NULL) {
   2587 		kauth_cred_free(cr);
   2588 	}
   2589 	pool_put(&nfs_srvdesc_pool, nd);
   2590 }
   2591 #endif /* defined(NFSSERVER) || (defined(NFS) && !defined(NFS_V2_ONLY)) */
   2592