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