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