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