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