2 * Block protocol for I/O error injection
4 * Copyright (C) 2016-2017 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/cutils.h"
29 #include "qemu/config-file.h"
30 #include "block/block_int.h"
31 #include "qemu/module.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qint.h"
35 #include "qapi/qmp/qstring.h"
36 #include "sysemu/qtest.h"
38 typedef struct BDRVBlkdebugState {
42 uint64_t max_transfer;
43 uint64_t opt_write_zero;
44 uint64_t max_write_zero;
48 /* For blkdebug_refresh_filename() */
51 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
52 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
53 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
56 typedef struct BlkdebugAIOCB {
61 typedef struct BlkdebugSuspendedReq {
64 QLIST_ENTRY(BlkdebugSuspendedReq) next;
65 } BlkdebugSuspendedReq;
73 typedef struct BlkdebugRule {
91 QLIST_ENTRY(BlkdebugRule) next;
92 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
95 static QemuOptsList inject_error_opts = {
96 .name = "inject-error",
97 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
101 .type = QEMU_OPT_STRING,
105 .type = QEMU_OPT_NUMBER,
109 .type = QEMU_OPT_NUMBER,
113 .type = QEMU_OPT_NUMBER,
117 .type = QEMU_OPT_BOOL,
120 .name = "immediately",
121 .type = QEMU_OPT_BOOL,
123 { /* end of list */ }
127 static QemuOptsList set_state_opts = {
129 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
133 .type = QEMU_OPT_STRING,
137 .type = QEMU_OPT_NUMBER,
141 .type = QEMU_OPT_NUMBER,
143 { /* end of list */ }
147 static QemuOptsList *config_groups[] = {
153 static int get_event_by_name(const char *name, BlkdebugEvent *event)
157 for (i = 0; i < BLKDBG__MAX; i++) {
158 if (!strcmp(BlkdebugEvent_lookup[i], name)) {
167 struct add_rule_data {
168 BDRVBlkdebugState *s;
172 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
174 struct add_rule_data *d = opaque;
175 BDRVBlkdebugState *s = d->s;
176 const char* event_name;
178 struct BlkdebugRule *rule;
181 /* Find the right event for the rule */
182 event_name = qemu_opt_get(opts, "event");
184 error_setg(errp, "Missing event name for rule");
186 } else if (get_event_by_name(event_name, &event) < 0) {
187 error_setg(errp, "Invalid event name \"%s\"", event_name);
191 /* Set attributes common for all actions */
192 rule = g_malloc0(sizeof(*rule));
193 *rule = (struct BlkdebugRule) {
196 .state = qemu_opt_get_number(opts, "state", 0),
199 /* Parse action-specific options */
201 case ACTION_INJECT_ERROR:
202 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
203 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
204 rule->options.inject.immediately =
205 qemu_opt_get_bool(opts, "immediately", 0);
206 sector = qemu_opt_get_number(opts, "sector", -1);
207 rule->options.inject.offset =
208 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
211 case ACTION_SET_STATE:
212 rule->options.set_state.new_state =
213 qemu_opt_get_number(opts, "new_state", 0);
217 rule->options.suspend.tag =
218 g_strdup(qemu_opt_get(opts, "tag"));
223 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
228 static void remove_rule(BlkdebugRule *rule)
230 switch (rule->action) {
231 case ACTION_INJECT_ERROR:
232 case ACTION_SET_STATE:
235 g_free(rule->options.suspend.tag);
239 QLIST_REMOVE(rule, next);
243 static int read_config(BDRVBlkdebugState *s, const char *filename,
244 QDict *options, Error **errp)
248 struct add_rule_data d;
249 Error *local_err = NULL;
252 f = fopen(filename, "r");
254 error_setg_errno(errp, errno, "Could not read blkdebug config file");
258 ret = qemu_config_parse(f, config_groups, filename);
260 error_setg(errp, "Could not parse blkdebug config file");
266 qemu_config_parse_qdict(options, config_groups, &local_err);
268 error_propagate(errp, local_err);
274 d.action = ACTION_INJECT_ERROR;
275 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
277 error_propagate(errp, local_err);
282 d.action = ACTION_SET_STATE;
283 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
285 error_propagate(errp, local_err);
292 qemu_opts_reset(&inject_error_opts);
293 qemu_opts_reset(&set_state_opts);
300 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
301 static void blkdebug_parse_filename(const char *filename, QDict *options,
306 /* Parse the blkdebug: prefix */
307 if (!strstart(filename, "blkdebug:", &filename)) {
308 /* There was no prefix; therefore, all options have to be already
309 present in the QDict (except for the filename) */
310 qdict_put_str(options, "x-image", filename);
314 /* Parse config file path */
315 c = strchr(filename, ':');
317 error_setg(errp, "blkdebug requires both config file and image path");
322 QString *config_path;
323 config_path = qstring_from_substr(filename, 0, c - filename - 1);
324 qdict_put(options, "config", config_path);
327 /* TODO Allow multi-level nesting and set file.filename here */
329 qdict_put_str(options, "x-image", filename);
332 static QemuOptsList runtime_opts = {
334 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
338 .type = QEMU_OPT_STRING,
339 .help = "Path to the configuration file",
343 .type = QEMU_OPT_STRING,
344 .help = "[internal use only, will be removed]",
348 .type = QEMU_OPT_SIZE,
349 .help = "Required alignment in bytes",
352 .name = "max-transfer",
353 .type = QEMU_OPT_SIZE,
354 .help = "Maximum transfer size in bytes",
357 .name = "opt-write-zero",
358 .type = QEMU_OPT_SIZE,
359 .help = "Optimum write zero alignment in bytes",
362 .name = "max-write-zero",
363 .type = QEMU_OPT_SIZE,
364 .help = "Maximum write zero size in bytes",
367 .name = "opt-discard",
368 .type = QEMU_OPT_SIZE,
369 .help = "Optimum discard alignment in bytes",
372 .name = "max-discard",
373 .type = QEMU_OPT_SIZE,
374 .help = "Maximum discard size in bytes",
376 { /* end of list */ }
380 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
383 BDRVBlkdebugState *s = bs->opaque;
385 Error *local_err = NULL;
389 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
390 qemu_opts_absorb_qdict(opts, options, &local_err);
392 error_propagate(errp, local_err);
397 /* Read rules from config file or command line options */
398 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
399 ret = read_config(s, s->config_file, options, errp);
404 /* Set initial state */
407 /* Open the image file */
408 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
409 bs, &child_file, false, &local_err);
412 error_propagate(errp, local_err);
416 bs->supported_write_flags = BDRV_REQ_FUA &
417 bs->file->bs->supported_write_flags;
418 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
419 bs->file->bs->supported_zero_flags;
422 /* Set alignment overrides */
423 s->align = qemu_opt_get_size(opts, "align", 0);
424 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
425 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
429 align = MAX(s->align, bs->file->bs->bl.request_alignment);
431 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
432 if (s->max_transfer &&
433 (s->max_transfer >= INT_MAX ||
434 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
435 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
440 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
441 if (s->opt_write_zero &&
442 (s->opt_write_zero >= INT_MAX ||
443 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
444 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
449 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
450 if (s->max_write_zero &&
451 (s->max_write_zero >= INT_MAX ||
452 !QEMU_IS_ALIGNED(s->max_write_zero,
453 MAX(s->opt_write_zero, align)))) {
454 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
459 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
460 if (s->opt_discard &&
461 (s->opt_discard >= INT_MAX ||
462 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
463 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
468 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
469 if (s->max_discard &&
470 (s->max_discard >= INT_MAX ||
471 !QEMU_IS_ALIGNED(s->max_discard,
472 MAX(s->opt_discard, align)))) {
473 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
481 g_free(s->config_file);
487 static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
489 BDRVBlkdebugState *s = bs->opaque;
490 BlkdebugRule *rule = NULL;
494 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
495 uint64_t inject_offset = rule->options.inject.offset;
497 if (inject_offset == -1 ||
498 (bytes && inject_offset >= offset &&
499 inject_offset < offset + bytes))
505 if (!rule || !rule->options.inject.error) {
509 immediately = rule->options.inject.immediately;
510 error = rule->options.inject.error;
512 if (rule->options.inject.once) {
513 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
518 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
519 qemu_coroutine_yield();
525 static int coroutine_fn
526 blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
527 QEMUIOVector *qiov, int flags)
531 /* Sanity check block layer guarantees */
532 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
533 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
534 if (bs->bl.max_transfer) {
535 assert(bytes <= bs->bl.max_transfer);
538 err = rule_check(bs, offset, bytes);
543 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
546 static int coroutine_fn
547 blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
548 QEMUIOVector *qiov, int flags)
552 /* Sanity check block layer guarantees */
553 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
554 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
555 if (bs->bl.max_transfer) {
556 assert(bytes <= bs->bl.max_transfer);
559 err = rule_check(bs, offset, bytes);
564 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
567 static int blkdebug_co_flush(BlockDriverState *bs)
569 int err = rule_check(bs, 0, 0);
575 return bdrv_co_flush(bs->file->bs);
578 static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
579 int64_t offset, int count,
580 BdrvRequestFlags flags)
582 uint32_t align = MAX(bs->bl.request_alignment,
583 bs->bl.pwrite_zeroes_alignment);
586 /* Only pass through requests that are larger than requested
587 * preferred alignment (so that we test the fallback to writes on
588 * unaligned portions), and check that the block layer never hands
589 * us anything unaligned that crosses an alignment boundary. */
591 assert(QEMU_IS_ALIGNED(offset, align) ||
592 QEMU_IS_ALIGNED(offset + count, align) ||
593 DIV_ROUND_UP(offset, align) ==
594 DIV_ROUND_UP(offset + count, align));
597 assert(QEMU_IS_ALIGNED(offset, align));
598 assert(QEMU_IS_ALIGNED(count, align));
599 if (bs->bl.max_pwrite_zeroes) {
600 assert(count <= bs->bl.max_pwrite_zeroes);
603 err = rule_check(bs, offset, count);
608 return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
611 static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
612 int64_t offset, int count)
614 uint32_t align = bs->bl.pdiscard_alignment;
617 /* Only pass through requests that are larger than requested
618 * minimum alignment, and ensure that unaligned requests do not
619 * cross optimum discard boundaries. */
620 if (count < bs->bl.request_alignment) {
621 assert(QEMU_IS_ALIGNED(offset, align) ||
622 QEMU_IS_ALIGNED(offset + count, align) ||
623 DIV_ROUND_UP(offset, align) ==
624 DIV_ROUND_UP(offset + count, align));
627 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
628 assert(QEMU_IS_ALIGNED(count, bs->bl.request_alignment));
629 if (align && count >= align) {
630 assert(QEMU_IS_ALIGNED(offset, align));
631 assert(QEMU_IS_ALIGNED(count, align));
633 if (bs->bl.max_pdiscard) {
634 assert(count <= bs->bl.max_pdiscard);
637 err = rule_check(bs, offset, count);
642 return bdrv_co_pdiscard(bs->file->bs, offset, count);
645 static void blkdebug_close(BlockDriverState *bs)
647 BDRVBlkdebugState *s = bs->opaque;
648 BlkdebugRule *rule, *next;
651 for (i = 0; i < BLKDBG__MAX; i++) {
652 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
657 g_free(s->config_file);
660 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
662 BDRVBlkdebugState *s = bs->opaque;
663 BlkdebugSuspendedReq r;
665 r = (BlkdebugSuspendedReq) {
666 .co = qemu_coroutine_self(),
667 .tag = g_strdup(rule->options.suspend.tag),
671 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
673 if (!qtest_enabled()) {
674 printf("blkdebug: Suspended request '%s'\n", r.tag);
676 qemu_coroutine_yield();
677 if (!qtest_enabled()) {
678 printf("blkdebug: Resuming request '%s'\n", r.tag);
681 QLIST_REMOVE(&r, next);
685 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
688 BDRVBlkdebugState *s = bs->opaque;
690 /* Only process rules for the current state */
691 if (rule->state && rule->state != s->state) {
695 /* Take the action */
696 switch (rule->action) {
697 case ACTION_INJECT_ERROR:
699 QSIMPLEQ_INIT(&s->active_rules);
702 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
705 case ACTION_SET_STATE:
706 s->new_state = rule->options.set_state.new_state;
710 suspend_request(bs, rule);
716 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
718 BDRVBlkdebugState *s = bs->opaque;
719 struct BlkdebugRule *rule, *next;
722 assert((int)event >= 0 && event < BLKDBG__MAX);
725 s->new_state = s->state;
726 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
727 injected = process_rule(bs, rule, injected);
729 s->state = s->new_state;
732 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
735 BDRVBlkdebugState *s = bs->opaque;
736 struct BlkdebugRule *rule;
737 BlkdebugEvent blkdebug_event;
739 if (get_event_by_name(event, &blkdebug_event) < 0) {
744 rule = g_malloc(sizeof(*rule));
745 *rule = (struct BlkdebugRule) {
746 .event = blkdebug_event,
747 .action = ACTION_SUSPEND,
749 .options.suspend.tag = g_strdup(tag),
752 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
757 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
759 BDRVBlkdebugState *s = bs->opaque;
760 BlkdebugSuspendedReq *r, *next;
762 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
763 if (!strcmp(r->tag, tag)) {
764 qemu_coroutine_enter(r->co);
771 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
774 BDRVBlkdebugState *s = bs->opaque;
775 BlkdebugSuspendedReq *r, *r_next;
776 BlkdebugRule *rule, *next;
777 int i, ret = -ENOENT;
779 for (i = 0; i < BLKDBG__MAX; i++) {
780 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
781 if (rule->action == ACTION_SUSPEND &&
782 !strcmp(rule->options.suspend.tag, tag)) {
788 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
789 if (!strcmp(r->tag, tag)) {
790 qemu_coroutine_enter(r->co);
797 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
799 BDRVBlkdebugState *s = bs->opaque;
800 BlkdebugSuspendedReq *r;
802 QLIST_FOREACH(r, &s->suspended_reqs, next) {
803 if (!strcmp(r->tag, tag)) {
810 static int64_t blkdebug_getlength(BlockDriverState *bs)
812 return bdrv_getlength(bs->file->bs);
815 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
817 return bdrv_truncate(bs->file, offset, errp);
820 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
822 BDRVBlkdebugState *s = bs->opaque;
825 bool force_json = false;
827 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
828 if (strcmp(qdict_entry_key(e), "config") &&
829 strcmp(qdict_entry_key(e), "x-image"))
836 if (force_json && !bs->file->bs->full_open_options) {
837 /* The config file cannot be recreated, so creating a plain filename
842 if (!force_json && bs->file->bs->exact_filename[0]) {
843 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
844 "blkdebug:%s:%s", s->config_file ?: "",
845 bs->file->bs->exact_filename);
849 qdict_put_str(opts, "driver", "blkdebug");
851 QINCREF(bs->file->bs->full_open_options);
852 qdict_put(opts, "image", bs->file->bs->full_open_options);
854 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
855 if (strcmp(qdict_entry_key(e), "x-image")) {
856 qobject_incref(qdict_entry_value(e));
857 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
861 bs->full_open_options = opts;
864 static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
866 BDRVBlkdebugState *s = bs->opaque;
869 bs->bl.request_alignment = s->align;
871 if (s->max_transfer) {
872 bs->bl.max_transfer = s->max_transfer;
874 if (s->opt_write_zero) {
875 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
877 if (s->max_write_zero) {
878 bs->bl.max_pwrite_zeroes = s->max_write_zero;
880 if (s->opt_discard) {
881 bs->bl.pdiscard_alignment = s->opt_discard;
883 if (s->max_discard) {
884 bs->bl.max_pdiscard = s->max_discard;
888 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
889 BlockReopenQueue *queue, Error **errp)
894 static BlockDriver bdrv_blkdebug = {
895 .format_name = "blkdebug",
896 .protocol_name = "blkdebug",
897 .instance_size = sizeof(BDRVBlkdebugState),
899 .bdrv_parse_filename = blkdebug_parse_filename,
900 .bdrv_file_open = blkdebug_open,
901 .bdrv_close = blkdebug_close,
902 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
903 .bdrv_child_perm = bdrv_filter_default_perms,
905 .bdrv_getlength = blkdebug_getlength,
906 .bdrv_truncate = blkdebug_truncate,
907 .bdrv_refresh_filename = blkdebug_refresh_filename,
908 .bdrv_refresh_limits = blkdebug_refresh_limits,
910 .bdrv_co_preadv = blkdebug_co_preadv,
911 .bdrv_co_pwritev = blkdebug_co_pwritev,
912 .bdrv_co_flush_to_disk = blkdebug_co_flush,
913 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
914 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
916 .bdrv_debug_event = blkdebug_debug_event,
917 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
918 .bdrv_debug_remove_breakpoint
919 = blkdebug_debug_remove_breakpoint,
920 .bdrv_debug_resume = blkdebug_debug_resume,
921 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
924 static void bdrv_blkdebug_init(void)
926 bdrv_register(&bdrv_blkdebug);
929 block_init(bdrv_blkdebug_init);