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