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