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