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