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