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