xref: /openbsd/sys/nfs/nfs_syscalls.c (revision d25d28bf)
1 /*	$OpenBSD: nfs_syscalls.c,v 1.106 2016/09/15 02:00:18 dlg Exp $	*/
2 /*	$NetBSD: nfs_syscalls.c,v 1.19 1996/02/18 11:53:52 fvdl Exp $	*/
3 
4 /*
5  * Copyright (c) 1989, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Rick Macklem at The University of Guelph.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)nfs_syscalls.c	8.5 (Berkeley) 3/30/95
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/pool.h>
46 #include <sys/proc.h>
47 #include <sys/uio.h>
48 #include <sys/malloc.h>
49 #include <sys/buf.h>
50 #include <sys/mbuf.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/domain.h>
54 #include <sys/protosw.h>
55 #include <sys/namei.h>
56 #include <sys/syslog.h>
57 #include <sys/filedesc.h>
58 #include <sys/signalvar.h>
59 #include <sys/kthread.h>
60 #include <sys/queue.h>
61 
62 #include <sys/syscallargs.h>
63 
64 #include <netinet/in.h>
65 #include <netinet/tcp.h>
66 #include <nfs/xdr_subs.h>
67 #include <nfs/rpcv2.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/nfs.h>
70 #include <nfs/nfsrvcache.h>
71 #include <nfs/nfsmount.h>
72 #include <nfs/nfsnode.h>
73 #include <nfs/nfs_var.h>
74 
75 /* Global defs. */
76 extern int nfs_numasync;
77 extern struct nfsstats nfsstats;
78 struct nfssvc_sock *nfs_udpsock;
79 int nfsd_waiting = 0;
80 
81 #ifdef NFSSERVER
82 struct pool nfsrv_descript_pl;
83 
84 int nfsrv_getslp(struct nfsd *nfsd);
85 
86 static int nfs_numnfsd = 0;
87 int (*nfsrv3_procs[NFS_NPROCS])(struct nfsrv_descript *,
88     struct nfssvc_sock *, struct proc *, struct mbuf **) = {
89 	nfsrv_null,
90 	nfsrv_getattr,
91 	nfsrv_setattr,
92 	nfsrv_lookup,
93 	nfsrv3_access,
94 	nfsrv_readlink,
95 	nfsrv_read,
96 	nfsrv_write,
97 	nfsrv_create,
98 	nfsrv_mkdir,
99 	nfsrv_symlink,
100 	nfsrv_mknod,
101 	nfsrv_remove,
102 	nfsrv_rmdir,
103 	nfsrv_rename,
104 	nfsrv_link,
105 	nfsrv_readdir,
106 	nfsrv_readdirplus,
107 	nfsrv_statfs,
108 	nfsrv_fsinfo,
109 	nfsrv_pathconf,
110 	nfsrv_commit,
111 	nfsrv_noop
112 };
113 #endif
114 
115 struct nfssvc_sockhead nfssvc_sockhead;
116 struct nfsdhead nfsd_head;
117 
118 int nfssvc_sockhead_flag;
119 int nfsd_head_flag;
120 
121 #ifdef NFSCLIENT
122 struct proc *nfs_asyncdaemon[NFS_MAXASYNCDAEMON];
123 int nfs_niothreads = -1;
124 #endif
125 
126 /*
127  * NFS server pseudo system call for the nfsd's
128  * Based on the flag value it either:
129  * - adds a socket to the selection list
130  * - remains in the kernel as an nfsd
131  */
132 int
133 sys_nfssvc(struct proc *p, void *v, register_t *retval)
134 {
135 	int error = 0;
136 #ifdef NFSSERVER
137 	struct sys_nfssvc_args /* {
138 		syscallarg(int) flag;
139 		syscallarg(caddr_t) argp;
140 	} */ *uap = v;
141 	int flags = SCARG(uap, flag);
142 	struct file *fp;
143 	struct mbuf *nam;
144 	struct nfsd_args nfsdarg;
145 	struct nfsd_srvargs nfsd_srvargs, *nsd = &nfsd_srvargs;
146 	struct nfsd *nfsd;
147 #endif
148 
149 	/* Must be super user */
150 	error = suser(p, 0);
151 	if (error)
152 		return (error);
153 
154 #ifndef NFSSERVER
155 	error = ENOSYS;
156 #else
157 
158 	while (nfssvc_sockhead_flag & SLP_INIT) {
159 		nfssvc_sockhead_flag |= SLP_WANTINIT;
160 		tsleep(&nfssvc_sockhead, PSOCK, "nfsd init", 0);
161 	}
162 
163 	switch (flags) {
164 	case NFSSVC_ADDSOCK:
165 		error = copyin(SCARG(uap, argp), &nfsdarg, sizeof(nfsdarg));
166 		if (error)
167 			return (error);
168 
169 		error = getsock(p, nfsdarg.sock, &fp);
170 		if (error)
171 			return (error);
172 
173 		/*
174 		 * Get the client address for connected sockets.
175 		 */
176 		if (nfsdarg.name == NULL || nfsdarg.namelen == 0)
177 			nam = NULL;
178 		else {
179 			error = sockargs(&nam, nfsdarg.name, nfsdarg.namelen,
180 				MT_SONAME);
181 			if (error) {
182 				FRELE(fp, p);
183 				return (error);
184 			}
185 		}
186 		error = nfssvc_addsock(fp, nam);
187 		FRELE(fp, p);
188 		break;
189 	case NFSSVC_NFSD:
190 		error = copyin(SCARG(uap, argp), nsd, sizeof(*nsd));
191 		if (error)
192 			return (error);
193 
194 		nfsd = malloc(sizeof(*nfsd), M_NFSD, M_WAITOK|M_ZERO);
195 		nfsd->nfsd_procp = p;
196 		nfsd->nfsd_slp = NULL;
197 
198 		error = nfssvc_nfsd(nfsd);
199 		break;
200 	default:
201 		error = EINVAL;
202 		break;
203 	}
204 
205 	if (error == EINTR || error == ERESTART)
206 		error = 0;
207 #endif	/* !NFSSERVER */
208 
209 	return (error);
210 }
211 
212 #ifdef NFSSERVER
213 /*
214  * Adds a socket to the list for servicing by nfsds.
215  */
216 int
217 nfssvc_addsock(struct file *fp, struct mbuf *mynam)
218 {
219 	struct mbuf *m;
220 	int siz;
221 	struct nfssvc_sock *slp;
222 	struct socket *so;
223 	struct nfssvc_sock *tslp;
224 	int error, s;
225 
226 	so = (struct socket *)fp->f_data;
227 	tslp = NULL;
228 	/*
229 	 * Add it to the list, as required.
230 	 */
231 	if (so->so_proto->pr_protocol == IPPROTO_UDP) {
232 		tslp = nfs_udpsock;
233 		if (tslp->ns_flag & SLP_VALID) {
234 			m_freem(mynam);
235 			return (EPERM);
236 		}
237 	}
238 	if (so->so_type == SOCK_STREAM)
239 		siz = NFS_MAXPACKET + sizeof (u_long);
240 	else
241 		siz = NFS_MAXPACKET;
242 	error = soreserve(so, siz, siz);
243 	if (error) {
244 		m_freem(mynam);
245 		return (error);
246 	}
247 
248 	/*
249 	 * Set protocol specific options { for now TCP only } and
250 	 * reserve some space. For datagram sockets, this can get called
251 	 * repeatedly for the same socket, but that isn't harmful.
252 	 */
253 	if (so->so_type == SOCK_STREAM) {
254 		MGET(m, M_WAIT, MT_SOOPTS);
255 		*mtod(m, int32_t *) = 1;
256 		m->m_len = sizeof(int32_t);
257 		sosetopt(so, SOL_SOCKET, SO_KEEPALIVE, m);
258 	}
259 	if (so->so_proto->pr_domain->dom_family == AF_INET &&
260 	    so->so_proto->pr_protocol == IPPROTO_TCP) {
261 		MGET(m, M_WAIT, MT_SOOPTS);
262 		*mtod(m, int32_t *) = 1;
263 		m->m_len = sizeof(int32_t);
264 		sosetopt(so, IPPROTO_TCP, TCP_NODELAY, m);
265 	}
266 	so->so_rcv.sb_flags &= ~SB_NOINTR;
267 	so->so_rcv.sb_timeo = 0;
268 	so->so_snd.sb_flags &= ~SB_NOINTR;
269 	so->so_snd.sb_timeo = 0;
270 	if (tslp)
271 		slp = tslp;
272 	else {
273 		slp = malloc(sizeof(*slp), M_NFSSVC,
274 		    M_WAITOK|M_ZERO);
275 		TAILQ_INSERT_TAIL(&nfssvc_sockhead, slp, ns_chain);
276 	}
277 	slp->ns_so = so;
278 	slp->ns_nam = mynam;
279 	fp->f_count++;
280 	slp->ns_fp = fp;
281 	s = splsoftnet();
282 	so->so_upcallarg = (caddr_t)slp;
283 	so->so_upcall = nfsrv_rcv;
284 	slp->ns_flag = (SLP_VALID | SLP_NEEDQ);
285 	nfsrv_wakenfsd(slp);
286 	splx(s);
287 	return (0);
288 }
289 
290 /*
291  * Called by nfssvc() for nfsds. Just loops around servicing rpc requests
292  * until it is killed by a signal.
293  */
294 int
295 nfssvc_nfsd(struct nfsd *nfsd)
296 {
297 	struct mbuf *m;
298 	int siz;
299 	struct nfssvc_sock *slp;
300 	struct socket *so;
301 	int *solockp;
302 	struct nfsrv_descript *nd = NULL;
303 	struct mbuf *mreq;
304 	int error = 0, cacherep, s, sotype;
305 
306 	cacherep = RC_DOIT;
307 
308 	s = splsoftnet();
309 	TAILQ_INSERT_TAIL(&nfsd_head, nfsd, nfsd_chain);
310 	nfs_numnfsd++;
311 
312 	/* Loop getting rpc requests until SIGKILL. */
313 loop:
314 	if (!ISSET(nfsd->nfsd_flag, NFSD_REQINPROG)) {
315 
316 		/* attach an nfssvc_sock to nfsd */
317 		error = nfsrv_getslp(nfsd);
318 		if (error)
319 			goto done;
320 
321 		slp = nfsd->nfsd_slp;
322 
323 		if (ISSET(slp->ns_flag, SLP_VALID)) {
324 			if (ISSET(slp->ns_flag, SLP_DISCONN)) {
325 				nfsrv_zapsock(slp);
326 			} else if (ISSET(slp->ns_flag, SLP_NEEDQ)) {
327 				CLR(slp->ns_flag, SLP_NEEDQ);
328 				nfs_sndlock(&slp->ns_solock, NULL);
329 				nfsrv_rcv(slp->ns_so, (caddr_t)slp, M_WAIT);
330 				nfs_sndunlock(&slp->ns_solock);
331 			}
332 
333 			error = nfsrv_dorec(slp, nfsd, &nd);
334 			SET(nfsd->nfsd_flag, NFSD_REQINPROG);
335 		}
336 	} else {
337 		error = 0;
338 		slp = nfsd->nfsd_slp;
339 	}
340 
341 	if (error || !ISSET(slp->ns_flag, SLP_VALID)) {
342 		if (nd != NULL) {
343 			pool_put(&nfsrv_descript_pl, nd);
344 			nd = NULL;
345 		}
346 		nfsd->nfsd_slp = NULL;
347 		CLR(nfsd->nfsd_flag, NFSD_REQINPROG);
348 		nfsrv_slpderef(slp);
349 		goto loop;
350 	}
351 
352 	splx(s);
353 
354 	so = slp->ns_so;
355 	sotype = so->so_type;
356 	if (ISSET(so->so_proto->pr_flags, PR_CONNREQUIRED))
357 		solockp = &slp->ns_solock;
358 	else
359 		solockp = NULL;
360 
361 	if (nd) {
362 		if (nd->nd_nam2)
363 			nd->nd_nam = nd->nd_nam2;
364 		else
365 			nd->nd_nam = slp->ns_nam;
366 	}
367 
368 	cacherep = nfsrv_getcache(nd, slp, &mreq);
369 	switch (cacherep) {
370 	case RC_DOIT:
371 		error = (*(nfsrv3_procs[nd->nd_procnum]))(nd, slp, nfsd->nfsd_procp, &mreq);
372 		if (mreq == NULL) {
373 			if (nd != NULL) {
374 				m_freem(nd->nd_nam2);
375 				m_freem(nd->nd_mrep);
376 			}
377 			break;
378 		}
379 		if (error) {
380 			nfsstats.srv_errs++;
381 			nfsrv_updatecache(nd, 0, mreq);
382 			m_freem(nd->nd_nam2);
383 			break;
384 		}
385 		nfsstats.srvrpccnt[nd->nd_procnum]++;
386 		nfsrv_updatecache(nd, 1, mreq);
387 		nd->nd_mrep = NULL;
388 
389 		/* FALLTHROUGH */
390 	case RC_REPLY:
391 		m = mreq;
392 		siz = 0;
393 		while (m) {
394 			siz += m->m_len;
395 			m = m->m_next;
396 		}
397 
398 		if (siz <= 0 || siz > NFS_MAXPACKET)
399 			panic("bad nfs svc reply, siz = %i", siz);
400 
401 		m = mreq;
402 		m->m_pkthdr.len = siz;
403 		m->m_pkthdr.ph_ifidx = 0;
404 
405 		/* For stream protocols, prepend a Sun RPC Record Mark. */
406 		if (sotype == SOCK_STREAM) {
407 			M_PREPEND(m, NFSX_UNSIGNED, M_WAIT);
408 			*mtod(m, u_int32_t *) = htonl(0x80000000 | siz);
409 		}
410 
411 		if (solockp)
412 			nfs_sndlock(solockp, NULL);
413 
414 		if (ISSET(slp->ns_flag, SLP_VALID))
415 		    error = nfs_send(so, nd->nd_nam2, m, NULL);
416 		else {
417 		    error = EPIPE;
418 		    m_freem(m);
419 		}
420 		m_freem(nd->nd_nam2);
421 		m_freem(nd->nd_mrep);
422 		if (error == EPIPE)
423 			nfsrv_zapsock(slp);
424 		if (solockp)
425 			nfs_sndunlock(solockp);
426 		if (error == EINTR || error == ERESTART) {
427 			pool_put(&nfsrv_descript_pl, nd);
428 			nfsrv_slpderef(slp);
429 			s = splsoftnet();
430 			goto done;
431 		}
432 		break;
433 	case RC_DROPIT:
434 		m_freem(nd->nd_mrep);
435 		m_freem(nd->nd_nam2);
436 		break;
437 	};
438 
439 	if (nd) {
440 		pool_put(&nfsrv_descript_pl, nd);
441 		nd = NULL;
442 	}
443 
444 	s = splsoftnet();
445 	if (nfsrv_dorec(slp, nfsd, &nd)) {
446 		nfsd->nfsd_flag &= ~NFSD_REQINPROG;
447 		nfsd->nfsd_slp = NULL;
448 		nfsrv_slpderef(slp);
449 	}
450 	goto loop;
451 
452 done:
453 	TAILQ_REMOVE(&nfsd_head, nfsd, nfsd_chain);
454 	splx(s);
455 	free(nfsd, M_NFSD, sizeof(*nfsd));
456 	if (--nfs_numnfsd == 0)
457 		nfsrv_init(1);	/* Reinitialize everything */
458 	return (error);
459 }
460 
461 /*
462  * Shut down a socket associated with an nfssvc_sock structure.
463  * Should be called with the send lock set, if required.
464  * The trick here is to increment the sref at the start, so that the nfsds
465  * will stop using it and clear ns_flag at the end so that it will not be
466  * reassigned during cleanup.
467  */
468 void
469 nfsrv_zapsock(struct nfssvc_sock *slp)
470 {
471 	struct socket *so;
472 	struct file *fp;
473 	struct mbuf *m, *n;
474 
475 	slp->ns_flag &= ~SLP_ALLFLAGS;
476 	fp = slp->ns_fp;
477 	if (fp) {
478 		FREF(fp);
479 		slp->ns_fp = NULL;
480 		so = slp->ns_so;
481 		so->so_upcall = NULL;
482 		soshutdown(so, SHUT_RDWR);
483 		closef(fp, NULL);
484 		if (slp->ns_nam)
485 	    		m = m_free(slp->ns_nam);
486 		m_freem(slp->ns_raw);
487 		m = slp->ns_rec;
488 		while (m) {
489 			n = m->m_nextpkt;
490 			m_freem(m);
491 			m = n;
492 		}
493 	}
494 }
495 
496 /*
497  * Derefence a server socket structure. If it has no more references and
498  * is no longer valid, you can throw it away.
499  */
500 void
501 nfsrv_slpderef(struct nfssvc_sock *slp)
502 {
503 	if (--(slp->ns_sref) == 0 && (slp->ns_flag & SLP_VALID) == 0) {
504 		TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
505 		free(slp, M_NFSSVC, sizeof(*slp));
506 	}
507 }
508 
509 /*
510  * Initialize the data structures for the server.
511  * Handshake with any new nfsds starting up to avoid any chance of
512  * corruption.
513  */
514 void
515 nfsrv_init(int terminating)
516 {
517 	struct nfssvc_sock *slp, *nslp;
518 
519 	if (nfssvc_sockhead_flag & SLP_INIT)
520 		panic("nfsd init");
521 	nfssvc_sockhead_flag |= SLP_INIT;
522 	if (terminating) {
523 		for (slp = TAILQ_FIRST(&nfssvc_sockhead); slp != NULL;
524 		    slp = nslp) {
525 			nslp = TAILQ_NEXT(slp, ns_chain);
526 			if (slp->ns_flag & SLP_VALID)
527 				nfsrv_zapsock(slp);
528 			TAILQ_REMOVE(&nfssvc_sockhead, slp, ns_chain);
529 			free(slp, M_NFSSVC, sizeof(*slp));
530 		}
531 		nfsrv_cleancache();	/* And clear out server cache */
532 	}
533 
534 	TAILQ_INIT(&nfssvc_sockhead);
535 	nfssvc_sockhead_flag &= ~SLP_INIT;
536 	if (nfssvc_sockhead_flag & SLP_WANTINIT) {
537 		nfssvc_sockhead_flag &= ~SLP_WANTINIT;
538 		wakeup((caddr_t)&nfssvc_sockhead);
539 	}
540 
541 	TAILQ_INIT(&nfsd_head);
542 	nfsd_head_flag &= ~NFSD_CHECKSLP;
543 
544 	nfs_udpsock =  malloc(sizeof(*nfs_udpsock), M_NFSSVC,
545 	    M_WAITOK|M_ZERO);
546 	TAILQ_INSERT_HEAD(&nfssvc_sockhead, nfs_udpsock, ns_chain);
547 
548 	if (!terminating) {
549 		pool_init(&nfsrv_descript_pl, sizeof(struct nfsrv_descript),
550 		    0, IPL_NONE, PR_WAITOK, "ndscpl", NULL);
551 	}
552 }
553 #endif /* NFSSERVER */
554 
555 #ifdef NFSCLIENT
556 /*
557  * Asynchronous I/O threads for client nfs.
558  * They do read-ahead and write-behind operations on the block I/O cache.
559  * Never returns unless it fails or gets killed.
560  */
561 void
562 nfssvc_iod(void *arg)
563 {
564 	struct proc *p = curproc;
565 	struct buf *bp, *nbp;
566 	int i, myiod;
567 	struct vnode *vp;
568 	int error = 0, s, bufcount;
569 
570 	bufcount = MIN(256, bcstats.kvaslots / 8);
571 	bufcount = MIN(bufcount, bcstats.numbufs / 8);
572 
573 	/* Assign my position or return error if too many already running. */
574 	myiod = -1;
575 	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
576 		if (nfs_asyncdaemon[i] == NULL) {
577 			myiod = i;
578 			break;
579 		}
580 	}
581 	if (myiod == -1)
582 		kthread_exit(EBUSY);
583 
584 	nfs_asyncdaemon[myiod] = p;
585 	nfs_numasync++;
586 
587 	/* Upper limit on how many bufs we'll queue up for this iod. */
588 	if (nfs_bufqmax > bcstats.kvaslots / 4) {
589 		nfs_bufqmax = bcstats.kvaslots / 4;
590 		bufcount = 0;
591 	}
592 	if (nfs_bufqmax > bcstats.numbufs / 4) {
593 		nfs_bufqmax = bcstats.numbufs / 4;
594 		bufcount = 0;
595 	}
596 
597 	nfs_bufqmax += bufcount;
598 	wakeup(&nfs_bufqlen); /* wake up anyone waiting for room to enqueue IO */
599 
600 	/* Just loop around doin our stuff until SIGKILL. */
601 	for (;;) {
602 	    while (TAILQ_FIRST(&nfs_bufq) == NULL && error == 0) {
603 		    error = tsleep(&nfs_bufq,
604 			PWAIT | PCATCH, "nfsidl", 0);
605 	    }
606 	    while ((bp = TAILQ_FIRST(&nfs_bufq)) != NULL) {
607 		/* Take one off the front of the list */
608 		TAILQ_REMOVE(&nfs_bufq, bp, b_freelist);
609 		nfs_bufqlen--;
610 		wakeup_one(&nfs_bufqlen);
611 		if (bp->b_flags & B_READ)
612 		    (void) nfs_doio(bp, NULL);
613 		else do {
614 		    /*
615 		     * Look for a delayed write for the same vnode, so I can do
616 		     * it now. We must grab it before calling nfs_doio() to
617 		     * avoid any risk of the vnode getting vclean()'d while
618 		     * we are doing the write rpc.
619 		     */
620 		    vp = bp->b_vp;
621 		    s = splbio();
622 		    LIST_FOREACH(nbp, &vp->v_dirtyblkhd, b_vnbufs) {
623 			if ((nbp->b_flags &
624 			    (B_BUSY|B_DELWRI|B_NEEDCOMMIT|B_NOCACHE))!=B_DELWRI)
625 			    continue;
626 			nbp->b_flags |= B_ASYNC;
627 			bremfree(nbp);
628 			buf_acquire(nbp);
629 			break;
630 		    }
631 		    /*
632 		     * For the delayed write, do the first part of nfs_bwrite()
633 		     * up to, but not including nfs_strategy().
634 		     */
635 		    if (nbp) {
636 			nbp->b_flags &= ~(B_READ|B_DONE|B_ERROR);
637 			buf_undirty(nbp);
638 			nbp->b_vp->v_numoutput++;
639 		    }
640 		    splx(s);
641 
642 		    (void) nfs_doio(bp, NULL);
643 		} while ((bp = nbp) != NULL);
644 	    }
645 	    if (error) {
646 		nfs_asyncdaemon[myiod] = NULL;
647 		nfs_numasync--;
648 		nfs_bufqmax -= bufcount;
649 		kthread_exit(error);
650 	    }
651 	}
652 }
653 
654 void
655 nfs_getset_niothreads(int set)
656 {
657 	int i, have, start;
658 
659 	for (have = 0, i = 0; i < NFS_MAXASYNCDAEMON; i++)
660 		if (nfs_asyncdaemon[i] != NULL)
661 			have++;
662 
663 	if (set) {
664 		/* clamp to sane range */
665 		nfs_niothreads = max(0, min(nfs_niothreads, NFS_MAXASYNCDAEMON));
666 
667 		start = nfs_niothreads - have;
668 
669 		while (start > 0) {
670 			kthread_create(nfssvc_iod, NULL, NULL, "nfsio");
671 			start--;
672 		}
673 
674 		for (i = 0; (start < 0) && (i < NFS_MAXASYNCDAEMON); i++)
675 			if (nfs_asyncdaemon[i] != NULL) {
676 				psignal(nfs_asyncdaemon[i], SIGKILL);
677 				start++;
678 			}
679 	} else {
680 		if (nfs_niothreads >= 0)
681 			nfs_niothreads = have;
682 	}
683 }
684 #endif /* NFSCLIENT */
685 
686 #ifdef NFSSERVER
687 /*
688  * Find an nfssrv_sock for nfsd, sleeping if needed.
689  */
690 int
691 nfsrv_getslp(struct nfsd *nfsd)
692 {
693 	struct nfssvc_sock *slp;
694 	int error;
695 
696 again:
697 	while (nfsd->nfsd_slp == NULL &&
698 	    (nfsd_head_flag & NFSD_CHECKSLP) == 0) {
699 		nfsd->nfsd_flag |= NFSD_WAITING;
700 		nfsd_waiting++;
701 		error = tsleep(nfsd, PSOCK | PCATCH, "nfsd", 0);
702 		nfsd_waiting--;
703 		if (error)
704 			return (error);
705 	}
706 
707 	if (nfsd->nfsd_slp == NULL &&
708 	    (nfsd_head_flag & NFSD_CHECKSLP) != 0) {
709 		TAILQ_FOREACH(slp, &nfssvc_sockhead, ns_chain) {
710 			if ((slp->ns_flag & (SLP_VALID | SLP_DOREC)) ==
711 			    (SLP_VALID | SLP_DOREC)) {
712 				slp->ns_flag &= ~SLP_DOREC;
713 				slp->ns_sref++;
714 				nfsd->nfsd_slp = slp;
715 				break;
716 			}
717 		}
718 		if (slp == NULL)
719 			nfsd_head_flag &= ~NFSD_CHECKSLP;
720 	}
721 
722 	if (nfsd->nfsd_slp == NULL)
723 		goto again;
724 
725 	return (0);
726 }
727 #endif /* NFSSERVER */
728