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