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
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
{
45 uint64_t max_transfer
;
46 uint64_t opt_write_zero
;
47 uint64_t max_write_zero
;
51 uint64_t take_child_perms
;
52 uint64_t unshare_child_perms
;
54 /* For blkdebug_refresh_filename() */
57 QLIST_HEAD(, BlkdebugRule
) rules
[BLKDBG__MAX
];
58 QSIMPLEQ_HEAD(, BlkdebugRule
) active_rules
;
59 QLIST_HEAD(, BlkdebugSuspendedReq
) suspended_reqs
;
62 typedef struct BlkdebugAIOCB
{
67 typedef struct BlkdebugSuspendedReq
{
70 QLIST_ENTRY(BlkdebugSuspendedReq
) next
;
71 } BlkdebugSuspendedReq
;
79 typedef struct BlkdebugRule
{
98 QLIST_ENTRY(BlkdebugRule
) next
;
99 QSIMPLEQ_ENTRY(BlkdebugRule
) active_next
;
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
),
111 .type
= QEMU_OPT_STRING
,
115 .type
= QEMU_OPT_NUMBER
,
119 .type
= QEMU_OPT_STRING
,
123 .type
= QEMU_OPT_NUMBER
,
127 .type
= QEMU_OPT_NUMBER
,
131 .type
= QEMU_OPT_BOOL
,
134 .name
= "immediately",
135 .type
= QEMU_OPT_BOOL
,
137 { /* end of list */ }
141 static QemuOptsList set_state_opts
= {
143 .head
= QTAILQ_HEAD_INITIALIZER(set_state_opts
.head
),
147 .type
= QEMU_OPT_STRING
,
151 .type
= QEMU_OPT_NUMBER
,
155 .type
= QEMU_OPT_NUMBER
,
157 { /* end of list */ }
161 static QemuOptsList
*config_groups
[] = {
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
;
180 BlkdebugIOType iotype
;
181 Error
*local_error
= NULL
;
183 /* Find the right event for the rule */
184 event_name
= qemu_opt_get(opts
, "event");
186 error_setg(errp
, "Missing event name for rule");
189 event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event_name
, -1, errp
);
194 /* Set attributes common for all actions */
195 rule
= g_malloc0(sizeof(*rule
));
196 *rule
= (struct BlkdebugRule
) {
199 .state
= qemu_opt_get_number(opts
, "state", 0),
202 /* Parse action-specific options */
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
);
217 error_propagate(errp
, local_error
);
221 if (iotype
!= BLKDEBUG_IO_TYPE__MAX
) {
222 rule
->options
.inject
.iotype_mask
= (1ull << iotype
);
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
);
235 case ACTION_SET_STATE
:
236 rule
->options
.set_state
.new_state
=
237 qemu_opt_get_number(opts
, "new_state", 0);
241 rule
->options
.suspend
.tag
=
242 g_strdup(qemu_opt_get(opts
, "tag"));
247 QLIST_INSERT_HEAD(&s
->rules
[event
], rule
, next
);
252 static void remove_rule(BlkdebugRule
*rule
)
254 switch (rule
->action
) {
255 case ACTION_INJECT_ERROR
:
256 case ACTION_SET_STATE
:
259 g_free(rule
->options
.suspend
.tag
);
263 QLIST_REMOVE(rule
, next
);
267 static int read_config(BDRVBlkdebugState
*s
, const char *filename
,
268 QDict
*options
, Error
**errp
)
272 struct add_rule_data d
;
273 Error
*local_err
= NULL
;
276 f
= fopen(filename
, "r");
278 error_setg_errno(errp
, errno
, "Could not read blkdebug config file");
282 ret
= qemu_config_parse(f
, config_groups
, filename
);
284 error_setg(errp
, "Could not parse blkdebug config file");
289 qemu_config_parse_qdict(options
, config_groups
, &local_err
);
291 error_propagate(errp
, local_err
);
297 d
.action
= ACTION_INJECT_ERROR
;
298 qemu_opts_foreach(&inject_error_opts
, add_rule
, &d
, &local_err
);
300 error_propagate(errp
, local_err
);
305 d
.action
= ACTION_SET_STATE
;
306 qemu_opts_foreach(&set_state_opts
, add_rule
, &d
, &local_err
);
308 error_propagate(errp
, local_err
);
315 qemu_opts_reset(&inject_error_opts
);
316 qemu_opts_reset(&set_state_opts
);
323 /* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
324 static void blkdebug_parse_filename(const char *filename
, QDict
*options
,
329 /* Parse the blkdebug: prefix */
330 if (!strstart(filename
, "blkdebug:", &filename
)) {
331 /* There was no prefix; therefore, all options have to be already
332 present in the QDict (except for the filename) */
333 qdict_put_str(options
, "x-image", filename
);
337 /* Parse config file path */
338 c
= strchr(filename
, ':');
340 error_setg(errp
, "blkdebug requires both config file and image path");
345 QString
*config_path
;
346 config_path
= qstring_from_substr(filename
, 0, c
- filename
);
347 qdict_put(options
, "config", config_path
);
350 /* TODO Allow multi-level nesting and set file.filename here */
352 qdict_put_str(options
, "x-image", filename
);
355 static int blkdebug_parse_perm_list(uint64_t *dest
, QDict
*options
,
356 const char *prefix
, Error
**errp
)
359 QDict
*subqdict
= NULL
;
360 QObject
*crumpled_subqdict
= NULL
;
362 BlockPermissionList
*perm_list
= NULL
, *element
;
366 qdict_extract_subqdict(options
, &subqdict
, prefix
);
367 if (!qdict_size(subqdict
)) {
371 crumpled_subqdict
= qdict_crumple(subqdict
, errp
);
372 if (!crumpled_subqdict
) {
377 v
= qobject_input_visitor_new(crumpled_subqdict
);
378 if (!visit_type_BlockPermissionList(v
, NULL
, &perm_list
, errp
)) {
383 for (element
= perm_list
; element
; element
= element
->next
) {
384 *dest
|= bdrv_qapi_perm_to_blk_perm(element
->value
);
388 qapi_free_BlockPermissionList(perm_list
);
390 qobject_unref(subqdict
);
391 qobject_unref(crumpled_subqdict
);
395 static int blkdebug_parse_perms(BDRVBlkdebugState
*s
, QDict
*options
,
400 ret
= blkdebug_parse_perm_list(&s
->take_child_perms
, options
,
401 "take-child-perms.", errp
);
406 ret
= blkdebug_parse_perm_list(&s
->unshare_child_perms
, options
,
407 "unshare-child-perms.", errp
);
415 static QemuOptsList runtime_opts
= {
417 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
421 .type
= QEMU_OPT_STRING
,
422 .help
= "Path to the configuration file",
426 .type
= QEMU_OPT_STRING
,
427 .help
= "[internal use only, will be removed]",
431 .type
= QEMU_OPT_SIZE
,
432 .help
= "Required alignment in bytes",
435 .name
= "max-transfer",
436 .type
= QEMU_OPT_SIZE
,
437 .help
= "Maximum transfer size in bytes",
440 .name
= "opt-write-zero",
441 .type
= QEMU_OPT_SIZE
,
442 .help
= "Optimum write zero alignment in bytes",
445 .name
= "max-write-zero",
446 .type
= QEMU_OPT_SIZE
,
447 .help
= "Maximum write zero size in bytes",
450 .name
= "opt-discard",
451 .type
= QEMU_OPT_SIZE
,
452 .help
= "Optimum discard alignment in bytes",
455 .name
= "max-discard",
456 .type
= QEMU_OPT_SIZE
,
457 .help
= "Maximum discard size in bytes",
459 { /* end of list */ }
463 static int blkdebug_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
466 BDRVBlkdebugState
*s
= bs
->opaque
;
468 Error
*local_err
= NULL
;
472 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
473 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
478 /* Read rules from config file or command line options */
479 s
->config_file
= g_strdup(qemu_opt_get(opts
, "config"));
480 ret
= read_config(s
, s
->config_file
, options
, errp
);
485 /* Set initial state */
488 /* Parse permissions modifiers before opening the image file */
489 ret
= blkdebug_parse_perms(s
, options
, errp
);
494 /* Open the image file */
495 bs
->file
= bdrv_open_child(qemu_opt_get(opts
, "x-image"), options
, "image",
497 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
501 error_propagate(errp
, local_err
);
505 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
|
506 (BDRV_REQ_FUA
& bs
->file
->bs
->supported_write_flags
);
507 bs
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
|
508 ((BDRV_REQ_FUA
| BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
) &
509 bs
->file
->bs
->supported_zero_flags
);
512 /* Set alignment overrides */
513 s
->align
= qemu_opt_get_size(opts
, "align", 0);
514 if (s
->align
&& (s
->align
>= INT_MAX
|| !is_power_of_2(s
->align
))) {
515 error_setg(errp
, "Cannot meet constraints with align %" PRIu64
,
519 align
= MAX(s
->align
, bs
->file
->bs
->bl
.request_alignment
);
521 s
->max_transfer
= qemu_opt_get_size(opts
, "max-transfer", 0);
522 if (s
->max_transfer
&&
523 (s
->max_transfer
>= INT_MAX
||
524 !QEMU_IS_ALIGNED(s
->max_transfer
, align
))) {
525 error_setg(errp
, "Cannot meet constraints with max-transfer %" PRIu64
,
530 s
->opt_write_zero
= qemu_opt_get_size(opts
, "opt-write-zero", 0);
531 if (s
->opt_write_zero
&&
532 (s
->opt_write_zero
>= INT_MAX
||
533 !QEMU_IS_ALIGNED(s
->opt_write_zero
, align
))) {
534 error_setg(errp
, "Cannot meet constraints with opt-write-zero %" PRIu64
,
539 s
->max_write_zero
= qemu_opt_get_size(opts
, "max-write-zero", 0);
540 if (s
->max_write_zero
&&
541 (s
->max_write_zero
>= INT_MAX
||
542 !QEMU_IS_ALIGNED(s
->max_write_zero
,
543 MAX(s
->opt_write_zero
, align
)))) {
544 error_setg(errp
, "Cannot meet constraints with max-write-zero %" PRIu64
,
549 s
->opt_discard
= qemu_opt_get_size(opts
, "opt-discard", 0);
550 if (s
->opt_discard
&&
551 (s
->opt_discard
>= INT_MAX
||
552 !QEMU_IS_ALIGNED(s
->opt_discard
, align
))) {
553 error_setg(errp
, "Cannot meet constraints with opt-discard %" PRIu64
,
558 s
->max_discard
= qemu_opt_get_size(opts
, "max-discard", 0);
559 if (s
->max_discard
&&
560 (s
->max_discard
>= INT_MAX
||
561 !QEMU_IS_ALIGNED(s
->max_discard
,
562 MAX(s
->opt_discard
, align
)))) {
563 error_setg(errp
, "Cannot meet constraints with max-discard %" PRIu64
,
568 bdrv_debug_event(bs
, BLKDBG_NONE
);
573 g_free(s
->config_file
);
579 static int rule_check(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
580 BlkdebugIOType iotype
)
582 BDRVBlkdebugState
*s
= bs
->opaque
;
583 BlkdebugRule
*rule
= NULL
;
587 QSIMPLEQ_FOREACH(rule
, &s
->active_rules
, active_next
) {
588 uint64_t inject_offset
= rule
->options
.inject
.offset
;
590 if ((inject_offset
== -1 ||
591 (bytes
&& inject_offset
>= offset
&&
592 inject_offset
< offset
+ bytes
)) &&
593 (rule
->options
.inject
.iotype_mask
& (1ull << iotype
)))
599 if (!rule
|| !rule
->options
.inject
.error
) {
603 immediately
= rule
->options
.inject
.immediately
;
604 error
= rule
->options
.inject
.error
;
606 if (rule
->options
.inject
.once
) {
607 QSIMPLEQ_REMOVE(&s
->active_rules
, rule
, BlkdebugRule
, active_next
);
612 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
613 qemu_coroutine_yield();
619 static int coroutine_fn
620 blkdebug_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
621 QEMUIOVector
*qiov
, int flags
)
625 /* Sanity check block layer guarantees */
626 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
627 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
628 if (bs
->bl
.max_transfer
) {
629 assert(bytes
<= bs
->bl
.max_transfer
);
632 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_READ
);
637 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
640 static int coroutine_fn
641 blkdebug_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
642 QEMUIOVector
*qiov
, int flags
)
646 /* Sanity check block layer guarantees */
647 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
648 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
649 if (bs
->bl
.max_transfer
) {
650 assert(bytes
<= bs
->bl
.max_transfer
);
653 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_WRITE
);
658 return bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
661 static int blkdebug_co_flush(BlockDriverState
*bs
)
663 int err
= rule_check(bs
, 0, 0, BLKDEBUG_IO_TYPE_FLUSH
);
669 return bdrv_co_flush(bs
->file
->bs
);
672 static int coroutine_fn
blkdebug_co_pwrite_zeroes(BlockDriverState
*bs
,
673 int64_t offset
, int bytes
,
674 BdrvRequestFlags flags
)
676 uint32_t align
= MAX(bs
->bl
.request_alignment
,
677 bs
->bl
.pwrite_zeroes_alignment
);
680 /* Only pass through requests that are larger than requested
681 * preferred alignment (so that we test the fallback to writes on
682 * unaligned portions), and check that the block layer never hands
683 * us anything unaligned that crosses an alignment boundary. */
685 assert(QEMU_IS_ALIGNED(offset
, align
) ||
686 QEMU_IS_ALIGNED(offset
+ bytes
, align
) ||
687 DIV_ROUND_UP(offset
, align
) ==
688 DIV_ROUND_UP(offset
+ bytes
, align
));
691 assert(QEMU_IS_ALIGNED(offset
, align
));
692 assert(QEMU_IS_ALIGNED(bytes
, align
));
693 if (bs
->bl
.max_pwrite_zeroes
) {
694 assert(bytes
<= bs
->bl
.max_pwrite_zeroes
);
697 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_WRITE_ZEROES
);
702 return bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
705 static int coroutine_fn
blkdebug_co_pdiscard(BlockDriverState
*bs
,
706 int64_t offset
, int bytes
)
708 uint32_t align
= bs
->bl
.pdiscard_alignment
;
711 /* Only pass through requests that are larger than requested
712 * minimum alignment, and ensure that unaligned requests do not
713 * cross optimum discard boundaries. */
714 if (bytes
< bs
->bl
.request_alignment
) {
715 assert(QEMU_IS_ALIGNED(offset
, align
) ||
716 QEMU_IS_ALIGNED(offset
+ bytes
, align
) ||
717 DIV_ROUND_UP(offset
, align
) ==
718 DIV_ROUND_UP(offset
+ bytes
, align
));
721 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
722 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
723 if (align
&& bytes
>= align
) {
724 assert(QEMU_IS_ALIGNED(offset
, align
));
725 assert(QEMU_IS_ALIGNED(bytes
, align
));
727 if (bs
->bl
.max_pdiscard
) {
728 assert(bytes
<= bs
->bl
.max_pdiscard
);
731 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_DISCARD
);
736 return bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
739 static int coroutine_fn
blkdebug_co_block_status(BlockDriverState
*bs
,
745 BlockDriverState
**file
)
749 assert(QEMU_IS_ALIGNED(offset
| bytes
, bs
->bl
.request_alignment
));
751 err
= rule_check(bs
, offset
, bytes
, BLKDEBUG_IO_TYPE_BLOCK_STATUS
);
756 assert(bs
->file
&& bs
->file
->bs
);
759 *file
= bs
->file
->bs
;
760 return BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
;
763 static void blkdebug_close(BlockDriverState
*bs
)
765 BDRVBlkdebugState
*s
= bs
->opaque
;
766 BlkdebugRule
*rule
, *next
;
769 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
770 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
775 g_free(s
->config_file
);
778 static void suspend_request(BlockDriverState
*bs
, BlkdebugRule
*rule
)
780 BDRVBlkdebugState
*s
= bs
->opaque
;
781 BlkdebugSuspendedReq r
;
783 r
= (BlkdebugSuspendedReq
) {
784 .co
= qemu_coroutine_self(),
785 .tag
= g_strdup(rule
->options
.suspend
.tag
),
789 QLIST_INSERT_HEAD(&s
->suspended_reqs
, &r
, next
);
791 if (!qtest_enabled()) {
792 printf("blkdebug: Suspended request '%s'\n", r
.tag
);
794 qemu_coroutine_yield();
795 if (!qtest_enabled()) {
796 printf("blkdebug: Resuming request '%s'\n", r
.tag
);
799 QLIST_REMOVE(&r
, next
);
803 static bool process_rule(BlockDriverState
*bs
, struct BlkdebugRule
*rule
,
806 BDRVBlkdebugState
*s
= bs
->opaque
;
808 /* Only process rules for the current state */
809 if (rule
->state
&& rule
->state
!= s
->state
) {
813 /* Take the action */
814 switch (rule
->action
) {
815 case ACTION_INJECT_ERROR
:
817 QSIMPLEQ_INIT(&s
->active_rules
);
820 QSIMPLEQ_INSERT_HEAD(&s
->active_rules
, rule
, active_next
);
823 case ACTION_SET_STATE
:
824 s
->new_state
= rule
->options
.set_state
.new_state
;
828 suspend_request(bs
, rule
);
834 static void blkdebug_debug_event(BlockDriverState
*bs
, BlkdebugEvent event
)
836 BDRVBlkdebugState
*s
= bs
->opaque
;
837 struct BlkdebugRule
*rule
, *next
;
840 assert((int)event
>= 0 && event
< BLKDBG__MAX
);
843 s
->new_state
= s
->state
;
844 QLIST_FOREACH_SAFE(rule
, &s
->rules
[event
], next
, next
) {
845 injected
= process_rule(bs
, rule
, injected
);
847 s
->state
= s
->new_state
;
850 static int blkdebug_debug_breakpoint(BlockDriverState
*bs
, const char *event
,
853 BDRVBlkdebugState
*s
= bs
->opaque
;
854 struct BlkdebugRule
*rule
;
857 blkdebug_event
= qapi_enum_parse(&BlkdebugEvent_lookup
, event
, -1, NULL
);
858 if (blkdebug_event
< 0) {
862 rule
= g_malloc(sizeof(*rule
));
863 *rule
= (struct BlkdebugRule
) {
864 .event
= blkdebug_event
,
865 .action
= ACTION_SUSPEND
,
867 .options
.suspend
.tag
= g_strdup(tag
),
870 QLIST_INSERT_HEAD(&s
->rules
[blkdebug_event
], rule
, next
);
875 static int blkdebug_debug_resume(BlockDriverState
*bs
, const char *tag
)
877 BDRVBlkdebugState
*s
= bs
->opaque
;
878 BlkdebugSuspendedReq
*r
, *next
;
880 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, next
) {
881 if (!strcmp(r
->tag
, tag
)) {
882 qemu_coroutine_enter(r
->co
);
889 static int blkdebug_debug_remove_breakpoint(BlockDriverState
*bs
,
892 BDRVBlkdebugState
*s
= bs
->opaque
;
893 BlkdebugSuspendedReq
*r
, *r_next
;
894 BlkdebugRule
*rule
, *next
;
895 int i
, ret
= -ENOENT
;
897 for (i
= 0; i
< BLKDBG__MAX
; i
++) {
898 QLIST_FOREACH_SAFE(rule
, &s
->rules
[i
], next
, next
) {
899 if (rule
->action
== ACTION_SUSPEND
&&
900 !strcmp(rule
->options
.suspend
.tag
, tag
)) {
906 QLIST_FOREACH_SAFE(r
, &s
->suspended_reqs
, next
, r_next
) {
907 if (!strcmp(r
->tag
, tag
)) {
908 qemu_coroutine_enter(r
->co
);
915 static bool blkdebug_debug_is_suspended(BlockDriverState
*bs
, const char *tag
)
917 BDRVBlkdebugState
*s
= bs
->opaque
;
918 BlkdebugSuspendedReq
*r
;
920 QLIST_FOREACH(r
, &s
->suspended_reqs
, next
) {
921 if (!strcmp(r
->tag
, tag
)) {
928 static int64_t blkdebug_getlength(BlockDriverState
*bs
)
930 return bdrv_getlength(bs
->file
->bs
);
933 static void blkdebug_refresh_filename(BlockDriverState
*bs
)
935 BDRVBlkdebugState
*s
= bs
->opaque
;
939 if (!bs
->file
->bs
->exact_filename
[0]) {
943 for (e
= qdict_first(bs
->full_open_options
); e
;
944 e
= qdict_next(bs
->full_open_options
, e
))
946 /* Real child options are under "image", but "x-image" may
947 * contain a filename */
948 if (strcmp(qdict_entry_key(e
), "config") &&
949 strcmp(qdict_entry_key(e
), "image") &&
950 strcmp(qdict_entry_key(e
), "x-image") &&
951 strcmp(qdict_entry_key(e
), "driver"))
957 ret
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
959 s
->config_file
?: "", bs
->file
->bs
->exact_filename
);
960 if (ret
>= sizeof(bs
->exact_filename
)) {
961 /* An overflow makes the filename unusable, so do not report any */
962 bs
->exact_filename
[0] = 0;
966 static void blkdebug_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
968 BDRVBlkdebugState
*s
= bs
->opaque
;
971 bs
->bl
.request_alignment
= s
->align
;
973 if (s
->max_transfer
) {
974 bs
->bl
.max_transfer
= s
->max_transfer
;
976 if (s
->opt_write_zero
) {
977 bs
->bl
.pwrite_zeroes_alignment
= s
->opt_write_zero
;
979 if (s
->max_write_zero
) {
980 bs
->bl
.max_pwrite_zeroes
= s
->max_write_zero
;
982 if (s
->opt_discard
) {
983 bs
->bl
.pdiscard_alignment
= s
->opt_discard
;
985 if (s
->max_discard
) {
986 bs
->bl
.max_pdiscard
= s
->max_discard
;
990 static int blkdebug_reopen_prepare(BDRVReopenState
*reopen_state
,
991 BlockReopenQueue
*queue
, Error
**errp
)
996 static void blkdebug_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
998 BlockReopenQueue
*reopen_queue
,
999 uint64_t perm
, uint64_t shared
,
1000 uint64_t *nperm
, uint64_t *nshared
)
1002 BDRVBlkdebugState
*s
= bs
->opaque
;
1004 bdrv_default_perms(bs
, c
, role
, reopen_queue
,
1005 perm
, shared
, nperm
, nshared
);
1007 *nperm
|= s
->take_child_perms
;
1008 *nshared
&= ~s
->unshare_child_perms
;
1011 static const char *const blkdebug_strong_runtime_opts
[] = {
1025 static BlockDriver bdrv_blkdebug
= {
1026 .format_name
= "blkdebug",
1027 .protocol_name
= "blkdebug",
1028 .instance_size
= sizeof(BDRVBlkdebugState
),
1031 .bdrv_parse_filename
= blkdebug_parse_filename
,
1032 .bdrv_file_open
= blkdebug_open
,
1033 .bdrv_close
= blkdebug_close
,
1034 .bdrv_reopen_prepare
= blkdebug_reopen_prepare
,
1035 .bdrv_child_perm
= blkdebug_child_perm
,
1037 .bdrv_getlength
= blkdebug_getlength
,
1038 .bdrv_refresh_filename
= blkdebug_refresh_filename
,
1039 .bdrv_refresh_limits
= blkdebug_refresh_limits
,
1041 .bdrv_co_preadv
= blkdebug_co_preadv
,
1042 .bdrv_co_pwritev
= blkdebug_co_pwritev
,
1043 .bdrv_co_flush_to_disk
= blkdebug_co_flush
,
1044 .bdrv_co_pwrite_zeroes
= blkdebug_co_pwrite_zeroes
,
1045 .bdrv_co_pdiscard
= blkdebug_co_pdiscard
,
1046 .bdrv_co_block_status
= blkdebug_co_block_status
,
1048 .bdrv_debug_event
= blkdebug_debug_event
,
1049 .bdrv_debug_breakpoint
= blkdebug_debug_breakpoint
,
1050 .bdrv_debug_remove_breakpoint
1051 = blkdebug_debug_remove_breakpoint
,
1052 .bdrv_debug_resume
= blkdebug_debug_resume
,
1053 .bdrv_debug_is_suspended
= blkdebug_debug_is_suspended
,
1055 .strong_runtime_opts
= blkdebug_strong_runtime_opts
,
1058 static void bdrv_blkdebug_init(void)
1060 bdrv_register(&bdrv_blkdebug
);
1063 block_init(bdrv_blkdebug_init
);