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