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