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