Home | History | Annotate | Line # | Download | only in nfs
nfs_clntsocket.c revision 1.1.40.2
      1  1.1.40.2  martin /*	$NetBSD: nfs_clntsocket.c,v 1.1.40.2 2016/07/10 09:42:34 martin Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*
      4       1.1   pooka  * Copyright (c) 1989, 1991, 1993, 1995
      5       1.1   pooka  *	The Regents of the University of California.  All rights reserved.
      6       1.1   pooka  *
      7       1.1   pooka  * This code is derived from software contributed to Berkeley by
      8       1.1   pooka  * Rick Macklem at The University of Guelph.
      9       1.1   pooka  *
     10       1.1   pooka  * Redistribution and use in source and binary forms, with or without
     11       1.1   pooka  * modification, are permitted provided that the following conditions
     12       1.1   pooka  * are met:
     13       1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     14       1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     15       1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   pooka  *    documentation and/or other materials provided with the distribution.
     18       1.1   pooka  * 3. Neither the name of the University nor the names of its contributors
     19       1.1   pooka  *    may be used to endorse or promote products derived from this software
     20       1.1   pooka  *    without specific prior written permission.
     21       1.1   pooka  *
     22       1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23       1.1   pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24       1.1   pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25       1.1   pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26       1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27       1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28       1.1   pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29       1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30       1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31       1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32       1.1   pooka  * SUCH DAMAGE.
     33       1.1   pooka  *
     34       1.1   pooka  *	@(#)nfs_socket.c	8.5 (Berkeley) 3/30/95
     35       1.1   pooka  */
     36       1.1   pooka 
     37       1.1   pooka /*
     38       1.1   pooka  * Socket operations for use by nfs
     39       1.1   pooka  */
     40       1.1   pooka 
     41       1.1   pooka #include <sys/cdefs.h>
     42  1.1.40.2  martin __KERNEL_RCSID(0, "$NetBSD: nfs_clntsocket.c,v 1.1.40.2 2016/07/10 09:42:34 martin Exp $");
     43       1.1   pooka 
     44       1.1   pooka #ifdef _KERNEL_OPT
     45       1.1   pooka #include "opt_nfs.h"
     46       1.1   pooka #include "opt_mbuftrace.h"
     47       1.1   pooka #endif
     48       1.1   pooka 
     49       1.1   pooka #include <sys/param.h>
     50       1.1   pooka #include <sys/systm.h>
     51       1.1   pooka #include <sys/evcnt.h>
     52       1.1   pooka #include <sys/callout.h>
     53       1.1   pooka #include <sys/proc.h>
     54       1.1   pooka #include <sys/mount.h>
     55       1.1   pooka #include <sys/kernel.h>
     56       1.1   pooka #include <sys/kmem.h>
     57       1.1   pooka #include <sys/mbuf.h>
     58       1.1   pooka #include <sys/vnode.h>
     59       1.1   pooka #include <sys/domain.h>
     60       1.1   pooka #include <sys/protosw.h>
     61       1.1   pooka #include <sys/socket.h>
     62       1.1   pooka #include <sys/socketvar.h>
     63       1.1   pooka #include <sys/syslog.h>
     64       1.1   pooka #include <sys/tprintf.h>
     65       1.1   pooka #include <sys/namei.h>
     66       1.1   pooka #include <sys/signal.h>
     67       1.1   pooka #include <sys/signalvar.h>
     68       1.1   pooka #include <sys/kauth.h>
     69       1.1   pooka 
     70       1.1   pooka #include <netinet/in.h>
     71       1.1   pooka #include <netinet/tcp.h>
     72       1.1   pooka 
     73       1.1   pooka #include <nfs/rpcv2.h>
     74       1.1   pooka #include <nfs/nfsproto.h>
     75       1.1   pooka #include <nfs/nfs.h>
     76       1.1   pooka #include <nfs/xdr_subs.h>
     77       1.1   pooka #include <nfs/nfsm_subs.h>
     78       1.1   pooka #include <nfs/nfsmount.h>
     79       1.1   pooka #include <nfs/nfsnode.h>
     80       1.1   pooka #include <nfs/nfsrtt.h>
     81       1.1   pooka #include <nfs/nfs_var.h>
     82       1.1   pooka 
     83       1.1   pooka static int nfs_sndlock(struct nfsmount *, struct nfsreq *);
     84       1.1   pooka static void nfs_sndunlock(struct nfsmount *);
     85       1.1   pooka 
     86       1.1   pooka /*
     87       1.1   pooka  * Receive a Sun RPC Request/Reply. For SOCK_DGRAM, the work is all
     88       1.1   pooka  * done by soreceive(), but for SOCK_STREAM we must deal with the Record
     89       1.1   pooka  * Mark and consolidate the data into a new mbuf list.
     90       1.1   pooka  * nb: Sometimes TCP passes the data up to soreceive() in long lists of
     91       1.1   pooka  *     small mbufs.
     92       1.1   pooka  * For SOCK_STREAM we must be very careful to read an entire record once
     93       1.1   pooka  * we have read any of it, even if the system call has been interrupted.
     94       1.1   pooka  */
     95       1.1   pooka static int
     96       1.1   pooka nfs_receive(struct nfsreq *rep, struct mbuf **aname, struct mbuf **mp,
     97       1.1   pooka     struct lwp *l)
     98       1.1   pooka {
     99       1.1   pooka 	struct socket *so;
    100       1.1   pooka 	struct uio auio;
    101       1.1   pooka 	struct iovec aio;
    102       1.1   pooka 	struct mbuf *m;
    103       1.1   pooka 	struct mbuf *control;
    104       1.1   pooka 	u_int32_t len;
    105       1.1   pooka 	struct mbuf **getnam;
    106       1.1   pooka 	int error, sotype, rcvflg;
    107       1.1   pooka 
    108       1.1   pooka 	/*
    109       1.1   pooka 	 * Set up arguments for soreceive()
    110       1.1   pooka 	 */
    111       1.1   pooka 	*mp = NULL;
    112       1.1   pooka 	*aname = NULL;
    113       1.1   pooka 	sotype = rep->r_nmp->nm_sotype;
    114       1.1   pooka 
    115       1.1   pooka 	/*
    116       1.1   pooka 	 * For reliable protocols, lock against other senders/receivers
    117       1.1   pooka 	 * in case a reconnect is necessary.
    118       1.1   pooka 	 * For SOCK_STREAM, first get the Record Mark to find out how much
    119       1.1   pooka 	 * more there is to get.
    120       1.1   pooka 	 * We must lock the socket against other receivers
    121       1.1   pooka 	 * until we have an entire rpc request/reply.
    122       1.1   pooka 	 */
    123       1.1   pooka 	if (sotype != SOCK_DGRAM) {
    124       1.1   pooka 		error = nfs_sndlock(rep->r_nmp, rep);
    125       1.1   pooka 		if (error)
    126       1.1   pooka 			return (error);
    127       1.1   pooka tryagain:
    128       1.1   pooka 		/*
    129       1.1   pooka 		 * Check for fatal errors and resending request.
    130       1.1   pooka 		 */
    131       1.1   pooka 		/*
    132       1.1   pooka 		 * Ugh: If a reconnect attempt just happened, nm_so
    133       1.1   pooka 		 * would have changed. NULL indicates a failed
    134       1.1   pooka 		 * attempt that has essentially shut down this
    135       1.1   pooka 		 * mount point.
    136       1.1   pooka 		 */
    137       1.1   pooka 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM)) {
    138       1.1   pooka 			nfs_sndunlock(rep->r_nmp);
    139       1.1   pooka 			return (EINTR);
    140       1.1   pooka 		}
    141       1.1   pooka 		so = rep->r_nmp->nm_so;
    142       1.1   pooka 		if (!so) {
    143       1.1   pooka 			error = nfs_reconnect(rep);
    144       1.1   pooka 			if (error) {
    145       1.1   pooka 				nfs_sndunlock(rep->r_nmp);
    146       1.1   pooka 				return (error);
    147       1.1   pooka 			}
    148       1.1   pooka 			goto tryagain;
    149       1.1   pooka 		}
    150       1.1   pooka 		while (rep->r_flags & R_MUSTRESEND) {
    151       1.1   pooka 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
    152       1.1   pooka 			nfsstats.rpcretries++;
    153       1.1   pooka 			rep->r_rtt = 0;
    154       1.1   pooka 			rep->r_flags &= ~R_TIMING;
    155       1.1   pooka 			error = nfs_send(so, rep->r_nmp->nm_nam, m, rep, l);
    156       1.1   pooka 			if (error) {
    157       1.1   pooka 				if (error == EINTR || error == ERESTART ||
    158       1.1   pooka 				    (error = nfs_reconnect(rep)) != 0) {
    159       1.1   pooka 					nfs_sndunlock(rep->r_nmp);
    160       1.1   pooka 					return (error);
    161       1.1   pooka 				}
    162       1.1   pooka 				goto tryagain;
    163       1.1   pooka 			}
    164       1.1   pooka 		}
    165       1.1   pooka 		nfs_sndunlock(rep->r_nmp);
    166       1.1   pooka 		if (sotype == SOCK_STREAM) {
    167       1.1   pooka 			aio.iov_base = (void *) &len;
    168       1.1   pooka 			aio.iov_len = sizeof(u_int32_t);
    169       1.1   pooka 			auio.uio_iov = &aio;
    170       1.1   pooka 			auio.uio_iovcnt = 1;
    171       1.1   pooka 			auio.uio_rw = UIO_READ;
    172       1.1   pooka 			auio.uio_offset = 0;
    173       1.1   pooka 			auio.uio_resid = sizeof(u_int32_t);
    174       1.1   pooka 			UIO_SETUP_SYSSPACE(&auio);
    175       1.1   pooka 			do {
    176       1.1   pooka 			   rcvflg = MSG_WAITALL;
    177       1.1   pooka 			   error = (*so->so_receive)(so, NULL, &auio,
    178       1.1   pooka 				NULL, NULL, &rcvflg);
    179       1.1   pooka 			   if (error == EWOULDBLOCK && rep) {
    180       1.1   pooka 				if (rep->r_flags & R_SOFTTERM)
    181       1.1   pooka 					return (EINTR);
    182       1.1   pooka 				/*
    183       1.1   pooka 				 * if it seems that the server died after it
    184       1.1   pooka 				 * received our request, set EPIPE so that
    185       1.1   pooka 				 * we'll reconnect and retransmit requests.
    186       1.1   pooka 				 */
    187       1.1   pooka 				if (rep->r_rexmit >= rep->r_nmp->nm_retry) {
    188       1.1   pooka 					nfsstats.rpctimeouts++;
    189       1.1   pooka 					error = EPIPE;
    190       1.1   pooka 				}
    191       1.1   pooka 			   }
    192       1.1   pooka 			} while (error == EWOULDBLOCK);
    193       1.1   pooka 			if (!error && auio.uio_resid > 0) {
    194       1.1   pooka 			    /*
    195       1.1   pooka 			     * Don't log a 0 byte receive; it means
    196       1.1   pooka 			     * that the socket has been closed, and
    197       1.1   pooka 			     * can happen during normal operation
    198       1.1   pooka 			     * (forcible unmount or Solaris server).
    199       1.1   pooka 			     */
    200       1.1   pooka 			    if (auio.uio_resid != sizeof (u_int32_t))
    201       1.1   pooka 			      log(LOG_INFO,
    202       1.1   pooka 				 "short receive (%lu/%lu) from nfs server %s\n",
    203       1.1   pooka 				 (u_long)sizeof(u_int32_t) - auio.uio_resid,
    204       1.1   pooka 				 (u_long)sizeof(u_int32_t),
    205       1.1   pooka 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    206       1.1   pooka 			    error = EPIPE;
    207       1.1   pooka 			}
    208       1.1   pooka 			if (error)
    209       1.1   pooka 				goto errout;
    210       1.1   pooka 			len = ntohl(len) & ~0x80000000;
    211       1.1   pooka 			/*
    212       1.1   pooka 			 * This is SERIOUS! We are out of sync with the sender
    213       1.1   pooka 			 * and forcing a disconnect/reconnect is all I can do.
    214       1.1   pooka 			 */
    215       1.1   pooka 			if (len > NFS_MAXPACKET) {
    216       1.1   pooka 			    log(LOG_ERR, "%s (%d) from nfs server %s\n",
    217       1.1   pooka 				"impossible packet length",
    218       1.1   pooka 				len,
    219       1.1   pooka 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    220       1.1   pooka 			    error = EFBIG;
    221       1.1   pooka 			    goto errout;
    222       1.1   pooka 			}
    223       1.1   pooka 			auio.uio_resid = len;
    224       1.1   pooka 			do {
    225       1.1   pooka 			    rcvflg = MSG_WAITALL;
    226       1.1   pooka 			    error =  (*so->so_receive)(so, NULL,
    227       1.1   pooka 				&auio, mp, NULL, &rcvflg);
    228       1.1   pooka 			} while (error == EWOULDBLOCK || error == EINTR ||
    229       1.1   pooka 				 error == ERESTART);
    230       1.1   pooka 			if (!error && auio.uio_resid > 0) {
    231       1.1   pooka 			    if (len != auio.uio_resid)
    232       1.1   pooka 			      log(LOG_INFO,
    233       1.1   pooka 				"short receive (%lu/%d) from nfs server %s\n",
    234       1.1   pooka 				(u_long)len - auio.uio_resid, len,
    235       1.1   pooka 				rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    236       1.1   pooka 			    error = EPIPE;
    237       1.1   pooka 			}
    238       1.1   pooka 		} else {
    239       1.1   pooka 			/*
    240       1.1   pooka 			 * NB: Since uio_resid is big, MSG_WAITALL is ignored
    241       1.1   pooka 			 * and soreceive() will return when it has either a
    242       1.1   pooka 			 * control msg or a data msg.
    243       1.1   pooka 			 * We have no use for control msg., but must grab them
    244       1.1   pooka 			 * and then throw them away so we know what is going
    245       1.1   pooka 			 * on.
    246       1.1   pooka 			 */
    247       1.1   pooka 			auio.uio_resid = len = 100000000; /* Anything Big */
    248       1.1   pooka 			/* not need to setup uio_vmspace */
    249       1.1   pooka 			do {
    250       1.1   pooka 			    rcvflg = 0;
    251       1.1   pooka 			    error =  (*so->so_receive)(so, NULL,
    252       1.1   pooka 				&auio, mp, &control, &rcvflg);
    253       1.1   pooka 			    if (control)
    254       1.1   pooka 				m_freem(control);
    255       1.1   pooka 			    if (error == EWOULDBLOCK && rep) {
    256       1.1   pooka 				if (rep->r_flags & R_SOFTTERM)
    257       1.1   pooka 					return (EINTR);
    258       1.1   pooka 			    }
    259       1.1   pooka 			} while (error == EWOULDBLOCK ||
    260       1.1   pooka 				 (!error && *mp == NULL && control));
    261       1.1   pooka 			if ((rcvflg & MSG_EOR) == 0)
    262       1.1   pooka 				printf("Egad!!\n");
    263       1.1   pooka 			if (!error && *mp == NULL)
    264       1.1   pooka 				error = EPIPE;
    265       1.1   pooka 			len -= auio.uio_resid;
    266       1.1   pooka 		}
    267       1.1   pooka errout:
    268       1.1   pooka 		if (error && error != EINTR && error != ERESTART) {
    269       1.1   pooka 			m_freem(*mp);
    270       1.1   pooka 			*mp = NULL;
    271       1.1   pooka 			if (error != EPIPE)
    272       1.1   pooka 				log(LOG_INFO,
    273       1.1   pooka 				    "receive error %d from nfs server %s\n",
    274       1.1   pooka 				    error,
    275       1.1   pooka 				 rep->r_nmp->nm_mountp->mnt_stat.f_mntfromname);
    276       1.1   pooka 			error = nfs_sndlock(rep->r_nmp, rep);
    277       1.1   pooka 			if (!error)
    278       1.1   pooka 				error = nfs_reconnect(rep);
    279       1.1   pooka 			if (!error)
    280       1.1   pooka 				goto tryagain;
    281       1.1   pooka 			else
    282       1.1   pooka 				nfs_sndunlock(rep->r_nmp);
    283       1.1   pooka 		}
    284       1.1   pooka 	} else {
    285       1.1   pooka 		if ((so = rep->r_nmp->nm_so) == NULL)
    286       1.1   pooka 			return (EACCES);
    287       1.1   pooka 		if (so->so_state & SS_ISCONNECTED)
    288       1.1   pooka 			getnam = NULL;
    289       1.1   pooka 		else
    290       1.1   pooka 			getnam = aname;
    291       1.1   pooka 		auio.uio_resid = len = 1000000;
    292       1.1   pooka 		/* not need to setup uio_vmspace */
    293       1.1   pooka 		do {
    294       1.1   pooka 			rcvflg = 0;
    295       1.1   pooka 			error =  (*so->so_receive)(so, getnam, &auio, mp,
    296       1.1   pooka 				NULL, &rcvflg);
    297       1.1   pooka 			if (error == EWOULDBLOCK &&
    298       1.1   pooka 			    (rep->r_flags & R_SOFTTERM))
    299       1.1   pooka 				return (EINTR);
    300       1.1   pooka 		} while (error == EWOULDBLOCK);
    301       1.1   pooka 		len -= auio.uio_resid;
    302       1.1   pooka 		if (!error && *mp == NULL)
    303       1.1   pooka 			error = EPIPE;
    304       1.1   pooka 	}
    305       1.1   pooka 	if (error) {
    306       1.1   pooka 		m_freem(*mp);
    307       1.1   pooka 		*mp = NULL;
    308       1.1   pooka 	}
    309       1.1   pooka 	return (error);
    310       1.1   pooka }
    311       1.1   pooka 
    312       1.1   pooka /*
    313       1.1   pooka  * Implement receipt of reply on a socket.
    314       1.1   pooka  * We must search through the list of received datagrams matching them
    315       1.1   pooka  * with outstanding requests using the xid, until ours is found.
    316       1.1   pooka  */
    317       1.1   pooka /* ARGSUSED */
    318       1.1   pooka static int
    319       1.1   pooka nfs_reply(struct nfsreq *myrep, struct lwp *lwp)
    320       1.1   pooka {
    321       1.1   pooka 	struct nfsreq *rep;
    322       1.1   pooka 	struct nfsmount *nmp = myrep->r_nmp;
    323       1.1   pooka 	int32_t t1;
    324       1.1   pooka 	struct mbuf *mrep, *nam, *md;
    325       1.1   pooka 	u_int32_t rxid, *tl;
    326       1.1   pooka 	char *dpos, *cp2;
    327  1.1.40.2  martin 	int error, s;
    328       1.1   pooka 
    329       1.1   pooka 	/*
    330       1.1   pooka 	 * Loop around until we get our own reply
    331       1.1   pooka 	 */
    332       1.1   pooka 	for (;;) {
    333       1.1   pooka 		/*
    334       1.1   pooka 		 * Lock against other receivers so that I don't get stuck in
    335       1.1   pooka 		 * sbwait() after someone else has received my reply for me.
    336       1.1   pooka 		 * Also necessary for connection based protocols to avoid
    337       1.1   pooka 		 * race conditions during a reconnect.
    338       1.1   pooka 		 */
    339       1.1   pooka 		error = nfs_rcvlock(nmp, myrep);
    340       1.1   pooka 		if (error == EALREADY)
    341       1.1   pooka 			return (0);
    342       1.1   pooka 		if (error)
    343       1.1   pooka 			return (error);
    344       1.1   pooka 		/*
    345       1.1   pooka 		 * Get the next Rpc reply off the socket
    346       1.1   pooka 		 */
    347       1.1   pooka 
    348       1.1   pooka 		mutex_enter(&nmp->nm_lock);
    349       1.1   pooka 		nmp->nm_waiters++;
    350       1.1   pooka 		mutex_exit(&nmp->nm_lock);
    351       1.1   pooka 
    352       1.1   pooka 		error = nfs_receive(myrep, &nam, &mrep, lwp);
    353       1.1   pooka 
    354       1.1   pooka 		mutex_enter(&nmp->nm_lock);
    355       1.1   pooka 		nmp->nm_waiters--;
    356       1.1   pooka 		cv_signal(&nmp->nm_disconcv);
    357       1.1   pooka 		mutex_exit(&nmp->nm_lock);
    358       1.1   pooka 
    359       1.1   pooka 		if (error) {
    360       1.1   pooka 			nfs_rcvunlock(nmp);
    361       1.1   pooka 
    362       1.1   pooka 			if (nmp->nm_iflag & NFSMNT_DISMNT) {
    363       1.1   pooka 				/*
    364       1.1   pooka 				 * Oops, we're going away now..
    365       1.1   pooka 				 */
    366       1.1   pooka 				return error;
    367       1.1   pooka 			}
    368       1.1   pooka 			/*
    369       1.1   pooka 			 * Ignore routing errors on connectionless protocols? ?
    370       1.1   pooka 			 */
    371       1.1   pooka 			if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
    372       1.1   pooka 				nmp->nm_so->so_error = 0;
    373       1.1   pooka #ifdef DEBUG
    374       1.1   pooka 				if (ratecheck(&nfs_reply_last_err_time,
    375       1.1   pooka 				    &nfs_err_interval))
    376       1.1   pooka 					printf("%s: ignoring error %d\n",
    377       1.1   pooka 					       __func__, error);
    378       1.1   pooka #endif
    379       1.1   pooka 				continue;
    380       1.1   pooka 			}
    381       1.1   pooka 			return (error);
    382       1.1   pooka 		}
    383       1.1   pooka 		if (nam)
    384       1.1   pooka 			m_freem(nam);
    385       1.1   pooka 
    386       1.1   pooka 		/*
    387       1.1   pooka 		 * Get the xid and check that it is an rpc reply
    388       1.1   pooka 		 */
    389       1.1   pooka 		md = mrep;
    390       1.1   pooka 		dpos = mtod(md, void *);
    391       1.1   pooka 		nfsm_dissect(tl, u_int32_t *, 2*NFSX_UNSIGNED);
    392       1.1   pooka 		rxid = *tl++;
    393       1.1   pooka 		if (*tl != rpc_reply) {
    394       1.1   pooka 			nfsstats.rpcinvalid++;
    395       1.1   pooka 			m_freem(mrep);
    396       1.1   pooka nfsmout:
    397       1.1   pooka 			nfs_rcvunlock(nmp);
    398       1.1   pooka 			continue;
    399       1.1   pooka 		}
    400       1.1   pooka 
    401       1.1   pooka 		/*
    402       1.1   pooka 		 * Loop through the request list to match up the reply
    403       1.1   pooka 		 * Iff no match, just drop the datagram
    404       1.1   pooka 		 */
    405  1.1.40.2  martin 		s = splsoftnet();
    406       1.1   pooka 		TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
    407       1.1   pooka 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
    408       1.1   pooka 				/* Found it.. */
    409       1.1   pooka 				rep->r_mrep = mrep;
    410       1.1   pooka 				rep->r_md = md;
    411       1.1   pooka 				rep->r_dpos = dpos;
    412       1.1   pooka 				if (nfsrtton) {
    413       1.1   pooka 					struct rttl *rt;
    414       1.1   pooka 
    415       1.1   pooka 					rt = &nfsrtt.rttl[nfsrtt.pos];
    416       1.1   pooka 					rt->proc = rep->r_procnum;
    417       1.1   pooka 					rt->rto = NFS_RTO(nmp, nfs_proct[rep->r_procnum]);
    418       1.1   pooka 					rt->sent = nmp->nm_sent;
    419       1.1   pooka 					rt->cwnd = nmp->nm_cwnd;
    420       1.1   pooka 					rt->srtt = nmp->nm_srtt[nfs_proct[rep->r_procnum] - 1];
    421       1.1   pooka 					rt->sdrtt = nmp->nm_sdrtt[nfs_proct[rep->r_procnum] - 1];
    422       1.1   pooka 					rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
    423       1.1   pooka 					getmicrotime(&rt->tstamp);
    424       1.1   pooka 					if (rep->r_flags & R_TIMING)
    425       1.1   pooka 						rt->rtt = rep->r_rtt;
    426       1.1   pooka 					else
    427       1.1   pooka 						rt->rtt = 1000000;
    428       1.1   pooka 					nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
    429       1.1   pooka 				}
    430       1.1   pooka 				/*
    431       1.1   pooka 				 * Update congestion window.
    432       1.1   pooka 				 * Do the additive increase of
    433       1.1   pooka 				 * one rpc/rtt.
    434       1.1   pooka 				 */
    435       1.1   pooka 				if (nmp->nm_cwnd <= nmp->nm_sent) {
    436       1.1   pooka 					nmp->nm_cwnd +=
    437       1.1   pooka 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
    438       1.1   pooka 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
    439       1.1   pooka 					if (nmp->nm_cwnd > NFS_MAXCWND)
    440       1.1   pooka 						nmp->nm_cwnd = NFS_MAXCWND;
    441       1.1   pooka 				}
    442       1.1   pooka 				rep->r_flags &= ~R_SENT;
    443       1.1   pooka 				nmp->nm_sent -= NFS_CWNDSCALE;
    444       1.1   pooka 				/*
    445       1.1   pooka 				 * Update rtt using a gain of 0.125 on the mean
    446       1.1   pooka 				 * and a gain of 0.25 on the deviation.
    447       1.1   pooka 				 */
    448       1.1   pooka 				if (rep->r_flags & R_TIMING) {
    449       1.1   pooka 					/*
    450       1.1   pooka 					 * Since the timer resolution of
    451       1.1   pooka 					 * NFS_HZ is so course, it can often
    452       1.1   pooka 					 * result in r_rtt == 0. Since
    453       1.1   pooka 					 * r_rtt == N means that the actual
    454       1.1   pooka 					 * rtt is between N+dt and N+2-dt ticks,
    455       1.1   pooka 					 * add 1.
    456       1.1   pooka 					 */
    457       1.1   pooka 					t1 = rep->r_rtt + 1;
    458       1.1   pooka 					t1 -= (NFS_SRTT(rep) >> 3);
    459       1.1   pooka 					NFS_SRTT(rep) += t1;
    460       1.1   pooka 					if (t1 < 0)
    461       1.1   pooka 						t1 = -t1;
    462       1.1   pooka 					t1 -= (NFS_SDRTT(rep) >> 2);
    463       1.1   pooka 					NFS_SDRTT(rep) += t1;
    464       1.1   pooka 				}
    465       1.1   pooka 				nmp->nm_timeouts = 0;
    466       1.1   pooka 				break;
    467       1.1   pooka 			}
    468       1.1   pooka 		}
    469  1.1.40.2  martin 		splx(s);
    470       1.1   pooka 		nfs_rcvunlock(nmp);
    471       1.1   pooka 		/*
    472       1.1   pooka 		 * If not matched to a request, drop it.
    473       1.1   pooka 		 * If it's mine, get out.
    474       1.1   pooka 		 */
    475       1.1   pooka 		if (rep == 0) {
    476       1.1   pooka 			nfsstats.rpcunexpected++;
    477       1.1   pooka 			m_freem(mrep);
    478       1.1   pooka 		} else if (rep == myrep) {
    479       1.1   pooka 			if (rep->r_mrep == NULL)
    480       1.1   pooka 				panic("nfsreply nil");
    481       1.1   pooka 			return (0);
    482       1.1   pooka 		}
    483       1.1   pooka 	}
    484       1.1   pooka }
    485       1.1   pooka 
    486       1.1   pooka /*
    487       1.1   pooka  * nfs_request - goes something like this
    488       1.1   pooka  *	- fill in request struct
    489       1.1   pooka  *	- links it into list
    490       1.1   pooka  *	- calls nfs_send() for first transmit
    491       1.1   pooka  *	- calls nfs_receive() to get reply
    492       1.1   pooka  *	- break down rpc header and return with nfs reply pointed to
    493       1.1   pooka  *	  by mrep or error
    494       1.1   pooka  * nb: always frees up mreq mbuf list
    495       1.1   pooka  */
    496       1.1   pooka int
    497       1.1   pooka nfs_request(struct nfsnode *np, struct mbuf *mrest, int procnum, struct lwp *lwp, kauth_cred_t cred, struct mbuf **mrp, struct mbuf **mdp, char **dposp, int *rexmitp)
    498       1.1   pooka {
    499       1.1   pooka 	struct mbuf *m, *mrep;
    500       1.1   pooka 	struct nfsreq *rep;
    501       1.1   pooka 	u_int32_t *tl;
    502       1.1   pooka 	int i;
    503       1.1   pooka 	struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount);
    504       1.1   pooka 	struct mbuf *md, *mheadend;
    505       1.1   pooka 	char nickv[RPCX_NICKVERF];
    506       1.1   pooka 	time_t waituntil;
    507       1.1   pooka 	char *dpos, *cp2;
    508       1.1   pooka 	int t1, s, error = 0, mrest_len, auth_len, auth_type;
    509       1.1   pooka 	int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
    510       1.1   pooka 	int verf_len, verf_type;
    511       1.1   pooka 	u_int32_t xid;
    512       1.1   pooka 	char *auth_str, *verf_str;
    513       1.1   pooka 	NFSKERBKEY_T key;		/* save session key */
    514       1.1   pooka 	kauth_cred_t acred;
    515       1.1   pooka 	struct mbuf *mrest_backup = NULL;
    516       1.1   pooka 	kauth_cred_t origcred = NULL; /* XXX: gcc */
    517       1.1   pooka 	bool retry_cred = true;
    518       1.1   pooka 	bool use_opencred = (np->n_flag & NUSEOPENCRED) != 0;
    519       1.1   pooka 
    520       1.1   pooka 	if (rexmitp != NULL)
    521       1.1   pooka 		*rexmitp = 0;
    522       1.1   pooka 
    523       1.1   pooka 	acred = kauth_cred_alloc();
    524       1.1   pooka 
    525       1.1   pooka tryagain_cred:
    526       1.1   pooka 	KASSERT(cred != NULL);
    527       1.1   pooka 	rep = kmem_alloc(sizeof(*rep), KM_SLEEP);
    528       1.1   pooka 	rep->r_nmp = nmp;
    529       1.1   pooka 	KASSERT(lwp == NULL || lwp == curlwp);
    530       1.1   pooka 	rep->r_lwp = lwp;
    531       1.1   pooka 	rep->r_procnum = procnum;
    532       1.1   pooka 	i = 0;
    533       1.1   pooka 	m = mrest;
    534       1.1   pooka 	while (m) {
    535       1.1   pooka 		i += m->m_len;
    536       1.1   pooka 		m = m->m_next;
    537       1.1   pooka 	}
    538       1.1   pooka 	mrest_len = i;
    539       1.1   pooka 
    540       1.1   pooka 	/*
    541       1.1   pooka 	 * Get the RPC header with authorization.
    542       1.1   pooka 	 */
    543       1.1   pooka kerbauth:
    544       1.1   pooka 	verf_str = auth_str = NULL;
    545       1.1   pooka 	if (nmp->nm_flag & NFSMNT_KERB) {
    546       1.1   pooka 		verf_str = nickv;
    547       1.1   pooka 		verf_len = sizeof (nickv);
    548       1.1   pooka 		auth_type = RPCAUTH_KERB4;
    549       1.1   pooka 		memset((void *)key, 0, sizeof (key));
    550       1.1   pooka 		if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
    551       1.1   pooka 			&auth_len, verf_str, verf_len)) {
    552       1.1   pooka 			error = nfs_getauth(nmp, rep, cred, &auth_str,
    553       1.1   pooka 				&auth_len, verf_str, &verf_len, key);
    554       1.1   pooka 			if (error) {
    555       1.1   pooka 				kmem_free(rep, sizeof(*rep));
    556       1.1   pooka 				m_freem(mrest);
    557       1.1   pooka 				KASSERT(kauth_cred_getrefcnt(acred) == 1);
    558       1.1   pooka 				kauth_cred_free(acred);
    559       1.1   pooka 				return (error);
    560       1.1   pooka 			}
    561       1.1   pooka 		}
    562       1.1   pooka 		retry_cred = false;
    563       1.1   pooka 	} else {
    564       1.1   pooka 		/* AUTH_UNIX */
    565       1.1   pooka 		uid_t uid;
    566       1.1   pooka 		gid_t gid;
    567       1.1   pooka 
    568       1.1   pooka 		/*
    569       1.1   pooka 		 * on the most unix filesystems, permission checks are
    570       1.1   pooka 		 * done when the file is open(2)'ed.
    571       1.1   pooka 		 * ie. once a file is successfully open'ed,
    572       1.1   pooka 		 * following i/o operations never fail with EACCES.
    573       1.1   pooka 		 * we try to follow the semantics as far as possible.
    574       1.1   pooka 		 *
    575       1.1   pooka 		 * note that we expect that the nfs server always grant
    576       1.1   pooka 		 * accesses by the file's owner.
    577       1.1   pooka 		 */
    578       1.1   pooka 		origcred = cred;
    579       1.1   pooka 		switch (procnum) {
    580       1.1   pooka 		case NFSPROC_READ:
    581       1.1   pooka 		case NFSPROC_WRITE:
    582       1.1   pooka 		case NFSPROC_COMMIT:
    583       1.1   pooka 			uid = np->n_vattr->va_uid;
    584       1.1   pooka 			gid = np->n_vattr->va_gid;
    585       1.1   pooka 			if (kauth_cred_geteuid(cred) == uid &&
    586       1.1   pooka 			    kauth_cred_getegid(cred) == gid) {
    587       1.1   pooka 				retry_cred = false;
    588       1.1   pooka 				break;
    589       1.1   pooka 			}
    590       1.1   pooka 			if (use_opencred)
    591       1.1   pooka 				break;
    592       1.1   pooka 			kauth_cred_setuid(acred, uid);
    593       1.1   pooka 			kauth_cred_seteuid(acred, uid);
    594       1.1   pooka 			kauth_cred_setsvuid(acred, uid);
    595       1.1   pooka 			kauth_cred_setgid(acred, gid);
    596       1.1   pooka 			kauth_cred_setegid(acred, gid);
    597       1.1   pooka 			kauth_cred_setsvgid(acred, gid);
    598       1.1   pooka 			cred = acred;
    599       1.1   pooka 			break;
    600       1.1   pooka 		default:
    601       1.1   pooka 			retry_cred = false;
    602       1.1   pooka 			break;
    603       1.1   pooka 		}
    604       1.1   pooka 		/*
    605       1.1   pooka 		 * backup mbuf chain if we can need it later to retry.
    606       1.1   pooka 		 *
    607       1.1   pooka 		 * XXX maybe we can keep a direct reference to
    608       1.1   pooka 		 * mrest without doing m_copym, but it's ...ugly.
    609       1.1   pooka 		 */
    610       1.1   pooka 		if (retry_cred)
    611       1.1   pooka 			mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT);
    612       1.1   pooka 		auth_type = RPCAUTH_UNIX;
    613       1.1   pooka 		/* XXX elad - ngroups */
    614       1.1   pooka 		auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ?
    615       1.1   pooka 			nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) +
    616       1.1   pooka 			5 * NFSX_UNSIGNED;
    617       1.1   pooka 	}
    618       1.1   pooka 	m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
    619       1.1   pooka 	     auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
    620       1.1   pooka 	if (auth_str)
    621       1.1   pooka 		free(auth_str, M_TEMP);
    622       1.1   pooka 
    623       1.1   pooka 	/*
    624       1.1   pooka 	 * For stream protocols, insert a Sun RPC Record Mark.
    625       1.1   pooka 	 */
    626       1.1   pooka 	if (nmp->nm_sotype == SOCK_STREAM) {
    627       1.1   pooka 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    628       1.1   pooka 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
    629       1.1   pooka 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
    630       1.1   pooka 	}
    631       1.1   pooka 	rep->r_mreq = m;
    632       1.1   pooka 	rep->r_xid = xid;
    633       1.1   pooka tryagain:
    634       1.1   pooka 	if (nmp->nm_flag & NFSMNT_SOFT)
    635       1.1   pooka 		rep->r_retry = nmp->nm_retry;
    636       1.1   pooka 	else
    637       1.1   pooka 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
    638       1.1   pooka 	rep->r_rtt = rep->r_rexmit = 0;
    639       1.1   pooka 	if (nfs_proct[procnum] > 0)
    640       1.1   pooka 		rep->r_flags = R_TIMING;
    641       1.1   pooka 	else
    642       1.1   pooka 		rep->r_flags = 0;
    643       1.1   pooka 	rep->r_mrep = NULL;
    644       1.1   pooka 
    645       1.1   pooka 	/*
    646       1.1   pooka 	 * Do the client side RPC.
    647       1.1   pooka 	 */
    648       1.1   pooka 	nfsstats.rpcrequests++;
    649       1.1   pooka 	/*
    650       1.1   pooka 	 * Chain request into list of outstanding requests. Be sure
    651       1.1   pooka 	 * to put it LAST so timer finds oldest requests first.
    652       1.1   pooka 	 */
    653       1.1   pooka 	s = splsoftnet();
    654       1.1   pooka 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
    655       1.1   pooka 	nfs_timer_start();
    656       1.1   pooka 
    657       1.1   pooka 	/*
    658       1.1   pooka 	 * If backing off another request or avoiding congestion, don't
    659       1.1   pooka 	 * send this one now but let timer do it. If not timing a request,
    660       1.1   pooka 	 * do it now.
    661       1.1   pooka 	 */
    662       1.1   pooka 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
    663       1.1   pooka 	    (nmp->nm_flag & NFSMNT_DUMBTIMR) || nmp->nm_sent < nmp->nm_cwnd)) {
    664       1.1   pooka 		splx(s);
    665       1.1   pooka 		if (nmp->nm_soflags & PR_CONNREQUIRED)
    666       1.1   pooka 			error = nfs_sndlock(nmp, rep);
    667       1.1   pooka 		if (!error) {
    668       1.1   pooka 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
    669       1.1   pooka 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
    670       1.1   pooka 			if (nmp->nm_soflags & PR_CONNREQUIRED)
    671       1.1   pooka 				nfs_sndunlock(nmp);
    672       1.1   pooka 		}
    673       1.1   pooka 		s = splsoftnet();
    674       1.1   pooka 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
    675       1.1   pooka 			if ((rep->r_flags & R_SENT) == 0) {
    676       1.1   pooka 				nmp->nm_sent += NFS_CWNDSCALE;
    677       1.1   pooka 				rep->r_flags |= R_SENT;
    678       1.1   pooka 			}
    679       1.1   pooka 		}
    680       1.1   pooka 		splx(s);
    681       1.1   pooka 	} else {
    682       1.1   pooka 		splx(s);
    683       1.1   pooka 		rep->r_rtt = -1;
    684       1.1   pooka 	}
    685       1.1   pooka 
    686       1.1   pooka 	/*
    687       1.1   pooka 	 * Wait for the reply from our send or the timer's.
    688       1.1   pooka 	 */
    689       1.1   pooka 	if (!error || error == EPIPE || error == EWOULDBLOCK)
    690       1.1   pooka 		error = nfs_reply(rep, lwp);
    691       1.1   pooka 
    692       1.1   pooka 	/*
    693       1.1   pooka 	 * RPC done, unlink the request.
    694       1.1   pooka 	 */
    695       1.1   pooka 	s = splsoftnet();
    696       1.1   pooka 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
    697       1.1   pooka 
    698       1.1   pooka 	/*
    699       1.1   pooka 	 * Decrement the outstanding request count.
    700       1.1   pooka 	 */
    701       1.1   pooka 	if (rep->r_flags & R_SENT) {
    702       1.1   pooka 		rep->r_flags &= ~R_SENT;	/* paranoia */
    703       1.1   pooka 		nmp->nm_sent -= NFS_CWNDSCALE;
    704       1.1   pooka 	}
    705       1.1   pooka 	splx(s);
    706       1.1   pooka 
    707       1.1   pooka 	if (rexmitp != NULL) {
    708       1.1   pooka 		int rexmit;
    709       1.1   pooka 
    710       1.1   pooka 		if (nmp->nm_sotype != SOCK_DGRAM)
    711       1.1   pooka 			rexmit = (rep->r_flags & R_REXMITTED) != 0;
    712       1.1   pooka 		else
    713       1.1   pooka 			rexmit = rep->r_rexmit;
    714       1.1   pooka 		*rexmitp = rexmit;
    715       1.1   pooka 	}
    716       1.1   pooka 
    717       1.1   pooka 	/*
    718       1.1   pooka 	 * If there was a successful reply and a tprintf msg.
    719       1.1   pooka 	 * tprintf a response.
    720       1.1   pooka 	 */
    721       1.1   pooka 	if (!error && (rep->r_flags & R_TPRINTFMSG))
    722       1.1   pooka 		nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
    723       1.1   pooka 		    "is alive again");
    724       1.1   pooka 	mrep = rep->r_mrep;
    725       1.1   pooka 	md = rep->r_md;
    726       1.1   pooka 	dpos = rep->r_dpos;
    727       1.1   pooka 	if (error)
    728       1.1   pooka 		goto nfsmout;
    729       1.1   pooka 
    730       1.1   pooka 	/*
    731       1.1   pooka 	 * break down the rpc header and check if ok
    732       1.1   pooka 	 */
    733       1.1   pooka 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    734       1.1   pooka 	if (*tl++ == rpc_msgdenied) {
    735       1.1   pooka 		if (*tl == rpc_mismatch)
    736       1.1   pooka 			error = EOPNOTSUPP;
    737       1.1   pooka 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
    738       1.1   pooka 			if (!failed_auth) {
    739       1.1   pooka 				failed_auth++;
    740       1.1   pooka 				mheadend->m_next = NULL;
    741       1.1   pooka 				m_freem(mrep);
    742       1.1   pooka 				m_freem(rep->r_mreq);
    743       1.1   pooka 				goto kerbauth;
    744       1.1   pooka 			} else
    745       1.1   pooka 				error = EAUTH;
    746       1.1   pooka 		} else
    747       1.1   pooka 			error = EACCES;
    748       1.1   pooka 		m_freem(mrep);
    749       1.1   pooka 		goto nfsmout;
    750       1.1   pooka 	}
    751       1.1   pooka 
    752       1.1   pooka 	/*
    753       1.1   pooka 	 * Grab any Kerberos verifier, otherwise just throw it away.
    754       1.1   pooka 	 */
    755       1.1   pooka 	verf_type = fxdr_unsigned(int, *tl++);
    756       1.1   pooka 	i = fxdr_unsigned(int32_t, *tl);
    757       1.1   pooka 	if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
    758       1.1   pooka 		error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
    759       1.1   pooka 		if (error)
    760       1.1   pooka 			goto nfsmout;
    761       1.1   pooka 	} else if (i > 0)
    762       1.1   pooka 		nfsm_adv(nfsm_rndup(i));
    763       1.1   pooka 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    764       1.1   pooka 	/* 0 == ok */
    765       1.1   pooka 	if (*tl == 0) {
    766       1.1   pooka 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    767       1.1   pooka 		if (*tl != 0) {
    768       1.1   pooka 			error = fxdr_unsigned(int, *tl);
    769       1.1   pooka 			switch (error) {
    770       1.1   pooka 			case NFSERR_PERM:
    771       1.1   pooka 				error = EPERM;
    772       1.1   pooka 				break;
    773       1.1   pooka 
    774       1.1   pooka 			case NFSERR_NOENT:
    775       1.1   pooka 				error = ENOENT;
    776       1.1   pooka 				break;
    777       1.1   pooka 
    778       1.1   pooka 			case NFSERR_IO:
    779       1.1   pooka 				error = EIO;
    780       1.1   pooka 				break;
    781       1.1   pooka 
    782       1.1   pooka 			case NFSERR_NXIO:
    783       1.1   pooka 				error = ENXIO;
    784       1.1   pooka 				break;
    785       1.1   pooka 
    786       1.1   pooka 			case NFSERR_ACCES:
    787       1.1   pooka 				error = EACCES;
    788       1.1   pooka 				if (!retry_cred)
    789       1.1   pooka 					break;
    790       1.1   pooka 				m_freem(mrep);
    791       1.1   pooka 				m_freem(rep->r_mreq);
    792       1.1   pooka 				kmem_free(rep, sizeof(*rep));
    793       1.1   pooka 				use_opencred = !use_opencred;
    794       1.1   pooka 				if (mrest_backup == NULL) {
    795       1.1   pooka 					/* m_copym failure */
    796       1.1   pooka 					KASSERT(
    797       1.1   pooka 					    kauth_cred_getrefcnt(acred) == 1);
    798       1.1   pooka 					kauth_cred_free(acred);
    799       1.1   pooka 					return ENOMEM;
    800       1.1   pooka 				}
    801       1.1   pooka 				mrest = mrest_backup;
    802       1.1   pooka 				mrest_backup = NULL;
    803       1.1   pooka 				cred = origcred;
    804       1.1   pooka 				error = 0;
    805       1.1   pooka 				retry_cred = false;
    806       1.1   pooka 				goto tryagain_cred;
    807       1.1   pooka 
    808       1.1   pooka 			case NFSERR_EXIST:
    809       1.1   pooka 				error = EEXIST;
    810       1.1   pooka 				break;
    811       1.1   pooka 
    812       1.1   pooka 			case NFSERR_XDEV:
    813       1.1   pooka 				error = EXDEV;
    814       1.1   pooka 				break;
    815       1.1   pooka 
    816       1.1   pooka 			case NFSERR_NODEV:
    817       1.1   pooka 				error = ENODEV;
    818       1.1   pooka 				break;
    819       1.1   pooka 
    820       1.1   pooka 			case NFSERR_NOTDIR:
    821       1.1   pooka 				error = ENOTDIR;
    822       1.1   pooka 				break;
    823       1.1   pooka 
    824       1.1   pooka 			case NFSERR_ISDIR:
    825       1.1   pooka 				error = EISDIR;
    826       1.1   pooka 				break;
    827       1.1   pooka 
    828       1.1   pooka 			case NFSERR_INVAL:
    829       1.1   pooka 				error = EINVAL;
    830       1.1   pooka 				break;
    831       1.1   pooka 
    832       1.1   pooka 			case NFSERR_FBIG:
    833       1.1   pooka 				error = EFBIG;
    834       1.1   pooka 				break;
    835       1.1   pooka 
    836       1.1   pooka 			case NFSERR_NOSPC:
    837       1.1   pooka 				error = ENOSPC;
    838       1.1   pooka 				break;
    839       1.1   pooka 
    840       1.1   pooka 			case NFSERR_ROFS:
    841       1.1   pooka 				error = EROFS;
    842       1.1   pooka 				break;
    843       1.1   pooka 
    844       1.1   pooka 			case NFSERR_MLINK:
    845       1.1   pooka 				error = EMLINK;
    846       1.1   pooka 				break;
    847       1.1   pooka 
    848       1.1   pooka 			case NFSERR_TIMEDOUT:
    849       1.1   pooka 				error = ETIMEDOUT;
    850       1.1   pooka 				break;
    851       1.1   pooka 
    852       1.1   pooka 			case NFSERR_NAMETOL:
    853       1.1   pooka 				error = ENAMETOOLONG;
    854       1.1   pooka 				break;
    855       1.1   pooka 
    856       1.1   pooka 			case NFSERR_NOTEMPTY:
    857       1.1   pooka 				error = ENOTEMPTY;
    858       1.1   pooka 				break;
    859       1.1   pooka 
    860       1.1   pooka 			case NFSERR_DQUOT:
    861       1.1   pooka 				error = EDQUOT;
    862       1.1   pooka 				break;
    863       1.1   pooka 
    864       1.1   pooka 			case NFSERR_STALE:
    865       1.1   pooka 				/*
    866       1.1   pooka 				 * If the File Handle was stale, invalidate the
    867       1.1   pooka 				 * lookup cache, just in case.
    868       1.1   pooka 				 */
    869       1.1   pooka 				error = ESTALE;
    870       1.1   pooka 				cache_purge(NFSTOV(np));
    871       1.1   pooka 				break;
    872       1.1   pooka 
    873       1.1   pooka 			case NFSERR_REMOTE:
    874       1.1   pooka 				error = EREMOTE;
    875       1.1   pooka 				break;
    876       1.1   pooka 
    877       1.1   pooka 			case NFSERR_WFLUSH:
    878       1.1   pooka 			case NFSERR_BADHANDLE:
    879       1.1   pooka 			case NFSERR_NOT_SYNC:
    880       1.1   pooka 			case NFSERR_BAD_COOKIE:
    881       1.1   pooka 				error = EINVAL;
    882       1.1   pooka 				break;
    883       1.1   pooka 
    884       1.1   pooka 			case NFSERR_NOTSUPP:
    885       1.1   pooka 				error = ENOTSUP;
    886       1.1   pooka 				break;
    887       1.1   pooka 
    888       1.1   pooka 			case NFSERR_TOOSMALL:
    889       1.1   pooka 			case NFSERR_SERVERFAULT:
    890       1.1   pooka 			case NFSERR_BADTYPE:
    891       1.1   pooka 				error = EINVAL;
    892       1.1   pooka 				break;
    893       1.1   pooka 
    894       1.1   pooka 			case NFSERR_TRYLATER:
    895       1.1   pooka 				if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
    896       1.1   pooka 					break;
    897       1.1   pooka 				m_freem(mrep);
    898       1.1   pooka 				error = 0;
    899       1.1   pooka 				waituntil = time_second + trylater_delay;
    900       1.1   pooka 				while (time_second < waituntil) {
    901       1.1   pooka 					kpause("nfstrylater", false, hz, NULL);
    902       1.1   pooka 				}
    903       1.1   pooka 				trylater_delay *= NFS_TRYLATERDELMUL;
    904       1.1   pooka 				if (trylater_delay > NFS_TRYLATERDELMAX)
    905       1.1   pooka 					trylater_delay = NFS_TRYLATERDELMAX;
    906       1.1   pooka 				/*
    907       1.1   pooka 				 * RFC1813:
    908       1.1   pooka 				 * The client should wait and then try
    909       1.1   pooka 				 * the request with a new RPC transaction ID.
    910       1.1   pooka 				 */
    911       1.1   pooka 				nfs_renewxid(rep);
    912       1.1   pooka 				goto tryagain;
    913       1.1   pooka 
    914       1.1   pooka 			default:
    915       1.1   pooka #ifdef DIAGNOSTIC
    916       1.1   pooka 				printf("Invalid rpc error code %d\n", error);
    917       1.1   pooka #endif
    918       1.1   pooka 				error = EINVAL;
    919       1.1   pooka 				break;
    920       1.1   pooka 			}
    921       1.1   pooka 
    922       1.1   pooka 			if (nmp->nm_flag & NFSMNT_NFSV3) {
    923       1.1   pooka 				*mrp = mrep;
    924       1.1   pooka 				*mdp = md;
    925       1.1   pooka 				*dposp = dpos;
    926       1.1   pooka 				error |= NFSERR_RETERR;
    927       1.1   pooka 			} else
    928       1.1   pooka 				m_freem(mrep);
    929       1.1   pooka 			goto nfsmout;
    930       1.1   pooka 		}
    931       1.1   pooka 
    932       1.1   pooka 		/*
    933       1.1   pooka 		 * note which credential worked to minimize number of retries.
    934       1.1   pooka 		 */
    935       1.1   pooka 		if (use_opencred)
    936       1.1   pooka 			np->n_flag |= NUSEOPENCRED;
    937       1.1   pooka 		else
    938       1.1   pooka 			np->n_flag &= ~NUSEOPENCRED;
    939       1.1   pooka 
    940       1.1   pooka 		*mrp = mrep;
    941       1.1   pooka 		*mdp = md;
    942       1.1   pooka 		*dposp = dpos;
    943       1.1   pooka 
    944       1.1   pooka 		KASSERT(error == 0);
    945       1.1   pooka 		goto nfsmout;
    946       1.1   pooka 	}
    947       1.1   pooka 	m_freem(mrep);
    948       1.1   pooka 	error = EPROTONOSUPPORT;
    949       1.1   pooka nfsmout:
    950       1.1   pooka 	KASSERT(kauth_cred_getrefcnt(acred) == 1);
    951       1.1   pooka 	kauth_cred_free(acred);
    952       1.1   pooka 	m_freem(rep->r_mreq);
    953       1.1   pooka 	kmem_free(rep, sizeof(*rep));
    954       1.1   pooka 	m_freem(mrest_backup);
    955       1.1   pooka 	return (error);
    956       1.1   pooka }
    957       1.1   pooka 
    958       1.1   pooka /*
    959       1.1   pooka  * Lock a socket against others.
    960       1.1   pooka  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
    961       1.1   pooka  * and also to avoid race conditions between the processes with nfs requests
    962       1.1   pooka  * in progress when a reconnect is necessary.
    963       1.1   pooka  */
    964       1.1   pooka static int
    965       1.1   pooka nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
    966       1.1   pooka {
    967       1.1   pooka 	struct lwp *l;
    968       1.1   pooka 	int timeo = 0;
    969       1.1   pooka 	bool catch = false;
    970       1.1   pooka 	int error = 0;
    971       1.1   pooka 
    972  1.1.40.1     riz 	if (nmp->nm_flag & NFSMNT_SOFT)
    973  1.1.40.1     riz 		timeo = nmp->nm_retry * nmp->nm_timeo;
    974  1.1.40.1     riz 
    975  1.1.40.1     riz 	if (nmp->nm_iflag & NFSMNT_DISMNTFORCE)
    976  1.1.40.1     riz 		timeo = hz;
    977  1.1.40.1     riz 
    978       1.1   pooka 	if (rep) {
    979       1.1   pooka 		l = rep->r_lwp;
    980       1.1   pooka 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
    981       1.1   pooka 			catch = true;
    982       1.1   pooka 	} else
    983       1.1   pooka 		l = NULL;
    984       1.1   pooka 	mutex_enter(&nmp->nm_lock);
    985       1.1   pooka 	while ((nmp->nm_iflag & NFSMNT_SNDLOCK) != 0) {
    986       1.1   pooka 		if (rep && nfs_sigintr(rep->r_nmp, rep, l)) {
    987       1.1   pooka 			error = EINTR;
    988       1.1   pooka 			goto quit;
    989       1.1   pooka 		}
    990       1.1   pooka 		if (catch) {
    991  1.1.40.1     riz 			error = cv_timedwait_sig(&nmp->nm_sndcv,
    992  1.1.40.1     riz 						 &nmp->nm_lock, timeo);
    993       1.1   pooka 		} else {
    994  1.1.40.1     riz 			error = cv_timedwait(&nmp->nm_sndcv,
    995  1.1.40.1     riz 					     &nmp->nm_lock, timeo);
    996  1.1.40.1     riz 		}
    997  1.1.40.1     riz 
    998  1.1.40.1     riz 		if (error) {
    999  1.1.40.1     riz 			if ((error == EWOULDBLOCK) &&
   1000  1.1.40.1     riz 			    (nmp->nm_flag & NFSMNT_SOFT)) {
   1001  1.1.40.1     riz 				error = EIO;
   1002  1.1.40.1     riz 				goto quit;
   1003  1.1.40.1     riz 			}
   1004  1.1.40.1     riz 			error = 0;
   1005       1.1   pooka 		}
   1006       1.1   pooka 		if (catch) {
   1007       1.1   pooka 			catch = false;
   1008       1.1   pooka 			timeo = 2 * hz;
   1009       1.1   pooka 		}
   1010       1.1   pooka 	}
   1011       1.1   pooka 	nmp->nm_iflag |= NFSMNT_SNDLOCK;
   1012       1.1   pooka quit:
   1013       1.1   pooka 	mutex_exit(&nmp->nm_lock);
   1014       1.1   pooka 	return error;
   1015       1.1   pooka }
   1016       1.1   pooka 
   1017       1.1   pooka /*
   1018       1.1   pooka  * Unlock the stream socket for others.
   1019       1.1   pooka  */
   1020       1.1   pooka static void
   1021       1.1   pooka nfs_sndunlock(struct nfsmount *nmp)
   1022       1.1   pooka {
   1023       1.1   pooka 
   1024       1.1   pooka 	mutex_enter(&nmp->nm_lock);
   1025       1.1   pooka 	if ((nmp->nm_iflag & NFSMNT_SNDLOCK) == 0)
   1026       1.1   pooka 		panic("nfs sndunlock");
   1027       1.1   pooka 	nmp->nm_iflag &= ~NFSMNT_SNDLOCK;
   1028       1.1   pooka 	cv_signal(&nmp->nm_sndcv);
   1029       1.1   pooka 	mutex_exit(&nmp->nm_lock);
   1030       1.1   pooka }
   1031