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