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