Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.140.4.1
      1  1.140.4.1       snj /*	$NetBSD: nfs_syscalls.c,v 1.140.4.1 2009/04/13 21:21:30 snj Exp $	*/
      2       1.11       cgd 
      3        1.1       cgd /*
      4       1.10   mycroft  * Copyright (c) 1989, 1993
      5       1.10   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.70       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.19      fvdl  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     35        1.1       cgd  */
     36       1.49     lukem 
     37       1.49     lukem #include <sys/cdefs.h>
     38  1.140.4.1       snj __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.140.4.1 2009/04/13 21:21:30 snj Exp $");
     39       1.28   thorpej 
     40       1.28   thorpej #include "fs_nfs.h"
     41       1.43     bjh21 #include "opt_nfs.h"
     42       1.30   thorpej #include "opt_nfsserver.h"
     43       1.31  jonathan #include "opt_iso.h"
     44       1.41      fvdl #include "opt_inet.h"
     45       1.39    tsarna #include "opt_compat_netbsd.h"
     46        1.1       cgd 
     47        1.7   mycroft #include <sys/param.h>
     48        1.7   mycroft #include <sys/systm.h>
     49        1.7   mycroft #include <sys/kernel.h>
     50        1.7   mycroft #include <sys/file.h>
     51        1.7   mycroft #include <sys/stat.h>
     52        1.7   mycroft #include <sys/vnode.h>
     53        1.7   mycroft #include <sys/mount.h>
     54        1.7   mycroft #include <sys/proc.h>
     55       1.10   mycroft #include <sys/uio.h>
     56        1.7   mycroft #include <sys/malloc.h>
     57      1.124      yamt #include <sys/kmem.h>
     58        1.7   mycroft #include <sys/buf.h>
     59        1.7   mycroft #include <sys/mbuf.h>
     60        1.7   mycroft #include <sys/socket.h>
     61        1.7   mycroft #include <sys/socketvar.h>
     62       1.39    tsarna #include <sys/signalvar.h>
     63        1.7   mycroft #include <sys/domain.h>
     64        1.7   mycroft #include <sys/protosw.h>
     65       1.10   mycroft #include <sys/namei.h>
     66       1.10   mycroft #include <sys/syslog.h>
     67       1.18  christos #include <sys/filedesc.h>
     68       1.39    tsarna #include <sys/kthread.h>
     69       1.92      elad #include <sys/kauth.h>
     70       1.13       cgd #include <sys/syscallargs.h>
     71       1.13       cgd 
     72        1.7   mycroft #include <netinet/in.h>
     73        1.7   mycroft #include <netinet/tcp.h>
     74       1.10   mycroft #ifdef ISO
     75       1.10   mycroft #include <netiso/iso.h>
     76       1.10   mycroft #endif
     77       1.19      fvdl #include <nfs/xdr_subs.h>
     78       1.10   mycroft #include <nfs/rpcv2.h>
     79       1.19      fvdl #include <nfs/nfsproto.h>
     80        1.7   mycroft #include <nfs/nfs.h>
     81       1.19      fvdl #include <nfs/nfsm_subs.h>
     82        1.7   mycroft #include <nfs/nfsrvcache.h>
     83       1.10   mycroft #include <nfs/nfsmount.h>
     84       1.10   mycroft #include <nfs/nfsnode.h>
     85       1.10   mycroft #include <nfs/nfsrtt.h>
     86       1.18  christos #include <nfs/nfs_var.h>
     87        1.1       cgd 
     88        1.1       cgd /* Global defs. */
     89       1.19      fvdl extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
     90       1.19      fvdl 						struct nfssvc_sock *,
     91       1.84  christos 						struct lwp *, struct mbuf **));
     92       1.19      fvdl extern int nfsrvw_procrastinate;
     93       1.51      matt 
     94       1.47       chs struct nfssvc_sock *nfs_udpsock;
     95       1.47       chs #ifdef ISO
     96       1.47       chs struct nfssvc_sock *nfs_cltpsock;
     97       1.47       chs #endif
     98       1.41      fvdl #ifdef INET6
     99       1.41      fvdl struct nfssvc_sock *nfs_udp6sock;
    100       1.41      fvdl #endif
    101       1.10   mycroft int nuidhash_max = NFS_MAXUIDHASH;
    102       1.18  christos #ifdef NFSSERVER
    103       1.10   mycroft static int nfs_numnfsd = 0;
    104       1.10   mycroft static struct nfsdrt nfsdrt;
    105       1.18  christos #endif
    106       1.51      matt 
    107       1.66      yamt #ifdef NFSSERVER
    108      1.111      yamt kmutex_t nfsd_lock;
    109       1.51      matt struct nfssvc_sockhead nfssvc_sockhead;
    110      1.111      yamt kcondvar_t nfsd_initcv;
    111       1.66      yamt struct nfssvc_sockhead nfssvc_sockpending;
    112       1.51      matt struct nfsdhead nfsd_head;
    113       1.66      yamt struct nfsdidlehead nfsd_idle_head;
    114       1.51      matt 
    115       1.51      matt int nfssvc_sockhead_flag;
    116       1.51      matt int nfsd_head_flag;
    117       1.66      yamt #endif
    118        1.1       cgd 
    119       1.24   thorpej #ifdef NFS
    120      1.124      yamt /*
    121      1.124      yamt  * locking order:
    122      1.124      yamt  *	nfs_iodlist_lock -> nid_lock -> nm_lock
    123      1.124      yamt  */
    124      1.123      yamt kmutex_t nfs_iodlist_lock;
    125      1.124      yamt struct nfs_iodlist nfs_iodlist_idle;
    126      1.124      yamt struct nfs_iodlist nfs_iodlist_all;
    127       1.40    tsarna int nfs_niothreads = -1; /* == "0, and has never been set" */
    128       1.23   thorpej #endif
    129       1.19      fvdl 
    130       1.18  christos #ifdef NFSSERVER
    131       1.65      yamt static struct nfssvc_sock *nfsrv_sockalloc __P((void));
    132       1.77      yamt static void nfsrv_sockfree __P((struct nfssvc_sock *));
    133       1.86      yamt static void nfsd_rt __P((int, struct nfsrv_descript *, int));
    134       1.18  christos #endif
    135       1.19      fvdl 
    136        1.1       cgd /*
    137        1.1       cgd  * NFS server system calls
    138        1.1       cgd  */
    139        1.1       cgd 
    140        1.1       cgd 
    141        1.1       cgd /*
    142       1.19      fvdl  * Nfs server pseudo system call for the nfsd's
    143       1.10   mycroft  * Based on the flag value it either:
    144       1.10   mycroft  * - adds a socket to the selection list
    145       1.10   mycroft  * - remains in the kernel as an nfsd
    146       1.10   mycroft  * - remains in the kernel as an nfsiod
    147        1.1       cgd  */
    148       1.18  christos int
    149      1.128       dsl sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
    150       1.15   thorpej {
    151      1.128       dsl 	/* {
    152       1.13       cgd 		syscallarg(int) flag;
    153      1.107  christos 		syscallarg(void *) argp;
    154      1.128       dsl 	} */
    155       1.23   thorpej 	int error;
    156       1.18  christos #ifdef NFSSERVER
    157      1.132        ad 	file_t *fp;
    158       1.10   mycroft 	struct mbuf *nam;
    159       1.10   mycroft 	struct nfsd_args nfsdarg;
    160       1.10   mycroft 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
    161       1.10   mycroft 	struct nfsd *nfsd;
    162       1.10   mycroft 	struct nfssvc_sock *slp;
    163       1.18  christos 	struct nfsuid *nuidp;
    164       1.18  christos #endif
    165        1.1       cgd 
    166        1.1       cgd 	/*
    167        1.1       cgd 	 * Must be super user
    168        1.1       cgd 	 */
    169      1.131      elad 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
    170      1.131      elad 	    KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
    171       1.71   thorpej 	if (error)
    172        1.1       cgd 		return (error);
    173       1.83   thorpej 
    174       1.83   thorpej 	/* Initialize NFS server / client shared data. */
    175       1.83   thorpej 	nfs_init();
    176       1.83   thorpej 
    177       1.67      yamt #ifdef NFSSERVER
    178      1.111      yamt 	mutex_enter(&nfsd_lock);
    179       1.12   mycroft 	while (nfssvc_sockhead_flag & SLP_INIT) {
    180      1.111      yamt 		cv_wait(&nfsd_initcv, &nfsd_lock);
    181       1.10   mycroft 	}
    182      1.111      yamt 	mutex_exit(&nfsd_lock);
    183       1.67      yamt #endif
    184       1.13       cgd 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    185       1.39    tsarna #if defined(NFS) && defined(COMPAT_14)
    186      1.118      yamt 		error = kpause("nfsbiod", true, 0, NULL); /* dummy impl */
    187       1.19      fvdl #else
    188       1.10   mycroft 		error = ENOSYS;
    189       1.19      fvdl #endif
    190       1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
    191       1.10   mycroft 		error = ENOSYS;
    192       1.13       cgd 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
    193       1.10   mycroft #ifndef NFSSERVER
    194       1.10   mycroft 		error = ENOSYS;
    195       1.19      fvdl #else
    196      1.107  christos 		error = copyin(SCARG(uap, argp), (void *)&nfsdarg,
    197       1.19      fvdl 		    sizeof(nfsdarg));
    198       1.18  christos 		if (error)
    199       1.10   mycroft 			return (error);
    200       1.35   thorpej 		/* getsock() will use the descriptor for us */
    201      1.137        ad 		if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
    202      1.137        ad 			return (EBADF);
    203      1.137        ad 		if (fp->f_type != DTYPE_SOCKET) {
    204      1.137        ad 			fd_putfile(nfsdarg.sock);
    205      1.137        ad 			return (ENOTSOCK);
    206      1.137        ad 		}
    207       1.19      fvdl 		if (error)
    208       1.10   mycroft 			return (error);
    209       1.10   mycroft 		/*
    210       1.10   mycroft 		 * Get the client address for connected sockets.
    211       1.10   mycroft 		 */
    212       1.10   mycroft 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    213       1.10   mycroft 			nam = (struct mbuf *)0;
    214       1.18  christos 		else {
    215       1.18  christos 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    216       1.19      fvdl 				MT_SONAME);
    217       1.35   thorpej 			if (error) {
    218      1.132        ad 				fd_putfile(nfsdarg.sock);
    219       1.18  christos 				return (error);
    220       1.35   thorpej 			}
    221       1.18  christos 		}
    222       1.10   mycroft 		error = nfssvc_addsock(fp, nam);
    223      1.132        ad 		fd_putfile(nfsdarg.sock);
    224       1.10   mycroft #endif /* !NFSSERVER */
    225       1.82      jmmv 	} else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
    226       1.82      jmmv #ifndef NFSSERVER
    227       1.82      jmmv 		error = ENOSYS;
    228       1.82      jmmv #else
    229       1.82      jmmv 		struct export_args *args;
    230       1.82      jmmv 		struct mountd_exports_list mel;
    231       1.82      jmmv 
    232       1.82      jmmv 		error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
    233       1.82      jmmv 		if (error != 0)
    234       1.82      jmmv 			return error;
    235       1.82      jmmv 
    236       1.82      jmmv 		args = (struct export_args *)malloc(mel.mel_nexports *
    237       1.82      jmmv 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
    238       1.82      jmmv 		error = copyin(mel.mel_exports, args, mel.mel_nexports *
    239       1.82      jmmv 		    sizeof(struct export_args));
    240       1.82      jmmv 		if (error != 0) {
    241       1.82      jmmv 			free(args, M_TEMP);
    242       1.82      jmmv 			return error;
    243       1.82      jmmv 		}
    244       1.82      jmmv 		mel.mel_exports = args;
    245       1.82      jmmv 
    246       1.84  christos 		error = mountd_set_exports_list(&mel, l);
    247       1.82      jmmv 
    248       1.82      jmmv 		free(args, M_TEMP);
    249       1.82      jmmv #endif /* !NFSSERVER */
    250       1.10   mycroft 	} else {
    251       1.10   mycroft #ifndef NFSSERVER
    252       1.10   mycroft 		error = ENOSYS;
    253       1.19      fvdl #else
    254      1.107  christos 		error = copyin(SCARG(uap, argp), (void *)nsd, sizeof (*nsd));
    255       1.18  christos 		if (error)
    256       1.10   mycroft 			return (error);
    257       1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    258       1.19      fvdl 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    259      1.127      yamt 		    (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
    260       1.19      fvdl 			slp = nfsd->nfsd_slp;
    261       1.10   mycroft 
    262       1.10   mycroft 			/*
    263       1.10   mycroft 			 * First check to see if another nfsd has already
    264       1.10   mycroft 			 * added this credential.
    265       1.10   mycroft 			 */
    266       1.92      elad 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
    267       1.57      yamt 			    nu_hash) {
    268       1.93      yamt 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    269       1.93      yamt 				    nsd->nsd_cr.cr_uid &&
    270       1.19      fvdl 				    (!nfsd->nfsd_nd->nd_nam2 ||
    271       1.19      fvdl 				     netaddr_match(NU_NETFAM(nuidp),
    272       1.19      fvdl 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    273       1.10   mycroft 					break;
    274       1.10   mycroft 			}
    275       1.19      fvdl 			if (nuidp) {
    276       1.93      yamt 			    kauth_cred_hold(nuidp->nu_cr);
    277       1.93      yamt 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    278       1.19      fvdl 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    279       1.19      fvdl 			} else {
    280       1.10   mycroft 			    /*
    281       1.10   mycroft 			     * Nope, so we will.
    282       1.10   mycroft 			     */
    283       1.10   mycroft 			    if (slp->ns_numuids < nuidhash_max) {
    284       1.10   mycroft 				slp->ns_numuids++;
    285      1.130      yamt 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
    286       1.10   mycroft 			    } else
    287       1.10   mycroft 				nuidp = (struct nfsuid *)0;
    288      1.127      yamt 			    if ((slp->ns_flags & SLP_VALID) == 0) {
    289       1.10   mycroft 				if (nuidp)
    290      1.130      yamt 				    kmem_free(nuidp, sizeof(*nuidp));
    291       1.10   mycroft 			    } else {
    292       1.10   mycroft 				if (nuidp == (struct nfsuid *)0) {
    293       1.57      yamt 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    294       1.12   mycroft 				    LIST_REMOVE(nuidp, nu_hash);
    295       1.12   mycroft 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    296       1.12   mycroft 					nu_lru);
    297       1.19      fvdl 				    if (nuidp->nu_flag & NU_NAM)
    298       1.19      fvdl 					m_freem(nuidp->nu_nam);
    299       1.10   mycroft 			        }
    300       1.19      fvdl 				nuidp->nu_flag = 0;
    301       1.98     pooka 				kauth_uucred_to_cred(nuidp->nu_cr,
    302       1.98     pooka 				    &nsd->nsd_cr);
    303       1.19      fvdl 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    304       1.95    kardel 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
    305       1.19      fvdl 				/*
    306       1.19      fvdl 				 * and save the session key in nu_key.
    307       1.19      fvdl 				 */
    308       1.32     perry 				memcpy(nuidp->nu_key, nsd->nsd_key,
    309       1.32     perry 				    sizeof(nsd->nsd_key));
    310       1.19      fvdl 				if (nfsd->nfsd_nd->nd_nam2) {
    311       1.19      fvdl 				    struct sockaddr_in *saddr;
    312       1.19      fvdl 
    313       1.19      fvdl 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    314       1.19      fvdl 					 struct sockaddr_in *);
    315       1.19      fvdl 				    switch (saddr->sin_family) {
    316       1.19      fvdl 				    case AF_INET:
    317       1.19      fvdl 					nuidp->nu_flag |= NU_INETADDR;
    318       1.19      fvdl 					nuidp->nu_inetaddr =
    319       1.19      fvdl 					     saddr->sin_addr.s_addr;
    320       1.19      fvdl 					break;
    321      1.140  christos #ifdef INET6
    322      1.140  christos 				    case AF_INET6:
    323      1.140  christos #endif
    324      1.140  christos #ifdef ISO
    325       1.19      fvdl 				    case AF_ISO:
    326      1.140  christos #endif
    327       1.19      fvdl 					nuidp->nu_flag |= NU_NAM;
    328       1.19      fvdl 					nuidp->nu_nam = m_copym(
    329       1.19      fvdl 					    nfsd->nfsd_nd->nd_nam2, 0,
    330       1.19      fvdl 					     M_COPYALL, M_WAIT);
    331       1.19      fvdl 					break;
    332      1.140  christos 				    default:
    333      1.140  christos 					return EAFNOSUPPORT;
    334       1.19      fvdl 				    };
    335       1.19      fvdl 				}
    336       1.12   mycroft 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    337       1.19      fvdl 					nu_lru);
    338       1.12   mycroft 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    339       1.19      fvdl 					nuidp, nu_hash);
    340       1.93      yamt 				kauth_cred_hold(nuidp->nu_cr);
    341       1.93      yamt 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    342       1.19      fvdl 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    343       1.10   mycroft 			    }
    344       1.10   mycroft 			}
    345       1.10   mycroft 		}
    346       1.13       cgd 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    347       1.13       cgd 		    (nfsd = nsd->nsd_nfsd))
    348       1.19      fvdl 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    349       1.53   thorpej 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    350       1.10   mycroft #endif /* !NFSSERVER */
    351       1.10   mycroft 	}
    352       1.10   mycroft 	if (error == EINTR || error == ERESTART)
    353       1.10   mycroft 		error = 0;
    354       1.10   mycroft 	return (error);
    355       1.10   mycroft }
    356       1.10   mycroft 
    357       1.10   mycroft #ifdef NFSSERVER
    358       1.54   thorpej MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
    359       1.54   thorpej 
    360       1.65      yamt static struct nfssvc_sock *
    361       1.65      yamt nfsrv_sockalloc()
    362       1.65      yamt {
    363       1.65      yamt 	struct nfssvc_sock *slp;
    364       1.78     perry 
    365      1.130      yamt 	slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
    366       1.65      yamt 	memset(slp, 0, sizeof (struct nfssvc_sock));
    367      1.135      yamt 	mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
    368      1.135      yamt 	mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
    369      1.111      yamt 	cv_init(&slp->ns_cv, "nfsdsock");
    370       1.65      yamt 	TAILQ_INIT(&slp->ns_uidlruhead);
    371       1.86      yamt 	LIST_INIT(&slp->ns_tq);
    372       1.86      yamt 	SIMPLEQ_INIT(&slp->ns_sendq);
    373      1.111      yamt 	mutex_enter(&nfsd_lock);
    374       1.65      yamt 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    375      1.111      yamt 	mutex_exit(&nfsd_lock);
    376       1.65      yamt 
    377       1.65      yamt 	return slp;
    378       1.65      yamt }
    379       1.65      yamt 
    380       1.77      yamt static void
    381       1.77      yamt nfsrv_sockfree(struct nfssvc_sock *slp)
    382       1.77      yamt {
    383       1.77      yamt 
    384       1.77      yamt 	KASSERT(slp->ns_so == NULL);
    385       1.77      yamt 	KASSERT(slp->ns_fp == NULL);
    386      1.127      yamt 	KASSERT((slp->ns_flags & SLP_VALID) == 0);
    387      1.111      yamt 	mutex_destroy(&slp->ns_lock);
    388      1.127      yamt 	mutex_destroy(&slp->ns_alock);
    389      1.111      yamt 	cv_destroy(&slp->ns_cv);
    390      1.130      yamt 	kmem_free(slp, sizeof(*slp));
    391       1.77      yamt }
    392       1.77      yamt 
    393       1.10   mycroft /*
    394       1.10   mycroft  * Adds a socket to the list for servicing by nfsds.
    395       1.10   mycroft  */
    396       1.18  christos int
    397       1.10   mycroft nfssvc_addsock(fp, mynam)
    398      1.132        ad 	file_t *fp;
    399       1.10   mycroft 	struct mbuf *mynam;
    400       1.10   mycroft {
    401       1.38  augustss 	int siz;
    402       1.38  augustss 	struct nfssvc_sock *slp;
    403       1.38  augustss 	struct socket *so;
    404       1.10   mycroft 	struct nfssvc_sock *tslp;
    405      1.134        ad 	int error;
    406      1.138    plunky 	int val;
    407       1.10   mycroft 
    408        1.1       cgd 	so = (struct socket *)fp->f_data;
    409       1.10   mycroft 	tslp = (struct nfssvc_sock *)0;
    410       1.10   mycroft 	/*
    411       1.10   mycroft 	 * Add it to the list, as required.
    412       1.10   mycroft 	 */
    413       1.10   mycroft 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    414       1.41      fvdl #ifdef INET6
    415       1.41      fvdl 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    416       1.41      fvdl 			tslp = nfs_udp6sock;
    417       1.41      fvdl 		else
    418       1.41      fvdl #endif
    419       1.10   mycroft 		tslp = nfs_udpsock;
    420      1.127      yamt 		if (tslp->ns_flags & SLP_VALID) {
    421       1.10   mycroft 			m_freem(mynam);
    422       1.10   mycroft 			return (EPERM);
    423       1.10   mycroft 		}
    424       1.10   mycroft #ifdef ISO
    425       1.10   mycroft 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
    426       1.10   mycroft 		tslp = nfs_cltpsock;
    427      1.127      yamt 		if (tslp->ns_flags & SLP_VALID) {
    428       1.10   mycroft 			m_freem(mynam);
    429       1.10   mycroft 			return (EPERM);
    430       1.10   mycroft 		}
    431       1.10   mycroft #endif /* ISO */
    432       1.10   mycroft 	}
    433       1.10   mycroft 	if (so->so_type == SOCK_STREAM)
    434       1.19      fvdl 		siz = NFS_MAXPACKET + sizeof (u_long);
    435       1.10   mycroft 	else
    436        1.1       cgd 		siz = NFS_MAXPACKET;
    437      1.134        ad 	solock(so);
    438       1.78     perry 	error = soreserve(so, siz, siz);
    439      1.134        ad 	sounlock(so);
    440       1.19      fvdl 	if (error) {
    441       1.10   mycroft 		m_freem(mynam);
    442       1.10   mycroft 		return (error);
    443       1.10   mycroft 	}
    444        1.1       cgd 
    445        1.1       cgd 	/*
    446        1.1       cgd 	 * Set protocol specific options { for now TCP only } and
    447        1.1       cgd 	 * reserve some space. For datagram sockets, this can get called
    448        1.1       cgd 	 * repeatedly for the same socket, but that isn't harmful.
    449        1.1       cgd 	 */
    450       1.10   mycroft 	if (so->so_type == SOCK_STREAM) {
    451      1.138    plunky 		val = 1;
    452      1.138    plunky 		so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
    453      1.138    plunky 		    sizeof(val));
    454        1.1       cgd 	}
    455       1.41      fvdl 	if ((so->so_proto->pr_domain->dom_family == AF_INET
    456       1.41      fvdl #ifdef INET6
    457       1.41      fvdl 	    || so->so_proto->pr_domain->dom_family == AF_INET6
    458       1.41      fvdl #endif
    459       1.41      fvdl 	    ) &&
    460       1.10   mycroft 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    461      1.138    plunky 		val = 1;
    462      1.138    plunky 		so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
    463      1.138    plunky 		    sizeof(val));
    464        1.1       cgd 	}
    465      1.134        ad 	solock(so);
    466        1.1       cgd 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    467        1.1       cgd 	so->so_rcv.sb_timeo = 0;
    468        1.1       cgd 	so->so_snd.sb_flags &= ~SB_NOINTR;
    469        1.1       cgd 	so->so_snd.sb_timeo = 0;
    470      1.134        ad 	sounlock(so);
    471      1.111      yamt 	if (tslp) {
    472       1.10   mycroft 		slp = tslp;
    473      1.111      yamt 	} else {
    474       1.65      yamt 		slp = nfsrv_sockalloc();
    475       1.10   mycroft 	}
    476       1.10   mycroft 	slp->ns_so = so;
    477       1.10   mycroft 	slp->ns_nam = mynam;
    478      1.127      yamt 	mutex_enter(&fp->f_lock);
    479       1.10   mycroft 	fp->f_count++;
    480      1.127      yamt 	mutex_exit(&fp->f_lock);
    481       1.10   mycroft 	slp->ns_fp = fp;
    482      1.127      yamt 	slp->ns_flags = SLP_VALID;
    483      1.127      yamt 	slp->ns_aflags = SLP_A_NEEDQ;
    484      1.127      yamt 	slp->ns_gflags = 0;
    485      1.127      yamt 	slp->ns_sflags = 0;
    486      1.134        ad 	solock(so);
    487      1.107  christos 	so->so_upcallarg = (void *)slp;
    488      1.127      yamt 	so->so_upcall = nfsrv_soupcall;
    489       1.29      matt 	so->so_rcv.sb_flags |= SB_UPCALL;
    490      1.134        ad 	sounlock(so);
    491       1.10   mycroft 	nfsrv_wakenfsd(slp);
    492       1.10   mycroft 	return (0);
    493       1.10   mycroft }
    494        1.1       cgd 
    495       1.10   mycroft /*
    496       1.10   mycroft  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    497       1.10   mycroft  * until it is killed by a signal.
    498       1.10   mycroft  */
    499       1.18  christos int
    500       1.53   thorpej nfssvc_nfsd(nsd, argp, l)
    501       1.10   mycroft 	struct nfsd_srvargs *nsd;
    502      1.107  christos 	void *argp;
    503       1.53   thorpej 	struct lwp *l;
    504       1.10   mycroft {
    505       1.95    kardel 	struct timeval tv;
    506       1.38  augustss 	struct mbuf *m;
    507       1.38  augustss 	struct nfssvc_sock *slp;
    508       1.19      fvdl 	struct nfsd *nfsd = nsd->nsd_nfsd;
    509       1.19      fvdl 	struct nfsrv_descript *nd = NULL;
    510       1.19      fvdl 	struct mbuf *mreq;
    511       1.19      fvdl 	u_quad_t cur_usec;
    512      1.111      yamt 	int error = 0, cacherep, siz, sotype, writes_todo;
    513       1.53   thorpej 	struct proc *p = l->l_proc;
    514      1.127      yamt 	bool doreinit;
    515       1.19      fvdl 
    516       1.19      fvdl #ifndef nolint
    517       1.19      fvdl 	cacherep = RC_DOIT;
    518       1.19      fvdl 	writes_todo = 0;
    519       1.19      fvdl #endif
    520      1.117        ad 	uvm_lwp_hold(l);
    521      1.111      yamt 	if (nfsd == NULL) {
    522      1.130      yamt 		nsd->nsd_nfsd = nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
    523      1.111      yamt 		memset(nfsd, 0, sizeof (struct nfsd));
    524      1.111      yamt 		cv_init(&nfsd->nfsd_cv, "nfsd");
    525       1.19      fvdl 		nfsd->nfsd_procp = p;
    526      1.111      yamt 		mutex_enter(&nfsd_lock);
    527      1.127      yamt 		while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
    528      1.127      yamt 			KASSERT(nfs_numnfsd == 0);
    529      1.127      yamt 			cv_wait(&nfsd_initcv, &nfsd_lock);
    530      1.127      yamt 		}
    531       1.19      fvdl 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    532       1.10   mycroft 		nfs_numnfsd++;
    533      1.111      yamt 		mutex_exit(&nfsd_lock);
    534       1.10   mycroft 	}
    535        1.1       cgd 	/*
    536       1.10   mycroft 	 * Loop getting rpc requests until SIGKILL.
    537        1.1       cgd 	 */
    538        1.1       cgd 	for (;;) {
    539      1.127      yamt 		bool dummy;
    540      1.127      yamt 
    541      1.113      yamt 		if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
    542      1.113      yamt 		    != 0) {
    543      1.113      yamt 			preempt();
    544      1.113      yamt 		}
    545       1.85      yamt 		if (nfsd->nfsd_slp == NULL) {
    546      1.111      yamt 			mutex_enter(&nfsd_lock);
    547       1.85      yamt 			while (nfsd->nfsd_slp == NULL &&
    548       1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    549       1.66      yamt 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    550       1.66      yamt 				    nfsd_idle);
    551      1.111      yamt 				error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
    552       1.66      yamt 				if (error) {
    553       1.66      yamt 					slp = nfsd->nfsd_slp;
    554       1.66      yamt 					nfsd->nfsd_slp = NULL;
    555       1.66      yamt 					if (!slp)
    556       1.66      yamt 						SLIST_REMOVE(&nfsd_idle_head,
    557       1.66      yamt 						    nfsd, nfsd, nfsd_idle);
    558      1.111      yamt 					mutex_exit(&nfsd_lock);
    559       1.66      yamt 					if (slp) {
    560       1.66      yamt 						nfsrv_wakenfsd(slp);
    561       1.66      yamt 						nfsrv_slpderef(slp);
    562       1.66      yamt 					}
    563       1.10   mycroft 					goto done;
    564       1.66      yamt 				}
    565       1.10   mycroft 			}
    566       1.85      yamt 			if (nfsd->nfsd_slp == NULL &&
    567       1.12   mycroft 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    568       1.66      yamt 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    569       1.66      yamt 				if (slp) {
    570      1.127      yamt 					KASSERT((slp->ns_gflags & SLP_G_DOREC)
    571      1.121      yamt 					    != 0);
    572       1.66      yamt 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    573       1.66      yamt 					    ns_pending);
    574      1.127      yamt 					slp->ns_gflags &= ~SLP_G_DOREC;
    575       1.66      yamt 					slp->ns_sref++;
    576       1.66      yamt 					nfsd->nfsd_slp = slp;
    577       1.66      yamt 				} else
    578       1.12   mycroft 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    579       1.10   mycroft 			}
    580      1.116      yamt 			KASSERT(nfsd->nfsd_slp == NULL ||
    581      1.116      yamt 			    nfsd->nfsd_slp->ns_sref > 0);
    582      1.111      yamt 			mutex_exit(&nfsd_lock);
    583       1.85      yamt 			if ((slp = nfsd->nfsd_slp) == NULL)
    584       1.10   mycroft 				continue;
    585      1.127      yamt 			if (slp->ns_flags & SLP_VALID) {
    586      1.127      yamt 				bool more;
    587      1.127      yamt 
    588      1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
    589      1.127      yamt 					nfsrv_rcv(slp);
    590       1.10   mycroft 				}
    591      1.127      yamt 				if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
    592      1.119      yamt 					nfsrv_zapsock(slp);
    593      1.119      yamt 				}
    594      1.127      yamt 				error = nfsrv_dorec(slp, nfsd, &nd, &more);
    595       1.95    kardel 				getmicrotime(&tv);
    596       1.95    kardel 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    597       1.95    kardel 					(u_quad_t)tv.tv_usec;
    598      1.127      yamt 				writes_todo = 0;
    599      1.127      yamt 				if (error) {
    600      1.127      yamt 					struct nfsrv_descript *nd2;
    601      1.127      yamt 
    602      1.127      yamt 					mutex_enter(&nfsd_lock);
    603      1.127      yamt 					nd2 = LIST_FIRST(&slp->ns_tq);
    604      1.127      yamt 					if (nd2 != NULL &&
    605      1.127      yamt 					    nd2->nd_time <= cur_usec) {
    606      1.127      yamt 						error = 0;
    607      1.127      yamt 						cacherep = RC_DOIT;
    608      1.127      yamt 						writes_todo = 1;
    609      1.127      yamt 					}
    610      1.127      yamt 					mutex_exit(&nfsd_lock);
    611      1.127      yamt 				}
    612      1.127      yamt 				if (error == 0 && more) {
    613       1.85      yamt 					nfsrv_wakenfsd(slp);
    614       1.85      yamt 				}
    615        1.1       cgd 			}
    616       1.10   mycroft 		} else {
    617       1.10   mycroft 			error = 0;
    618       1.19      fvdl 			slp = nfsd->nfsd_slp;
    619       1.10   mycroft 		}
    620      1.116      yamt 		KASSERT(slp != NULL);
    621      1.116      yamt 		KASSERT(nfsd->nfsd_slp == slp);
    622      1.127      yamt 		if (error || (slp->ns_flags & SLP_VALID) == 0) {
    623       1.19      fvdl 			if (nd) {
    624       1.93      yamt 				nfsdreq_free(nd);
    625       1.19      fvdl 				nd = NULL;
    626       1.19      fvdl 			}
    627       1.85      yamt 			nfsd->nfsd_slp = NULL;
    628       1.10   mycroft 			nfsrv_slpderef(slp);
    629        1.1       cgd 			continue;
    630        1.1       cgd 		}
    631       1.86      yamt 		sotype = slp->ns_so->so_type;
    632       1.19      fvdl 		if (nd) {
    633       1.95    kardel 			getmicrotime(&nd->nd_starttime);
    634       1.63      yamt 			if (nd->nd_nam2)
    635       1.63      yamt 				nd->nd_nam = nd->nd_nam2;
    636       1.63      yamt 			else
    637       1.63      yamt 				nd->nd_nam = slp->ns_nam;
    638       1.63      yamt 
    639       1.63      yamt 			/*
    640       1.63      yamt 			 * Check to see if authorization is needed.
    641       1.63      yamt 			 */
    642       1.63      yamt 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    643       1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    644       1.63      yamt 				nsd->nsd_haddr = mtod(nd->nd_nam,
    645       1.63      yamt 				    struct sockaddr_in *)->sin_addr.s_addr;
    646       1.63      yamt 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    647       1.63      yamt 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    648       1.63      yamt 				if (!copyout(nfsd->nfsd_authstr,
    649       1.63      yamt 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    650       1.63      yamt 				    !copyout(nfsd->nfsd_verfstr,
    651       1.63      yamt 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    652       1.63      yamt 				    !copyout(nsd, argp, sizeof (*nsd))) {
    653      1.117        ad 					uvm_lwp_rele(l);
    654       1.63      yamt 					return (ENEEDAUTH);
    655       1.63      yamt 				}
    656       1.63      yamt 				cacherep = RC_DROPIT;
    657       1.10   mycroft 			} else
    658       1.63      yamt 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    659       1.63      yamt 
    660      1.100      yamt 			if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    661       1.63      yamt 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    662       1.10   mycroft 				nd->nd_procnum = NFSPROC_NOOP;
    663       1.63      yamt 				nd->nd_repstat =
    664       1.63      yamt 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    665       1.10   mycroft 				cacherep = RC_DOIT;
    666       1.63      yamt 			}
    667       1.10   mycroft 		}
    668       1.10   mycroft 
    669       1.19      fvdl 		/*
    670       1.19      fvdl 		 * Loop to get all the write rpc relies that have been
    671       1.19      fvdl 		 * gathered together.
    672       1.19      fvdl 		 */
    673       1.19      fvdl 		do {
    674       1.63      yamt 			switch (cacherep) {
    675       1.63      yamt 			case RC_DOIT:
    676       1.80      onoe 				mreq = NULL;
    677       1.88      yamt 				netexport_rdlock();
    678       1.90  christos 				if (writes_todo || nd == NULL ||
    679       1.90  christos 				     (!(nd->nd_flag & ND_NFSV3) &&
    680       1.63      yamt 				     nd->nd_procnum == NFSPROC_WRITE &&
    681      1.100      yamt 				     nfsrvw_procrastinate > 0))
    682       1.63      yamt 					error = nfsrv_writegather(&nd, slp,
    683       1.84  christos 					    l, &mreq);
    684       1.63      yamt 				else
    685       1.63      yamt 					error =
    686       1.63      yamt 					    (*(nfsrv3_procs[nd->nd_procnum]))
    687       1.84  christos 					    (nd, slp, l, &mreq);
    688       1.88      yamt 				netexport_rdunlock();
    689       1.80      onoe 				if (mreq == NULL) {
    690       1.80      onoe 					if (nd != NULL) {
    691       1.80      onoe 						if (nd->nd_nam2)
    692       1.80      onoe 							m_free(nd->nd_nam2);
    693       1.80      onoe 					}
    694       1.63      yamt 					break;
    695       1.80      onoe 				}
    696       1.63      yamt 				if (error) {
    697      1.100      yamt 					nfsstats.srv_errs++;
    698      1.106   thorpej 					nfsrv_updatecache(nd, false, mreq);
    699       1.63      yamt 					if (nd->nd_nam2)
    700       1.63      yamt 						m_freem(nd->nd_nam2);
    701       1.63      yamt 					break;
    702       1.63      yamt 				}
    703       1.63      yamt 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    704      1.106   thorpej 				nfsrv_updatecache(nd, true, mreq);
    705       1.63      yamt 				nd->nd_mrep = (struct mbuf *)0;
    706       1.63      yamt 			case RC_REPLY:
    707       1.63      yamt 				m = mreq;
    708       1.63      yamt 				siz = 0;
    709       1.63      yamt 				while (m) {
    710       1.63      yamt 					siz += m->m_len;
    711       1.63      yamt 					m = m->m_next;
    712       1.63      yamt 				}
    713       1.63      yamt 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    714       1.63      yamt 					printf("mbuf siz=%d\n",siz);
    715       1.63      yamt 					panic("Bad nfs svc reply");
    716       1.63      yamt 				}
    717       1.63      yamt 				m = mreq;
    718       1.63      yamt 				m->m_pkthdr.len = siz;
    719       1.63      yamt 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    720       1.63      yamt 				/*
    721       1.63      yamt 				 * For stream protocols, prepend a Sun RPC
    722       1.63      yamt 				 * Record Mark.
    723       1.63      yamt 				 */
    724       1.63      yamt 				if (sotype == SOCK_STREAM) {
    725       1.63      yamt 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    726       1.63      yamt 					*mtod(m, u_int32_t *) =
    727       1.63      yamt 					    htonl(0x80000000 | siz);
    728       1.63      yamt 				}
    729       1.86      yamt 				nd->nd_mreq = m;
    730       1.86      yamt 				if (nfsrtton) {
    731       1.86      yamt 					nfsd_rt(slp->ns_so->so_type, nd,
    732       1.86      yamt 					    cacherep);
    733       1.63      yamt 				}
    734       1.86      yamt 				error = nfsdsock_sendreply(slp, nd);
    735       1.86      yamt 				nd = NULL;
    736       1.63      yamt 				if (error == EPIPE)
    737       1.63      yamt 					nfsrv_zapsock(slp);
    738       1.63      yamt 				if (error == EINTR || error == ERESTART) {
    739      1.114      yamt 					nfsd->nfsd_slp = NULL;
    740       1.63      yamt 					nfsrv_slpderef(slp);
    741       1.63      yamt 					goto done;
    742       1.63      yamt 				}
    743       1.19      fvdl 				break;
    744       1.63      yamt 			case RC_DROPIT:
    745       1.63      yamt 				if (nfsrtton)
    746       1.63      yamt 					nfsd_rt(sotype, nd, cacherep);
    747       1.63      yamt 				m_freem(nd->nd_mrep);
    748       1.63      yamt 				m_freem(nd->nd_nam2);
    749        1.1       cgd 				break;
    750        1.1       cgd 			}
    751       1.63      yamt 			if (nd) {
    752       1.93      yamt 				nfsdreq_free(nd);
    753       1.63      yamt 				nd = NULL;
    754       1.63      yamt 			}
    755       1.63      yamt 
    756        1.1       cgd 			/*
    757       1.63      yamt 			 * Check to see if there are outstanding writes that
    758       1.63      yamt 			 * need to be serviced.
    759        1.1       cgd 			 */
    760       1.95    kardel 			getmicrotime(&tv);
    761       1.95    kardel 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    762       1.95    kardel 			    (u_quad_t)tv.tv_usec;
    763  1.140.4.1       snj 			mutex_enter(&nfsd_lock);
    764       1.63      yamt 			if (LIST_FIRST(&slp->ns_tq) &&
    765       1.63      yamt 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    766       1.63      yamt 				cacherep = RC_DOIT;
    767       1.63      yamt 				writes_todo = 1;
    768       1.63      yamt 			} else
    769       1.63      yamt 				writes_todo = 0;
    770  1.140.4.1       snj 			mutex_exit(&nfsd_lock);
    771       1.19      fvdl 		} while (writes_todo);
    772      1.127      yamt 		if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
    773       1.19      fvdl 			nfsd->nfsd_slp = NULL;
    774       1.10   mycroft 			nfsrv_slpderef(slp);
    775       1.10   mycroft 		}
    776        1.1       cgd 	}
    777       1.10   mycroft done:
    778      1.111      yamt 	mutex_enter(&nfsd_lock);
    779       1.19      fvdl 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    780      1.127      yamt 	doreinit = --nfs_numnfsd == 0;
    781      1.127      yamt 	if (doreinit)
    782      1.127      yamt 		nfssvc_sockhead_flag |= SLP_INIT;
    783      1.111      yamt 	mutex_exit(&nfsd_lock);
    784      1.111      yamt 	cv_destroy(&nfsd->nfsd_cv);
    785      1.130      yamt 	kmem_free(nfsd, sizeof(*nfsd));
    786      1.111      yamt 	nsd->nsd_nfsd = NULL;
    787      1.127      yamt 	if (doreinit)
    788      1.106   thorpej 		nfsrv_init(true);	/* Reinitialize everything */
    789      1.117        ad 	uvm_lwp_rele(l);
    790        1.1       cgd 	return (error);
    791        1.1       cgd }
    792        1.1       cgd 
    793       1.10   mycroft /*
    794       1.10   mycroft  * Shut down a socket associated with an nfssvc_sock structure.
    795       1.10   mycroft  * Should be called with the send lock set, if required.
    796       1.10   mycroft  * The trick here is to increment the sref at the start, so that the nfsds
    797       1.10   mycroft  * will stop using it and clear ns_flag at the end so that it will not be
    798       1.10   mycroft  * reassigned during cleanup.
    799       1.73      yamt  *
    800       1.73      yamt  * called at splsoftnet.
    801       1.10   mycroft  */
    802       1.18  christos void
    803       1.10   mycroft nfsrv_zapsock(slp)
    804       1.38  augustss 	struct nfssvc_sock *slp;
    805       1.10   mycroft {
    806       1.38  augustss 	struct nfsuid *nuidp, *nnuidp;
    807      1.127      yamt 	struct nfsrv_descript *nwp;
    808       1.10   mycroft 	struct socket *so;
    809      1.104      yamt 	struct mbuf *m;
    810       1.10   mycroft 
    811       1.85      yamt 	if (nfsdsock_drain(slp)) {
    812       1.74      yamt 		return;
    813       1.74      yamt 	}
    814      1.111      yamt 	mutex_enter(&nfsd_lock);
    815      1.127      yamt 	if (slp->ns_gflags & SLP_G_DOREC) {
    816       1.73      yamt 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    817      1.127      yamt 		slp->ns_gflags &= ~SLP_G_DOREC;
    818       1.73      yamt 	}
    819      1.111      yamt 	mutex_exit(&nfsd_lock);
    820       1.77      yamt 
    821       1.77      yamt 	so = slp->ns_so;
    822       1.77      yamt 	KASSERT(so != NULL);
    823      1.134        ad 	solock(so);
    824       1.77      yamt 	so->so_upcall = NULL;
    825       1.77      yamt 	so->so_upcallarg = NULL;
    826       1.77      yamt 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    827       1.77      yamt 	soshutdown(so, SHUT_RDWR);
    828      1.134        ad 	sounlock(so);
    829       1.77      yamt 
    830       1.77      yamt 	m_freem(slp->ns_raw);
    831      1.104      yamt 	m = slp->ns_rec;
    832      1.104      yamt 	while (m != NULL) {
    833      1.104      yamt 		struct mbuf *n;
    834      1.104      yamt 
    835      1.104      yamt 		n = m->m_nextpkt;
    836      1.104      yamt 		m_freem(m);
    837      1.104      yamt 		m = n;
    838      1.104      yamt 	}
    839  1.140.4.1       snj 	/* XXX what about freeing ns_frag ? */
    840       1.77      yamt 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    841       1.77      yamt 	    nuidp = nnuidp) {
    842       1.77      yamt 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    843       1.77      yamt 		LIST_REMOVE(nuidp, nu_hash);
    844       1.77      yamt 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    845       1.77      yamt 		if (nuidp->nu_flag & NU_NAM)
    846       1.77      yamt 			m_freem(nuidp->nu_nam);
    847      1.130      yamt 		kmem_free(nuidp, sizeof(*nuidp));
    848       1.77      yamt 	}
    849      1.127      yamt 	mutex_enter(&nfsd_lock);
    850      1.127      yamt 	while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
    851       1.77      yamt 		LIST_REMOVE(nwp, nd_tq);
    852      1.127      yamt 		mutex_exit(&nfsd_lock);
    853       1.93      yamt 		nfsdreq_free(nwp);
    854      1.127      yamt 		mutex_enter(&nfsd_lock);
    855       1.10   mycroft 	}
    856      1.127      yamt 	mutex_exit(&nfsd_lock);
    857       1.10   mycroft }
    858       1.10   mycroft 
    859       1.10   mycroft /*
    860       1.10   mycroft  * Derefence a server socket structure. If it has no more references and
    861       1.10   mycroft  * is no longer valid, you can throw it away.
    862       1.10   mycroft  */
    863       1.10   mycroft void
    864       1.10   mycroft nfsrv_slpderef(slp)
    865       1.38  augustss 	struct nfssvc_sock *slp;
    866       1.10   mycroft {
    867      1.116      yamt 	uint32_t ref;
    868       1.66      yamt 
    869      1.116      yamt 	mutex_enter(&nfsd_lock);
    870      1.116      yamt 	KASSERT(slp->ns_sref > 0);
    871      1.116      yamt 	ref = --slp->ns_sref;
    872      1.127      yamt 	if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
    873      1.132        ad 		file_t *fp;
    874      1.111      yamt 
    875      1.127      yamt 		KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
    876       1.12   mycroft 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    877      1.111      yamt 		mutex_exit(&nfsd_lock);
    878       1.85      yamt 
    879       1.85      yamt 		fp = slp->ns_fp;
    880       1.85      yamt 		if (fp != NULL) {
    881       1.85      yamt 			slp->ns_fp = NULL;
    882       1.85      yamt 			KASSERT(fp != NULL);
    883       1.85      yamt 			KASSERT(fp->f_data == slp->ns_so);
    884      1.132        ad 			KASSERT(fp->f_count > 0);
    885      1.132        ad 			closef(fp);
    886       1.85      yamt 			slp->ns_so = NULL;
    887       1.85      yamt 		}
    888  1.140.4.1       snj 
    889  1.140.4.1       snj 		if (slp->ns_nam)
    890  1.140.4.1       snj 			m_free(slp->ns_nam);
    891       1.77      yamt 		nfsrv_sockfree(slp);
    892  1.140.4.1       snj 	} else
    893  1.140.4.1       snj 		mutex_exit(&nfsd_lock);
    894       1.10   mycroft }
    895       1.10   mycroft 
    896       1.10   mycroft /*
    897       1.10   mycroft  * Initialize the data structures for the server.
    898       1.10   mycroft  * Handshake with any new nfsds starting up to avoid any chance of
    899       1.10   mycroft  * corruption.
    900       1.10   mycroft  */
    901       1.10   mycroft void
    902       1.10   mycroft nfsrv_init(terminating)
    903       1.10   mycroft 	int terminating;
    904       1.10   mycroft {
    905       1.66      yamt 	struct nfssvc_sock *slp;
    906       1.10   mycroft 
    907      1.111      yamt 	if (!terminating) {
    908      1.135      yamt 		mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
    909      1.111      yamt 		cv_init(&nfsd_initcv, "nfsdinit");
    910      1.111      yamt 	}
    911      1.111      yamt 
    912      1.111      yamt 	mutex_enter(&nfsd_lock);
    913      1.127      yamt 	if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
    914       1.10   mycroft 		panic("nfsd init");
    915       1.12   mycroft 	nfssvc_sockhead_flag |= SLP_INIT;
    916       1.66      yamt 
    917       1.10   mycroft 	if (terminating) {
    918      1.114      yamt 		KASSERT(SLIST_EMPTY(&nfsd_idle_head));
    919      1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfsd_head));
    920       1.66      yamt 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    921      1.111      yamt 			mutex_exit(&nfsd_lock);
    922       1.85      yamt 			KASSERT(slp->ns_sref == 0);
    923       1.85      yamt 			slp->ns_sref++;
    924       1.85      yamt 			nfsrv_zapsock(slp);
    925       1.85      yamt 			nfsrv_slpderef(slp);
    926      1.111      yamt 			mutex_enter(&nfsd_lock);
    927       1.10   mycroft 		}
    928      1.114      yamt 		KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
    929      1.111      yamt 		mutex_exit(&nfsd_lock);
    930       1.10   mycroft 		nfsrv_cleancache();	/* And clear out server cache */
    931       1.66      yamt 	} else {
    932      1.111      yamt 		mutex_exit(&nfsd_lock);
    933       1.26      fvdl 		nfs_pub.np_valid = 0;
    934       1.66      yamt 	}
    935       1.12   mycroft 
    936       1.12   mycroft 	TAILQ_INIT(&nfssvc_sockhead);
    937       1.66      yamt 	TAILQ_INIT(&nfssvc_sockpending);
    938       1.12   mycroft 
    939       1.12   mycroft 	TAILQ_INIT(&nfsd_head);
    940       1.66      yamt 	SLIST_INIT(&nfsd_idle_head);
    941       1.12   mycroft 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    942       1.12   mycroft 
    943       1.65      yamt 	nfs_udpsock = nfsrv_sockalloc();
    944       1.41      fvdl 
    945       1.41      fvdl #ifdef INET6
    946       1.65      yamt 	nfs_udp6sock = nfsrv_sockalloc();
    947       1.41      fvdl #endif
    948       1.12   mycroft 
    949       1.47       chs #ifdef ISO
    950       1.65      yamt 	nfs_cltpsock = nfsrv_sockalloc();
    951       1.47       chs #endif
    952       1.66      yamt 
    953      1.111      yamt 	mutex_enter(&nfsd_lock);
    954      1.111      yamt 	nfssvc_sockhead_flag &= ~SLP_INIT;
    955      1.111      yamt 	cv_broadcast(&nfsd_initcv);
    956      1.111      yamt 	mutex_exit(&nfsd_lock);
    957       1.10   mycroft }
    958       1.10   mycroft 
    959       1.10   mycroft /*
    960       1.10   mycroft  * Add entries to the server monitor log.
    961       1.10   mycroft  */
    962       1.10   mycroft static void
    963       1.19      fvdl nfsd_rt(sotype, nd, cacherep)
    964       1.10   mycroft 	int sotype;
    965       1.38  augustss 	struct nfsrv_descript *nd;
    966       1.10   mycroft 	int cacherep;
    967       1.10   mycroft {
    968       1.95    kardel 	struct timeval tv;
    969       1.38  augustss 	struct drt *rt;
    970       1.10   mycroft 
    971       1.10   mycroft 	rt = &nfsdrt.drt[nfsdrt.pos];
    972       1.10   mycroft 	if (cacherep == RC_DOIT)
    973       1.10   mycroft 		rt->flag = 0;
    974       1.10   mycroft 	else if (cacherep == RC_REPLY)
    975       1.10   mycroft 		rt->flag = DRT_CACHEREPLY;
    976       1.10   mycroft 	else
    977       1.10   mycroft 		rt->flag = DRT_CACHEDROP;
    978       1.10   mycroft 	if (sotype == SOCK_STREAM)
    979       1.10   mycroft 		rt->flag |= DRT_TCP;
    980      1.102      yamt 	if (nd->nd_flag & ND_NFSV3)
    981       1.19      fvdl 		rt->flag |= DRT_NFSV3;
    982       1.10   mycroft 	rt->proc = nd->nd_procnum;
    983       1.19      fvdl 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    984       1.19      fvdl 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    985       1.10   mycroft 	else
    986       1.19      fvdl 	    rt->ipadr = INADDR_ANY;
    987       1.95    kardel 	getmicrotime(&tv);
    988       1.95    kardel 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    989       1.95    kardel 		(tv.tv_usec - nd->nd_starttime.tv_usec);
    990       1.95    kardel 	rt->tstamp = tv;
    991       1.10   mycroft 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    992       1.10   mycroft }
    993        1.3     glass #endif /* NFSSERVER */
    994        1.3     glass 
    995       1.24   thorpej #ifdef NFS
    996       1.22   thorpej 
    997       1.22   thorpej int nfs_defect = 0;
    998        1.1       cgd /*
    999       1.39    tsarna  * Asynchronous I/O threads for client nfs.
   1000       1.10   mycroft  * They do read-ahead and write-behind operations on the block I/O cache.
   1001       1.10   mycroft  * Never returns unless it fails or gets killed.
   1002        1.1       cgd  */
   1003       1.39    tsarna 
   1004      1.118      yamt static void
   1005      1.118      yamt nfssvc_iod(void *arg)
   1006        1.1       cgd {
   1007       1.38  augustss 	struct buf *bp;
   1008       1.58      yamt 	struct nfs_iod *myiod;
   1009       1.22   thorpej 	struct nfsmount *nmp;
   1010        1.1       cgd 
   1011      1.124      yamt 	myiod = kmem_alloc(sizeof(*myiod), KM_SLEEP);
   1012      1.124      yamt 	mutex_init(&myiod->nid_lock, MUTEX_DEFAULT, IPL_NONE);
   1013      1.124      yamt 	cv_init(&myiod->nid_cv, "nfsiod");
   1014      1.124      yamt 	myiod->nid_exiting = false;
   1015      1.124      yamt 	myiod->nid_mount = NULL;
   1016      1.123      yamt 	mutex_enter(&nfs_iodlist_lock);
   1017      1.124      yamt 	LIST_INSERT_HEAD(&nfs_iodlist_all, myiod, nid_all);
   1018      1.123      yamt 	mutex_exit(&nfs_iodlist_lock);
   1019      1.118      yamt 
   1020        1.1       cgd 	for (;;) {
   1021      1.124      yamt 		mutex_enter(&nfs_iodlist_lock);
   1022      1.124      yamt 		LIST_INSERT_HEAD(&nfs_iodlist_idle, myiod, nid_idle);
   1023      1.124      yamt 		mutex_exit(&nfs_iodlist_lock);
   1024      1.124      yamt 
   1025      1.109      yamt 		mutex_enter(&myiod->nid_lock);
   1026      1.106   thorpej 		while (/*CONSTCOND*/ true) {
   1027       1.60      yamt 			nmp = myiod->nid_mount;
   1028       1.60      yamt 			if (nmp) {
   1029      1.124      yamt 				myiod->nid_mount = NULL;
   1030      1.124      yamt 				break;
   1031      1.124      yamt 			}
   1032      1.124      yamt 			if (__predict_false(myiod->nid_exiting)) {
   1033      1.124      yamt 				/*
   1034      1.124      yamt 				 * drop nid_lock to preserve locking order.
   1035      1.124      yamt 				 */
   1036      1.124      yamt 				mutex_exit(&myiod->nid_lock);
   1037      1.124      yamt 				mutex_enter(&nfs_iodlist_lock);
   1038      1.124      yamt 				mutex_enter(&myiod->nid_lock);
   1039      1.124      yamt 				/*
   1040      1.124      yamt 				 * recheck nid_mount because nfs_asyncio can
   1041      1.124      yamt 				 * pick us in the meantime as we are still on
   1042      1.124      yamt 				 * nfs_iodlist_lock.
   1043      1.124      yamt 				 */
   1044      1.124      yamt 				if (myiod->nid_mount != NULL) {
   1045      1.124      yamt 					mutex_exit(&nfs_iodlist_lock);
   1046      1.124      yamt 					continue;
   1047       1.60      yamt 				}
   1048      1.124      yamt 				LIST_REMOVE(myiod, nid_idle);
   1049      1.124      yamt 				mutex_exit(&nfs_iodlist_lock);
   1050       1.60      yamt 				goto quit;
   1051      1.110      yamt 			}
   1052      1.118      yamt 			cv_wait(&myiod->nid_cv, &myiod->nid_lock);
   1053       1.59      yamt 		}
   1054      1.124      yamt 		mutex_exit(&myiod->nid_lock);
   1055       1.60      yamt 
   1056      1.124      yamt 		mutex_enter(&nmp->nm_lock);
   1057       1.60      yamt 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
   1058       1.59      yamt 			/* Take one off the front of the list */
   1059       1.59      yamt 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
   1060       1.59      yamt 			nmp->nm_bufqlen--;
   1061      1.123      yamt 			if (nmp->nm_bufqlen < 2 * nmp->nm_bufqiods) {
   1062      1.109      yamt 				cv_broadcast(&nmp->nm_aiocv);
   1063       1.59      yamt 			}
   1064      1.109      yamt 			mutex_exit(&nmp->nm_lock);
   1065      1.124      yamt 			KERNEL_LOCK(1, curlwp);
   1066       1.79  christos 			(void)nfs_doio(bp);
   1067      1.124      yamt 			KERNEL_UNLOCK_LAST(curlwp);
   1068      1.109      yamt 			mutex_enter(&nmp->nm_lock);
   1069       1.59      yamt 			/*
   1070       1.81    rpaulo 			 * If there are more than one iod on this mount,
   1071       1.81    rpaulo 			 * then defect so that the iods can be shared out
   1072       1.81    rpaulo 			 * fairly between the mounts
   1073       1.59      yamt 			 */
   1074       1.59      yamt 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1075       1.59      yamt 				break;
   1076       1.59      yamt 			}
   1077       1.59      yamt 		}
   1078      1.124      yamt 		KASSERT(nmp->nm_bufqiods > 0);
   1079      1.124      yamt 		nmp->nm_bufqiods--;
   1080      1.109      yamt 		mutex_exit(&nmp->nm_lock);
   1081        1.1       cgd 	}
   1082       1.60      yamt quit:
   1083      1.124      yamt 	KASSERT(myiod->nid_mount == NULL);
   1084      1.124      yamt 	mutex_exit(&myiod->nid_lock);
   1085      1.124      yamt 
   1086      1.124      yamt 	cv_destroy(&myiod->nid_cv);
   1087      1.124      yamt 	mutex_destroy(&myiod->nid_lock);
   1088      1.124      yamt 	kmem_free(myiod, sizeof(*myiod));
   1089       1.39    tsarna 
   1090      1.118      yamt 	kthread_exit(0);
   1091       1.60      yamt }
   1092       1.60      yamt 
   1093       1.60      yamt void
   1094       1.60      yamt nfs_iodinit()
   1095       1.60      yamt {
   1096       1.60      yamt 
   1097      1.123      yamt 	mutex_init(&nfs_iodlist_lock, MUTEX_DEFAULT, IPL_NONE);
   1098      1.124      yamt 	LIST_INIT(&nfs_iodlist_all);
   1099      1.124      yamt 	LIST_INIT(&nfs_iodlist_idle);
   1100        1.1       cgd }
   1101        1.3     glass 
   1102      1.124      yamt int
   1103      1.124      yamt nfs_set_niothreads(int newval)
   1104       1.39    tsarna {
   1105      1.124      yamt 	struct nfs_iod *nid;
   1106      1.124      yamt 	int error = 0;
   1107      1.124      yamt         int hold_count;
   1108      1.124      yamt 
   1109      1.124      yamt 	KERNEL_UNLOCK_ALL(curlwp, &hold_count);
   1110       1.78     perry 
   1111      1.123      yamt 	mutex_enter(&nfs_iodlist_lock);
   1112      1.124      yamt 	/* clamp to sane range */
   1113      1.124      yamt 	nfs_niothreads = max(0, min(newval, NFS_MAXASYNCDAEMON));
   1114       1.39    tsarna 
   1115      1.124      yamt 	while (nfs_numasync != nfs_niothreads && error == 0) {
   1116      1.124      yamt 		while (nfs_numasync < nfs_niothreads) {
   1117       1.39    tsarna 
   1118      1.124      yamt 			/*
   1119      1.124      yamt 			 * kthread_create can wait for pagedaemon and
   1120      1.124      yamt 			 * pagedaemon can wait for nfsiod which needs to acquire
   1121      1.124      yamt 			 * nfs_iodlist_lock.
   1122      1.124      yamt 			 */
   1123      1.124      yamt 
   1124      1.124      yamt 			mutex_exit(&nfs_iodlist_lock);
   1125      1.124      yamt 			error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
   1126      1.123      yamt 			    nfssvc_iod, NULL, NULL, "nfsio");
   1127      1.124      yamt 			mutex_enter(&nfs_iodlist_lock);
   1128      1.124      yamt 			if (error) {
   1129      1.124      yamt 				/* give up */
   1130      1.124      yamt 				nfs_niothreads = nfs_numasync;
   1131      1.124      yamt 				break;
   1132      1.124      yamt 			}
   1133      1.124      yamt 			nfs_numasync++;
   1134       1.39    tsarna 		}
   1135      1.124      yamt 		while (nfs_numasync > nfs_niothreads) {
   1136      1.124      yamt 			nid = LIST_FIRST(&nfs_iodlist_all);
   1137      1.124      yamt 			if (nid == NULL) {
   1138      1.124      yamt 				/* iod has not started yet. */
   1139      1.124      yamt 				kpause("nfsiorm", false, hz, &nfs_iodlist_lock);
   1140      1.124      yamt 				continue;
   1141       1.39    tsarna 			}
   1142      1.124      yamt 			LIST_REMOVE(nid, nid_all);
   1143      1.124      yamt 			mutex_enter(&nid->nid_lock);
   1144      1.124      yamt 			KASSERT(!nid->nid_exiting);
   1145      1.124      yamt 			nid->nid_exiting = true;
   1146      1.124      yamt 			cv_signal(&nid->nid_cv);
   1147      1.124      yamt 			mutex_exit(&nid->nid_lock);
   1148      1.124      yamt 			nfs_numasync--;
   1149      1.118      yamt 		}
   1150       1.39    tsarna 	}
   1151      1.123      yamt 	mutex_exit(&nfs_iodlist_lock);
   1152      1.124      yamt 
   1153      1.124      yamt 	KERNEL_LOCK(hold_count, curlwp);
   1154      1.124      yamt 	return error;
   1155       1.39    tsarna }
   1156       1.19      fvdl 
   1157       1.10   mycroft /*
   1158       1.10   mycroft  * Get an authorization string for the uid by having the mount_nfs sitting
   1159       1.10   mycroft  * on this mount point porpous out of the kernel and do it.
   1160       1.10   mycroft  */
   1161       1.18  christos int
   1162       1.19      fvdl nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1163       1.38  augustss 	struct nfsmount *nmp;
   1164       1.10   mycroft 	struct nfsreq *rep;
   1165       1.92      elad 	kauth_cred_t cred;
   1166       1.10   mycroft 	char **auth_str;
   1167       1.10   mycroft 	int *auth_len;
   1168       1.19      fvdl 	char *verf_str;
   1169       1.19      fvdl 	int *verf_len;
   1170       1.19      fvdl 	NFSKERBKEY_T key;		/* return session key */
   1171       1.10   mycroft {
   1172       1.10   mycroft 	int error = 0;
   1173       1.10   mycroft 
   1174       1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1175       1.27      fvdl 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1176      1.107  christos 		(void) tsleep((void *)&nmp->nm_authtype, PSOCK,
   1177       1.10   mycroft 			"nfsauth1", 2 * hz);
   1178       1.84  christos 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1179       1.18  christos 		if (error) {
   1180       1.27      fvdl 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1181       1.10   mycroft 			return (error);
   1182       1.10   mycroft 		}
   1183       1.10   mycroft 	}
   1184       1.27      fvdl 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1185       1.10   mycroft 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1186       1.19      fvdl 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1187       1.19      fvdl 	nmp->nm_verfstr = verf_str;
   1188       1.19      fvdl 	nmp->nm_verflen = *verf_len;
   1189       1.92      elad 	nmp->nm_authuid = kauth_cred_geteuid(cred);
   1190      1.107  christos 	wakeup((void *)&nmp->nm_authstr);
   1191        1.3     glass 
   1192       1.10   mycroft 	/*
   1193       1.10   mycroft 	 * And wait for mount_nfs to do its stuff.
   1194       1.10   mycroft 	 */
   1195       1.27      fvdl 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1196      1.107  christos 		(void) tsleep((void *)&nmp->nm_authlen, PSOCK,
   1197       1.10   mycroft 			"nfsauth2", 2 * hz);
   1198       1.84  christos 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1199       1.10   mycroft 	}
   1200       1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1201       1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1202       1.10   mycroft 		error = EAUTH;
   1203       1.10   mycroft 	}
   1204       1.10   mycroft 	if (error)
   1205      1.107  christos 		free((void *)*auth_str, M_TEMP);
   1206       1.10   mycroft 	else {
   1207       1.10   mycroft 		*auth_len = nmp->nm_authlen;
   1208       1.19      fvdl 		*verf_len = nmp->nm_verflen;
   1209       1.52       chs 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1210       1.10   mycroft 	}
   1211       1.27      fvdl 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1212       1.27      fvdl 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1213       1.27      fvdl 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1214       1.27      fvdl 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1215      1.107  christos 		wakeup((void *)&nmp->nm_authtype);
   1216       1.10   mycroft 	}
   1217       1.19      fvdl 	return (error);
   1218       1.19      fvdl }
   1219       1.19      fvdl 
   1220       1.19      fvdl /*
   1221       1.19      fvdl  * Get a nickname authenticator and verifier.
   1222       1.19      fvdl  */
   1223       1.19      fvdl int
   1224       1.99      yamt nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
   1225       1.99      yamt     int *auth_len, char *verf_str, int verf_len)
   1226       1.19      fvdl {
   1227       1.95    kardel 	struct timeval ktvin, ktvout, tv;
   1228       1.38  augustss 	struct nfsuid *nuidp;
   1229       1.38  augustss 	u_int32_t *nickp, *verfp;
   1230       1.19      fvdl 
   1231       1.91       mrg 	memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
   1232       1.91       mrg 
   1233       1.19      fvdl #ifdef DIAGNOSTIC
   1234       1.19      fvdl 	if (verf_len < (4 * NFSX_UNSIGNED))
   1235       1.19      fvdl 		panic("nfs_getnickauth verf too small");
   1236       1.19      fvdl #endif
   1237       1.92      elad 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
   1238       1.92      elad 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
   1239       1.19      fvdl 			break;
   1240       1.19      fvdl 	}
   1241       1.95    kardel 	if (!nuidp || nuidp->nu_expire < time_second)
   1242       1.19      fvdl 		return (EACCES);
   1243       1.19      fvdl 
   1244       1.19      fvdl 	/*
   1245       1.19      fvdl 	 * Move to the end of the lru list (end of lru == most recently used).
   1246       1.19      fvdl 	 */
   1247       1.19      fvdl 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1248       1.19      fvdl 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1249       1.19      fvdl 
   1250       1.19      fvdl 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1251       1.19      fvdl 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1252       1.19      fvdl 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1253       1.19      fvdl 	*auth_str = (char *)nickp;
   1254       1.19      fvdl 	*auth_len = 2 * NFSX_UNSIGNED;
   1255       1.19      fvdl 
   1256       1.19      fvdl 	/*
   1257       1.19      fvdl 	 * Now we must encrypt the verifier and package it up.
   1258       1.19      fvdl 	 */
   1259       1.19      fvdl 	verfp = (u_int32_t *)verf_str;
   1260       1.19      fvdl 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1261       1.95    kardel 	getmicrotime(&tv);
   1262       1.95    kardel 	if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1263       1.95    kardel 	    (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1264       1.95    kardel 	     tv.tv_usec > nuidp->nu_timestamp.tv_usec))
   1265       1.95    kardel 		nuidp->nu_timestamp = tv;
   1266       1.19      fvdl 	else
   1267       1.19      fvdl 		nuidp->nu_timestamp.tv_usec++;
   1268       1.19      fvdl 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1269       1.19      fvdl 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1270       1.19      fvdl 
   1271       1.19      fvdl 	/*
   1272       1.19      fvdl 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1273       1.19      fvdl 	 * key.
   1274       1.19      fvdl 	 */
   1275       1.19      fvdl #ifdef NFSKERB
   1276       1.19      fvdl 	XXX
   1277       1.19      fvdl #endif
   1278       1.19      fvdl 
   1279       1.19      fvdl 	*verfp++ = ktvout.tv_sec;
   1280       1.19      fvdl 	*verfp++ = ktvout.tv_usec;
   1281       1.19      fvdl 	*verfp = 0;
   1282       1.19      fvdl 	return (0);
   1283       1.19      fvdl }
   1284       1.19      fvdl 
   1285       1.19      fvdl /*
   1286       1.19      fvdl  * Save the current nickname in a hash list entry on the mount point.
   1287       1.19      fvdl  */
   1288       1.19      fvdl int
   1289       1.19      fvdl nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1290       1.38  augustss 	struct nfsmount *nmp;
   1291       1.92      elad 	kauth_cred_t cred;
   1292       1.19      fvdl 	int len;
   1293       1.19      fvdl 	NFSKERBKEY_T key;
   1294       1.19      fvdl 	struct mbuf **mdp;
   1295       1.19      fvdl 	char **dposp;
   1296       1.19      fvdl 	struct mbuf *mrep;
   1297       1.19      fvdl {
   1298       1.38  augustss 	struct nfsuid *nuidp;
   1299       1.38  augustss 	u_int32_t *tl;
   1300       1.38  augustss 	int32_t t1;
   1301       1.19      fvdl 	struct mbuf *md = *mdp;
   1302       1.19      fvdl 	struct timeval ktvin, ktvout;
   1303       1.19      fvdl 	u_int32_t nick;
   1304       1.19      fvdl 	char *dpos = *dposp, *cp2;
   1305       1.19      fvdl 	int deltasec, error = 0;
   1306       1.19      fvdl 
   1307       1.91       mrg 	memset(&ktvout, 0, sizeof ktvout);	 /* XXX gcc */
   1308       1.91       mrg 
   1309       1.19      fvdl 	if (len == (3 * NFSX_UNSIGNED)) {
   1310       1.19      fvdl 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1311       1.19      fvdl 		ktvin.tv_sec = *tl++;
   1312       1.19      fvdl 		ktvin.tv_usec = *tl++;
   1313       1.19      fvdl 		nick = fxdr_unsigned(u_int32_t, *tl);
   1314       1.19      fvdl 
   1315       1.19      fvdl 		/*
   1316       1.19      fvdl 		 * Decrypt the timestamp in ecb mode.
   1317       1.19      fvdl 		 */
   1318       1.19      fvdl #ifdef NFSKERB
   1319       1.19      fvdl 		XXX
   1320       1.19      fvdl #endif
   1321       1.19      fvdl 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1322       1.19      fvdl 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1323       1.95    kardel 		deltasec = time_second - ktvout.tv_sec;
   1324       1.19      fvdl 		if (deltasec < 0)
   1325       1.19      fvdl 			deltasec = -deltasec;
   1326       1.19      fvdl 		/*
   1327       1.19      fvdl 		 * If ok, add it to the hash list for the mount point.
   1328       1.19      fvdl 		 */
   1329       1.19      fvdl 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1330       1.19      fvdl 			if (nmp->nm_numuids < nuidhash_max) {
   1331       1.19      fvdl 				nmp->nm_numuids++;
   1332      1.130      yamt 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
   1333       1.19      fvdl 			} else {
   1334       1.57      yamt 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1335       1.19      fvdl 				LIST_REMOVE(nuidp, nu_hash);
   1336       1.19      fvdl 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1337       1.19      fvdl 					nu_lru);
   1338       1.19      fvdl 			}
   1339       1.19      fvdl 			nuidp->nu_flag = 0;
   1340       1.92      elad 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
   1341       1.95    kardel 			nuidp->nu_expire = time_second + NFS_KERBTTL;
   1342       1.19      fvdl 			nuidp->nu_timestamp = ktvout;
   1343       1.19      fvdl 			nuidp->nu_nickname = nick;
   1344       1.52       chs 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1345       1.19      fvdl 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1346       1.19      fvdl 				nu_lru);
   1347       1.92      elad 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
   1348       1.19      fvdl 				nuidp, nu_hash);
   1349       1.19      fvdl 		}
   1350       1.19      fvdl 	} else
   1351       1.19      fvdl 		nfsm_adv(nfsm_rndup(len));
   1352       1.19      fvdl nfsmout:
   1353       1.19      fvdl 	*mdp = md;
   1354       1.19      fvdl 	*dposp = dpos;
   1355       1.10   mycroft 	return (error);
   1356       1.10   mycroft }
   1357       1.24   thorpej #endif /* NFS */
   1358