pci core: function pci_bus_init() cleanup
[qemu/ar7.git] / block / blkdebug.c
blobf85c54bdc8e14d7a1f3acba62399ff9d7416eb93
1 /*
2 * Block protocol for I/O error injection
4 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/config-file.h"
28 #include "block/block_int.h"
29 #include "qemu/module.h"
30 #include "qapi/qmp/qbool.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qapi/qmp/qint.h"
33 #include "qapi/qmp/qstring.h"
34 #include "sysemu/qtest.h"
36 typedef struct BDRVBlkdebugState {
37 int state;
38 int new_state;
40 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
41 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
42 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
43 } BDRVBlkdebugState;
45 typedef struct BlkdebugAIOCB {
46 BlockAIOCB common;
47 QEMUBH *bh;
48 int ret;
49 } BlkdebugAIOCB;
51 typedef struct BlkdebugSuspendedReq {
52 Coroutine *co;
53 char *tag;
54 QLIST_ENTRY(BlkdebugSuspendedReq) next;
55 } BlkdebugSuspendedReq;
57 static const AIOCBInfo blkdebug_aiocb_info = {
58 .aiocb_size = sizeof(BlkdebugAIOCB),
61 enum {
62 ACTION_INJECT_ERROR,
63 ACTION_SET_STATE,
64 ACTION_SUSPEND,
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 int64_t sector;
77 } inject;
78 struct {
79 int new_state;
80 } set_state;
81 struct {
82 char *tag;
83 } suspend;
84 } options;
85 QLIST_ENTRY(BlkdebugRule) next;
86 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
87 } BlkdebugRule;
89 static QemuOptsList inject_error_opts = {
90 .name = "inject-error",
91 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
92 .desc = {
94 .name = "event",
95 .type = QEMU_OPT_STRING,
98 .name = "state",
99 .type = QEMU_OPT_NUMBER,
102 .name = "errno",
103 .type = QEMU_OPT_NUMBER,
106 .name = "sector",
107 .type = QEMU_OPT_NUMBER,
110 .name = "once",
111 .type = QEMU_OPT_BOOL,
114 .name = "immediately",
115 .type = QEMU_OPT_BOOL,
117 { /* end of list */ }
121 static QemuOptsList set_state_opts = {
122 .name = "set-state",
123 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
124 .desc = {
126 .name = "event",
127 .type = QEMU_OPT_STRING,
130 .name = "state",
131 .type = QEMU_OPT_NUMBER,
134 .name = "new_state",
135 .type = QEMU_OPT_NUMBER,
137 { /* end of list */ }
141 static QemuOptsList *config_groups[] = {
142 &inject_error_opts,
143 &set_state_opts,
144 NULL
147 static int get_event_by_name(const char *name, BlkdebugEvent *event)
149 int i;
151 for (i = 0; i < BLKDBG__MAX; i++) {
152 if (!strcmp(BlkdebugEvent_lookup[i], name)) {
153 *event = i;
154 return 0;
158 return -1;
161 struct add_rule_data {
162 BDRVBlkdebugState *s;
163 int action;
166 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
168 struct add_rule_data *d = opaque;
169 BDRVBlkdebugState *s = d->s;
170 const char* event_name;
171 BlkdebugEvent event;
172 struct BlkdebugRule *rule;
174 /* Find the right event for the rule */
175 event_name = qemu_opt_get(opts, "event");
176 if (!event_name) {
177 error_setg(errp, "Missing event name for rule");
178 return -1;
179 } else if (get_event_by_name(event_name, &event) < 0) {
180 error_setg(errp, "Invalid event name \"%s\"", event_name);
181 return -1;
184 /* Set attributes common for all actions */
185 rule = g_malloc0(sizeof(*rule));
186 *rule = (struct BlkdebugRule) {
187 .event = event,
188 .action = d->action,
189 .state = qemu_opt_get_number(opts, "state", 0),
192 /* Parse action-specific options */
193 switch (d->action) {
194 case ACTION_INJECT_ERROR:
195 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
196 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
197 rule->options.inject.immediately =
198 qemu_opt_get_bool(opts, "immediately", 0);
199 rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
200 break;
202 case ACTION_SET_STATE:
203 rule->options.set_state.new_state =
204 qemu_opt_get_number(opts, "new_state", 0);
205 break;
207 case ACTION_SUSPEND:
208 rule->options.suspend.tag =
209 g_strdup(qemu_opt_get(opts, "tag"));
210 break;
213 /* Add the rule */
214 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
216 return 0;
219 static void remove_rule(BlkdebugRule *rule)
221 switch (rule->action) {
222 case ACTION_INJECT_ERROR:
223 case ACTION_SET_STATE:
224 break;
225 case ACTION_SUSPEND:
226 g_free(rule->options.suspend.tag);
227 break;
230 QLIST_REMOVE(rule, next);
231 g_free(rule);
234 static int read_config(BDRVBlkdebugState *s, const char *filename,
235 QDict *options, Error **errp)
237 FILE *f = NULL;
238 int ret;
239 struct add_rule_data d;
240 Error *local_err = NULL;
242 if (filename) {
243 f = fopen(filename, "r");
244 if (f == NULL) {
245 error_setg_errno(errp, errno, "Could not read blkdebug config file");
246 return -errno;
249 ret = qemu_config_parse(f, config_groups, filename);
250 if (ret < 0) {
251 error_setg(errp, "Could not parse blkdebug config file");
252 ret = -EINVAL;
253 goto fail;
257 qemu_config_parse_qdict(options, config_groups, &local_err);
258 if (local_err) {
259 error_propagate(errp, local_err);
260 ret = -EINVAL;
261 goto fail;
264 d.s = s;
265 d.action = ACTION_INJECT_ERROR;
266 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
267 if (local_err) {
268 error_propagate(errp, local_err);
269 ret = -EINVAL;
270 goto fail;
273 d.action = ACTION_SET_STATE;
274 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
275 if (local_err) {
276 error_propagate(errp, local_err);
277 ret = -EINVAL;
278 goto fail;
281 ret = 0;
282 fail:
283 qemu_opts_reset(&inject_error_opts);
284 qemu_opts_reset(&set_state_opts);
285 if (f) {
286 fclose(f);
288 return ret;
291 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
292 static void blkdebug_parse_filename(const char *filename, QDict *options,
293 Error **errp)
295 const char *c;
297 /* Parse the blkdebug: prefix */
298 if (!strstart(filename, "blkdebug:", &filename)) {
299 /* There was no prefix; therefore, all options have to be already
300 present in the QDict (except for the filename) */
301 qdict_put(options, "x-image", qstring_from_str(filename));
302 return;
305 /* Parse config file path */
306 c = strchr(filename, ':');
307 if (c == NULL) {
308 error_setg(errp, "blkdebug requires both config file and image path");
309 return;
312 if (c != filename) {
313 QString *config_path;
314 config_path = qstring_from_substr(filename, 0, c - filename - 1);
315 qdict_put(options, "config", config_path);
318 /* TODO Allow multi-level nesting and set file.filename here */
319 filename = c + 1;
320 qdict_put(options, "x-image", qstring_from_str(filename));
323 static QemuOptsList runtime_opts = {
324 .name = "blkdebug",
325 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
326 .desc = {
328 .name = "config",
329 .type = QEMU_OPT_STRING,
330 .help = "Path to the configuration file",
333 .name = "x-image",
334 .type = QEMU_OPT_STRING,
335 .help = "[internal use only, will be removed]",
338 .name = "align",
339 .type = QEMU_OPT_SIZE,
340 .help = "Required alignment in bytes",
342 { /* end of list */ }
346 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
347 Error **errp)
349 BDRVBlkdebugState *s = bs->opaque;
350 QemuOpts *opts;
351 Error *local_err = NULL;
352 const char *config;
353 uint64_t align;
354 int ret;
356 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
357 qemu_opts_absorb_qdict(opts, options, &local_err);
358 if (local_err) {
359 error_propagate(errp, local_err);
360 ret = -EINVAL;
361 goto out;
364 /* Read rules from config file or command line options */
365 config = qemu_opt_get(opts, "config");
366 ret = read_config(s, config, options, errp);
367 if (ret) {
368 goto out;
371 /* Set initial state */
372 s->state = 1;
374 /* Open the image file */
375 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
376 bs, &child_file, false, &local_err);
377 if (local_err) {
378 ret = -EINVAL;
379 error_propagate(errp, local_err);
380 goto out;
383 /* Set request alignment */
384 align = qemu_opt_get_size(opts, "align", bs->request_alignment);
385 if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
386 bs->request_alignment = align;
387 } else {
388 error_setg(errp, "Invalid alignment");
389 ret = -EINVAL;
390 goto fail_unref;
393 ret = 0;
394 goto out;
396 fail_unref:
397 bdrv_unref_child(bs, bs->file);
398 out:
399 qemu_opts_del(opts);
400 return ret;
403 static void error_callback_bh(void *opaque)
405 struct BlkdebugAIOCB *acb = opaque;
406 qemu_bh_delete(acb->bh);
407 acb->common.cb(acb->common.opaque, acb->ret);
408 qemu_aio_unref(acb);
411 static BlockAIOCB *inject_error(BlockDriverState *bs,
412 BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
414 BDRVBlkdebugState *s = bs->opaque;
415 int error = rule->options.inject.error;
416 struct BlkdebugAIOCB *acb;
417 QEMUBH *bh;
418 bool immediately = rule->options.inject.immediately;
420 if (rule->options.inject.once) {
421 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
422 remove_rule(rule);
425 if (immediately) {
426 return NULL;
429 acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
430 acb->ret = -error;
432 bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
433 acb->bh = bh;
434 qemu_bh_schedule(bh);
436 return &acb->common;
439 static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
440 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
441 BlockCompletionFunc *cb, void *opaque)
443 BDRVBlkdebugState *s = bs->opaque;
444 BlkdebugRule *rule = NULL;
446 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
447 if (rule->options.inject.sector == -1 ||
448 (rule->options.inject.sector >= sector_num &&
449 rule->options.inject.sector < sector_num + nb_sectors)) {
450 break;
454 if (rule && rule->options.inject.error) {
455 return inject_error(bs, cb, opaque, rule);
458 return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors,
459 cb, opaque);
462 static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
463 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
464 BlockCompletionFunc *cb, void *opaque)
466 BDRVBlkdebugState *s = bs->opaque;
467 BlkdebugRule *rule = NULL;
469 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
470 if (rule->options.inject.sector == -1 ||
471 (rule->options.inject.sector >= sector_num &&
472 rule->options.inject.sector < sector_num + nb_sectors)) {
473 break;
477 if (rule && rule->options.inject.error) {
478 return inject_error(bs, cb, opaque, rule);
481 return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
482 cb, opaque);
485 static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
486 BlockCompletionFunc *cb, void *opaque)
488 BDRVBlkdebugState *s = bs->opaque;
489 BlkdebugRule *rule = NULL;
491 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
492 if (rule->options.inject.sector == -1) {
493 break;
497 if (rule && rule->options.inject.error) {
498 return inject_error(bs, cb, opaque, rule);
501 return bdrv_aio_flush(bs->file->bs, cb, opaque);
505 static void blkdebug_close(BlockDriverState *bs)
507 BDRVBlkdebugState *s = bs->opaque;
508 BlkdebugRule *rule, *next;
509 int i;
511 for (i = 0; i < BLKDBG__MAX; i++) {
512 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
513 remove_rule(rule);
518 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
520 BDRVBlkdebugState *s = bs->opaque;
521 BlkdebugSuspendedReq r;
523 r = (BlkdebugSuspendedReq) {
524 .co = qemu_coroutine_self(),
525 .tag = g_strdup(rule->options.suspend.tag),
528 remove_rule(rule);
529 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
531 if (!qtest_enabled()) {
532 printf("blkdebug: Suspended request '%s'\n", r.tag);
534 qemu_coroutine_yield();
535 if (!qtest_enabled()) {
536 printf("blkdebug: Resuming request '%s'\n", r.tag);
539 QLIST_REMOVE(&r, next);
540 g_free(r.tag);
543 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
544 bool injected)
546 BDRVBlkdebugState *s = bs->opaque;
548 /* Only process rules for the current state */
549 if (rule->state && rule->state != s->state) {
550 return injected;
553 /* Take the action */
554 switch (rule->action) {
555 case ACTION_INJECT_ERROR:
556 if (!injected) {
557 QSIMPLEQ_INIT(&s->active_rules);
558 injected = true;
560 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
561 break;
563 case ACTION_SET_STATE:
564 s->new_state = rule->options.set_state.new_state;
565 break;
567 case ACTION_SUSPEND:
568 suspend_request(bs, rule);
569 break;
571 return injected;
574 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
576 BDRVBlkdebugState *s = bs->opaque;
577 struct BlkdebugRule *rule, *next;
578 bool injected;
580 assert((int)event >= 0 && event < BLKDBG__MAX);
582 injected = false;
583 s->new_state = s->state;
584 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
585 injected = process_rule(bs, rule, injected);
587 s->state = s->new_state;
590 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
591 const char *tag)
593 BDRVBlkdebugState *s = bs->opaque;
594 struct BlkdebugRule *rule;
595 BlkdebugEvent blkdebug_event;
597 if (get_event_by_name(event, &blkdebug_event) < 0) {
598 return -ENOENT;
602 rule = g_malloc(sizeof(*rule));
603 *rule = (struct BlkdebugRule) {
604 .event = blkdebug_event,
605 .action = ACTION_SUSPEND,
606 .state = 0,
607 .options.suspend.tag = g_strdup(tag),
610 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
612 return 0;
615 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
617 BDRVBlkdebugState *s = bs->opaque;
618 BlkdebugSuspendedReq *r, *next;
620 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
621 if (!strcmp(r->tag, tag)) {
622 qemu_coroutine_enter(r->co, NULL);
623 return 0;
626 return -ENOENT;
629 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
630 const char *tag)
632 BDRVBlkdebugState *s = bs->opaque;
633 BlkdebugSuspendedReq *r, *r_next;
634 BlkdebugRule *rule, *next;
635 int i, ret = -ENOENT;
637 for (i = 0; i < BLKDBG__MAX; i++) {
638 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
639 if (rule->action == ACTION_SUSPEND &&
640 !strcmp(rule->options.suspend.tag, tag)) {
641 remove_rule(rule);
642 ret = 0;
646 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
647 if (!strcmp(r->tag, tag)) {
648 qemu_coroutine_enter(r->co, NULL);
649 ret = 0;
652 return ret;
655 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
657 BDRVBlkdebugState *s = bs->opaque;
658 BlkdebugSuspendedReq *r;
660 QLIST_FOREACH(r, &s->suspended_reqs, next) {
661 if (!strcmp(r->tag, tag)) {
662 return true;
665 return false;
668 static int64_t blkdebug_getlength(BlockDriverState *bs)
670 return bdrv_getlength(bs->file->bs);
673 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
675 return bdrv_truncate(bs->file->bs, offset);
678 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
680 QDict *opts;
681 const QDictEntry *e;
682 bool force_json = false;
684 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
685 if (strcmp(qdict_entry_key(e), "config") &&
686 strcmp(qdict_entry_key(e), "x-image"))
688 force_json = true;
689 break;
693 if (force_json && !bs->file->bs->full_open_options) {
694 /* The config file cannot be recreated, so creating a plain filename
695 * is impossible */
696 return;
699 if (!force_json && bs->file->bs->exact_filename[0]) {
700 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
701 "blkdebug:%s:%s",
702 qdict_get_try_str(options, "config") ?: "",
703 bs->file->bs->exact_filename);
706 opts = qdict_new();
707 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
709 QINCREF(bs->file->bs->full_open_options);
710 qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options));
712 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
713 if (strcmp(qdict_entry_key(e), "x-image")) {
714 qobject_incref(qdict_entry_value(e));
715 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
719 bs->full_open_options = opts;
722 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
723 BlockReopenQueue *queue, Error **errp)
725 return 0;
728 static BlockDriver bdrv_blkdebug = {
729 .format_name = "blkdebug",
730 .protocol_name = "blkdebug",
731 .instance_size = sizeof(BDRVBlkdebugState),
733 .bdrv_parse_filename = blkdebug_parse_filename,
734 .bdrv_file_open = blkdebug_open,
735 .bdrv_close = blkdebug_close,
736 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
737 .bdrv_getlength = blkdebug_getlength,
738 .bdrv_truncate = blkdebug_truncate,
739 .bdrv_refresh_filename = blkdebug_refresh_filename,
741 .bdrv_aio_readv = blkdebug_aio_readv,
742 .bdrv_aio_writev = blkdebug_aio_writev,
743 .bdrv_aio_flush = blkdebug_aio_flush,
745 .bdrv_debug_event = blkdebug_debug_event,
746 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
747 .bdrv_debug_remove_breakpoint
748 = blkdebug_debug_remove_breakpoint,
749 .bdrv_debug_resume = blkdebug_debug_resume,
750 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
753 static void bdrv_blkdebug_init(void)
755 bdrv_register(&bdrv_blkdebug);
758 block_init(bdrv_blkdebug_init);