xref: /openbsd-src/sys/nfs/nfs_aiod.c (revision 898184e3e61f9129feb5978fad5a8c6865f00b92)
1 /*	$OpenBSD: nfs_aiod.c,v 1.4 2009/08/27 23:39:46 thib Exp $	*/
2 /*
3  * Copyright (c) 1989, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Rick Macklem at The University of Guelph.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/malloc.h>
39 #include <sys/mount.h>
40 #include <sys/vnode.h>
41 #include <sys/kthread.h>
42 #include <sys/rwlock.h>
43 #include <sys/signalvar.h>
44 #include <sys/queue.h>
45 #include <sys/mutex.h>
46 
47 #include <nfs/rpcv2.h>
48 #include <nfs/nfsproto.h>
49 #include <nfs/nfs.h>
50 #include <nfs/nfsnode.h>
51 #include <nfs/nfs_var.h>
52 #include <nfs/nfsmount.h>
53 
54 /* The nfs_aiodl_mtx mutex protects the two lists. */
55 struct mutex		nfs_aiodl_mtx;
56 struct nfs_aiodhead	nfs_aiods_all;
57 struct nfs_aiodhead	nfs_aiods_idle;
58 
59 /* Current number of "running" aiods. Defaults to NFS_DEFASYNCDAEMON (4). */
60 int			nfs_numaiods = -1;
61 
62 /* Maximum # of buf to queue on an aiod. */
63 int			nfs_aiodbufqmax;
64 
65 /*
66  * Asynchronous I/O threads for client nfs.
67  * They do read-ahead and write-behind operations on the block I/O cache.
68  * Never returns unless it fails or gets killed.
69  */
70 void
71 nfs_aiod(void *arg)
72 {
73 	struct nfs_aiod	*aiod;
74 	struct nfsmount	*nmp;
75 	struct proc	*p;
76 	struct buf	*bp;
77 
78 	p = (struct proc *)arg;
79 
80 	aiod = malloc(sizeof(*aiod), M_TEMP, M_WAITOK|M_ZERO);
81 	mtx_enter(&nfs_aiodl_mtx);
82 	LIST_INSERT_HEAD(&nfs_aiods_all, aiod, nad_all);
83 	LIST_INSERT_HEAD(&nfs_aiods_idle, aiod, nad_idle);
84 	mtx_leave(&nfs_aiodl_mtx);
85 	nfs_numaiods++;
86 
87 	/*
88 	 * Enforce an upper limit on how many bufs we'll queue up for
89 	 * a given aiod. This is arbitrarily chosen to be a quarter of
90 	 * the number of bufs in the system, divided evenly between
91 	 * the running aiods.
92 	 *
93 	 * Since the number of bufs in the system is dynamic, and the
94 	 * aiods are usually started up very early (during boot), the
95 	 * number of buffers available is pretty low, so the limit we
96 	 * enforce is way to low: So, always allow a minimum of 64 bufs.
97 	 * XXX: Footshooting.
98 	 */
99 	nfs_aiodbufqmax = max((bcstats.numbufs / 4) / nfs_numaiods, 64);
100 
101 
102 loop:	/* Loop around until SIGKILL */
103 	if (aiod->nad_flags & NFSAIOD_WAKEUP) {
104 		mtx_enter(&nfs_aiodl_mtx);
105 		LIST_INSERT_HEAD(&nfs_aiods_idle, aiod, nad_idle);
106 		mtx_leave(&nfs_aiodl_mtx);
107 		aiod->nad_flags &= ~NFSAIOD_WAKEUP;
108 	}
109 
110 	while (1) {
111 		nmp = aiod->nad_mnt;
112 		if (nmp) {
113 			aiod->nad_mnt = NULL;
114 			break;
115 		}
116 
117 		while (!(aiod->nad_flags & NFSAIOD_WAKEUP))
118 			tsleep(aiod, PWAIT, "aiodidle", 0);
119 
120 		/*
121 		 * Wakeup for this aiod happens in one of the following
122 		 * situations:
123 		 * - The thread is being asked to exit by nfs_set_naiod(), or
124 		 * - nfs_asyncio() has found work for this thread on a mount.
125 		 *
126 		 * In the former case, check to see if nfs_asyncio() has just
127 		 * found some work for this thread, and if so, ignore it until
128 		 * later.
129 		 */
130 		if (aiod->nad_flags & NFSAIOD_EXIT) {
131 			if (aiod->nad_mnt == NULL)
132 				goto out1;
133 			else
134 				break;
135 		}
136 	}
137 
138 	while ((bp = TAILQ_FIRST(&nmp->nm_bufq)) != NULL) {
139 		/* Take one off the front of the list */
140 		TAILQ_REMOVE(&nmp->nm_bufq, bp, b_freelist);
141 		nmp->nm_bufqlen--;
142 		nfs_doio(bp, NULL);
143 	}
144 
145 	KASSERT(nmp->nm_naiods > 0);
146 	nmp->nm_naiods--;
147 	if (aiod->nad_flags & NFSAIOD_EXIT)
148 		goto out1;
149 
150 	goto loop;
151 
152 out1:
153 	free(aiod, M_TEMP);
154 	nfs_numaiods--;
155 	KASSERT(nfs_numaiods >= 0);
156 	/* Rejust the limit of bufs to queue. See comment above. */
157 	if (nfs_numaiods > 0)
158 		nfs_aiodbufqmax = max((bcstats.numbufs / 4) / nfs_numaiods, 64);
159 	else
160 		nfs_aiodbufqmax = 0;
161 	kthread_exit(0);
162 }
163 
164 int
165 nfs_set_naiod(int howmany)
166 {
167 	struct nfs_aiod	*aiod;
168 	struct proc	*p;
169 	int		 want, error;
170 
171 	KASSERT(howmany >= 0);
172 
173 	error = 0;
174 
175 	if (nfs_numaiods == -1)
176 		nfs_numaiods = 0;
177 
178 	want = howmany - nfs_numaiods;
179 
180 	if (want > 0) {
181 		/* Add more. */
182 		want = min(want, NFS_MAXASYNCDAEMON);
183 		while (want > 0) {
184 			error = kthread_create(nfs_aiod, p, &p, "nfsaio");
185 			if (error)
186 				return (error);
187 			want--;
188 		}
189 	} else if (want < 0) {
190 		/* Get rid of some. */
191 		want = -want;
192 		want = min(want, nfs_numaiods);
193 
194 		/* Favour idle aiod's. */
195 		mtx_enter(&nfs_aiodl_mtx);
196 		while (!LIST_EMPTY(&nfs_aiods_idle) && want > 0) {
197 			aiod = LIST_FIRST(&nfs_aiods_idle);
198 			LIST_REMOVE(aiod, nad_idle);
199 			LIST_REMOVE(aiod, nad_all);	/* Yuck. */
200 			aiod->nad_flags |= NFSAIOD_QUIT;
201 			wakeup_one(aiod);
202 			want--;
203 		}
204 
205 		while (!LIST_EMPTY(&nfs_aiods_all) && want > 0) {
206 			aiod = LIST_FIRST(&nfs_aiods_all);
207 			LIST_REMOVE(aiod, nad_all);
208 			aiod->nad_flags |= NFSAIOD_QUIT;
209 			wakeup_one(aiod);
210 			want--;
211 		}
212 		mtx_leave(&nfs_aiodl_mtx);
213 	}
214 	/* ignore the want == nfs_numaiods case, since it means no work */
215 
216 	return (error);
217 }
218