Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.102
      1 /*	$NetBSD: nfs_syscalls.c,v 1.102 2006/12/28 00:39:03 yamt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Rick Macklem at The University of Guelph.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.102 2006/12/28 00:39:03 yamt Exp $");
     39 
     40 #include "fs_nfs.h"
     41 #include "opt_nfs.h"
     42 #include "opt_nfsserver.h"
     43 #include "opt_iso.h"
     44 #include "opt_inet.h"
     45 #include "opt_compat_netbsd.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/file.h>
     51 #include <sys/stat.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/proc.h>
     55 #include <sys/uio.h>
     56 #include <sys/malloc.h>
     57 #include <sys/buf.h>
     58 #include <sys/mbuf.h>
     59 #include <sys/socket.h>
     60 #include <sys/socketvar.h>
     61 #include <sys/signalvar.h>
     62 #include <sys/domain.h>
     63 #include <sys/protosw.h>
     64 #include <sys/namei.h>
     65 #include <sys/syslog.h>
     66 #include <sys/filedesc.h>
     67 #include <sys/kthread.h>
     68 #include <sys/kauth.h>
     69 
     70 #include <sys/sa.h>
     71 #include <sys/syscallargs.h>
     72 
     73 #include <netinet/in.h>
     74 #include <netinet/tcp.h>
     75 #ifdef ISO
     76 #include <netiso/iso.h>
     77 #endif
     78 #include <nfs/xdr_subs.h>
     79 #include <nfs/rpcv2.h>
     80 #include <nfs/nfsproto.h>
     81 #include <nfs/nfs.h>
     82 #include <nfs/nfsm_subs.h>
     83 #include <nfs/nfsrvcache.h>
     84 #include <nfs/nfsmount.h>
     85 #include <nfs/nfsnode.h>
     86 #include <nfs/nfsrtt.h>
     87 #include <nfs/nfs_var.h>
     88 
     89 /* Global defs. */
     90 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
     91 						struct nfssvc_sock *,
     92 						struct lwp *, struct mbuf **));
     93 extern int nfsrvw_procrastinate;
     94 
     95 struct nfssvc_sock *nfs_udpsock;
     96 #ifdef ISO
     97 struct nfssvc_sock *nfs_cltpsock;
     98 #endif
     99 #ifdef INET6
    100 struct nfssvc_sock *nfs_udp6sock;
    101 #endif
    102 int nuidhash_max = NFS_MAXUIDHASH;
    103 int nfsd_waiting = 0;
    104 #ifdef NFSSERVER
    105 static int nfs_numnfsd = 0;
    106 static struct nfsdrt nfsdrt;
    107 #endif
    108 
    109 #ifdef NFSSERVER
    110 struct simplelock nfsd_slock = SIMPLELOCK_INITIALIZER;
    111 struct nfssvc_sockhead nfssvc_sockhead;
    112 struct nfssvc_sockhead nfssvc_sockpending;
    113 struct nfsdhead nfsd_head;
    114 struct nfsdidlehead nfsd_idle_head;
    115 
    116 int nfssvc_sockhead_flag;
    117 int nfsd_head_flag;
    118 #endif
    119 
    120 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
    121 
    122 #ifdef NFS
    123 struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
    124 int nfs_niothreads = -1; /* == "0, and has never been set" */
    125 #endif
    126 
    127 #ifdef NFSSERVER
    128 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
    129 static void nfsrv_sockfree __P((struct nfssvc_sock *));
    130 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
    131 #endif
    132 
    133 /*
    134  * NFS server system calls
    135  */
    136 
    137 
    138 /*
    139  * Nfs server pseudo system call for the nfsd's
    140  * Based on the flag value it either:
    141  * - adds a socket to the selection list
    142  * - remains in the kernel as an nfsd
    143  * - remains in the kernel as an nfsiod
    144  */
    145 int
    146 sys_nfssvc(struct lwp *l, void *v, register_t *retval)
    147 {
    148 	struct sys_nfssvc_args /* {
    149 		syscallarg(int) flag;
    150 		syscallarg(caddr_t) argp;
    151 	} */ *uap = v;
    152 	int error;
    153 #ifdef NFSSERVER
    154 	int s;
    155 	struct file *fp;
    156 	struct mbuf *nam;
    157 	struct nfsd_args nfsdarg;
    158 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
    159 	struct nfsd *nfsd;
    160 	struct nfssvc_sock *slp;
    161 	struct nfsuid *nuidp;
    162 #endif
    163 
    164 	/*
    165 	 * Must be super user
    166 	 */
    167 	error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    168 	    &l->l_acflag);
    169 	if (error)
    170 		return (error);
    171 
    172 	/* Initialize NFS server / client shared data. */
    173 	nfs_init();
    174 
    175 #ifdef NFSSERVER
    176 	s = splsoftnet();
    177 	simple_lock(&nfsd_slock);
    178 	while (nfssvc_sockhead_flag & SLP_INIT) {
    179 		nfssvc_sockhead_flag |= SLP_WANTINIT;
    180 		(void) ltsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0,
    181 		    &nfsd_slock);
    182 	}
    183 	simple_unlock(&nfsd_slock);
    184 	splx(s);
    185 #endif
    186 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
    187 #if defined(NFS) && defined(COMPAT_14)
    188 		error = nfssvc_iod(l);
    189 #else
    190 		error = ENOSYS;
    191 #endif
    192 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
    193 		error = ENOSYS;
    194 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
    195 #ifndef NFSSERVER
    196 		error = ENOSYS;
    197 #else
    198 		error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
    199 		    sizeof(nfsdarg));
    200 		if (error)
    201 			return (error);
    202 		/* getsock() will use the descriptor for us */
    203 		error = getsock(l->l_proc->p_fd, nfsdarg.sock, &fp);
    204 		if (error)
    205 			return (error);
    206 		/*
    207 		 * Get the client address for connected sockets.
    208 		 */
    209 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
    210 			nam = (struct mbuf *)0;
    211 		else {
    212 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
    213 				MT_SONAME);
    214 			if (error) {
    215 				FILE_UNUSE(fp, NULL);
    216 				return (error);
    217 			}
    218 		}
    219 		error = nfssvc_addsock(fp, nam);
    220 		FILE_UNUSE(fp, NULL);
    221 #endif /* !NFSSERVER */
    222 	} else if (SCARG(uap, flag) & NFSSVC_SETEXPORTSLIST) {
    223 #ifndef NFSSERVER
    224 		error = ENOSYS;
    225 #else
    226 		struct export_args *args;
    227 		struct mountd_exports_list mel;
    228 
    229 		error = copyin(SCARG(uap, argp), &mel, sizeof(mel));
    230 		if (error != 0)
    231 			return error;
    232 
    233 		args = (struct export_args *)malloc(mel.mel_nexports *
    234 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
    235 		error = copyin(mel.mel_exports, args, mel.mel_nexports *
    236 		    sizeof(struct export_args));
    237 		if (error != 0) {
    238 			free(args, M_TEMP);
    239 			return error;
    240 		}
    241 		mel.mel_exports = args;
    242 
    243 		error = mountd_set_exports_list(&mel, l);
    244 
    245 		free(args, M_TEMP);
    246 #endif /* !NFSSERVER */
    247 	} else {
    248 #ifndef NFSSERVER
    249 		error = ENOSYS;
    250 #else
    251 		error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
    252 		if (error)
    253 			return (error);
    254 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
    255 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
    256 		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
    257 			slp = nfsd->nfsd_slp;
    258 
    259 			/*
    260 			 * First check to see if another nfsd has already
    261 			 * added this credential.
    262 			 */
    263 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
    264 			    nu_hash) {
    265 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
    266 				    nsd->nsd_cr.cr_uid &&
    267 				    (!nfsd->nfsd_nd->nd_nam2 ||
    268 				     netaddr_match(NU_NETFAM(nuidp),
    269 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
    270 					break;
    271 			}
    272 			if (nuidp) {
    273 			    kauth_cred_hold(nuidp->nu_cr);
    274 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    275 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    276 			} else {
    277 			    /*
    278 			     * Nope, so we will.
    279 			     */
    280 			    if (slp->ns_numuids < nuidhash_max) {
    281 				slp->ns_numuids++;
    282 				nuidp = (struct nfsuid *)
    283 				   malloc(sizeof (struct nfsuid), M_NFSUID,
    284 					M_WAITOK);
    285 			    } else
    286 				nuidp = (struct nfsuid *)0;
    287 			    if ((slp->ns_flag & SLP_VALID) == 0) {
    288 				if (nuidp)
    289 				    free((caddr_t)nuidp, M_NFSUID);
    290 			    } else {
    291 				if (nuidp == (struct nfsuid *)0) {
    292 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
    293 				    LIST_REMOVE(nuidp, nu_hash);
    294 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
    295 					nu_lru);
    296 				    if (nuidp->nu_flag & NU_NAM)
    297 					m_freem(nuidp->nu_nam);
    298 			        }
    299 				nuidp->nu_flag = 0;
    300 				kauth_uucred_to_cred(nuidp->nu_cr,
    301 				    &nsd->nsd_cr);
    302 				nuidp->nu_timestamp = nsd->nsd_timestamp;
    303 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
    304 				/*
    305 				 * and save the session key in nu_key.
    306 				 */
    307 				memcpy(nuidp->nu_key, nsd->nsd_key,
    308 				    sizeof(nsd->nsd_key));
    309 				if (nfsd->nfsd_nd->nd_nam2) {
    310 				    struct sockaddr_in *saddr;
    311 
    312 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
    313 					 struct sockaddr_in *);
    314 				    switch (saddr->sin_family) {
    315 				    case AF_INET:
    316 					nuidp->nu_flag |= NU_INETADDR;
    317 					nuidp->nu_inetaddr =
    318 					     saddr->sin_addr.s_addr;
    319 					break;
    320 				    case AF_ISO:
    321 				    default:
    322 					nuidp->nu_flag |= NU_NAM;
    323 					nuidp->nu_nam = m_copym(
    324 					    nfsd->nfsd_nd->nd_nam2, 0,
    325 					     M_COPYALL, M_WAIT);
    326 					break;
    327 				    };
    328 				}
    329 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
    330 					nu_lru);
    331 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
    332 					nuidp, nu_hash);
    333 				kauth_cred_hold(nuidp->nu_cr);
    334 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
    335 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
    336 			    }
    337 			}
    338 		}
    339 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
    340 		    (nfsd = nsd->nsd_nfsd))
    341 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
    342 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
    343 #endif /* !NFSSERVER */
    344 	}
    345 	if (error == EINTR || error == ERESTART)
    346 		error = 0;
    347 	return (error);
    348 }
    349 
    350 #ifdef NFSSERVER
    351 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
    352 MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
    353 
    354 static struct nfssvc_sock *
    355 nfsrv_sockalloc()
    356 {
    357 	struct nfssvc_sock *slp;
    358 	int s;
    359 
    360 	slp = (struct nfssvc_sock *)
    361 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    362 	memset(slp, 0, sizeof (struct nfssvc_sock));
    363 	simple_lock_init(&slp->ns_lock);
    364 	TAILQ_INIT(&slp->ns_uidlruhead);
    365 	LIST_INIT(&slp->ns_tq);
    366 	SIMPLEQ_INIT(&slp->ns_sendq);
    367 	s = splsoftnet();
    368 	simple_lock(&nfsd_slock);
    369 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
    370 	simple_unlock(&nfsd_slock);
    371 	splx(s);
    372 
    373 	return slp;
    374 }
    375 
    376 static void
    377 nfsrv_sockfree(struct nfssvc_sock *slp)
    378 {
    379 
    380 	KASSERT(slp->ns_so == NULL);
    381 	KASSERT(slp->ns_fp == NULL);
    382 	KASSERT((slp->ns_flag & SLP_VALID) == 0);
    383 	free(slp, M_NFSSVC);
    384 }
    385 
    386 /*
    387  * Adds a socket to the list for servicing by nfsds.
    388  */
    389 int
    390 nfssvc_addsock(fp, mynam)
    391 	struct file *fp;
    392 	struct mbuf *mynam;
    393 {
    394 	struct mbuf *m;
    395 	int siz;
    396 	struct nfssvc_sock *slp;
    397 	struct socket *so;
    398 	struct nfssvc_sock *tslp;
    399 	int error, s;
    400 
    401 	so = (struct socket *)fp->f_data;
    402 	tslp = (struct nfssvc_sock *)0;
    403 	/*
    404 	 * Add it to the list, as required.
    405 	 */
    406 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
    407 #ifdef INET6
    408 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
    409 			tslp = nfs_udp6sock;
    410 		else
    411 #endif
    412 		tslp = nfs_udpsock;
    413 		if (tslp->ns_flag & SLP_VALID) {
    414 			m_freem(mynam);
    415 			return (EPERM);
    416 		}
    417 #ifdef ISO
    418 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
    419 		tslp = nfs_cltpsock;
    420 		if (tslp->ns_flag & SLP_VALID) {
    421 			m_freem(mynam);
    422 			return (EPERM);
    423 		}
    424 #endif /* ISO */
    425 	}
    426 	if (so->so_type == SOCK_STREAM)
    427 		siz = NFS_MAXPACKET + sizeof (u_long);
    428 	else
    429 		siz = NFS_MAXPACKET;
    430 	error = soreserve(so, siz, siz);
    431 	if (error) {
    432 		m_freem(mynam);
    433 		return (error);
    434 	}
    435 
    436 	/*
    437 	 * Set protocol specific options { for now TCP only } and
    438 	 * reserve some space. For datagram sockets, this can get called
    439 	 * repeatedly for the same socket, but that isn't harmful.
    440 	 */
    441 	if (so->so_type == SOCK_STREAM) {
    442 		m = m_get(M_WAIT, MT_SOOPTS);
    443 		MCLAIM(m, &nfs_mowner);
    444 		*mtod(m, int32_t *) = 1;
    445 		m->m_len = sizeof(int32_t);
    446 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
    447 	}
    448 	if ((so->so_proto->pr_domain->dom_family == AF_INET
    449 #ifdef INET6
    450 	    || so->so_proto->pr_domain->dom_family == AF_INET6
    451 #endif
    452 	    ) &&
    453 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
    454 		m = m_get(M_WAIT, MT_SOOPTS);
    455 		MCLAIM(m, &nfs_mowner);
    456 		*mtod(m, int32_t *) = 1;
    457 		m->m_len = sizeof(int32_t);
    458 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
    459 	}
    460 	so->so_rcv.sb_flags &= ~SB_NOINTR;
    461 	so->so_rcv.sb_timeo = 0;
    462 	so->so_snd.sb_flags &= ~SB_NOINTR;
    463 	so->so_snd.sb_timeo = 0;
    464 	if (tslp)
    465 		slp = tslp;
    466 	else {
    467 		slp = nfsrv_sockalloc();
    468 	}
    469 	slp->ns_so = so;
    470 	slp->ns_nam = mynam;
    471 	fp->f_count++;
    472 	slp->ns_fp = fp;
    473 	s = splsoftnet();
    474 	so->so_upcallarg = (caddr_t)slp;
    475 	so->so_upcall = nfsrv_rcv;
    476 	so->so_rcv.sb_flags |= SB_UPCALL;
    477 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
    478 	nfsrv_wakenfsd(slp);
    479 	splx(s);
    480 	return (0);
    481 }
    482 
    483 /*
    484  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
    485  * until it is killed by a signal.
    486  */
    487 int
    488 nfssvc_nfsd(nsd, argp, l)
    489 	struct nfsd_srvargs *nsd;
    490 	caddr_t argp;
    491 	struct lwp *l;
    492 {
    493 	struct timeval tv;
    494 	struct mbuf *m;
    495 	struct nfssvc_sock *slp;
    496 	struct nfsd *nfsd = nsd->nsd_nfsd;
    497 	struct nfsrv_descript *nd = NULL;
    498 	struct mbuf *mreq;
    499 	u_quad_t cur_usec;
    500 	int error = 0, cacherep, s, siz, sotype, writes_todo;
    501 	struct proc *p = l->l_proc;
    502 
    503 #ifndef nolint
    504 	cacherep = RC_DOIT;
    505 	writes_todo = 0;
    506 #endif
    507 	s = splsoftnet();
    508 	if (nfsd == (struct nfsd *)0) {
    509 		nsd->nsd_nfsd = nfsd = (struct nfsd *)
    510 			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
    511 		memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
    512 		nfsd->nfsd_procp = p;
    513 		simple_lock(&nfsd_slock);
    514 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
    515 		nfs_numnfsd++;
    516 		simple_unlock(&nfsd_slock);
    517 	}
    518 	PHOLD(l);
    519 	/*
    520 	 * Loop getting rpc requests until SIGKILL.
    521 	 */
    522 	for (;;) {
    523 		if (nfsd->nfsd_slp == NULL) {
    524 			simple_lock(&nfsd_slock);
    525 			while (nfsd->nfsd_slp == NULL &&
    526 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
    527 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
    528 				    nfsd_idle);
    529 				nfsd_waiting++;
    530 				error = ltsleep(nfsd, PSOCK | PCATCH, "nfsd",
    531 				    0, &nfsd_slock);
    532 				nfsd_waiting--;
    533 				if (error) {
    534 					slp = nfsd->nfsd_slp;
    535 					nfsd->nfsd_slp = NULL;
    536 					if (!slp)
    537 						SLIST_REMOVE(&nfsd_idle_head,
    538 						    nfsd, nfsd, nfsd_idle);
    539 					simple_unlock(&nfsd_slock);
    540 					if (slp) {
    541 						nfsrv_wakenfsd(slp);
    542 						nfsrv_slpderef(slp);
    543 					}
    544 					goto done;
    545 				}
    546 			}
    547 			if (nfsd->nfsd_slp == NULL &&
    548 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
    549 				slp = TAILQ_FIRST(&nfssvc_sockpending);
    550 				if (slp) {
    551 					KASSERT((slp->ns_flag &
    552 					    (SLP_VALID | SLP_DOREC))
    553 					    == (SLP_VALID | SLP_DOREC));
    554 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
    555 					    ns_pending);
    556 					slp->ns_flag &= ~SLP_DOREC;
    557 					slp->ns_sref++;
    558 					nfsd->nfsd_slp = slp;
    559 				} else
    560 					nfsd_head_flag &= ~NFSD_CHECKSLP;
    561 			}
    562 			simple_unlock(&nfsd_slock);
    563 			if ((slp = nfsd->nfsd_slp) == NULL)
    564 				continue;
    565 			if (slp->ns_flag & SLP_VALID) {
    566 				if (slp->ns_flag & SLP_DISCONN)
    567 					nfsrv_zapsock(slp);
    568 				else if ((slp->ns_flag & SLP_NEEDQ) != 0) {
    569 					nfsrv_rcv(slp->ns_so, (void *)slp,
    570 					    M_WAIT);
    571 				}
    572 				error = nfsrv_dorec(slp, nfsd, &nd);
    573 				getmicrotime(&tv);
    574 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    575 					(u_quad_t)tv.tv_usec;
    576 				if (error && LIST_FIRST(&slp->ns_tq) &&
    577 				    LIST_FIRST(&slp->ns_tq)->nd_time <=
    578 				    cur_usec) {
    579 					error = 0;
    580 					cacherep = RC_DOIT;
    581 					writes_todo = 1;
    582 				} else
    583 					writes_todo = 0;
    584 				if (error == 0 && slp->ns_rec != NULL) {
    585 					nfsrv_wakenfsd(slp);
    586 				}
    587 			}
    588 		} else {
    589 			error = 0;
    590 			slp = nfsd->nfsd_slp;
    591 		}
    592 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
    593 			if (nd) {
    594 				nfsdreq_free(nd);
    595 				nd = NULL;
    596 			}
    597 			nfsd->nfsd_slp = NULL;
    598 			nfsrv_slpderef(slp);
    599 			continue;
    600 		}
    601 		splx(s);
    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 				s = splsoftnet();
    742 				error = nfsdsock_sendreply(slp, nd);
    743 				nd = NULL;
    744 				if (error == EPIPE)
    745 					nfsrv_zapsock(slp);
    746 				if (error == EINTR || error == ERESTART) {
    747 					nfsrv_slpderef(slp);
    748 					goto done;
    749 				}
    750 				splx(s);
    751 				break;
    752 			case RC_DROPIT:
    753 				if (nfsrtton)
    754 					nfsd_rt(sotype, nd, cacherep);
    755 				m_freem(nd->nd_mrep);
    756 				m_freem(nd->nd_nam2);
    757 				break;
    758 			}
    759 			if (nd) {
    760 				nfsdreq_free(nd);
    761 				nd = NULL;
    762 			}
    763 
    764 			/*
    765 			 * Check to see if there are outstanding writes that
    766 			 * need to be serviced.
    767 			 */
    768 			getmicrotime(&tv);
    769 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
    770 			    (u_quad_t)tv.tv_usec;
    771 			s = splsoftclock();
    772 			if (LIST_FIRST(&slp->ns_tq) &&
    773 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
    774 				cacherep = RC_DOIT;
    775 				writes_todo = 1;
    776 			} else
    777 				writes_todo = 0;
    778 			splx(s);
    779 		} while (writes_todo);
    780 		s = splsoftnet();
    781 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    782 			nfsd->nfsd_slp = NULL;
    783 			nfsrv_slpderef(slp);
    784 		}
    785 	}
    786 done:
    787 	PRELE(l);
    788 	simple_lock(&nfsd_slock);
    789 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    790 	simple_unlock(&nfsd_slock);
    791 	splx(s);
    792 	free((caddr_t)nfsd, M_NFSD);
    793 	nsd->nsd_nfsd = (struct nfsd *)0;
    794 	if (--nfs_numnfsd == 0)
    795 		nfsrv_init(TRUE);	/* Reinitialize everything */
    796 	return (error);
    797 }
    798 
    799 /*
    800  * Shut down a socket associated with an nfssvc_sock structure.
    801  * Should be called with the send lock set, if required.
    802  * The trick here is to increment the sref at the start, so that the nfsds
    803  * will stop using it and clear ns_flag at the end so that it will not be
    804  * reassigned during cleanup.
    805  *
    806  * called at splsoftnet.
    807  */
    808 void
    809 nfsrv_zapsock(slp)
    810 	struct nfssvc_sock *slp;
    811 {
    812 	struct nfsuid *nuidp, *nnuidp;
    813 	struct nfsrv_descript *nwp, *nnwp;
    814 	struct socket *so;
    815 	int s;
    816 
    817 	if (nfsdsock_drain(slp)) {
    818 		return;
    819 	}
    820 	simple_lock(&nfsd_slock);
    821 	if (slp->ns_flag & SLP_DOREC) {
    822 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
    823 	}
    824 	simple_unlock(&nfsd_slock);
    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_freem(slp->ns_rec);
    837 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
    838 	    nuidp = nnuidp) {
    839 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
    840 		LIST_REMOVE(nuidp, nu_hash);
    841 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    842 		if (nuidp->nu_flag & NU_NAM)
    843 			m_freem(nuidp->nu_nam);
    844 		free((caddr_t)nuidp, M_NFSUID);
    845 	}
    846 	s = splsoftclock();
    847 	for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
    848 		nnwp = LIST_NEXT(nwp, nd_tq);
    849 		LIST_REMOVE(nwp, nd_tq);
    850 		nfsdreq_free(nwp);
    851 	}
    852 	splx(s);
    853 }
    854 
    855 /*
    856  * Derefence a server socket structure. If it has no more references and
    857  * is no longer valid, you can throw it away.
    858  */
    859 void
    860 nfsrv_slpderef(slp)
    861 	struct nfssvc_sock *slp;
    862 {
    863 	LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
    864 
    865 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    866 		struct file *fp;
    867 		int s = splsoftnet();
    868 		simple_lock(&nfsd_slock);
    869 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    870 		simple_unlock(&nfsd_slock);
    871 		splx(s);
    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 	int s;
    899 
    900 	s = splsoftnet();
    901 	simple_lock(&nfsd_slock);
    902 	if (nfssvc_sockhead_flag & SLP_INIT)
    903 		panic("nfsd init");
    904 	nfssvc_sockhead_flag |= SLP_INIT;
    905 
    906 	if (terminating) {
    907 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
    908 			simple_unlock(&nfsd_slock);
    909 			KASSERT(slp->ns_sref == 0);
    910 			slp->ns_sref++;
    911 			nfsrv_zapsock(slp);
    912 			nfsrv_slpderef(slp);
    913 			simple_lock(&nfsd_slock);
    914 		}
    915 		simple_unlock(&nfsd_slock);
    916 		splx(s);
    917 		nfsrv_cleancache();	/* And clear out server cache */
    918 	} else {
    919 		simple_unlock(&nfsd_slock);
    920 		splx(s);
    921 		nfs_pub.np_valid = 0;
    922 	}
    923 
    924 	TAILQ_INIT(&nfssvc_sockhead);
    925 	TAILQ_INIT(&nfssvc_sockpending);
    926 	nfssvc_sockhead_flag &= ~SLP_INIT;
    927 
    928 	TAILQ_INIT(&nfsd_head);
    929 	SLIST_INIT(&nfsd_idle_head);
    930 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    931 
    932 	nfs_udpsock = nfsrv_sockalloc();
    933 
    934 #ifdef INET6
    935 	nfs_udp6sock = nfsrv_sockalloc();
    936 #endif
    937 
    938 #ifdef ISO
    939 	nfs_cltpsock = nfsrv_sockalloc();
    940 #endif
    941 
    942 	simple_lock(&nfsd_slock);
    943 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
    944 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
    945 		wakeup(&nfssvc_sockhead);
    946 	}
    947 	simple_unlock(&nfsd_slock);
    948 	splx(s);
    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 		while (/*CONSTCOND*/ TRUE) {
   1026 			simple_lock(&myiod->nid_slock);
   1027 			nmp = myiod->nid_mount;
   1028 			if (nmp) {
   1029 				simple_lock(&nmp->nm_slock);
   1030 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
   1031 					simple_unlock(&myiod->nid_slock);
   1032 					break;
   1033 				}
   1034 				nmp->nm_bufqiods--;
   1035 				simple_unlock(&nmp->nm_slock);
   1036 			}
   1037 			myiod->nid_want = p;
   1038 			myiod->nid_mount = NULL;
   1039 			error = ltsleep(&myiod->nid_want,
   1040 			    PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
   1041 			    &myiod->nid_slock);
   1042 			if (error)
   1043 				goto quit;
   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_bufqwant &&
   1051 			    nmp->nm_bufqlen < 2 * nfs_numasync) {
   1052 				nmp->nm_bufqwant = FALSE;
   1053 				wakeup(&nmp->nm_bufq);
   1054 			}
   1055 			simple_unlock(&nmp->nm_slock);
   1056 			(void)nfs_doio(bp);
   1057 			simple_lock(&nmp->nm_slock);
   1058 			/*
   1059 			 * If there are more than one iod on this mount,
   1060 			 * then defect so that the iods can be shared out
   1061 			 * fairly between the mounts
   1062 			 */
   1063 			if (nfs_defect && nmp->nm_bufqiods > 1) {
   1064 				myiod->nid_mount = NULL;
   1065 				nmp->nm_bufqiods--;
   1066 				break;
   1067 			}
   1068 		}
   1069 		simple_unlock(&nmp->nm_slock);
   1070 	}
   1071 quit:
   1072 	PRELE(l);
   1073 	if (nmp)
   1074 		nmp->nm_bufqiods--;
   1075 	myiod->nid_want = NULL;
   1076 	myiod->nid_mount = NULL;
   1077 	myiod->nid_proc = NULL;
   1078 	nfs_numasync--;
   1079 
   1080 	return error;
   1081 }
   1082 
   1083 void
   1084 nfs_iodinit()
   1085 {
   1086 	int i;
   1087 
   1088 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1089 		simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
   1090 }
   1091 
   1092 void
   1093 start_nfsio(void *arg)
   1094 {
   1095 	nfssvc_iod(curlwp);
   1096 
   1097 	kthread_exit(0);
   1098 }
   1099 
   1100 void
   1101 nfs_getset_niothreads(set)
   1102 	int set;
   1103 {
   1104 	int i, have, start;
   1105 
   1106 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1107 		if (nfs_asyncdaemon[i].nid_proc != NULL)
   1108 			have++;
   1109 
   1110 	if (set) {
   1111 		/* clamp to sane range */
   1112 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1113 
   1114 		start = nfs_niothreads - have;
   1115 
   1116 		while (start > 0) {
   1117 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1118 			start--;
   1119 		}
   1120 
   1121 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1122 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
   1123 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
   1124 				start++;
   1125 			}
   1126 	} else {
   1127 		if (nfs_niothreads >= 0)
   1128 			nfs_niothreads = have;
   1129 	}
   1130 }
   1131 
   1132 /*
   1133  * Get an authorization string for the uid by having the mount_nfs sitting
   1134  * on this mount point porpous out of the kernel and do it.
   1135  */
   1136 int
   1137 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1138 	struct nfsmount *nmp;
   1139 	struct nfsreq *rep;
   1140 	kauth_cred_t cred;
   1141 	char **auth_str;
   1142 	int *auth_len;
   1143 	char *verf_str;
   1144 	int *verf_len;
   1145 	NFSKERBKEY_T key;		/* return session key */
   1146 {
   1147 	int error = 0;
   1148 
   1149 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1150 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1151 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
   1152 			"nfsauth1", 2 * hz);
   1153 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1154 		if (error) {
   1155 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1156 			return (error);
   1157 		}
   1158 	}
   1159 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1160 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1161 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1162 	nmp->nm_verfstr = verf_str;
   1163 	nmp->nm_verflen = *verf_len;
   1164 	nmp->nm_authuid = kauth_cred_geteuid(cred);
   1165 	wakeup((caddr_t)&nmp->nm_authstr);
   1166 
   1167 	/*
   1168 	 * And wait for mount_nfs to do its stuff.
   1169 	 */
   1170 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1171 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
   1172 			"nfsauth2", 2 * hz);
   1173 		error = nfs_sigintr(nmp, rep, rep->r_lwp);
   1174 	}
   1175 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1176 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1177 		error = EAUTH;
   1178 	}
   1179 	if (error)
   1180 		free((caddr_t)*auth_str, M_TEMP);
   1181 	else {
   1182 		*auth_len = nmp->nm_authlen;
   1183 		*verf_len = nmp->nm_verflen;
   1184 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
   1185 	}
   1186 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1187 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1188 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1189 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1190 		wakeup((caddr_t)&nmp->nm_authtype);
   1191 	}
   1192 	return (error);
   1193 }
   1194 
   1195 /*
   1196  * Get a nickname authenticator and verifier.
   1197  */
   1198 int
   1199 nfs_getnickauth(struct nfsmount *nmp, kauth_cred_t cred, char **auth_str,
   1200     int *auth_len, char *verf_str, int verf_len)
   1201 {
   1202 	struct timeval ktvin, ktvout, tv;
   1203 	struct nfsuid *nuidp;
   1204 	u_int32_t *nickp, *verfp;
   1205 
   1206 	memset(&ktvout, 0, sizeof ktvout);	/* XXX gcc */
   1207 
   1208 #ifdef DIAGNOSTIC
   1209 	if (verf_len < (4 * NFSX_UNSIGNED))
   1210 		panic("nfs_getnickauth verf too small");
   1211 #endif
   1212 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, kauth_cred_geteuid(cred)), nu_hash) {
   1213 		if (kauth_cred_geteuid(nuidp->nu_cr) == kauth_cred_geteuid(cred))
   1214 			break;
   1215 	}
   1216 	if (!nuidp || nuidp->nu_expire < time_second)
   1217 		return (EACCES);
   1218 
   1219 	/*
   1220 	 * Move to the end of the lru list (end of lru == most recently used).
   1221 	 */
   1222 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1223 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1224 
   1225 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1226 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1227 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1228 	*auth_str = (char *)nickp;
   1229 	*auth_len = 2 * NFSX_UNSIGNED;
   1230 
   1231 	/*
   1232 	 * Now we must encrypt the verifier and package it up.
   1233 	 */
   1234 	verfp = (u_int32_t *)verf_str;
   1235 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1236 	getmicrotime(&tv);
   1237 	if (tv.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1238 	    (tv.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1239 	     tv.tv_usec > nuidp->nu_timestamp.tv_usec))
   1240 		nuidp->nu_timestamp = tv;
   1241 	else
   1242 		nuidp->nu_timestamp.tv_usec++;
   1243 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1244 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1245 
   1246 	/*
   1247 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1248 	 * key.
   1249 	 */
   1250 #ifdef NFSKERB
   1251 	XXX
   1252 #endif
   1253 
   1254 	*verfp++ = ktvout.tv_sec;
   1255 	*verfp++ = ktvout.tv_usec;
   1256 	*verfp = 0;
   1257 	return (0);
   1258 }
   1259 
   1260 /*
   1261  * Save the current nickname in a hash list entry on the mount point.
   1262  */
   1263 int
   1264 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1265 	struct nfsmount *nmp;
   1266 	kauth_cred_t cred;
   1267 	int len;
   1268 	NFSKERBKEY_T key;
   1269 	struct mbuf **mdp;
   1270 	char **dposp;
   1271 	struct mbuf *mrep;
   1272 {
   1273 	struct nfsuid *nuidp;
   1274 	u_int32_t *tl;
   1275 	int32_t t1;
   1276 	struct mbuf *md = *mdp;
   1277 	struct timeval ktvin, ktvout;
   1278 	u_int32_t nick;
   1279 	char *dpos = *dposp, *cp2;
   1280 	int deltasec, error = 0;
   1281 
   1282 	memset(&ktvout, 0, sizeof ktvout);	 /* XXX gcc */
   1283 
   1284 	if (len == (3 * NFSX_UNSIGNED)) {
   1285 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1286 		ktvin.tv_sec = *tl++;
   1287 		ktvin.tv_usec = *tl++;
   1288 		nick = fxdr_unsigned(u_int32_t, *tl);
   1289 
   1290 		/*
   1291 		 * Decrypt the timestamp in ecb mode.
   1292 		 */
   1293 #ifdef NFSKERB
   1294 		XXX
   1295 #endif
   1296 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1297 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1298 		deltasec = time_second - ktvout.tv_sec;
   1299 		if (deltasec < 0)
   1300 			deltasec = -deltasec;
   1301 		/*
   1302 		 * If ok, add it to the hash list for the mount point.
   1303 		 */
   1304 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1305 			if (nmp->nm_numuids < nuidhash_max) {
   1306 				nmp->nm_numuids++;
   1307 				nuidp = (struct nfsuid *)
   1308 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1309 					M_WAITOK);
   1310 			} else {
   1311 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
   1312 				LIST_REMOVE(nuidp, nu_hash);
   1313 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1314 					nu_lru);
   1315 			}
   1316 			nuidp->nu_flag = 0;
   1317 			kauth_cred_seteuid(nuidp->nu_cr, kauth_cred_geteuid(cred));
   1318 			nuidp->nu_expire = time_second + NFS_KERBTTL;
   1319 			nuidp->nu_timestamp = ktvout;
   1320 			nuidp->nu_nickname = nick;
   1321 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
   1322 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1323 				nu_lru);
   1324 			LIST_INSERT_HEAD(NMUIDHASH(nmp, kauth_cred_geteuid(cred)),
   1325 				nuidp, nu_hash);
   1326 		}
   1327 	} else
   1328 		nfsm_adv(nfsm_rndup(len));
   1329 nfsmout:
   1330 	*mdp = md;
   1331 	*dposp = dpos;
   1332 	return (error);
   1333 }
   1334 #endif /* NFS */
   1335