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