xref: /netbsd-src/sys/nfs/nfs_syscalls.c (revision 4d6fc14bc9b0c5bf3e30be318c143ee82cadd108)
1 /*	$NetBSD: nfs_syscalls.c,v 1.162 2020/03/14 18:08:39 ad 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.162 2020/03/14 18:08:39 ad Exp $");
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/proc.h>
48 #include <sys/uio.h>
49 #include <sys/malloc.h>
50 #include <sys/kmem.h>
51 #include <sys/buf.h>
52 #include <sys/mbuf.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/signalvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/namei.h>
59 #include <sys/syslog.h>
60 #include <sys/filedesc.h>
61 #include <sys/kthread.h>
62 #include <sys/kauth.h>
63 #include <sys/syscallargs.h>
64 #include <sys/cprng.h>
65 #include <sys/rbtree.h>
66 
67 #include <netinet/in.h>
68 #include <netinet/tcp.h>
69 #include <nfs/xdr_subs.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsm_subs.h>
74 #include <nfs/nfsrvcache.h>
75 #include <nfs/nfsmount.h>
76 #include <nfs/nfsnode.h>
77 #include <nfs/nfsrtt.h>
78 #include <nfs/nfs_var.h>
79 
80 extern int32_t (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *,
81 						struct nfssvc_sock *,
82 						struct lwp *, struct mbuf **);
83 extern int nfsrvw_procrastinate;
84 extern int nuidhash_max;
85 
86 static int nfs_numnfsd = 0;
87 static struct nfsdrt nfsdrt;
88 kmutex_t nfsd_lock;
89 struct nfssvc_sockhead nfssvc_sockhead;
90 kcondvar_t nfsd_initcv;
91 struct nfssvc_sockhead nfssvc_sockpending;
92 struct nfsdidlehead nfsd_idle_head;
93 
94 static rb_tree_t nfsd_tree;
95 static const rb_tree_ops_t nfsd_tree_ops;
96 
97 int nfssvc_sockhead_flag;
98 int nfsd_head_flag;
99 
100 struct nfssvc_sock *nfs_udpsock;
101 struct nfssvc_sock *nfs_udp6sock;
102 
103 static struct nfssvc_sock *nfsrv_sockalloc(void);
104 static void nfsrv_sockfree(struct nfssvc_sock *);
105 static void nfsd_rt(int, struct nfsrv_descript *, int);
106 static int nfssvc_nfsd(struct nfssvc_copy_ops *, struct nfsd_srvargs *, void *,
107 		struct lwp *);
108 
109 static int nfsd_compare_nodes(void *, const void *, const void *);
110 static int nfsd_compare_key(void *, const void *, const void *);
111 
112 static struct nfsd *nfsd_bake_cookie(struct nfsd *);
113 static void nfsd_toss_cookie(struct nfsd *);
114 static struct nfsd *nfsd_get(struct nfsd *);
115 
116 static int nfssvc_addsock_in(struct nfsd_args *, const void *);
117 static int nfssvc_setexports_in(struct mountd_exports_list *, const void *);
118 static int nfssvc_nsd_in(struct nfsd_srvargs *, const void *);
119 static int nfssvc_nsd_out(void *, const struct nfsd_srvargs *);
120 static int nfssvc_exp_in(struct export_args *, const void *, size_t);
121 
122 static const rb_tree_ops_t nfsd_tree_ops = {
123 	.rbto_compare_nodes = nfsd_compare_nodes,
124 	.rbto_compare_key = nfsd_compare_key,
125 	.rbto_node_offset = offsetof(struct nfsd, nfsd_node),
126 };
127 
128 static int
129 nfsd_compare_nodes(void *cookie, const void *va, const void *vb)
130 {
131 	const struct nfsd *na = va;
132 	const struct nfsd *nb = vb;
133 
134 	if (na->nfsd_cookie < nb->nfsd_cookie)
135 		return -1;
136 	if (na->nfsd_cookie > nb->nfsd_cookie)
137 		return +1;
138 	return 0;
139 }
140 
141 static int
142 nfsd_compare_key(void *cookie, const void *vn, const void *vk)
143 {
144 	const struct nfsd *n = vn;
145 	const uint32_t *k = vk;
146 
147 	if (n->nfsd_cookie < *k)
148 		return -1;
149 	if (n->nfsd_cookie > *k)
150 		return +1;
151 	return 0;
152 }
153 
154 /*
155  * nfsd_bake_cookie(nfsd)
156  *
157  *	Bake a cookie for nfsd, hang it on the tree of nfsds, and
158  *	return a userland-safe pointer nfsdu neatly packed for
159  *	transport in struct nfsd_srvargs::nsd_nfsd.
160  */
161 static struct nfsd *
162 nfsd_bake_cookie(struct nfsd *nfsd)
163 {
164 
165 	KASSERT(mutex_owned(&nfsd_lock));
166 
167 	do {
168 		nfsd->nfsd_cookie = cprng_fast32();
169 	} while (nfsd->nfsd_cookie == 0 ||
170 	    rb_tree_insert_node(&nfsd_tree, nfsd) != nfsd);
171 
172 	return (struct nfsd *)(uintptr_t)nfsd->nfsd_cookie;
173 }
174 
175 /*
176  * nfsd_toss_cookie(nfsd)
177  *
178  *	Toss nfsd's cookie.
179  */
180 static void
181 nfsd_toss_cookie(struct nfsd *nfsd)
182 {
183 
184 	KASSERT(mutex_owned(&nfsd_lock));
185 	KASSERT(nfsd->nfsd_cookie != 0);
186 
187 	rb_tree_remove_node(&nfsd_tree, nfsd);
188 	nfsd->nfsd_cookie = 0;	/* paranoia */
189 }
190 
191 /*
192  * nfsd_get(nfsdu)
193  *
194  *	Return the struct nfsd pointer for the userland nfsdu cookie,
195  *	as stored in struct nfsd_srvargs::nsd_nfsd, or NULL if nfsdu is
196  *	not a current valid nfsd cookie.
197  *
198  *	Caller MUST NOT hold nfsd_lock.  Caller MUST NOT pass (struct
199  *	nfsd *)(uintptr_t)0, which is the sentinel value for no nfsd
200  *	cookie, for which the caller should check first.
201  */
202 static struct nfsd *
203 nfsd_get(struct nfsd *nfsdu)
204 {
205 	uintptr_t cookie = (uintptr_t)nfsdu;
206 	uint32_t key;
207 	struct nfsd *nfsd;
208 
209 	KASSERT(cookie != 0);
210 	if (cookie > UINT32_MAX)
211 		return NULL;
212 	key = cookie;
213 
214 	mutex_enter(&nfsd_lock);
215 	nfsd = rb_tree_find_node(&nfsd_tree, &key);
216 	mutex_exit(&nfsd_lock);
217 
218 	return nfsd;
219 }
220 
221 static int
222 nfssvc_addsock_in(struct nfsd_args *nfsdarg, const void *argp)
223 {
224 
225 	return copyin(argp, nfsdarg, sizeof *nfsdarg);
226 }
227 
228 static int
229 nfssvc_setexports_in(struct mountd_exports_list *mel, const void *argp)
230 {
231 
232 	return copyin(argp, mel, sizeof *mel);
233 }
234 
235 static int
236 nfssvc_nsd_in(struct nfsd_srvargs *nsd, const void *argp)
237 {
238 
239 	return copyin(argp, nsd, sizeof *nsd);
240 }
241 
242 static int
243 nfssvc_nsd_out(void *argp, const struct nfsd_srvargs *nsd)
244 {
245 
246 	return copyout(nsd, argp, sizeof *nsd);
247 }
248 
249 static int
250 nfssvc_exp_in(struct export_args *exp, const void *argp, size_t nexports)
251 {
252 
253 	return copyin(argp, exp, sizeof(*exp) * nexports);
254 }
255 
256 /*
257  * NFS server system calls
258  */
259 
260 static struct nfssvc_copy_ops native_ops = {
261 	.addsock_in = nfssvc_addsock_in,
262 	.setexports_in = nfssvc_setexports_in,
263 	.nsd_in = nfssvc_nsd_in,
264 	.nsd_out = nfssvc_nsd_out,
265 	.exp_in = nfssvc_exp_in,
266 };
267 
268 /*
269  * Nfs server pseudo system call for the nfsd's
270  * Based on the flag value it either:
271  * - adds a socket to the selection list
272  * - remains in the kernel as an nfsd
273  * - remains in the kernel as an nfsiod
274  */
275 
276 int
277 sys_nfssvc(struct lwp *l, const struct sys_nfssvc_args *uap, register_t *retval)
278 {
279 	/* {
280 		syscallarg(int) flag;
281 		syscallarg(void *) argp;
282 	} */
283 	int	flag = SCARG(uap, flag);
284 	void	*argp = SCARG(uap, argp);
285 
286 	return do_nfssvc(&native_ops, l, flag, argp, retval);
287 }
288 
289 int
290 do_nfssvc(struct nfssvc_copy_ops *ops, struct lwp *l, int flag, void *argp, register_t *retval)
291 {
292 	int error;
293 	file_t *fp;
294 	struct mbuf *nam;
295 	struct nfsd_args nfsdarg;
296 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
297 	struct nfsd *nfsd = NULL;
298 	struct nfssvc_sock *slp;
299 	struct nfsuid *nuidp;
300 
301 	error = kauth_authorize_network(l->l_cred, KAUTH_NETWORK_NFS,
302 	    KAUTH_REQ_NETWORK_NFS_SVC, NULL, NULL, NULL);
303 	if (error)
304 		return (error);
305 
306 	mutex_enter(&nfsd_lock);
307 	while (nfssvc_sockhead_flag & SLP_INIT) {
308 		cv_wait(&nfsd_initcv, &nfsd_lock);
309 	}
310 	mutex_exit(&nfsd_lock);
311 
312 	if (flag & NFSSVC_BIOD) {
313 		/* Dummy implementation of nfsios for 1.4 and earlier. */
314 		error = kpause("nfsbiod", true, 0, NULL);
315 	} else if (flag & NFSSVC_MNTD) {
316 		error = ENOSYS;
317 	} else if (flag & NFSSVC_ADDSOCK) {
318 		error = ops->addsock_in(&nfsdarg, argp);
319 		if (error)
320 			return (error);
321 		/* getsock() will use the descriptor for us */
322 		if ((fp = fd_getfile(nfsdarg.sock)) == NULL)
323 			return (EBADF);
324 		if (fp->f_type != DTYPE_SOCKET) {
325 			fd_putfile(nfsdarg.sock);
326 			return (ENOTSOCK);
327 		}
328 		/*
329 		 * Get the client address for connected sockets.
330 		 */
331 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
332 			nam = (struct mbuf *)0;
333 		else {
334 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
335 				UIO_USERSPACE, MT_SONAME);
336 			if (error) {
337 				fd_putfile(nfsdarg.sock);
338 				return (error);
339 			}
340 		}
341 		error = nfssvc_addsock(fp, nam);
342 		fd_putfile(nfsdarg.sock);
343 	} else if (flag & NFSSVC_SETEXPORTSLIST) {
344 		struct export_args *args;
345 		struct mountd_exports_list mel;
346 
347 		error = ops->setexports_in(&mel, argp);
348 		if (error != 0)
349 			return error;
350 
351 		args = (struct export_args *)malloc(mel.mel_nexports *
352 		    sizeof(struct export_args), M_TEMP, M_WAITOK);
353 		error = ops->exp_in(args, mel.mel_exports, mel.mel_nexports);
354 		if (error != 0) {
355 			free(args, M_TEMP);
356 			return error;
357 		}
358 		mel.mel_exports = args;
359 
360 		error = mountd_set_exports_list(&mel, l, NULL);
361 
362 		free(args, M_TEMP);
363 	} else {
364 		error = ops->nsd_in(nsd, argp);
365 		if (error)
366 			return (error);
367 		if ((uintptr_t)nsd->nsd_nfsd != 0 &&
368 		    (nfsd = nfsd_get(nsd->nsd_nfsd)) == NULL)
369 			return (EINVAL);
370 		if ((flag & NFSSVC_AUTHIN) &&
371 		    nfsd != NULL &&
372 		    (nfsd->nfsd_slp->ns_flags & SLP_VALID)) {
373 			slp = nfsd->nfsd_slp;
374 
375 			/*
376 			 * First check to see if another nfsd has already
377 			 * added this credential.
378 			 */
379 			LIST_FOREACH(nuidp, NUIDHASH(slp, nsd->nsd_cr.cr_uid),
380 			    nu_hash) {
381 				if (kauth_cred_geteuid(nuidp->nu_cr) ==
382 				    nsd->nsd_cr.cr_uid &&
383 				    (!nfsd->nfsd_nd->nd_nam2 ||
384 				     netaddr_match(NU_NETFAM(nuidp),
385 				     &nuidp->nu_haddr, nfsd->nfsd_nd->nd_nam2)))
386 					break;
387 			}
388 			if (nuidp) {
389 			    kauth_cred_hold(nuidp->nu_cr);
390 			    nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
391 			    nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
392 			} else {
393 			    /*
394 			     * Nope, so we will.
395 			     */
396 			    if (slp->ns_numuids < nuidhash_max) {
397 				slp->ns_numuids++;
398 				nuidp = kmem_alloc(sizeof(*nuidp), KM_SLEEP);
399 			    } else
400 				nuidp = (struct nfsuid *)0;
401 			    if ((slp->ns_flags & SLP_VALID) == 0) {
402 				if (nuidp)
403 				    kmem_free(nuidp, sizeof(*nuidp));
404 			    } else {
405 				if (nuidp == (struct nfsuid *)0) {
406 				    nuidp = TAILQ_FIRST(&slp->ns_uidlruhead);
407 				    LIST_REMOVE(nuidp, nu_hash);
408 				    TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp,
409 					nu_lru);
410 				    if (nuidp->nu_flag & NU_NAM)
411 					m_freem(nuidp->nu_nam);
412 			        }
413 				nuidp->nu_flag = 0;
414 				kauth_uucred_to_cred(nuidp->nu_cr,
415 				    &nsd->nsd_cr);
416 				nuidp->nu_timestamp = nsd->nsd_timestamp;
417 				nuidp->nu_expire = time_second + nsd->nsd_ttl;
418 				/*
419 				 * and save the session key in nu_key.
420 				 */
421 				memcpy(nuidp->nu_key, nsd->nsd_key,
422 				    sizeof(nsd->nsd_key));
423 				if (nfsd->nfsd_nd->nd_nam2) {
424 				    struct sockaddr_in *saddr;
425 
426 				    saddr = mtod(nfsd->nfsd_nd->nd_nam2,
427 					 struct sockaddr_in *);
428 				    switch (saddr->sin_family) {
429 				    case AF_INET:
430 					nuidp->nu_flag |= NU_INETADDR;
431 					nuidp->nu_inetaddr =
432 					     saddr->sin_addr.s_addr;
433 					break;
434 				    case AF_INET6:
435 					nuidp->nu_flag |= NU_NAM;
436 					nuidp->nu_nam = m_copym(
437 					    nfsd->nfsd_nd->nd_nam2, 0,
438 					     M_COPYALL, M_WAIT);
439 					break;
440 				    default:
441 					kmem_free(nuidp, sizeof(*nuidp));
442 					return EAFNOSUPPORT;
443 				    };
444 				}
445 				TAILQ_INSERT_TAIL(&slp->ns_uidlruhead, nuidp,
446 					nu_lru);
447 				LIST_INSERT_HEAD(NUIDHASH(slp, nsd->nsd_uid),
448 					nuidp, nu_hash);
449 				kauth_cred_hold(nuidp->nu_cr);
450 				nfsd->nfsd_nd->nd_cr = nuidp->nu_cr;
451 				nfsd->nfsd_nd->nd_flag |= ND_KERBFULL;
452 			    }
453 			}
454 		}
455 		if ((flag & NFSSVC_AUTHINFAIL) &&
456 		    nfsd != NULL)
457 			nfsd->nfsd_flag |= NFSD_AUTHFAIL;
458 		error = nfssvc_nfsd(ops, nsd, argp, l);
459 	}
460 	if (error == EINTR || error == ERESTART)
461 		error = 0;
462 	return (error);
463 }
464 
465 static struct nfssvc_sock *
466 nfsrv_sockalloc(void)
467 {
468 	struct nfssvc_sock *slp;
469 
470 	slp = kmem_alloc(sizeof(*slp), KM_SLEEP);
471 	memset(slp, 0, sizeof (struct nfssvc_sock));
472 	mutex_init(&slp->ns_lock, MUTEX_DRIVER, IPL_SOFTNET);
473 	mutex_init(&slp->ns_alock, MUTEX_DRIVER, IPL_SOFTNET);
474 	cv_init(&slp->ns_cv, "nfsdsock");
475 	TAILQ_INIT(&slp->ns_uidlruhead);
476 	LIST_INIT(&slp->ns_tq);
477 	SIMPLEQ_INIT(&slp->ns_sendq);
478 	mutex_enter(&nfsd_lock);
479 	TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
480 	mutex_exit(&nfsd_lock);
481 
482 	return slp;
483 }
484 
485 static void
486 nfsrv_sockfree(struct nfssvc_sock *slp)
487 {
488 
489 	KASSERT(slp->ns_so == NULL);
490 	KASSERT(slp->ns_fp == NULL);
491 	KASSERT((slp->ns_flags & SLP_VALID) == 0);
492 	mutex_destroy(&slp->ns_lock);
493 	mutex_destroy(&slp->ns_alock);
494 	cv_destroy(&slp->ns_cv);
495 	kmem_free(slp, sizeof(*slp));
496 }
497 
498 /*
499  * Adds a socket to the list for servicing by nfsds.
500  */
501 int
502 nfssvc_addsock(file_t *fp, struct mbuf *mynam)
503 {
504 	int siz;
505 	struct nfssvc_sock *slp;
506 	struct socket *so;
507 	struct nfssvc_sock *tslp;
508 	int error;
509 	int val;
510 
511 	so = fp->f_socket;
512 	tslp = (struct nfssvc_sock *)0;
513 	/*
514 	 * Add it to the list, as required.
515 	 */
516 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
517 		if (so->so_proto->pr_domain->dom_family == AF_INET6)
518 			tslp = nfs_udp6sock;
519 		else {
520 			tslp = nfs_udpsock;
521 			if (tslp->ns_flags & SLP_VALID) {
522 				m_freem(mynam);
523 				return (EPERM);
524 			}
525 		}
526 	}
527 	if (so->so_type == SOCK_STREAM)
528 		siz = NFS_MAXPACKET + sizeof (u_long);
529 	else
530 		siz = NFS_MAXPACKET;
531 	solock(so);
532 	error = soreserve(so, siz, siz);
533 	sounlock(so);
534 	if (error) {
535 		m_freem(mynam);
536 		return (error);
537 	}
538 
539 	/*
540 	 * Set protocol specific options { for now TCP only } and
541 	 * reserve some space. For datagram sockets, this can get called
542 	 * repeatedly for the same socket, but that isn't harmful.
543 	 */
544 	if (so->so_type == SOCK_STREAM) {
545 		val = 1;
546 		so_setsockopt(NULL, so, SOL_SOCKET, SO_KEEPALIVE, &val,
547 		    sizeof(val));
548 	}
549 	if ((so->so_proto->pr_domain->dom_family == AF_INET ||
550 	    so->so_proto->pr_domain->dom_family == AF_INET6) &&
551 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
552 		val = 1;
553 		so_setsockopt(NULL, so, IPPROTO_TCP, TCP_NODELAY, &val,
554 		    sizeof(val));
555 	}
556 	solock(so);
557 	so->so_rcv.sb_flags &= ~SB_NOINTR;
558 	so->so_rcv.sb_timeo = 0;
559 	so->so_snd.sb_flags &= ~SB_NOINTR;
560 	so->so_snd.sb_timeo = 0;
561 	sounlock(so);
562 	if (tslp) {
563 		slp = tslp;
564 	} else {
565 		slp = nfsrv_sockalloc();
566 	}
567 	slp->ns_so = so;
568 	slp->ns_nam = mynam;
569 	mutex_enter(&fp->f_lock);
570 	fp->f_count++;
571 	mutex_exit(&fp->f_lock);
572 	slp->ns_fp = fp;
573 	slp->ns_flags = SLP_VALID;
574 	slp->ns_aflags = SLP_A_NEEDQ;
575 	slp->ns_gflags = 0;
576 	slp->ns_sflags = 0;
577 	solock(so);
578 	so->so_upcallarg = (void *)slp;
579 	so->so_upcall = nfsrv_soupcall;
580 	so->so_rcv.sb_flags |= SB_UPCALL;
581 	sounlock(so);
582 	nfsrv_wakenfsd(slp);
583 	return (0);
584 }
585 
586 /*
587  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
588  * until it is killed by a signal.
589  */
590 static int
591 nfssvc_nfsd(struct nfssvc_copy_ops *ops, struct nfsd_srvargs *nsd,
592 	    void *argp, struct lwp *l)
593 {
594 	struct timeval tv;
595 	struct mbuf *m;
596 	struct nfssvc_sock *slp;
597 	struct nfsd *nfsd;
598 	struct nfsrv_descript *nd = NULL;
599 	struct mbuf *mreq;
600 	u_quad_t cur_usec;
601 	int error = 0, cacherep, siz, sotype, writes_todo;
602 	struct proc *p = l->l_proc;
603 	bool doreinit;
604 
605 #ifndef nolint
606 	cacherep = RC_DOIT;
607 	writes_todo = 0;
608 #endif
609 	/*
610 	 * If userland didn't provide an nfsd cookie, bake a fresh one;
611 	 * if they did provide one, look it up.
612 	 */
613 	if ((uintptr_t)nsd->nsd_nfsd == 0) {
614 		nfsd = kmem_alloc(sizeof(*nfsd), KM_SLEEP);
615 		memset(nfsd, 0, sizeof (struct nfsd));
616 		cv_init(&nfsd->nfsd_cv, "nfsd");
617 		nfsd->nfsd_procp = p;
618 		mutex_enter(&nfsd_lock);
619 		while ((nfssvc_sockhead_flag & SLP_INIT) != 0) {
620 			KASSERT(nfs_numnfsd == 0);
621 			cv_wait(&nfsd_initcv, &nfsd_lock);
622 		}
623 		nsd->nsd_nfsd = nfsd_bake_cookie(nfsd);
624 		nfs_numnfsd++;
625 		mutex_exit(&nfsd_lock);
626 	} else if ((nfsd = nfsd_get(nsd->nsd_nfsd)) == NULL) {
627 		return (EINVAL);
628 	}
629 	KASSERT(nfsd != NULL);
630 	KASSERT(nsd->nsd_nfsd != (struct nfsd *)(uintptr_t)0);
631 
632 	/*
633 	 * Loop getting rpc requests until SIGKILL.
634 	 */
635 	for (;;) {
636 		bool dummy;
637 
638 		preempt_point();
639 
640 		if (nfsd->nfsd_slp == NULL) {
641 			mutex_enter(&nfsd_lock);
642 			while (nfsd->nfsd_slp == NULL &&
643 			    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
644 				SLIST_INSERT_HEAD(&nfsd_idle_head, nfsd,
645 				    nfsd_idle);
646 				error = cv_wait_sig(&nfsd->nfsd_cv, &nfsd_lock);
647 				if (error) {
648 					slp = nfsd->nfsd_slp;
649 					nfsd->nfsd_slp = NULL;
650 					if (!slp)
651 						SLIST_REMOVE(&nfsd_idle_head,
652 						    nfsd, nfsd, nfsd_idle);
653 					mutex_exit(&nfsd_lock);
654 					if (slp) {
655 						nfsrv_wakenfsd(slp);
656 						nfsrv_slpderef(slp);
657 					}
658 					goto done;
659 				}
660 			}
661 			if (nfsd->nfsd_slp == NULL &&
662 			    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
663 				slp = TAILQ_FIRST(&nfssvc_sockpending);
664 				if (slp) {
665 					KASSERT((slp->ns_gflags & SLP_G_DOREC)
666 					    != 0);
667 					TAILQ_REMOVE(&nfssvc_sockpending, slp,
668 					    ns_pending);
669 					slp->ns_gflags &= ~SLP_G_DOREC;
670 					slp->ns_sref++;
671 					nfsd->nfsd_slp = slp;
672 				} else
673 					nfsd_head_flag &= ~NFSD_CHECKSLP;
674 			}
675 			KASSERT(nfsd->nfsd_slp == NULL ||
676 			    nfsd->nfsd_slp->ns_sref > 0);
677 			mutex_exit(&nfsd_lock);
678 			if ((slp = nfsd->nfsd_slp) == NULL)
679 				continue;
680 			if (slp->ns_flags & SLP_VALID) {
681 				bool more;
682 
683 				if (nfsdsock_testbits(slp, SLP_A_NEEDQ)) {
684 					nfsrv_rcv(slp);
685 				}
686 				if (nfsdsock_testbits(slp, SLP_A_DISCONN)) {
687 					nfsrv_zapsock(slp);
688 				}
689 				error = nfsrv_dorec(slp, nfsd, &nd, &more);
690 				getmicrotime(&tv);
691 				cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
692 					(u_quad_t)tv.tv_usec;
693 				writes_todo = 0;
694 				if (error) {
695 					struct nfsrv_descript *nd2;
696 
697 					mutex_enter(&nfsd_lock);
698 					nd2 = LIST_FIRST(&slp->ns_tq);
699 					if (nd2 != NULL &&
700 					    nd2->nd_time <= cur_usec) {
701 						error = 0;
702 						cacherep = RC_DOIT;
703 						writes_todo = 1;
704 					}
705 					mutex_exit(&nfsd_lock);
706 				}
707 				if (error == 0 && more) {
708 					nfsrv_wakenfsd(slp);
709 				}
710 			}
711 		} else {
712 			error = 0;
713 			slp = nfsd->nfsd_slp;
714 		}
715 		KASSERT(slp != NULL);
716 		KASSERT(nfsd->nfsd_slp == slp);
717 		if (error || (slp->ns_flags & SLP_VALID) == 0) {
718 			if (nd) {
719 				nfsdreq_free(nd);
720 				nd = NULL;
721 			}
722 			nfsd->nfsd_slp = NULL;
723 			nfsrv_slpderef(slp);
724 			continue;
725 		}
726 		sotype = slp->ns_so->so_type;
727 		if (nd) {
728 			getmicrotime(&nd->nd_starttime);
729 			if (nd->nd_nam2)
730 				nd->nd_nam = nd->nd_nam2;
731 			else
732 				nd->nd_nam = slp->ns_nam;
733 
734 			/*
735 			 * Check to see if authorization is needed.
736 			 */
737 			if (nfsd->nfsd_flag & NFSD_NEEDAUTH) {
738 				nfsd->nfsd_flag &= ~NFSD_NEEDAUTH;
739 				nsd->nsd_haddr = mtod(nd->nd_nam,
740 				    struct sockaddr_in *)->sin_addr.s_addr;
741 				nsd->nsd_authlen = nfsd->nfsd_authlen;
742 				nsd->nsd_verflen = nfsd->nfsd_verflen;
743 				if (!copyout(nfsd->nfsd_authstr,
744 				    nsd->nsd_authstr, nfsd->nfsd_authlen) &&
745 				    !copyout(nfsd->nfsd_verfstr,
746 				    nsd->nsd_verfstr, nfsd->nfsd_verflen) &&
747 				    !ops->nsd_out(argp, nsd)) {
748 					return (ENEEDAUTH);
749 				}
750 				cacherep = RC_DROPIT;
751 			} else
752 				cacherep = nfsrv_getcache(nd, slp, &mreq);
753 
754 			if (nfsd->nfsd_flag & NFSD_AUTHFAIL) {
755 				nfsd->nfsd_flag &= ~NFSD_AUTHFAIL;
756 				nd->nd_procnum = NFSPROC_NOOP;
757 				nd->nd_repstat =
758 				    (NFSERR_AUTHERR | AUTH_TOOWEAK);
759 				cacherep = RC_DOIT;
760 			}
761 		}
762 
763 		/*
764 		 * Loop to get all the write rpc relies that have been
765 		 * gathered together.
766 		 */
767 		do {
768 			switch (cacherep) {
769 			case RC_DOIT:
770 				mreq = NULL;
771 				netexport_rdlock();
772 				if (writes_todo || nd == NULL ||
773 				     (!(nd->nd_flag & ND_NFSV3) &&
774 				     nd->nd_procnum == NFSPROC_WRITE &&
775 				     nfsrvw_procrastinate > 0))
776 					error = nfsrv_writegather(&nd, slp,
777 					    l, &mreq);
778 				else
779 					error =
780 					    (*(nfsrv3_procs[nd->nd_procnum]))
781 					    (nd, slp, l, &mreq);
782 				netexport_rdunlock();
783 				if (mreq == NULL) {
784 					if (nd != NULL) {
785 						if (nd->nd_nam2)
786 							m_free(nd->nd_nam2);
787 					}
788 					break;
789 				}
790 				if (error) {
791 					nfsstats.srv_errs++;
792 					if (nd) {
793 						nfsrv_updatecache(nd, false,
794 						    mreq);
795 						if (nd->nd_nam2)
796 							m_freem(nd->nd_nam2);
797 					}
798 					break;
799 				}
800 				if (nd) {
801 					nfsstats.srvrpccnt[nd->nd_procnum]++;
802 					nfsrv_updatecache(nd, true, mreq);
803 					nd->nd_mrep = NULL;
804 				}
805 				/* FALLTHROUGH */
806 			case RC_REPLY:
807 				m = mreq;
808 				siz = 0;
809 				while (m) {
810 					siz += m->m_len;
811 					m = m->m_next;
812 				}
813 				if (siz <= 0 || siz > NFS_MAXPACKET) {
814 					printf("mbuf siz=%d\n",siz);
815 					panic("Bad nfs svc reply");
816 				}
817 				m = mreq;
818 				m->m_pkthdr.len = siz;
819 				m_reset_rcvif(m);
820 				/*
821 				 * For stream protocols, prepend a Sun RPC
822 				 * Record Mark.
823 				 */
824 				if (sotype == SOCK_STREAM) {
825 					M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
826 					*mtod(m, u_int32_t *) =
827 					    htonl(0x80000000 | siz);
828 				}
829 				if (nd) {
830 					nd->nd_mreq = m;
831 					if (nfsrtton) {
832 						nfsd_rt(slp->ns_so->so_type, nd,
833 						    cacherep);
834 					}
835 					error = nfsdsock_sendreply(slp, nd);
836 					nd = NULL;
837 				}
838 				if (error == EPIPE)
839 					nfsrv_zapsock(slp);
840 				if (error == EINTR || error == ERESTART) {
841 					nfsd->nfsd_slp = NULL;
842 					nfsrv_slpderef(slp);
843 					goto done;
844 				}
845 				break;
846 			case RC_DROPIT:
847 				if (nd) {
848 					if (nfsrtton)
849 						nfsd_rt(sotype, nd, cacherep);
850 					m_freem(nd->nd_mrep);
851 					m_freem(nd->nd_nam2);
852 				}
853 				break;
854 			}
855 			if (nd) {
856 				nfsdreq_free(nd);
857 				nd = NULL;
858 			}
859 
860 			/*
861 			 * Check to see if there are outstanding writes that
862 			 * need to be serviced.
863 			 */
864 			getmicrotime(&tv);
865 			cur_usec = (u_quad_t)tv.tv_sec * 1000000 +
866 			    (u_quad_t)tv.tv_usec;
867 			mutex_enter(&nfsd_lock);
868 			if (LIST_FIRST(&slp->ns_tq) &&
869 			    LIST_FIRST(&slp->ns_tq)->nd_time <= cur_usec) {
870 				cacherep = RC_DOIT;
871 				writes_todo = 1;
872 			} else
873 				writes_todo = 0;
874 			mutex_exit(&nfsd_lock);
875 		} while (writes_todo);
876 		if (nfsrv_dorec(slp, nfsd, &nd, &dummy)) {
877 			nfsd->nfsd_slp = NULL;
878 			nfsrv_slpderef(slp);
879 		}
880 	}
881 done:
882 	mutex_enter(&nfsd_lock);
883 	nfsd_toss_cookie(nfsd);
884 	doreinit = --nfs_numnfsd == 0;
885 	if (doreinit)
886 		nfssvc_sockhead_flag |= SLP_INIT;
887 	mutex_exit(&nfsd_lock);
888 	cv_destroy(&nfsd->nfsd_cv);
889 	kmem_free(nfsd, sizeof(*nfsd));
890 	KASSERT(nsd->nsd_nfsd != (struct nfsd *)(uintptr_t)0);
891 	nsd->nsd_nfsd = (struct nfsd *)(uintptr_t)0;
892 	if (doreinit)
893 		nfsrv_init(true);	/* Reinitialize everything */
894 	return (error);
895 }
896 
897 /*
898  * Shut down a socket associated with an nfssvc_sock structure.
899  * Should be called with the send lock set, if required.
900  * The trick here is to increment the sref at the start, so that the nfsds
901  * will stop using it and clear ns_flag at the end so that it will not be
902  * reassigned during cleanup.
903  *
904  * called at splsoftnet.
905  */
906 void
907 nfsrv_zapsock(struct nfssvc_sock *slp)
908 {
909 	struct nfsuid *nuidp, *nnuidp;
910 	struct nfsrv_descript *nwp;
911 	struct socket *so;
912 	struct mbuf *m;
913 
914 	if (nfsdsock_drain(slp)) {
915 		return;
916 	}
917 	mutex_enter(&nfsd_lock);
918 	if (slp->ns_gflags & SLP_G_DOREC) {
919 		TAILQ_REMOVE(&nfssvc_sockpending, slp, ns_pending);
920 		slp->ns_gflags &= ~SLP_G_DOREC;
921 	}
922 	mutex_exit(&nfsd_lock);
923 
924 	so = slp->ns_so;
925 	KASSERT(so != NULL);
926 	solock(so);
927 	so->so_upcall = NULL;
928 	so->so_upcallarg = NULL;
929 	so->so_rcv.sb_flags &= ~SB_UPCALL;
930 	soshutdown(so, SHUT_RDWR);
931 	sounlock(so);
932 
933 	m_freem(slp->ns_raw);
934 	m = slp->ns_rec;
935 	while (m != NULL) {
936 		struct mbuf *n;
937 
938 		n = m->m_nextpkt;
939 		m_freem(m);
940 		m = n;
941 	}
942 	/* XXX what about freeing ns_frag ? */
943 	for (nuidp = TAILQ_FIRST(&slp->ns_uidlruhead); nuidp != 0;
944 	    nuidp = nnuidp) {
945 		nnuidp = TAILQ_NEXT(nuidp, nu_lru);
946 		LIST_REMOVE(nuidp, nu_hash);
947 		TAILQ_REMOVE(&slp->ns_uidlruhead, nuidp, nu_lru);
948 		if (nuidp->nu_flag & NU_NAM)
949 			m_freem(nuidp->nu_nam);
950 		kmem_free(nuidp, sizeof(*nuidp));
951 	}
952 	mutex_enter(&nfsd_lock);
953 	while ((nwp = LIST_FIRST(&slp->ns_tq)) != NULL) {
954 		LIST_REMOVE(nwp, nd_tq);
955 		mutex_exit(&nfsd_lock);
956 		nfsdreq_free(nwp);
957 		mutex_enter(&nfsd_lock);
958 	}
959 	mutex_exit(&nfsd_lock);
960 }
961 
962 /*
963  * Derefence a server socket structure. If it has no more references and
964  * is no longer valid, you can throw it away.
965  */
966 void
967 nfsrv_slpderef(struct nfssvc_sock *slp)
968 {
969 	uint32_t ref;
970 
971 	mutex_enter(&nfsd_lock);
972 	KASSERT(slp->ns_sref > 0);
973 	ref = --slp->ns_sref;
974 	if (ref == 0 && (slp->ns_flags & SLP_VALID) == 0) {
975 		file_t *fp;
976 
977 		KASSERT((slp->ns_gflags & SLP_G_DOREC) == 0);
978 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
979 		mutex_exit(&nfsd_lock);
980 
981 		fp = slp->ns_fp;
982 		if (fp != NULL) {
983 			slp->ns_fp = NULL;
984 			KASSERT(fp != NULL);
985 			KASSERT(fp->f_socket == slp->ns_so);
986 			KASSERT(fp->f_count > 0);
987 			closef(fp);
988 			slp->ns_so = NULL;
989 		}
990 
991 		if (slp->ns_nam)
992 			m_free(slp->ns_nam);
993 		nfsrv_sockfree(slp);
994 	} else
995 		mutex_exit(&nfsd_lock);
996 }
997 
998 /*
999  * Initialize the data structures for the server.
1000  * Handshake with any new nfsds starting up to avoid any chance of
1001  * corruption.
1002  */
1003 void
1004 nfsrv_init(int terminating)
1005 {
1006 	struct nfssvc_sock *slp;
1007 
1008 	if (!terminating) {
1009 		mutex_init(&nfsd_lock, MUTEX_DRIVER, IPL_SOFTNET);
1010 		cv_init(&nfsd_initcv, "nfsdinit");
1011 	}
1012 
1013 	mutex_enter(&nfsd_lock);
1014 	if (!terminating && (nfssvc_sockhead_flag & SLP_INIT) != 0)
1015 		panic("nfsd init");
1016 	nfssvc_sockhead_flag |= SLP_INIT;
1017 
1018 	if (terminating) {
1019 		KASSERT(SLIST_EMPTY(&nfsd_idle_head));
1020 		KASSERT(RB_TREE_MIN(&nfsd_tree) == NULL);
1021 		while ((slp = TAILQ_FIRST(&nfssvc_sockhead)) != NULL) {
1022 			mutex_exit(&nfsd_lock);
1023 			KASSERT(slp->ns_sref == 0);
1024 			slp->ns_sref++;
1025 			nfsrv_zapsock(slp);
1026 			nfsrv_slpderef(slp);
1027 			mutex_enter(&nfsd_lock);
1028 		}
1029 		KASSERT(TAILQ_EMPTY(&nfssvc_sockpending));
1030 		mutex_exit(&nfsd_lock);
1031 		nfsrv_cleancache();	/* And clear out server cache */
1032 	} else {
1033 		mutex_exit(&nfsd_lock);
1034 		nfs_pub.np_valid = 0;
1035 	}
1036 
1037 	TAILQ_INIT(&nfssvc_sockhead);
1038 	TAILQ_INIT(&nfssvc_sockpending);
1039 
1040 	rb_tree_init(&nfsd_tree, &nfsd_tree_ops);
1041 	SLIST_INIT(&nfsd_idle_head);
1042 	nfsd_head_flag &= ~NFSD_CHECKSLP;
1043 
1044 	nfs_udpsock = nfsrv_sockalloc();
1045 	nfs_udp6sock = nfsrv_sockalloc();
1046 
1047 	mutex_enter(&nfsd_lock);
1048 	nfssvc_sockhead_flag &= ~SLP_INIT;
1049 	cv_broadcast(&nfsd_initcv);
1050 	mutex_exit(&nfsd_lock);
1051 }
1052 
1053 void
1054 nfsrv_fini(void)
1055 {
1056 
1057 	nfsrv_init(true);
1058 	cv_destroy(&nfsd_initcv);
1059 	mutex_destroy(&nfsd_lock);
1060 }
1061 
1062 /*
1063  * Add entries to the server monitor log.
1064  */
1065 static void
1066 nfsd_rt(int sotype, struct nfsrv_descript *nd, int cacherep)
1067 {
1068 	struct timeval tv;
1069 	struct drt *rt;
1070 
1071 	rt = &nfsdrt.drt[nfsdrt.pos];
1072 	if (cacherep == RC_DOIT)
1073 		rt->flag = 0;
1074 	else if (cacherep == RC_REPLY)
1075 		rt->flag = DRT_CACHEREPLY;
1076 	else
1077 		rt->flag = DRT_CACHEDROP;
1078 	if (sotype == SOCK_STREAM)
1079 		rt->flag |= DRT_TCP;
1080 	if (nd->nd_flag & ND_NFSV3)
1081 		rt->flag |= DRT_NFSV3;
1082 	rt->proc = nd->nd_procnum;
1083 	if (mtod(nd->nd_nam, struct sockaddr *)->sa_family == AF_INET)
1084 	    rt->ipadr = mtod(nd->nd_nam, struct sockaddr_in *)->sin_addr.s_addr;
1085 	else
1086 	    rt->ipadr = INADDR_ANY;
1087 	getmicrotime(&tv);
1088 	rt->resptime = ((tv.tv_sec - nd->nd_starttime.tv_sec) * 1000000) +
1089 		(tv.tv_usec - nd->nd_starttime.tv_usec);
1090 	rt->tstamp = tv;
1091 	nfsdrt.pos = (nfsdrt.pos + 1) % NFSRTTLOGSIZ;
1092 }
1093