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