qcow2: Add function for refcount order amendment
[qemu.git] / block / blkdebug.c
blob86b143dc2d6db554afc623002cae34e3c4cf7a9a
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-common.h"
26 #include "qemu/config-file.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qapi/qmp/qint.h"
32 #include "qapi/qmp/qstring.h"
33 #include "sysemu/qtest.h"
35 typedef struct BDRVBlkdebugState {
36 int state;
37 int new_state;
39 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
40 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
41 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
42 } BDRVBlkdebugState;
44 typedef struct BlkdebugAIOCB {
45 BlockAIOCB common;
46 QEMUBH *bh;
47 int ret;
48 } BlkdebugAIOCB;
50 typedef struct BlkdebugSuspendedReq {
51 Coroutine *co;
52 char *tag;
53 QLIST_ENTRY(BlkdebugSuspendedReq) next;
54 } BlkdebugSuspendedReq;
56 static const AIOCBInfo blkdebug_aiocb_info = {
57 .aiocb_size = sizeof(BlkdebugAIOCB),
60 enum {
61 ACTION_INJECT_ERROR,
62 ACTION_SET_STATE,
63 ACTION_SUSPEND,
66 typedef struct BlkdebugRule {
67 BlkdebugEvent event;
68 int action;
69 int state;
70 union {
71 struct {
72 int error;
73 int immediately;
74 int once;
75 int64_t sector;
76 } inject;
77 struct {
78 int new_state;
79 } set_state;
80 struct {
81 char *tag;
82 } suspend;
83 } options;
84 QLIST_ENTRY(BlkdebugRule) next;
85 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
86 } BlkdebugRule;
88 static QemuOptsList inject_error_opts = {
89 .name = "inject-error",
90 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
91 .desc = {
93 .name = "event",
94 .type = QEMU_OPT_STRING,
97 .name = "state",
98 .type = QEMU_OPT_NUMBER,
101 .name = "errno",
102 .type = QEMU_OPT_NUMBER,
105 .name = "sector",
106 .type = QEMU_OPT_NUMBER,
109 .name = "once",
110 .type = QEMU_OPT_BOOL,
113 .name = "immediately",
114 .type = QEMU_OPT_BOOL,
116 { /* end of list */ }
120 static QemuOptsList set_state_opts = {
121 .name = "set-state",
122 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
123 .desc = {
125 .name = "event",
126 .type = QEMU_OPT_STRING,
129 .name = "state",
130 .type = QEMU_OPT_NUMBER,
133 .name = "new_state",
134 .type = QEMU_OPT_NUMBER,
136 { /* end of list */ }
140 static QemuOptsList *config_groups[] = {
141 &inject_error_opts,
142 &set_state_opts,
143 NULL
146 static int get_event_by_name(const char *name, BlkdebugEvent *event)
148 int i;
150 for (i = 0; i < BLKDBG__MAX; i++) {
151 if (!strcmp(BlkdebugEvent_lookup[i], name)) {
152 *event = i;
153 return 0;
157 return -1;
160 struct add_rule_data {
161 BDRVBlkdebugState *s;
162 int action;
165 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
167 struct add_rule_data *d = opaque;
168 BDRVBlkdebugState *s = d->s;
169 const char* event_name;
170 BlkdebugEvent event;
171 struct BlkdebugRule *rule;
173 /* Find the right event for the rule */
174 event_name = qemu_opt_get(opts, "event");
175 if (!event_name) {
176 error_setg(errp, "Missing event name for rule");
177 return -1;
178 } else if (get_event_by_name(event_name, &event) < 0) {
179 error_setg(errp, "Invalid event name \"%s\"", event_name);
180 return -1;
183 /* Set attributes common for all actions */
184 rule = g_malloc0(sizeof(*rule));
185 *rule = (struct BlkdebugRule) {
186 .event = event,
187 .action = d->action,
188 .state = qemu_opt_get_number(opts, "state", 0),
191 /* Parse action-specific options */
192 switch (d->action) {
193 case ACTION_INJECT_ERROR:
194 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
195 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
196 rule->options.inject.immediately =
197 qemu_opt_get_bool(opts, "immediately", 0);
198 rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
199 break;
201 case ACTION_SET_STATE:
202 rule->options.set_state.new_state =
203 qemu_opt_get_number(opts, "new_state", 0);
204 break;
206 case ACTION_SUSPEND:
207 rule->options.suspend.tag =
208 g_strdup(qemu_opt_get(opts, "tag"));
209 break;
212 /* Add the rule */
213 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
215 return 0;
218 static void remove_rule(BlkdebugRule *rule)
220 switch (rule->action) {
221 case ACTION_INJECT_ERROR:
222 case ACTION_SET_STATE:
223 break;
224 case ACTION_SUSPEND:
225 g_free(rule->options.suspend.tag);
226 break;
229 QLIST_REMOVE(rule, next);
230 g_free(rule);
233 static int read_config(BDRVBlkdebugState *s, const char *filename,
234 QDict *options, Error **errp)
236 FILE *f = NULL;
237 int ret;
238 struct add_rule_data d;
239 Error *local_err = NULL;
241 if (filename) {
242 f = fopen(filename, "r");
243 if (f == NULL) {
244 error_setg_errno(errp, errno, "Could not read blkdebug config file");
245 return -errno;
248 ret = qemu_config_parse(f, config_groups, filename);
249 if (ret < 0) {
250 error_setg(errp, "Could not parse blkdebug config file");
251 ret = -EINVAL;
252 goto fail;
256 qemu_config_parse_qdict(options, config_groups, &local_err);
257 if (local_err) {
258 error_propagate(errp, local_err);
259 ret = -EINVAL;
260 goto fail;
263 d.s = s;
264 d.action = ACTION_INJECT_ERROR;
265 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
266 if (local_err) {
267 error_propagate(errp, local_err);
268 ret = -EINVAL;
269 goto fail;
272 d.action = ACTION_SET_STATE;
273 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
274 if (local_err) {
275 error_propagate(errp, local_err);
276 ret = -EINVAL;
277 goto fail;
280 ret = 0;
281 fail:
282 qemu_opts_reset(&inject_error_opts);
283 qemu_opts_reset(&set_state_opts);
284 if (f) {
285 fclose(f);
287 return ret;
290 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
291 static void blkdebug_parse_filename(const char *filename, QDict *options,
292 Error **errp)
294 const char *c;
296 /* Parse the blkdebug: prefix */
297 if (!strstart(filename, "blkdebug:", &filename)) {
298 /* There was no prefix; therefore, all options have to be already
299 present in the QDict (except for the filename) */
300 qdict_put(options, "x-image", qstring_from_str(filename));
301 return;
304 /* Parse config file path */
305 c = strchr(filename, ':');
306 if (c == NULL) {
307 error_setg(errp, "blkdebug requires both config file and image path");
308 return;
311 if (c != filename) {
312 QString *config_path;
313 config_path = qstring_from_substr(filename, 0, c - filename - 1);
314 qdict_put(options, "config", config_path);
317 /* TODO Allow multi-level nesting and set file.filename here */
318 filename = c + 1;
319 qdict_put(options, "x-image", qstring_from_str(filename));
322 static QemuOptsList runtime_opts = {
323 .name = "blkdebug",
324 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
325 .desc = {
327 .name = "config",
328 .type = QEMU_OPT_STRING,
329 .help = "Path to the configuration file",
332 .name = "x-image",
333 .type = QEMU_OPT_STRING,
334 .help = "[internal use only, will be removed]",
337 .name = "align",
338 .type = QEMU_OPT_SIZE,
339 .help = "Required alignment in bytes",
341 { /* end of list */ }
345 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
346 Error **errp)
348 BDRVBlkdebugState *s = bs->opaque;
349 QemuOpts *opts;
350 Error *local_err = NULL;
351 const char *config;
352 uint64_t align;
353 int ret;
355 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
356 qemu_opts_absorb_qdict(opts, options, &local_err);
357 if (local_err) {
358 error_propagate(errp, local_err);
359 ret = -EINVAL;
360 goto out;
363 /* Read rules from config file or command line options */
364 config = qemu_opt_get(opts, "config");
365 ret = read_config(s, config, options, errp);
366 if (ret) {
367 goto out;
370 /* Set initial state */
371 s->state = 1;
373 /* Open the image file */
374 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
375 bs, &child_file, false, &local_err);
376 if (local_err) {
377 ret = -EINVAL;
378 error_propagate(errp, local_err);
379 goto out;
382 /* Set request alignment */
383 align = qemu_opt_get_size(opts, "align", bs->request_alignment);
384 if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
385 bs->request_alignment = align;
386 } else {
387 error_setg(errp, "Invalid alignment");
388 ret = -EINVAL;
389 goto fail_unref;
392 ret = 0;
393 goto out;
395 fail_unref:
396 bdrv_unref_child(bs, bs->file);
397 out:
398 qemu_opts_del(opts);
399 return ret;
402 static void error_callback_bh(void *opaque)
404 struct BlkdebugAIOCB *acb = opaque;
405 qemu_bh_delete(acb->bh);
406 acb->common.cb(acb->common.opaque, acb->ret);
407 qemu_aio_unref(acb);
410 static BlockAIOCB *inject_error(BlockDriverState *bs,
411 BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
413 BDRVBlkdebugState *s = bs->opaque;
414 int error = rule->options.inject.error;
415 struct BlkdebugAIOCB *acb;
416 QEMUBH *bh;
417 bool immediately = rule->options.inject.immediately;
419 if (rule->options.inject.once) {
420 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
421 remove_rule(rule);
424 if (immediately) {
425 return NULL;
428 acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
429 acb->ret = -error;
431 bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
432 acb->bh = bh;
433 qemu_bh_schedule(bh);
435 return &acb->common;
438 static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
439 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
440 BlockCompletionFunc *cb, void *opaque)
442 BDRVBlkdebugState *s = bs->opaque;
443 BlkdebugRule *rule = NULL;
445 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
446 if (rule->options.inject.sector == -1 ||
447 (rule->options.inject.sector >= sector_num &&
448 rule->options.inject.sector < sector_num + nb_sectors)) {
449 break;
453 if (rule && rule->options.inject.error) {
454 return inject_error(bs, cb, opaque, rule);
457 return bdrv_aio_readv(bs->file->bs, sector_num, qiov, nb_sectors,
458 cb, opaque);
461 static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
462 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
463 BlockCompletionFunc *cb, void *opaque)
465 BDRVBlkdebugState *s = bs->opaque;
466 BlkdebugRule *rule = NULL;
468 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
469 if (rule->options.inject.sector == -1 ||
470 (rule->options.inject.sector >= sector_num &&
471 rule->options.inject.sector < sector_num + nb_sectors)) {
472 break;
476 if (rule && rule->options.inject.error) {
477 return inject_error(bs, cb, opaque, rule);
480 return bdrv_aio_writev(bs->file->bs, sector_num, qiov, nb_sectors,
481 cb, opaque);
484 static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
485 BlockCompletionFunc *cb, void *opaque)
487 BDRVBlkdebugState *s = bs->opaque;
488 BlkdebugRule *rule = NULL;
490 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
491 if (rule->options.inject.sector == -1) {
492 break;
496 if (rule && rule->options.inject.error) {
497 return inject_error(bs, cb, opaque, rule);
500 return bdrv_aio_flush(bs->file->bs, cb, opaque);
504 static void blkdebug_close(BlockDriverState *bs)
506 BDRVBlkdebugState *s = bs->opaque;
507 BlkdebugRule *rule, *next;
508 int i;
510 for (i = 0; i < BLKDBG__MAX; i++) {
511 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
512 remove_rule(rule);
517 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
519 BDRVBlkdebugState *s = bs->opaque;
520 BlkdebugSuspendedReq r;
522 r = (BlkdebugSuspendedReq) {
523 .co = qemu_coroutine_self(),
524 .tag = g_strdup(rule->options.suspend.tag),
527 remove_rule(rule);
528 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
530 if (!qtest_enabled()) {
531 printf("blkdebug: Suspended request '%s'\n", r.tag);
533 qemu_coroutine_yield();
534 if (!qtest_enabled()) {
535 printf("blkdebug: Resuming request '%s'\n", r.tag);
538 QLIST_REMOVE(&r, next);
539 g_free(r.tag);
542 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
543 bool injected)
545 BDRVBlkdebugState *s = bs->opaque;
547 /* Only process rules for the current state */
548 if (rule->state && rule->state != s->state) {
549 return injected;
552 /* Take the action */
553 switch (rule->action) {
554 case ACTION_INJECT_ERROR:
555 if (!injected) {
556 QSIMPLEQ_INIT(&s->active_rules);
557 injected = true;
559 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
560 break;
562 case ACTION_SET_STATE:
563 s->new_state = rule->options.set_state.new_state;
564 break;
566 case ACTION_SUSPEND:
567 suspend_request(bs, rule);
568 break;
570 return injected;
573 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
575 BDRVBlkdebugState *s = bs->opaque;
576 struct BlkdebugRule *rule, *next;
577 bool injected;
579 assert((int)event >= 0 && event < BLKDBG__MAX);
581 injected = false;
582 s->new_state = s->state;
583 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
584 injected = process_rule(bs, rule, injected);
586 s->state = s->new_state;
589 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
590 const char *tag)
592 BDRVBlkdebugState *s = bs->opaque;
593 struct BlkdebugRule *rule;
594 BlkdebugEvent blkdebug_event;
596 if (get_event_by_name(event, &blkdebug_event) < 0) {
597 return -ENOENT;
601 rule = g_malloc(sizeof(*rule));
602 *rule = (struct BlkdebugRule) {
603 .event = blkdebug_event,
604 .action = ACTION_SUSPEND,
605 .state = 0,
606 .options.suspend.tag = g_strdup(tag),
609 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
611 return 0;
614 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
616 BDRVBlkdebugState *s = bs->opaque;
617 BlkdebugSuspendedReq *r, *next;
619 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
620 if (!strcmp(r->tag, tag)) {
621 qemu_coroutine_enter(r->co, NULL);
622 return 0;
625 return -ENOENT;
628 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
629 const char *tag)
631 BDRVBlkdebugState *s = bs->opaque;
632 BlkdebugSuspendedReq *r, *r_next;
633 BlkdebugRule *rule, *next;
634 int i, ret = -ENOENT;
636 for (i = 0; i < BLKDBG__MAX; i++) {
637 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
638 if (rule->action == ACTION_SUSPEND &&
639 !strcmp(rule->options.suspend.tag, tag)) {
640 remove_rule(rule);
641 ret = 0;
645 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
646 if (!strcmp(r->tag, tag)) {
647 qemu_coroutine_enter(r->co, NULL);
648 ret = 0;
651 return ret;
654 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
656 BDRVBlkdebugState *s = bs->opaque;
657 BlkdebugSuspendedReq *r;
659 QLIST_FOREACH(r, &s->suspended_reqs, next) {
660 if (!strcmp(r->tag, tag)) {
661 return true;
664 return false;
667 static int64_t blkdebug_getlength(BlockDriverState *bs)
669 return bdrv_getlength(bs->file->bs);
672 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset)
674 return bdrv_truncate(bs->file->bs, offset);
677 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
679 QDict *opts;
680 const QDictEntry *e;
681 bool force_json = false;
683 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
684 if (strcmp(qdict_entry_key(e), "config") &&
685 strcmp(qdict_entry_key(e), "x-image"))
687 force_json = true;
688 break;
692 if (force_json && !bs->file->bs->full_open_options) {
693 /* The config file cannot be recreated, so creating a plain filename
694 * is impossible */
695 return;
698 if (!force_json && bs->file->bs->exact_filename[0]) {
699 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
700 "blkdebug:%s:%s",
701 qdict_get_try_str(options, "config") ?: "",
702 bs->file->bs->exact_filename);
705 opts = qdict_new();
706 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
708 QINCREF(bs->file->bs->full_open_options);
709 qdict_put_obj(opts, "image", QOBJECT(bs->file->bs->full_open_options));
711 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
712 if (strcmp(qdict_entry_key(e), "x-image")) {
713 qobject_incref(qdict_entry_value(e));
714 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
718 bs->full_open_options = opts;
721 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
722 BlockReopenQueue *queue, Error **errp)
724 return 0;
727 static BlockDriver bdrv_blkdebug = {
728 .format_name = "blkdebug",
729 .protocol_name = "blkdebug",
730 .instance_size = sizeof(BDRVBlkdebugState),
732 .bdrv_parse_filename = blkdebug_parse_filename,
733 .bdrv_file_open = blkdebug_open,
734 .bdrv_close = blkdebug_close,
735 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
736 .bdrv_getlength = blkdebug_getlength,
737 .bdrv_truncate = blkdebug_truncate,
738 .bdrv_refresh_filename = blkdebug_refresh_filename,
740 .bdrv_aio_readv = blkdebug_aio_readv,
741 .bdrv_aio_writev = blkdebug_aio_writev,
742 .bdrv_aio_flush = blkdebug_aio_flush,
744 .bdrv_debug_event = blkdebug_debug_event,
745 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
746 .bdrv_debug_remove_breakpoint
747 = blkdebug_debug_remove_breakpoint,
748 .bdrv_debug_resume = blkdebug_debug_resume,
749 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
752 static void bdrv_blkdebug_init(void)
754 bdrv_register(&bdrv_blkdebug);
757 block_init(bdrv_blkdebug_init);