xref: /netbsd-src/sys/nfs/nfs_syscalls.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: nfs_syscalls.c,v 1.81 2005/09/11 20:19:31 rpaulo Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Rick Macklem at The University of Guelph.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
35  */
36 
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: nfs_syscalls.c,v 1.81 2005/09/11 20:19:31 rpaulo Exp $");
39 
40 #include "fs_nfs.h"
41 #include "opt_nfs.h"
42 #include "opt_nfsserver.h"
43 #include "opt_iso.h"
44 #include "opt_inet.h"
45 #include "opt_compat_netbsd.h"
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/uio.h>
56 #include <sys/malloc.h>
57 #include <sys/buf.h>
58 #include <sys/mbuf.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
61 #include <sys/signalvar.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/namei.h>
65 #include <sys/syslog.h>
66 #include <sys/filedesc.h>
67 #include <sys/kthread.h>
68 
69 #include <sys/sa.h>
70 #include <sys/syscallargs.h>
71 
72 #include <netinet/in.h>
73 #include <netinet/tcp.h>
74 #ifdef ISO
75 #include <netiso/iso.h>
76 #endif
77 #include <nfs/xdr_subs.h>
78 #include <nfs/rpcv2.h>
79 #include <nfs/nfsproto.h>
80 #include <nfs/nfs.h>
81 #include <nfs/nfsm_subs.h>
82 #include <nfs/nfsrvcache.h>
83 #include <nfs/nfsmount.h>
84 #include <nfs/nfsnode.h>
85 #include <nfs/nqnfs.h>
86 #include <nfs/nfsrtt.h>
87 #include <nfs/nfs_var.h>
88 
89 /* Global defs. */
90 extern int32_t (*nfsrv3_procs[NFS_NPROCS]) __P((struct nfsrv_descript *,
91 						struct nfssvc_sock *,
92 						struct proc *, struct mbuf **));
93 extern time_t nqnfsstarttime;
94 extern int nfsrvw_procrastinate;
95 
96 struct nfssvc_sock *nfs_udpsock;
97 #ifdef ISO
98 struct nfssvc_sock *nfs_cltpsock;
99 #endif
100 #ifdef INET6
101 struct nfssvc_sock *nfs_udp6sock;
102 #endif
103 int nuidhash_max = NFS_MAXUIDHASH;
104 int nfsd_waiting = 0;
105 #ifdef NFSSERVER
106 static int nfs_numnfsd = 0;
107 static int notstarted = 1;
108 static int modify_flag = 0;
109 static struct nfsdrt nfsdrt;
110 #endif
111 
112 #ifdef NFSSERVER
113 struct simplelock nfsd_slock = SIMPLELOCK_INITIALIZER;
114 struct nfssvc_sockhead nfssvc_sockhead;
115 struct nfssvc_sockhead nfssvc_sockpending;
116 struct nfsdhead nfsd_head;
117 struct nfsdidlehead nfsd_idle_head;
118 
119 int nfssvc_sockhead_flag;
120 int nfsd_head_flag;
121 #endif
122 
123 MALLOC_DEFINE(M_NFSUID, "NFS uid", "Nfs uid mapping structure");
124 
125 #ifdef NFS
126 struct nfs_iod nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
127 int nfs_niothreads = -1; /* == "0, and has never been set" */
128 #endif
129 
130 #ifdef NFSSERVER
131 static void nfsd_rt __P((int, struct nfsrv_descript *, int));
132 static struct nfssvc_sock *nfsrv_sockalloc __P((void));
133 static void nfsrv_sockfree __P((struct nfssvc_sock *));
134 #endif
135 
136 /*
137  * NFS server system calls
138  */
139 
140 
141 /*
142  * Nfs server pseudo system call for the nfsd's
143  * Based on the flag value it either:
144  * - adds a socket to the selection list
145  * - remains in the kernel as an nfsd
146  * - remains in the kernel as an nfsiod
147  */
148 int
149 sys_nfssvc(l, v, retval)
150 	struct lwp *l;
151 	void *v;
152 	register_t *retval;
153 {
154 	struct sys_nfssvc_args /* {
155 		syscallarg(int) flag;
156 		syscallarg(caddr_t) argp;
157 	} */ *uap = v;
158 	struct proc *p = l->l_proc;
159 	int error;
160 #ifdef NFS
161 	struct nameidata nd;
162 	struct nfsmount *nmp;
163 	struct nfsd_cargs ncd;
164 #endif
165 #ifdef NFSSERVER
166 	int s;
167 	struct file *fp;
168 	struct mbuf *nam;
169 	struct nfsd_args nfsdarg;
170 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
171 	struct nfsd *nfsd;
172 	struct nfssvc_sock *slp;
173 	struct nfsuid *nuidp;
174 #endif
175 
176 	/*
177 	 * Must be super user
178 	 */
179 	error = suser(p->p_ucred, &p->p_acflag);
180 	if (error)
181 		return (error);
182 #ifdef NFSSERVER
183 	s = splsoftnet();
184 	simple_lock(&nfsd_slock);
185 	while (nfssvc_sockhead_flag & SLP_INIT) {
186 		nfssvc_sockhead_flag |= SLP_WANTINIT;
187 		(void) ltsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0,
188 		    &nfsd_slock);
189 	}
190 	simple_unlock(&nfsd_slock);
191 	splx(s);
192 #endif
193 	if (SCARG(uap, flag) & NFSSVC_BIOD) {
194 #if defined(NFS) && defined(COMPAT_14)
195 		error = nfssvc_iod(l);
196 #else
197 		error = ENOSYS;
198 #endif
199 	} else if (SCARG(uap, flag) & NFSSVC_MNTD) {
200 #ifndef NFS
201 		error = ENOSYS;
202 #else
203 		error = copyin(SCARG(uap, argp), (caddr_t)&ncd, sizeof (ncd));
204 		if (error)
205 			return (error);
206 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
207 			ncd.ncd_dirp, p);
208 		error = namei(&nd);
209 		if (error)
210 			return (error);
211 		if ((nd.ni_vp->v_flag & VROOT) == 0)
212 			error = EINVAL;
213 		nmp = VFSTONFS(nd.ni_vp->v_mount);
214 		vput(nd.ni_vp);
215 		if (error)
216 			return (error);
217 		if ((nmp->nm_iflag & NFSMNT_MNTD) &&
218 			(SCARG(uap, flag) & NFSSVC_GOTAUTH) == 0)
219 			return (0);
220 		nmp->nm_iflag |= NFSMNT_MNTD;
221 		error = nqnfs_clientd(nmp, p->p_ucred, &ncd, SCARG(uap, flag),
222 			SCARG(uap, argp), l);
223 #endif /* NFS */
224 	} else if (SCARG(uap, flag) & NFSSVC_ADDSOCK) {
225 #ifndef NFSSERVER
226 		error = ENOSYS;
227 #else
228 		error = copyin(SCARG(uap, argp), (caddr_t)&nfsdarg,
229 		    sizeof(nfsdarg));
230 		if (error)
231 			return (error);
232 		/* getsock() will use the descriptor for us */
233 		error = getsock(p->p_fd, nfsdarg.sock, &fp);
234 		if (error)
235 			return (error);
236 		/*
237 		 * Get the client address for connected sockets.
238 		 */
239 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
240 			nam = (struct mbuf *)0;
241 		else {
242 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
243 				MT_SONAME);
244 			if (error) {
245 				FILE_UNUSE(fp, NULL);
246 				return (error);
247 			}
248 		}
249 		error = nfssvc_addsock(fp, nam);
250 		FILE_UNUSE(fp, NULL);
251 #endif /* !NFSSERVER */
252 	} else {
253 #ifndef NFSSERVER
254 		error = ENOSYS;
255 #else
256 		error = copyin(SCARG(uap, argp), (caddr_t)nsd, sizeof (*nsd));
257 		if (error)
258 			return (error);
259 		if ((SCARG(uap, flag) & NFSSVC_AUTHIN) &&
260 		    ((nfsd = nsd->nsd_nfsd)) != NULL &&
261 		    (nfsd->nfsd_slp->ns_flag & SLP_VALID)) {
262 			slp = nfsd->nfsd_slp;
263 
264 			/*
265 			 * First check to see if another nfsd has already
266 			 * added this credential.
267 			 */
268 			LIST_FOREACH(nuidp, NUIDHASH(slp,nsd->nsd_cr.cr_uid),
269 			    nu_hash) {
270 				if (nuidp->nu_cr.cr_uid == nsd->nsd_cr.cr_uid &&
271 				    (!nfsd->nfsd_nd->nd_nam2 ||
272 				     netaddr_match(NU_NETFAM(nuidp),
273 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
274 					break;
275 			}
276 			if (nuidp) {
277 			    nfsrv_setcred(&nuidp->nu_cr,&nfsd->nfsd_nd->nd_cr);
278 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
279 			} else {
280 			    /*
281 			     * Nope, so we will.
282 			     */
283 			    if (slp->ns_numuids < nuidhash_max) {
284 				slp->ns_numuids++;
285 				nuidp = (struct nfsuid *)
286 				   malloc(sizeof (struct nfsuid), M_NFSUID,
287 					M_WAITOK);
288 			    } else
289 				nuidp = (struct nfsuid *)0;
290 			    if ((slp->ns_flag & SLP_VALID) == 0) {
291 				if (nuidp)
292 				    free((caddr_t)nuidp, M_NFSUID);
293 			    } else {
294 				if (nuidp == (struct nfsuid *)0) {
295 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
296 				    LIST_REMOVE(nuidp, nu_hash);
297 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
298 					nu_lru);
299 				    if (nuidp->nu_flag & NU_NAM)
300 					m_freem(nuidp->nu_nam);
301 			        }
302 				nuidp->nu_flag = 0;
303 				crcvt(&nuidp->nu_cr, &nsd->nsd_cr);
304 				if (nuidp->nu_cr.cr_ngroups > NGROUPS)
305 				    nuidp->nu_cr.cr_ngroups = NGROUPS;
306 				nuidp->nu_cr.cr_ref = 1;
307 				nuidp->nu_timestamp = nsd->nsd_timestamp;
308 				nuidp->nu_expire = time.tv_sec + nsd->nsd_ttl;
309 				/*
310 				 * and save the session key in nu_key.
311 				 */
312 				memcpy(nuidp->nu_key, nsd->nsd_key,
313 				    sizeof(nsd->nsd_key));
314 				if (nfsd->nfsd_nd->nd_nam2) {
315 				    struct sockaddr_in *saddr;
316 
317 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
318 					 struct sockaddr_in *);
319 				    switch (saddr->sin_family) {
320 				    case AF_INET:
321 					nuidp->nu_flag |= NU_INETADDR;
322 					nuidp->nu_inetaddr =
323 					     saddr->sin_addr.s_addr;
324 					break;
325 				    case AF_ISO:
326 				    default:
327 					nuidp->nu_flag |= NU_NAM;
328 					nuidp->nu_nam = m_copym(
329 					    nfsd->nfsd_nd->nd_nam2, 0,
330 					     M_COPYALL, M_WAIT);
331 					break;
332 				    };
333 				}
334 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
335 					nu_lru);
336 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
337 					nuidp, nu_hash);
338 				nfsrv_setcred(&nuidp->nu_cr,
339 				    &nfsd->nfsd_nd->nd_cr);
340 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
341 			    }
342 			}
343 		}
344 		if ((SCARG(uap, flag) & NFSSVC_AUTHINFAIL) &&
345 		    (nfsd = nsd->nsd_nfsd))
346 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
347 		error = nfssvc_nfsd(nsd, SCARG(uap, argp), l);
348 #endif /* !NFSSERVER */
349 	}
350 	if (error == EINTR || error == ERESTART)
351 		error = 0;
352 	return (error);
353 }
354 
355 #ifdef NFSSERVER
356 MALLOC_DEFINE(M_NFSD, "NFS daemon", "Nfs server daemon structure");
357 MALLOC_DEFINE(M_NFSSVC, "NFS srvsock", "Nfs server structure");
358 struct pool nfs_srvdesc_pool;
359 
360 static struct nfssvc_sock *
361 nfsrv_sockalloc()
362 {
363 	struct nfssvc_sock *slp;
364 	int s;
365 
366 	slp = (struct nfssvc_sock *)
367 	    malloc(sizeof (struct nfssvc_sock), M_NFSSVC, M_WAITOK);
368 	memset(slp, 0, sizeof (struct nfssvc_sock));
369 	TAILQ_INIT(&slp->ns_uidlruhead);
370 	s = splsoftnet();
371 	simple_lock(&nfsd_slock);
372 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
373 	simple_unlock(&nfsd_slock);
374 	splx(s);
375 
376 	return slp;
377 }
378 
379 static void
380 nfsrv_sockfree(struct nfssvc_sock *slp)
381 {
382 
383 	KASSERT(slp->ns_so == NULL);
384 	KASSERT(slp->ns_fp == NULL);
385 	KASSERT((slp->ns_flag & SLP_ALLFLAGS) == 0);
386 	free(slp, M_NFSSVC);
387 }
388 
389 /*
390  * Adds a socket to the list for servicing by nfsds.
391  */
392 int
393 nfssvc_addsock(fp, mynam)
394 	struct file *fp;
395 	struct mbuf *mynam;
396 {
397 	struct mbuf *m;
398 	int siz;
399 	struct nfssvc_sock *slp;
400 	struct socket *so;
401 	struct nfssvc_sock *tslp;
402 	int error, s;
403 
404 	so = (struct socket *)fp->f_data;
405 	tslp = (struct nfssvc_sock *)0;
406 	/*
407 	 * Add it to the list, as required.
408 	 */
409 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
410 #ifdef INET6
411 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
412 			tslp = nfs_udp6sock;
413 		else
414 #endif
415 		tslp = nfs_udpsock;
416 		if (tslp->ns_flag & SLP_VALID) {
417 			m_freem(mynam);
418 			return (EPERM);
419 		}
420 #ifdef ISO
421 	} else if (so->so_proto->pr_protocol == ISOPROTO_CLTP) {
422 		tslp = nfs_cltpsock;
423 		if (tslp->ns_flag & SLP_VALID) {
424 			m_freem(mynam);
425 			return (EPERM);
426 		}
427 #endif /* ISO */
428 	}
429 	if (so->so_type == SOCK_STREAM)
430 		siz = NFS_MAXPACKET + sizeof (u_long);
431 	else
432 		siz = NFS_MAXPACKET;
433 	error = soreserve(so, siz, siz);
434 	if (error) {
435 		m_freem(mynam);
436 		return (error);
437 	}
438 
439 	/*
440 	 * Set protocol specific options { for now TCP only } and
441 	 * reserve some space. For datagram sockets, this can get called
442 	 * repeatedly for the same socket, but that isn't harmful.
443 	 */
444 	if (so->so_type == SOCK_STREAM) {
445 		m = m_get(M_WAIT, MT_SOOPTS);
446 		MCLAIM(m, &nfs_mowner);
447 		*mtod(m, int32_t *) = 1;
448 		m->m_len = sizeof(int32_t);
449 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
450 	}
451 	if ((so->so_proto->pr_domain->dom_family == AF_INET
452 #ifdef INET6
453 	    || so->so_proto->pr_domain->dom_family == AF_INET6
454 #endif
455 	    ) &&
456 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
457 		m = m_get(M_WAIT, MT_SOOPTS);
458 		MCLAIM(m, &nfs_mowner);
459 		*mtod(m, int32_t *) = 1;
460 		m->m_len = sizeof(int32_t);
461 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
462 	}
463 	so->so_rcv.sb_flags &= ~SB_NOINTR;
464 	so->so_rcv.sb_timeo = 0;
465 	so->so_snd.sb_flags &= ~SB_NOINTR;
466 	so->so_snd.sb_timeo = 0;
467 	if (tslp)
468 		slp = tslp;
469 	else {
470 		slp = nfsrv_sockalloc();
471 	}
472 	slp->ns_so = so;
473 	slp->ns_nam = mynam;
474 	fp->f_count++;
475 	slp->ns_fp = fp;
476 	s = splsoftnet();
477 	so->so_upcallarg = (caddr_t)slp;
478 	so->so_upcall = nfsrv_rcv;
479 	so->so_rcv.sb_flags |= SB_UPCALL;
480 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
481 	nfsrv_wakenfsd(slp);
482 	splx(s);
483 	return (0);
484 }
485 
486 /*
487  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
488  * until it is killed by a signal.
489  */
490 int
491 nfssvc_nfsd(nsd, argp, l)
492 	struct nfsd_srvargs *nsd;
493 	caddr_t argp;
494 	struct lwp *l;
495 {
496 	struct mbuf *m;
497 	int siz;
498 	struct nfssvc_sock *slp;
499 	struct socket *so;
500 	int *solockp;
501 	struct nfsd *nfsd = nsd->nsd_nfsd;
502 	struct nfsrv_descript *nd = NULL;
503 	struct mbuf *mreq;
504 	int error = 0, cacherep, s, sotype, writes_todo;
505 	u_quad_t cur_usec;
506 	struct proc *p = l->l_proc;
507 
508 #ifndef nolint
509 	cacherep = RC_DOIT;
510 	writes_todo = 0;
511 #endif
512 	s = splsoftnet();
513 	if (nfsd == (struct nfsd *)0) {
514 		nsd->nsd_nfsd = nfsd = (struct nfsd *)
515 			malloc(sizeof (struct nfsd), M_NFSD, M_WAITOK);
516 		memset((caddr_t)nfsd, 0, sizeof (struct nfsd));
517 		nfsd->nfsd_procp = p;
518 		simple_lock(&nfsd_slock);
519 		TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
520 		nfs_numnfsd++;
521 		simple_unlock(&nfsd_slock);
522 	}
523 	PHOLD(l);
524 	/*
525 	 * Loop getting rpc requests until SIGKILL.
526 	 */
527 	for (;;) {
528 		if ((nfsd->nfsd_flag & NFSD_REQINPROG) == 0) {
529 			simple_lock(&nfsd_slock);
530 			while (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
531 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
532 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
533 				    nfsd_idle);
534 				nfsd->nfsd_flag |= NFSD_WAITING;
535 				nfsd_waiting++;
536 				error = ltsleep(nfsd, PSOCK | PCATCH, "nfsd",
537 				    0, &nfsd_slock);
538 				nfsd_waiting--;
539 				if (error) {
540 					slp = nfsd->nfsd_slp;
541 					nfsd->nfsd_slp = NULL;
542 					if (!slp)
543 						SLIST_REMOVE(&nfsd_idle_head,
544 						    nfsd, nfsd, nfsd_idle);
545 					simple_unlock(&nfsd_slock);
546 					if (slp) {
547 						nfsrv_wakenfsd(slp);
548 						nfsrv_slpderef(slp);
549 					}
550 					goto done;
551 				}
552 			}
553 			if (nfsd->nfsd_slp == (struct nfssvc_sock *)0 &&
554 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
555 				slp = TAILQ_FIRST(&nfssvc_sockpending);
556 				if (slp) {
557 					KASSERT((slp->ns_flag &
558 					    (SLP_VALID | SLP_DOREC))
559 					    == (SLP_VALID | SLP_DOREC));
560 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
561 					    ns_pending);
562 					slp->ns_flag &= ~SLP_DOREC;
563 					slp->ns_sref++;
564 					nfsd->nfsd_slp = slp;
565 				} else
566 					nfsd_head_flag &= ~NFSD_CHECKSLP;
567 			}
568 			simple_unlock(&nfsd_slock);
569 			if ((slp = nfsd->nfsd_slp) == (struct nfssvc_sock *)0)
570 				continue;
571 			if (slp->ns_flag & SLP_VALID) {
572 				if (slp->ns_flag & SLP_DISCONN)
573 					nfsrv_zapsock(slp);
574 				else if (slp->ns_flag & SLP_NEEDQ) {
575 					slp->ns_flag &= ~SLP_NEEDQ;
576 					(void) nfs_sndlock(&slp->ns_solock,
577 						(struct nfsreq *)0);
578 					nfsrv_rcv(slp->ns_so, (caddr_t)slp,
579 						M_WAIT);
580 					nfs_sndunlock(&slp->ns_solock);
581 				}
582 				error = nfsrv_dorec(slp, nfsd, &nd);
583 				cur_usec = (u_quad_t)time.tv_sec * 1000000 +
584 					(u_quad_t)time.tv_usec;
585 				if (error && LIST_FIRST(&slp->ns_tq) &&
586 				    LIST_FIRST(&slp->ns_tq)->nd_time <=
587 				    cur_usec) {
588 					error = 0;
589 					cacherep = RC_DOIT;
590 					writes_todo = 1;
591 				} else
592 					writes_todo = 0;
593 				nfsd->nfsd_flag |= NFSD_REQINPROG;
594 			}
595 		} else {
596 			error = 0;
597 			slp = nfsd->nfsd_slp;
598 		}
599 		if (error || (slp->ns_flag & SLP_VALID) == 0) {
600 			if (nd) {
601 				pool_put(&nfs_srvdesc_pool, nd);
602 				nd = NULL;
603 			}
604 			nfsd->nfsd_slp = (struct nfssvc_sock *)0;
605 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
606 			nfsrv_slpderef(slp);
607 			continue;
608 		}
609 		splx(s);
610 		so = slp->ns_so;
611 		sotype = so->so_type;
612 		if (so->so_proto->pr_flags & PR_CONNREQUIRED)
613 			solockp = &slp->ns_solock;
614 		else
615 			solockp = (int *)0;
616 		if (nd) {
617 			nd->nd_starttime = time;
618 			if (nd->nd_nam2)
619 				nd->nd_nam = nd->nd_nam2;
620 			else
621 				nd->nd_nam = slp->ns_nam;
622 
623 			/*
624 			 * Check to see if authorization is needed.
625 			 */
626 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
627 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
628 				nsd->nsd_haddr = mtod(nd->nd_nam,
629 				    struct sockaddr_in *)->sin_addr.s_addr;
630 				nsd->nsd_authlen = nfsd->nfsd_authlen;
631 				nsd->nsd_verflen = nfsd->nfsd_verflen;
632 				if (!copyout(nfsd->nfsd_authstr,
633 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
634 				    !copyout(nfsd->nfsd_verfstr,
635 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
636 				    !copyout(nsd, argp, sizeof (*nsd))) {
637 					PRELE(l);
638 					return (ENEEDAUTH);
639 				}
640 				cacherep = RC_DROPIT;
641 			} else
642 				cacherep = nfsrv_getcache(nd, slp, &mreq);
643 
644 			/*
645 			 * Check for just starting up for NQNFS and send
646 			 * fake "try again later" replies to the NQNFS clients.
647 			 */
648 			if (notstarted && nqnfsstarttime <= time.tv_sec) {
649 				if (modify_flag) {
650 					nqnfsstarttime =
651 					    time.tv_sec + nqsrv_writeslack;
652 					modify_flag = 0;
653 				} else
654 					notstarted = 0;
655 			}
656 			if (notstarted) {
657 				if ((nd->nd_flag & ND_NQNFS) == 0)
658 					cacherep = RC_DROPIT;
659 				else if (nd->nd_procnum != NFSPROC_WRITE) {
660 					nd->nd_procnum = NFSPROC_NOOP;
661 					nd->nd_repstat = NQNFS_TRYLATER;
662 					cacherep = RC_DOIT;
663 				} else
664 					modify_flag = 1;
665 			} else if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
666 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
667 				nd->nd_procnum = NFSPROC_NOOP;
668 				nd->nd_repstat =
669 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
670 				cacherep = RC_DOIT;
671 			}
672 		}
673 
674 		/*
675 		 * Loop to get all the write rpc relies that have been
676 		 * gathered together.
677 		 */
678 		do {
679 #ifdef DIAGNOSTIC
680 			int lockcount;
681 #endif
682 			switch (cacherep) {
683 			case RC_DOIT:
684 #ifdef DIAGNOSTIC
685 				/*
686 				 * NFS server procs should neither release
687 				 * locks already held, nor leave things
688 				 * locked.  Catch this sooner, rather than
689 				 * later (when we try to relock something we
690 				 * already have locked).  Careful inspection
691 				 * of the failing routine usually turns up the
692 				 * lock leak.. once we know what it is..
693 				 */
694 				lockcount = l->l_locks;
695 #endif
696 				mreq = NULL;
697 				if (writes_todo || (!(nd->nd_flag & ND_NFSV3) &&
698 				     nd->nd_procnum == NFSPROC_WRITE &&
699 				     nfsrvw_procrastinate > 0 && !notstarted))
700 					error = nfsrv_writegather(&nd, slp,
701 					    nfsd->nfsd_procp, &mreq);
702 				else
703 					error =
704 					    (*(nfsrv3_procs[nd->nd_procnum]))
705 					    (nd, slp, nfsd->nfsd_procp, &mreq);
706 #ifdef DIAGNOSTIC
707 				if (l->l_locks != lockcount) {
708 					/*
709 					 * If you see this panic, audit
710 					 * nfsrv3_procs[nd->nd_procnum] for
711 					 * vnode locking errors (usually, it's
712 					 * due to forgetting to vput()
713 					 * something).
714 					 */
715 #ifdef DEBUG
716 					extern void printlockedvnodes(void);
717 					printlockedvnodes();
718 #endif
719 					printf("nfsd: locking botch in op %d"
720 					    " (before %d, after %d)\n",
721 					    nd ? nd->nd_procnum : -1,
722 					    lockcount, l->l_locks);
723 				}
724 #endif
725 				if (mreq == NULL) {
726 					if (nd != NULL) {
727 						if (nd->nd_nam2)
728 							m_free(nd->nd_nam2);
729 						if (nd->nd_mrep)
730 							m_freem(nd->nd_mrep);
731 					}
732 					break;
733 				}
734 				if (error) {
735 					if (nd->nd_procnum != NQNFSPROC_VACATED)
736 						nfsstats.srv_errs++;
737 					nfsrv_updatecache(nd, FALSE, mreq);
738 					if (nd->nd_nam2)
739 						m_freem(nd->nd_nam2);
740 					break;
741 				}
742 				nfsstats.srvrpccnt[nd->nd_procnum]++;
743 				nfsrv_updatecache(nd, TRUE, mreq);
744 				nd->nd_mrep = (struct mbuf *)0;
745 			case RC_REPLY:
746 				m = mreq;
747 				siz = 0;
748 				while (m) {
749 					siz += m->m_len;
750 					m = m->m_next;
751 				}
752 				if (siz <= 0 || siz > NFS_MAXPACKET) {
753 					printf("mbuf siz=%d\n",siz);
754 					panic("Bad nfs svc reply");
755 				}
756 				m = mreq;
757 				m->m_pkthdr.len = siz;
758 				m->m_pkthdr.rcvif = (struct ifnet *)0;
759 				/*
760 				 * For stream protocols, prepend a Sun RPC
761 				 * Record Mark.
762 				 */
763 				if (sotype == SOCK_STREAM) {
764 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
765 					*mtod(m, u_int32_t *) =
766 					    htonl(0x80000000 | siz);
767 				}
768 				if (solockp)
769 					(void) nfs_sndlock(solockp, NULL);
770 				if (slp->ns_flag & SLP_VALID) {
771 					error =
772 					    nfs_send(so, nd->nd_nam2, m, NULL, p);
773 				} else {
774 					error = EPIPE;
775 					m_freem(m);
776 				}
777 				if (nfsrtton)
778 					nfsd_rt(sotype, nd, cacherep);
779 				if (nd->nd_nam2)
780 					m_free(nd->nd_nam2);
781 				if (nd->nd_mrep)
782 					m_freem(nd->nd_mrep);
783 				if (error == EPIPE)
784 					nfsrv_zapsock(slp);
785 				if (solockp)
786 					nfs_sndunlock(solockp);
787 				if (error == EINTR || error == ERESTART) {
788 					pool_put(&nfs_srvdesc_pool, nd);
789 					nfsrv_slpderef(slp);
790 					s = splsoftnet();
791 					goto done;
792 				}
793 				break;
794 			case RC_DROPIT:
795 				if (nfsrtton)
796 					nfsd_rt(sotype, nd, cacherep);
797 				m_freem(nd->nd_mrep);
798 				m_freem(nd->nd_nam2);
799 				break;
800 			}
801 			if (nd) {
802 				pool_put(&nfs_srvdesc_pool, nd);
803 				nd = NULL;
804 			}
805 
806 			/*
807 			 * Check to see if there are outstanding writes that
808 			 * need to be serviced.
809 			 */
810 			cur_usec = (u_quad_t)time.tv_sec * 1000000 +
811 			    (u_quad_t)time.tv_usec;
812 			s = splsoftclock();
813 			if (LIST_FIRST(&slp->ns_tq) &&
814 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
815 				cacherep = RC_DOIT;
816 				writes_todo = 1;
817 			} else
818 				writes_todo = 0;
819 			splx(s);
820 		} while (writes_todo);
821 		s = splsoftnet();
822 		if (nfsrv_dorec(slp, nfsd, &nd)) {
823 			nfsd->nfsd_flag &= ~NFSD_REQINPROG;
824 			nfsd->nfsd_slp = NULL;
825 			nfsrv_slpderef(slp);
826 		}
827 	}
828 done:
829 	PRELE(l);
830 	simple_lock(&nfsd_slock);
831 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
832 	simple_unlock(&nfsd_slock);
833 	splx(s);
834 	free((caddr_t)nfsd, M_NFSD);
835 	nsd->nsd_nfsd = (struct nfsd *)0;
836 	if (--nfs_numnfsd == 0)
837 		nfsrv_init(TRUE);	/* Reinitialize everything */
838 	return (error);
839 }
840 
841 /*
842  * Shut down a socket associated with an nfssvc_sock structure.
843  * Should be called with the send lock set, if required.
844  * The trick here is to increment the sref at the start, so that the nfsds
845  * will stop using it and clear ns_flag at the end so that it will not be
846  * reassigned during cleanup.
847  *
848  * called at splsoftnet.
849  */
850 void
851 nfsrv_zapsock(slp)
852 	struct nfssvc_sock *slp;
853 {
854 	struct nfsuid *nuidp, *nnuidp;
855 	struct nfsrv_descript *nwp, *nnwp;
856 	struct socket *so;
857 	struct file *fp;
858 	int s;
859 
860 	simple_lock(&nfsd_slock);
861 	if ((slp->ns_flag & SLP_VALID) == 0) {
862 		simple_unlock(&nfsd_slock);
863 		return;
864 	}
865 	if (slp->ns_flag & SLP_DOREC) {
866 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
867 	}
868 	slp->ns_flag &= ~SLP_ALLFLAGS;
869 	simple_unlock(&nfsd_slock);
870 
871 	fp = slp->ns_fp;
872 	slp->ns_fp = NULL;
873 	so = slp->ns_so;
874 	slp->ns_so = NULL;
875 	KASSERT(fp != NULL);
876 	KASSERT(so != NULL);
877 	KASSERT(fp->f_data == so);
878 	simple_lock(&fp->f_slock);
879 	FILE_USE(fp);
880 	so->so_upcall = NULL;
881 	so->so_upcallarg = NULL;
882 	so->so_rcv.sb_flags &= ~SB_UPCALL;
883 	soshutdown(so, SHUT_RDWR);
884 	closef(fp, (struct proc *)0);
885 
886 	if (slp->ns_nam)
887 		m_free(slp->ns_nam);
888 	m_freem(slp->ns_raw);
889 	m_freem(slp->ns_rec);
890 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
891 	    nuidp = nnuidp) {
892 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
893 		LIST_REMOVE(nuidp, nu_hash);
894 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
895 		if (nuidp->nu_flag & NU_NAM)
896 			m_freem(nuidp->nu_nam);
897 		free((caddr_t)nuidp, M_NFSUID);
898 	}
899 	s = splsoftclock();
900 	for (nwp = LIST_FIRST(&slp->ns_tq); nwp; nwp = nnwp) {
901 		nnwp = LIST_NEXT(nwp, nd_tq);
902 		LIST_REMOVE(nwp, nd_tq);
903 		pool_put(&nfs_srvdesc_pool, nwp);
904 	}
905 	LIST_INIT(&slp->ns_tq);
906 	splx(s);
907 }
908 
909 /*
910  * Derefence a server socket structure. If it has no more references and
911  * is no longer valid, you can throw it away.
912  */
913 void
914 nfsrv_slpderef(slp)
915 	struct nfssvc_sock *slp;
916 {
917 	LOCK_ASSERT(!simple_lock_held(&nfsd_slock));
918 
919 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
920 		int s = splsoftnet();
921 		simple_lock(&nfsd_slock);
922 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
923 		simple_unlock(&nfsd_slock);
924 		splx(s);
925 		nfsrv_sockfree(slp);
926 	}
927 }
928 
929 /*
930  * Initialize the data structures for the server.
931  * Handshake with any new nfsds starting up to avoid any chance of
932  * corruption.
933  */
934 void
935 nfsrv_init(terminating)
936 	int terminating;
937 {
938 	struct nfssvc_sock *slp;
939 	int s;
940 
941 	s = splsoftnet();
942 	simple_lock(&nfsd_slock);
943 	if (nfssvc_sockhead_flag & SLP_INIT)
944 		panic("nfsd init");
945 	nfssvc_sockhead_flag |= SLP_INIT;
946 
947 	if (terminating) {
948 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
949 			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
950 			simple_unlock(&nfsd_slock);
951 			if (slp->ns_flag & SLP_VALID)
952 				nfsrv_zapsock(slp);
953 			nfsrv_sockfree(slp);
954 			simple_lock(&nfsd_slock);
955 		}
956 		simple_unlock(&nfsd_slock);
957 		splx(s);
958 		nfsrv_cleancache();	/* And clear out server cache */
959 	} else {
960 		simple_unlock(&nfsd_slock);
961 		splx(s);
962 		nfs_pub.np_valid = 0;
963 	}
964 
965 	TAILQ_INIT(&nfssvc_sockhead);
966 	TAILQ_INIT(&nfssvc_sockpending);
967 	nfssvc_sockhead_flag &= ~SLP_INIT;
968 
969 	TAILQ_INIT(&nfsd_head);
970 	SLIST_INIT(&nfsd_idle_head);
971 	nfsd_head_flag &= ~NFSD_CHECKSLP;
972 
973 	nfs_udpsock = nfsrv_sockalloc();
974 
975 #ifdef INET6
976 	nfs_udp6sock = nfsrv_sockalloc();
977 #endif
978 
979 #ifdef ISO
980 	nfs_cltpsock = nfsrv_sockalloc();
981 #endif
982 
983 	simple_lock(&nfsd_slock);
984 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
985 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
986 		wakeup(&nfssvc_sockhead);
987 	}
988 	simple_unlock(&nfsd_slock);
989 	splx(s);
990 }
991 
992 /*
993  * Add entries to the server monitor log.
994  */
995 static void
996 nfsd_rt(sotype, nd, cacherep)
997 	int sotype;
998 	struct nfsrv_descript *nd;
999 	int cacherep;
1000 {
1001 	struct drt *rt;
1002 
1003 	rt = &nfsdrt.drt[nfsdrt.pos];
1004 	if (cacherep == RC_DOIT)
1005 		rt->flag = 0;
1006 	else if (cacherep == RC_REPLY)
1007 		rt->flag = DRT_CACHEREPLY;
1008 	else
1009 		rt->flag = DRT_CACHEDROP;
1010 	if (sotype == SOCK_STREAM)
1011 		rt->flag |= DRT_TCP;
1012 	if (nd->nd_flag & ND_NQNFS)
1013 		rt->flag |= DRT_NQNFS;
1014 	else if (nd->nd_flag & ND_NFSV3)
1015 		rt->flag |= DRT_NFSV3;
1016 	rt->proc = nd->nd_procnum;
1017 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
1018 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
1019 	else
1020 	    rt->ipadr = INADDR_ANY;
1021 	rt->resptime = ((time.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
1022 		(time.tv_usec - nd->nd_starttime.tv_usec);
1023 	rt->tstamp = time;
1024 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1025 }
1026 #endif /* NFSSERVER */
1027 
1028 #ifdef NFS
1029 
1030 int nfs_defect = 0;
1031 /*
1032  * Asynchronous I/O threads for client nfs.
1033  * They do read-ahead and write-behind operations on the block I/O cache.
1034  * Never returns unless it fails or gets killed.
1035  */
1036 
1037 int
1038 nfssvc_iod(l)
1039 	struct lwp *l;
1040 {
1041 	struct buf *bp;
1042 	int i;
1043 	struct nfs_iod *myiod;
1044 	struct nfsmount *nmp;
1045 	int error = 0;
1046 	struct proc *p = l->l_proc;
1047 
1048 	/*
1049 	 * Assign my position or return error if too many already running
1050 	 */
1051 	myiod = NULL;
1052 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1053 		if (nfs_asyncdaemon[i].nid_proc == NULL) {
1054 			myiod = &nfs_asyncdaemon[i];
1055 			break;
1056 		}
1057 	if (myiod == NULL)
1058 		return (EBUSY);
1059 	myiod->nid_proc = p;
1060 	nfs_numasync++;
1061 	PHOLD(l);
1062 	/*
1063 	 * Just loop around doing our stuff until SIGKILL
1064 	 */
1065 	for (;;) {
1066 		while (/*CONSTCOND*/ TRUE) {
1067 			simple_lock(&myiod->nid_slock);
1068 			nmp = myiod->nid_mount;
1069 			if (nmp) {
1070 				simple_lock(&nmp->nm_slock);
1071 				if (!TAILQ_EMPTY(&nmp->nm_bufq)) {
1072 					simple_unlock(&myiod->nid_slock);
1073 					break;
1074 				}
1075 				nmp->nm_bufqiods--;
1076 				simple_unlock(&nmp->nm_slock);
1077 			}
1078 			myiod->nid_want = p;
1079 			myiod->nid_mount = NULL;
1080 			error = ltsleep(&myiod->nid_want,
1081 			    PWAIT | PCATCH | PNORELOCK, "nfsidl", 0,
1082 			    &myiod->nid_slock);
1083 			if (error)
1084 				goto quit;
1085 		}
1086 
1087 		while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
1088 			/* Take one off the front of the list */
1089 			TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
1090 			nmp->nm_bufqlen--;
1091 			if (nmp->nm_bufqwant &&
1092 			    nmp->nm_bufqlen < 2 * nfs_numasync) {
1093 				nmp->nm_bufqwant = FALSE;
1094 				wakeup(&nmp->nm_bufq);
1095 			}
1096 			simple_unlock(&nmp->nm_slock);
1097 			(void)nfs_doio(bp);
1098 			simple_lock(&nmp->nm_slock);
1099 			/*
1100 			 * If there are more than one iod on this mount,
1101 			 * then defect so that the iods can be shared out
1102 			 * fairly between the mounts
1103 			 */
1104 			if (nfs_defect && nmp->nm_bufqiods > 1) {
1105 				myiod->nid_mount = NULL;
1106 				nmp->nm_bufqiods--;
1107 				break;
1108 			}
1109 		}
1110 		simple_unlock(&nmp->nm_slock);
1111 	}
1112 quit:
1113 	PRELE(l);
1114 	if (nmp)
1115 		nmp->nm_bufqiods--;
1116 	myiod->nid_want = NULL;
1117 	myiod->nid_mount = NULL;
1118 	myiod->nid_proc = NULL;
1119 	nfs_numasync--;
1120 
1121 	return error;
1122 }
1123 
1124 void
1125 nfs_iodinit()
1126 {
1127 	int i;
1128 
1129 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++)
1130 		simple_lock_init(&nfs_asyncdaemon[i].nid_slock);
1131 }
1132 
1133 void
1134 start_nfsio(arg)
1135 	void *arg;
1136 {
1137 	nfssvc_iod(curlwp);
1138 
1139 	kthread_exit(0);
1140 }
1141 
1142 void
1143 nfs_getset_niothreads(set)
1144 	int set;
1145 {
1146 	int i, have, start;
1147 
1148 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
1149 		if (nfs_asyncdaemon[i].nid_proc != NULL)
1150 			have++;
1151 
1152 	if (set) {
1153 		/* clamp to sane range */
1154 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
1155 
1156 		start = nfs_niothreads - have;
1157 
1158 		while (start > 0) {
1159 			kthread_create1(start_nfsio, NULL, NULL, "nfsio");
1160 			start--;
1161 		}
1162 
1163 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
1164 			if (nfs_asyncdaemon[i].nid_proc != NULL) {
1165 				psignal(nfs_asyncdaemon[i].nid_proc, SIGKILL);
1166 				start++;
1167 			}
1168 	} else {
1169 		if (nfs_niothreads >= 0)
1170 			nfs_niothreads = have;
1171 	}
1172 }
1173 
1174 /*
1175  * Get an authorization string for the uid by having the mount_nfs sitting
1176  * on this mount point porpous out of the kernel and do it.
1177  */
1178 int
1179 nfs_getauth(nmp, rep, cred, auth_str, auth_len, verf_str, verf_len, key)
1180 	struct nfsmount *nmp;
1181 	struct nfsreq *rep;
1182 	struct ucred *cred;
1183 	char **auth_str;
1184 	int *auth_len;
1185 	char *verf_str;
1186 	int *verf_len;
1187 	NFSKERBKEY_T key;		/* return session key */
1188 {
1189 	int error = 0;
1190 
1191 	while ((nmp->nm_iflag & NFSMNT_WAITAUTH) == 0) {
1192 		nmp->nm_iflag |= NFSMNT_WANTAUTH;
1193 		(void) tsleep((caddr_t)&nmp->nm_authtype, PSOCK,
1194 			"nfsauth1", 2 * hz);
1195 		error = nfs_sigintr(nmp, rep, rep->r_procp);
1196 		if (error) {
1197 			nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1198 			return (error);
1199 		}
1200 	}
1201 	nmp->nm_iflag &= ~(NFSMNT_WAITAUTH | NFSMNT_WANTAUTH);
1202 	nmp->nm_authstr = *auth_str = (char *)malloc(RPCAUTH_MAXSIZ, M_TEMP, M_WAITOK);
1203 	nmp->nm_authlen = RPCAUTH_MAXSIZ;
1204 	nmp->nm_verfstr = verf_str;
1205 	nmp->nm_verflen = *verf_len;
1206 	nmp->nm_authuid = cred->cr_uid;
1207 	wakeup((caddr_t)&nmp->nm_authstr);
1208 
1209 	/*
1210 	 * And wait for mount_nfs to do its stuff.
1211 	 */
1212 	while ((nmp->nm_iflag & NFSMNT_HASAUTH) == 0 && error == 0) {
1213 		(void) tsleep((caddr_t)&nmp->nm_authlen, PSOCK,
1214 			"nfsauth2", 2 * hz);
1215 		error = nfs_sigintr(nmp, rep, rep->r_procp);
1216 	}
1217 	if (nmp->nm_iflag & NFSMNT_AUTHERR) {
1218 		nmp->nm_iflag &= ~NFSMNT_AUTHERR;
1219 		error = EAUTH;
1220 	}
1221 	if (error)
1222 		free((caddr_t)*auth_str, M_TEMP);
1223 	else {
1224 		*auth_len = nmp->nm_authlen;
1225 		*verf_len = nmp->nm_verflen;
1226 		memcpy(key, nmp->nm_key, sizeof (NFSKERBKEY_T));
1227 	}
1228 	nmp->nm_iflag &= ~NFSMNT_HASAUTH;
1229 	nmp->nm_iflag |= NFSMNT_WAITAUTH;
1230 	if (nmp->nm_iflag & NFSMNT_WANTAUTH) {
1231 		nmp->nm_iflag &= ~NFSMNT_WANTAUTH;
1232 		wakeup((caddr_t)&nmp->nm_authtype);
1233 	}
1234 	return (error);
1235 }
1236 
1237 /*
1238  * Get a nickname authenticator and verifier.
1239  */
1240 int
1241 nfs_getnickauth(nmp, cred, auth_str, auth_len, verf_str, verf_len)
1242 	struct nfsmount *nmp;
1243 	struct ucred *cred;
1244 	char **auth_str;
1245 	int *auth_len;
1246 	char *verf_str;
1247 	int verf_len;
1248 {
1249 	struct nfsuid *nuidp;
1250 	u_int32_t *nickp, *verfp;
1251 	struct timeval ktvin, ktvout;
1252 
1253 #ifdef DIAGNOSTIC
1254 	if (verf_len < (4 * NFSX_UNSIGNED))
1255 		panic("nfs_getnickauth verf too small");
1256 #endif
1257 	LIST_FOREACH(nuidp, NMUIDHASH(nmp, cred->cr_uid), nu_hash) {
1258 		if (nuidp->nu_cr.cr_uid == cred->cr_uid)
1259 			break;
1260 	}
1261 	if (!nuidp || nuidp->nu_expire < time.tv_sec)
1262 		return (EACCES);
1263 
1264 	/*
1265 	 * Move to the end of the lru list (end of lru == most recently used).
1266 	 */
1267 	TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp, nu_lru);
1268 	TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp, nu_lru);
1269 
1270 	nickp = (u_int32_t *)malloc(2 * NFSX_UNSIGNED, M_TEMP, M_WAITOK);
1271 	*nickp++ = txdr_unsigned(RPCAKN_NICKNAME);
1272 	*nickp = txdr_unsigned(nuidp->nu_nickname);
1273 	*auth_str = (char *)nickp;
1274 	*auth_len = 2 * NFSX_UNSIGNED;
1275 
1276 	/*
1277 	 * Now we must encrypt the verifier and package it up.
1278 	 */
1279 	verfp = (u_int32_t *)verf_str;
1280 	*verfp++ = txdr_unsigned(RPCAKN_NICKNAME);
1281 	if (time.tv_sec > nuidp->nu_timestamp.tv_sec ||
1282 	    (time.tv_sec == nuidp->nu_timestamp.tv_sec &&
1283 	     time.tv_usec > nuidp->nu_timestamp.tv_usec))
1284 		nuidp->nu_timestamp = time;
1285 	else
1286 		nuidp->nu_timestamp.tv_usec++;
1287 	ktvin.tv_sec = txdr_unsigned(nuidp->nu_timestamp.tv_sec);
1288 	ktvin.tv_usec = txdr_unsigned(nuidp->nu_timestamp.tv_usec);
1289 
1290 	/*
1291 	 * Now encrypt the timestamp verifier in ecb mode using the session
1292 	 * key.
1293 	 */
1294 #ifdef NFSKERB
1295 	XXX
1296 #endif
1297 
1298 	*verfp++ = ktvout.tv_sec;
1299 	*verfp++ = ktvout.tv_usec;
1300 	*verfp = 0;
1301 	return (0);
1302 }
1303 
1304 /*
1305  * Save the current nickname in a hash list entry on the mount point.
1306  */
1307 int
1308 nfs_savenickauth(nmp, cred, len, key, mdp, dposp, mrep)
1309 	struct nfsmount *nmp;
1310 	struct ucred *cred;
1311 	int len;
1312 	NFSKERBKEY_T key;
1313 	struct mbuf **mdp;
1314 	char **dposp;
1315 	struct mbuf *mrep;
1316 {
1317 	struct nfsuid *nuidp;
1318 	u_int32_t *tl;
1319 	int32_t t1;
1320 	struct mbuf *md = *mdp;
1321 	struct timeval ktvin, ktvout;
1322 	u_int32_t nick;
1323 	char *dpos = *dposp, *cp2;
1324 	int deltasec, error = 0;
1325 
1326 	if (len == (3 * NFSX_UNSIGNED)) {
1327 		nfsm_dissect(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
1328 		ktvin.tv_sec = *tl++;
1329 		ktvin.tv_usec = *tl++;
1330 		nick = fxdr_unsigned(u_int32_t, *tl);
1331 
1332 		/*
1333 		 * Decrypt the timestamp in ecb mode.
1334 		 */
1335 #ifdef NFSKERB
1336 		XXX
1337 #endif
1338 		ktvout.tv_sec = fxdr_unsigned(long, ktvout.tv_sec);
1339 		ktvout.tv_usec = fxdr_unsigned(long, ktvout.tv_usec);
1340 		deltasec = time.tv_sec - ktvout.tv_sec;
1341 		if (deltasec < 0)
1342 			deltasec = -deltasec;
1343 		/*
1344 		 * If ok, add it to the hash list for the mount point.
1345 		 */
1346 		if (deltasec <= NFS_KERBCLOCKSKEW) {
1347 			if (nmp->nm_numuids < nuidhash_max) {
1348 				nmp->nm_numuids++;
1349 				nuidp = (struct nfsuid *)
1350 				   malloc(sizeof (struct nfsuid), M_NFSUID,
1351 					M_WAITOK);
1352 			} else {
1353 				nuidp = TAILQ_FIRST(&nmp->nm_uidlruhead);
1354 				LIST_REMOVE(nuidp, nu_hash);
1355 				TAILQ_REMOVE(&nmp->nm_uidlruhead, nuidp,
1356 					nu_lru);
1357 			}
1358 			nuidp->nu_flag = 0;
1359 			nuidp->nu_cr.cr_uid = cred->cr_uid;
1360 			nuidp->nu_expire = time.tv_sec + NFS_KERBTTL;
1361 			nuidp->nu_timestamp = ktvout;
1362 			nuidp->nu_nickname = nick;
1363 			memcpy(nuidp->nu_key, key, sizeof (NFSKERBKEY_T));
1364 			TAILQ_INSERT_TAIL(&nmp->nm_uidlruhead, nuidp,
1365 				nu_lru);
1366 			LIST_INSERT_HEAD(NMUIDHASH(nmp, cred->cr_uid),
1367 				nuidp, nu_hash);
1368 		}
1369 	} else
1370 		nfsm_adv(nfsm_rndup(len));
1371 nfsmout:
1372 	*mdp = md;
1373 	*dposp = dpos;
1374 	return (error);
1375 }
1376 #endif /* NFS */
1377