hw/arm/virt: add pmu interrupt state
[qemu/ar7.git] / block / blkdebug.c
blob8e385acf54678376197070531ad340301030db8a
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 "qemu/module.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qdict.h"
34 #include "qapi/qmp/qstring.h"
35 #include "sysemu/qtest.h"
37 typedef struct BDRVBlkdebugState {
38 int state;
39 int new_state;
40 uint64_t align;
41 uint64_t max_transfer;
42 uint64_t opt_write_zero;
43 uint64_t max_write_zero;
44 uint64_t opt_discard;
45 uint64_t max_discard;
47 /* For blkdebug_refresh_filename() */
48 char *config_file;
50 QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
51 QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
52 QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
53 } BDRVBlkdebugState;
55 typedef struct BlkdebugAIOCB {
56 BlockAIOCB common;
57 int ret;
58 } BlkdebugAIOCB;
60 typedef struct BlkdebugSuspendedReq {
61 Coroutine *co;
62 char *tag;
63 QLIST_ENTRY(BlkdebugSuspendedReq) next;
64 } BlkdebugSuspendedReq;
66 enum {
67 ACTION_INJECT_ERROR,
68 ACTION_SET_STATE,
69 ACTION_SUSPEND,
72 typedef struct BlkdebugRule {
73 BlkdebugEvent event;
74 int action;
75 int state;
76 union {
77 struct {
78 int error;
79 int immediately;
80 int once;
81 int64_t offset;
82 } inject;
83 struct {
84 int new_state;
85 } set_state;
86 struct {
87 char *tag;
88 } suspend;
89 } options;
90 QLIST_ENTRY(BlkdebugRule) next;
91 QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
92 } BlkdebugRule;
94 static QemuOptsList inject_error_opts = {
95 .name = "inject-error",
96 .head = QTAILQ_HEAD_INITIALIZER(inject_error_opts.head),
97 .desc = {
99 .name = "event",
100 .type = QEMU_OPT_STRING,
103 .name = "state",
104 .type = QEMU_OPT_NUMBER,
107 .name = "errno",
108 .type = QEMU_OPT_NUMBER,
111 .name = "sector",
112 .type = QEMU_OPT_NUMBER,
115 .name = "once",
116 .type = QEMU_OPT_BOOL,
119 .name = "immediately",
120 .type = QEMU_OPT_BOOL,
122 { /* end of list */ }
126 static QemuOptsList set_state_opts = {
127 .name = "set-state",
128 .head = QTAILQ_HEAD_INITIALIZER(set_state_opts.head),
129 .desc = {
131 .name = "event",
132 .type = QEMU_OPT_STRING,
135 .name = "state",
136 .type = QEMU_OPT_NUMBER,
139 .name = "new_state",
140 .type = QEMU_OPT_NUMBER,
142 { /* end of list */ }
146 static QemuOptsList *config_groups[] = {
147 &inject_error_opts,
148 &set_state_opts,
149 NULL
152 struct add_rule_data {
153 BDRVBlkdebugState *s;
154 int action;
157 static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
159 struct add_rule_data *d = opaque;
160 BDRVBlkdebugState *s = d->s;
161 const char* event_name;
162 int event;
163 struct BlkdebugRule *rule;
164 int64_t sector;
166 /* Find the right event for the rule */
167 event_name = qemu_opt_get(opts, "event");
168 if (!event_name) {
169 error_setg(errp, "Missing event name for rule");
170 return -1;
172 event = qapi_enum_parse(&BlkdebugEvent_lookup, event_name, -1, errp);
173 if (event < 0) {
174 return -1;
177 /* Set attributes common for all actions */
178 rule = g_malloc0(sizeof(*rule));
179 *rule = (struct BlkdebugRule) {
180 .event = event,
181 .action = d->action,
182 .state = qemu_opt_get_number(opts, "state", 0),
185 /* Parse action-specific options */
186 switch (d->action) {
187 case ACTION_INJECT_ERROR:
188 rule->options.inject.error = qemu_opt_get_number(opts, "errno", EIO);
189 rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
190 rule->options.inject.immediately =
191 qemu_opt_get_bool(opts, "immediately", 0);
192 sector = qemu_opt_get_number(opts, "sector", -1);
193 rule->options.inject.offset =
194 sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
195 break;
197 case ACTION_SET_STATE:
198 rule->options.set_state.new_state =
199 qemu_opt_get_number(opts, "new_state", 0);
200 break;
202 case ACTION_SUSPEND:
203 rule->options.suspend.tag =
204 g_strdup(qemu_opt_get(opts, "tag"));
205 break;
208 /* Add the rule */
209 QLIST_INSERT_HEAD(&s->rules[event], rule, next);
211 return 0;
214 static void remove_rule(BlkdebugRule *rule)
216 switch (rule->action) {
217 case ACTION_INJECT_ERROR:
218 case ACTION_SET_STATE:
219 break;
220 case ACTION_SUSPEND:
221 g_free(rule->options.suspend.tag);
222 break;
225 QLIST_REMOVE(rule, next);
226 g_free(rule);
229 static int read_config(BDRVBlkdebugState *s, const char *filename,
230 QDict *options, Error **errp)
232 FILE *f = NULL;
233 int ret;
234 struct add_rule_data d;
235 Error *local_err = NULL;
237 if (filename) {
238 f = fopen(filename, "r");
239 if (f == NULL) {
240 error_setg_errno(errp, errno, "Could not read blkdebug config file");
241 return -errno;
244 ret = qemu_config_parse(f, config_groups, filename);
245 if (ret < 0) {
246 error_setg(errp, "Could not parse blkdebug config file");
247 ret = -EINVAL;
248 goto fail;
252 qemu_config_parse_qdict(options, config_groups, &local_err);
253 if (local_err) {
254 error_propagate(errp, local_err);
255 ret = -EINVAL;
256 goto fail;
259 d.s = s;
260 d.action = ACTION_INJECT_ERROR;
261 qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
262 if (local_err) {
263 error_propagate(errp, local_err);
264 ret = -EINVAL;
265 goto fail;
268 d.action = ACTION_SET_STATE;
269 qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
270 if (local_err) {
271 error_propagate(errp, local_err);
272 ret = -EINVAL;
273 goto fail;
276 ret = 0;
277 fail:
278 qemu_opts_reset(&inject_error_opts);
279 qemu_opts_reset(&set_state_opts);
280 if (f) {
281 fclose(f);
283 return ret;
286 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
287 static void blkdebug_parse_filename(const char *filename, QDict *options,
288 Error **errp)
290 const char *c;
292 /* Parse the blkdebug: prefix */
293 if (!strstart(filename, "blkdebug:", &filename)) {
294 /* There was no prefix; therefore, all options have to be already
295 present in the QDict (except for the filename) */
296 qdict_put_str(options, "x-image", filename);
297 return;
300 /* Parse config file path */
301 c = strchr(filename, ':');
302 if (c == NULL) {
303 error_setg(errp, "blkdebug requires both config file and image path");
304 return;
307 if (c != filename) {
308 QString *config_path;
309 config_path = qstring_from_substr(filename, 0, c - filename - 1);
310 qdict_put(options, "config", config_path);
313 /* TODO Allow multi-level nesting and set file.filename here */
314 filename = c + 1;
315 qdict_put_str(options, "x-image", filename);
318 static QemuOptsList runtime_opts = {
319 .name = "blkdebug",
320 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
321 .desc = {
323 .name = "config",
324 .type = QEMU_OPT_STRING,
325 .help = "Path to the configuration file",
328 .name = "x-image",
329 .type = QEMU_OPT_STRING,
330 .help = "[internal use only, will be removed]",
333 .name = "align",
334 .type = QEMU_OPT_SIZE,
335 .help = "Required alignment in bytes",
338 .name = "max-transfer",
339 .type = QEMU_OPT_SIZE,
340 .help = "Maximum transfer size in bytes",
343 .name = "opt-write-zero",
344 .type = QEMU_OPT_SIZE,
345 .help = "Optimum write zero alignment in bytes",
348 .name = "max-write-zero",
349 .type = QEMU_OPT_SIZE,
350 .help = "Maximum write zero size in bytes",
353 .name = "opt-discard",
354 .type = QEMU_OPT_SIZE,
355 .help = "Optimum discard alignment in bytes",
358 .name = "max-discard",
359 .type = QEMU_OPT_SIZE,
360 .help = "Maximum discard size in bytes",
362 { /* end of list */ }
366 static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
367 Error **errp)
369 BDRVBlkdebugState *s = bs->opaque;
370 QemuOpts *opts;
371 Error *local_err = NULL;
372 int ret;
373 uint64_t align;
375 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
376 qemu_opts_absorb_qdict(opts, options, &local_err);
377 if (local_err) {
378 error_propagate(errp, local_err);
379 ret = -EINVAL;
380 goto out;
383 /* Read rules from config file or command line options */
384 s->config_file = g_strdup(qemu_opt_get(opts, "config"));
385 ret = read_config(s, s->config_file, options, errp);
386 if (ret) {
387 goto out;
390 /* Set initial state */
391 s->state = 1;
393 /* Open the image file */
394 bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
395 bs, &child_file, false, &local_err);
396 if (local_err) {
397 ret = -EINVAL;
398 error_propagate(errp, local_err);
399 goto out;
402 bs->supported_write_flags = BDRV_REQ_FUA &
403 bs->file->bs->supported_write_flags;
404 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
405 bs->file->bs->supported_zero_flags;
406 ret = -EINVAL;
408 /* Set alignment overrides */
409 s->align = qemu_opt_get_size(opts, "align", 0);
410 if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
411 error_setg(errp, "Cannot meet constraints with align %" PRIu64,
412 s->align);
413 goto out;
415 align = MAX(s->align, bs->file->bs->bl.request_alignment);
417 s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
418 if (s->max_transfer &&
419 (s->max_transfer >= INT_MAX ||
420 !QEMU_IS_ALIGNED(s->max_transfer, align))) {
421 error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
422 s->max_transfer);
423 goto out;
426 s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
427 if (s->opt_write_zero &&
428 (s->opt_write_zero >= INT_MAX ||
429 !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
430 error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
431 s->opt_write_zero);
432 goto out;
435 s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
436 if (s->max_write_zero &&
437 (s->max_write_zero >= INT_MAX ||
438 !QEMU_IS_ALIGNED(s->max_write_zero,
439 MAX(s->opt_write_zero, align)))) {
440 error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
441 s->max_write_zero);
442 goto out;
445 s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
446 if (s->opt_discard &&
447 (s->opt_discard >= INT_MAX ||
448 !QEMU_IS_ALIGNED(s->opt_discard, align))) {
449 error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
450 s->opt_discard);
451 goto out;
454 s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
455 if (s->max_discard &&
456 (s->max_discard >= INT_MAX ||
457 !QEMU_IS_ALIGNED(s->max_discard,
458 MAX(s->opt_discard, align)))) {
459 error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
460 s->max_discard);
461 goto out;
464 ret = 0;
465 out:
466 if (ret < 0) {
467 g_free(s->config_file);
469 qemu_opts_del(opts);
470 return ret;
473 static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
475 BDRVBlkdebugState *s = bs->opaque;
476 BlkdebugRule *rule = NULL;
477 int error;
478 bool immediately;
480 QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
481 uint64_t inject_offset = rule->options.inject.offset;
483 if (inject_offset == -1 ||
484 (bytes && inject_offset >= offset &&
485 inject_offset < offset + bytes))
487 break;
491 if (!rule || !rule->options.inject.error) {
492 return 0;
495 immediately = rule->options.inject.immediately;
496 error = rule->options.inject.error;
498 if (rule->options.inject.once) {
499 QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
500 remove_rule(rule);
503 if (!immediately) {
504 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
505 qemu_coroutine_yield();
508 return -error;
511 static int coroutine_fn
512 blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
513 QEMUIOVector *qiov, int flags)
515 int err;
517 /* Sanity check block layer guarantees */
518 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
519 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
520 if (bs->bl.max_transfer) {
521 assert(bytes <= bs->bl.max_transfer);
524 err = rule_check(bs, offset, bytes);
525 if (err) {
526 return err;
529 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
532 static int coroutine_fn
533 blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
534 QEMUIOVector *qiov, int flags)
536 int err;
538 /* Sanity check block layer guarantees */
539 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
540 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
541 if (bs->bl.max_transfer) {
542 assert(bytes <= bs->bl.max_transfer);
545 err = rule_check(bs, offset, bytes);
546 if (err) {
547 return err;
550 return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
553 static int blkdebug_co_flush(BlockDriverState *bs)
555 int err = rule_check(bs, 0, 0);
557 if (err) {
558 return err;
561 return bdrv_co_flush(bs->file->bs);
564 static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
565 int64_t offset, int bytes,
566 BdrvRequestFlags flags)
568 uint32_t align = MAX(bs->bl.request_alignment,
569 bs->bl.pwrite_zeroes_alignment);
570 int err;
572 /* Only pass through requests that are larger than requested
573 * preferred alignment (so that we test the fallback to writes on
574 * unaligned portions), and check that the block layer never hands
575 * us anything unaligned that crosses an alignment boundary. */
576 if (bytes < align) {
577 assert(QEMU_IS_ALIGNED(offset, align) ||
578 QEMU_IS_ALIGNED(offset + bytes, align) ||
579 DIV_ROUND_UP(offset, align) ==
580 DIV_ROUND_UP(offset + bytes, align));
581 return -ENOTSUP;
583 assert(QEMU_IS_ALIGNED(offset, align));
584 assert(QEMU_IS_ALIGNED(bytes, align));
585 if (bs->bl.max_pwrite_zeroes) {
586 assert(bytes <= bs->bl.max_pwrite_zeroes);
589 err = rule_check(bs, offset, bytes);
590 if (err) {
591 return err;
594 return bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
597 static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
598 int64_t offset, int bytes)
600 uint32_t align = bs->bl.pdiscard_alignment;
601 int err;
603 /* Only pass through requests that are larger than requested
604 * minimum alignment, and ensure that unaligned requests do not
605 * cross optimum discard boundaries. */
606 if (bytes < bs->bl.request_alignment) {
607 assert(QEMU_IS_ALIGNED(offset, align) ||
608 QEMU_IS_ALIGNED(offset + bytes, align) ||
609 DIV_ROUND_UP(offset, align) ==
610 DIV_ROUND_UP(offset + bytes, align));
611 return -ENOTSUP;
613 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
614 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
615 if (align && bytes >= align) {
616 assert(QEMU_IS_ALIGNED(offset, align));
617 assert(QEMU_IS_ALIGNED(bytes, align));
619 if (bs->bl.max_pdiscard) {
620 assert(bytes <= bs->bl.max_pdiscard);
623 err = rule_check(bs, offset, bytes);
624 if (err) {
625 return err;
628 return bdrv_co_pdiscard(bs->file->bs, offset, bytes);
631 static int64_t coroutine_fn blkdebug_co_get_block_status(
632 BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum,
633 BlockDriverState **file)
635 *pnum = nb_sectors;
636 *file = bs->file->bs;
637 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID |
638 (sector_num << BDRV_SECTOR_BITS);
641 static void blkdebug_close(BlockDriverState *bs)
643 BDRVBlkdebugState *s = bs->opaque;
644 BlkdebugRule *rule, *next;
645 int i;
647 for (i = 0; i < BLKDBG__MAX; i++) {
648 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
649 remove_rule(rule);
653 g_free(s->config_file);
656 static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
658 BDRVBlkdebugState *s = bs->opaque;
659 BlkdebugSuspendedReq r;
661 r = (BlkdebugSuspendedReq) {
662 .co = qemu_coroutine_self(),
663 .tag = g_strdup(rule->options.suspend.tag),
666 remove_rule(rule);
667 QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
669 if (!qtest_enabled()) {
670 printf("blkdebug: Suspended request '%s'\n", r.tag);
672 qemu_coroutine_yield();
673 if (!qtest_enabled()) {
674 printf("blkdebug: Resuming request '%s'\n", r.tag);
677 QLIST_REMOVE(&r, next);
678 g_free(r.tag);
681 static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
682 bool injected)
684 BDRVBlkdebugState *s = bs->opaque;
686 /* Only process rules for the current state */
687 if (rule->state && rule->state != s->state) {
688 return injected;
691 /* Take the action */
692 switch (rule->action) {
693 case ACTION_INJECT_ERROR:
694 if (!injected) {
695 QSIMPLEQ_INIT(&s->active_rules);
696 injected = true;
698 QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
699 break;
701 case ACTION_SET_STATE:
702 s->new_state = rule->options.set_state.new_state;
703 break;
705 case ACTION_SUSPEND:
706 suspend_request(bs, rule);
707 break;
709 return injected;
712 static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
714 BDRVBlkdebugState *s = bs->opaque;
715 struct BlkdebugRule *rule, *next;
716 bool injected;
718 assert((int)event >= 0 && event < BLKDBG__MAX);
720 injected = false;
721 s->new_state = s->state;
722 QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
723 injected = process_rule(bs, rule, injected);
725 s->state = s->new_state;
728 static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
729 const char *tag)
731 BDRVBlkdebugState *s = bs->opaque;
732 struct BlkdebugRule *rule;
733 int blkdebug_event;
735 blkdebug_event = qapi_enum_parse(&BlkdebugEvent_lookup, event, -1, NULL);
736 if (blkdebug_event < 0) {
737 return -ENOENT;
740 rule = g_malloc(sizeof(*rule));
741 *rule = (struct BlkdebugRule) {
742 .event = blkdebug_event,
743 .action = ACTION_SUSPEND,
744 .state = 0,
745 .options.suspend.tag = g_strdup(tag),
748 QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
750 return 0;
753 static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
755 BDRVBlkdebugState *s = bs->opaque;
756 BlkdebugSuspendedReq *r, *next;
758 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
759 if (!strcmp(r->tag, tag)) {
760 qemu_coroutine_enter(r->co);
761 return 0;
764 return -ENOENT;
767 static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
768 const char *tag)
770 BDRVBlkdebugState *s = bs->opaque;
771 BlkdebugSuspendedReq *r, *r_next;
772 BlkdebugRule *rule, *next;
773 int i, ret = -ENOENT;
775 for (i = 0; i < BLKDBG__MAX; i++) {
776 QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
777 if (rule->action == ACTION_SUSPEND &&
778 !strcmp(rule->options.suspend.tag, tag)) {
779 remove_rule(rule);
780 ret = 0;
784 QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
785 if (!strcmp(r->tag, tag)) {
786 qemu_coroutine_enter(r->co);
787 ret = 0;
790 return ret;
793 static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
795 BDRVBlkdebugState *s = bs->opaque;
796 BlkdebugSuspendedReq *r;
798 QLIST_FOREACH(r, &s->suspended_reqs, next) {
799 if (!strcmp(r->tag, tag)) {
800 return true;
803 return false;
806 static int64_t blkdebug_getlength(BlockDriverState *bs)
808 return bdrv_getlength(bs->file->bs);
811 static int blkdebug_truncate(BlockDriverState *bs, int64_t offset,
812 PreallocMode prealloc, Error **errp)
814 return bdrv_truncate(bs->file, offset, prealloc, errp);
817 static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
819 BDRVBlkdebugState *s = bs->opaque;
820 QDict *opts;
821 const QDictEntry *e;
822 bool force_json = false;
824 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
825 if (strcmp(qdict_entry_key(e), "config") &&
826 strcmp(qdict_entry_key(e), "x-image"))
828 force_json = true;
829 break;
833 if (force_json && !bs->file->bs->full_open_options) {
834 /* The config file cannot be recreated, so creating a plain filename
835 * is impossible */
836 return;
839 if (!force_json && bs->file->bs->exact_filename[0]) {
840 int ret = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
841 "blkdebug:%s:%s", s->config_file ?: "",
842 bs->file->bs->exact_filename);
843 if (ret >= sizeof(bs->exact_filename)) {
844 /* An overflow makes the filename unusable, so do not report any */
845 bs->exact_filename[0] = 0;
849 opts = qdict_new();
850 qdict_put_str(opts, "driver", "blkdebug");
852 QINCREF(bs->file->bs->full_open_options);
853 qdict_put(opts, "image", bs->file->bs->full_open_options);
855 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
856 if (strcmp(qdict_entry_key(e), "x-image")) {
857 qobject_incref(qdict_entry_value(e));
858 qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
862 bs->full_open_options = opts;
865 static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
867 BDRVBlkdebugState *s = bs->opaque;
869 if (s->align) {
870 bs->bl.request_alignment = s->align;
872 if (s->max_transfer) {
873 bs->bl.max_transfer = s->max_transfer;
875 if (s->opt_write_zero) {
876 bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
878 if (s->max_write_zero) {
879 bs->bl.max_pwrite_zeroes = s->max_write_zero;
881 if (s->opt_discard) {
882 bs->bl.pdiscard_alignment = s->opt_discard;
884 if (s->max_discard) {
885 bs->bl.max_pdiscard = s->max_discard;
889 static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
890 BlockReopenQueue *queue, Error **errp)
892 return 0;
895 static BlockDriver bdrv_blkdebug = {
896 .format_name = "blkdebug",
897 .protocol_name = "blkdebug",
898 .instance_size = sizeof(BDRVBlkdebugState),
900 .bdrv_parse_filename = blkdebug_parse_filename,
901 .bdrv_file_open = blkdebug_open,
902 .bdrv_close = blkdebug_close,
903 .bdrv_reopen_prepare = blkdebug_reopen_prepare,
904 .bdrv_child_perm = bdrv_filter_default_perms,
906 .bdrv_getlength = blkdebug_getlength,
907 .bdrv_truncate = blkdebug_truncate,
908 .bdrv_refresh_filename = blkdebug_refresh_filename,
909 .bdrv_refresh_limits = blkdebug_refresh_limits,
911 .bdrv_co_preadv = blkdebug_co_preadv,
912 .bdrv_co_pwritev = blkdebug_co_pwritev,
913 .bdrv_co_flush_to_disk = blkdebug_co_flush,
914 .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes,
915 .bdrv_co_pdiscard = blkdebug_co_pdiscard,
916 .bdrv_co_get_block_status = blkdebug_co_get_block_status,
918 .bdrv_debug_event = blkdebug_debug_event,
919 .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
920 .bdrv_debug_remove_breakpoint
921 = blkdebug_debug_remove_breakpoint,
922 .bdrv_debug_resume = blkdebug_debug_resume,
923 .bdrv_debug_is_suspended = blkdebug_debug_is_suspended,
926 static void bdrv_blkdebug_init(void)
928 bdrv_register(&bdrv_blkdebug);
931 block_init(bdrv_blkdebug_init);