Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.88.8.5
      1 /*	$NetBSD: nfs_syscalls.c,v 1.88.8.5 2006/03/12 17:15:15 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.5 2006/03/12 17:15:15 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 				pool_put(&nfs_srvdesc_pool, nd);
    631 				nd = NULL;
    632 			}
    633 			nfsd->nfsd_slp = NULL;
    634 			nfsrv_slpderef(slp);
    635 			continue;
    636 		}
    637 		splx(s);
    638 		sotype = slp->ns_so->so_type;
    639 		if (nd) {
    640 			nd->nd_starttime = time;
    641 			if (nd->nd_nam2)
    642 				nd->nd_nam = nd->nd_nam2;
    643 			else
    644 				nd->nd_nam = slp->ns_nam;
    645 
    646 			/*
    647 			 * Check to see if authorization is needed.
    648 			 */
    649 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    650 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    651 				nsd->nsd_haddr = mtod(nd->nd_nam,
    652 				    struct sockaddr_in *)->sin_addr.s_addr;
    653 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    654 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    655 				if (!copyout(nfsd->nfsd_authstr,
    656 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    657 				    !copyout(nfsd->nfsd_verfstr,
    658 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    659 				    !copyout(nsd, argp, sizeof (*nsd))) {
    660 					PRELE(l);
    661 					return (ENEEDAUTH);
    662 				}
    663 				cacherep = RC_DROPIT;
    664 			} else
    665 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    666 
    667 			/*
    668 			 * Check for just starting up for NQNFS and send
    669 			 * fake "try again later" replies to the NQNFS clients.
    670 			 */
    671 			if (notstarted && nqnfsstarttime <= time.tv_sec) {
    672 				if (modify_flag) {
    673 					nqnfsstarttime =
    674 					    time.tv_sec + nqsrv_writeslack;
    675 					modify_flag = 0;
    676 				} else
    677 					notstarted = 0;
    678 			}
    679 			if (notstarted) {
    680 				if ((nd->nd_flag & ND_NQNFS) == 0)
    681 					cacherep = RC_DROPIT;
    682 				else if (nd->nd_procnum != NFSPROC_WRITE) {
    683 					nd->nd_procnum = NFSPROC_NOOP;
    684 					nd->nd_repstat = NQNFS_TRYLATER;
    685 					cacherep = RC_DOIT;
    686 				} else
    687 					modify_flag = 1;
    688 			} else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    689 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    690 				nd->nd_procnum = NFSPROC_NOOP;
    691 				nd->nd_repstat =
    692 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    693 				cacherep = RC_DOIT;
    694 			}
    695 		}
    696 
    697 		/*
    698 		 * Loop to get all the write rpc relies that have been
    699 		 * gathered together.
    700 		 */
    701 		do {
    702 #ifdef DIAGNOSTIC
    703 			int lockcount;
    704 #endif
    705 			switch (cacherep) {
    706 			case RC_DOIT:
    707 #ifdef DIAGNOSTIC
    708 				/*
    709 				 * NFS server procs should neither release
    710 				 * locks already held, nor leave things
    711 				 * locked.  Catch this sooner, rather than
    712 				 * later (when we try to relock something we
    713 				 * already have locked).  Careful inspection
    714 				 * of the failing routine usually turns up the
    715 				 * lock leak.. once we know what it is..
    716 				 */
    717 				lockcount = l->l_locks;
    718 #endif
    719 				mreq = NULL;
    720 				netexport_rdlock();
    721 				if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
    722 				     nd->nd_procnum == NFSPROC_WRITE &&
    723 				     nfsrvw_procrastinate > 0 && !notstarted))
    724 					error = nfsrv_writegather(&nd, slp,
    725 					    l, &mreq);
    726 				else
    727 					error =
    728 					    (*(nfsrv3_procs[nd->nd_procnum]))
    729 					    (nd, slp, l, &mreq);
    730 				netexport_rdunlock();
    731 #ifdef DIAGNOSTIC
    732 				if (l->l_locks != lockcount) {
    733 					/*
    734 					 * If you see this panic, audit
    735 					 * nfsrv3_procs[nd->nd_procnum] for
    736 					 * vnode locking errors (usually, it's
    737 					 * due to forgetting to vput()
    738 					 * something).
    739 					 */
    740 #ifdef DEBUG
    741 					extern void printlockedvnodes(void);
    742 					printlockedvnodes();
    743 #endif
    744 					printf("nfsd: locking botch in op %d"
    745 					    " (before %d, after %d)\n",
    746 					    nd ? nd->nd_procnum : -1,
    747 					    lockcount, l->l_locks);
    748 				}
    749 #endif
    750 				if (mreq == NULL) {
    751 					if (nd != NULL) {
    752 						if (nd->nd_nam2)
    753 							m_free(nd->nd_nam2);
    754 						if (nd->nd_mrep)
    755 							m_freem(nd->nd_mrep);
    756 					}
    757 					break;
    758 				}
    759 				if (error) {
    760 					if (nd->nd_procnum != NQNFSPROC_VACATED)
    761 						nfsstats.srv_errs++;
    762 					nfsrv_updatecache(nd, FALSE, mreq);
    763 					if (nd->nd_nam2)
    764 						m_freem(nd->nd_nam2);
    765 					break;
    766 				}
    767 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    768 				nfsrv_updatecache(nd, TRUE, mreq);
    769 				nd->nd_mrep = (struct mbuf *)0;
    770 			case RC_REPLY:
    771 				m = mreq;
    772 				siz = 0;
    773 				while (m) {
    774 					siz += m->m_len;
    775 					m = m->m_next;
    776 				}
    777 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    778 					printf("mbuf siz=%d\n",siz);
    779 					panic("Bad nfs svc reply");
    780 				}
    781 				m = mreq;
    782 				m->m_pkthdr.len = siz;
    783 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    784 				/*
    785 				 * For stream protocols, prepend a Sun RPC
    786 				 * Record Mark.
    787 				 */
    788 				if (sotype == SOCK_STREAM) {
    789 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    790 					*mtod(m, u_int32_t *) =
    791 					    htonl(0x80000000 | siz);
    792 				}
    793 				nd->nd_mreq = m;
    794 				if (nfsrtton) {
    795 					nfsd_rt(slp->ns_so->so_type, nd,
    796 					    cacherep);
    797 				}
    798 				s = splsoftnet();
    799 				error = nfsdsock_sendreply(slp, nd);
    800 				nd = NULL;
    801 				if (error == EPIPE)
    802 					nfsrv_zapsock(slp);
    803 				if (error == EINTR || error == ERESTART) {
    804 					nfsrv_slpderef(slp);
    805 					goto done;
    806 				}
    807 				splx(s);
    808 				break;
    809 			case RC_DROPIT:
    810 				if (nfsrtton)
    811 					nfsd_rt(sotype, nd, cacherep);
    812 				m_freem(nd->nd_mrep);
    813 				m_freem(nd->nd_nam2);
    814 				break;
    815 			}
    816 			if (nd) {
    817 				pool_put(&nfs_srvdesc_pool, nd);
    818 				nd = NULL;
    819 			}
    820 
    821 			/*
    822 			 * Check to see if there are outstanding writes that
    823 			 * need to be serviced.
    824 			 */
    825 			cur_usec = (u_quad_t)time.tv_sec * 1000000 +
    826 			    (u_quad_t)time.tv_usec;
    827 			s = splsoftclock();
    828 			if (LIST_FIRST(&slp->ns_tq) &&
    829 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    830 				cacherep = RC_DOIT;
    831 				writes_todo = 1;
    832 			} else
    833 				writes_todo = 0;
    834 			splx(s);
    835 		} while (writes_todo);
    836 		s = splsoftnet();
    837 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    838 			nfsd->nfsd_slp = NULL;
    839 			nfsrv_slpderef(slp);
    840 		}
    841 	}
    842 done:
    843 	PRELE(l);
    844 	simple_lock(&nfsd_slock);
    845 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    846 	simple_unlock(&nfsd_slock);
    847 	splx(s);
    848 	free((caddr_t)nfsd, M_NFSD);
    849 	nsd->nsd_nfsd = (struct nfsd *)0;
    850 	if (--nfs_numnfsd == 0)
    851 		nfsrv_init(TRUE);	/* Reinitialize everything */
    852 	return (error);
    853 }
    854 
    855 /*
    856  * Shut down a socket associated with an nfssvc_sock structure.
    857  * Should be called with the send lock set, if required.
    858  * The trick here is to increment the sref at the start, so that the nfsds
    859  * will stop using it and clear ns_flag at the end so that it will not be
    860  * reassigned during cleanup.
    861  *
    862  * called at splsoftnet.
    863  */
    864 void
    865 nfsrv_zapsock(slp)
    866 	struct nfssvc_sock *slp;
    867 {
    868 	struct nfsuid *nuidp, *nnuidp;
    869 	struct nfsrv_descript *nwp, *nnwp;
    870 	struct socket *so;
    871 	int s;
    872 
    873 	if (nfsdsock_drain(slp)) {
    874 		return;
    875 	}
    876 	simple_lock(&nfsd_slock);
    877 	if (slp->ns_flag & SLP_DOREC) {
    878 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    879 	}
    880 	simple_unlock(&nfsd_slock);
    881 
    882 	so = slp->ns_so;
    883 	KASSERT(so != NULL);
    884 	so->so_upcall = NULL;
    885 	so->so_upcallarg = NULL;
    886 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    887 	soshutdown(so, SHUT_RDWR);
    888 
    889 	if (slp->ns_nam)
    890 		m_free(slp->ns_nam);
    891 	m_freem(slp->ns_raw);
    892 	m_freem(slp->ns_rec);
    893 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    894 	    nuidp = nnuidp) {
    895 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    896 		LIST_REMOVE(nuidp, nu_hash);
    897 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    898 		if (nuidp->nu_flag & NU_NAM)
    899 			m_freem(nuidp->nu_nam);
    900 		free((caddr_t)nuidp, M_NFSUID);
    901 	}
    902 	s = splsoftclock();
    903 	for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
    904 		nnwp = LIST_NEXT(nwp, nd_tq);
    905 		LIST_REMOVE(nwp, nd_tq);
    906 		pool_put(&nfs_srvdesc_pool, nwp);
    907 	}
    908 	splx(s);
    909 }
    910 
    911 /*
    912  * Derefence a server socket structure. If it has no more references and
    913  * is no longer valid, you can throw it away.
    914  */
    915 void
    916 nfsrv_slpderef(slp)
    917 	struct nfssvc_sock *slp;
    918 {
    919 	LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
    920 
    921 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    922 		struct file *fp;
    923 		int s = splsoftnet();
    924 		simple_lock(&nfsd_slock);
    925 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    926 		simple_unlock(&nfsd_slock);
    927 		splx(s);
    928 
    929 		fp = slp->ns_fp;
    930 		if (fp != NULL) {
    931 			slp->ns_fp = NULL;
    932 			KASSERT(fp != NULL);
    933 			KASSERT(fp->f_data == slp->ns_so);
    934 			simple_lock(&fp->f_slock);
    935 			FILE_USE(fp);
    936 			closef(fp, (struct lwp *)0);
    937 			slp->ns_so = NULL;
    938 		}
    939 
    940 		nfsrv_sockfree(slp);
    941 	}
    942 }
    943 
    944 /*
    945  * Initialize the data structures for the server.
    946  * Handshake with any new nfsds starting up to avoid any chance of
    947  * corruption.
    948  */
    949 void
    950 nfsrv_init(terminating)
    951 	int terminating;
    952 {
    953 	struct nfssvc_sock *slp;
    954 	int s;
    955 
    956 	s = splsoftnet();
    957 	simple_lock(&nfsd_slock);
    958 	if (nfssvc_sockhead_flag & SLP_INIT)
    959 		panic("nfsd init");
    960 	nfssvc_sockhead_flag |= SLP_INIT;
    961 
    962 	if (terminating) {
    963 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    964 			simple_unlock(&nfsd_slock);
    965 			KASSERT(slp->ns_sref == 0);
    966 			slp->ns_sref++;
    967 			nfsrv_zapsock(slp);
    968 			nfsrv_slpderef(slp);
    969 			simple_lock(&nfsd_slock);
    970 		}
    971 		simple_unlock(&nfsd_slock);
    972 		splx(s);
    973 		nfsrv_cleancache();	/* And clear out server cache */
    974 	} else {
    975 		simple_unlock(&nfsd_slock);
    976 		splx(s);
    977 		nfs_pub.np_valid = 0;
    978 	}
    979 
    980 	TAILQ_INIT(&nfssvc_sockhead);
    981 	TAILQ_INIT(&nfssvc_sockpending);
    982 	nfssvc_sockhead_flag &= ~SLP_INIT;
    983 
    984 	TAILQ_INIT(&nfsd_head);
    985 	SLIST_INIT(&nfsd_idle_head);
    986 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    987 
    988 	nfs_udpsock = nfsrv_sockalloc();
    989 
    990 #ifdef INET6
    991 	nfs_udp6sock = nfsrv_sockalloc();
    992 #endif
    993 
    994 #ifdef ISO
    995 	nfs_cltpsock = nfsrv_sockalloc();
    996 #endif
    997 
    998 	simple_lock(&nfsd_slock);
    999 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
   1000 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
   1001 		wakeup(&nfssvc_sockhead);
   1002 	}
   1003 	simple_unlock(&nfsd_slock);
   1004 	splx(s);
   1005 }
   1006 
   1007 /*
   1008  * Add entries to the server monitor log.
   1009  */
   1010 static void
   1011 nfsd_rt(sotype, nd, cacherep)
   1012 	int sotype;
   1013 	struct nfsrv_descript *nd;
   1014 	int cacherep;
   1015 {
   1016 	struct drt *rt;
   1017 
   1018 	rt = &nfsdrt.drt[nfsdrt.pos];
   1019 	if (cacherep == RC_DOIT)
   1020 		rt->flag = 0;
   1021 	else if (cacherep == RC_REPLY)
   1022 		rt->flag = DRT_CACHEREPLY;
   1023 	else
   1024 		rt->flag = DRT_CACHEDROP;
   1025 	if (sotype == SOCK_STREAM)
   1026 		rt->flag |= DRT_TCP;
   1027 	if (nd->nd_flag & ND_NQNFS)
   1028 		rt->flag |= DRT_NQNFS;
   1029 	else if (nd->nd_flag & ND_NFSV3)
   1030 		rt->flag |= DRT_NFSV3;
   1031 	rt->proc = nd->nd_procnum;
   1032 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
   1033 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
   1034 	else
   1035 	    rt->ipadr = INADDR_ANY;
   1036 	rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
   1037 		(time.tv_usec - nd->nd_starttime.tv_usec);
   1038 	rt->tstamp = time;
   1039 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
   1040 }
   1041 #endif /* NFSSERVER */
   1042 
   1043 #ifdef NFS
   1044 
   1045 int nfs_defect = 0;
   1046 /*
   1047  * Asynchronous I/O threads for client nfs.
   1048  * They do read-ahead and write-behind operations on the block I/O cache.
   1049  * Never returns unless it fails or gets killed.
   1050  */
   1051 
   1052 int
   1053 nfssvc_iod(l)
   1054 	struct lwp *l;
   1055 {
   1056 	struct buf *bp;
   1057 	int i;
   1058 	struct nfs_iod *myiod;
   1059 	struct nfsmount *nmp;
   1060 	int error = 0;
   1061 	struct proc *p = l ? l->l_proc : NULL;
   1062 
   1063 	/*
   1064 	 * Assign my position or return error if too many already running
   1065 	 */
   1066 	myiod = NULL;
   1067 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1068 		if (nfs_asyncdaemon[i].nid_proc == NULL) {
   1069 			myiod = &nfs_asyncdaemon[i];
   1070 			break;
   1071 		}
   1072 	if (myiod == NULL)
   1073 		return (EBUSY);
   1074 	myiod->nid_proc = p;
   1075 	nfs_numasync++;
   1076 	PHOLD(l);
   1077 	/*
   1078 	 * Just loop around doing our stuff until SIGKILL
   1079 	 */
   1080 	for (;;) {
   1081 		while (/*CONSTCOND*/ TRUE) {
   1082 			simple_lock(&myiod->nid_slock);
   1083 			nmp = myiod->nid_mount;
   1084 			if (nmp) {
   1085 				simple_lock(&nmp->nm_slock);
   1086 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
   1087 					simple_unlock(&myiod->nid_slock);
   1088 					break;
   1089 				}
   1090 				nmp->nm_bufqiods--;
   1091 				simple_unlock(&nmp->nm_slock);
   1092 			}
   1093 			myiod->nid_want = p;
   1094 			myiod->nid_mount = NULL;
   1095 			error = ltsleep(&myiod->nid_want,
   1096 			    PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
   1097 			    &myiod->nid_slock);
   1098 			if (error)
   1099 				goto quit;
   1100 		}
   1101 
   1102 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
   1103 			/* Take one off the front of the list */
   1104 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
   1105 			nmp->nm_bufqlen--;
   1106 			if (nmp->nm_bufqwant &&
   1107 			    nmp->nm_bufqlen < 2 * nfs_numasync) {
   1108 				nmp->nm_bufqwant = FALSE;
   1109 				wakeup(&nmp->nm_bufq);
   1110 			}
   1111 			simple_unlock(&nmp->nm_slock);
   1112 			(void)nfs_doio(bp);
   1113 			simple_lock(&nmp->nm_slock);
   1114 			/*
   1115 			 * If there are more than one iod on this mount,
   1116 			 * then defect so that the iods can be shared out
   1117 			 * fairly between the mounts
   1118 			 */
   1119 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1120 				myiod->nid_mount = NULL;
   1121 				nmp->nm_bufqiods--;
   1122 				break;
   1123 			}
   1124 		}
   1125 		simple_unlock(&nmp->nm_slock);
   1126 	}
   1127 quit:
   1128 	PRELE(l);
   1129 	if (nmp)
   1130 		nmp->nm_bufqiods--;
   1131 	myiod->nid_want = NULL;
   1132 	myiod->nid_mount = NULL;
   1133 	myiod->nid_proc = NULL;
   1134 	nfs_numasync--;
   1135 
   1136 	return error;
   1137 }
   1138 
   1139 void
   1140 nfs_iodinit()
   1141 {
   1142 	int i;
   1143 
   1144 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1145 		simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
   1146 }
   1147 
   1148 void
   1149 start_nfsio(arg)
   1150 	void *arg;
   1151 {
   1152 	nfssvc_iod(curlwp);
   1153 
   1154 	kthread_exit(0);
   1155 }
   1156 
   1157 void
   1158 nfs_getset_niothreads(set)
   1159 	int set;
   1160 {
   1161 	int i, have, start;
   1162 
   1163 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1164 		if (nfs_asyncdaemon[i].nid_proc != NULL)
   1165 			have++;
   1166 
   1167 	if (set) {
   1168 		/* clamp to sane range */
   1169 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1170 
   1171 		start = nfs_niothreads - have;
   1172 
   1173 		while (start > 0) {
   1174 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1175 			start--;
   1176 		}
   1177 
   1178 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1179 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
   1180 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
   1181 				start++;
   1182 			}
   1183 	} else {
   1184 		if (nfs_niothreads >= 0)
   1185 			nfs_niothreads = have;
   1186 	}
   1187 }
   1188 
   1189 /*
   1190  * Get an authorization string for the uid by having the mount_nfs sitting
   1191  * on this mount point porpous out of the kernel and do it.
   1192  */
   1193 int
   1194 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1195 	struct nfsmount *nmp;
   1196 	struct nfsreq *rep;
   1197 	kauth_cred_t cred;
   1198 	char **auth_str;
   1199 	int *auth_len;
   1200 	char *verf_str;
   1201 	int *verf_len;
   1202 	NFSKERBKEY_T key;		/* return session key */
   1203 {
   1204 	int error = 0;
   1205 
   1206 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1207 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1208 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
   1209 			"nfsauth1", 2 * hz);
   1210 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1211 		if (error) {
   1212 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1213 			return (error);
   1214 		}
   1215 	}
   1216 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1217 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1218 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1219 	nmp->nm_verfstr = verf_str;
   1220 	nmp->nm_verflen = *verf_len;
   1221 	nmp->nm_authuid = kauth_cred_geteuid(cred);
   1222 	wakeup((caddr_t)&nmp->nm_authstr);
   1223 
   1224 	/*
   1225 	 * And wait for mount_nfs to do its stuff.
   1226 	 */
   1227 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1228 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
   1229 			"nfsauth2", 2 * hz);
   1230 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1231 	}
   1232 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1233 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1234 		error = EAUTH;
   1235 	}
   1236 	if (error)
   1237 		free((caddr_t)*auth_str, M_TEMP);
   1238 	else {
   1239 		*auth_len = nmp->nm_authlen;
   1240 		*verf_len = nmp->nm_verflen;
   1241 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1242 	}
   1243 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1244 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1245 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1246 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1247 		wakeup((caddr_t)&nmp->nm_authtype);
   1248 	}
   1249 	return (error);
   1250 }
   1251 
   1252 /*
   1253  * Get a nickname authenticator and verifier.
   1254  */
   1255 int
   1256 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
   1257 	struct nfsmount *nmp;
   1258 	kauth_cred_t cred;
   1259 	char **auth_str;
   1260 	int *auth_len;
   1261 	char *verf_str;
   1262 	int verf_len;
   1263 {
   1264 	struct nfsuid *nuidp;
   1265 	u_int32_t *nickp, *verfp;
   1266 	struct timeval ktvin, ktvout;
   1267 
   1268 #ifdef DIAGNOSTIC
   1269 	if (verf_len < (4 * NFSX_UNSIGNED))
   1270 		panic("nfs_getnickauth verf too small");
   1271 #endif
   1272 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
   1273 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
   1274 			break;
   1275 	}
   1276 	if (!nuidp || nuidp->nu_expire < time.tv_sec)
   1277 		return (EACCES);
   1278 
   1279 	/*
   1280 	 * Move to the end of the lru list (end of lru == most recently used).
   1281 	 */
   1282 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1283 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1284 
   1285 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1286 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1287 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1288 	*auth_str = (char *)nickp;
   1289 	*auth_len = 2 * NFSX_UNSIGNED;
   1290 
   1291 	/*
   1292 	 * Now we must encrypt the verifier and package it up.
   1293 	 */
   1294 	verfp = (u_int32_t *)verf_str;
   1295 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1296 	if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1297 	    (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1298 	     time.tv_usec > nuidp->nu_timestamp.tv_usec))
   1299 		nuidp->nu_timestamp = time;
   1300 	else
   1301 		nuidp->nu_timestamp.tv_usec++;
   1302 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1303 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1304 
   1305 	/*
   1306 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1307 	 * key.
   1308 	 */
   1309 #ifdef NFSKERB
   1310 	XXX
   1311 #endif
   1312 
   1313 	*verfp++ = ktvout.tv_sec;
   1314 	*verfp++ = ktvout.tv_usec;
   1315 	*verfp = 0;
   1316 	return (0);
   1317 }
   1318 
   1319 /*
   1320  * Save the current nickname in a hash list entry on the mount point.
   1321  */
   1322 int
   1323 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1324 	struct nfsmount *nmp;
   1325 	kauth_cred_t cred;
   1326 	int len;
   1327 	NFSKERBKEY_T key;
   1328 	struct mbuf **mdp;
   1329 	char **dposp;
   1330 	struct mbuf *mrep;
   1331 {
   1332 	struct nfsuid *nuidp;
   1333 	u_int32_t *tl;
   1334 	int32_t t1;
   1335 	struct mbuf *md = *mdp;
   1336 	struct timeval ktvin, ktvout;
   1337 	u_int32_t nick;
   1338 	char *dpos = *dposp, *cp2;
   1339 	int deltasec, error = 0;
   1340 
   1341 	if (len == (3 * NFSX_UNSIGNED)) {
   1342 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1343 		ktvin.tv_sec = *tl++;
   1344 		ktvin.tv_usec = *tl++;
   1345 		nick = fxdr_unsigned(u_int32_t, *tl);
   1346 
   1347 		/*
   1348 		 * Decrypt the timestamp in ecb mode.
   1349 		 */
   1350 #ifdef NFSKERB
   1351 		XXX
   1352 #endif
   1353 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1354 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1355 		deltasec = time.tv_sec - ktvout.tv_sec;
   1356 		if (deltasec < 0)
   1357 			deltasec = -deltasec;
   1358 		/*
   1359 		 * If ok, add it to the hash list for the mount point.
   1360 		 */
   1361 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1362 			if (nmp->nm_numuids < nuidhash_max) {
   1363 				nmp->nm_numuids++;
   1364 				nuidp = (struct nfsuid *)
   1365 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1366 					M_WAITOK);
   1367 			} else {
   1368 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1369 				LIST_REMOVE(nuidp, nu_hash);
   1370 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1371 					nu_lru);
   1372 			}
   1373 			nuidp->nu_flag = 0;
   1374 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
   1375 			nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
   1376 			nuidp->nu_timestamp = ktvout;
   1377 			nuidp->nu_nickname = nick;
   1378 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1379 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1380 				nu_lru);
   1381 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
   1382 				nuidp, nu_hash);
   1383 		}
   1384 	} else
   1385 		nfsm_adv(nfsm_rndup(len));
   1386 nfsmout:
   1387 	*mdp = md;
   1388 	*dposp = dpos;
   1389 	return (error);
   1390 }
   1391 #endif /* NFS */
   1392