Home | History | Annotate | Line # | Download | only in nfs
nfs_clntsocket.c revision 1.1
      1  1.1  pooka /*	$NetBSD: nfs_clntsocket.c,v 1.1 2010/03/02 23:19:09 pooka 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  pooka __KERNEL_RCSID(0, "$NetBSD: nfs_clntsocket.c,v 1.1 2010/03/02 23:19:09 pooka 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  pooka 	int error;
    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  pooka 		TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
    406  1.1  pooka 			if (rep->r_mrep == NULL && rxid == rep->r_xid) {
    407  1.1  pooka 				/* Found it.. */
    408  1.1  pooka 				rep->r_mrep = mrep;
    409  1.1  pooka 				rep->r_md = md;
    410  1.1  pooka 				rep->r_dpos = dpos;
    411  1.1  pooka 				if (nfsrtton) {
    412  1.1  pooka 					struct rttl *rt;
    413  1.1  pooka 
    414  1.1  pooka 					rt = &nfsrtt.rttl[nfsrtt.pos];
    415  1.1  pooka 					rt->proc = rep->r_procnum;
    416  1.1  pooka 					rt->rto = NFS_RTO(nmp, nfs_proct[rep->r_procnum]);
    417  1.1  pooka 					rt->sent = nmp->nm_sent;
    418  1.1  pooka 					rt->cwnd = nmp->nm_cwnd;
    419  1.1  pooka 					rt->srtt = nmp->nm_srtt[nfs_proct[rep->r_procnum] - 1];
    420  1.1  pooka 					rt->sdrtt = nmp->nm_sdrtt[nfs_proct[rep->r_procnum] - 1];
    421  1.1  pooka 					rt->fsid = nmp->nm_mountp->mnt_stat.f_fsidx;
    422  1.1  pooka 					getmicrotime(&rt->tstamp);
    423  1.1  pooka 					if (rep->r_flags & R_TIMING)
    424  1.1  pooka 						rt->rtt = rep->r_rtt;
    425  1.1  pooka 					else
    426  1.1  pooka 						rt->rtt = 1000000;
    427  1.1  pooka 					nfsrtt.pos = (nfsrtt.pos + 1) % NFSRTTLOGSIZ;
    428  1.1  pooka 				}
    429  1.1  pooka 				/*
    430  1.1  pooka 				 * Update congestion window.
    431  1.1  pooka 				 * Do the additive increase of
    432  1.1  pooka 				 * one rpc/rtt.
    433  1.1  pooka 				 */
    434  1.1  pooka 				if (nmp->nm_cwnd <= nmp->nm_sent) {
    435  1.1  pooka 					nmp->nm_cwnd +=
    436  1.1  pooka 					   (NFS_CWNDSCALE * NFS_CWNDSCALE +
    437  1.1  pooka 					   (nmp->nm_cwnd >> 1)) / nmp->nm_cwnd;
    438  1.1  pooka 					if (nmp->nm_cwnd > NFS_MAXCWND)
    439  1.1  pooka 						nmp->nm_cwnd = NFS_MAXCWND;
    440  1.1  pooka 				}
    441  1.1  pooka 				rep->r_flags &= ~R_SENT;
    442  1.1  pooka 				nmp->nm_sent -= NFS_CWNDSCALE;
    443  1.1  pooka 				/*
    444  1.1  pooka 				 * Update rtt using a gain of 0.125 on the mean
    445  1.1  pooka 				 * and a gain of 0.25 on the deviation.
    446  1.1  pooka 				 */
    447  1.1  pooka 				if (rep->r_flags & R_TIMING) {
    448  1.1  pooka 					/*
    449  1.1  pooka 					 * Since the timer resolution of
    450  1.1  pooka 					 * NFS_HZ is so course, it can often
    451  1.1  pooka 					 * result in r_rtt == 0. Since
    452  1.1  pooka 					 * r_rtt == N means that the actual
    453  1.1  pooka 					 * rtt is between N+dt and N+2-dt ticks,
    454  1.1  pooka 					 * add 1.
    455  1.1  pooka 					 */
    456  1.1  pooka 					t1 = rep->r_rtt + 1;
    457  1.1  pooka 					t1 -= (NFS_SRTT(rep) >> 3);
    458  1.1  pooka 					NFS_SRTT(rep) += t1;
    459  1.1  pooka 					if (t1 < 0)
    460  1.1  pooka 						t1 = -t1;
    461  1.1  pooka 					t1 -= (NFS_SDRTT(rep) >> 2);
    462  1.1  pooka 					NFS_SDRTT(rep) += t1;
    463  1.1  pooka 				}
    464  1.1  pooka 				nmp->nm_timeouts = 0;
    465  1.1  pooka 				break;
    466  1.1  pooka 			}
    467  1.1  pooka 		}
    468  1.1  pooka 		nfs_rcvunlock(nmp);
    469  1.1  pooka 		/*
    470  1.1  pooka 		 * If not matched to a request, drop it.
    471  1.1  pooka 		 * If it's mine, get out.
    472  1.1  pooka 		 */
    473  1.1  pooka 		if (rep == 0) {
    474  1.1  pooka 			nfsstats.rpcunexpected++;
    475  1.1  pooka 			m_freem(mrep);
    476  1.1  pooka 		} else if (rep == myrep) {
    477  1.1  pooka 			if (rep->r_mrep == NULL)
    478  1.1  pooka 				panic("nfsreply nil");
    479  1.1  pooka 			return (0);
    480  1.1  pooka 		}
    481  1.1  pooka 	}
    482  1.1  pooka }
    483  1.1  pooka 
    484  1.1  pooka /*
    485  1.1  pooka  * nfs_request - goes something like this
    486  1.1  pooka  *	- fill in request struct
    487  1.1  pooka  *	- links it into list
    488  1.1  pooka  *	- calls nfs_send() for first transmit
    489  1.1  pooka  *	- calls nfs_receive() to get reply
    490  1.1  pooka  *	- break down rpc header and return with nfs reply pointed to
    491  1.1  pooka  *	  by mrep or error
    492  1.1  pooka  * nb: always frees up mreq mbuf list
    493  1.1  pooka  */
    494  1.1  pooka int
    495  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)
    496  1.1  pooka {
    497  1.1  pooka 	struct mbuf *m, *mrep;
    498  1.1  pooka 	struct nfsreq *rep;
    499  1.1  pooka 	u_int32_t *tl;
    500  1.1  pooka 	int i;
    501  1.1  pooka 	struct nfsmount *nmp = VFSTONFS(np->n_vnode->v_mount);
    502  1.1  pooka 	struct mbuf *md, *mheadend;
    503  1.1  pooka 	char nickv[RPCX_NICKVERF];
    504  1.1  pooka 	time_t waituntil;
    505  1.1  pooka 	char *dpos, *cp2;
    506  1.1  pooka 	int t1, s, error = 0, mrest_len, auth_len, auth_type;
    507  1.1  pooka 	int trylater_delay = NFS_TRYLATERDEL, failed_auth = 0;
    508  1.1  pooka 	int verf_len, verf_type;
    509  1.1  pooka 	u_int32_t xid;
    510  1.1  pooka 	char *auth_str, *verf_str;
    511  1.1  pooka 	NFSKERBKEY_T key;		/* save session key */
    512  1.1  pooka 	kauth_cred_t acred;
    513  1.1  pooka 	struct mbuf *mrest_backup = NULL;
    514  1.1  pooka 	kauth_cred_t origcred = NULL; /* XXX: gcc */
    515  1.1  pooka 	bool retry_cred = true;
    516  1.1  pooka 	bool use_opencred = (np->n_flag & NUSEOPENCRED) != 0;
    517  1.1  pooka 
    518  1.1  pooka 	if (rexmitp != NULL)
    519  1.1  pooka 		*rexmitp = 0;
    520  1.1  pooka 
    521  1.1  pooka 	acred = kauth_cred_alloc();
    522  1.1  pooka 
    523  1.1  pooka tryagain_cred:
    524  1.1  pooka 	KASSERT(cred != NULL);
    525  1.1  pooka 	rep = kmem_alloc(sizeof(*rep), KM_SLEEP);
    526  1.1  pooka 	rep->r_nmp = nmp;
    527  1.1  pooka 	KASSERT(lwp == NULL || lwp == curlwp);
    528  1.1  pooka 	rep->r_lwp = lwp;
    529  1.1  pooka 	rep->r_procnum = procnum;
    530  1.1  pooka 	i = 0;
    531  1.1  pooka 	m = mrest;
    532  1.1  pooka 	while (m) {
    533  1.1  pooka 		i += m->m_len;
    534  1.1  pooka 		m = m->m_next;
    535  1.1  pooka 	}
    536  1.1  pooka 	mrest_len = i;
    537  1.1  pooka 
    538  1.1  pooka 	/*
    539  1.1  pooka 	 * Get the RPC header with authorization.
    540  1.1  pooka 	 */
    541  1.1  pooka kerbauth:
    542  1.1  pooka 	verf_str = auth_str = NULL;
    543  1.1  pooka 	if (nmp->nm_flag & NFSMNT_KERB) {
    544  1.1  pooka 		verf_str = nickv;
    545  1.1  pooka 		verf_len = sizeof (nickv);
    546  1.1  pooka 		auth_type = RPCAUTH_KERB4;
    547  1.1  pooka 		memset((void *)key, 0, sizeof (key));
    548  1.1  pooka 		if (failed_auth || nfs_getnickauth(nmp, cred, &auth_str,
    549  1.1  pooka 			&auth_len, verf_str, verf_len)) {
    550  1.1  pooka 			error = nfs_getauth(nmp, rep, cred, &auth_str,
    551  1.1  pooka 				&auth_len, verf_str, &verf_len, key);
    552  1.1  pooka 			if (error) {
    553  1.1  pooka 				kmem_free(rep, sizeof(*rep));
    554  1.1  pooka 				m_freem(mrest);
    555  1.1  pooka 				KASSERT(kauth_cred_getrefcnt(acred) == 1);
    556  1.1  pooka 				kauth_cred_free(acred);
    557  1.1  pooka 				return (error);
    558  1.1  pooka 			}
    559  1.1  pooka 		}
    560  1.1  pooka 		retry_cred = false;
    561  1.1  pooka 	} else {
    562  1.1  pooka 		/* AUTH_UNIX */
    563  1.1  pooka 		uid_t uid;
    564  1.1  pooka 		gid_t gid;
    565  1.1  pooka 
    566  1.1  pooka 		/*
    567  1.1  pooka 		 * on the most unix filesystems, permission checks are
    568  1.1  pooka 		 * done when the file is open(2)'ed.
    569  1.1  pooka 		 * ie. once a file is successfully open'ed,
    570  1.1  pooka 		 * following i/o operations never fail with EACCES.
    571  1.1  pooka 		 * we try to follow the semantics as far as possible.
    572  1.1  pooka 		 *
    573  1.1  pooka 		 * note that we expect that the nfs server always grant
    574  1.1  pooka 		 * accesses by the file's owner.
    575  1.1  pooka 		 */
    576  1.1  pooka 		origcred = cred;
    577  1.1  pooka 		switch (procnum) {
    578  1.1  pooka 		case NFSPROC_READ:
    579  1.1  pooka 		case NFSPROC_WRITE:
    580  1.1  pooka 		case NFSPROC_COMMIT:
    581  1.1  pooka 			uid = np->n_vattr->va_uid;
    582  1.1  pooka 			gid = np->n_vattr->va_gid;
    583  1.1  pooka 			if (kauth_cred_geteuid(cred) == uid &&
    584  1.1  pooka 			    kauth_cred_getegid(cred) == gid) {
    585  1.1  pooka 				retry_cred = false;
    586  1.1  pooka 				break;
    587  1.1  pooka 			}
    588  1.1  pooka 			if (use_opencred)
    589  1.1  pooka 				break;
    590  1.1  pooka 			kauth_cred_setuid(acred, uid);
    591  1.1  pooka 			kauth_cred_seteuid(acred, uid);
    592  1.1  pooka 			kauth_cred_setsvuid(acred, uid);
    593  1.1  pooka 			kauth_cred_setgid(acred, gid);
    594  1.1  pooka 			kauth_cred_setegid(acred, gid);
    595  1.1  pooka 			kauth_cred_setsvgid(acred, gid);
    596  1.1  pooka 			cred = acred;
    597  1.1  pooka 			break;
    598  1.1  pooka 		default:
    599  1.1  pooka 			retry_cred = false;
    600  1.1  pooka 			break;
    601  1.1  pooka 		}
    602  1.1  pooka 		/*
    603  1.1  pooka 		 * backup mbuf chain if we can need it later to retry.
    604  1.1  pooka 		 *
    605  1.1  pooka 		 * XXX maybe we can keep a direct reference to
    606  1.1  pooka 		 * mrest without doing m_copym, but it's ...ugly.
    607  1.1  pooka 		 */
    608  1.1  pooka 		if (retry_cred)
    609  1.1  pooka 			mrest_backup = m_copym(mrest, 0, M_COPYALL, M_WAIT);
    610  1.1  pooka 		auth_type = RPCAUTH_UNIX;
    611  1.1  pooka 		/* XXX elad - ngroups */
    612  1.1  pooka 		auth_len = (((kauth_cred_ngroups(cred) > nmp->nm_numgrps) ?
    613  1.1  pooka 			nmp->nm_numgrps : kauth_cred_ngroups(cred)) << 2) +
    614  1.1  pooka 			5 * NFSX_UNSIGNED;
    615  1.1  pooka 	}
    616  1.1  pooka 	m = nfsm_rpchead(cred, nmp->nm_flag, procnum, auth_type, auth_len,
    617  1.1  pooka 	     auth_str, verf_len, verf_str, mrest, mrest_len, &mheadend, &xid);
    618  1.1  pooka 	if (auth_str)
    619  1.1  pooka 		free(auth_str, M_TEMP);
    620  1.1  pooka 
    621  1.1  pooka 	/*
    622  1.1  pooka 	 * For stream protocols, insert a Sun RPC Record Mark.
    623  1.1  pooka 	 */
    624  1.1  pooka 	if (nmp->nm_sotype == SOCK_STREAM) {
    625  1.1  pooka 		M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    626  1.1  pooka 		*mtod(m, u_int32_t *) = htonl(0x80000000 |
    627  1.1  pooka 			 (m->m_pkthdr.len - NFSX_UNSIGNED));
    628  1.1  pooka 	}
    629  1.1  pooka 	rep->r_mreq = m;
    630  1.1  pooka 	rep->r_xid = xid;
    631  1.1  pooka tryagain:
    632  1.1  pooka 	if (nmp->nm_flag & NFSMNT_SOFT)
    633  1.1  pooka 		rep->r_retry = nmp->nm_retry;
    634  1.1  pooka 	else
    635  1.1  pooka 		rep->r_retry = NFS_MAXREXMIT + 1;	/* past clip limit */
    636  1.1  pooka 	rep->r_rtt = rep->r_rexmit = 0;
    637  1.1  pooka 	if (nfs_proct[procnum] > 0)
    638  1.1  pooka 		rep->r_flags = R_TIMING;
    639  1.1  pooka 	else
    640  1.1  pooka 		rep->r_flags = 0;
    641  1.1  pooka 	rep->r_mrep = NULL;
    642  1.1  pooka 
    643  1.1  pooka 	/*
    644  1.1  pooka 	 * Do the client side RPC.
    645  1.1  pooka 	 */
    646  1.1  pooka 	nfsstats.rpcrequests++;
    647  1.1  pooka 	/*
    648  1.1  pooka 	 * Chain request into list of outstanding requests. Be sure
    649  1.1  pooka 	 * to put it LAST so timer finds oldest requests first.
    650  1.1  pooka 	 */
    651  1.1  pooka 	s = splsoftnet();
    652  1.1  pooka 	TAILQ_INSERT_TAIL(&nfs_reqq, rep, r_chain);
    653  1.1  pooka 	nfs_timer_start();
    654  1.1  pooka 
    655  1.1  pooka 	/*
    656  1.1  pooka 	 * If backing off another request or avoiding congestion, don't
    657  1.1  pooka 	 * send this one now but let timer do it. If not timing a request,
    658  1.1  pooka 	 * do it now.
    659  1.1  pooka 	 */
    660  1.1  pooka 	if (nmp->nm_so && (nmp->nm_sotype != SOCK_DGRAM ||
    661  1.1  pooka 	    (nmp->nm_flag & NFSMNT_DUMBTIMR) || nmp->nm_sent < nmp->nm_cwnd)) {
    662  1.1  pooka 		splx(s);
    663  1.1  pooka 		if (nmp->nm_soflags & PR_CONNREQUIRED)
    664  1.1  pooka 			error = nfs_sndlock(nmp, rep);
    665  1.1  pooka 		if (!error) {
    666  1.1  pooka 			m = m_copym(rep->r_mreq, 0, M_COPYALL, M_WAIT);
    667  1.1  pooka 			error = nfs_send(nmp->nm_so, nmp->nm_nam, m, rep, lwp);
    668  1.1  pooka 			if (nmp->nm_soflags & PR_CONNREQUIRED)
    669  1.1  pooka 				nfs_sndunlock(nmp);
    670  1.1  pooka 		}
    671  1.1  pooka 		s = splsoftnet();
    672  1.1  pooka 		if (!error && (rep->r_flags & R_MUSTRESEND) == 0) {
    673  1.1  pooka 			if ((rep->r_flags & R_SENT) == 0) {
    674  1.1  pooka 				nmp->nm_sent += NFS_CWNDSCALE;
    675  1.1  pooka 				rep->r_flags |= R_SENT;
    676  1.1  pooka 			}
    677  1.1  pooka 		}
    678  1.1  pooka 		splx(s);
    679  1.1  pooka 	} else {
    680  1.1  pooka 		splx(s);
    681  1.1  pooka 		rep->r_rtt = -1;
    682  1.1  pooka 	}
    683  1.1  pooka 
    684  1.1  pooka 	/*
    685  1.1  pooka 	 * Wait for the reply from our send or the timer's.
    686  1.1  pooka 	 */
    687  1.1  pooka 	if (!error || error == EPIPE || error == EWOULDBLOCK)
    688  1.1  pooka 		error = nfs_reply(rep, lwp);
    689  1.1  pooka 
    690  1.1  pooka 	/*
    691  1.1  pooka 	 * RPC done, unlink the request.
    692  1.1  pooka 	 */
    693  1.1  pooka 	s = splsoftnet();
    694  1.1  pooka 	TAILQ_REMOVE(&nfs_reqq, rep, r_chain);
    695  1.1  pooka 
    696  1.1  pooka 	/*
    697  1.1  pooka 	 * Decrement the outstanding request count.
    698  1.1  pooka 	 */
    699  1.1  pooka 	if (rep->r_flags & R_SENT) {
    700  1.1  pooka 		rep->r_flags &= ~R_SENT;	/* paranoia */
    701  1.1  pooka 		nmp->nm_sent -= NFS_CWNDSCALE;
    702  1.1  pooka 	}
    703  1.1  pooka 	splx(s);
    704  1.1  pooka 
    705  1.1  pooka 	if (rexmitp != NULL) {
    706  1.1  pooka 		int rexmit;
    707  1.1  pooka 
    708  1.1  pooka 		if (nmp->nm_sotype != SOCK_DGRAM)
    709  1.1  pooka 			rexmit = (rep->r_flags & R_REXMITTED) != 0;
    710  1.1  pooka 		else
    711  1.1  pooka 			rexmit = rep->r_rexmit;
    712  1.1  pooka 		*rexmitp = rexmit;
    713  1.1  pooka 	}
    714  1.1  pooka 
    715  1.1  pooka 	/*
    716  1.1  pooka 	 * If there was a successful reply and a tprintf msg.
    717  1.1  pooka 	 * tprintf a response.
    718  1.1  pooka 	 */
    719  1.1  pooka 	if (!error && (rep->r_flags & R_TPRINTFMSG))
    720  1.1  pooka 		nfs_msg(rep->r_lwp, nmp->nm_mountp->mnt_stat.f_mntfromname,
    721  1.1  pooka 		    "is alive again");
    722  1.1  pooka 	mrep = rep->r_mrep;
    723  1.1  pooka 	md = rep->r_md;
    724  1.1  pooka 	dpos = rep->r_dpos;
    725  1.1  pooka 	if (error)
    726  1.1  pooka 		goto nfsmout;
    727  1.1  pooka 
    728  1.1  pooka 	/*
    729  1.1  pooka 	 * break down the rpc header and check if ok
    730  1.1  pooka 	 */
    731  1.1  pooka 	nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    732  1.1  pooka 	if (*tl++ == rpc_msgdenied) {
    733  1.1  pooka 		if (*tl == rpc_mismatch)
    734  1.1  pooka 			error = EOPNOTSUPP;
    735  1.1  pooka 		else if ((nmp->nm_flag & NFSMNT_KERB) && *tl++ == rpc_autherr) {
    736  1.1  pooka 			if (!failed_auth) {
    737  1.1  pooka 				failed_auth++;
    738  1.1  pooka 				mheadend->m_next = NULL;
    739  1.1  pooka 				m_freem(mrep);
    740  1.1  pooka 				m_freem(rep->r_mreq);
    741  1.1  pooka 				goto kerbauth;
    742  1.1  pooka 			} else
    743  1.1  pooka 				error = EAUTH;
    744  1.1  pooka 		} else
    745  1.1  pooka 			error = EACCES;
    746  1.1  pooka 		m_freem(mrep);
    747  1.1  pooka 		goto nfsmout;
    748  1.1  pooka 	}
    749  1.1  pooka 
    750  1.1  pooka 	/*
    751  1.1  pooka 	 * Grab any Kerberos verifier, otherwise just throw it away.
    752  1.1  pooka 	 */
    753  1.1  pooka 	verf_type = fxdr_unsigned(int, *tl++);
    754  1.1  pooka 	i = fxdr_unsigned(int32_t, *tl);
    755  1.1  pooka 	if ((nmp->nm_flag & NFSMNT_KERB) && verf_type == RPCAUTH_KERB4) {
    756  1.1  pooka 		error = nfs_savenickauth(nmp, cred, i, key, &md, &dpos, mrep);
    757  1.1  pooka 		if (error)
    758  1.1  pooka 			goto nfsmout;
    759  1.1  pooka 	} else if (i > 0)
    760  1.1  pooka 		nfsm_adv(nfsm_rndup(i));
    761  1.1  pooka 	nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    762  1.1  pooka 	/* 0 == ok */
    763  1.1  pooka 	if (*tl == 0) {
    764  1.1  pooka 		nfsm_dissect(tl, u_int32_t *, NFSX_UNSIGNED);
    765  1.1  pooka 		if (*tl != 0) {
    766  1.1  pooka 			error = fxdr_unsigned(int, *tl);
    767  1.1  pooka 			switch (error) {
    768  1.1  pooka 			case NFSERR_PERM:
    769  1.1  pooka 				error = EPERM;
    770  1.1  pooka 				break;
    771  1.1  pooka 
    772  1.1  pooka 			case NFSERR_NOENT:
    773  1.1  pooka 				error = ENOENT;
    774  1.1  pooka 				break;
    775  1.1  pooka 
    776  1.1  pooka 			case NFSERR_IO:
    777  1.1  pooka 				error = EIO;
    778  1.1  pooka 				break;
    779  1.1  pooka 
    780  1.1  pooka 			case NFSERR_NXIO:
    781  1.1  pooka 				error = ENXIO;
    782  1.1  pooka 				break;
    783  1.1  pooka 
    784  1.1  pooka 			case NFSERR_ACCES:
    785  1.1  pooka 				error = EACCES;
    786  1.1  pooka 				if (!retry_cred)
    787  1.1  pooka 					break;
    788  1.1  pooka 				m_freem(mrep);
    789  1.1  pooka 				m_freem(rep->r_mreq);
    790  1.1  pooka 				kmem_free(rep, sizeof(*rep));
    791  1.1  pooka 				use_opencred = !use_opencred;
    792  1.1  pooka 				if (mrest_backup == NULL) {
    793  1.1  pooka 					/* m_copym failure */
    794  1.1  pooka 					KASSERT(
    795  1.1  pooka 					    kauth_cred_getrefcnt(acred) == 1);
    796  1.1  pooka 					kauth_cred_free(acred);
    797  1.1  pooka 					return ENOMEM;
    798  1.1  pooka 				}
    799  1.1  pooka 				mrest = mrest_backup;
    800  1.1  pooka 				mrest_backup = NULL;
    801  1.1  pooka 				cred = origcred;
    802  1.1  pooka 				error = 0;
    803  1.1  pooka 				retry_cred = false;
    804  1.1  pooka 				goto tryagain_cred;
    805  1.1  pooka 
    806  1.1  pooka 			case NFSERR_EXIST:
    807  1.1  pooka 				error = EEXIST;
    808  1.1  pooka 				break;
    809  1.1  pooka 
    810  1.1  pooka 			case NFSERR_XDEV:
    811  1.1  pooka 				error = EXDEV;
    812  1.1  pooka 				break;
    813  1.1  pooka 
    814  1.1  pooka 			case NFSERR_NODEV:
    815  1.1  pooka 				error = ENODEV;
    816  1.1  pooka 				break;
    817  1.1  pooka 
    818  1.1  pooka 			case NFSERR_NOTDIR:
    819  1.1  pooka 				error = ENOTDIR;
    820  1.1  pooka 				break;
    821  1.1  pooka 
    822  1.1  pooka 			case NFSERR_ISDIR:
    823  1.1  pooka 				error = EISDIR;
    824  1.1  pooka 				break;
    825  1.1  pooka 
    826  1.1  pooka 			case NFSERR_INVAL:
    827  1.1  pooka 				error = EINVAL;
    828  1.1  pooka 				break;
    829  1.1  pooka 
    830  1.1  pooka 			case NFSERR_FBIG:
    831  1.1  pooka 				error = EFBIG;
    832  1.1  pooka 				break;
    833  1.1  pooka 
    834  1.1  pooka 			case NFSERR_NOSPC:
    835  1.1  pooka 				error = ENOSPC;
    836  1.1  pooka 				break;
    837  1.1  pooka 
    838  1.1  pooka 			case NFSERR_ROFS:
    839  1.1  pooka 				error = EROFS;
    840  1.1  pooka 				break;
    841  1.1  pooka 
    842  1.1  pooka 			case NFSERR_MLINK:
    843  1.1  pooka 				error = EMLINK;
    844  1.1  pooka 				break;
    845  1.1  pooka 
    846  1.1  pooka 			case NFSERR_TIMEDOUT:
    847  1.1  pooka 				error = ETIMEDOUT;
    848  1.1  pooka 				break;
    849  1.1  pooka 
    850  1.1  pooka 			case NFSERR_NAMETOL:
    851  1.1  pooka 				error = ENAMETOOLONG;
    852  1.1  pooka 				break;
    853  1.1  pooka 
    854  1.1  pooka 			case NFSERR_NOTEMPTY:
    855  1.1  pooka 				error = ENOTEMPTY;
    856  1.1  pooka 				break;
    857  1.1  pooka 
    858  1.1  pooka 			case NFSERR_DQUOT:
    859  1.1  pooka 				error = EDQUOT;
    860  1.1  pooka 				break;
    861  1.1  pooka 
    862  1.1  pooka 			case NFSERR_STALE:
    863  1.1  pooka 				/*
    864  1.1  pooka 				 * If the File Handle was stale, invalidate the
    865  1.1  pooka 				 * lookup cache, just in case.
    866  1.1  pooka 				 */
    867  1.1  pooka 				error = ESTALE;
    868  1.1  pooka 				cache_purge(NFSTOV(np));
    869  1.1  pooka 				break;
    870  1.1  pooka 
    871  1.1  pooka 			case NFSERR_REMOTE:
    872  1.1  pooka 				error = EREMOTE;
    873  1.1  pooka 				break;
    874  1.1  pooka 
    875  1.1  pooka 			case NFSERR_WFLUSH:
    876  1.1  pooka 			case NFSERR_BADHANDLE:
    877  1.1  pooka 			case NFSERR_NOT_SYNC:
    878  1.1  pooka 			case NFSERR_BAD_COOKIE:
    879  1.1  pooka 				error = EINVAL;
    880  1.1  pooka 				break;
    881  1.1  pooka 
    882  1.1  pooka 			case NFSERR_NOTSUPP:
    883  1.1  pooka 				error = ENOTSUP;
    884  1.1  pooka 				break;
    885  1.1  pooka 
    886  1.1  pooka 			case NFSERR_TOOSMALL:
    887  1.1  pooka 			case NFSERR_SERVERFAULT:
    888  1.1  pooka 			case NFSERR_BADTYPE:
    889  1.1  pooka 				error = EINVAL;
    890  1.1  pooka 				break;
    891  1.1  pooka 
    892  1.1  pooka 			case NFSERR_TRYLATER:
    893  1.1  pooka 				if ((nmp->nm_flag & NFSMNT_NFSV3) == 0)
    894  1.1  pooka 					break;
    895  1.1  pooka 				m_freem(mrep);
    896  1.1  pooka 				error = 0;
    897  1.1  pooka 				waituntil = time_second + trylater_delay;
    898  1.1  pooka 				while (time_second < waituntil) {
    899  1.1  pooka 					kpause("nfstrylater", false, hz, NULL);
    900  1.1  pooka 				}
    901  1.1  pooka 				trylater_delay *= NFS_TRYLATERDELMUL;
    902  1.1  pooka 				if (trylater_delay > NFS_TRYLATERDELMAX)
    903  1.1  pooka 					trylater_delay = NFS_TRYLATERDELMAX;
    904  1.1  pooka 				/*
    905  1.1  pooka 				 * RFC1813:
    906  1.1  pooka 				 * The client should wait and then try
    907  1.1  pooka 				 * the request with a new RPC transaction ID.
    908  1.1  pooka 				 */
    909  1.1  pooka 				nfs_renewxid(rep);
    910  1.1  pooka 				goto tryagain;
    911  1.1  pooka 
    912  1.1  pooka 			default:
    913  1.1  pooka #ifdef DIAGNOSTIC
    914  1.1  pooka 				printf("Invalid rpc error code %d\n", error);
    915  1.1  pooka #endif
    916  1.1  pooka 				error = EINVAL;
    917  1.1  pooka 				break;
    918  1.1  pooka 			}
    919  1.1  pooka 
    920  1.1  pooka 			if (nmp->nm_flag & NFSMNT_NFSV3) {
    921  1.1  pooka 				*mrp = mrep;
    922  1.1  pooka 				*mdp = md;
    923  1.1  pooka 				*dposp = dpos;
    924  1.1  pooka 				error |= NFSERR_RETERR;
    925  1.1  pooka 			} else
    926  1.1  pooka 				m_freem(mrep);
    927  1.1  pooka 			goto nfsmout;
    928  1.1  pooka 		}
    929  1.1  pooka 
    930  1.1  pooka 		/*
    931  1.1  pooka 		 * note which credential worked to minimize number of retries.
    932  1.1  pooka 		 */
    933  1.1  pooka 		if (use_opencred)
    934  1.1  pooka 			np->n_flag |= NUSEOPENCRED;
    935  1.1  pooka 		else
    936  1.1  pooka 			np->n_flag &= ~NUSEOPENCRED;
    937  1.1  pooka 
    938  1.1  pooka 		*mrp = mrep;
    939  1.1  pooka 		*mdp = md;
    940  1.1  pooka 		*dposp = dpos;
    941  1.1  pooka 
    942  1.1  pooka 		KASSERT(error == 0);
    943  1.1  pooka 		goto nfsmout;
    944  1.1  pooka 	}
    945  1.1  pooka 	m_freem(mrep);
    946  1.1  pooka 	error = EPROTONOSUPPORT;
    947  1.1  pooka nfsmout:
    948  1.1  pooka 	KASSERT(kauth_cred_getrefcnt(acred) == 1);
    949  1.1  pooka 	kauth_cred_free(acred);
    950  1.1  pooka 	m_freem(rep->r_mreq);
    951  1.1  pooka 	kmem_free(rep, sizeof(*rep));
    952  1.1  pooka 	m_freem(mrest_backup);
    953  1.1  pooka 	return (error);
    954  1.1  pooka }
    955  1.1  pooka 
    956  1.1  pooka /*
    957  1.1  pooka  * Lock a socket against others.
    958  1.1  pooka  * Necessary for STREAM sockets to ensure you get an entire rpc request/reply
    959  1.1  pooka  * and also to avoid race conditions between the processes with nfs requests
    960  1.1  pooka  * in progress when a reconnect is necessary.
    961  1.1  pooka  */
    962  1.1  pooka static int
    963  1.1  pooka nfs_sndlock(struct nfsmount *nmp, struct nfsreq *rep)
    964  1.1  pooka {
    965  1.1  pooka 	struct lwp *l;
    966  1.1  pooka 	int timeo = 0;
    967  1.1  pooka 	bool catch = false;
    968  1.1  pooka 	int error = 0;
    969  1.1  pooka 
    970  1.1  pooka 	if (rep) {
    971  1.1  pooka 		l = rep->r_lwp;
    972  1.1  pooka 		if (rep->r_nmp->nm_flag & NFSMNT_INT)
    973  1.1  pooka 			catch = true;
    974  1.1  pooka 	} else
    975  1.1  pooka 		l = NULL;
    976  1.1  pooka 	mutex_enter(&nmp->nm_lock);
    977  1.1  pooka 	while ((nmp->nm_iflag & NFSMNT_SNDLOCK) != 0) {
    978  1.1  pooka 		if (rep && nfs_sigintr(rep->r_nmp, rep, l)) {
    979  1.1  pooka 			error = EINTR;
    980  1.1  pooka 			goto quit;
    981  1.1  pooka 		}
    982  1.1  pooka 		if (catch) {
    983  1.1  pooka 			cv_timedwait_sig(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
    984  1.1  pooka 		} else {
    985  1.1  pooka 			cv_timedwait(&nmp->nm_sndcv, &nmp->nm_lock, timeo);
    986  1.1  pooka 		}
    987  1.1  pooka 		if (catch) {
    988  1.1  pooka 			catch = false;
    989  1.1  pooka 			timeo = 2 * hz;
    990  1.1  pooka 		}
    991  1.1  pooka 	}
    992  1.1  pooka 	nmp->nm_iflag |= NFSMNT_SNDLOCK;
    993  1.1  pooka quit:
    994  1.1  pooka 	mutex_exit(&nmp->nm_lock);
    995  1.1  pooka 	return error;
    996  1.1  pooka }
    997  1.1  pooka 
    998  1.1  pooka /*
    999  1.1  pooka  * Unlock the stream socket for others.
   1000  1.1  pooka  */
   1001  1.1  pooka static void
   1002  1.1  pooka nfs_sndunlock(struct nfsmount *nmp)
   1003  1.1  pooka {
   1004  1.1  pooka 
   1005  1.1  pooka 	mutex_enter(&nmp->nm_lock);
   1006  1.1  pooka 	if ((nmp->nm_iflag & NFSMNT_SNDLOCK) == 0)
   1007  1.1  pooka 		panic("nfs sndunlock");
   1008  1.1  pooka 	nmp->nm_iflag &= ~NFSMNT_SNDLOCK;
   1009  1.1  pooka 	cv_signal(&nmp->nm_sndcv);
   1010  1.1  pooka 	mutex_exit(&nmp->nm_lock);
   1011  1.1  pooka }
   1012