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