Merge remote-tracking branch 'remotes/vivier2/tags/linux-user-for-6.1-pull-request...
[qemu/ar7.git] / block / blkdebug.c
blob2c0b9b0ee85efa74837e1e495872dfb19ba0080f
1 /*
2 * Block protocol for I/O error injection
4 * Copyright (C) 2016-2017 Red Hat, Inc.
5 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
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
23 * THE SOFTWARE.
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 "block/qdict.h"
32 #include "qemu/module.h"
33 #include "qemu/option.h"
34 #include "qapi/qapi-visit-block-core.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qlist.h"
37 #include "qapi/qmp/qstring.h"
38 #include "qapi/qobject-input-visitor.h"
39 #include "sysemu/qtest.h"
41 typedef struct BDRVBlkdebugState {
42 int state;
43 int new_state;
44 uint64_t align;
45 uint64_t max_transfer;
46 uint64_t opt_write_zero;
47 uint64_t max_write_zero;
48 uint64_t opt_discard;
49 uint64_t max_discard;
51 uint64_t take_child_perms;
52 uint64_t unshare_child_perms;
54 /* For blkdebug_refresh_filename() */
55 char *config_file;
57 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
58 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
59 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
60 } BDRVBlkdebugState;
62 typedef struct BlkdebugAIOCB {
63 BlockAIOCB common;
64 int ret;
65 } BlkdebugAIOCB;
67 typedef struct BlkdebugSuspendedReq {
68 Coroutine *co;
69 char *tag;
70 QLIST_ENTRY(BlkdebugSuspendedReq) next;
71 } BlkdebugSuspendedReq;
73 enum {
74 ACTION_INJECT_ERROR,
75 ACTION_SET_STATE,
76 ACTION_SUSPEND,
79 typedef struct BlkdebugRule {
80 BlkdebugEvent event;
81 int action;
82 int state;
83 union {
84 struct {
85 uint64_t iotype_mask;
86 int error;
87 int immediately;
88 int once;
89 int64_t offset;
90 } inject;
91 struct {
92 int new_state;
93 } set_state;
94 struct {
95 char *tag;
96 } suspend;
97 } options;
98 QLIST_ENTRY(BlkdebugRule) next;
99 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
100 } BlkdebugRule;
102 QEMU_BUILD_BUG_MSG(BLKDEBUG_IO_TYPE__MAX > 64,
103 "BlkdebugIOType mask does not fit into an uint64_t");
105 static QemuOptsList inject_error_opts = {
106 .name = "inject-error",
107 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
108 .desc = {
110 .name = "event",
111 .type = QEMU_OPT_STRING,
114 .name = "state",
115 .type = QEMU_OPT_NUMBER,
118 .name = "iotype",
119 .type = QEMU_OPT_STRING,
122 .name = "errno",
123 .type = QEMU_OPT_NUMBER,
126 .name = "sector",
127 .type = QEMU_OPT_NUMBER,
130 .name = "once",
131 .type = QEMU_OPT_BOOL,
134 .name = "immediately",
135 .type = QEMU_OPT_BOOL,
137 { /* end of list */ }
141 static QemuOptsList set_state_opts = {
142 .name = "set-state",
143 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
144 .desc = {
146 .name = "event",
147 .type = QEMU_OPT_STRING,
150 .name = "state",
151 .type = QEMU_OPT_NUMBER,
154 .name = "new_state",
155 .type = QEMU_OPT_NUMBER,
157 { /* end of list */ }
161 static QemuOptsList *config_groups[] = {
162 &inject_error_opts,
163 &set_state_opts,
164 NULL
167 struct add_rule_data {
168 BDRVBlkdebugState *s;
169 int action;
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;
177 int event;
178 struct BlkdebugRule *rule;
179 int64_t sector;
180 BlkdebugIOType iotype;
181 Error *local_error = NULL;
183 /* Find the right event for the rule */
184 event_name = qemu_opt_get(opts, "event");
185 if (!event_name) {
186 error_setg(errp, "Missing event name for rule");
187 return -1;
189 event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
190 if (event < 0) {
191 return -1;
194 /* Set attributes common for all actions */
195 rule = g_malloc0(sizeof(*rule));
196 *rule = (struct BlkdebugRule) {
197 .event = event,
198 .action = d->action,
199 .state = qemu_opt_get_number(opts, "state", 0),
202 /* Parse action-specific options */
203 switch (d->action) {
204 case ACTION_INJECT_ERROR:
205 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
206 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
207 rule->options.inject.immediately =
208 qemu_opt_get_bool(opts, "immediately", 0);
209 sector = qemu_opt_get_number(opts, "sector", -1);
210 rule->options.inject.offset =
211 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
213 iotype = qapi_enum_parse(&BlkdebugIOType_lookup,
214 qemu_opt_get(opts, "iotype"),
215 BLKDEBUG_IO_TYPE__MAX, &local_error);
216 if (local_error) {
217 error_propagate(errp, local_error);
218 g_free(rule);
219 return -1;
221 if (iotype != BLKDEBUG_IO_TYPE__MAX) {
222 rule->options.inject.iotype_mask = (1ull << iotype);
223 } else {
224 /* Apply the default */
225 rule->options.inject.iotype_mask =
226 (1ull << BLKDEBUG_IO_TYPE_READ)
227 | (1ull << BLKDEBUG_IO_TYPE_WRITE)
228 | (1ull << BLKDEBUG_IO_TYPE_WRITE_ZEROES)
229 | (1ull << BLKDEBUG_IO_TYPE_DISCARD)
230 | (1ull << BLKDEBUG_IO_TYPE_FLUSH);
233 break;
235 case ACTION_SET_STATE:
236 rule->options.set_state.new_state =
237 qemu_opt_get_number(opts, "new_state", 0);
238 break;
240 case ACTION_SUSPEND:
241 rule->options.suspend.tag =
242 g_strdup(qemu_opt_get(opts, "tag"));
243 break;
246 /* Add the rule */
247 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
249 return 0;
252 static void remove_rule(BlkdebugRule *rule)
254 switch (rule->action) {
255 case ACTION_INJECT_ERROR:
256 case ACTION_SET_STATE:
257 break;
258 case ACTION_SUSPEND:
259 g_free(rule->options.suspend.tag);
260 break;
263 QLIST_REMOVE(rule, next);
264 g_free(rule);
267 static int read_config(BDRVBlkdebugState *s, const char *filename,
268 QDict *options, Error **errp)
270 FILE *f = NULL;
271 int ret;
272 struct add_rule_data d;
273 Error *local_err = NULL;
275 if (filename) {
276 f = fopen(filename, "r");
277 if (f == NULL) {
278 error_setg_errno(errp, errno, "Could not read blkdebug config file");
279 return -errno;
282 ret = qemu_config_parse(f, config_groups, filename, errp);
283 if (ret < 0) {
284 goto fail;
288 qemu_config_parse_qdict(options, config_groups, &local_err);
289 if (local_err) {
290 error_propagate(errp, local_err);
291 ret = -EINVAL;
292 goto fail;
295 d.s = s;
296 d.action = ACTION_INJECT_ERROR;
297 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
298 if (local_err) {
299 error_propagate(errp, local_err);
300 ret = -EINVAL;
301 goto fail;
304 d.action = ACTION_SET_STATE;
305 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
306 if (local_err) {
307 error_propagate(errp, local_err);
308 ret = -EINVAL;
309 goto fail;
312 ret = 0;
313 fail:
314 qemu_opts_reset(&inject_error_opts);
315 qemu_opts_reset(&set_state_opts);
316 if (f) {
317 fclose(f);
319 return ret;
322 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
323 static void blkdebug_parse_filename(const char *filename, QDict *options,
324 Error **errp)
326 const char *c;
328 /* Parse the blkdebug: prefix */
329 if (!strstart(filename, "blkdebug:", &filename)) {
330 /* There was no prefix; therefore, all options have to be already
331 present in the QDict (except for the filename) */
332 qdict_put_str(options, "x-image", filename);
333 return;
336 /* Parse config file path */
337 c = strchr(filename, ':');
338 if (c == NULL) {
339 error_setg(errp, "blkdebug requires both config file and image path");
340 return;
343 if (c != filename) {
344 QString *config_path;
345 config_path = qstring_from_substr(filename, 0, c - filename);
346 qdict_put(options, "config", config_path);
349 /* TODO Allow multi-level nesting and set file.filename here */
350 filename = c + 1;
351 qdict_put_str(options, "x-image", filename);
354 static int blkdebug_parse_perm_list(uint64_t *dest, QDict *options,
355 const char *prefix, Error **errp)
357 int ret = 0;
358 QDict *subqdict = NULL;
359 QObject *crumpled_subqdict = NULL;
360 Visitor *v = NULL;
361 BlockPermissionList *perm_list = NULL, *element;
363 *dest = 0;
365 qdict_extract_subqdict(options, &subqdict, prefix);
366 if (!qdict_size(subqdict)) {
367 goto out;
370 crumpled_subqdict = qdict_crumple(subqdict, errp);
371 if (!crumpled_subqdict) {
372 ret = -EINVAL;
373 goto out;
376 v = qobject_input_visitor_new(crumpled_subqdict);
377 if (!visit_type_BlockPermissionList(v, NULL, &perm_list, errp)) {
378 ret = -EINVAL;
379 goto out;
382 for (element = perm_list; element; element = element->next) {
383 *dest |= bdrv_qapi_perm_to_blk_perm(element->value);
386 out:
387 qapi_free_BlockPermissionList(perm_list);
388 visit_free(v);
389 qobject_unref(subqdict);
390 qobject_unref(crumpled_subqdict);
391 return ret;
394 static int blkdebug_parse_perms(BDRVBlkdebugState *s, QDict *options,
395 Error **errp)
397 int ret;
399 ret = blkdebug_parse_perm_list(&s->take_child_perms, options,
400 "take-child-perms.", errp);
401 if (ret < 0) {
402 return ret;
405 ret = blkdebug_parse_perm_list(&s->unshare_child_perms, options,
406 "unshare-child-perms.", errp);
407 if (ret < 0) {
408 return ret;
411 return 0;
414 static QemuOptsList runtime_opts = {
415 .name = "blkdebug",
416 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
417 .desc = {
419 .name = "config",
420 .type = QEMU_OPT_STRING,
421 .help = "Path to the configuration file",
424 .name = "x-image",
425 .type = QEMU_OPT_STRING,
426 .help = "[internal use only, will be removed]",
429 .name = "align",
430 .type = QEMU_OPT_SIZE,
431 .help = "Required alignment in bytes",
434 .name = "max-transfer",
435 .type = QEMU_OPT_SIZE,
436 .help = "Maximum transfer size in bytes",
439 .name = "opt-write-zero",
440 .type = QEMU_OPT_SIZE,
441 .help = "Optimum write zero alignment in bytes",
444 .name = "max-write-zero",
445 .type = QEMU_OPT_SIZE,
446 .help = "Maximum write zero size in bytes",
449 .name = "opt-discard",
450 .type = QEMU_OPT_SIZE,
451 .help = "Optimum discard alignment in bytes",
454 .name = "max-discard",
455 .type = QEMU_OPT_SIZE,
456 .help = "Maximum discard size in bytes",
458 { /* end of list */ }
462 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
463 Error **errp)
465 BDRVBlkdebugState *s = bs->opaque;
466 QemuOpts *opts;
467 int ret;
468 uint64_t align;
470 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
471 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
472 ret = -EINVAL;
473 goto out;
476 /* Read rules from config file or command line options */
477 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
478 ret = read_config(s, s->config_file, options, errp);
479 if (ret) {
480 goto out;
483 /* Set initial state */
484 s->state = 1;
486 /* Parse permissions modifiers before opening the image file */
487 ret = blkdebug_parse_perms(s, options, errp);
488 if (ret < 0) {
489 goto out;
492 /* Open the image file */
493 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
494 bs, &child_of_bds,
495 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
496 false, errp);
497 if (!bs->file) {
498 ret = -EINVAL;
499 goto out;
502 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED |
503 (BDRV_REQ_FUA & bs->file->bs->supported_write_flags);
504 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED |
505 ((BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK) &
506 bs->file->bs->supported_zero_flags);
507 ret = -EINVAL;
509 /* Set alignment overrides */
510 s->align = qemu_opt_get_size(opts, "align", 0);
511 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
512 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
513 s->align);
514 goto out;
516 align = MAX(s->align, bs->file->bs->bl.request_alignment);
518 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
519 if (s->max_transfer &&
520 (s->max_transfer >= INT_MAX ||
521 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
522 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
523 s->max_transfer);
524 goto out;
527 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
528 if (s->opt_write_zero &&
529 (s->opt_write_zero >= INT_MAX ||
530 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
531 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
532 s->opt_write_zero);
533 goto out;
536 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
537 if (s->max_write_zero &&
538 (s->max_write_zero >= INT_MAX ||
539 !QEMU_IS_ALIGNED(s->max_write_zero,
540 MAX(s->opt_write_zero, align)))) {
541 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
542 s->max_write_zero);
543 goto out;
546 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
547 if (s->opt_discard &&
548 (s->opt_discard >= INT_MAX ||
549 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
550 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
551 s->opt_discard);
552 goto out;
555 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
556 if (s->max_discard &&
557 (s->max_discard >= INT_MAX ||
558 !QEMU_IS_ALIGNED(s->max_discard,
559 MAX(s->opt_discard, align)))) {
560 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
561 s->max_discard);
562 goto out;
565 bdrv_debug_event(bs, BLKDBG_NONE);
567 ret = 0;
568 out:
569 if (ret < 0) {
570 g_free(s->config_file);
572 qemu_opts_del(opts);
573 return ret;
576 static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
577 BlkdebugIOType iotype)
579 BDRVBlkdebugState *s = bs->opaque;
580 BlkdebugRule *rule = NULL;
581 int error;
582 bool immediately;
584 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
585 uint64_t inject_offset = rule->options.inject.offset;
587 if ((inject_offset == -1 ||
588 (bytes && inject_offset >= offset &&
589 inject_offset < offset + bytes)) &&
590 (rule->options.inject.iotype_mask & (1ull << iotype)))
592 break;
596 if (!rule || !rule->options.inject.error) {
597 return 0;
600 immediately = rule->options.inject.immediately;
601 error = rule->options.inject.error;
603 if (rule->options.inject.once) {
604 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
605 remove_rule(rule);
608 if (!immediately) {
609 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
610 qemu_coroutine_yield();
613 return -error;
616 static int coroutine_fn
617 blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
618 QEMUIOVector *qiov, int flags)
620 int err;
622 /* Sanity check block layer guarantees */
623 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
624 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
625 if (bs->bl.max_transfer) {
626 assert(bytes <= bs->bl.max_transfer);
629 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_READ);
630 if (err) {
631 return err;
634 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
637 static int coroutine_fn
638 blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
639 QEMUIOVector *qiov, int flags)
641 int err;
643 /* Sanity check block layer guarantees */
644 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
645 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
646 if (bs->bl.max_transfer) {
647 assert(bytes <= bs->bl.max_transfer);
650 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE);
651 if (err) {
652 return err;
655 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
658 static int blkdebug_co_flush(BlockDriverState *bs)
660 int err = rule_check(bs, 0, 0, BLKDEBUG_IO_TYPE_FLUSH);
662 if (err) {
663 return err;
666 return bdrv_co_flush(bs->file->bs);
669 static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
670 int64_t offset, int bytes,
671 BdrvRequestFlags flags)
673 uint32_t align = MAX(bs->bl.request_alignment,
674 bs->bl.pwrite_zeroes_alignment);
675 int err;
677 /* Only pass through requests that are larger than requested
678 * preferred alignment (so that we test the fallback to writes on
679 * unaligned portions), and check that the block layer never hands
680 * us anything unaligned that crosses an alignment boundary. */
681 if (bytes < align) {
682 assert(QEMU_IS_ALIGNED(offset, align) ||
683 QEMU_IS_ALIGNED(offset + bytes, align) ||
684 DIV_ROUND_UP(offset, align) ==
685 DIV_ROUND_UP(offset + bytes, align));
686 return -ENOTSUP;
688 assert(QEMU_IS_ALIGNED(offset, align));
689 assert(QEMU_IS_ALIGNED(bytes, align));
690 if (bs->bl.max_pwrite_zeroes) {
691 assert(bytes <= bs->bl.max_pwrite_zeroes);
694 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_WRITE_ZEROES);
695 if (err) {
696 return err;
699 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
702 static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
703 int64_t offset, int bytes)
705 uint32_t align = bs->bl.pdiscard_alignment;
706 int err;
708 /* Only pass through requests that are larger than requested
709 * minimum alignment, and ensure that unaligned requests do not
710 * cross optimum discard boundaries. */
711 if (bytes < bs->bl.request_alignment) {
712 assert(QEMU_IS_ALIGNED(offset, align) ||
713 QEMU_IS_ALIGNED(offset + bytes, align) ||
714 DIV_ROUND_UP(offset, align) ==
715 DIV_ROUND_UP(offset + bytes, align));
716 return -ENOTSUP;
718 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
719 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
720 if (align && bytes >= align) {
721 assert(QEMU_IS_ALIGNED(offset, align));
722 assert(QEMU_IS_ALIGNED(bytes, align));
724 if (bs->bl.max_pdiscard) {
725 assert(bytes <= bs->bl.max_pdiscard);
728 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_DISCARD);
729 if (err) {
730 return err;
733 return bdrv_co_pdiscard(bs->file, offset, bytes);
736 static int coroutine_fn blkdebug_co_block_status(BlockDriverState *bs,
737 bool want_zero,
738 int64_t offset,
739 int64_t bytes,
740 int64_t *pnum,
741 int64_t *map,
742 BlockDriverState **file)
744 int err;
746 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
748 err = rule_check(bs, offset, bytes, BLKDEBUG_IO_TYPE_BLOCK_STATUS);
749 if (err) {
750 return err;
753 assert(bs->file && bs->file->bs);
754 *pnum = bytes;
755 *map = offset;
756 *file = bs->file->bs;
757 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID;
760 static void blkdebug_close(BlockDriverState *bs)
762 BDRVBlkdebugState *s = bs->opaque;
763 BlkdebugRule *rule, *next;
764 int i;
766 for (i = 0; i < BLKDBG__MAX; i++) {
767 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
768 remove_rule(rule);
772 g_free(s->config_file);
775 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
777 BDRVBlkdebugState *s = bs->opaque;
778 BlkdebugSuspendedReq r;
780 r = (BlkdebugSuspendedReq) {
781 .co = qemu_coroutine_self(),
782 .tag = g_strdup(rule->options.suspend.tag),
785 remove_rule(rule);
786 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
788 if (!qtest_enabled()) {
789 printf("blkdebug: Suspended request '%s'\n", r.tag);
791 qemu_coroutine_yield();
792 if (!qtest_enabled()) {
793 printf("blkdebug: Resuming request '%s'\n", r.tag);
796 QLIST_REMOVE(&r, next);
797 g_free(r.tag);
800 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
801 bool injected)
803 BDRVBlkdebugState *s = bs->opaque;
805 /* Only process rules for the current state */
806 if (rule->state && rule->state != s->state) {
807 return injected;
810 /* Take the action */
811 switch (rule->action) {
812 case ACTION_INJECT_ERROR:
813 if (!injected) {
814 QSIMPLEQ_INIT(&s->active_rules);
815 injected = true;
817 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
818 break;
820 case ACTION_SET_STATE:
821 s->new_state = rule->options.set_state.new_state;
822 break;
824 case ACTION_SUSPEND:
825 suspend_request(bs, rule);
826 break;
828 return injected;
831 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
833 BDRVBlkdebugState *s = bs->opaque;
834 struct BlkdebugRule *rule, *next;
835 bool injected;
837 assert((int)event >= 0 && event < BLKDBG__MAX);
839 injected = false;
840 s->new_state = s->state;
841 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
842 injected = process_rule(bs, rule, injected);
844 s->state = s->new_state;
847 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
848 const char *tag)
850 BDRVBlkdebugState *s = bs->opaque;
851 struct BlkdebugRule *rule;
852 int blkdebug_event;
854 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
855 if (blkdebug_event < 0) {
856 return -ENOENT;
859 rule = g_malloc(sizeof(*rule));
860 *rule = (struct BlkdebugRule) {
861 .event = blkdebug_event,
862 .action = ACTION_SUSPEND,
863 .state = 0,
864 .options.suspend.tag = g_strdup(tag),
867 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
869 return 0;
872 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
874 BDRVBlkdebugState *s = bs->opaque;
875 BlkdebugSuspendedReq *r, *next;
877 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
878 if (!strcmp(r->tag, tag)) {
879 qemu_coroutine_enter(r->co);
880 return 0;
883 return -ENOENT;
886 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
887 const char *tag)
889 BDRVBlkdebugState *s = bs->opaque;
890 BlkdebugSuspendedReq *r, *r_next;
891 BlkdebugRule *rule, *next;
892 int i, ret = -ENOENT;
894 for (i = 0; i < BLKDBG__MAX; i++) {
895 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
896 if (rule->action == ACTION_SUSPEND &&
897 !strcmp(rule->options.suspend.tag, tag)) {
898 remove_rule(rule);
899 ret = 0;
903 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
904 if (!strcmp(r->tag, tag)) {
905 qemu_coroutine_enter(r->co);
906 ret = 0;
909 return ret;
912 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
914 BDRVBlkdebugState *s = bs->opaque;
915 BlkdebugSuspendedReq *r;
917 QLIST_FOREACH(r, &s->suspended_reqs, next) {
918 if (!strcmp(r->tag, tag)) {
919 return true;
922 return false;
925 static int64_t blkdebug_getlength(BlockDriverState *bs)
927 return bdrv_getlength(bs->file->bs);
930 static void blkdebug_refresh_filename(BlockDriverState *bs)
932 BDRVBlkdebugState *s = bs->opaque;
933 const QDictEntry *e;
934 int ret;
936 if (!bs->file->bs->exact_filename[0]) {
937 return;
940 for (e = qdict_first(bs->full_open_options); e;
941 e = qdict_next(bs->full_open_options, e))
943 /* Real child options are under "image", but "x-image" may
944 * contain a filename */
945 if (strcmp(qdict_entry_key(e), "config") &&
946 strcmp(qdict_entry_key(e), "image") &&
947 strcmp(qdict_entry_key(e), "x-image") &&
948 strcmp(qdict_entry_key(e), "driver"))
950 return;
954 ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
955 "blkdebug:%s:%s",
956 s->config_file ?: "", bs->file->bs->exact_filename);
957 if (ret >= sizeof(bs->exact_filename)) {
958 /* An overflow makes the filename unusable, so do not report any */
959 bs->exact_filename[0] = 0;
963 static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
965 BDRVBlkdebugState *s = bs->opaque;
967 if (s->align) {
968 bs->bl.request_alignment = s->align;
970 if (s->max_transfer) {
971 bs->bl.max_transfer = s->max_transfer;
973 if (s->opt_write_zero) {
974 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
976 if (s->max_write_zero) {
977 bs->bl.max_pwrite_zeroes = s->max_write_zero;
979 if (s->opt_discard) {
980 bs->bl.pdiscard_alignment = s->opt_discard;
982 if (s->max_discard) {
983 bs->bl.max_pdiscard = s->max_discard;
987 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
988 BlockReopenQueue *queue, Error **errp)
990 return 0;
993 static void blkdebug_child_perm(BlockDriverState *bs, BdrvChild *c,
994 BdrvChildRole role,
995 BlockReopenQueue *reopen_queue,
996 uint64_t perm, uint64_t shared,
997 uint64_t *nperm, uint64_t *nshared)
999 BDRVBlkdebugState *s = bs->opaque;
1001 bdrv_default_perms(bs, c, role, reopen_queue,
1002 perm, shared, nperm, nshared);
1004 *nperm |= s->take_child_perms;
1005 *nshared &= ~s->unshare_child_perms;
1008 static const char *const blkdebug_strong_runtime_opts[] = {
1009 "config",
1010 "inject-error.",
1011 "set-state.",
1012 "align",
1013 "max-transfer",
1014 "opt-write-zero",
1015 "max-write-zero",
1016 "opt-discard",
1017 "max-discard",
1019 NULL
1022 static BlockDriver bdrv_blkdebug = {
1023 .format_name = "blkdebug",
1024 .protocol_name = "blkdebug",
1025 .instance_size = sizeof(BDRVBlkdebugState),
1026 .is_filter = true,
1028 .bdrv_parse_filename = blkdebug_parse_filename,
1029 .bdrv_file_open = blkdebug_open,
1030 .bdrv_close = blkdebug_close,
1031 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
1032 .bdrv_child_perm = blkdebug_child_perm,
1034 .bdrv_getlength = blkdebug_getlength,
1035 .bdrv_refresh_filename = blkdebug_refresh_filename,
1036 .bdrv_refresh_limits = blkdebug_refresh_limits,
1038 .bdrv_co_preadv = blkdebug_co_preadv,
1039 .bdrv_co_pwritev = blkdebug_co_pwritev,
1040 .bdrv_co_flush_to_disk = blkdebug_co_flush,
1041 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
1042 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
1043 .bdrv_co_block_status = blkdebug_co_block_status,
1045 .bdrv_debug_event = blkdebug_debug_event,
1046 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
1047 .bdrv_debug_remove_breakpoint
1048 = blkdebug_debug_remove_breakpoint,
1049 .bdrv_debug_resume = blkdebug_debug_resume,
1050 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
1052 .strong_runtime_opts = blkdebug_strong_runtime_opts,
1055 static void bdrv_blkdebug_init(void)
1057 bdrv_register(&bdrv_blkdebug);
1060 block_init(bdrv_blkdebug_init);