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