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