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