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