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