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