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