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