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