subr_bufq.c revision 1.12.18.2 1 1.12.18.2 yamt /* $NetBSD: subr_bufq.c,v 1.12.18.2 2006/06/21 15:09:38 yamt Exp $ */
2 1.12.18.2 yamt /* NetBSD: subr_disk.c,v 1.70 2005/08/20 12:00:01 yamt Exp $ */
3 1.12.18.2 yamt
4 1.12.18.2 yamt /*-
5 1.12.18.2 yamt * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
6 1.12.18.2 yamt * All rights reserved.
7 1.12.18.2 yamt *
8 1.12.18.2 yamt * This code is derived from software contributed to The NetBSD Foundation
9 1.12.18.2 yamt * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 1.12.18.2 yamt * NASA Ames Research Center.
11 1.12.18.2 yamt *
12 1.12.18.2 yamt * Redistribution and use in source and binary forms, with or without
13 1.12.18.2 yamt * modification, are permitted provided that the following conditions
14 1.12.18.2 yamt * are met:
15 1.12.18.2 yamt * 1. Redistributions of source code must retain the above copyright
16 1.12.18.2 yamt * notice, this list of conditions and the following disclaimer.
17 1.12.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
18 1.12.18.2 yamt * notice, this list of conditions and the following disclaimer in the
19 1.12.18.2 yamt * documentation and/or other materials provided with the distribution.
20 1.12.18.2 yamt * 3. All advertising materials mentioning features or use of this software
21 1.12.18.2 yamt * must display the following acknowledgement:
22 1.12.18.2 yamt * This product includes software developed by the NetBSD
23 1.12.18.2 yamt * Foundation, Inc. and its contributors.
24 1.12.18.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
25 1.12.18.2 yamt * contributors may be used to endorse or promote products derived
26 1.12.18.2 yamt * from this software without specific prior written permission.
27 1.12.18.2 yamt *
28 1.12.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 1.12.18.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 1.12.18.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 1.12.18.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 1.12.18.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 1.12.18.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 1.12.18.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 1.12.18.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 1.12.18.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 1.12.18.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 1.12.18.2 yamt * POSSIBILITY OF SUCH DAMAGE.
39 1.12.18.2 yamt */
40 1.12.18.2 yamt
41 1.12.18.2 yamt /*
42 1.12.18.2 yamt * Copyright (c) 1982, 1986, 1988, 1993
43 1.12.18.2 yamt * The Regents of the University of California. All rights reserved.
44 1.12.18.2 yamt * (c) UNIX System Laboratories, Inc.
45 1.12.18.2 yamt * All or some portions of this file are derived from material licensed
46 1.12.18.2 yamt * to the University of California by American Telephone and Telegraph
47 1.12.18.2 yamt * Co. or Unix System Laboratories, Inc. and are reproduced herein with
48 1.12.18.2 yamt * the permission of UNIX System Laboratories, Inc.
49 1.12.18.2 yamt *
50 1.12.18.2 yamt * Redistribution and use in source and binary forms, with or without
51 1.12.18.2 yamt * modification, are permitted provided that the following conditions
52 1.12.18.2 yamt * are met:
53 1.12.18.2 yamt * 1. Redistributions of source code must retain the above copyright
54 1.12.18.2 yamt * notice, this list of conditions and the following disclaimer.
55 1.12.18.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
56 1.12.18.2 yamt * notice, this list of conditions and the following disclaimer in the
57 1.12.18.2 yamt * documentation and/or other materials provided with the distribution.
58 1.12.18.2 yamt * 3. Neither the name of the University nor the names of its contributors
59 1.12.18.2 yamt * may be used to endorse or promote products derived from this software
60 1.12.18.2 yamt * without specific prior written permission.
61 1.12.18.2 yamt *
62 1.12.18.2 yamt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 1.12.18.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 1.12.18.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 1.12.18.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 1.12.18.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 1.12.18.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 1.12.18.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 1.12.18.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 1.12.18.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 1.12.18.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 1.12.18.2 yamt * SUCH DAMAGE.
73 1.12.18.2 yamt *
74 1.12.18.2 yamt * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
75 1.12.18.2 yamt */
76 1.12.18.2 yamt
77 1.12.18.2 yamt #include <sys/cdefs.h>
78 1.12.18.2 yamt __KERNEL_RCSID(0, "$NetBSD: subr_bufq.c,v 1.12.18.2 2006/06/21 15:09:38 yamt Exp $");
79 1.12.18.2 yamt
80 1.12.18.2 yamt #include <sys/param.h>
81 1.12.18.2 yamt #include <sys/systm.h>
82 1.12.18.2 yamt #include <sys/buf.h>
83 1.12.18.2 yamt #include <sys/bufq.h>
84 1.12.18.2 yamt #include <sys/bufq_impl.h>
85 1.12.18.2 yamt #include <sys/malloc.h>
86 1.12.18.2 yamt #include <sys/sysctl.h>
87 1.12.18.2 yamt
88 1.12.18.2 yamt BUFQ_DEFINE(dummy, 0, NULL); /* so that bufq_strats won't be empty */
89 1.12.18.2 yamt
90 1.12.18.2 yamt #define STRAT_MATCH(id, bs) (strcmp((id), (bs)->bs_name) == 0)
91 1.12.18.2 yamt
92 1.12.18.2 yamt /*
93 1.12.18.2 yamt * Create a device buffer queue.
94 1.12.18.2 yamt */
95 1.12.18.2 yamt int
96 1.12.18.2 yamt bufq_alloc(struct bufq_state **bufqp, const char *strategy, int flags)
97 1.12.18.2 yamt {
98 1.12.18.2 yamt __link_set_decl(bufq_strats, const struct bufq_strat);
99 1.12.18.2 yamt const struct bufq_strat *bsp;
100 1.12.18.2 yamt const struct bufq_strat * const *it;
101 1.12.18.2 yamt struct bufq_state *bufq;
102 1.12.18.2 yamt int error = 0;
103 1.12.18.2 yamt
104 1.12.18.2 yamt KASSERT((flags & BUFQ_EXACT) == 0 || strategy != BUFQ_STRAT_ANY);
105 1.12.18.2 yamt
106 1.12.18.2 yamt switch (flags & BUFQ_SORT_MASK) {
107 1.12.18.2 yamt case BUFQ_SORT_RAWBLOCK:
108 1.12.18.2 yamt case BUFQ_SORT_CYLINDER:
109 1.12.18.2 yamt break;
110 1.12.18.2 yamt case 0:
111 1.12.18.2 yamt /*
112 1.12.18.2 yamt * for strategies which don't care about block numbers.
113 1.12.18.2 yamt * eg. fcfs
114 1.12.18.2 yamt */
115 1.12.18.2 yamt flags |= BUFQ_SORT_RAWBLOCK;
116 1.12.18.2 yamt break;
117 1.12.18.2 yamt default:
118 1.12.18.2 yamt panic("bufq_alloc: sort out of range");
119 1.12.18.2 yamt }
120 1.12.18.2 yamt
121 1.12.18.2 yamt /*
122 1.12.18.2 yamt * select strategy.
123 1.12.18.2 yamt * if a strategy specified by flags is found, use it.
124 1.12.18.2 yamt * otherwise, select one with the largest bs_prio.
125 1.12.18.2 yamt */
126 1.12.18.2 yamt bsp = NULL;
127 1.12.18.2 yamt __link_set_foreach(it, bufq_strats) {
128 1.12.18.2 yamt if ((*it) == &bufq_strat_dummy)
129 1.12.18.2 yamt continue;
130 1.12.18.2 yamt if (strategy != BUFQ_STRAT_ANY &&
131 1.12.18.2 yamt STRAT_MATCH(strategy, (*it))) {
132 1.12.18.2 yamt bsp = *it;
133 1.12.18.2 yamt break;
134 1.12.18.2 yamt }
135 1.12.18.2 yamt if (bsp == NULL || (*it)->bs_prio > bsp->bs_prio)
136 1.12.18.2 yamt bsp = *it;
137 1.12.18.2 yamt }
138 1.12.18.2 yamt
139 1.12.18.2 yamt if (bsp == NULL) {
140 1.12.18.2 yamt panic("bufq_alloc: no strategy");
141 1.12.18.2 yamt }
142 1.12.18.2 yamt if (strategy != BUFQ_STRAT_ANY && !STRAT_MATCH(strategy, bsp)) {
143 1.12.18.2 yamt if ((flags & BUFQ_EXACT)) {
144 1.12.18.2 yamt error = ENOENT;
145 1.12.18.2 yamt goto out;
146 1.12.18.2 yamt }
147 1.12.18.2 yamt #if defined(DEBUG)
148 1.12.18.2 yamt printf("bufq_alloc: '%s' is not available. using '%s'.\n",
149 1.12.18.2 yamt strategy, bsp->bs_name);
150 1.12.18.2 yamt #endif
151 1.12.18.2 yamt }
152 1.12.18.2 yamt #if defined(BUFQ_DEBUG)
153 1.12.18.2 yamt /* XXX aprint? */
154 1.12.18.2 yamt printf("bufq_alloc: using '%s'\n", bsp->bs_name);
155 1.12.18.2 yamt #endif
156 1.12.18.2 yamt
157 1.12.18.2 yamt *bufqp = bufq = malloc(sizeof(*bufq), M_DEVBUF, M_WAITOK | M_ZERO);
158 1.12.18.2 yamt bufq->bq_flags = flags;
159 1.12.18.2 yamt bufq->bq_strat = bsp;
160 1.12.18.2 yamt (*bsp->bs_initfn)(bufq);
161 1.12.18.2 yamt
162 1.12.18.2 yamt out:
163 1.12.18.2 yamt return error;
164 1.12.18.2 yamt }
165 1.12.18.2 yamt
166 1.12.18.2 yamt void
167 1.12.18.2 yamt bufq_put(struct bufq_state *bufq, struct buf *bp)
168 1.12.18.2 yamt {
169 1.12.18.2 yamt
170 1.12.18.2 yamt (*bufq->bq_put)(bufq, bp);
171 1.12.18.2 yamt }
172 1.12.18.2 yamt
173 1.12.18.2 yamt struct buf *
174 1.12.18.2 yamt bufq_get(struct bufq_state *bufq)
175 1.12.18.2 yamt {
176 1.12.18.2 yamt
177 1.12.18.2 yamt return (*bufq->bq_get)(bufq, 1);
178 1.12.18.2 yamt }
179 1.12.18.2 yamt
180 1.12.18.2 yamt struct buf *
181 1.12.18.2 yamt bufq_peek(struct bufq_state *bufq)
182 1.12.18.2 yamt {
183 1.12.18.2 yamt
184 1.12.18.2 yamt return (*bufq->bq_get)(bufq, 0);
185 1.12.18.2 yamt }
186 1.12.18.2 yamt
187 1.12.18.2 yamt /*
188 1.12.18.2 yamt * Drain a device buffer queue.
189 1.12.18.2 yamt */
190 1.12.18.2 yamt void
191 1.12.18.2 yamt bufq_drain(struct bufq_state *bufq)
192 1.12.18.2 yamt {
193 1.12.18.2 yamt struct buf *bp;
194 1.12.18.2 yamt
195 1.12.18.2 yamt while ((bp = BUFQ_GET(bufq)) != NULL) {
196 1.12.18.2 yamt bp->b_error = EIO;
197 1.12.18.2 yamt bp->b_flags |= B_ERROR;
198 1.12.18.2 yamt bp->b_resid = bp->b_bcount;
199 1.12.18.2 yamt biodone(bp);
200 1.12.18.2 yamt }
201 1.12.18.2 yamt }
202 1.12.18.2 yamt
203 1.12.18.2 yamt /*
204 1.12.18.2 yamt * Destroy a device buffer queue.
205 1.12.18.2 yamt */
206 1.12.18.2 yamt void
207 1.12.18.2 yamt bufq_free(struct bufq_state *bufq)
208 1.12.18.2 yamt {
209 1.12.18.2 yamt
210 1.12.18.2 yamt KASSERT(bufq->bq_private != NULL);
211 1.12.18.2 yamt KASSERT(BUFQ_PEEK(bufq) == NULL);
212 1.12.18.2 yamt
213 1.12.18.2 yamt free(bufq->bq_private, M_DEVBUF);
214 1.12.18.2 yamt free(bufq, M_DEVBUF);
215 1.12.18.2 yamt }
216 1.12.18.2 yamt
217 1.12.18.2 yamt /*
218 1.12.18.2 yamt * get a strategy identifier of a buffer queue.
219 1.12.18.2 yamt */
220 1.12.18.2 yamt const char *
221 1.12.18.2 yamt bufq_getstrategyname(struct bufq_state *bufq)
222 1.12.18.2 yamt {
223 1.12.18.2 yamt
224 1.12.18.2 yamt return bufq->bq_strat->bs_name;
225 1.12.18.2 yamt }
226 1.12.18.2 yamt
227 1.12.18.2 yamt /*
228 1.12.18.2 yamt * move all requests on a buffer queue to another.
229 1.12.18.2 yamt */
230 1.12.18.2 yamt void
231 1.12.18.2 yamt bufq_move(struct bufq_state *dst, struct bufq_state *src)
232 1.12.18.2 yamt {
233 1.12.18.2 yamt struct buf *bp;
234 1.12.18.2 yamt
235 1.12.18.2 yamt while ((bp = BUFQ_GET(src)) != NULL) {
236 1.12.18.2 yamt BUFQ_PUT(dst, bp);
237 1.12.18.2 yamt }
238 1.12.18.2 yamt }
239 1.12.18.2 yamt
240 1.12.18.2 yamt static int
241 1.12.18.2 yamt docopy(char *buf, size_t *bufoffp, size_t buflen,
242 1.12.18.2 yamt const char *datap, size_t datalen)
243 1.12.18.2 yamt {
244 1.12.18.2 yamt int error = 0;
245 1.12.18.2 yamt
246 1.12.18.2 yamt if (buf != NULL && datalen > 0) {
247 1.12.18.2 yamt
248 1.12.18.2 yamt if (*bufoffp + datalen > buflen) {
249 1.12.18.2 yamt goto out;
250 1.12.18.2 yamt }
251 1.12.18.2 yamt error = copyout(datap, buf + *bufoffp, datalen);
252 1.12.18.2 yamt if (error) {
253 1.12.18.2 yamt goto out;
254 1.12.18.2 yamt }
255 1.12.18.2 yamt }
256 1.12.18.2 yamt out:
257 1.12.18.2 yamt if (error == 0) {
258 1.12.18.2 yamt *bufoffp += datalen;
259 1.12.18.2 yamt }
260 1.12.18.2 yamt
261 1.12.18.2 yamt return error;
262 1.12.18.2 yamt }
263 1.12.18.2 yamt
264 1.12.18.2 yamt static int
265 1.12.18.2 yamt docopystr(char *buf, size_t *bufoffp, size_t buflen, const char *datap)
266 1.12.18.2 yamt {
267 1.12.18.2 yamt
268 1.12.18.2 yamt return docopy(buf, bufoffp, buflen, datap, strlen(datap));
269 1.12.18.2 yamt }
270 1.12.18.2 yamt
271 1.12.18.2 yamt static int
272 1.12.18.2 yamt docopynul(char *buf, size_t *bufoffp, size_t buflen)
273 1.12.18.2 yamt {
274 1.12.18.2 yamt
275 1.12.18.2 yamt return docopy(buf, bufoffp, buflen, "", 1);
276 1.12.18.2 yamt }
277 1.12.18.2 yamt
278 1.12.18.2 yamt /*
279 1.12.18.2 yamt * sysctl function that will print all bufq strategies
280 1.12.18.2 yamt * built in the kernel.
281 1.12.18.2 yamt */
282 1.12.18.2 yamt static int
283 1.12.18.2 yamt sysctl_kern_bufq_strategies(SYSCTLFN_ARGS)
284 1.12.18.2 yamt {
285 1.12.18.2 yamt __link_set_decl(bufq_strats, const struct bufq_strat);
286 1.12.18.2 yamt const struct bufq_strat * const *bq_strat;
287 1.12.18.2 yamt const char *delim = "";
288 1.12.18.2 yamt size_t off = 0;
289 1.12.18.2 yamt size_t buflen = *oldlenp;
290 1.12.18.2 yamt int error;
291 1.12.18.2 yamt
292 1.12.18.2 yamt __link_set_foreach(bq_strat, bufq_strats) {
293 1.12.18.2 yamt if ((*bq_strat) == &bufq_strat_dummy) {
294 1.12.18.2 yamt continue;
295 1.12.18.2 yamt }
296 1.12.18.2 yamt error = docopystr(oldp, &off, buflen, delim);
297 1.12.18.2 yamt if (error) {
298 1.12.18.2 yamt goto out;
299 1.12.18.2 yamt }
300 1.12.18.2 yamt error = docopystr(oldp, &off, buflen, (*bq_strat)->bs_name);
301 1.12.18.2 yamt if (error) {
302 1.12.18.2 yamt goto out;
303 1.12.18.2 yamt }
304 1.12.18.2 yamt delim = " ";
305 1.12.18.2 yamt }
306 1.12.18.2 yamt
307 1.12.18.2 yamt /* NUL terminate */
308 1.12.18.2 yamt error = docopynul(oldp, &off, buflen);
309 1.12.18.2 yamt out:
310 1.12.18.2 yamt *oldlenp = off;
311 1.12.18.2 yamt return error;
312 1.12.18.2 yamt }
313 1.12.18.2 yamt
314 1.12.18.2 yamt SYSCTL_SETUP(sysctl_kern_bufq_strategies_setup, "sysctl kern.bufq tree setup")
315 1.12.18.2 yamt {
316 1.12.18.2 yamt const struct sysctlnode *node;
317 1.12.18.2 yamt
318 1.12.18.2 yamt sysctl_createv(clog, 0, NULL, NULL,
319 1.12.18.2 yamt CTLFLAG_PERMANENT,
320 1.12.18.2 yamt CTLTYPE_NODE, "kern", NULL,
321 1.12.18.2 yamt NULL, 0, NULL, 0,
322 1.12.18.2 yamt CTL_KERN, CTL_EOL);
323 1.12.18.2 yamt node = NULL;
324 1.12.18.2 yamt sysctl_createv(clog, 0, NULL, &node,
325 1.12.18.2 yamt CTLFLAG_PERMANENT,
326 1.12.18.2 yamt CTLTYPE_NODE, "bufq",
327 1.12.18.2 yamt SYSCTL_DESCR("buffer queue subtree"),
328 1.12.18.2 yamt NULL, 0, NULL, 0,
329 1.12.18.2 yamt CTL_KERN, CTL_CREATE, CTL_EOL);
330 1.12.18.2 yamt if (node != NULL) {
331 1.12.18.2 yamt sysctl_createv(clog, 0, NULL, NULL,
332 1.12.18.2 yamt CTLFLAG_PERMANENT,
333 1.12.18.2 yamt CTLTYPE_STRING, "strategies",
334 1.12.18.2 yamt SYSCTL_DESCR("List of bufq strategies present"),
335 1.12.18.2 yamt sysctl_kern_bufq_strategies, 0, NULL, 0,
336 1.12.18.2 yamt CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);
337 1.12.18.2 yamt }
338 1.12.18.2 yamt }
339