]>
Commit | Line | Data |
---|---|---|
6a143727 KW |
1 | /* |
2 | * Block protocol for I/O error injection | |
3 | * | |
4 | * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu-common.h" | |
26 | #include "block_int.h" | |
27 | #include "module.h" | |
28 | ||
b9f66d96 KW |
29 | typedef struct BlkdebugVars { |
30 | int state; | |
31 | ||
32 | /* If inject_errno != 0, an error is injected for requests */ | |
33 | int inject_errno; | |
34 | ||
35 | /* Decides if all future requests fail (false) or only the next one and | |
36 | * after the next request inject_errno is reset to 0 (true) */ | |
37 | bool inject_once; | |
38 | ||
39 | /* Decides if aio_readv/writev fails right away (true) or returns an error | |
40 | * return value only in the callback (false) */ | |
41 | bool inject_immediately; | |
42 | } BlkdebugVars; | |
43 | ||
6a143727 | 44 | typedef struct BDRVBlkdebugState { |
b9f66d96 | 45 | BlkdebugVars vars; |
8b9b0cc2 | 46 | QLIST_HEAD(list, BlkdebugRule) rules[BLKDBG_EVENT_MAX]; |
6a143727 KW |
47 | } BDRVBlkdebugState; |
48 | ||
b9f66d96 KW |
49 | typedef struct BlkdebugAIOCB { |
50 | BlockDriverAIOCB common; | |
51 | QEMUBH *bh; | |
52 | int ret; | |
53 | } BlkdebugAIOCB; | |
54 | ||
55 | static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb); | |
56 | ||
57 | static AIOPool blkdebug_aio_pool = { | |
58 | .aiocb_size = sizeof(BlkdebugAIOCB), | |
59 | .cancel = blkdebug_aio_cancel, | |
60 | }; | |
61 | ||
8b9b0cc2 KW |
62 | enum { |
63 | ACTION_INJECT_ERROR, | |
64 | ACTION_SET_STATE, | |
65 | }; | |
66 | ||
67 | typedef struct BlkdebugRule { | |
68 | BlkDebugEvent event; | |
69 | int action; | |
70 | int state; | |
71 | union { | |
72 | struct { | |
73 | int error; | |
74 | int immediately; | |
75 | int once; | |
76 | } inject; | |
77 | struct { | |
78 | int new_state; | |
79 | } set_state; | |
80 | } options; | |
81 | QLIST_ENTRY(BlkdebugRule) next; | |
82 | } BlkdebugRule; | |
83 | ||
84 | static QemuOptsList inject_error_opts = { | |
85 | .name = "inject-error", | |
86 | .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head), | |
87 | .desc = { | |
88 | { | |
89 | .name = "event", | |
90 | .type = QEMU_OPT_STRING, | |
91 | }, | |
92 | { | |
93 | .name = "state", | |
94 | .type = QEMU_OPT_NUMBER, | |
95 | }, | |
96 | { | |
97 | .name = "errno", | |
98 | .type = QEMU_OPT_NUMBER, | |
99 | }, | |
100 | { | |
101 | .name = "once", | |
102 | .type = QEMU_OPT_BOOL, | |
103 | }, | |
104 | { | |
105 | .name = "immediately", | |
106 | .type = QEMU_OPT_BOOL, | |
107 | }, | |
108 | { /* end of list */ } | |
109 | }, | |
110 | }; | |
111 | ||
112 | static QemuOptsList set_state_opts = { | |
113 | .name = "set-state", | |
327cdad4 | 114 | .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head), |
8b9b0cc2 KW |
115 | .desc = { |
116 | { | |
117 | .name = "event", | |
118 | .type = QEMU_OPT_STRING, | |
119 | }, | |
120 | { | |
121 | .name = "state", | |
122 | .type = QEMU_OPT_NUMBER, | |
123 | }, | |
124 | { | |
125 | .name = "new_state", | |
126 | .type = QEMU_OPT_NUMBER, | |
127 | }, | |
128 | { /* end of list */ } | |
129 | }, | |
130 | }; | |
131 | ||
132 | static QemuOptsList *config_groups[] = { | |
133 | &inject_error_opts, | |
134 | &set_state_opts, | |
135 | NULL | |
136 | }; | |
137 | ||
138 | static const char *event_names[BLKDBG_EVENT_MAX] = { | |
8252278a KW |
139 | [BLKDBG_L1_UPDATE] = "l1_update", |
140 | [BLKDBG_L1_GROW_ALLOC_TABLE] = "l1_grow.alloc_table", | |
141 | [BLKDBG_L1_GROW_WRITE_TABLE] = "l1_grow.write_table", | |
142 | [BLKDBG_L1_GROW_ACTIVATE_TABLE] = "l1_grow.activate_table", | |
143 | ||
144 | [BLKDBG_L2_LOAD] = "l2_load", | |
145 | [BLKDBG_L2_UPDATE] = "l2_update", | |
146 | [BLKDBG_L2_UPDATE_COMPRESSED] = "l2_update_compressed", | |
147 | [BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read", | |
148 | [BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write", | |
149 | ||
8252278a | 150 | [BLKDBG_READ_AIO] = "read_aio", |
8252278a KW |
151 | [BLKDBG_READ_BACKING_AIO] = "read_backing_aio", |
152 | [BLKDBG_READ_COMPRESSED] = "read_compressed", | |
153 | ||
154 | [BLKDBG_WRITE_AIO] = "write_aio", | |
155 | [BLKDBG_WRITE_COMPRESSED] = "write_compressed", | |
156 | ||
157 | [BLKDBG_VMSTATE_LOAD] = "vmstate_load", | |
158 | [BLKDBG_VMSTATE_SAVE] = "vmstate_save", | |
159 | ||
160 | [BLKDBG_COW_READ] = "cow_read", | |
161 | [BLKDBG_COW_WRITE] = "cow_write", | |
162 | ||
163 | [BLKDBG_REFTABLE_LOAD] = "reftable_load", | |
164 | [BLKDBG_REFTABLE_GROW] = "reftable_grow", | |
165 | ||
166 | [BLKDBG_REFBLOCK_LOAD] = "refblock_load", | |
167 | [BLKDBG_REFBLOCK_UPDATE] = "refblock_update", | |
168 | [BLKDBG_REFBLOCK_UPDATE_PART] = "refblock_update_part", | |
169 | [BLKDBG_REFBLOCK_ALLOC] = "refblock_alloc", | |
170 | [BLKDBG_REFBLOCK_ALLOC_HOOKUP] = "refblock_alloc.hookup", | |
171 | [BLKDBG_REFBLOCK_ALLOC_WRITE] = "refblock_alloc.write", | |
172 | [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS] = "refblock_alloc.write_blocks", | |
173 | [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE] = "refblock_alloc.write_table", | |
174 | [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE] = "refblock_alloc.switch_table", | |
175 | ||
176 | [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", | |
177 | [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", | |
178 | [BLKDBG_CLUSTER_FREE] = "cluster_free", | |
8b9b0cc2 KW |
179 | }; |
180 | ||
181 | static int get_event_by_name(const char *name, BlkDebugEvent *event) | |
182 | { | |
183 | int i; | |
184 | ||
185 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
186 | if (!strcmp(event_names[i], name)) { | |
187 | *event = i; | |
188 | return 0; | |
189 | } | |
190 | } | |
191 | ||
192 | return -1; | |
193 | } | |
194 | ||
195 | struct add_rule_data { | |
196 | BDRVBlkdebugState *s; | |
197 | int action; | |
198 | }; | |
199 | ||
200 | static int add_rule(QemuOpts *opts, void *opaque) | |
201 | { | |
202 | struct add_rule_data *d = opaque; | |
203 | BDRVBlkdebugState *s = d->s; | |
204 | const char* event_name; | |
205 | BlkDebugEvent event; | |
206 | struct BlkdebugRule *rule; | |
207 | ||
208 | /* Find the right event for the rule */ | |
209 | event_name = qemu_opt_get(opts, "event"); | |
210 | if (!event_name || get_event_by_name(event_name, &event) < 0) { | |
211 | return -1; | |
212 | } | |
213 | ||
214 | /* Set attributes common for all actions */ | |
7267c094 | 215 | rule = g_malloc0(sizeof(*rule)); |
8b9b0cc2 KW |
216 | *rule = (struct BlkdebugRule) { |
217 | .event = event, | |
218 | .action = d->action, | |
219 | .state = qemu_opt_get_number(opts, "state", 0), | |
220 | }; | |
221 | ||
222 | /* Parse action-specific options */ | |
223 | switch (d->action) { | |
224 | case ACTION_INJECT_ERROR: | |
225 | rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO); | |
226 | rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0); | |
227 | rule->options.inject.immediately = | |
228 | qemu_opt_get_bool(opts, "immediately", 0); | |
229 | break; | |
230 | ||
231 | case ACTION_SET_STATE: | |
232 | rule->options.set_state.new_state = | |
233 | qemu_opt_get_number(opts, "new_state", 0); | |
234 | break; | |
235 | }; | |
236 | ||
237 | /* Add the rule */ | |
238 | QLIST_INSERT_HEAD(&s->rules[event], rule, next); | |
239 | ||
240 | return 0; | |
241 | } | |
242 | ||
243 | static int read_config(BDRVBlkdebugState *s, const char *filename) | |
244 | { | |
245 | FILE *f; | |
246 | int ret; | |
247 | struct add_rule_data d; | |
248 | ||
249 | f = fopen(filename, "r"); | |
250 | if (f == NULL) { | |
251 | return -errno; | |
252 | } | |
253 | ||
254 | ret = qemu_config_parse(f, config_groups, filename); | |
255 | if (ret < 0) { | |
256 | goto fail; | |
257 | } | |
258 | ||
259 | d.s = s; | |
260 | d.action = ACTION_INJECT_ERROR; | |
261 | qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0); | |
262 | ||
263 | d.action = ACTION_SET_STATE; | |
264 | qemu_opts_foreach(&set_state_opts, add_rule, &d, 0); | |
265 | ||
266 | ret = 0; | |
267 | fail: | |
698f0d52 KW |
268 | qemu_opts_reset(&inject_error_opts); |
269 | qemu_opts_reset(&set_state_opts); | |
8b9b0cc2 KW |
270 | fclose(f); |
271 | return ret; | |
272 | } | |
273 | ||
274 | /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */ | |
6a143727 KW |
275 | static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags) |
276 | { | |
277 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
278 | int ret; |
279 | char *config, *c; | |
6a143727 | 280 | |
8b9b0cc2 | 281 | /* Parse the blkdebug: prefix */ |
6a143727 KW |
282 | if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) { |
283 | return -EINVAL; | |
284 | } | |
285 | filename += strlen("blkdebug:"); | |
286 | ||
8b9b0cc2 KW |
287 | /* Read rules from config file */ |
288 | c = strchr(filename, ':'); | |
289 | if (c == NULL) { | |
290 | return -EINVAL; | |
291 | } | |
292 | ||
031380d8 | 293 | config = g_strdup(filename); |
8b9b0cc2 KW |
294 | config[c - filename] = '\0'; |
295 | ret = read_config(s, config); | |
031380d8 | 296 | g_free(config); |
8b9b0cc2 KW |
297 | if (ret < 0) { |
298 | return ret; | |
299 | } | |
300 | filename = c + 1; | |
301 | ||
8db520ce KW |
302 | /* Set initial state */ |
303 | s->vars.state = 1; | |
304 | ||
8b9b0cc2 | 305 | /* Open the backing file */ |
66f82cee | 306 | ret = bdrv_file_open(&bs->file, filename, flags); |
8b9b0cc2 KW |
307 | if (ret < 0) { |
308 | return ret; | |
309 | } | |
310 | ||
311 | return 0; | |
6a143727 KW |
312 | } |
313 | ||
b9f66d96 KW |
314 | static void error_callback_bh(void *opaque) |
315 | { | |
316 | struct BlkdebugAIOCB *acb = opaque; | |
317 | qemu_bh_delete(acb->bh); | |
318 | acb->common.cb(acb->common.opaque, acb->ret); | |
319 | qemu_aio_release(acb); | |
320 | } | |
321 | ||
322 | static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb) | |
323 | { | |
b666d239 | 324 | BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common); |
b9f66d96 KW |
325 | qemu_aio_release(acb); |
326 | } | |
327 | ||
328 | static BlockDriverAIOCB *inject_error(BlockDriverState *bs, | |
329 | BlockDriverCompletionFunc *cb, void *opaque) | |
330 | { | |
331 | BDRVBlkdebugState *s = bs->opaque; | |
332 | int error = s->vars.inject_errno; | |
333 | struct BlkdebugAIOCB *acb; | |
334 | QEMUBH *bh; | |
335 | ||
336 | if (s->vars.inject_once) { | |
337 | s->vars.inject_errno = 0; | |
338 | } | |
339 | ||
340 | if (s->vars.inject_immediately) { | |
341 | return NULL; | |
342 | } | |
343 | ||
344 | acb = qemu_aio_get(&blkdebug_aio_pool, bs, cb, opaque); | |
345 | acb->ret = -error; | |
346 | ||
347 | bh = qemu_bh_new(error_callback_bh, acb); | |
348 | acb->bh = bh; | |
349 | qemu_bh_schedule(bh); | |
350 | ||
b666d239 | 351 | return &acb->common; |
b9f66d96 KW |
352 | } |
353 | ||
6a143727 KW |
354 | static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs, |
355 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
356 | BlockDriverCompletionFunc *cb, void *opaque) | |
357 | { | |
358 | BDRVBlkdebugState *s = bs->opaque; | |
b9f66d96 KW |
359 | |
360 | if (s->vars.inject_errno) { | |
361 | return inject_error(bs, cb, opaque); | |
362 | } | |
363 | ||
368e8dd1 | 364 | return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
365 | } |
366 | ||
367 | static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs, | |
368 | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, | |
369 | BlockDriverCompletionFunc *cb, void *opaque) | |
370 | { | |
371 | BDRVBlkdebugState *s = bs->opaque; | |
b9f66d96 KW |
372 | |
373 | if (s->vars.inject_errno) { | |
374 | return inject_error(bs, cb, opaque); | |
375 | } | |
376 | ||
368e8dd1 | 377 | return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque); |
6a143727 KW |
378 | } |
379 | ||
380 | static void blkdebug_close(BlockDriverState *bs) | |
381 | { | |
382 | BDRVBlkdebugState *s = bs->opaque; | |
8b9b0cc2 KW |
383 | BlkdebugRule *rule, *next; |
384 | int i; | |
385 | ||
386 | for (i = 0; i < BLKDBG_EVENT_MAX; i++) { | |
387 | QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) { | |
388 | QLIST_REMOVE(rule, next); | |
7267c094 | 389 | g_free(rule); |
8b9b0cc2 KW |
390 | } |
391 | } | |
6a143727 KW |
392 | } |
393 | ||
8b9b0cc2 KW |
394 | static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule, |
395 | BlkdebugVars *old_vars) | |
396 | { | |
397 | BDRVBlkdebugState *s = bs->opaque; | |
398 | BlkdebugVars *vars = &s->vars; | |
399 | ||
400 | /* Only process rules for the current state */ | |
401 | if (rule->state && rule->state != old_vars->state) { | |
402 | return; | |
403 | } | |
404 | ||
405 | /* Take the action */ | |
406 | switch (rule->action) { | |
407 | case ACTION_INJECT_ERROR: | |
408 | vars->inject_errno = rule->options.inject.error; | |
409 | vars->inject_once = rule->options.inject.once; | |
410 | vars->inject_immediately = rule->options.inject.immediately; | |
411 | break; | |
412 | ||
413 | case ACTION_SET_STATE: | |
414 | vars->state = rule->options.set_state.new_state; | |
415 | break; | |
416 | } | |
417 | } | |
418 | ||
419 | static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event) | |
420 | { | |
421 | BDRVBlkdebugState *s = bs->opaque; | |
422 | struct BlkdebugRule *rule; | |
423 | BlkdebugVars old_vars = s->vars; | |
424 | ||
95ee3914 | 425 | assert((int)event >= 0 && event < BLKDBG_EVENT_MAX); |
8b9b0cc2 KW |
426 | |
427 | QLIST_FOREACH(rule, &s->rules[event], next) { | |
428 | process_rule(bs, rule, &old_vars); | |
429 | } | |
430 | } | |
431 | ||
e1302255 PB |
432 | static int64_t blkdebug_getlength(BlockDriverState *bs) |
433 | { | |
434 | return bdrv_getlength(bs->file); | |
435 | } | |
436 | ||
6a143727 KW |
437 | static BlockDriver bdrv_blkdebug = { |
438 | .format_name = "blkdebug", | |
439 | .protocol_name = "blkdebug", | |
440 | ||
441 | .instance_size = sizeof(BDRVBlkdebugState), | |
442 | ||
66f82cee | 443 | .bdrv_file_open = blkdebug_open, |
6a143727 | 444 | .bdrv_close = blkdebug_close, |
e1302255 | 445 | .bdrv_getlength = blkdebug_getlength, |
6a143727 KW |
446 | |
447 | .bdrv_aio_readv = blkdebug_aio_readv, | |
448 | .bdrv_aio_writev = blkdebug_aio_writev, | |
8b9b0cc2 KW |
449 | |
450 | .bdrv_debug_event = blkdebug_debug_event, | |
6a143727 KW |
451 | }; |
452 | ||
453 | static void bdrv_blkdebug_init(void) | |
454 | { | |
455 | bdrv_register(&bdrv_blkdebug); | |
456 | } | |
457 | ||
458 | block_init(bdrv_blkdebug_init); |