Home | History | Annotate | Line # | Download | only in nfs
nfs_syscalls.c revision 1.36.2.1
      1 /*	$NetBSD: nfs_syscalls.c,v 1.36.2.1 2000/11/20 18:11:19 bouyer 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 #ifdef DEBUG
    640 				extern void printlockedvnodes(void);
    641 				printlockedvnodes();
    642 #endif
    643 				printf("nfsd: locking botch in op %d"
    644 				    " (before %d, after %d)\n",
    645 				    nd ? nd->nd_procnum : -1,
    646 				    lockcount, p->p_locks);
    647 			}
    648 #endif
    649 			if (mreq == NULL)
    650 				break;
    651 			if (error) {
    652 				if (nd->nd_procnum != NQNFSPROC_VACATED)
    653 					nfsstats.srv_errs++;
    654 				nfsrv_updatecache(nd, FALSE, mreq);
    655 				if (nd->nd_nam2)
    656 					m_freem(nd->nd_nam2);
    657 				break;
    658 			}
    659 			nfsstats.srvrpccnt[nd->nd_procnum]++;
    660 			nfsrv_updatecache(nd, TRUE, mreq);
    661 			nd->nd_mrep = (struct mbuf *)0;
    662 		    case RC_REPLY:
    663 			m = mreq;
    664 			siz = 0;
    665 			while (m) {
    666 				siz += m->m_len;
    667 				m = m->m_next;
    668 			}
    669 			if (siz <= 0 || siz > NFS_MAXPACKET) {
    670 				printf("mbuf siz=%d\n",siz);
    671 				panic("Bad nfs svc reply");
    672 			}
    673 			m = mreq;
    674 			m->m_pkthdr.len = siz;
    675 			m->m_pkthdr.rcvif = (struct ifnet *)0;
    676 			/*
    677 			 * For stream protocols, prepend a Sun RPC
    678 			 * Record Mark.
    679 			 */
    680 			if (sotype == SOCK_STREAM) {
    681 				M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
    682 				*mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
    683 			}
    684 			if (solockp)
    685 				(void) nfs_sndlock(solockp, (struct nfsreq *)0);
    686 			if (slp->ns_flag & SLP_VALID)
    687 			    error = nfs_send(so, nd->nd_nam2, m, NULL);
    688 			else {
    689 			    error = EPIPE;
    690 			    m_freem(m);
    691 			}
    692 			if (nfsrtton)
    693 				nfsd_rt(sotype, nd, cacherep);
    694 			if (nd->nd_nam2)
    695 				MFREE(nd->nd_nam2, m);
    696 			if (nd->nd_mrep)
    697 				m_freem(nd->nd_mrep);
    698 			if (error == EPIPE)
    699 				nfsrv_zapsock(slp);
    700 			if (solockp)
    701 				nfs_sndunlock(solockp);
    702 			if (error == EINTR || error == ERESTART) {
    703 				free((caddr_t)nd, M_NFSRVDESC);
    704 				nfsrv_slpderef(slp);
    705 				s = splsoftnet();
    706 				goto done;
    707 			}
    708 			break;
    709 		    case RC_DROPIT:
    710 			if (nfsrtton)
    711 				nfsd_rt(sotype, nd, cacherep);
    712 			m_freem(nd->nd_mrep);
    713 			m_freem(nd->nd_nam2);
    714 			break;
    715 		    };
    716 		    if (nd) {
    717 			FREE((caddr_t)nd, M_NFSRVDESC);
    718 			nd = NULL;
    719 		    }
    720 
    721 		    /*
    722 		     * Check to see if there are outstanding writes that
    723 		     * need to be serviced.
    724 		     */
    725 		    cur_usec = (u_quad_t)time.tv_sec * 1000000 +
    726 			(u_quad_t)time.tv_usec;
    727 		    s = splsoftclock();
    728 		    if (slp->ns_tq.lh_first &&
    729 			slp->ns_tq.lh_first->nd_time <= cur_usec) {
    730 			cacherep = RC_DOIT;
    731 			writes_todo = 1;
    732 		    } else
    733 			writes_todo = 0;
    734 		    splx(s);
    735 		} while (writes_todo);
    736 		s = splsoftnet();
    737 		if (nfsrv_dorec(slp, nfsd, &nd)) {
    738 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
    739 			nfsd->nfsd_slp = NULL;
    740 			nfsrv_slpderef(slp);
    741 		}
    742 	}
    743 done:
    744 	p->p_holdcnt--;
    745 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
    746 	splx(s);
    747 	free((caddr_t)nfsd, M_NFSD);
    748 	nsd->nsd_nfsd = (struct nfsd *)0;
    749 	if (--nfs_numnfsd == 0)
    750 		nfsrv_init(TRUE);	/* Reinitialize everything */
    751 	return (error);
    752 }
    753 
    754 /*
    755  * Shut down a socket associated with an nfssvc_sock structure.
    756  * Should be called with the send lock set, if required.
    757  * The trick here is to increment the sref at the start, so that the nfsds
    758  * will stop using it and clear ns_flag at the end so that it will not be
    759  * reassigned during cleanup.
    760  */
    761 void
    762 nfsrv_zapsock(slp)
    763 	struct nfssvc_sock *slp;
    764 {
    765 	struct nfsuid *nuidp, *nnuidp;
    766 	struct nfsrv_descript *nwp, *nnwp;
    767 	struct socket *so;
    768 	struct file *fp;
    769 	struct mbuf *m;
    770 	int s;
    771 
    772 	slp->ns_flag &= ~SLP_ALLFLAGS;
    773 	fp = slp->ns_fp;
    774 	if (fp) {
    775 		FILE_USE(fp);
    776 		slp->ns_fp = (struct file *)0;
    777 		so = slp->ns_so;
    778 		so->so_upcall = NULL;
    779 		so->so_upcallarg = NULL;
    780 		so->so_rcv.sb_flags &= ~SB_UPCALL;
    781 		soshutdown(so, 2);
    782 		closef(fp, (struct proc *)0);
    783 		if (slp->ns_nam)
    784 			MFREE(slp->ns_nam, m);
    785 		m_freem(slp->ns_raw);
    786 		m_freem(slp->ns_rec);
    787 		for (nuidp = slp->ns_uidlruhead.tqh_first; nuidp != 0;
    788 		    nuidp = nnuidp) {
    789 			nnuidp = nuidp->nu_lru.tqe_next;
    790 			LIST_REMOVE(nuidp, nu_hash);
    791 			TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
    792 			if (nuidp->nu_flag & NU_NAM)
    793 				m_freem(nuidp->nu_nam);
    794 			free((caddr_t)nuidp, M_NFSUID);
    795 		}
    796 		s = splsoftclock();
    797 		for (nwp = slp->ns_tq.lh_first; nwp; nwp = nnwp) {
    798 			nnwp = nwp->nd_tq.le_next;
    799 			LIST_REMOVE(nwp, nd_tq);
    800 			free((caddr_t)nwp, M_NFSRVDESC);
    801 		}
    802 		LIST_INIT(&slp->ns_tq);
    803 		splx(s);
    804 	}
    805 }
    806 
    807 /*
    808  * Derefence a server socket structure. If it has no more references and
    809  * is no longer valid, you can throw it away.
    810  */
    811 void
    812 nfsrv_slpderef(slp)
    813 	struct nfssvc_sock *slp;
    814 {
    815 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
    816 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    817 		free((caddr_t)slp, M_NFSSVC);
    818 	}
    819 }
    820 
    821 /*
    822  * Initialize the data structures for the server.
    823  * Handshake with any new nfsds starting up to avoid any chance of
    824  * corruption.
    825  */
    826 void
    827 nfsrv_init(terminating)
    828 	int terminating;
    829 {
    830 	struct nfssvc_sock *slp, *nslp;
    831 
    832 	if (nfssvc_sockhead_flag & SLP_INIT)
    833 		panic("nfsd init");
    834 	nfssvc_sockhead_flag |= SLP_INIT;
    835 	if (terminating) {
    836 		for (slp = nfssvc_sockhead.tqh_first; slp != 0; slp = nslp) {
    837 			nslp = slp->ns_chain.tqe_next;
    838 			if (slp->ns_flag & SLP_VALID)
    839 				nfsrv_zapsock(slp);
    840 			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
    841 			free((caddr_t)slp, M_NFSSVC);
    842 		}
    843 		nfsrv_cleancache();	/* And clear out server cache */
    844 	} else
    845 		nfs_pub.np_valid = 0;
    846 
    847 	TAILQ_INIT(&nfssvc_sockhead);
    848 	nfssvc_sockhead_flag &= ~SLP_INIT;
    849 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
    850 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
    851 		wakeup((caddr_t)&nfssvc_sockhead);
    852 	}
    853 
    854 	TAILQ_INIT(&nfsd_head);
    855 	nfsd_head_flag &= ~NFSD_CHECKSLP;
    856 
    857 	nfs_udpsock = (struct nfssvc_sock *)
    858 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    859 	memset((caddr_t)nfs_udpsock, 0, sizeof (struct nfssvc_sock));
    860 	TAILQ_INIT(&nfs_udpsock->ns_uidlruhead);
    861 	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
    862 
    863 #ifdef INET6
    864 	nfs_udp6sock = (struct nfssvc_sock *)
    865 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    866 	memset((caddr_t)nfs_udp6sock, 0, sizeof (struct nfssvc_sock));
    867 	TAILQ_INIT(&nfs_udp6sock->ns_uidlruhead);
    868 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_udp6sock, ns_chain);
    869 #endif
    870 
    871 	nfs_cltpsock = (struct nfssvc_sock *)
    872 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
    873 	memset((caddr_t)nfs_cltpsock, 0, sizeof (struct nfssvc_sock));
    874 	TAILQ_INIT(&nfs_cltpsock->ns_uidlruhead);
    875 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, nfs_cltpsock, ns_chain);
    876 }
    877 
    878 /*
    879  * Add entries to the server monitor log.
    880  */
    881 static void
    882 nfsd_rt(sotype, nd, cacherep)
    883 	int sotype;
    884 	struct nfsrv_descript *nd;
    885 	int cacherep;
    886 {
    887 	struct drt *rt;
    888 
    889 	rt = &nfsdrt.drt[nfsdrt.pos];
    890 	if (cacherep == RC_DOIT)
    891 		rt->flag = 0;
    892 	else if (cacherep == RC_REPLY)
    893 		rt->flag = DRT_CACHEREPLY;
    894 	else
    895 		rt->flag = DRT_CACHEDROP;
    896 	if (sotype == SOCK_STREAM)
    897 		rt->flag |= DRT_TCP;
    898 	if (nd->nd_flag & ND_NQNFS)
    899 		rt->flag |= DRT_NQNFS;
    900 	else if (nd->nd_flag & ND_NFSV3)
    901 		rt->flag |= DRT_NFSV3;
    902 	rt->proc = nd->nd_procnum;
    903 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
    904 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
    905 	else
    906 	    rt->ipadr = INADDR_ANY;
    907 	rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
    908 		(time.tv_usec - nd->nd_starttime.tv_usec);
    909 	rt->tstamp = time;
    910 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
    911 }
    912 #endif /* NFSSERVER */
    913 
    914 #ifdef NFS
    915 
    916 int nfs_defect = 0;
    917 /*
    918  * Asynchronous I/O threads for client nfs.
    919  * They do read-ahead and write-behind operations on the block I/O cache.
    920  * Never returns unless it fails or gets killed.
    921  */
    922 
    923 int
    924 nfssvc_iod(p)
    925 	struct proc *p;
    926 {
    927 	struct buf *bp;
    928 	int i, myiod;
    929 	struct nfsmount *nmp;
    930 	int error = 0;
    931 
    932 	/*
    933 	 * Assign my position or return error if too many already running
    934 	 */
    935 	myiod = -1;
    936 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
    937 		if (nfs_asyncdaemon[i] == NULL) {
    938 			myiod = i;
    939 			break;
    940 		}
    941 	if (myiod == -1)
    942 		return (EBUSY);
    943 	nfs_asyncdaemon[myiod] = p;
    944 	nfs_numasync++;
    945 	p->p_holdcnt++;
    946 	/*
    947 	 * Just loop around doing our stuff until SIGKILL
    948 	 */
    949 	for (;;) {
    950 	    while (((nmp = nfs_iodmount[myiod]) == NULL
    951 		    || nmp->nm_bufq.tqh_first == NULL)
    952 		    && error == 0) {
    953 		if (nmp)
    954 		    nmp->nm_bufqiods--;
    955 		nfs_iodwant[myiod] = p;
    956 		nfs_iodmount[myiod] = NULL;
    957 		error = tsleep((caddr_t)&nfs_iodwant[myiod],
    958 			PWAIT | PCATCH, "nfsidl", 0);
    959 	    }
    960 	    while (nmp != NULL && (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 	    if (error) {
    983 		    break;
    984 	    }
    985 	}
    986 	p->p_holdcnt--;
    987 	if (nmp)
    988 		nmp->nm_bufqiods--;
    989 	nfs_iodwant[myiod] = NULL;
    990 	nfs_iodmount[myiod] = NULL;
    991 	nfs_asyncdaemon[myiod] = NULL;
    992 	nfs_numasync--;
    993 
    994 	return (error);
    995 }
    996 
    997 void
    998 start_nfsio(arg)
    999 	void *arg;
   1000 {
   1001 	nfssvc_iod(curproc);
   1002 
   1003 	kthread_exit(0);
   1004 }
   1005 
   1006 void
   1007 nfs_getset_niothreads(set)
   1008 	int set;
   1009 {
   1010 	int i, have, start;
   1011 
   1012 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
   1013 		if (nfs_asyncdaemon[i] != NULL)
   1014 			have++;
   1015 
   1016 	if (set) {
   1017 		/* clamp to sane range */
   1018 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
   1019 
   1020 		start = nfs_niothreads - have;
   1021 
   1022 		while (start > 0) {
   1023 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
   1024 			start--;
   1025 		}
   1026 
   1027 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
   1028 			if (nfs_asyncdaemon[i] != NULL) {
   1029 				psignal(nfs_asyncdaemon[i], SIGKILL);
   1030 				start++;
   1031 			}
   1032 	} else {
   1033 		if (nfs_niothreads >= 0)
   1034 			nfs_niothreads = have;
   1035 	}
   1036 }
   1037 
   1038 /*
   1039  * Get an authorization string for the uid by having the mount_nfs sitting
   1040  * on this mount point porpous out of the kernel and do it.
   1041  */
   1042 int
   1043 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
   1044 	struct nfsmount *nmp;
   1045 	struct nfsreq *rep;
   1046 	struct ucred *cred;
   1047 	char **auth_str;
   1048 	int *auth_len;
   1049 	char *verf_str;
   1050 	int *verf_len;
   1051 	NFSKERBKEY_T key;		/* return session key */
   1052 {
   1053 	int error = 0;
   1054 
   1055 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
   1056 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
   1057 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
   1058 			"nfsauth1", 2 * hz);
   1059 		error = nfs_sigintr(nmp, rep, rep->r_procp);
   1060 		if (error) {
   1061 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1062 			return (error);
   1063 		}
   1064 	}
   1065 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
   1066 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
   1067 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
   1068 	nmp->nm_verfstr = verf_str;
   1069 	nmp->nm_verflen = *verf_len;
   1070 	nmp->nm_authuid = cred->cr_uid;
   1071 	wakeup((caddr_t)&nmp->nm_authstr);
   1072 
   1073 	/*
   1074 	 * And wait for mount_nfs to do its stuff.
   1075 	 */
   1076 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
   1077 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
   1078 			"nfsauth2", 2 * hz);
   1079 		error = nfs_sigintr(nmp, rep, rep->r_procp);
   1080 	}
   1081 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
   1082 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
   1083 		error = EAUTH;
   1084 	}
   1085 	if (error)
   1086 		free((caddr_t)*auth_str, M_TEMP);
   1087 	else {
   1088 		*auth_len = nmp->nm_authlen;
   1089 		*verf_len = nmp->nm_verflen;
   1090 		memcpy((caddr_t)key, (caddr_t)nmp->nm_key, sizeof (key));
   1091 	}
   1092 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
   1093 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
   1094 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
   1095 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
   1096 		wakeup((caddr_t)&nmp->nm_authtype);
   1097 	}
   1098 	return (error);
   1099 }
   1100 
   1101 /*
   1102  * Get a nickname authenticator and verifier.
   1103  */
   1104 int
   1105 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
   1106 	struct nfsmount *nmp;
   1107 	struct ucred *cred;
   1108 	char **auth_str;
   1109 	int *auth_len;
   1110 	char *verf_str;
   1111 	int verf_len;
   1112 {
   1113 	struct nfsuid *nuidp;
   1114 	u_int32_t *nickp, *verfp;
   1115 	struct timeval ktvin, ktvout;
   1116 
   1117 #ifdef DIAGNOSTIC
   1118 	if (verf_len < (4 * NFSX_UNSIGNED))
   1119 		panic("nfs_getnickauth verf too small");
   1120 #endif
   1121 	for (nuidp = NMUIDHASH(nmp, cred->cr_uid)->lh_first;
   1122 	    nuidp != 0; nuidp = nuidp->nu_hash.le_next) {
   1123 		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
   1124 			break;
   1125 	}
   1126 	if (!nuidp || nuidp->nu_expire < time.tv_sec)
   1127 		return (EACCES);
   1128 
   1129 	/*
   1130 	 * Move to the end of the lru list (end of lru == most recently used).
   1131 	 */
   1132 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1133 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
   1134 
   1135 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
   1136 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1137 	*nickp = txdr_unsigned(nuidp->nu_nickname);
   1138 	*auth_str = (char *)nickp;
   1139 	*auth_len = 2 * NFSX_UNSIGNED;
   1140 
   1141 	/*
   1142 	 * Now we must encrypt the verifier and package it up.
   1143 	 */
   1144 	verfp = (u_int32_t *)verf_str;
   1145 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
   1146 	if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
   1147 	    (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
   1148 	     time.tv_usec > nuidp->nu_timestamp.tv_usec))
   1149 		nuidp->nu_timestamp = time;
   1150 	else
   1151 		nuidp->nu_timestamp.tv_usec++;
   1152 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
   1153 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
   1154 
   1155 	/*
   1156 	 * Now encrypt the timestamp verifier in ecb mode using the session
   1157 	 * key.
   1158 	 */
   1159 #ifdef NFSKERB
   1160 	XXX
   1161 #endif
   1162 
   1163 	*verfp++ = ktvout.tv_sec;
   1164 	*verfp++ = ktvout.tv_usec;
   1165 	*verfp = 0;
   1166 	return (0);
   1167 }
   1168 
   1169 /*
   1170  * Save the current nickname in a hash list entry on the mount point.
   1171  */
   1172 int
   1173 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
   1174 	struct nfsmount *nmp;
   1175 	struct ucred *cred;
   1176 	int len;
   1177 	NFSKERBKEY_T key;
   1178 	struct mbuf **mdp;
   1179 	char **dposp;
   1180 	struct mbuf *mrep;
   1181 {
   1182 	struct nfsuid *nuidp;
   1183 	u_int32_t *tl;
   1184 	int32_t t1;
   1185 	struct mbuf *md = *mdp;
   1186 	struct timeval ktvin, ktvout;
   1187 	u_int32_t nick;
   1188 	char *dpos = *dposp, *cp2;
   1189 	int deltasec, error = 0;
   1190 
   1191 	if (len == (3 * NFSX_UNSIGNED)) {
   1192 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
   1193 		ktvin.tv_sec = *tl++;
   1194 		ktvin.tv_usec = *tl++;
   1195 		nick = fxdr_unsigned(u_int32_t, *tl);
   1196 
   1197 		/*
   1198 		 * Decrypt the timestamp in ecb mode.
   1199 		 */
   1200 #ifdef NFSKERB
   1201 		XXX
   1202 #endif
   1203 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
   1204 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
   1205 		deltasec = time.tv_sec - ktvout.tv_sec;
   1206 		if (deltasec < 0)
   1207 			deltasec = -deltasec;
   1208 		/*
   1209 		 * If ok, add it to the hash list for the mount point.
   1210 		 */
   1211 		if (deltasec <= NFS_KERBCLOCKSKEW) {
   1212 			if (nmp->nm_numuids < nuidhash_max) {
   1213 				nmp->nm_numuids++;
   1214 				nuidp = (struct nfsuid *)
   1215 				   malloc(sizeof (struct nfsuid), M_NFSUID,
   1216 					M_WAITOK);
   1217 			} else {
   1218 				nuidp = nmp->nm_uidlruhead.tqh_first;
   1219 				LIST_REMOVE(nuidp, nu_hash);
   1220 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
   1221 					nu_lru);
   1222 			}
   1223 			nuidp->nu_flag = 0;
   1224 			nuidp->nu_cr.cr_uid = cred->cr_uid;
   1225 			nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
   1226 			nuidp->nu_timestamp = ktvout;
   1227 			nuidp->nu_nickname = nick;
   1228 			memcpy(nuidp->nu_key, key, sizeof (key));
   1229 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
   1230 				nu_lru);
   1231 			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
   1232 				nuidp, nu_hash);
   1233 		}
   1234 	} else
   1235 		nfsm_adv(nfsm_rndup(len));
   1236 nfsmout:
   1237 	*mdp = md;
   1238 	*dposp = dpos;
   1239 	return (error);
   1240 }
   1241 #endif /* NFS */
   1242