No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / subr_bufq.c
blobca2606f39ab2197dad12dc0b27da48d975467be7
1 /* $NetBSD: subr_bufq.c,v 1.19 2009/09/16 15:23:04 pooka Exp $ */
2 /* NetBSD: subr_disk.c,v 1.70 2005/08/20 12:00:01 yamt Exp $ */
4 /*-
5 * Copyright (c) 1996, 1997, 1999, 2000 The NetBSD Foundation, Inc.
6 * All rights reserved.
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.
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.
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.
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.
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.
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.
67 * @(#)ufs_disksubr.c 8.5 (Berkeley) 1/21/94
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: subr_bufq.c,v 1.19 2009/09/16 15:23:04 pooka Exp $");
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/kmem.h>
79 #include <sys/once.h>
80 #include <sys/sysctl.h>
82 BUFQ_DEFINE(dummy, 0, NULL); /* so that bufq_strats won't be empty */
84 #define STRAT_MATCH(id, bs) (strcmp((id), (bs)->bs_name) == 0)
86 static int bufq_init(void);
87 static void sysctl_kern_bufq_strategies_setup(struct sysctllog **);
89 static struct sysctllog *sysctllog;
90 static int
91 bufq_init(void)
94 sysctl_kern_bufq_strategies_setup(&sysctllog);
95 return 0;
99 * Create a device buffer queue.
102 bufq_alloc(struct bufq_state **bufqp, const char *strategy, int flags)
104 __link_set_decl(bufq_strats, const struct bufq_strat);
105 const struct bufq_strat *bsp;
106 const struct bufq_strat * const *it;
107 struct bufq_state *bufq;
108 static ONCE_DECL(bufq_init_ctrl);
109 int error = 0;
111 RUN_ONCE(&bufq_init_ctrl, bufq_init);
113 KASSERT((flags & BUFQ_EXACT) == 0 || strategy != BUFQ_STRAT_ANY);
115 switch (flags & BUFQ_SORT_MASK) {
116 case BUFQ_SORT_RAWBLOCK:
117 case BUFQ_SORT_CYLINDER:
118 break;
119 case 0:
121 * for strategies which don't care about block numbers.
122 * eg. fcfs
124 flags |= BUFQ_SORT_RAWBLOCK;
125 break;
126 default:
127 panic("bufq_alloc: sort out of range");
131 * select strategy.
132 * if a strategy specified by flags is found, use it.
133 * otherwise, select one with the largest bs_prio.
135 bsp = NULL;
136 __link_set_foreach(it, bufq_strats) {
137 if ((*it) == &bufq_strat_dummy)
138 continue;
139 if (strategy != BUFQ_STRAT_ANY &&
140 STRAT_MATCH(strategy, (*it))) {
141 bsp = *it;
142 break;
144 if (bsp == NULL || (*it)->bs_prio > bsp->bs_prio)
145 bsp = *it;
148 if (bsp == NULL) {
149 panic("bufq_alloc: no strategy");
151 if (strategy != BUFQ_STRAT_ANY && !STRAT_MATCH(strategy, bsp)) {
152 if ((flags & BUFQ_EXACT)) {
153 error = ENOENT;
154 goto out;
156 #if defined(DEBUG)
157 printf("bufq_alloc: '%s' is not available. using '%s'.\n",
158 strategy, bsp->bs_name);
159 #endif
161 #if defined(BUFQ_DEBUG)
162 /* XXX aprint? */
163 printf("bufq_alloc: using '%s'\n", bsp->bs_name);
164 #endif
166 *bufqp = bufq = kmem_zalloc(sizeof(*bufq), KM_SLEEP);
167 bufq->bq_flags = flags;
168 bufq->bq_strat = bsp;
169 (*bsp->bs_initfn)(bufq);
171 out:
172 return error;
175 void
176 bufq_put(struct bufq_state *bufq, struct buf *bp)
179 (*bufq->bq_put)(bufq, bp);
182 struct buf *
183 bufq_get(struct bufq_state *bufq)
186 return (*bufq->bq_get)(bufq, 1);
189 struct buf *
190 bufq_peek(struct bufq_state *bufq)
193 return (*bufq->bq_get)(bufq, 0);
196 struct buf *
197 bufq_cancel(struct bufq_state *bufq, struct buf *bp)
200 return (*bufq->bq_cancel)(bufq, bp);
204 * Drain a device buffer queue.
206 void
207 bufq_drain(struct bufq_state *bufq)
209 struct buf *bp;
211 while ((bp = bufq_get(bufq)) != NULL) {
212 bp->b_error = EIO;
213 bp->b_resid = bp->b_bcount;
214 biodone(bp);
219 * Destroy a device buffer queue.
221 void
222 bufq_free(struct bufq_state *bufq)
225 KASSERT(bufq_peek(bufq) == NULL);
227 bufq->bq_fini(bufq);
228 kmem_free(bufq, sizeof(*bufq));
232 * get a strategy identifier of a buffer queue.
234 const char *
235 bufq_getstrategyname(struct bufq_state *bufq)
238 return bufq->bq_strat->bs_name;
242 * move all requests on a buffer queue to another.
244 void
245 bufq_move(struct bufq_state *dst, struct bufq_state *src)
247 struct buf *bp;
249 while ((bp = bufq_get(src)) != NULL) {
250 bufq_put(dst, bp);
254 static int
255 docopy(char *buf, size_t *bufoffp, size_t buflen,
256 const char *datap, size_t datalen)
258 int error = 0;
260 if (buf != NULL && datalen > 0) {
262 if (*bufoffp + datalen > buflen) {
263 goto out;
265 error = copyout(datap, buf + *bufoffp, datalen);
266 if (error) {
267 goto out;
270 out:
271 if (error == 0) {
272 *bufoffp += datalen;
275 return error;
278 static int
279 docopystr(char *buf, size_t *bufoffp, size_t buflen, const char *datap)
282 return docopy(buf, bufoffp, buflen, datap, strlen(datap));
285 static int
286 docopynul(char *buf, size_t *bufoffp, size_t buflen)
289 return docopy(buf, bufoffp, buflen, "", 1);
293 * sysctl function that will print all bufq strategies
294 * built in the kernel.
296 static int
297 sysctl_kern_bufq_strategies(SYSCTLFN_ARGS)
299 __link_set_decl(bufq_strats, const struct bufq_strat);
300 const struct bufq_strat * const *bq_strat;
301 const char *delim = "";
302 size_t off = 0;
303 size_t buflen = *oldlenp;
304 int error;
306 __link_set_foreach(bq_strat, bufq_strats) {
307 if ((*bq_strat) == &bufq_strat_dummy) {
308 continue;
310 error = docopystr(oldp, &off, buflen, delim);
311 if (error) {
312 goto out;
314 error = docopystr(oldp, &off, buflen, (*bq_strat)->bs_name);
315 if (error) {
316 goto out;
318 delim = " ";
321 /* NUL terminate */
322 error = docopynul(oldp, &off, buflen);
323 out:
324 *oldlenp = off;
325 return error;
328 static void
329 sysctl_kern_bufq_strategies_setup(struct sysctllog **clog)
331 const struct sysctlnode *node;
333 sysctl_createv(clog, 0, NULL, NULL,
334 CTLFLAG_PERMANENT,
335 CTLTYPE_NODE, "kern", NULL,
336 NULL, 0, NULL, 0,
337 CTL_KERN, CTL_EOL);
338 node = NULL;
339 sysctl_createv(clog, 0, NULL, &node,
340 CTLFLAG_PERMANENT,
341 CTLTYPE_NODE, "bufq",
342 SYSCTL_DESCR("buffer queue subtree"),
343 NULL, 0, NULL, 0,
344 CTL_KERN, CTL_CREATE, CTL_EOL);
345 if (node != NULL) {
346 sysctl_createv(clog, 0, NULL, NULL,
347 CTLFLAG_PERMANENT,
348 CTLTYPE_STRING, "strategies",
349 SYSCTL_DESCR("List of bufq strategies present"),
350 sysctl_kern_bufq_strategies, 0, NULL, 0,
351 CTL_KERN, node->sysctl_num, CTL_CREATE, CTL_EOL);