Home | History | Annotate | Line # | Download | only in nfs
nfs_socket.c revision 1.170.2.7
      1  1.170.2.7      yamt /*	$NetBSD: nfs_socket.c,v 1.170.2.7 2010/03/11 15:04:31 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.7      yamt __KERNEL_RCSID(0, "$NetBSD: nfs_socket.c,v 1.170.2.7 2010/03/11 15:04:31 yamt Exp $");
     43       1.42   thorpej 
     44  1.170.2.2      yamt #ifdef _KERNEL_OPT
     45       1.59     bjh21 #include "opt_nfs.h"
     46       1.89    martin #include "opt_mbuftrace.h"
     47  1.170.2.2      yamt #endif
     48        1.1       cgd 
     49        1.9   mycroft #include <sys/param.h>
     50        1.9   mycroft #include <sys/systm.h>
     51      1.161      yamt #include <sys/evcnt.h>
     52       1.54   thorpej #include <sys/callout.h>
     53        1.9   mycroft #include <sys/proc.h>
     54        1.9   mycroft #include <sys/mount.h>
     55        1.9   mycroft #include <sys/kernel.h>
     56      1.167      yamt #include <sys/kmem.h>
     57        1.9   mycroft #include <sys/mbuf.h>
     58        1.9   mycroft #include <sys/vnode.h>
     59        1.9   mycroft #include <sys/domain.h>
     60        1.9   mycroft #include <sys/protosw.h>
     61        1.9   mycroft #include <sys/socket.h>
     62        1.9   mycroft #include <sys/socketvar.h>
     63        1.9   mycroft #include <sys/syslog.h>
     64        1.9   mycroft #include <sys/tprintf.h>
     65       1.23  christos #include <sys/namei.h>
     66       1.47   mycroft #include <sys/signal.h>
     67       1.47   mycroft #include <sys/signalvar.h>
     68      1.130      elad #include <sys/kauth.h>
     69        1.1       cgd 
     70        1.9   mycroft #include <netinet/in.h>
     71        1.9   mycroft #include <netinet/tcp.h>
     72       1.24      fvdl 
     73        1.9   mycroft #include <nfs/rpcv2.h>
     74       1.24      fvdl #include <nfs/nfsproto.h>
     75        1.9   mycroft #include <nfs/nfs.h>
     76        1.9   mycroft #include <nfs/xdr_subs.h>
     77        1.9   mycroft #include <nfs/nfsm_subs.h>
     78        1.9   mycroft #include <nfs/nfsmount.h>
     79       1.14   mycroft #include <nfs/nfsnode.h>
     80       1.14   mycroft #include <nfs/nfsrtt.h>
     81       1.23  christos #include <nfs/nfs_var.h>
     82       1.78   thorpej 
     83       1.79      matt #ifdef MBUFTRACE
     84      1.139    dogcow struct mowner nfs_mowner = MOWNER_INIT("nfs","");
     85       1.79      matt #endif
     86        1.1       cgd 
     87        1.1       cgd /*
     88       1.14   mycroft  * Estimate rto for an nfs rpc sent via. an unreliable datagram.
     89       1.14   mycroft  * Use the mean and mean deviation of rtt for the appropriate type of rpc
     90       1.14   mycroft  * for the frequent rpcs and a default for the others.
     91       1.14   mycroft  * The justification for doing "other" this way is that these rpcs
     92       1.14   mycroft  * happen so infrequently that timer est. would probably be stale.
     93       1.14   mycroft  * Also, since many of these rpcs are
     94       1.14   mycroft  * non-idempotent, a conservative timeout is desired.
     95       1.14   mycroft  * getattr, lookup - A+2D
     96       1.14   mycroft  * read, write     - A+4D
     97       1.14   mycroft  * other           - nm_timeo
     98       1.14   mycroft  */
     99       1.14   mycroft #define	NFS_RTO(n, t) \
    100       1.14   mycroft 	((t) == 0 ? (n)->nm_timeo : \
    101       1.14   mycroft 	 ((t) < 3 ? \
    102       1.14   mycroft 	  (((((n)->nm_srtt[t-1] + 3) >> 2) + (n)->nm_sdrtt[t-1] + 1) >> 1) : \
    103       1.14   mycroft 	  ((((n)->nm_srtt[t-1] + 7) >> 3) + (n)->nm_sdrtt[t-1] + 1)))
    104  1.170.2.7      yamt #define	NFS_SRTT(r)	(r)->r_nmp->nm_srtt[nfs_proct[(r)->r_procnum] - 1]
    105  1.170.2.7      yamt #define	NFS_SDRTT(r)	(r)->r_nmp->nm_sdrtt[nfs_proct[(r)->r_procnum] - 1]
    106  1.170.2.2      yamt 
    107       1.14   mycroft /*
    108       1.14   mycroft  * Defines which timer to use for the procnum.
    109       1.14   mycroft  * 0 - default
    110       1.14   mycroft  * 1 - getattr
    111       1.14   mycroft  * 2 - lookup
    112       1.14   mycroft  * 3 - read
    113       1.14   mycroft  * 4 - write
    114       1.14   mycroft  */
    115  1.170.2.7      yamt const int nfs_proct[NFS_NPROCS] = {
    116      1.118      yamt 	[NFSPROC_NULL] = 0,
    117      1.118      yamt 	[NFSPROC_GETATTR] = 1,
    118      1.118      yamt 	[NFSPROC_SETATTR] = 0,
    119      1.118      yamt 	[NFSPROC_LOOKUP] = 2,
    120      1.118      yamt 	[NFSPROC_ACCESS] = 1,
    121      1.118      yamt 	[NFSPROC_READLINK] = 3,
    122      1.118      yamt 	[NFSPROC_READ] = 3,
    123      1.118      yamt 	[NFSPROC_WRITE] = 4,
    124      1.118      yamt 	[NFSPROC_CREATE] = 0,
    125      1.118      yamt 	[NFSPROC_MKDIR] = 0,
    126      1.118      yamt 	[NFSPROC_SYMLINK] = 0,
    127      1.118      yamt 	[NFSPROC_MKNOD] = 0,
    128      1.118      yamt 	[NFSPROC_REMOVE] = 0,
    129      1.118      yamt 	[NFSPROC_RMDIR] = 0,
    130      1.118      yamt 	[NFSPROC_RENAME] = 0,
    131      1.118      yamt 	[NFSPROC_LINK] = 0,
    132      1.118      yamt 	[NFSPROC_READDIR] = 3,
    133      1.118      yamt 	[NFSPROC_READDIRPLUS] = 3,
    134      1.118      yamt 	[NFSPROC_FSSTAT] = 0,
    135      1.118      yamt 	[NFSPROC_FSINFO] = 0,
    136      1.118      yamt 	[NFSPROC_PATHCONF] = 0,
    137      1.118      yamt 	[NFSPROC_COMMIT] = 0,
    138      1.118      yamt 	[NFSPROC_NOOP] = 0,
    139        1.1       cgd };
    140       1.14   mycroft 
    141  1.170.2.7      yamt #ifdef DEBUG
    142  1.170.2.7      yamt /*
    143  1.170.2.7      yamt  * Avoid spamming the console with debugging messages.  We only print
    144  1.170.2.7      yamt  * the nfs timer and reply error debugs every 10 seconds.
    145  1.170.2.7      yamt  */
    146  1.170.2.7      yamt const struct timeval nfs_err_interval = { 10, 0 };
    147  1.170.2.7      yamt struct timeval nfs_reply_last_err_time;
    148  1.170.2.7      yamt struct timeval nfs_timer_last_err_time;
    149  1.170.2.7      yamt #endif
    150  1.170.2.7      yamt 
    151       1.14   mycroft /*
    152       1.14   mycroft  * There is a congestion window for outstanding rpcs maintained per mount
    153       1.14   mycroft  * point. The cwnd size is adjusted in roughly the way that:
    154       1.14   mycroft  * Van Jacobson, Congestion avoidance and Control, In "Proceedings of
    155       1.14   mycroft  * SIGCOMM '88". ACM, August 1988.
    156       1.14   mycroft  * describes for TCP. The cwnd size is chopped in half on a retransmit timeout
    157       1.14   mycroft  * and incremented by 1/cwnd when each rpc reply is received and a full cwnd
    158       1.14   mycroft  * of rpcs is in progress.
    159       1.14   mycroft  * (The sent count and cwnd are scaled for integer arith.)
    160       1.14   mycroft  * Variants of "slow start" were tried and were found to be too much of a
    161       1.14   mycroft  * performance hit (ave. rtt 3 times larger),
    162       1.14   mycroft  * I suspect due to the large rtt that nfs rpcs have.
    163       1.14   mycroft  */
    164  1.170.2.7      yamt int nfsrtton = 0;
    165       1.14   mycroft struct nfsrtt nfsrtt;
    166  1.170.2.7      yamt static const int nfs_backoff[8] = { 2, 4, 8, 16, 32, 64, 128, 256, };
    167  1.170.2.1      yamt kmutex_t nfs_reqq_lock;
    168       1.74      matt struct nfsreqhead nfs_reqq;
    169      1.161      yamt static callout_t nfs_timer_ch;
    170      1.161      yamt static struct evcnt nfs_timer_ev;
    171      1.161      yamt static struct evcnt nfs_timer_start_ev;
    172      1.161      yamt static struct evcnt nfs_timer_stop_ev;
    173  1.170.2.2      yamt static kmutex_t nfs_timer_lock;
    174  1.170.2.2      yamt static bool (*nfs_timer_srvvec)(void);
    175       1.54   thorpej 
    176        1.1       cgd /*
    177        1.1       cgd  * Initialize sockets and congestion for a new NFS connection.
    178        1.1       cgd  * We do not free the sockaddr if error.
    179        1.1       cgd  */
    180       1.23  christos int
    181  1.170.2.2      yamt nfs_connect(struct nfsmount *nmp, struct nfsreq *rep, struct lwp *l)
    182        1.1       cgd {
    183       1.55  augustss 	struct socket *so;
    184      1.170        ad 	int error, rcvreserve, sndreserve;
    185       1.11       cgd 	struct sockaddr *saddr;
    186       1.14   mycroft 	struct sockaddr_in *sin;
    187       1.57      fvdl 	struct sockaddr_in6 *sin6;
    188        1.1       cgd 	struct mbuf *m;
    189  1.170.2.2      yamt 	int val;
    190        1.1       cgd 
    191  1.170.2.1      yamt 	KASSERT(rw_write_held(&nmp->nm_solock));
    192  1.170.2.1      yamt 	KASSERT(nmp->nm_so == NULL);
    193  1.170.2.1      yamt 
    194       1.11       cgd 	saddr = mtod(nmp->nm_nam, struct sockaddr *);
    195      1.105  jonathan 	error = socreate(saddr->sa_family, &nmp->nm_so,
    196      1.170        ad 		nmp->nm_sotype, nmp->nm_soproto, l, NULL);
    197       1.23  christos 	if (error)
    198        1.1       cgd 		goto bad;
    199        1.1       cgd 	so = nmp->nm_so;
    200       1.79      matt #ifdef MBUFTRACE
    201       1.79      matt 	so->so_mowner = &nfs_mowner;
    202       1.79      matt 	so->so_rcv.sb_mowner = &nfs_mowner;
    203       1.79      matt 	so->so_snd.sb_mowner = &nfs_mowner;
    204       1.79      matt #endif
    205        1.1       cgd 	nmp->nm_soflags = so->so_proto->pr_flags;
    206        1.1       cgd 
    207        1.2       cgd 	/*
    208        1.2       cgd 	 * Some servers require that the client port be a reserved port number.
    209        1.2       cgd 	 */
    210       1.14   mycroft 	if (saddr->sa_family == AF_INET && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    211  1.170.2.2      yamt 		val = IP_PORTRANGE_LOW;
    212  1.170.2.2      yamt 
    213  1.170.2.2      yamt 		if ((error = so_setsockopt(NULL, so, IPPROTO_IP, IP_PORTRANGE,
    214  1.170.2.2      yamt 		    &val, sizeof(val))))
    215       1.72     lukem 			goto bad;
    216       1.79      matt 		m = m_get(M_WAIT, MT_SONAME);
    217       1.79      matt 		MCLAIM(m, so->so_mowner);
    218        1.2       cgd 		sin = mtod(m, struct sockaddr_in *);
    219        1.2       cgd 		sin->sin_len = m->m_len = sizeof (struct sockaddr_in);
    220        1.2       cgd 		sin->sin_family = AF_INET;
    221        1.2       cgd 		sin->sin_addr.s_addr = INADDR_ANY;
    222       1.72     lukem 		sin->sin_port = 0;
    223      1.119  christos 		error = sobind(so, m, &lwp0);
    224        1.2       cgd 		m_freem(m);
    225       1.14   mycroft 		if (error)
    226       1.14   mycroft 			goto bad;
    227        1.2       cgd 	}
    228       1.57      fvdl 	if (saddr->sa_family == AF_INET6 && (nmp->nm_flag & NFSMNT_RESVPORT)) {
    229  1.170.2.2      yamt 		val = IPV6_PORTRANGE_LOW;
    230  1.170.2.2      yamt 
    231  1.170.2.2      yamt 		if ((error = so_setsockopt(NULL, so, IPPROTO_IPV6,
    232  1.170.2.2      yamt 		    IPV6_PORTRANGE, &val, sizeof(val))))
    233       1.72     lukem 			goto bad;
    234       1.79      matt 		m = m_get(M_WAIT, MT_SONAME);
    235       1.79      matt 		MCLAIM(m, so->so_mowner);
    236       1.57      fvdl 		sin6 = mtod(m, struct sockaddr_in6 *);
    237  1.170.2.2      yamt 		memset(sin6, 0, sizeof(*sin6));
    238       1.57      fvdl 		sin6->sin6_len = m->m_len = sizeof (struct sockaddr_in6);
    239       1.57      fvdl 		sin6->sin6_family = AF_INET6;
    240      1.119  christos 		error = sobind(so, m, &lwp0);
    241       1.57      fvdl 		m_freem(m);
    242       1.57      fvdl 		if (error)
    243       1.57      fvdl 			goto bad;
    244       1.57      fvdl 	}
    245        1.2       cgd 
    246        1.1       cgd 	/*
    247        1.1       cgd 	 * Protocols that do not require connections may be optionally left
    248        1.1       cgd 	 * unconnected for servers that reply from a port other than NFS_PORT.
    249        1.1       cgd 	 */
    250      1.170        ad 	solock(so);
    251        1.1       cgd 	if (nmp->nm_flag & NFSMNT_NOCONN) {
    252        1.1       cgd 		if (nmp->nm_soflags & PR_CONNREQUIRED) {
    253      1.170        ad 			sounlock(so);
    254        1.1       cgd 			error = ENOTCONN;
    255        1.1       cgd 			goto bad;
    256        1.1       cgd 		}
    257        1.1       cgd 	} else {
    258      1.119  christos 		error = soconnect(so, nmp->nm_nam, l);
    259      1.170        ad 		if (error) {
    260      1.170        ad 			sounlock(so);
    261        1.1       cgd 			goto bad;
    262      1.170        ad 		}
    263        1.1       cgd 
    264        1.1       cgd 		/*
    265        1.1       cgd 		 * Wait for the connection to complete. Cribbed from the
    266       1.14   mycroft 		 * connect system call but with the wait timing out so
    267       1.14   mycroft 		 * that interruptible mounts don't hang here for a long time.
    268        1.1       cgd 		 */
    269       1.14   mycroft 		while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    270  1.170.2.2      yamt 			(void)sowait(so, false, 2 * hz);
    271       1.14   mycroft 			if ((so->so_state & SS_ISCONNECTING) &&
    272       1.14   mycroft 			    so->so_error == 0 && rep &&
    273      1.119  christos 			    (error = nfs_sigintr(nmp, rep, rep->r_lwp)) != 0){
    274       1.14   mycroft 				so->so_state &= ~SS_ISCONNECTING;
    275      1.170        ad 				sounlock(so);
    276       1.14   mycroft 				goto bad;
    277       1.14   mycroft 			}
    278       1.14   mycroft 		}
    279        1.1       cgd 		if (so->so_error) {
    280        1.1       cgd 			error = so->so_error;
    281       1.14   mycroft 			so->so_error = 0;
    282      1.170        ad 			sounlock(so);
    283        1.1       cgd 			goto bad;
    284        1.1       cgd 		}
    285       1.14   mycroft 	}
    286       1.14   mycroft 	if (nmp->nm_flag & (NFSMNT_SOFT | NFSMNT_INT)) {
    287       1.14   mycroft 		so->so_rcv.sb_timeo = (5 * hz);
    288       1.14   mycroft 		so->so_snd.sb_timeo = (5 * hz);
    289       1.14   mycroft 	} else {
    290      1.106      yamt 		/*
    291      1.106      yamt 		 * enable receive timeout to detect server crash and reconnect.
    292      1.106      yamt 		 * otherwise, we can be stuck in soreceive forever.
    293      1.106      yamt 		 */
    294      1.106      yamt 		so->so_rcv.sb_timeo = (5 * hz);
    295       1.14   mycroft 		so->so_snd.sb_timeo = 0;
    296        1.1       cgd 	}
    297        1.1       cgd 	if (nmp->nm_sotype == SOCK_DGRAM) {
    298       1.37      fvdl 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    299       1.37      fvdl 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    300       1.37      fvdl 		    NFS_MAXPKTHDR) * 2;
    301       1.14   mycroft 	} else if (nmp->nm_sotype == SOCK_SEQPACKET) {
    302       1.26      fvdl 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR) * 2;
    303       1.26      fvdl 		rcvreserve = (max(nmp->nm_rsize, nmp->nm_readdirsize) +
    304       1.25      fvdl 		    NFS_MAXPKTHDR) * 2;
    305        1.1       cgd 	} else {
    306      1.170        ad 		sounlock(so);
    307       1.14   mycroft 		if (nmp->nm_sotype != SOCK_STREAM)
    308       1.14   mycroft 			panic("nfscon sotype");
    309        1.1       cgd 		if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
    310  1.170.2.2      yamt 			val = 1;
    311  1.170.2.2      yamt 			so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
    312  1.170.2.2      yamt 			    sizeof(val));
    313        1.1       cgd 		}
    314       1.14   mycroft 		if (so->so_proto->pr_protocol == IPPROTO_TCP) {
    315  1.170.2.2      yamt 			val = 1;
    316  1.170.2.2      yamt 			so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
    317  1.170.2.2      yamt 			    sizeof(val));
    318        1.1       cgd 		}
    319       1.22       cgd 		sndreserve = (nmp->nm_wsize + NFS_MAXPKTHDR +
    320       1.22       cgd 		    sizeof (u_int32_t)) * 2;
    321       1.22       cgd 		rcvreserve = (nmp->nm_rsize + NFS_MAXPKTHDR +
    322       1.22       cgd 		    sizeof (u_int32_t)) * 2;
    323      1.170        ad 		solock(so);
    324        1.1       cgd 	}
    325       1.24      fvdl 	error = soreserve(so, sndreserve, rcvreserve);
    326      1.170        ad 	if (error) {
    327      1.170        ad 		sounlock(so);
    328       1.14   mycroft 		goto bad;
    329      1.170        ad 	}
    330        1.1       cgd 	so->so_rcv.sb_flags |= SB_NOINTR;
    331        1.1       cgd 	so->so_snd.sb_flags |= SB_NOINTR;
    332      1.170        ad 	sounlock(so);
    333        1.1       cgd 
    334        1.1       cgd 	/* Initialize other non-zero congestion variables */
    335  1.170.2.1      yamt 	mutex_enter(&nfs_reqq_lock);
    336       1.14   mycroft 	nmp->nm_srtt[0] = nmp->nm_srtt[1] = nmp->nm_srtt[2] = nmp->nm_srtt[3] =
    337       1.68    simonb 		NFS_TIMEO << 3;
    338       1.14   mycroft 	nmp->nm_sdrtt[0] = nmp->nm_sdrtt[1] = nmp->nm_sdrtt[2] =
    339       1.68    simonb 		nmp->nm_sdrtt[3] = 0;
    340       1.14   mycroft 	nmp->nm_cwnd = NFS_MAXCWND / 2;	    /* Initial send window */
    341       1.14   mycroft 	nmp->nm_timeouts = 0;
    342  1.170.2.1      yamt 	mutex_exit(&nfs_reqq_lock);
    343        1.1       cgd 	return (0);
    344        1.1       cgd 
    345        1.1       cgd bad:
    346        1.1       cgd 	nfs_disconnect(nmp);
    347        1.1       cgd 	return (error);
    348        1.1       cgd }
    349        1.1       cgd 
    350        1.1       cgd /*
    351        1.1       cgd  * Reconnect routine:
    352        1.1       cgd  * Called when a connection is broken on a reliable protocol.
    353        1.1       cgd  * - clean up the old socket
    354        1.1       cgd  * - nfs_connect() again
    355        1.1       cgd  * - set R_MUSTRESEND for all outstanding requests on mount point
    356        1.1       cgd  * If this fails the mount point is DEAD!
    357        1.1       cgd  */
    358       1.23  christos int
    359      1.164      yamt nfs_reconnect(struct nfsreq *rep)
    360        1.1       cgd {
    361       1.55  augustss 	struct nfsmount *nmp = rep->r_nmp;
    362        1.1       cgd 	int error;
    363        1.1       cgd 
    364  1.170.2.1      yamt 	KASSERT(rep->r_nmp->nm_rcvlwp == curlwp);
    365  1.170.2.1      yamt 	if (!rw_tryupgrade(&nmp->nm_solock)) {
    366  1.170.2.1      yamt 		return EAGAIN;
    367  1.170.2.1      yamt 	}
    368       1.14   mycroft 	nfs_disconnect(nmp);
    369      1.164      yamt 	while ((error = nfs_connect(nmp, rep, &lwp0)) != 0) {
    370  1.170.2.1      yamt 		if (error == EINTR || error == ERESTART) {
    371  1.170.2.1      yamt 			rw_downgrade(&nmp->nm_solock);
    372  1.170.2.1      yamt 			return EINTR;
    373  1.170.2.1      yamt 		}
    374      1.163      yamt 		kpause("nfscn2", false, hz, NULL);
    375        1.1       cgd 	}
    376        1.1       cgd 
    377  1.170.2.1      yamt 	rw_downgrade(&nmp->nm_solock);
    378  1.170.2.1      yamt 	return 0;
    379        1.1       cgd }
    380        1.1       cgd 
    381        1.1       cgd /*
    382        1.1       cgd  * NFS disconnect. Clean up and unlink.
    383        1.1       cgd  */
    384        1.1       cgd void
    385  1.170.2.1      yamt nfs_disconnect(struct nfsmount *nmp)
    386        1.1       cgd {
    387  1.170.2.1      yamt 	struct nfsreq *rp;
    388       1.55  augustss 	struct socket *so;
    389       1.53  sommerfe 	int drain = 0;
    390      1.112     perry 
    391  1.170.2.1      yamt 	KASSERT(rw_write_held(&nmp->nm_solock));
    392        1.1       cgd 	if (nmp->nm_so) {
    393        1.1       cgd 		so = nmp->nm_so;
    394  1.170.2.1      yamt 		nmp->nm_so = NULL;
    395      1.170        ad 		solock(so);
    396      1.143      yamt 		soshutdown(so, SHUT_RDWR);
    397      1.170        ad 		sounlock(so);
    398       1.53  sommerfe 		drain = (nmp->nm_iflag & NFSMNT_DISMNT) != 0;
    399       1.53  sommerfe 		if (drain) {
    400       1.51  sommerfe 			/*
    401       1.51  sommerfe 			 * soshutdown() above should wake up the current
    402       1.51  sommerfe 			 * listener.
    403       1.71   minoura 			 * Now wake up those waiting for the receive lock, and
    404       1.51  sommerfe 			 * wait for them to go away unhappy, to prevent *nmp
    405       1.51  sommerfe 			 * from evaporating while they're sleeping.
    406       1.51  sommerfe 			 */
    407      1.151      yamt 			mutex_enter(&nmp->nm_lock);
    408       1.51  sommerfe 			while (nmp->nm_waiters > 0) {
    409      1.150      yamt 				cv_broadcast(&nmp->nm_rcvcv);
    410      1.150      yamt 				cv_broadcast(&nmp->nm_sndcv);
    411      1.151      yamt 				cv_wait(&nmp->nm_disconcv, &nmp->nm_lock);
    412       1.51  sommerfe 			}
    413      1.151      yamt 			mutex_exit(&nmp->nm_lock);
    414       1.51  sommerfe 		}
    415        1.1       cgd 		soclose(so);
    416       1.41      fvdl 	}
    417  1.170.2.1      yamt 
    418       1.51  sommerfe #ifdef DIAGNOSTIC
    419       1.53  sommerfe 	if (drain && (nmp->nm_waiters > 0))
    420       1.76    provos 		panic("nfs_disconnect: waiters left after drain?");
    421       1.51  sommerfe #endif
    422  1.170.2.1      yamt 
    423  1.170.2.1      yamt 	/*
    424  1.170.2.1      yamt 	 * Loop through outstanding request list and fix up all requests
    425  1.170.2.1      yamt 	 * on old socket.
    426  1.170.2.1      yamt 	 */
    427  1.170.2.1      yamt 	mutex_enter(&nfs_reqq_lock);
    428  1.170.2.1      yamt 	TAILQ_FOREACH(rp, &nfs_reqq, r_chain) {
    429  1.170.2.1      yamt 		if (rp->r_nmp == nmp) {
    430  1.170.2.1      yamt 			if ((rp->r_flags & R_MUSTRESEND) == 0)
    431  1.170.2.1      yamt 				rp->r_flags |= R_MUSTRESEND | R_REXMITTED;
    432  1.170.2.1      yamt 			rp->r_rexmit = 0;
    433  1.170.2.1      yamt 			if ((rp->r_rflags & RR_SENT) != 0) {
    434  1.170.2.1      yamt 				KASSERT(nmp->nm_sent >= NFS_CWNDSCALE);
    435  1.170.2.1      yamt 				rp->r_rflags &= ~RR_SENT;
    436  1.170.2.1      yamt 				nmp->nm_sent -= NFS_CWNDSCALE;
    437  1.170.2.1      yamt 			}
    438  1.170.2.1      yamt 		}
    439  1.170.2.1      yamt 	}
    440  1.170.2.1      yamt 	KASSERT(nmp->nm_sent == 0);
    441  1.170.2.1      yamt 	mutex_exit(&nfs_reqq_lock);
    442       1.41      fvdl }
    443       1.40      fvdl 
    444       1.41      fvdl void
    445  1.170.2.1      yamt nfs_safedisconnect(struct nfsmount *nmp)
    446       1.41      fvdl {
    447       1.41      fvdl 
    448  1.170.2.1      yamt 	KASSERT(rw_write_held(&nmp->nm_solock));
    449  1.170.2.1      yamt 
    450  1.170.2.1      yamt 	nfs_rcvlock(nmp, NULL); /* XXX ignored error return */
    451       1.41      fvdl 	nfs_disconnect(nmp);
    452       1.87      yamt 	nfs_rcvunlock(nmp);
    453        1.1       cgd }
    454        1.1       cgd 
    455        1.1       cgd /*
    456  1.170.2.5      yamt  * This is the nfs send routine.
    457        1.1       cgd  * "rep == NULL" indicates that it has been called from a server.
    458       1.14   mycroft  * For the client side:
    459       1.14   mycroft  * - return EINTR if the RPC is terminated, 0 otherwise
    460       1.14   mycroft  * - set R_MUSTRESEND if the send fails for any reason
    461       1.58       mrg  * - do any cleanup required by recoverable socket errors (? ? ?)
    462       1.14   mycroft  * For the server side:
    463       1.14   mycroft  * - return EINTR or ERESTART if interrupted by a signal
    464       1.14   mycroft  * - return EPIPE if a connection is lost for connection based sockets (TCP...)
    465       1.58       mrg  * - do any cleanup required by recoverable socket errors (? ? ?)
    466        1.1       cgd  */
    467  1.170.2.3      yamt int
    468  1.170.2.1      yamt nfs_send(struct socket *so, struct mbuf *nam, struct mbuf *top,
    469  1.170.2.1      yamt     struct nfsreq *rep)
    470        1.1       cgd {
    471        1.1       cgd 	struct mbuf *sendnam;
    472       1.14   mycroft 	int error, soflags, flags;
    473        1.1       cgd 
    474        1.1       cgd 	if (rep) {
    475  1.170.2.1      yamt 		KASSERT(rw_read_held(&rep->r_nmp->nm_solock));
    476        1.1       cgd 		if (rep->r_flags & R_SOFTTERM) {
    477        1.1       cgd 			m_freem(top);
    478        1.1       cgd 			return (EINTR);
    479        1.1       cgd 		}
    480       1.14   mycroft 		if ((so = rep->r_nmp->nm_so) == NULL) {
    481       1.14   mycroft 			rep->r_flags |= R_MUSTRESEND;
    482       1.14   mycroft 			m_freem(top);
    483       1.14   mycroft 			return (0);
    484       1.14   mycroft 		}
    485        1.1       cgd 		rep->r_flags &= ~R_MUSTRESEND;
    486        1.1       cgd 		soflags = rep->r_nmp->nm_soflags;
    487        1.1       cgd 	} else
    488        1.1       cgd 		soflags = so->so_proto->pr_flags;
    489        1.1       cgd 	if ((soflags & PR_CONNREQUIRED) || (so->so_state & SS_ISCONNECTED))
    490  1.170.2.1      yamt 		sendnam = NULL;
    491        1.1       cgd 	else
    492        1.1       cgd 		sendnam = nam;
    493       1.14   mycroft 	if (so->so_type == SOCK_SEQPACKET)
    494       1.14   mycroft 		flags = MSG_EOR;
    495       1.14   mycroft 	else
    496       1.14   mycroft 		flags = 0;
    497        1.1       cgd 
    498  1.170.2.1      yamt 	error = (*so->so_send)(so, sendnam, NULL, top, NULL, flags, curlwp);
    499       1.14   mycroft 	if (error) {
    500       1.14   mycroft 		if (rep) {
    501       1.60      fvdl 			if (error == ENOBUFS && so->so_type == SOCK_DGRAM) {
    502       1.60      fvdl 				/*
    503       1.60      fvdl 				 * We're too fast for the network/driver,
    504       1.60      fvdl 				 * and UDP isn't flowcontrolled.
    505       1.60      fvdl 				 * We need to resend. This is not fatal,
    506       1.60      fvdl 				 * just try again.
    507       1.60      fvdl 				 *
    508       1.60      fvdl 				 * Could be smarter here by doing some sort
    509       1.60      fvdl 				 * of a backoff, but this is rare.
    510       1.60      fvdl 				 */
    511       1.14   mycroft 				rep->r_flags |= R_MUSTRESEND;
    512       1.60      fvdl 			} else {
    513      1.101      matt 				if (error != EPIPE)
    514      1.101      matt 					log(LOG_INFO,
    515      1.101      matt 					    "nfs send error %d for %s\n",
    516      1.101      matt 					    error,
    517      1.101      matt 					    rep->r_nmp->nm_mountp->
    518      1.101      matt 						    mnt_stat.f_mntfromname);
    519       1.60      fvdl 				/*
    520       1.60      fvdl 				 * Deal with errors for the client side.
    521       1.60      fvdl 				 */
    522       1.60      fvdl 				if (rep->r_flags & R_SOFTTERM)
    523       1.60      fvdl 					error = EINTR;
    524  1.170.2.7      yamt 				else if (error != EMSGSIZE)
    525       1.60      fvdl 					rep->r_flags |= R_MUSTRESEND;
    526       1.60      fvdl 			}
    527       1.67      fvdl 		} else {
    528       1.67      fvdl 			/*
    529       1.67      fvdl 			 * See above. This error can happen under normal
    530       1.67      fvdl 			 * circumstances and the log is too noisy.
    531       1.67      fvdl 			 * The error will still show up in nfsstat.
    532       1.67      fvdl 			 */
    533       1.67      fvdl 			if (error != ENOBUFS || so->so_type != SOCK_DGRAM)
    534       1.67      fvdl 				log(LOG_INFO, "nfsd send error %d\n", error);
    535       1.67      fvdl 		}
    536       1.14   mycroft 
    537       1.14   mycroft 		/*
    538       1.58       mrg 		 * Handle any recoverable (soft) socket errors here. (? ? ?)
    539       1.14   mycroft 		 */
    540       1.14   mycroft 		if (error != EINTR && error != ERESTART &&
    541  1.170.2.7      yamt 		    error != EWOULDBLOCK && error != EPIPE &&
    542  1.170.2.7      yamt 		    error != EMSGSIZE)
    543        1.1       cgd 			error = 0;
    544        1.1       cgd 	}
    545        1.1       cgd 	return (error);
    546        1.1       cgd }
    547        1.1       cgd 
    548        1.1       cgd /*
    549        1.1       cgd  * Generate the rpc reply header
    550        1.1       cgd  * siz arg. is used to decide if adding a cluster is worthwhile
    551        1.1       cgd  */
    552       1.23  christos int
    553  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)
    554        1.1       cgd {
    555       1.55  augustss 	u_int32_t *tl;
    556       1.55  augustss 	struct mbuf *mreq;
    557      1.148  christos 	char *bpos;
    558       1.79      matt 	struct mbuf *mb;
    559        1.1       cgd 
    560       1.79      matt 	mreq = m_gethdr(M_WAIT, MT_DATA);
    561       1.79      matt 	MCLAIM(mreq, &nfs_mowner);
    562        1.1       cgd 	mb = mreq;
    563       1.14   mycroft 	/*
    564       1.14   mycroft 	 * If this is a big reply, use a cluster else
    565       1.14   mycroft 	 * try and leave leading space for the lower level headers.
    566       1.14   mycroft 	 */
    567       1.14   mycroft 	siz += RPC_REPLYSIZ;
    568       1.45      fvdl 	if (siz >= max_datalen) {
    569       1.79      matt 		m_clget(mreq, M_WAIT);
    570       1.14   mycroft 	} else
    571       1.14   mycroft 		mreq->m_data += max_hdr;
    572       1.22       cgd 	tl = mtod(mreq, u_int32_t *);
    573       1.24      fvdl 	mreq->m_len = 6 * NFSX_UNSIGNED;
    574      1.148  christos 	bpos = ((char *)tl) + mreq->m_len;
    575       1.14   mycroft 	*tl++ = txdr_unsigned(nd->nd_retxid);
    576        1.1       cgd 	*tl++ = rpc_reply;
    577       1.24      fvdl 	if (err == ERPCMISMATCH || (err & NFSERR_AUTHERR)) {
    578        1.1       cgd 		*tl++ = rpc_msgdenied;
    579       1.24      fvdl 		if (err & NFSERR_AUTHERR) {
    580       1.14   mycroft 			*tl++ = rpc_autherr;
    581       1.24      fvdl 			*tl = txdr_unsigned(err & ~NFSERR_AUTHERR);
    582       1.14   mycroft 			mreq->m_len -= NFSX_UNSIGNED;
    583       1.14   mycroft 			bpos -= NFSX_UNSIGNED;
    584       1.14   mycroft 		} else {
    585       1.14   mycroft 			*tl++ = rpc_mismatch;
    586       1.24      fvdl 			*tl++ = txdr_unsigned(RPC_VER2);
    587       1.24      fvdl 			*tl = txdr_unsigned(RPC_VER2);
    588       1.14   mycroft 		}
    589        1.1       cgd 	} else {
    590        1.1       cgd 		*tl++ = rpc_msgaccepted;
    591       1.24      fvdl 
    592       1.24      fvdl 		/*
    593       1.24      fvdl 		 * For Kerberos authentication, we must send the nickname
    594       1.24      fvdl 		 * verifier back, otherwise just RPCAUTH_NULL.
    595       1.24      fvdl 		 */
    596       1.24      fvdl 		if (nd->nd_flag & ND_KERBFULL) {
    597       1.84      yamt 			struct nfsuid *nuidp;
    598       1.84      yamt 			struct timeval ktvin, ktvout;
    599       1.24      fvdl 
    600      1.129       mrg 			memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
    601      1.129       mrg 
    602      1.131      yamt 			LIST_FOREACH(nuidp,
    603      1.131      yamt 			    NUIDHASH(slp, kauth_cred_geteuid(nd->nd_cr)),
    604       1.84      yamt 			    nu_hash) {
    605      1.131      yamt 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    606      1.131      yamt 				kauth_cred_geteuid(nd->nd_cr) &&
    607       1.84      yamt 				    (!nd->nd_nam2 || netaddr_match(
    608       1.84      yamt 				    NU_NETFAM(nuidp), &nuidp->nu_haddr,
    609       1.84      yamt 				    nd->nd_nam2)))
    610       1.84      yamt 					break;
    611       1.84      yamt 			}
    612       1.84      yamt 			if (nuidp) {
    613       1.84      yamt 				ktvin.tv_sec =
    614       1.84      yamt 				    txdr_unsigned(nuidp->nu_timestamp.tv_sec
    615       1.84      yamt 					- 1);
    616       1.84      yamt 				ktvin.tv_usec =
    617       1.84      yamt 				    txdr_unsigned(nuidp->nu_timestamp.tv_usec);
    618       1.24      fvdl 
    619       1.84      yamt 				/*
    620       1.84      yamt 				 * Encrypt the timestamp in ecb mode using the
    621       1.84      yamt 				 * session key.
    622       1.84      yamt 				 */
    623       1.24      fvdl #ifdef NFSKERB
    624       1.84      yamt 				XXX
    625       1.24      fvdl #endif
    626       1.24      fvdl 
    627       1.84      yamt 				*tl++ = rpc_auth_kerb;
    628       1.84      yamt 				*tl++ = txdr_unsigned(3 * NFSX_UNSIGNED);
    629       1.84      yamt 				*tl = ktvout.tv_sec;
    630       1.84      yamt 				nfsm_build(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
    631       1.84      yamt 				*tl++ = ktvout.tv_usec;
    632      1.131      yamt 				*tl++ = txdr_unsigned(
    633      1.131      yamt 				    kauth_cred_geteuid(nuidp->nu_cr));
    634       1.84      yamt 			} else {
    635       1.84      yamt 				*tl++ = 0;
    636       1.84      yamt 				*tl++ = 0;
    637       1.84      yamt 			}
    638       1.24      fvdl 		} else {
    639       1.24      fvdl 			*tl++ = 0;
    640       1.24      fvdl 			*tl++ = 0;
    641       1.24      fvdl 		}
    642        1.1       cgd 		switch (err) {
    643        1.1       cgd 		case EPROGUNAVAIL:
    644        1.1       cgd 			*tl = txdr_unsigned(RPC_PROGUNAVAIL);
    645        1.1       cgd 			break;
    646        1.1       cgd 		case EPROGMISMATCH:
    647        1.1       cgd 			*tl = txdr_unsigned(RPC_PROGMISMATCH);
    648       1.24      fvdl 			nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
    649      1.144      yamt 			*tl++ = txdr_unsigned(2);
    650      1.144      yamt 			*tl = txdr_unsigned(3);
    651        1.1       cgd 			break;
    652        1.1       cgd 		case EPROCUNAVAIL:
    653        1.1       cgd 			*tl = txdr_unsigned(RPC_PROCUNAVAIL);
    654        1.1       cgd 			break;
    655       1.24      fvdl 		case EBADRPC:
    656       1.24      fvdl 			*tl = txdr_unsigned(RPC_GARBAGE);
    657       1.24      fvdl 			break;
    658        1.1       cgd 		default:
    659        1.1       cgd 			*tl = 0;
    660       1.24      fvdl 			if (err != NFSERR_RETVOID) {
    661       1.22       cgd 				nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
    662       1.14   mycroft 				if (err)
    663       1.24      fvdl 				    *tl = txdr_unsigned(nfsrv_errmap(nd, err));
    664       1.14   mycroft 				else
    665       1.24      fvdl 				    *tl = 0;
    666        1.1       cgd 			}
    667        1.1       cgd 			break;
    668        1.1       cgd 		};
    669        1.1       cgd 	}
    670       1.14   mycroft 
    671       1.34      fvdl 	if (mrq != NULL)
    672       1.34      fvdl 		*mrq = mreq;
    673        1.1       cgd 	*mbp = mb;
    674        1.1       cgd 	*bposp = bpos;
    675       1.24      fvdl 	if (err != 0 && err != NFSERR_RETVOID)
    676        1.1       cgd 		nfsstats.srvrpc_errs++;
    677        1.1       cgd 	return (0);
    678        1.1       cgd }
    679        1.1       cgd 
    680      1.161      yamt static void
    681      1.161      yamt nfs_timer_schedule(void)
    682      1.161      yamt {
    683      1.161      yamt 
    684      1.161      yamt 	callout_schedule(&nfs_timer_ch, nfs_ticks);
    685      1.161      yamt }
    686      1.161      yamt 
    687      1.161      yamt void
    688      1.161      yamt nfs_timer_start(void)
    689      1.161      yamt {
    690      1.161      yamt 
    691      1.161      yamt 	if (callout_pending(&nfs_timer_ch))
    692      1.161      yamt 		return;
    693      1.161      yamt 
    694      1.161      yamt 	nfs_timer_start_ev.ev_count++;
    695      1.161      yamt 	nfs_timer_schedule();
    696      1.161      yamt }
    697      1.161      yamt 
    698      1.161      yamt void
    699      1.161      yamt nfs_timer_init(void)
    700      1.161      yamt {
    701      1.161      yamt 
    702  1.170.2.2      yamt 	mutex_init(&nfs_timer_lock, MUTEX_DEFAULT, IPL_NONE);
    703  1.170.2.1      yamt 	callout_init(&nfs_timer_ch, CALLOUT_MPSAFE);
    704      1.161      yamt 	callout_setfunc(&nfs_timer_ch, nfs_timer, NULL);
    705      1.161      yamt 	evcnt_attach_dynamic(&nfs_timer_ev, EVCNT_TYPE_MISC, NULL,
    706      1.161      yamt 	    "nfs", "timer");
    707      1.161      yamt 	evcnt_attach_dynamic(&nfs_timer_start_ev, EVCNT_TYPE_MISC, NULL,
    708      1.161      yamt 	    "nfs", "timer start");
    709      1.161      yamt 	evcnt_attach_dynamic(&nfs_timer_stop_ev, EVCNT_TYPE_MISC, NULL,
    710      1.161      yamt 	    "nfs", "timer stop");
    711      1.161      yamt }
    712      1.161      yamt 
    713  1.170.2.2      yamt void
    714  1.170.2.2      yamt nfs_timer_fini(void)
    715  1.170.2.2      yamt {
    716  1.170.2.2      yamt 
    717  1.170.2.2      yamt 	callout_halt(&nfs_timer_ch, NULL);
    718  1.170.2.2      yamt 	callout_destroy(&nfs_timer_ch);
    719  1.170.2.2      yamt 	mutex_destroy(&nfs_timer_lock);
    720  1.170.2.2      yamt 	evcnt_detach(&nfs_timer_ev);
    721  1.170.2.2      yamt 	evcnt_detach(&nfs_timer_start_ev);
    722  1.170.2.2      yamt 	evcnt_detach(&nfs_timer_stop_ev);
    723  1.170.2.2      yamt }
    724  1.170.2.2      yamt 
    725  1.170.2.2      yamt void
    726  1.170.2.2      yamt nfs_timer_srvinit(bool (*func)(void))
    727  1.170.2.2      yamt {
    728  1.170.2.2      yamt 
    729  1.170.2.2      yamt 	nfs_timer_srvvec = func;
    730  1.170.2.2      yamt }
    731  1.170.2.2      yamt 
    732  1.170.2.2      yamt void
    733  1.170.2.2      yamt nfs_timer_srvfini(void)
    734  1.170.2.2      yamt {
    735  1.170.2.2      yamt 
    736  1.170.2.2      yamt 	mutex_enter(&nfs_timer_lock);
    737  1.170.2.2      yamt 	nfs_timer_srvvec = NULL;
    738  1.170.2.2      yamt 	mutex_exit(&nfs_timer_lock);
    739  1.170.2.2      yamt }
    740  1.170.2.2      yamt 
    741  1.170.2.2      yamt 
    742        1.1       cgd /*
    743        1.1       cgd  * Nfs timer routine
    744        1.1       cgd  * Scan the nfsreq list and retranmit any requests that have timed out
    745        1.1       cgd  * To avoid retransmission attempts on STREAM sockets (in the future) make
    746        1.1       cgd  * sure to set the r_retry field to 0 (implies nm_retry == 0).
    747        1.1       cgd  */
    748        1.7   mycroft void
    749      1.141      yamt nfs_timer(void *arg)
    750        1.1       cgd {
    751       1.55  augustss 	struct nfsreq *rep;
    752       1.55  augustss 	struct mbuf *m;
    753       1.55  augustss 	struct socket *so;
    754       1.55  augustss 	struct nfsmount *nmp;
    755       1.55  augustss 	int timeo;
    756  1.170.2.1      yamt 	int error;
    757      1.161      yamt 	bool more = false;
    758        1.1       cgd 
    759      1.161      yamt 	nfs_timer_ev.ev_count++;
    760      1.160        ad 
    761  1.170.2.1      yamt 	mutex_enter(&nfs_reqq_lock);
    762       1.73  christos 	TAILQ_FOREACH(rep, &nfs_reqq, r_chain) {
    763      1.161      yamt 		more = true;
    764        1.1       cgd 		nmp = rep->r_nmp;
    765       1.14   mycroft 		if (rep->r_mrep || (rep->r_flags & R_SOFTTERM))
    766        1.1       cgd 			continue;
    767      1.119  christos 		if (nfs_sigintr(nmp, rep, rep->r_lwp)) {
    768        1.1       cgd 			rep->r_flags |= R_SOFTTERM;
    769        1.1       cgd 			continue;
    770        1.1       cgd 		}
    771       1.14   mycroft 		if (rep->r_rtt >= 0) {
    772       1.14   mycroft 			rep->r_rtt++;
    773       1.14   mycroft 			if (nmp->nm_flag & NFSMNT_DUMBTIMR)
    774       1.14   mycroft 				timeo = nmp->nm_timeo;
    775       1.14   mycroft 			else
    776  1.170.2.7      yamt 				timeo = NFS_RTO(nmp, nfs_proct[rep->r_procnum]);
    777       1.14   mycroft 			if (nmp->nm_timeouts > 0)
    778       1.14   mycroft 				timeo *= nfs_backoff[nmp->nm_timeouts - 1];
    779  1.170.2.2      yamt 			if (timeo > NFS_MAXTIMEO)
    780  1.170.2.2      yamt 				timeo = NFS_MAXTIMEO;
    781       1.14   mycroft 			if (rep->r_rtt <= timeo)
    782       1.14   mycroft 				continue;
    783       1.98      yamt 			if (nmp->nm_timeouts <
    784       1.98      yamt 			    (sizeof(nfs_backoff) / sizeof(nfs_backoff[0])))
    785       1.14   mycroft 				nmp->nm_timeouts++;
    786        1.1       cgd 		}
    787        1.1       cgd 		/*
    788        1.1       cgd 		 * Check for server not responding
    789        1.1       cgd 		 */
    790        1.1       cgd 		if ((rep->r_flags & R_TPRINTFMSG) == 0 &&
    791       1.14   mycroft 		     rep->r_rexmit > nmp->nm_deadthresh) {
    792      1.119  christos 			nfs_msg(rep->r_lwp,
    793        1.1       cgd 			    nmp->nm_mountp->mnt_stat.f_mntfromname,
    794        1.1       cgd 			    "not responding");
    795        1.1       cgd 			rep->r_flags |= R_TPRINTFMSG;
    796        1.1       cgd 		}
    797        1.1       cgd 		if (rep->r_rexmit >= rep->r_retry) {	/* too many */
    798        1.1       cgd 			nfsstats.rpctimeouts++;
    799        1.1       cgd 			rep->r_flags |= R_SOFTTERM;
    800        1.1       cgd 			continue;
    801        1.1       cgd 		}
    802       1.14   mycroft 		if (nmp->nm_sotype != SOCK_DGRAM) {
    803       1.14   mycroft 			if (++rep->r_rexmit > NFS_MAXREXMIT)
    804       1.14   mycroft 				rep->r_rexmit = NFS_MAXREXMIT;
    805       1.14   mycroft 			continue;
    806       1.14   mycroft 		}
    807  1.170.2.1      yamt 		if (!rw_tryenter(&nmp->nm_solock, RW_READER)) {
    808  1.170.2.1      yamt 			printf("%s: rw_trylock failed\n", __func__);
    809        1.1       cgd 			continue;
    810  1.170.2.1      yamt 		}
    811  1.170.2.1      yamt 		if ((so = nmp->nm_so) == NULL) {
    812  1.170.2.1      yamt 			rw_exit(&nmp->nm_solock);
    813  1.170.2.1      yamt 			continue;
    814  1.170.2.1      yamt 		}
    815        1.1       cgd 
    816        1.1       cgd 		/*
    817        1.1       cgd 		 * If there is enough space and the window allows..
    818        1.1       cgd 		 *	Resend it
    819       1.14   mycroft 		 * Set r_rtt to -1 in case we fail to send it now.
    820        1.1       cgd 		 */
    821  1.170.2.4      yamt 		solock(so);
    822       1.14   mycroft 		rep->r_rtt = -1;
    823        1.1       cgd 		if (sbspace(&so->so_snd) >= rep->r_mreq->m_pkthdr.len &&
    824       1.14   mycroft 		   ((nmp->nm_flag & NFSMNT_DUMBTIMR) ||
    825  1.170.2.1      yamt 		    (rep->r_rflags & RR_SENT) ||
    826       1.14   mycroft 		    nmp->nm_sent < nmp->nm_cwnd) &&
    827  1.170.2.1      yamt 		   (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))) {
    828       1.40      fvdl 		        if (so->so_state & SS_ISCONNECTED)
    829  1.170.2.1      yamt 				error = (*so->so_proto->pr_usrreq)(so, PRU_SEND,
    830  1.170.2.1      yamt 				    m, NULL, NULL, NULL);
    831        1.1       cgd 			else
    832  1.170.2.1      yamt 				error = (*so->so_proto->pr_usrreq)(so, PRU_SEND,
    833  1.170.2.1      yamt 				    m, nmp->nm_nam, NULL, NULL);
    834        1.1       cgd 			if (error) {
    835       1.33      fvdl 				if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) {
    836       1.37      fvdl #ifdef DEBUG
    837  1.170.2.2      yamt 					if (ratecheck(&nfs_timer_last_err_time,
    838  1.170.2.2      yamt 					    &nfs_err_interval))
    839  1.170.2.2      yamt 						printf("%s: ignoring error "
    840  1.170.2.2      yamt 						       "%d\n", __func__, error);
    841       1.37      fvdl #endif
    842        1.1       cgd 					so->so_error = 0;
    843       1.33      fvdl 				}
    844        1.1       cgd 			} else {
    845        1.1       cgd 				/*
    846       1.14   mycroft 				 * Iff first send, start timing
    847       1.14   mycroft 				 * else turn timing off, backoff timer
    848       1.14   mycroft 				 * and divide congestion window by 2.
    849        1.1       cgd 				 */
    850  1.170.2.1      yamt 				if (rep->r_rflags & RR_SENT) {
    851       1.14   mycroft 					rep->r_flags &= ~R_TIMING;
    852       1.14   mycroft 					if (++rep->r_rexmit > NFS_MAXREXMIT)
    853       1.14   mycroft 						rep->r_rexmit = NFS_MAXREXMIT;
    854       1.14   mycroft 					nmp->nm_cwnd >>= 1;
    855       1.14   mycroft 					if (nmp->nm_cwnd < NFS_CWNDSCALE)
    856       1.14   mycroft 						nmp->nm_cwnd = NFS_CWNDSCALE;
    857       1.14   mycroft 					nfsstats.rpcretries++;
    858       1.14   mycroft 				} else {
    859  1.170.2.1      yamt 					rep->r_rflags |= RR_SENT;
    860       1.14   mycroft 					nmp->nm_sent += NFS_CWNDSCALE;
    861       1.14   mycroft 				}
    862       1.14   mycroft 				rep->r_rtt = 0;
    863        1.1       cgd 			}
    864        1.1       cgd 		}
    865      1.170        ad 		sounlock(so);
    866  1.170.2.1      yamt 		rw_exit(&nmp->nm_solock);
    867        1.1       cgd 	}
    868  1.170.2.1      yamt 	mutex_exit(&nfs_reqq_lock);
    869       1.14   mycroft 
    870  1.170.2.2      yamt 	mutex_enter(&nfs_timer_lock);
    871  1.170.2.2      yamt 	if (nfs_timer_srvvec != NULL) {
    872  1.170.2.2      yamt 		more |= (*nfs_timer_srvvec)();
    873       1.24      fvdl 	}
    874  1.170.2.2      yamt 	mutex_exit(&nfs_timer_lock);
    875  1.170.2.2      yamt 
    876      1.161      yamt 	if (more) {
    877      1.161      yamt 		nfs_timer_schedule();
    878      1.161      yamt 	} else {
    879      1.161      yamt 		nfs_timer_stop_ev.ev_count++;
    880      1.161      yamt 	}
    881        1.1       cgd }
    882        1.1       cgd 
    883        1.1       cgd /*
    884       1.14   mycroft  * Test for a termination condition pending on the process.
    885       1.14   mycroft  * This is used for NFSMNT_INT mounts.
    886        1.1       cgd  */
    887       1.23  christos int
    888  1.170.2.2      yamt nfs_sigintr(struct nfsmount *nmp, struct nfsreq *rep, struct lwp *l)
    889       1.14   mycroft {
    890       1.47   mycroft 	sigset_t ss;
    891       1.14   mycroft 
    892       1.14   mycroft 	if (rep && (rep->r_flags & R_SOFTTERM))
    893       1.14   mycroft 		return (EINTR);
    894       1.14   mycroft 	if (!(nmp->nm_flag & NFSMNT_INT))
    895       1.14   mycroft 		return (0);
    896      1.119  christos 	if (l) {
    897      1.145        ad 		sigpending1(l, &ss);
    898       1.47   mycroft #if 0
    899      1.119  christos 		sigminusset(&l->l_proc->p_sigctx.ps_sigignore, &ss);
    900       1.47   mycroft #endif
    901       1.47   mycroft 		if (sigismember(&ss, SIGINT) || sigismember(&ss, SIGTERM) ||
    902       1.47   mycroft 		    sigismember(&ss, SIGKILL) || sigismember(&ss, SIGHUP) ||
    903       1.47   mycroft 		    sigismember(&ss, SIGQUIT))
    904       1.47   mycroft 			return (EINTR);
    905       1.47   mycroft 	}
    906       1.14   mycroft 	return (0);
    907       1.14   mycroft }
    908        1.1       cgd 
    909  1.170.2.7      yamt int
    910      1.150      yamt nfs_rcvlock(struct nfsmount *nmp, struct nfsreq *rep)
    911       1.14   mycroft {
    912      1.150      yamt 	int slptimeo = 0;
    913      1.150      yamt 	bool catch;
    914       1.87      yamt 	int error = 0;
    915       1.14   mycroft 
    916  1.170.2.1      yamt 	KASSERT(rep == NULL || nmp == rep->r_nmp);
    917      1.150      yamt 
    918      1.154      yamt 	catch = (nmp->nm_flag & NFSMNT_INT) != 0;
    919      1.150      yamt 	mutex_enter(&nmp->nm_lock);
    920      1.153      yamt 	while (/* CONSTCOND */ true) {
    921  1.170.2.1      yamt 		KASSERT(nmp->nm_rcvlwp != curlwp);
    922  1.170.2.1      yamt 		if (nmp->nm_iflag & NFSMNT_DISMNT) {
    923      1.151      yamt 			cv_signal(&nmp->nm_disconcv);
    924       1.87      yamt 			error = EIO;
    925      1.153      yamt 			break;
    926       1.51  sommerfe 		}
    927       1.36      fvdl 		/* If our reply was received while we were sleeping,
    928       1.36      fvdl 		 * then just return without taking the lock to avoid a
    929       1.36      fvdl 		 * situation where a single iod could 'capture' the
    930       1.36      fvdl 		 * receive lock.
    931       1.36      fvdl 		 */
    932  1.170.2.1      yamt 		if (rep != NULL) {
    933  1.170.2.1      yamt 			if (rep->r_mrep != NULL) {
    934  1.170.2.1      yamt 				error = EALREADY;
    935  1.170.2.1      yamt 				break;
    936  1.170.2.1      yamt 			}
    937  1.170.2.1      yamt 			if (nfs_sigintr(rep->r_nmp, rep, rep->r_lwp)) {
    938  1.170.2.1      yamt 				error = EINTR;
    939  1.170.2.1      yamt 				break;
    940  1.170.2.1      yamt 			}
    941      1.153      yamt 		}
    942  1.170.2.1      yamt 		if (nmp->nm_rcvlwp == NULL) {
    943  1.170.2.1      yamt 			nmp->nm_rcvlwp = curlwp;
    944      1.153      yamt 			break;
    945      1.153      yamt 		}
    946      1.153      yamt 		if (catch) {
    947      1.153      yamt 			cv_timedwait_sig(&nmp->nm_rcvcv, &nmp->nm_lock,
    948      1.153      yamt 			    slptimeo);
    949      1.153      yamt 		} else {
    950      1.153      yamt 			cv_timedwait(&nmp->nm_rcvcv, &nmp->nm_lock,
    951      1.153      yamt 			    slptimeo);
    952       1.87      yamt 		}
    953      1.150      yamt 		if (catch) {
    954      1.150      yamt 			catch = false;
    955       1.14   mycroft 			slptimeo = 2 * hz;
    956        1.1       cgd 		}
    957        1.1       cgd 	}
    958      1.150      yamt 	mutex_exit(&nmp->nm_lock);
    959       1.87      yamt 	return error;
    960       1.14   mycroft }
    961       1.14   mycroft 
    962       1.14   mycroft /*
    963       1.14   mycroft  * Unlock the stream socket for others.
    964       1.14   mycroft  */
    965  1.170.2.7      yamt void
    966      1.150      yamt nfs_rcvunlock(struct nfsmount *nmp)
    967       1.14   mycroft {
    968       1.14   mycroft 
    969      1.150      yamt 	mutex_enter(&nmp->nm_lock);
    970  1.170.2.1      yamt 	if (nmp->nm_rcvlwp != curlwp)
    971       1.14   mycroft 		panic("nfs rcvunlock");
    972  1.170.2.1      yamt 	nmp->nm_rcvlwp = NULL;
    973      1.153      yamt 	cv_broadcast(&nmp->nm_rcvcv);
    974      1.150      yamt 	mutex_exit(&nmp->nm_lock);
    975        1.1       cgd }
    976        1.1       cgd 
    977       1.14   mycroft /*
    978       1.14   mycroft  * Parse an RPC request
    979       1.14   mycroft  * - verify it
    980      1.131      yamt  * - allocate and fill in the cred.
    981        1.1       cgd  */
    982       1.23  christos int
    983  1.170.2.2      yamt nfs_getreq(struct nfsrv_descript *nd, struct nfsd *nfsd, int has_header)
    984        1.1       cgd {
    985       1.55  augustss 	int len, i;
    986       1.55  augustss 	u_int32_t *tl;
    987       1.55  augustss 	int32_t t1;
    988       1.14   mycroft 	struct uio uio;
    989       1.14   mycroft 	struct iovec iov;
    990      1.148  christos 	char *dpos, *cp2, *cp;
    991       1.22       cgd 	u_int32_t nfsvers, auth_type;
    992       1.24      fvdl 	uid_t nickuid;
    993      1.144      yamt 	int error = 0, ticklen;
    994       1.14   mycroft 	struct mbuf *mrep, *md;
    995       1.55  augustss 	struct nfsuid *nuidp;
    996       1.24      fvdl 	struct timeval tvin, tvout;
    997       1.14   mycroft 
    998      1.129       mrg 	memset(&tvout, 0, sizeof tvout);	/* XXX gcc */
    999      1.129       mrg 
   1000      1.131      yamt 	KASSERT(nd->nd_cr == NULL);
   1001       1.14   mycroft 	mrep = nd->nd_mrep;
   1002       1.14   mycroft 	md = nd->nd_md;
   1003       1.14   mycroft 	dpos = nd->nd_dpos;
   1004       1.14   mycroft 	if (has_header) {
   1005       1.24      fvdl 		nfsm_dissect(tl, u_int32_t *, 10 * NFSX_UNSIGNED);
   1006       1.24      fvdl 		nd->nd_retxid = fxdr_unsigned(u_int32_t, *tl++);
   1007       1.14   mycroft 		if (*tl++ != rpc_call) {
   1008       1.14   mycroft 			m_freem(mrep);
   1009       1.14   mycroft 			return (EBADRPC);
   1010       1.14   mycroft 		}
   1011       1.24      fvdl 	} else
   1012       1.24      fvdl 		nfsm_dissect(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
   1013       1.14   mycroft 	nd->nd_repstat = 0;
   1014       1.24      fvdl 	nd->nd_flag = 0;
   1015       1.14   mycroft 	if (*tl++ != rpc_vers) {
   1016       1.14   mycroft 		nd->nd_repstat = ERPCMISMATCH;
   1017       1.14   mycroft 		nd->nd_procnum = NFSPROC_NOOP;
   1018       1.14   mycroft 		return (0);
   1019       1.14   mycroft 	}
   1020       1.14   mycroft 	if (*tl != nfs_prog) {
   1021      1.144      yamt 		nd->nd_repstat = EPROGUNAVAIL;
   1022      1.144      yamt 		nd->nd_procnum = NFSPROC_NOOP;
   1023      1.144      yamt 		return (0);
   1024       1.14   mycroft 	}
   1025       1.14   mycroft 	tl++;
   1026       1.24      fvdl 	nfsvers = fxdr_unsigned(u_int32_t, *tl++);
   1027      1.144      yamt 	if (nfsvers < NFS_VER2 || nfsvers > NFS_VER3) {
   1028       1.14   mycroft 		nd->nd_repstat = EPROGMISMATCH;
   1029       1.14   mycroft 		nd->nd_procnum = NFSPROC_NOOP;
   1030       1.14   mycroft 		return (0);
   1031       1.14   mycroft 	}
   1032      1.144      yamt 	if (nfsvers == NFS_VER3)
   1033       1.24      fvdl 		nd->nd_flag = ND_NFSV3;
   1034       1.24      fvdl 	nd->nd_procnum = fxdr_unsigned(u_int32_t, *tl++);
   1035       1.14   mycroft 	if (nd->nd_procnum == NFSPROC_NULL)
   1036       1.14   mycroft 		return (0);
   1037      1.144      yamt 	if (nd->nd_procnum > NFSPROC_COMMIT ||
   1038      1.144      yamt 	    (!nd->nd_flag && nd->nd_procnum > NFSV2PROC_STATFS)) {
   1039       1.14   mycroft 		nd->nd_repstat = EPROCUNAVAIL;
   1040       1.14   mycroft 		nd->nd_procnum = NFSPROC_NOOP;
   1041        1.1       cgd 		return (0);
   1042       1.14   mycroft 	}
   1043       1.24      fvdl 	if ((nd->nd_flag & ND_NFSV3) == 0)
   1044       1.24      fvdl 		nd->nd_procnum = nfsv3_procid[nd->nd_procnum];
   1045       1.14   mycroft 	auth_type = *tl++;
   1046       1.14   mycroft 	len = fxdr_unsigned(int, *tl++);
   1047       1.14   mycroft 	if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1048       1.14   mycroft 		m_freem(mrep);
   1049       1.14   mycroft 		return (EBADRPC);
   1050       1.14   mycroft 	}
   1051       1.14   mycroft 
   1052       1.24      fvdl 	nd->nd_flag &= ~ND_KERBAUTH;
   1053       1.14   mycroft 	/*
   1054       1.14   mycroft 	 * Handle auth_unix or auth_kerb.
   1055       1.14   mycroft 	 */
   1056       1.14   mycroft 	if (auth_type == rpc_auth_unix) {
   1057      1.130      elad 		uid_t uid;
   1058      1.167      yamt 		gid_t gid;
   1059      1.130      elad 
   1060      1.131      yamt 		nd->nd_cr = kauth_cred_alloc();
   1061       1.14   mycroft 		len = fxdr_unsigned(int, *++tl);
   1062       1.14   mycroft 		if (len < 0 || len > NFS_MAXNAMLEN) {
   1063       1.14   mycroft 			m_freem(mrep);
   1064      1.131      yamt 			error = EBADRPC;
   1065      1.131      yamt 			goto errout;
   1066       1.14   mycroft 		}
   1067       1.14   mycroft 		nfsm_adv(nfsm_rndup(len));
   1068       1.24      fvdl 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1069      1.130      elad 
   1070      1.130      elad 		uid = fxdr_unsigned(uid_t, *tl++);
   1071      1.130      elad 		gid = fxdr_unsigned(gid_t, *tl++);
   1072      1.130      elad 		kauth_cred_setuid(nd->nd_cr, uid);
   1073      1.137      yamt 		kauth_cred_seteuid(nd->nd_cr, uid);
   1074      1.137      yamt 		kauth_cred_setsvuid(nd->nd_cr, uid);
   1075      1.130      elad 		kauth_cred_setgid(nd->nd_cr, gid);
   1076      1.137      yamt 		kauth_cred_setegid(nd->nd_cr, gid);
   1077      1.130      elad 		kauth_cred_setsvgid(nd->nd_cr, gid);
   1078      1.130      elad 
   1079       1.14   mycroft 		len = fxdr_unsigned(int, *tl);
   1080       1.14   mycroft 		if (len < 0 || len > RPCAUTH_UNIXGIDS) {
   1081       1.14   mycroft 			m_freem(mrep);
   1082      1.131      yamt 			error = EBADRPC;
   1083      1.131      yamt 			goto errout;
   1084       1.14   mycroft 		}
   1085       1.24      fvdl 		nfsm_dissect(tl, u_int32_t *, (len + 2) * NFSX_UNSIGNED);
   1086      1.130      elad 
   1087      1.167      yamt 		if (len > 0) {
   1088      1.167      yamt 			size_t grbuf_size = min(len, NGROUPS) * sizeof(gid_t);
   1089      1.167      yamt 			gid_t *grbuf = kmem_alloc(grbuf_size, KM_SLEEP);
   1090      1.167      yamt 
   1091      1.167      yamt 			for (i = 0; i < len; i++) {
   1092      1.167      yamt 				if (i < NGROUPS) /* XXX elad */
   1093      1.167      yamt 					grbuf[i] = fxdr_unsigned(gid_t, *tl++);
   1094      1.167      yamt 				else
   1095      1.167      yamt 					tl++;
   1096      1.167      yamt 			}
   1097      1.167      yamt 			kauth_cred_setgroups(nd->nd_cr, grbuf,
   1098      1.167      yamt 			    min(len, NGROUPS), -1, UIO_SYSSPACE);
   1099      1.167      yamt 			kmem_free(grbuf, grbuf_size);
   1100      1.130      elad 		}
   1101      1.130      elad 
   1102       1.24      fvdl 		len = fxdr_unsigned(int, *++tl);
   1103       1.24      fvdl 		if (len < 0 || len > RPCAUTH_MAXSIZ) {
   1104       1.14   mycroft 			m_freem(mrep);
   1105      1.131      yamt 			error = EBADRPC;
   1106      1.131      yamt 			goto errout;
   1107       1.14   mycroft 		}
   1108       1.24      fvdl 		if (len > 0)
   1109       1.24      fvdl 			nfsm_adv(nfsm_rndup(len));
   1110       1.24      fvdl 	} else if (auth_type == rpc_auth_kerb) {
   1111       1.24      fvdl 		switch (fxdr_unsigned(int, *tl++)) {
   1112       1.24      fvdl 		case RPCAKN_FULLNAME:
   1113       1.24      fvdl 			ticklen = fxdr_unsigned(int, *tl);
   1114       1.24      fvdl 			*((u_int32_t *)nfsd->nfsd_authstr) = *tl;
   1115       1.24      fvdl 			uio.uio_resid = nfsm_rndup(ticklen) + NFSX_UNSIGNED;
   1116       1.24      fvdl 			nfsd->nfsd_authlen = uio.uio_resid + NFSX_UNSIGNED;
   1117       1.24      fvdl 			if (uio.uio_resid > (len - 2 * NFSX_UNSIGNED)) {
   1118       1.24      fvdl 				m_freem(mrep);
   1119      1.131      yamt 				error = EBADRPC;
   1120      1.131      yamt 				goto errout;
   1121       1.24      fvdl 			}
   1122       1.24      fvdl 			uio.uio_offset = 0;
   1123       1.24      fvdl 			uio.uio_iov = &iov;
   1124       1.24      fvdl 			uio.uio_iovcnt = 1;
   1125      1.123      yamt 			UIO_SETUP_SYSSPACE(&uio);
   1126      1.148  christos 			iov.iov_base = (void *)&nfsd->nfsd_authstr[4];
   1127       1.24      fvdl 			iov.iov_len = RPCAUTH_MAXSIZ - 4;
   1128       1.24      fvdl 			nfsm_mtouio(&uio, uio.uio_resid);
   1129       1.24      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1130       1.24      fvdl 			if (*tl++ != rpc_auth_kerb ||
   1131       1.24      fvdl 				fxdr_unsigned(int, *tl) != 4 * NFSX_UNSIGNED) {
   1132       1.31  christos 				printf("Bad kerb verifier\n");
   1133       1.24      fvdl 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1134       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1135       1.24      fvdl 				return (0);
   1136       1.24      fvdl 			}
   1137      1.148  christos 			nfsm_dissect(cp, void *, 4 * NFSX_UNSIGNED);
   1138       1.24      fvdl 			tl = (u_int32_t *)cp;
   1139       1.24      fvdl 			if (fxdr_unsigned(int, *tl) != RPCAKN_FULLNAME) {
   1140       1.31  christos 				printf("Not fullname kerb verifier\n");
   1141       1.24      fvdl 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1142       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1143       1.24      fvdl 				return (0);
   1144       1.24      fvdl 			}
   1145       1.24      fvdl 			cp += NFSX_UNSIGNED;
   1146       1.46     perry 			memcpy(nfsd->nfsd_verfstr, cp, 3 * NFSX_UNSIGNED);
   1147       1.24      fvdl 			nfsd->nfsd_verflen = 3 * NFSX_UNSIGNED;
   1148       1.24      fvdl 			nd->nd_flag |= ND_KERBFULL;
   1149       1.24      fvdl 			nfsd->nfsd_flag |= NFSD_NEEDAUTH;
   1150       1.24      fvdl 			break;
   1151       1.24      fvdl 		case RPCAKN_NICKNAME:
   1152       1.24      fvdl 			if (len != 2 * NFSX_UNSIGNED) {
   1153       1.31  christos 				printf("Kerb nickname short\n");
   1154       1.24      fvdl 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADCRED);
   1155       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1156       1.24      fvdl 				return (0);
   1157       1.24      fvdl 			}
   1158       1.24      fvdl 			nickuid = fxdr_unsigned(uid_t, *tl);
   1159       1.24      fvdl 			nfsm_dissect(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
   1160       1.24      fvdl 			if (*tl++ != rpc_auth_kerb ||
   1161       1.24      fvdl 				fxdr_unsigned(int, *tl) != 3 * NFSX_UNSIGNED) {
   1162       1.31  christos 				printf("Kerb nick verifier bad\n");
   1163       1.24      fvdl 				nd->nd_repstat = (NFSERR_AUTHERR|AUTH_BADVERF);
   1164       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1165       1.24      fvdl 				return (0);
   1166       1.24      fvdl 			}
   1167       1.24      fvdl 			nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1168       1.24      fvdl 			tvin.tv_sec = *tl++;
   1169       1.24      fvdl 			tvin.tv_usec = *tl;
   1170       1.24      fvdl 
   1171       1.80      yamt 			LIST_FOREACH(nuidp, NUIDHASH(nfsd->nfsd_slp, nickuid),
   1172       1.80      yamt 			    nu_hash) {
   1173      1.130      elad 				if (kauth_cred_geteuid(nuidp->nu_cr) == nickuid &&
   1174       1.24      fvdl 				    (!nd->nd_nam2 ||
   1175       1.24      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
   1176       1.24      fvdl 				      &nuidp->nu_haddr, nd->nd_nam2)))
   1177       1.24      fvdl 					break;
   1178       1.24      fvdl 			}
   1179       1.24      fvdl 			if (!nuidp) {
   1180       1.24      fvdl 				nd->nd_repstat =
   1181       1.24      fvdl 					(NFSERR_AUTHERR|AUTH_REJECTCRED);
   1182       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1183       1.24      fvdl 				return (0);
   1184       1.24      fvdl 			}
   1185       1.24      fvdl 
   1186       1.24      fvdl 			/*
   1187       1.24      fvdl 			 * Now, decrypt the timestamp using the session key
   1188       1.24      fvdl 			 * and validate it.
   1189       1.24      fvdl 			 */
   1190       1.24      fvdl #ifdef NFSKERB
   1191       1.24      fvdl 			XXX
   1192       1.24      fvdl #endif
   1193       1.14   mycroft 
   1194       1.24      fvdl 			tvout.tv_sec = fxdr_unsigned(long, tvout.tv_sec);
   1195       1.24      fvdl 			tvout.tv_usec = fxdr_unsigned(long, tvout.tv_usec);
   1196      1.135    kardel 			if (nuidp->nu_expire < time_second ||
   1197       1.24      fvdl 			    nuidp->nu_timestamp.tv_sec > tvout.tv_sec ||
   1198       1.24      fvdl 			    (nuidp->nu_timestamp.tv_sec == tvout.tv_sec &&
   1199       1.24      fvdl 			     nuidp->nu_timestamp.tv_usec > tvout.tv_usec)) {
   1200       1.24      fvdl 				nuidp->nu_expire = 0;
   1201       1.24      fvdl 				nd->nd_repstat =
   1202       1.24      fvdl 				    (NFSERR_AUTHERR|AUTH_REJECTVERF);
   1203       1.24      fvdl 				nd->nd_procnum = NFSPROC_NOOP;
   1204       1.24      fvdl 				return (0);
   1205       1.24      fvdl 			}
   1206      1.131      yamt 			kauth_cred_hold(nuidp->nu_cr);
   1207      1.131      yamt 			nd->nd_cr = nuidp->nu_cr;
   1208       1.24      fvdl 			nd->nd_flag |= ND_KERBNICK;
   1209      1.131      yamt 		}
   1210       1.24      fvdl 	} else {
   1211       1.24      fvdl 		nd->nd_repstat = (NFSERR_AUTHERR | AUTH_REJECTCRED);
   1212       1.24      fvdl 		nd->nd_procnum = NFSPROC_NOOP;
   1213       1.24      fvdl 		return (0);
   1214       1.14   mycroft 	}
   1215       1.14   mycroft 
   1216       1.14   mycroft 	nd->nd_md = md;
   1217       1.14   mycroft 	nd->nd_dpos = dpos;
   1218      1.131      yamt 	KASSERT((nd->nd_cr == NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) != 0)
   1219      1.131      yamt 	     || (nd->nd_cr != NULL && (nfsd->nfsd_flag & NFSD_NEEDAUTH) == 0));
   1220       1.14   mycroft 	return (0);
   1221       1.14   mycroft nfsmout:
   1222      1.131      yamt errout:
   1223      1.131      yamt 	KASSERT(error != 0);
   1224      1.131      yamt 	if (nd->nd_cr != NULL) {
   1225      1.131      yamt 		kauth_cred_free(nd->nd_cr);
   1226      1.131      yamt 		nd->nd_cr = NULL;
   1227      1.131      yamt 	}
   1228       1.14   mycroft 	return (error);
   1229        1.1       cgd }
   1230        1.1       cgd 
   1231       1.24      fvdl int
   1232  1.170.2.2      yamt nfs_msg(struct lwp *l, const char *server, const char *msg)
   1233        1.1       cgd {
   1234        1.1       cgd 	tpr_t tpr;
   1235        1.1       cgd 
   1236  1.170.2.7      yamt #if 0 /* XXX nfs_timer can't block on proc_lock */
   1237      1.119  christos 	if (l)
   1238      1.119  christos 		tpr = tprintf_open(l->l_proc);
   1239        1.1       cgd 	else
   1240  1.170.2.7      yamt #endif
   1241        1.1       cgd 		tpr = NULL;
   1242        1.1       cgd 	tprintf(tpr, "nfs server %s: %s\n", server, msg);
   1243        1.1       cgd 	tprintf_close(tpr);
   1244       1.24      fvdl 	return (0);
   1245        1.1       cgd }
   1246        1.1       cgd 
   1247  1.170.2.2      yamt static struct pool nfs_srvdesc_pool;
   1248      1.165      yamt 
   1249      1.165      yamt void
   1250  1.170.2.2      yamt nfsdreq_init(void)
   1251      1.165      yamt {
   1252      1.165      yamt 
   1253  1.170.2.2      yamt 	pool_init(&nfs_srvdesc_pool, sizeof(struct nfsrv_descript),
   1254  1.170.2.2      yamt 	    0, 0, 0, "nfsrvdescpl", &pool_allocator_nointr, IPL_NONE);
   1255      1.165      yamt }
   1256      1.132      yamt 
   1257      1.132      yamt void
   1258  1.170.2.2      yamt nfsdreq_fini(void)
   1259      1.132      yamt {
   1260      1.132      yamt 
   1261  1.170.2.2      yamt 	pool_destroy(&nfs_srvdesc_pool);
   1262      1.132      yamt }
   1263      1.132      yamt 
   1264      1.132      yamt struct nfsrv_descript *
   1265      1.132      yamt nfsdreq_alloc(void)
   1266      1.132      yamt {
   1267      1.132      yamt 	struct nfsrv_descript *nd;
   1268      1.132      yamt 
   1269      1.132      yamt 	nd = pool_get(&nfs_srvdesc_pool, PR_WAITOK);
   1270      1.132      yamt 	nd->nd_cr = NULL;
   1271      1.132      yamt 	return nd;
   1272      1.132      yamt }
   1273      1.132      yamt 
   1274      1.132      yamt void
   1275      1.132      yamt nfsdreq_free(struct nfsrv_descript *nd)
   1276      1.132      yamt {
   1277      1.132      yamt 	kauth_cred_t cr;
   1278      1.132      yamt 
   1279      1.132      yamt 	cr = nd->nd_cr;
   1280      1.132      yamt 	if (cr != NULL) {
   1281      1.132      yamt 		kauth_cred_free(cr);
   1282      1.132      yamt 	}
   1283      1.132      yamt 	pool_put(&nfs_srvdesc_pool, nd);
   1284      1.132      yamt }
   1285  1.170.2.1      yamt 
   1286  1.170.2.1      yamt #if defined(NFS)
   1287  1.170.2.1      yamt void nfs_reqq_dump(void);
   1288  1.170.2.1      yamt 
   1289  1.170.2.1      yamt void
   1290  1.170.2.1      yamt nfs_reqq_dump(void)
   1291  1.170.2.1      yamt {
   1292  1.170.2.1      yamt 	struct nfsreq *r;
   1293  1.170.2.1      yamt 
   1294  1.170.2.1      yamt 	TAILQ_FOREACH(r, &nfs_reqq, r_chain) {
   1295  1.170.2.1      yamt 		printf("%p: proc=%" PRId32 ", lwp=%p, nmp=%p\n"
   1296  1.170.2.1      yamt 		    "\tflags=0x%x, rflags=0x%x\n"
   1297  1.170.2.1      yamt 		    "\treq=%p, rep=%p, xid=0x%" PRIx32 "\n"
   1298  1.170.2.1      yamt 		    "\tretry=%d, rexmit=%d, timer=%d, rtt=%d\n",
   1299  1.170.2.1      yamt 		    r, r->r_procnum, r->r_lwp, r->r_nmp,
   1300  1.170.2.1      yamt 		    r->r_flags, r->r_rflags,
   1301  1.170.2.1      yamt 		    r->r_mreq, r->r_mrep, r->r_xid,
   1302  1.170.2.1      yamt 		    r->r_retry, r->r_rexmit, r->r_timer, r->r_rtt);
   1303  1.170.2.1      yamt 	}
   1304  1.170.2.1      yamt }
   1305  1.170.2.1      yamt #endif /* defined(NFS) */
   1306