Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.114
      1 /*	$NetBSD: nfs_syscalls.c,v 1.114 2007/06/12 09:30:49 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.114 2007/06/12 09:30:49 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 			mutex_exit(&nfsd_lock);
    559 			if ((slp = nfsd->nfsd_slp) == NULL)
    560 				continue;
    561 			if (slp->ns_flag & SLP_VALID) {
    562 				if (slp->ns_flag & SLP_DISCONN)
    563 					nfsrv_zapsock(slp);
    564 				else if ((slp->ns_flag & SLP_NEEDQ) != 0) {
    565 					nfsrv_rcv(slp->ns_so, (void *)slp,
    566 					    M_WAIT);
    567 				}
    568 				error = nfsrv_dorec(slp, nfsd, &nd);
    569 				getmicrotime(&tv);
    570 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    571 					(u_quad_t)tv.tv_usec;
    572 				if (error && LIST_FIRST(&slp->ns_tq) &&
    573 				    LIST_FIRST(&slp->ns_tq)->nd_time <=
    574 				    cur_usec) {
    575 					error = 0;
    576 					cacherep = RC_DOIT;
    577 					writes_todo = 1;
    578 				} else
    579 					writes_todo = 0;
    580 				if (error == 0 && slp->ns_rec != NULL) {
    581 					nfsrv_wakenfsd(slp);
    582 				}
    583 			}
    584 		} else {
    585 			error = 0;
    586 			slp = nfsd->nfsd_slp;
    587 		}
    588 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
    589 			if (nd) {
    590 				nfsdreq_free(nd);
    591 				nd = NULL;
    592 			}
    593 			nfsd->nfsd_slp = NULL;
    594 			nfsrv_slpderef(slp);
    595 			continue;
    596 		}
    597 		sotype = slp->ns_so->so_type;
    598 		if (nd) {
    599 			getmicrotime(&nd->nd_starttime);
    600 			if (nd->nd_nam2)
    601 				nd->nd_nam = nd->nd_nam2;
    602 			else
    603 				nd->nd_nam = slp->ns_nam;
    604 
    605 			/*
    606 			 * Check to see if authorization is needed.
    607 			 */
    608 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
    609 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
    610 				nsd->nsd_haddr = mtod(nd->nd_nam,
    611 				    struct sockaddr_in *)->sin_addr.s_addr;
    612 				nsd->nsd_authlen = nfsd->nfsd_authlen;
    613 				nsd->nsd_verflen = nfsd->nfsd_verflen;
    614 				if (!copyout(nfsd->nfsd_authstr,
    615 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
    616 				    !copyout(nfsd->nfsd_verfstr,
    617 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
    618 				    !copyout(nsd, argp, sizeof (*nsd))) {
    619 					PRELE(l);
    620 					return (ENEEDAUTH);
    621 				}
    622 				cacherep = RC_DROPIT;
    623 			} else
    624 				cacherep = nfsrv_getcache(nd, slp, &mreq);
    625 
    626 			if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
    627 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
    628 				nd->nd_procnum = NFSPROC_NOOP;
    629 				nd->nd_repstat =
    630 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
    631 				cacherep = RC_DOIT;
    632 			}
    633 		}
    634 
    635 		/*
    636 		 * Loop to get all the write rpc relies that have been
    637 		 * gathered together.
    638 		 */
    639 		do {
    640 #ifdef DIAGNOSTIC
    641 			int lockcount;
    642 #endif
    643 			switch (cacherep) {
    644 			case RC_DOIT:
    645 #ifdef DIAGNOSTIC
    646 				/*
    647 				 * NFS server procs should neither release
    648 				 * locks already held, nor leave things
    649 				 * locked.  Catch this sooner, rather than
    650 				 * later (when we try to relock something we
    651 				 * already have locked).  Careful inspection
    652 				 * of the failing routine usually turns up the
    653 				 * lock leak.. once we know what it is..
    654 				 */
    655 				lockcount = l->l_locks;
    656 #endif
    657 				mreq = NULL;
    658 				netexport_rdlock();
    659 				if (writes_todo || nd == NULL ||
    660 				     (!(nd->nd_flag & ND_NFSV3) &&
    661 				     nd->nd_procnum == NFSPROC_WRITE &&
    662 				     nfsrvw_procrastinate > 0))
    663 					error = nfsrv_writegather(&nd, slp,
    664 					    l, &mreq);
    665 				else
    666 					error =
    667 					    (*(nfsrv3_procs[nd->nd_procnum]))
    668 					    (nd, slp, l, &mreq);
    669 				netexport_rdunlock();
    670 #ifdef DIAGNOSTIC
    671 				if (l->l_locks != lockcount) {
    672 					/*
    673 					 * If you see this panic, audit
    674 					 * nfsrv3_procs[nd->nd_procnum] for
    675 					 * vnode locking errors (usually, it's
    676 					 * due to forgetting to vput()
    677 					 * something).
    678 					 */
    679 #ifdef DEBUG
    680 					extern void printlockedvnodes(void);
    681 					printlockedvnodes();
    682 #endif
    683 					printf("nfsd: locking botch in op %d"
    684 					    " (before %d, after %d)\n",
    685 					    nd ? nd->nd_procnum : -1,
    686 					    lockcount, l->l_locks);
    687 				}
    688 #endif
    689 				if (mreq == NULL) {
    690 					if (nd != NULL) {
    691 						if (nd->nd_nam2)
    692 							m_free(nd->nd_nam2);
    693 						if (nd->nd_mrep)
    694 							m_freem(nd->nd_mrep);
    695 					}
    696 					break;
    697 				}
    698 				if (error) {
    699 					nfsstats.srv_errs++;
    700 					nfsrv_updatecache(nd, false, mreq);
    701 					if (nd->nd_nam2)
    702 						m_freem(nd->nd_nam2);
    703 					break;
    704 				}
    705 				nfsstats.srvrpccnt[nd->nd_procnum]++;
    706 				nfsrv_updatecache(nd, true, mreq);
    707 				nd->nd_mrep = (struct mbuf *)0;
    708 			case RC_REPLY:
    709 				m = mreq;
    710 				siz = 0;
    711 				while (m) {
    712 					siz += m->m_len;
    713 					m = m->m_next;
    714 				}
    715 				if (siz <= 0 || siz > NFS_MAXPACKET) {
    716 					printf("mbuf siz=%d\n",siz);
    717 					panic("Bad nfs svc reply");
    718 				}
    719 				m = mreq;
    720 				m->m_pkthdr.len = siz;
    721 				m->m_pkthdr.rcvif = (struct ifnet *)0;
    722 				/*
    723 				 * For stream protocols, prepend a Sun RPC
    724 				 * Record Mark.
    725 				 */
    726 				if (sotype == SOCK_STREAM) {
    727 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    728 					*mtod(m, u_int32_t *) =
    729 					    htonl(0x80000000 | siz);
    730 				}
    731 				nd->nd_mreq = m;
    732 				if (nfsrtton) {
    733 					nfsd_rt(slp->ns_so->so_type, nd,
    734 					    cacherep);
    735 				}
    736 				error = nfsdsock_sendreply(slp, nd);
    737 				nd = NULL;
    738 				if (error == EPIPE)
    739 					nfsrv_zapsock(slp);
    740 				if (error == EINTR || error == ERESTART) {
    741 					nfsd->nfsd_slp = NULL;
    742 					nfsrv_slpderef(slp);
    743 					goto done;
    744 				}
    745 				break;
    746 			case RC_DROPIT:
    747 				if (nfsrtton)
    748 					nfsd_rt(sotype, nd, cacherep);
    749 				m_freem(nd->nd_mrep);
    750 				m_freem(nd->nd_nam2);
    751 				break;
    752 			}
    753 			if (nd) {
    754 				nfsdreq_free(nd);
    755 				nd = NULL;
    756 			}
    757 
    758 			/*
    759 			 * Check to see if there are outstanding writes that
    760 			 * need to be serviced.
    761 			 */
    762 			getmicrotime(&tv);
    763 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    764 			    (u_quad_t)tv.tv_usec;
    765 			s = splsoftclock();
    766 			if (LIST_FIRST(&slp->ns_tq) &&
    767 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    768 				cacherep = RC_DOIT;
    769 				writes_todo = 1;
    770 			} else
    771 				writes_todo = 0;
    772 			splx(s);
    773 		} while (writes_todo);
    774 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    775 			nfsd->nfsd_slp = NULL;
    776 			nfsrv_slpderef(slp);
    777 		}
    778 	}
    779 done:
    780 	KASSERT(nfsd->nfsd_slp == NULL);
    781 	PRELE(l);
    782 	mutex_enter(&nfsd_lock);
    783 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    784 	mutex_exit(&nfsd_lock);
    785 	cv_destroy(&nfsd->nfsd_cv);
    786 	free(nfsd, M_NFSD);
    787 	nsd->nsd_nfsd = NULL;
    788 	if (--nfs_numnfsd == 0)
    789 		nfsrv_init(true);	/* Reinitialize everything */
    790 	return (error);
    791 }
    792 
    793 /*
    794  * Shut down a socket associated with an nfssvc_sock structure.
    795  * Should be called with the send lock set, if required.
    796  * The trick here is to increment the sref at the start, so that the nfsds
    797  * will stop using it and clear ns_flag at the end so that it will not be
    798  * reassigned during cleanup.
    799  *
    800  * called at splsoftnet.
    801  */
    802 void
    803 nfsrv_zapsock(slp)
    804 	struct nfssvc_sock *slp;
    805 {
    806 	struct nfsuid *nuidp, *nnuidp;
    807 	struct nfsrv_descript *nwp, *nnwp;
    808 	struct socket *so;
    809 	struct mbuf *m;
    810 	int s;
    811 
    812 	if (nfsdsock_drain(slp)) {
    813 		return;
    814 	}
    815 	mutex_enter(&nfsd_lock);
    816 	if (slp->ns_flag & SLP_DOREC) {
    817 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    818 	}
    819 	mutex_exit(&nfsd_lock);
    820 
    821 	so = slp->ns_so;
    822 	KASSERT(so != NULL);
    823 	so->so_upcall = NULL;
    824 	so->so_upcallarg = NULL;
    825 	so->so_rcv.sb_flags &= ~SB_UPCALL;
    826 	soshutdown(so, SHUT_RDWR);
    827 
    828 	if (slp->ns_nam)
    829 		m_free(slp->ns_nam);
    830 	m_freem(slp->ns_raw);
    831 	m = slp->ns_rec;
    832 	while (m != NULL) {
    833 		struct mbuf *n;
    834 
    835 		n = m->m_nextpkt;
    836 		m_freem(m);
    837 		m = n;
    838 	}
    839 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    840 	    nuidp = nnuidp) {
    841 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    842 		LIST_REMOVE(nuidp, nu_hash);
    843 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    844 		if (nuidp->nu_flag & NU_NAM)
    845 			m_freem(nuidp->nu_nam);
    846 		free((void *)nuidp, M_NFSUID);
    847 	}
    848 	s = splsoftclock();
    849 	for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
    850 		nnwp = LIST_NEXT(nwp, nd_tq);
    851 		LIST_REMOVE(nwp, nd_tq);
    852 		nfsdreq_free(nwp);
    853 	}
    854 	splx(s);
    855 }
    856 
    857 /*
    858  * Derefence a server socket structure. If it has no more references and
    859  * is no longer valid, you can throw it away.
    860  */
    861 void
    862 nfsrv_slpderef(slp)
    863 	struct nfssvc_sock *slp;
    864 {
    865 
    866 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    867 		struct file *fp;
    868 
    869 		mutex_enter(&nfsd_lock);
    870 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    871 		mutex_exit(&nfsd_lock);
    872 
    873 		fp = slp->ns_fp;
    874 		if (fp != NULL) {
    875 			slp->ns_fp = NULL;
    876 			KASSERT(fp != NULL);
    877 			KASSERT(fp->f_data == slp->ns_so);
    878 			simple_lock(&fp->f_slock);
    879 			FILE_USE(fp);
    880 			closef(fp, (struct lwp *)0);
    881 			slp->ns_so = NULL;
    882 		}
    883 
    884 		nfsrv_sockfree(slp);
    885 	}
    886 }
    887 
    888 /*
    889  * Initialize the data structures for the server.
    890  * Handshake with any new nfsds starting up to avoid any chance of
    891  * corruption.
    892  */
    893 void
    894 nfsrv_init(terminating)
    895 	int terminating;
    896 {
    897 	struct nfssvc_sock *slp;
    898 
    899 	if (!terminating) {
    900 		mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
    901 		cv_init(&nfsd_initcv, "nfsdinit");
    902 	}
    903 
    904 	mutex_enter(&nfsd_lock);
    905 	if (nfssvc_sockhead_flag & SLP_INIT)
    906 		panic("nfsd init");
    907 	nfssvc_sockhead_flag |= SLP_INIT;
    908 
    909 	if (terminating) {
    910 		KASSERT(SLIST_EMPTY(&nfsd_idle_head));
    911 		KASSERT(TAILQ_EMPTY(&nfsd_head));
    912 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    913 			mutex_exit(&nfsd_lock);
    914 			KASSERT(slp->ns_sref == 0);
    915 			slp->ns_sref++;
    916 			nfsrv_zapsock(slp);
    917 			nfsrv_slpderef(slp);
    918 			mutex_enter(&nfsd_lock);
    919 		}
    920 		KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
    921 		mutex_exit(&nfsd_lock);
    922 		nfsrv_cleancache();	/* And clear out server cache */
    923 	} else {
    924 		mutex_exit(&nfsd_lock);
    925 		nfs_pub.np_valid = 0;
    926 	}
    927 
    928 	TAILQ_INIT(&nfssvc_sockhead);
    929 	TAILQ_INIT(&nfssvc_sockpending);
    930 
    931 	TAILQ_INIT(&nfsd_head);
    932 	SLIST_INIT(&nfsd_idle_head);
    933 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    934 
    935 	nfs_udpsock = nfsrv_sockalloc();
    936 
    937 #ifdef INET6
    938 	nfs_udp6sock = nfsrv_sockalloc();
    939 #endif
    940 
    941 #ifdef ISO
    942 	nfs_cltpsock = nfsrv_sockalloc();
    943 #endif
    944 
    945 	mutex_enter(&nfsd_lock);
    946 	nfssvc_sockhead_flag &= ~SLP_INIT;
    947 	cv_broadcast(&nfsd_initcv);
    948 	mutex_exit(&nfsd_lock);
    949 }
    950 
    951 /*
    952  * Add entries to the server monitor log.
    953  */
    954 static void
    955 nfsd_rt(sotype, nd, cacherep)
    956 	int sotype;
    957 	struct nfsrv_descript *nd;
    958 	int cacherep;
    959 {
    960 	struct timeval tv;
    961 	struct drt *rt;
    962 
    963 	rt = &nfsdrt.drt[nfsdrt.pos];
    964 	if (cacherep == RC_DOIT)
    965 		rt->flag = 0;
    966 	else if (cacherep == RC_REPLY)
    967 		rt->flag = DRT_CACHEREPLY;
    968 	else
    969 		rt->flag = DRT_CACHEDROP;
    970 	if (sotype == SOCK_STREAM)
    971 		rt->flag |= DRT_TCP;
    972 	if (nd->nd_flag & ND_NFSV3)
    973 		rt->flag |= DRT_NFSV3;
    974 	rt->proc = nd->nd_procnum;
    975 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    976 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    977 	else
    978 	    rt->ipadr = INADDR_ANY;
    979 	getmicrotime(&tv);
    980 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    981 		(tv.tv_usec - nd->nd_starttime.tv_usec);
    982 	rt->tstamp = tv;
    983 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    984 }
    985 #endif /* NFSSERVER */
    986 
    987 #ifdef NFS
    988 
    989 int nfs_defect = 0;
    990 /*
    991  * Asynchronous I/O threads for client nfs.
    992  * They do read-ahead and write-behind operations on the block I/O cache.
    993  * Never returns unless it fails or gets killed.
    994  */
    995 
    996 int
    997 nfssvc_iod(l)
    998 	struct lwp *l;
    999 {
   1000 	struct buf *bp;
   1001 	int i;
   1002 	struct nfs_iod *myiod;
   1003 	struct nfsmount *nmp;
   1004 	int error = 0;
   1005 	struct proc *p = l->l_proc;
   1006 
   1007 	/*
   1008 	 * Assign my position or return error if too many already running
   1009 	 */
   1010 	myiod = NULL;
   1011 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1012 		if (nfs_asyncdaemon[i].nid_proc == NULL) {
   1013 			myiod = &nfs_asyncdaemon[i];
   1014 			break;
   1015 		}
   1016 	if (myiod == NULL)
   1017 		return (EBUSY);
   1018 	myiod->nid_proc = p;
   1019 	nfs_numasync++;
   1020 	PHOLD(l);
   1021 	/*
   1022 	 * Just loop around doing our stuff until SIGKILL
   1023 	 */
   1024 	for (;;) {
   1025 		mutex_enter(&myiod->nid_lock);
   1026 		while (/*CONSTCOND*/ true) {
   1027 			nmp = myiod->nid_mount;
   1028 			if (nmp) {
   1029 				mutex_enter(&nmp->nm_lock);
   1030 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
   1031 					mutex_exit(&myiod->nid_lock);
   1032 					break;
   1033 				}
   1034 				nmp->nm_bufqiods--;
   1035 				mutex_exit(&nmp->nm_lock);
   1036 			}
   1037 			myiod->nid_want = p;
   1038 			myiod->nid_mount = NULL;
   1039 			error = cv_wait_sig(&myiod->nid_cv, &myiod->nid_lock);
   1040 			if (error) {
   1041 				mutex_exit(&myiod->nid_lock);
   1042 				goto quit;
   1043 			}
   1044 		}
   1045 
   1046 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
   1047 			/* Take one off the front of the list */
   1048 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
   1049 			nmp->nm_bufqlen--;
   1050 			if (nmp->nm_bufqlen < 2 * nfs_numasync) {
   1051 				cv_broadcast(&nmp->nm_aiocv);
   1052 			}
   1053 			mutex_exit(&nmp->nm_lock);
   1054 			(void)nfs_doio(bp);
   1055 			mutex_enter(&nmp->nm_lock);
   1056 			/*
   1057 			 * If there are more than one iod on this mount,
   1058 			 * then defect so that the iods can be shared out
   1059 			 * fairly between the mounts
   1060 			 */
   1061 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1062 				myiod->nid_mount = NULL;
   1063 				nmp->nm_bufqiods--;
   1064 				break;
   1065 			}
   1066 		}
   1067 		mutex_exit(&nmp->nm_lock);
   1068 	}
   1069 quit:
   1070 	PRELE(l);
   1071 	if (nmp)
   1072 		nmp->nm_bufqiods--;
   1073 	myiod->nid_want = NULL;
   1074 	myiod->nid_mount = NULL;
   1075 	myiod->nid_proc = NULL;
   1076 	nfs_numasync--;
   1077 
   1078 	return error;
   1079 }
   1080 
   1081 void
   1082 nfs_iodinit()
   1083 {
   1084 	int i;
   1085 
   1086 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
   1087 		struct nfs_iod *nid = &nfs_asyncdaemon[i];
   1088 		mutex_init(&nid->nid_lock, MUTEX_DEFAULT, IPL_NONE);
   1089 		cv_init(&nid->nid_cv, "nfsiod");
   1090 	}
   1091 }
   1092 
   1093 void
   1094 start_nfsio(void *arg)
   1095 {
   1096 	nfssvc_iod(curlwp);
   1097 
   1098 	kthread_exit(0);
   1099 }
   1100 
   1101 void
   1102 nfs_getset_niothreads(set)
   1103 	int set;
   1104 {
   1105 	int i, have, start;
   1106 
   1107 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1108 		if (nfs_asyncdaemon[i].nid_proc != NULL)
   1109 			have++;
   1110 
   1111 	if (set) {
   1112 		/* clamp to sane range */
   1113 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1114 
   1115 		start = nfs_niothreads - have;
   1116 
   1117 		while (start > 0) {
   1118 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1119 			start--;
   1120 		}
   1121 
   1122 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1123 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
   1124 				mutex_enter(&proclist_mutex);
   1125 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
   1126 				mutex_exit(&proclist_mutex);
   1127 				start++;
   1128 			}
   1129 	} else {
   1130 		if (nfs_niothreads >= 0)
   1131 			nfs_niothreads = have;
   1132 	}
   1133 }
   1134 
   1135 /*
   1136  * Get an authorization string for the uid by having the mount_nfs sitting
   1137  * on this mount point porpous out of the kernel and do it.
   1138  */
   1139 int
   1140 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1141 	struct nfsmount *nmp;
   1142 	struct nfsreq *rep;
   1143 	kauth_cred_t cred;
   1144 	char **auth_str;
   1145 	int *auth_len;
   1146 	char *verf_str;
   1147 	int *verf_len;
   1148 	NFSKERBKEY_T key;		/* return session key */
   1149 {
   1150 	int error = 0;
   1151 
   1152 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1153 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1154 		(void) tsleep((void *)&nmp->nm_authtype, PSOCK,
   1155 			"nfsauth1", 2 * hz);
   1156 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1157 		if (error) {
   1158 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1159 			return (error);
   1160 		}
   1161 	}
   1162 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1163 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1164 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1165 	nmp->nm_verfstr = verf_str;
   1166 	nmp->nm_verflen = *verf_len;
   1167 	nmp->nm_authuid = kauth_cred_geteuid(cred);
   1168 	wakeup((void *)&nmp->nm_authstr);
   1169 
   1170 	/*
   1171 	 * And wait for mount_nfs to do its stuff.
   1172 	 */
   1173 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1174 		(void) tsleep((void *)&nmp->nm_authlen, PSOCK,
   1175 			"nfsauth2", 2 * hz);
   1176 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1177 	}
   1178 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1179 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1180 		error = EAUTH;
   1181 	}
   1182 	if (error)
   1183 		free((void *)*auth_str, M_TEMP);
   1184 	else {
   1185 		*auth_len = nmp->nm_authlen;
   1186 		*verf_len = nmp->nm_verflen;
   1187 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1188 	}
   1189 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1190 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1191 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1192 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1193 		wakeup((void *)&nmp->nm_authtype);
   1194 	}
   1195 	return (error);
   1196 }
   1197 
   1198 /*
   1199  * Get a nickname authenticator and verifier.
   1200  */
   1201 int
   1202 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
   1203     int *auth_len, char *verf_str, int verf_len)
   1204 {
   1205 	struct timeval ktvin, ktvout, tv;
   1206 	struct nfsuid *nuidp;
   1207 	u_int32_t *nickp, *verfp;
   1208 
   1209 	memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
   1210 
   1211 #ifdef DIAGNOSTIC
   1212 	if (verf_len < (4 * NFSX_UNSIGNED))
   1213 		panic("nfs_getnickauth verf too small");
   1214 #endif
   1215 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
   1216 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
   1217 			break;
   1218 	}
   1219 	if (!nuidp || nuidp->nu_expire < time_second)
   1220 		return (EACCES);
   1221 
   1222 	/*
   1223 	 * Move to the end of the lru list (end of lru == most recently used).
   1224 	 */
   1225 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1226 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1227 
   1228 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1229 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1230 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1231 	*auth_str = (char *)nickp;
   1232 	*auth_len = 2 * NFSX_UNSIGNED;
   1233 
   1234 	/*
   1235 	 * Now we must encrypt the verifier and package it up.
   1236 	 */
   1237 	verfp = (u_int32_t *)verf_str;
   1238 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1239 	getmicrotime(&tv);
   1240 	if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1241 	    (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1242 	     tv.tv_usec > nuidp->nu_timestamp.tv_usec))
   1243 		nuidp->nu_timestamp = tv;
   1244 	else
   1245 		nuidp->nu_timestamp.tv_usec++;
   1246 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1247 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1248 
   1249 	/*
   1250 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1251 	 * key.
   1252 	 */
   1253 #ifdef NFSKERB
   1254 	XXX
   1255 #endif
   1256 
   1257 	*verfp++ = ktvout.tv_sec;
   1258 	*verfp++ = ktvout.tv_usec;
   1259 	*verfp = 0;
   1260 	return (0);
   1261 }
   1262 
   1263 /*
   1264  * Save the current nickname in a hash list entry on the mount point.
   1265  */
   1266 int
   1267 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1268 	struct nfsmount *nmp;
   1269 	kauth_cred_t cred;
   1270 	int len;
   1271 	NFSKERBKEY_T key;
   1272 	struct mbuf **mdp;
   1273 	char **dposp;
   1274 	struct mbuf *mrep;
   1275 {
   1276 	struct nfsuid *nuidp;
   1277 	u_int32_t *tl;
   1278 	int32_t t1;
   1279 	struct mbuf *md = *mdp;
   1280 	struct timeval ktvin, ktvout;
   1281 	u_int32_t nick;
   1282 	char *dpos = *dposp, *cp2;
   1283 	int deltasec, error = 0;
   1284 
   1285 	memset(&ktvout, 0, sizeof ktvout);	 /* XXX gcc */
   1286 
   1287 	if (len == (3 * NFSX_UNSIGNED)) {
   1288 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1289 		ktvin.tv_sec = *tl++;
   1290 		ktvin.tv_usec = *tl++;
   1291 		nick = fxdr_unsigned(u_int32_t, *tl);
   1292 
   1293 		/*
   1294 		 * Decrypt the timestamp in ecb mode.
   1295 		 */
   1296 #ifdef NFSKERB
   1297 		XXX
   1298 #endif
   1299 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1300 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1301 		deltasec = time_second - ktvout.tv_sec;
   1302 		if (deltasec < 0)
   1303 			deltasec = -deltasec;
   1304 		/*
   1305 		 * If ok, add it to the hash list for the mount point.
   1306 		 */
   1307 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1308 			if (nmp->nm_numuids < nuidhash_max) {
   1309 				nmp->nm_numuids++;
   1310 				nuidp = (struct nfsuid *)
   1311 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1312 					M_WAITOK);
   1313 			} else {
   1314 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1315 				LIST_REMOVE(nuidp, nu_hash);
   1316 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1317 					nu_lru);
   1318 			}
   1319 			nuidp->nu_flag = 0;
   1320 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
   1321 			nuidp->nu_expire = time_second + NFS_KERBTTL;
   1322 			nuidp->nu_timestamp = ktvout;
   1323 			nuidp->nu_nickname = nick;
   1324 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1325 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1326 				nu_lru);
   1327 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
   1328 				nuidp, nu_hash);
   1329 		}
   1330 	} else
   1331 		nfsm_adv(nfsm_rndup(len));
   1332 nfsmout:
   1333 	*mdp = md;
   1334 	*dposp = dpos;
   1335 	return (error);
   1336 }
   1337 #endif /* NFS */
   1338