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