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