2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/qdict.h"
39 #include "block/throttle-groups.h"
40 #include "monitor/monitor.h"
41 #include "qemu/error-report.h"
42 #include "qemu/option.h"
43 #include "qemu/qemu-print.h"
44 #include "qemu/config-file.h"
45 #include "qapi/qapi-commands-block.h"
46 #include "qapi/qapi-commands-transaction.h"
47 #include "qapi/qapi-visit-block-core.h"
48 #include "qapi/qmp/qdict.h"
49 #include "qapi/qmp/qnum.h"
50 #include "qapi/qmp/qstring.h"
51 #include "qapi/error.h"
52 #include "qapi/qmp/qerror.h"
53 #include "qapi/qmp/qlist.h"
54 #include "qapi/qobject-output-visitor.h"
55 #include "sysemu/sysemu.h"
56 #include "sysemu/iothread.h"
57 #include "block/block_int.h"
58 #include "block/trace.h"
59 #include "sysemu/arch_init.h"
60 #include "sysemu/qtest.h"
61 #include "sysemu/runstate.h"
62 #include "qemu/cutils.h"
63 #include "qemu/help_option.h"
64 #include "qemu/main-loop.h"
65 #include "qemu/throttle-options.h"
67 QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
68 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
70 void bdrv_set_monitor_owned(BlockDriverState
*bs
)
72 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
75 static const char *const if_name
[IF_COUNT
] = {
79 [IF_FLOPPY
] = "floppy",
80 [IF_PFLASH
] = "pflash",
83 [IF_VIRTIO
] = "virtio",
87 static int if_max_devs
[IF_COUNT
] = {
89 * Do not change these numbers! They govern how drive option
90 * index maps to unit and bus. That mapping is ABI.
92 * All controllers used to implement if=T drives need to support
93 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
94 * Otherwise, some index values map to "impossible" bus, unit
97 * For instance, if you change [IF_SCSI] to 255, -drive
98 * if=scsi,index=12 no longer means bus=1,unit=5, but
99 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
100 * the drive can't be set up. Regression.
107 * Boards may call this to offer board-by-board overrides
108 * of the default, global values.
110 void override_max_devs(BlockInterfaceType type
, int max_devs
)
119 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
120 dinfo
= blk_legacy_dinfo(blk
);
121 if (dinfo
->type
== type
) {
122 fprintf(stderr
, "Cannot override units-per-bus property of"
123 " the %s interface, because a drive of that type has"
124 " already been added.\n", if_name
[type
]);
125 g_assert_not_reached();
129 if_max_devs
[type
] = max_devs
;
133 * We automatically delete the drive when a device using it gets
134 * unplugged. Questionable feature, but we can't just drop it.
135 * Device models call blockdev_mark_auto_del() to schedule the
136 * automatic deletion, and generic qdev code calls blockdev_auto_del()
137 * when deletion is actually safe.
139 void blockdev_mark_auto_del(BlockBackend
*blk
)
141 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
148 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
149 if (block_job_has_bdrv(job
, blk_bs(blk
))) {
150 AioContext
*aio_context
= job
->job
.aio_context
;
151 aio_context_acquire(aio_context
);
153 job_cancel(&job
->job
, false);
155 aio_context_release(aio_context
);
162 void blockdev_auto_del(BlockBackend
*blk
)
164 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
166 if (dinfo
&& dinfo
->auto_del
) {
167 monitor_remove_blk(blk
);
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
180 int drive_get_max_devs(BlockInterfaceType type
)
182 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
183 return if_max_devs
[type
];
189 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
191 int max_devs
= if_max_devs
[type
];
192 return max_devs
? index
/ max_devs
: 0;
195 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
197 int max_devs
= if_max_devs
[type
];
198 return max_devs
? index
% max_devs
: index
;
201 QemuOpts
*drive_def(const char *optstr
)
203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
206 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
211 opts
= drive_def(optstr
);
215 if (type
!= IF_DEFAULT
) {
216 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
219 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
222 qemu_opt_set(opts
, "file", file
, &error_abort
);
226 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
231 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
232 dinfo
= blk_legacy_dinfo(blk
);
233 if (dinfo
&& dinfo
->type
== type
234 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
242 void drive_check_orphaned(void)
247 bool orphans
= false;
249 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
250 dinfo
= blk_legacy_dinfo(blk
);
251 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
252 dinfo
->type
!= IF_NONE
) {
254 qemu_opts_loc_restore(dinfo
->opts
);
255 error_report("machine type does not support"
256 " if=%s,bus=%d,unit=%d",
257 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
268 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
270 return drive_get(type
,
271 drive_index_to_bus_id(type
, index
),
272 drive_index_to_unit_id(type
, index
));
275 int drive_get_max_bus(BlockInterfaceType type
)
282 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
283 dinfo
= blk_legacy_dinfo(blk
);
284 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
285 max_bus
= dinfo
->bus
;
291 /* Get a block device. This should only be used for single-drive devices
292 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
294 DriveInfo
*drive_get_next(BlockInterfaceType type
)
296 static int next_block_unit
[IF_COUNT
];
298 return drive_get(type
, 0, next_block_unit
[type
]++);
301 static void bdrv_format_print(void *opaque
, const char *name
)
303 qemu_printf(" %s", name
);
308 BlockDriverState
*bs
;
311 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
313 if (!strcmp(buf
, "ignore")) {
314 return BLOCKDEV_ON_ERROR_IGNORE
;
315 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
316 return BLOCKDEV_ON_ERROR_ENOSPC
;
317 } else if (!strcmp(buf
, "stop")) {
318 return BLOCKDEV_ON_ERROR_STOP
;
319 } else if (!strcmp(buf
, "report")) {
320 return BLOCKDEV_ON_ERROR_REPORT
;
322 error_setg(errp
, "'%s' invalid %s error action",
323 buf
, is_read
? "read" : "write");
328 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
331 const QListEntry
*entry
;
332 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
333 switch (qobject_type(entry
->value
)) {
335 case QTYPE_QSTRING
: {
336 unsigned long long length
;
337 const char *str
= qstring_get_str(qobject_to(QString
,
339 if (parse_uint_full(str
, &length
, 10) == 0 &&
340 length
> 0 && length
<= UINT_MAX
) {
341 block_acct_add_interval(stats
, (unsigned) length
);
343 error_setg(errp
, "Invalid interval length: %s", str
);
350 int64_t length
= qnum_get_int(qobject_to(QNum
, entry
->value
));
352 if (length
> 0 && length
<= UINT_MAX
) {
353 block_acct_add_interval(stats
, (unsigned) length
);
355 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
362 error_setg(errp
, "The specification of stats-intervals is invalid");
369 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
371 /* All parameters but @opts are optional and may be set to NULL. */
372 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
373 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
374 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
376 Error
*local_error
= NULL
;
380 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
381 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
384 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
385 if (bdrv_parse_aio(aio
, bdrv_flags
) < 0) {
386 error_setg(errp
, "invalid aio option");
392 /* disk I/O throttling */
393 if (throttling_group
) {
394 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
398 throttle_config_init(throttle_cfg
);
399 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
400 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
401 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
402 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
403 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
404 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
405 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
406 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
407 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
408 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
409 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
410 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
412 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
413 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
414 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
415 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
416 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
417 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
419 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
420 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
421 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
422 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
423 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
425 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
426 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
427 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
428 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
429 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
430 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
431 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
432 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
433 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
434 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
435 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
436 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
438 throttle_cfg
->op_size
=
439 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
441 if (!throttle_is_valid(throttle_cfg
, errp
)) {
448 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
449 qemu_opt_get(opts
, "detect-zeroes"),
450 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
453 error_propagate(errp
, local_error
);
459 /* Takes the ownership of bs_opts */
460 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
465 int on_read_error
, on_write_error
;
466 bool account_invalid
, account_failed
;
467 bool writethrough
, read_only
;
469 BlockDriverState
*bs
;
474 QDict
*interval_dict
= NULL
;
475 QList
*interval_list
= NULL
;
477 BlockdevDetectZeroesOptions detect_zeroes
=
478 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
479 const char *throttling_group
= NULL
;
481 /* Check common options by copying from bs_opts to opts, all other options
482 * stay in bs_opts for processing by bdrv_open(). */
483 id
= qdict_get_try_str(bs_opts
, "id");
484 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
486 error_propagate(errp
, error
);
490 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
492 error_propagate(errp
, error
);
497 qdict_del(bs_opts
, "id");
500 /* extract parameters */
501 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
503 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
504 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
506 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
508 id
= qemu_opts_id(opts
);
510 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
511 qdict_array_split(interval_dict
, &interval_list
);
513 if (qdict_size(interval_dict
) != 0) {
514 error_setg(errp
, "Invalid option stats-intervals.%s",
515 qdict_first(interval_dict
)->key
);
519 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
520 &detect_zeroes
, &error
);
522 error_propagate(errp
, error
);
526 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
527 if (is_help_option(buf
)) {
528 qemu_printf("Supported formats:");
529 bdrv_iterate_format(bdrv_format_print
, NULL
, false);
530 qemu_printf("\nSupported formats (read-only):");
531 bdrv_iterate_format(bdrv_format_print
, NULL
, true);
536 if (qdict_haskey(bs_opts
, "driver")) {
537 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
540 qdict_put_str(bs_opts
, "driver", buf
);
543 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
544 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
545 on_write_error
= parse_block_error_action(buf
, 0, &error
);
547 error_propagate(errp
, error
);
552 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
553 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
554 on_read_error
= parse_block_error_action(buf
, 1, &error
);
556 error_propagate(errp
, error
);
562 bdrv_flags
|= BDRV_O_SNAPSHOT
;
565 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
568 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
569 BlockBackendRootState
*blk_rs
;
571 blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
572 blk_rs
= blk_get_root_state(blk
);
573 blk_rs
->open_flags
= bdrv_flags
;
574 blk_rs
->read_only
= read_only
;
575 blk_rs
->detect_zeroes
= detect_zeroes
;
577 qobject_unref(bs_opts
);
579 if (file
&& !*file
) {
583 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
584 * with other callers) rather than what we want as the real defaults.
585 * Apply the defaults here instead. */
586 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
587 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
588 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
589 read_only
? "on" : "off");
590 qdict_set_default_str(bs_opts
, BDRV_OPT_AUTO_READ_ONLY
, "on");
591 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
593 if (runstate_check(RUN_STATE_INMIGRATE
)) {
594 bdrv_flags
|= BDRV_O_INACTIVE
;
597 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
603 bs
->detect_zeroes
= detect_zeroes
;
605 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
607 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
614 /* disk I/O throttling */
615 if (throttle_enabled(&cfg
)) {
616 if (!throttling_group
) {
617 throttling_group
= id
;
619 blk_io_limits_enable(blk
, throttling_group
);
620 blk_set_io_limits(blk
, &cfg
);
623 blk_set_enable_write_cache(blk
, !writethrough
);
624 blk_set_on_error(blk
, on_read_error
, on_write_error
);
626 if (!monitor_add_blk(blk
, id
, errp
)) {
634 qobject_unref(interval_dict
);
635 qobject_unref(interval_list
);
640 qobject_unref(interval_dict
);
641 qobject_unref(interval_list
);
643 qobject_unref(bs_opts
);
647 /* Takes the ownership of bs_opts */
648 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
652 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
653 * with other callers) rather than what we want as the real defaults.
654 * Apply the defaults here instead. */
655 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
656 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
657 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
659 if (runstate_check(RUN_STATE_INMIGRATE
)) {
660 bdrv_flags
|= BDRV_O_INACTIVE
;
663 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
666 void blockdev_close_all_bdrv_states(void)
668 BlockDriverState
*bs
, *next_bs
;
670 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
671 AioContext
*ctx
= bdrv_get_aio_context(bs
);
673 aio_context_acquire(ctx
);
675 aio_context_release(ctx
);
679 /* Iterates over the list of monitor-owned BlockDriverStates */
680 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
682 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
683 : QTAILQ_FIRST(&monitor_bdrv_states
);
686 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
691 value
= qemu_opt_get(opts
, from
);
693 if (qemu_opt_find(opts
, to
)) {
694 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
695 "same time", to
, from
);
700 /* rename all items in opts */
701 while ((value
= qemu_opt_get(opts
, from
))) {
702 qemu_opt_set(opts
, to
, value
, &error_abort
);
703 qemu_opt_unset(opts
, from
);
707 QemuOptsList qemu_legacy_drive_opts
= {
709 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
713 .type
= QEMU_OPT_NUMBER
,
714 .help
= "bus number",
717 .type
= QEMU_OPT_NUMBER
,
718 .help
= "unit number (i.e. lun for scsi)",
721 .type
= QEMU_OPT_NUMBER
,
722 .help
= "index number",
725 .type
= QEMU_OPT_STRING
,
726 .help
= "media type (disk, cdrom)",
729 .type
= QEMU_OPT_STRING
,
730 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
733 .type
= QEMU_OPT_STRING
,
737 /* Options that are passed on, but have special semantics with -drive */
739 .name
= BDRV_OPT_READ_ONLY
,
740 .type
= QEMU_OPT_BOOL
,
741 .help
= "open drive file as read-only",
744 .type
= QEMU_OPT_STRING
,
745 .help
= "read error action",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "write error action",
751 .name
= "copy-on-read",
752 .type
= QEMU_OPT_BOOL
,
753 .help
= "copy read data from backing file into image file",
756 { /* end of list */ }
760 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
,
765 DriveInfo
*dinfo
= NULL
;
767 QemuOpts
*legacy_opts
;
768 DriveMediaType media
= MEDIA_DISK
;
769 BlockInterfaceType type
;
770 int max_devs
, bus_id
, unit_id
, index
;
771 const char *werror
, *rerror
;
772 bool read_only
= false;
774 const char *filename
;
775 Error
*local_err
= NULL
;
778 /* Change legacy command line options into QMP ones */
779 static const struct {
783 { "iops", "throttling.iops-total" },
784 { "iops_rd", "throttling.iops-read" },
785 { "iops_wr", "throttling.iops-write" },
787 { "bps", "throttling.bps-total" },
788 { "bps_rd", "throttling.bps-read" },
789 { "bps_wr", "throttling.bps-write" },
791 { "iops_max", "throttling.iops-total-max" },
792 { "iops_rd_max", "throttling.iops-read-max" },
793 { "iops_wr_max", "throttling.iops-write-max" },
795 { "bps_max", "throttling.bps-total-max" },
796 { "bps_rd_max", "throttling.bps-read-max" },
797 { "bps_wr_max", "throttling.bps-write-max" },
799 { "iops_size", "throttling.iops-size" },
801 { "group", "throttling.group" },
803 { "readonly", BDRV_OPT_READ_ONLY
},
806 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
807 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
810 error_propagate(errp
, local_err
);
815 value
= qemu_opt_get(all_opts
, "cache");
820 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
821 error_setg(errp
, "invalid cache option");
825 /* Specific options take precedence */
826 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
827 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
828 !writethrough
, &error_abort
);
830 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
831 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
832 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
834 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
835 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
836 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
838 qemu_opt_unset(all_opts
, "cache");
841 /* Get a QDict for processing the options */
842 bs_opts
= qdict_new();
843 qemu_opts_to_qdict(all_opts
, bs_opts
);
845 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
847 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
849 error_propagate(errp
, local_err
);
854 value
= qemu_opt_get(legacy_opts
, "media");
856 if (!strcmp(value
, "disk")) {
858 } else if (!strcmp(value
, "cdrom")) {
862 error_setg(errp
, "'%s' invalid media", value
);
867 /* copy-on-read is disabled with a warning for read-only devices */
868 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
869 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
871 if (read_only
&& copy_on_read
) {
872 warn_report("disabling copy-on-read on read-only drive");
873 copy_on_read
= false;
876 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
877 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
879 /* Controller type */
880 value
= qemu_opt_get(legacy_opts
, "if");
883 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
886 if (type
== IF_COUNT
) {
887 error_setg(errp
, "unsupported bus type '%s'", value
);
891 type
= block_default_type
;
894 /* Device address specified by bus/unit or index.
895 * If none was specified, try to find the first free one. */
896 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
897 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
898 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
900 max_devs
= if_max_devs
[type
];
903 if (bus_id
!= 0 || unit_id
!= -1) {
904 error_setg(errp
, "index cannot be used with bus and unit");
907 bus_id
= drive_index_to_bus_id(type
, index
);
908 unit_id
= drive_index_to_unit_id(type
, index
);
913 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
915 if (max_devs
&& unit_id
>= max_devs
) {
922 if (max_devs
&& unit_id
>= max_devs
) {
923 error_setg(errp
, "unit %d too big (max is %d)", unit_id
, max_devs
- 1);
927 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
928 error_setg(errp
, "drive with bus=%d, unit=%d (index=%d) exists",
929 bus_id
, unit_id
, index
);
933 /* no id supplied -> create one */
934 if (qemu_opts_id(all_opts
) == NULL
) {
936 const char *mediastr
= "";
937 if (type
== IF_IDE
|| type
== IF_SCSI
) {
938 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
941 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
944 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
947 qdict_put_str(bs_opts
, "id", new_id
);
951 /* Add virtio block device */
952 if (type
== IF_VIRTIO
) {
954 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
956 if (arch_type
== QEMU_ARCH_S390X
) {
957 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
959 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
961 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
965 filename
= qemu_opt_get(legacy_opts
, "file");
967 /* Check werror/rerror compatibility with if=... */
968 werror
= qemu_opt_get(legacy_opts
, "werror");
969 if (werror
!= NULL
) {
970 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
972 error_setg(errp
, "werror is not supported by this bus type");
975 qdict_put_str(bs_opts
, "werror", werror
);
978 rerror
= qemu_opt_get(legacy_opts
, "rerror");
979 if (rerror
!= NULL
) {
980 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
982 error_setg(errp
, "rerror is not supported by this bus type");
985 qdict_put_str(bs_opts
, "rerror", rerror
);
988 /* Actual block device init: Functionality shared with blockdev-add */
989 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
992 error_propagate(errp
, local_err
);
998 /* Create legacy DriveInfo */
999 dinfo
= g_malloc0(sizeof(*dinfo
));
1000 dinfo
->opts
= all_opts
;
1003 dinfo
->bus
= bus_id
;
1004 dinfo
->unit
= unit_id
;
1006 blk_set_legacy_dinfo(blk
, dinfo
);
1013 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1020 qemu_opts_del(legacy_opts
);
1021 qobject_unref(bs_opts
);
1025 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1027 BlockDriverState
*bs
;
1029 bs
= bdrv_lookup_bs(name
, name
, errp
);
1034 if (!bdrv_is_root_node(bs
)) {
1035 error_setg(errp
, "Need a root block node");
1039 if (!bdrv_is_inserted(bs
)) {
1040 error_setg(errp
, "Device has no medium");
1047 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1049 TransactionActionList list
;
1051 list
.value
= action
;
1053 qmp_transaction(&list
, false, NULL
, errp
);
1056 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1057 bool has_node_name
, const char *node_name
,
1058 const char *snapshot_file
,
1059 bool has_snapshot_node_name
,
1060 const char *snapshot_node_name
,
1061 bool has_format
, const char *format
,
1062 bool has_mode
, NewImageMode mode
, Error
**errp
)
1064 BlockdevSnapshotSync snapshot
= {
1065 .has_device
= has_device
,
1066 .device
= (char *) device
,
1067 .has_node_name
= has_node_name
,
1068 .node_name
= (char *) node_name
,
1069 .snapshot_file
= (char *) snapshot_file
,
1070 .has_snapshot_node_name
= has_snapshot_node_name
,
1071 .snapshot_node_name
= (char *) snapshot_node_name
,
1072 .has_format
= has_format
,
1073 .format
= (char *) format
,
1074 .has_mode
= has_mode
,
1077 TransactionAction action
= {
1078 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1079 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1081 blockdev_do_action(&action
, errp
);
1084 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1087 BlockdevSnapshot snapshot_data
= {
1088 .node
= (char *) node
,
1089 .overlay
= (char *) overlay
1091 TransactionAction action
= {
1092 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1093 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1095 blockdev_do_action(&action
, errp
);
1098 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1102 BlockdevSnapshotInternal snapshot
= {
1103 .device
= (char *) device
,
1104 .name
= (char *) name
1106 TransactionAction action
= {
1107 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1108 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1110 blockdev_do_action(&action
, errp
);
1113 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1120 BlockDriverState
*bs
;
1121 AioContext
*aio_context
;
1122 QEMUSnapshotInfo sn
;
1123 Error
*local_err
= NULL
;
1124 SnapshotInfo
*info
= NULL
;
1127 bs
= qmp_get_root_bs(device
, errp
);
1131 aio_context
= bdrv_get_aio_context(bs
);
1132 aio_context_acquire(aio_context
);
1143 error_setg(errp
, "Name or id must be provided");
1144 goto out_aio_context
;
1147 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1148 goto out_aio_context
;
1151 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1153 error_propagate(errp
, local_err
);
1154 goto out_aio_context
;
1158 "Snapshot with id '%s' and name '%s' does not exist on "
1160 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1161 goto out_aio_context
;
1164 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1166 error_propagate(errp
, local_err
);
1167 goto out_aio_context
;
1170 aio_context_release(aio_context
);
1172 info
= g_new0(SnapshotInfo
, 1);
1173 info
->id
= g_strdup(sn
.id_str
);
1174 info
->name
= g_strdup(sn
.name
);
1175 info
->date_nsec
= sn
.date_nsec
;
1176 info
->date_sec
= sn
.date_sec
;
1177 info
->vm_state_size
= sn
.vm_state_size
;
1178 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1179 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1184 aio_context_release(aio_context
);
1189 * block_dirty_bitmap_lookup:
1190 * Return a dirty bitmap (if present), after validating
1191 * the node reference and bitmap names.
1193 * @node: The name of the BDS node to search for bitmaps
1194 * @name: The name of the bitmap to search for
1195 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1196 * @errp: Output pointer for error information. Can be NULL.
1198 * @return: A bitmap object on success, or NULL on failure.
1200 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1202 BlockDriverState
**pbs
,
1205 BlockDriverState
*bs
;
1206 BdrvDirtyBitmap
*bitmap
;
1209 error_setg(errp
, "Node cannot be NULL");
1213 error_setg(errp
, "Bitmap name cannot be NULL");
1216 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1218 error_setg(errp
, "Node '%s' not found", node
);
1222 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1224 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1235 /* New and old BlockDriverState structs for atomic group operations */
1237 typedef struct BlkActionState BlkActionState
;
1241 * Table of operations that define an Action.
1243 * @instance_size: Size of state struct, in bytes.
1244 * @prepare: Prepare the work, must NOT be NULL.
1245 * @commit: Commit the changes, can be NULL.
1246 * @abort: Abort the changes on fail, can be NULL.
1247 * @clean: Clean up resources after all transaction actions have called
1248 * commit() or abort(). Can be NULL.
1250 * Only prepare() may fail. In a single transaction, only one of commit() or
1251 * abort() will be called. clean() will always be called if it is present.
1253 typedef struct BlkActionOps
{
1254 size_t instance_size
;
1255 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1256 void (*commit
)(BlkActionState
*common
);
1257 void (*abort
)(BlkActionState
*common
);
1258 void (*clean
)(BlkActionState
*common
);
1263 * Describes one Action's state within a Transaction.
1265 * @action: QAPI-defined enum identifying which Action to perform.
1266 * @ops: Table of ActionOps this Action can perform.
1267 * @block_job_txn: Transaction which this action belongs to.
1268 * @entry: List membership for all Actions in this Transaction.
1270 * This structure must be arranged as first member in a subclassed type,
1271 * assuming that the compiler will also arrange it to the same offsets as the
1274 struct BlkActionState
{
1275 TransactionAction
*action
;
1276 const BlkActionOps
*ops
;
1277 JobTxn
*block_job_txn
;
1278 TransactionProperties
*txn_props
;
1279 QTAILQ_ENTRY(BlkActionState
) entry
;
1282 /* internal snapshot private data */
1283 typedef struct InternalSnapshotState
{
1284 BlkActionState common
;
1285 BlockDriverState
*bs
;
1286 QEMUSnapshotInfo sn
;
1288 } InternalSnapshotState
;
1291 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1293 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1295 "Action '%s' does not support Transaction property "
1296 "completion-mode = %s",
1297 TransactionActionKind_str(s
->action
->type
),
1298 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1304 static void internal_snapshot_prepare(BlkActionState
*common
,
1307 Error
*local_err
= NULL
;
1310 BlockDriverState
*bs
;
1311 QEMUSnapshotInfo old_sn
, *sn
;
1314 BlockdevSnapshotInternal
*internal
;
1315 InternalSnapshotState
*state
;
1316 AioContext
*aio_context
;
1319 g_assert(common
->action
->type
==
1320 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1321 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1322 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1324 /* 1. parse input */
1325 device
= internal
->device
;
1326 name
= internal
->name
;
1328 /* 2. check for validation */
1329 if (action_check_completion_mode(common
, errp
) < 0) {
1333 bs
= qmp_get_root_bs(device
, errp
);
1338 aio_context
= bdrv_get_aio_context(bs
);
1339 aio_context_acquire(aio_context
);
1343 /* Paired with .clean() */
1344 bdrv_drained_begin(bs
);
1346 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1350 if (bdrv_is_read_only(bs
)) {
1351 error_setg(errp
, "Device '%s' is read only", device
);
1355 if (!bdrv_can_snapshot(bs
)) {
1356 error_setg(errp
, "Block format '%s' used by device '%s' "
1357 "does not support internal snapshots",
1358 bs
->drv
->format_name
, device
);
1362 if (!strlen(name
)) {
1363 error_setg(errp
, "Name is empty");
1367 /* check whether a snapshot with name exist */
1368 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1371 error_propagate(errp
, local_err
);
1375 "Snapshot with name '%s' already exists on device '%s'",
1380 /* 3. take the snapshot */
1382 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1383 qemu_gettimeofday(&tv
);
1384 sn
->date_sec
= tv
.tv_sec
;
1385 sn
->date_nsec
= tv
.tv_usec
* 1000;
1386 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1388 ret1
= bdrv_snapshot_create(bs
, sn
);
1390 error_setg_errno(errp
, -ret1
,
1391 "Failed to create snapshot '%s' on device '%s'",
1396 /* 4. succeed, mark a snapshot is created */
1397 state
->created
= true;
1400 aio_context_release(aio_context
);
1403 static void internal_snapshot_abort(BlkActionState
*common
)
1405 InternalSnapshotState
*state
=
1406 DO_UPCAST(InternalSnapshotState
, common
, common
);
1407 BlockDriverState
*bs
= state
->bs
;
1408 QEMUSnapshotInfo
*sn
= &state
->sn
;
1409 AioContext
*aio_context
;
1410 Error
*local_error
= NULL
;
1412 if (!state
->created
) {
1416 aio_context
= bdrv_get_aio_context(state
->bs
);
1417 aio_context_acquire(aio_context
);
1419 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1420 error_reportf_err(local_error
,
1421 "Failed to delete snapshot with id '%s' and "
1422 "name '%s' on device '%s' in abort: ",
1423 sn
->id_str
, sn
->name
,
1424 bdrv_get_device_name(bs
));
1427 aio_context_release(aio_context
);
1430 static void internal_snapshot_clean(BlkActionState
*common
)
1432 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1434 AioContext
*aio_context
;
1440 aio_context
= bdrv_get_aio_context(state
->bs
);
1441 aio_context_acquire(aio_context
);
1443 bdrv_drained_end(state
->bs
);
1445 aio_context_release(aio_context
);
1448 /* external snapshot private data */
1449 typedef struct ExternalSnapshotState
{
1450 BlkActionState common
;
1451 BlockDriverState
*old_bs
;
1452 BlockDriverState
*new_bs
;
1453 bool overlay_appended
;
1454 } ExternalSnapshotState
;
1456 static void external_snapshot_prepare(BlkActionState
*common
,
1460 QDict
*options
= NULL
;
1461 Error
*local_err
= NULL
;
1462 /* Device and node name of the image to generate the snapshot from */
1464 const char *node_name
;
1465 /* Reference to the new image (for 'blockdev-snapshot') */
1466 const char *snapshot_ref
;
1467 /* File name of the new image (for 'blockdev-snapshot-sync') */
1468 const char *new_image_file
;
1469 ExternalSnapshotState
*state
=
1470 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1471 TransactionAction
*action
= common
->action
;
1472 AioContext
*aio_context
;
1473 uint64_t perm
, shared
;
1475 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1476 * purpose but a different set of parameters */
1477 switch (action
->type
) {
1478 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1480 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1482 node_name
= s
->node
;
1483 new_image_file
= NULL
;
1484 snapshot_ref
= s
->overlay
;
1487 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1489 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1490 device
= s
->has_device
? s
->device
: NULL
;
1491 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1492 new_image_file
= s
->snapshot_file
;
1493 snapshot_ref
= NULL
;
1497 g_assert_not_reached();
1500 /* start processing */
1501 if (action_check_completion_mode(common
, errp
) < 0) {
1505 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1506 if (!state
->old_bs
) {
1510 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1511 aio_context_acquire(aio_context
);
1513 /* Paired with .clean() */
1514 bdrv_drained_begin(state
->old_bs
);
1516 if (!bdrv_is_inserted(state
->old_bs
)) {
1517 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1521 if (bdrv_op_is_blocked(state
->old_bs
,
1522 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1526 if (!bdrv_is_read_only(state
->old_bs
)) {
1527 if (bdrv_flush(state
->old_bs
)) {
1528 error_setg(errp
, QERR_IO_ERROR
);
1533 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1534 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1535 const char *format
= s
->has_format
? s
->format
: "qcow2";
1536 enum NewImageMode mode
;
1537 const char *snapshot_node_name
=
1538 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1540 if (node_name
&& !snapshot_node_name
) {
1541 error_setg(errp
, "New overlay node name missing");
1545 if (snapshot_node_name
&&
1546 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1547 error_setg(errp
, "New overlay node name already in use");
1551 flags
= state
->old_bs
->open_flags
;
1552 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1553 flags
|= BDRV_O_NO_BACKING
;
1555 /* create new image w/backing file */
1556 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1557 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1558 int64_t size
= bdrv_getlength(state
->old_bs
);
1560 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1563 bdrv_refresh_filename(state
->old_bs
);
1564 bdrv_img_create(new_image_file
, format
,
1565 state
->old_bs
->filename
,
1566 state
->old_bs
->drv
->format_name
,
1567 NULL
, size
, flags
, false, &local_err
);
1569 error_propagate(errp
, local_err
);
1574 options
= qdict_new();
1575 if (snapshot_node_name
) {
1576 qdict_put_str(options
, "node-name", snapshot_node_name
);
1578 qdict_put_str(options
, "driver", format
);
1581 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1583 /* We will manually add the backing_hd field to the bs later */
1584 if (!state
->new_bs
) {
1589 * Allow attaching a backing file to an overlay that's already in use only
1590 * if the parents don't assume that they are already seeing a valid image.
1591 * (Specifically, allow it as a mirror target, which is write-only access.)
1593 bdrv_get_cumulative_perm(state
->new_bs
, &perm
, &shared
);
1594 if (perm
& BLK_PERM_CONSISTENT_READ
) {
1595 error_setg(errp
, "The overlay is already in use");
1599 if (state
->new_bs
->backing
!= NULL
) {
1600 error_setg(errp
, "The overlay already has a backing image");
1604 if (!state
->new_bs
->drv
->supports_backing
) {
1605 error_setg(errp
, "The overlay does not support backing images");
1609 /* This removes our old bs and adds the new bs. This is an operation that
1610 * can fail, so we need to do it in .prepare; undoing it for abort is
1611 * always possible. */
1612 bdrv_ref(state
->new_bs
);
1613 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1615 error_propagate(errp
, local_err
);
1618 state
->overlay_appended
= true;
1621 aio_context_release(aio_context
);
1624 static void external_snapshot_commit(BlkActionState
*common
)
1626 ExternalSnapshotState
*state
=
1627 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1628 AioContext
*aio_context
;
1630 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1631 aio_context_acquire(aio_context
);
1633 /* We don't need (or want) to use the transactional
1634 * bdrv_reopen_multiple() across all the entries at once, because we
1635 * don't want to abort all of them if one of them fails the reopen */
1636 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1637 bdrv_reopen_set_read_only(state
->old_bs
, true, NULL
);
1640 aio_context_release(aio_context
);
1643 static void external_snapshot_abort(BlkActionState
*common
)
1645 ExternalSnapshotState
*state
=
1646 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1647 if (state
->new_bs
) {
1648 if (state
->overlay_appended
) {
1649 AioContext
*aio_context
;
1650 AioContext
*tmp_context
;
1653 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1654 aio_context_acquire(aio_context
);
1656 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1657 close state->old_bs; we need it */
1658 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1661 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1662 * the main AioContext. As we're still going to be using it, return
1663 * it to the AioContext it was before.
1665 tmp_context
= bdrv_get_aio_context(state
->old_bs
);
1666 if (aio_context
!= tmp_context
) {
1667 aio_context_release(aio_context
);
1668 aio_context_acquire(tmp_context
);
1670 ret
= bdrv_try_set_aio_context(state
->old_bs
,
1674 aio_context_release(tmp_context
);
1675 aio_context_acquire(aio_context
);
1678 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1679 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1681 aio_context_release(aio_context
);
1686 static void external_snapshot_clean(BlkActionState
*common
)
1688 ExternalSnapshotState
*state
=
1689 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1690 AioContext
*aio_context
;
1692 if (!state
->old_bs
) {
1696 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1697 aio_context_acquire(aio_context
);
1699 bdrv_drained_end(state
->old_bs
);
1700 bdrv_unref(state
->new_bs
);
1702 aio_context_release(aio_context
);
1705 typedef struct DriveBackupState
{
1706 BlkActionState common
;
1707 BlockDriverState
*bs
;
1711 static BlockJob
*do_backup_common(BackupCommon
*backup
,
1712 BlockDriverState
*bs
,
1713 BlockDriverState
*target_bs
,
1714 AioContext
*aio_context
,
1715 JobTxn
*txn
, Error
**errp
);
1717 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1719 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1720 DriveBackup
*backup
;
1721 BlockDriverState
*bs
;
1722 BlockDriverState
*target_bs
;
1723 BlockDriverState
*source
= NULL
;
1724 AioContext
*aio_context
;
1725 AioContext
*old_context
;
1727 Error
*local_err
= NULL
;
1730 bool set_backing_hd
= false;
1733 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1734 backup
= common
->action
->u
.drive_backup
.data
;
1736 if (!backup
->has_mode
) {
1737 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1740 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1746 error_setg(errp
, "Device has no medium");
1750 aio_context
= bdrv_get_aio_context(bs
);
1751 aio_context_acquire(aio_context
);
1753 /* Paired with .clean() */
1754 bdrv_drained_begin(bs
);
1756 if (!backup
->has_format
) {
1757 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
1758 NULL
: (char *) bs
->drv
->format_name
;
1761 /* Early check to avoid creating target */
1762 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
1766 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1769 * See if we have a backing HD we can use to create our new image
1772 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
1773 source
= backing_bs(bs
);
1775 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
1778 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
1780 flags
|= BDRV_O_NO_BACKING
;
1781 set_backing_hd
= true;
1784 size
= bdrv_getlength(bs
);
1786 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1790 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
1791 assert(backup
->format
);
1793 bdrv_refresh_filename(source
);
1794 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
1795 source
->drv
->format_name
, NULL
,
1796 size
, flags
, false, &local_err
);
1798 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
1799 size
, flags
, false, &local_err
);
1804 error_propagate(errp
, local_err
);
1808 options
= qdict_new();
1809 qdict_put_str(options
, "discard", "unmap");
1810 qdict_put_str(options
, "detect-zeroes", "unmap");
1811 if (backup
->format
) {
1812 qdict_put_str(options
, "driver", backup
->format
);
1815 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
1820 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1821 old_context
= bdrv_get_aio_context(target_bs
);
1822 aio_context_release(aio_context
);
1823 aio_context_acquire(old_context
);
1825 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1827 bdrv_unref(target_bs
);
1828 aio_context_release(old_context
);
1832 aio_context_release(old_context
);
1833 aio_context_acquire(aio_context
);
1835 if (set_backing_hd
) {
1836 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
1844 state
->job
= do_backup_common(qapi_DriveBackup_base(backup
),
1845 bs
, target_bs
, aio_context
,
1846 common
->block_job_txn
, errp
);
1849 bdrv_unref(target_bs
);
1851 aio_context_release(aio_context
);
1854 static void drive_backup_commit(BlkActionState
*common
)
1856 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1857 AioContext
*aio_context
;
1859 aio_context
= bdrv_get_aio_context(state
->bs
);
1860 aio_context_acquire(aio_context
);
1863 job_start(&state
->job
->job
);
1865 aio_context_release(aio_context
);
1868 static void drive_backup_abort(BlkActionState
*common
)
1870 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1873 AioContext
*aio_context
;
1875 aio_context
= bdrv_get_aio_context(state
->bs
);
1876 aio_context_acquire(aio_context
);
1878 job_cancel_sync(&state
->job
->job
);
1880 aio_context_release(aio_context
);
1884 static void drive_backup_clean(BlkActionState
*common
)
1886 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1887 AioContext
*aio_context
;
1893 aio_context
= bdrv_get_aio_context(state
->bs
);
1894 aio_context_acquire(aio_context
);
1896 bdrv_drained_end(state
->bs
);
1898 aio_context_release(aio_context
);
1901 typedef struct BlockdevBackupState
{
1902 BlkActionState common
;
1903 BlockDriverState
*bs
;
1905 } BlockdevBackupState
;
1907 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1909 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1910 BlockdevBackup
*backup
;
1911 BlockDriverState
*bs
;
1912 BlockDriverState
*target_bs
;
1913 AioContext
*aio_context
;
1914 AioContext
*old_context
;
1917 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1918 backup
= common
->action
->u
.blockdev_backup
.data
;
1920 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1925 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1930 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1931 aio_context
= bdrv_get_aio_context(bs
);
1932 old_context
= bdrv_get_aio_context(target_bs
);
1933 aio_context_acquire(old_context
);
1935 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1937 aio_context_release(old_context
);
1941 aio_context_release(old_context
);
1942 aio_context_acquire(aio_context
);
1945 /* Paired with .clean() */
1946 bdrv_drained_begin(state
->bs
);
1948 state
->job
= do_backup_common(qapi_BlockdevBackup_base(backup
),
1949 bs
, target_bs
, aio_context
,
1950 common
->block_job_txn
, errp
);
1952 aio_context_release(aio_context
);
1955 static void blockdev_backup_commit(BlkActionState
*common
)
1957 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1958 AioContext
*aio_context
;
1960 aio_context
= bdrv_get_aio_context(state
->bs
);
1961 aio_context_acquire(aio_context
);
1964 job_start(&state
->job
->job
);
1966 aio_context_release(aio_context
);
1969 static void blockdev_backup_abort(BlkActionState
*common
)
1971 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1974 AioContext
*aio_context
;
1976 aio_context
= bdrv_get_aio_context(state
->bs
);
1977 aio_context_acquire(aio_context
);
1979 job_cancel_sync(&state
->job
->job
);
1981 aio_context_release(aio_context
);
1985 static void blockdev_backup_clean(BlkActionState
*common
)
1987 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1988 AioContext
*aio_context
;
1994 aio_context
= bdrv_get_aio_context(state
->bs
);
1995 aio_context_acquire(aio_context
);
1997 bdrv_drained_end(state
->bs
);
1999 aio_context_release(aio_context
);
2002 typedef struct BlockDirtyBitmapState
{
2003 BlkActionState common
;
2004 BdrvDirtyBitmap
*bitmap
;
2005 BlockDriverState
*bs
;
2009 } BlockDirtyBitmapState
;
2011 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2014 Error
*local_err
= NULL
;
2015 BlockDirtyBitmapAdd
*action
;
2016 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2019 if (action_check_completion_mode(common
, errp
) < 0) {
2023 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2024 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2025 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2026 action
->has_granularity
, action
->granularity
,
2027 action
->has_persistent
, action
->persistent
,
2028 action
->has_disabled
, action
->disabled
,
2032 state
->prepared
= true;
2034 error_propagate(errp
, local_err
);
2038 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2040 BlockDirtyBitmapAdd
*action
;
2041 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2044 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2045 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2046 * then the node reference and bitmap name must have been valid.
2048 if (state
->prepared
) {
2049 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2053 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2056 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2058 BlockDirtyBitmap
*action
;
2060 if (action_check_completion_mode(common
, errp
) < 0) {
2064 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2065 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2069 if (!state
->bitmap
) {
2073 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
2077 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2080 static void block_dirty_bitmap_restore(BlkActionState
*common
)
2082 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2085 if (state
->backup
) {
2086 bdrv_restore_dirty_bitmap(state
->bitmap
, state
->backup
);
2090 static void block_dirty_bitmap_free_backup(BlkActionState
*common
)
2092 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2095 hbitmap_free(state
->backup
);
2098 static void block_dirty_bitmap_enable_prepare(BlkActionState
*common
,
2101 BlockDirtyBitmap
*action
;
2102 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2105 if (action_check_completion_mode(common
, errp
) < 0) {
2109 action
= common
->action
->u
.block_dirty_bitmap_enable
.data
;
2110 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2114 if (!state
->bitmap
) {
2118 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2122 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2123 bdrv_enable_dirty_bitmap(state
->bitmap
);
2126 static void block_dirty_bitmap_enable_abort(BlkActionState
*common
)
2128 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2131 if (!state
->was_enabled
) {
2132 bdrv_disable_dirty_bitmap(state
->bitmap
);
2136 static void block_dirty_bitmap_disable_prepare(BlkActionState
*common
,
2139 BlockDirtyBitmap
*action
;
2140 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2143 if (action_check_completion_mode(common
, errp
) < 0) {
2147 action
= common
->action
->u
.block_dirty_bitmap_disable
.data
;
2148 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2152 if (!state
->bitmap
) {
2156 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2160 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2161 bdrv_disable_dirty_bitmap(state
->bitmap
);
2164 static void block_dirty_bitmap_disable_abort(BlkActionState
*common
)
2166 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2169 if (state
->was_enabled
) {
2170 bdrv_enable_dirty_bitmap(state
->bitmap
);
2174 static BdrvDirtyBitmap
*do_block_dirty_bitmap_merge(
2175 const char *node
, const char *target
,
2176 BlockDirtyBitmapMergeSourceList
*bitmaps
,
2177 HBitmap
**backup
, Error
**errp
);
2179 static void block_dirty_bitmap_merge_prepare(BlkActionState
*common
,
2182 BlockDirtyBitmapMerge
*action
;
2183 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2186 if (action_check_completion_mode(common
, errp
) < 0) {
2190 action
= common
->action
->u
.block_dirty_bitmap_merge
.data
;
2192 state
->bitmap
= do_block_dirty_bitmap_merge(action
->node
, action
->target
,
2193 action
->bitmaps
, &state
->backup
,
2197 static BdrvDirtyBitmap
*do_block_dirty_bitmap_remove(
2198 const char *node
, const char *name
, bool release
,
2199 BlockDriverState
**bitmap_bs
, Error
**errp
);
2201 static void block_dirty_bitmap_remove_prepare(BlkActionState
*common
,
2204 BlockDirtyBitmap
*action
;
2205 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2208 if (action_check_completion_mode(common
, errp
) < 0) {
2212 action
= common
->action
->u
.block_dirty_bitmap_remove
.data
;
2214 state
->bitmap
= do_block_dirty_bitmap_remove(action
->node
, action
->name
,
2215 false, &state
->bs
, errp
);
2216 if (state
->bitmap
) {
2217 bdrv_dirty_bitmap_skip_store(state
->bitmap
, true);
2218 bdrv_dirty_bitmap_set_busy(state
->bitmap
, true);
2222 static void block_dirty_bitmap_remove_abort(BlkActionState
*common
)
2224 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2227 if (state
->bitmap
) {
2228 bdrv_dirty_bitmap_skip_store(state
->bitmap
, false);
2229 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2233 static void block_dirty_bitmap_remove_commit(BlkActionState
*common
)
2235 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2238 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2239 bdrv_release_dirty_bitmap(state
->bitmap
);
2242 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2244 error_setg(errp
, "Transaction aborted using Abort action");
2247 static void abort_commit(BlkActionState
*common
)
2249 g_assert_not_reached(); /* this action never succeeds */
2252 static const BlkActionOps actions
[] = {
2253 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2254 .instance_size
= sizeof(ExternalSnapshotState
),
2255 .prepare
= external_snapshot_prepare
,
2256 .commit
= external_snapshot_commit
,
2257 .abort
= external_snapshot_abort
,
2258 .clean
= external_snapshot_clean
,
2260 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2261 .instance_size
= sizeof(ExternalSnapshotState
),
2262 .prepare
= external_snapshot_prepare
,
2263 .commit
= external_snapshot_commit
,
2264 .abort
= external_snapshot_abort
,
2265 .clean
= external_snapshot_clean
,
2267 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2268 .instance_size
= sizeof(DriveBackupState
),
2269 .prepare
= drive_backup_prepare
,
2270 .commit
= drive_backup_commit
,
2271 .abort
= drive_backup_abort
,
2272 .clean
= drive_backup_clean
,
2274 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2275 .instance_size
= sizeof(BlockdevBackupState
),
2276 .prepare
= blockdev_backup_prepare
,
2277 .commit
= blockdev_backup_commit
,
2278 .abort
= blockdev_backup_abort
,
2279 .clean
= blockdev_backup_clean
,
2281 [TRANSACTION_ACTION_KIND_ABORT
] = {
2282 .instance_size
= sizeof(BlkActionState
),
2283 .prepare
= abort_prepare
,
2284 .commit
= abort_commit
,
2286 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2287 .instance_size
= sizeof(InternalSnapshotState
),
2288 .prepare
= internal_snapshot_prepare
,
2289 .abort
= internal_snapshot_abort
,
2290 .clean
= internal_snapshot_clean
,
2292 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2293 .instance_size
= sizeof(BlockDirtyBitmapState
),
2294 .prepare
= block_dirty_bitmap_add_prepare
,
2295 .abort
= block_dirty_bitmap_add_abort
,
2297 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2298 .instance_size
= sizeof(BlockDirtyBitmapState
),
2299 .prepare
= block_dirty_bitmap_clear_prepare
,
2300 .commit
= block_dirty_bitmap_free_backup
,
2301 .abort
= block_dirty_bitmap_restore
,
2303 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE
] = {
2304 .instance_size
= sizeof(BlockDirtyBitmapState
),
2305 .prepare
= block_dirty_bitmap_enable_prepare
,
2306 .abort
= block_dirty_bitmap_enable_abort
,
2308 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE
] = {
2309 .instance_size
= sizeof(BlockDirtyBitmapState
),
2310 .prepare
= block_dirty_bitmap_disable_prepare
,
2311 .abort
= block_dirty_bitmap_disable_abort
,
2313 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE
] = {
2314 .instance_size
= sizeof(BlockDirtyBitmapState
),
2315 .prepare
= block_dirty_bitmap_merge_prepare
,
2316 .commit
= block_dirty_bitmap_free_backup
,
2317 .abort
= block_dirty_bitmap_restore
,
2319 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE
] = {
2320 .instance_size
= sizeof(BlockDirtyBitmapState
),
2321 .prepare
= block_dirty_bitmap_remove_prepare
,
2322 .commit
= block_dirty_bitmap_remove_commit
,
2323 .abort
= block_dirty_bitmap_remove_abort
,
2325 /* Where are transactions for MIRROR, COMMIT and STREAM?
2326 * Although these blockjobs use transaction callbacks like the backup job,
2327 * these jobs do not necessarily adhere to transaction semantics.
2328 * These jobs may not fully undo all of their actions on abort, nor do they
2329 * necessarily work in transactions with more than one job in them.
2334 * Allocate a TransactionProperties structure if necessary, and fill
2335 * that structure with desired defaults if they are unset.
2337 static TransactionProperties
*get_transaction_properties(
2338 TransactionProperties
*props
)
2341 props
= g_new0(TransactionProperties
, 1);
2344 if (!props
->has_completion_mode
) {
2345 props
->has_completion_mode
= true;
2346 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2353 * 'Atomic' group operations. The operations are performed as a set, and if
2354 * any fail then we roll back all operations in the group.
2356 void qmp_transaction(TransactionActionList
*dev_list
,
2358 struct TransactionProperties
*props
,
2361 TransactionActionList
*dev_entry
= dev_list
;
2362 JobTxn
*block_job_txn
= NULL
;
2363 BlkActionState
*state
, *next
;
2364 Error
*local_err
= NULL
;
2366 QTAILQ_HEAD(, BlkActionState
) snap_bdrv_states
;
2367 QTAILQ_INIT(&snap_bdrv_states
);
2369 /* Does this transaction get canceled as a group on failure?
2370 * If not, we don't really need to make a JobTxn.
2372 props
= get_transaction_properties(props
);
2373 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2374 block_job_txn
= job_txn_new();
2377 /* drain all i/o before any operations */
2380 /* We don't do anything in this loop that commits us to the operations */
2381 while (NULL
!= dev_entry
) {
2382 TransactionAction
*dev_info
= NULL
;
2383 const BlkActionOps
*ops
;
2385 dev_info
= dev_entry
->value
;
2386 dev_entry
= dev_entry
->next
;
2388 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2390 ops
= &actions
[dev_info
->type
];
2391 assert(ops
->instance_size
> 0);
2393 state
= g_malloc0(ops
->instance_size
);
2395 state
->action
= dev_info
;
2396 state
->block_job_txn
= block_job_txn
;
2397 state
->txn_props
= props
;
2398 QTAILQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2400 state
->ops
->prepare(state
, &local_err
);
2402 error_propagate(errp
, local_err
);
2403 goto delete_and_fail
;
2407 QTAILQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2408 if (state
->ops
->commit
) {
2409 state
->ops
->commit(state
);
2417 /* failure, and it is all-or-none; roll back all operations */
2418 QTAILQ_FOREACH_REVERSE(state
, &snap_bdrv_states
, entry
) {
2419 if (state
->ops
->abort
) {
2420 state
->ops
->abort(state
);
2424 QTAILQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2425 if (state
->ops
->clean
) {
2426 state
->ops
->clean(state
);
2431 qapi_free_TransactionProperties(props
);
2433 job_txn_unref(block_job_txn
);
2436 void qmp_block_passwd(bool has_device
, const char *device
,
2437 bool has_node_name
, const char *node_name
,
2438 const char *password
, Error
**errp
)
2441 "Setting block passwords directly is no longer supported");
2444 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2445 bool has_granularity
, uint32_t granularity
,
2446 bool has_persistent
, bool persistent
,
2447 bool has_disabled
, bool disabled
,
2450 BlockDriverState
*bs
;
2451 BdrvDirtyBitmap
*bitmap
;
2452 AioContext
*aio_context
;
2454 if (!name
|| name
[0] == '\0') {
2455 error_setg(errp
, "Bitmap name cannot be empty");
2459 bs
= bdrv_lookup_bs(node
, node
, errp
);
2464 aio_context
= bdrv_get_aio_context(bs
);
2465 aio_context_acquire(aio_context
);
2467 if (has_granularity
) {
2468 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2469 error_setg(errp
, "Granularity must be power of 2 "
2470 "and at least 512");
2474 /* Default to cluster size, if available: */
2475 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2478 if (!has_persistent
) {
2482 if (!has_disabled
) {
2487 !bdrv_can_store_new_dirty_bitmap(bs
, name
, granularity
, errp
))
2492 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2493 if (bitmap
== NULL
) {
2498 bdrv_disable_dirty_bitmap(bitmap
);
2501 bdrv_dirty_bitmap_set_persistence(bitmap
, persistent
);
2504 aio_context_release(aio_context
);
2507 static BdrvDirtyBitmap
*do_block_dirty_bitmap_remove(
2508 const char *node
, const char *name
, bool release
,
2509 BlockDriverState
**bitmap_bs
, Error
**errp
)
2511 BlockDriverState
*bs
;
2512 BdrvDirtyBitmap
*bitmap
;
2513 AioContext
*aio_context
;
2515 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2516 if (!bitmap
|| !bs
) {
2520 aio_context
= bdrv_get_aio_context(bs
);
2521 aio_context_acquire(aio_context
);
2523 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_BUSY
| BDRV_BITMAP_RO
,
2525 aio_context_release(aio_context
);
2529 if (bdrv_dirty_bitmap_get_persistence(bitmap
) &&
2530 bdrv_remove_persistent_dirty_bitmap(bs
, name
, errp
) < 0)
2532 aio_context_release(aio_context
);
2537 bdrv_release_dirty_bitmap(bitmap
);
2544 aio_context_release(aio_context
);
2545 return release
? NULL
: bitmap
;
2548 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2551 do_block_dirty_bitmap_remove(node
, name
, true, NULL
, errp
);
2555 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2556 * immediately after a full backup operation.
2558 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2561 BdrvDirtyBitmap
*bitmap
;
2562 BlockDriverState
*bs
;
2564 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2565 if (!bitmap
|| !bs
) {
2569 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
2573 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2576 void qmp_block_dirty_bitmap_enable(const char *node
, const char *name
,
2579 BlockDriverState
*bs
;
2580 BdrvDirtyBitmap
*bitmap
;
2582 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2587 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2591 bdrv_enable_dirty_bitmap(bitmap
);
2594 void qmp_block_dirty_bitmap_disable(const char *node
, const char *name
,
2597 BlockDriverState
*bs
;
2598 BdrvDirtyBitmap
*bitmap
;
2600 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2605 if (bdrv_dirty_bitmap_check(bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2609 bdrv_disable_dirty_bitmap(bitmap
);
2612 static BdrvDirtyBitmap
*do_block_dirty_bitmap_merge(
2613 const char *node
, const char *target
,
2614 BlockDirtyBitmapMergeSourceList
*bitmaps
,
2615 HBitmap
**backup
, Error
**errp
)
2617 BlockDriverState
*bs
;
2618 BdrvDirtyBitmap
*dst
, *src
, *anon
;
2619 BlockDirtyBitmapMergeSourceList
*lst
;
2620 Error
*local_err
= NULL
;
2622 dst
= block_dirty_bitmap_lookup(node
, target
, &bs
, errp
);
2627 anon
= bdrv_create_dirty_bitmap(bs
, bdrv_dirty_bitmap_granularity(dst
),
2633 for (lst
= bitmaps
; lst
; lst
= lst
->next
) {
2634 switch (lst
->value
->type
) {
2635 const char *name
, *node
;
2637 name
= lst
->value
->u
.local
;
2638 src
= bdrv_find_dirty_bitmap(bs
, name
);
2640 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
2646 node
= lst
->value
->u
.external
.node
;
2647 name
= lst
->value
->u
.external
.name
;
2648 src
= block_dirty_bitmap_lookup(node
, name
, NULL
, errp
);
2658 bdrv_merge_dirty_bitmap(anon
, src
, NULL
, &local_err
);
2660 error_propagate(errp
, local_err
);
2666 /* Merge into dst; dst is unchanged on failure. */
2667 bdrv_merge_dirty_bitmap(dst
, anon
, backup
, errp
);
2670 bdrv_release_dirty_bitmap(anon
);
2674 void qmp_block_dirty_bitmap_merge(const char *node
, const char *target
,
2675 BlockDirtyBitmapMergeSourceList
*bitmaps
,
2678 do_block_dirty_bitmap_merge(node
, target
, bitmaps
, NULL
, errp
);
2681 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2685 BdrvDirtyBitmap
*bitmap
;
2686 BlockDriverState
*bs
;
2687 BlockDirtyBitmapSha256
*ret
= NULL
;
2690 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2691 if (!bitmap
|| !bs
) {
2695 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2696 if (sha256
== NULL
) {
2700 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2701 ret
->sha256
= sha256
;
2706 void qmp_block_resize(bool has_device
, const char *device
,
2707 bool has_node_name
, const char *node_name
,
2708 int64_t size
, Error
**errp
)
2710 Error
*local_err
= NULL
;
2711 BlockBackend
*blk
= NULL
;
2712 BlockDriverState
*bs
;
2713 AioContext
*aio_context
;
2715 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2716 has_node_name
? node_name
: NULL
,
2719 error_propagate(errp
, local_err
);
2723 aio_context
= bdrv_get_aio_context(bs
);
2724 aio_context_acquire(aio_context
);
2727 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2731 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2732 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2736 blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
, errp
);
2741 bdrv_drained_begin(bs
);
2742 blk_truncate(blk
, size
, false, PREALLOC_MODE_OFF
, 0, errp
);
2743 bdrv_drained_end(bs
);
2747 aio_context_release(aio_context
);
2750 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
2751 bool has_base
, const char *base
,
2752 bool has_base_node
, const char *base_node
,
2753 bool has_backing_file
, const char *backing_file
,
2754 bool has_speed
, int64_t speed
,
2755 bool has_on_error
, BlockdevOnError on_error
,
2756 bool has_auto_finalize
, bool auto_finalize
,
2757 bool has_auto_dismiss
, bool auto_dismiss
,
2760 BlockDriverState
*bs
, *iter
;
2761 BlockDriverState
*base_bs
= NULL
;
2762 AioContext
*aio_context
;
2763 Error
*local_err
= NULL
;
2764 const char *base_name
= NULL
;
2765 int job_flags
= JOB_DEFAULT
;
2767 if (!has_on_error
) {
2768 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2771 bs
= bdrv_lookup_bs(device
, device
, errp
);
2776 aio_context
= bdrv_get_aio_context(bs
);
2777 aio_context_acquire(aio_context
);
2779 if (has_base
&& has_base_node
) {
2780 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2781 "at the same time");
2786 base_bs
= bdrv_find_backing_image(bs
, base
);
2787 if (base_bs
== NULL
) {
2788 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2791 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2795 if (has_base_node
) {
2796 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2800 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2801 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2805 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2806 bdrv_refresh_filename(base_bs
);
2807 base_name
= base_bs
->filename
;
2810 /* Check for op blockers in the whole chain between bs and base */
2811 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
2812 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2817 /* if we are streaming the entire chain, the result will have no backing
2818 * file, and specifying one is therefore an error */
2819 if (base_bs
== NULL
&& has_backing_file
) {
2820 error_setg(errp
, "backing file specified, but streaming the "
2825 /* backing_file string overrides base bs filename */
2826 base_name
= has_backing_file
? backing_file
: base_name
;
2828 if (has_auto_finalize
&& !auto_finalize
) {
2829 job_flags
|= JOB_MANUAL_FINALIZE
;
2831 if (has_auto_dismiss
&& !auto_dismiss
) {
2832 job_flags
|= JOB_MANUAL_DISMISS
;
2835 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
2836 job_flags
, has_speed
? speed
: 0, on_error
, &local_err
);
2838 error_propagate(errp
, local_err
);
2842 trace_qmp_block_stream(bs
);
2845 aio_context_release(aio_context
);
2848 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
2849 bool has_base_node
, const char *base_node
,
2850 bool has_base
, const char *base
,
2851 bool has_top_node
, const char *top_node
,
2852 bool has_top
, const char *top
,
2853 bool has_backing_file
, const char *backing_file
,
2854 bool has_speed
, int64_t speed
,
2855 bool has_on_error
, BlockdevOnError on_error
,
2856 bool has_filter_node_name
, const char *filter_node_name
,
2857 bool has_auto_finalize
, bool auto_finalize
,
2858 bool has_auto_dismiss
, bool auto_dismiss
,
2861 BlockDriverState
*bs
;
2862 BlockDriverState
*iter
;
2863 BlockDriverState
*base_bs
, *top_bs
;
2864 AioContext
*aio_context
;
2865 Error
*local_err
= NULL
;
2866 int job_flags
= JOB_DEFAULT
;
2871 if (!has_on_error
) {
2872 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2874 if (!has_filter_node_name
) {
2875 filter_node_name
= NULL
;
2877 if (has_auto_finalize
&& !auto_finalize
) {
2878 job_flags
|= JOB_MANUAL_FINALIZE
;
2880 if (has_auto_dismiss
&& !auto_dismiss
) {
2881 job_flags
|= JOB_MANUAL_DISMISS
;
2885 * libvirt relies on the DeviceNotFound error class in order to probe for
2886 * live commit feature versions; for this to work, we must make sure to
2887 * perform the device lookup before any generic errors that may occur in a
2888 * scenario in which all optional arguments are omitted. */
2889 bs
= qmp_get_root_bs(device
, &local_err
);
2891 bs
= bdrv_lookup_bs(device
, device
, NULL
);
2893 error_free(local_err
);
2894 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2895 "Device '%s' not found", device
);
2897 error_propagate(errp
, local_err
);
2902 aio_context
= bdrv_get_aio_context(bs
);
2903 aio_context_acquire(aio_context
);
2905 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2909 /* default top_bs is the active layer */
2912 if (has_top_node
&& has_top
) {
2913 error_setg(errp
, "'top-node' and 'top' are mutually exclusive");
2915 } else if (has_top_node
) {
2916 top_bs
= bdrv_lookup_bs(NULL
, top_node
, errp
);
2917 if (top_bs
== NULL
) {
2920 if (!bdrv_chain_contains(bs
, top_bs
)) {
2921 error_setg(errp
, "'%s' is not in this backing file chain",
2925 } else if (has_top
&& top
) {
2926 /* This strcmp() is just a shortcut, there is no need to
2927 * refresh @bs's filename. If it mismatches,
2928 * bdrv_find_backing_image() will do the refresh and may still
2930 if (strcmp(bs
->filename
, top
) != 0) {
2931 top_bs
= bdrv_find_backing_image(bs
, top
);
2935 if (top_bs
== NULL
) {
2936 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2940 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2942 if (has_base_node
&& has_base
) {
2943 error_setg(errp
, "'base-node' and 'base' are mutually exclusive");
2945 } else if (has_base_node
) {
2946 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2947 if (base_bs
== NULL
) {
2950 if (!bdrv_chain_contains(top_bs
, base_bs
)) {
2951 error_setg(errp
, "'%s' is not in this backing file chain",
2955 } else if (has_base
&& base
) {
2956 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2958 base_bs
= bdrv_find_base(top_bs
);
2961 if (base_bs
== NULL
) {
2962 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2966 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2968 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
2969 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2974 /* Do not allow attempts to commit an image into itself */
2975 if (top_bs
== base_bs
) {
2976 error_setg(errp
, "cannot commit an image into itself");
2981 if (has_backing_file
) {
2982 error_setg(errp
, "'backing-file' specified,"
2983 " but 'top' is the active layer");
2986 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
2987 job_flags
, speed
, on_error
,
2988 filter_node_name
, NULL
, NULL
, false, &local_err
);
2990 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
2991 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2994 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, job_flags
,
2995 speed
, on_error
, has_backing_file
? backing_file
: NULL
,
2996 filter_node_name
, &local_err
);
2998 if (local_err
!= NULL
) {
2999 error_propagate(errp
, local_err
);
3004 aio_context_release(aio_context
);
3007 /* Common QMP interface for drive-backup and blockdev-backup */
3008 static BlockJob
*do_backup_common(BackupCommon
*backup
,
3009 BlockDriverState
*bs
,
3010 BlockDriverState
*target_bs
,
3011 AioContext
*aio_context
,
3012 JobTxn
*txn
, Error
**errp
)
3014 BlockJob
*job
= NULL
;
3015 BdrvDirtyBitmap
*bmap
= NULL
;
3016 int job_flags
= JOB_DEFAULT
;
3018 if (!backup
->has_speed
) {
3021 if (!backup
->has_on_source_error
) {
3022 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3024 if (!backup
->has_on_target_error
) {
3025 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3027 if (!backup
->has_job_id
) {
3028 backup
->job_id
= NULL
;
3030 if (!backup
->has_auto_finalize
) {
3031 backup
->auto_finalize
= true;
3033 if (!backup
->has_auto_dismiss
) {
3034 backup
->auto_dismiss
= true;
3036 if (!backup
->has_compress
) {
3037 backup
->compress
= false;
3040 if ((backup
->sync
== MIRROR_SYNC_MODE_BITMAP
) ||
3041 (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
)) {
3042 /* done before desugaring 'incremental' to print the right message */
3043 if (!backup
->has_bitmap
) {
3044 error_setg(errp
, "must provide a valid bitmap name for "
3045 "'%s' sync mode", MirrorSyncMode_str(backup
->sync
));
3050 if (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
) {
3051 if (backup
->has_bitmap_mode
&&
3052 backup
->bitmap_mode
!= BITMAP_SYNC_MODE_ON_SUCCESS
) {
3053 error_setg(errp
, "Bitmap sync mode must be '%s' "
3054 "when using sync mode '%s'",
3055 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS
),
3056 MirrorSyncMode_str(backup
->sync
));
3059 backup
->has_bitmap_mode
= true;
3060 backup
->sync
= MIRROR_SYNC_MODE_BITMAP
;
3061 backup
->bitmap_mode
= BITMAP_SYNC_MODE_ON_SUCCESS
;
3064 if (backup
->has_bitmap
) {
3065 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3067 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3070 if (!backup
->has_bitmap_mode
) {
3071 error_setg(errp
, "Bitmap sync mode must be given "
3072 "when providing a bitmap");
3075 if (bdrv_dirty_bitmap_check(bmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
3079 /* This does not produce a useful bitmap artifact: */
3080 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3081 error_setg(errp
, "sync mode '%s' does not produce meaningful bitmap"
3082 " outputs", MirrorSyncMode_str(backup
->sync
));
3086 /* If the bitmap isn't used for input or output, this is useless: */
3087 if (backup
->bitmap_mode
== BITMAP_SYNC_MODE_NEVER
&&
3088 backup
->sync
!= MIRROR_SYNC_MODE_BITMAP
) {
3089 error_setg(errp
, "Bitmap sync mode '%s' has no meaningful effect"
3090 " when combined with sync mode '%s'",
3091 BitmapSyncMode_str(backup
->bitmap_mode
),
3092 MirrorSyncMode_str(backup
->sync
));
3097 if (!backup
->has_bitmap
&& backup
->has_bitmap_mode
) {
3098 error_setg(errp
, "Cannot specify bitmap sync mode without a bitmap");
3102 if (!backup
->auto_finalize
) {
3103 job_flags
|= JOB_MANUAL_FINALIZE
;
3105 if (!backup
->auto_dismiss
) {
3106 job_flags
|= JOB_MANUAL_DISMISS
;
3109 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3110 backup
->sync
, bmap
, backup
->bitmap_mode
,
3112 backup
->filter_node_name
,
3113 backup
->on_source_error
,
3114 backup
->on_target_error
,
3115 job_flags
, NULL
, NULL
, txn
, errp
);
3119 void qmp_drive_backup(DriveBackup
*backup
, Error
**errp
)
3121 TransactionAction action
= {
3122 .type
= TRANSACTION_ACTION_KIND_DRIVE_BACKUP
,
3123 .u
.drive_backup
.data
= backup
,
3125 blockdev_do_action(&action
, errp
);
3128 BlockDeviceInfoList
*qmp_query_named_block_nodes(bool has_flat
,
3132 bool return_flat
= has_flat
&& flat
;
3134 return bdrv_named_nodes_list(return_flat
, errp
);
3137 XDbgBlockGraph
*qmp_x_debug_query_block_graph(Error
**errp
)
3139 return bdrv_get_xdbg_block_graph(errp
);
3142 void qmp_blockdev_backup(BlockdevBackup
*backup
, Error
**errp
)
3144 TransactionAction action
= {
3145 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
,
3146 .u
.blockdev_backup
.data
= backup
,
3148 blockdev_do_action(&action
, errp
);
3151 /* Parameter check and block job starting for drive mirroring.
3152 * Caller should hold @device and @target's aio context (must be the same).
3154 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3155 BlockDriverState
*target
,
3156 bool has_replaces
, const char *replaces
,
3157 enum MirrorSyncMode sync
,
3158 BlockMirrorBackingMode backing_mode
,
3160 bool has_speed
, int64_t speed
,
3161 bool has_granularity
, uint32_t granularity
,
3162 bool has_buf_size
, int64_t buf_size
,
3163 bool has_on_source_error
,
3164 BlockdevOnError on_source_error
,
3165 bool has_on_target_error
,
3166 BlockdevOnError on_target_error
,
3167 bool has_unmap
, bool unmap
,
3168 bool has_filter_node_name
,
3169 const char *filter_node_name
,
3170 bool has_copy_mode
, MirrorCopyMode copy_mode
,
3171 bool has_auto_finalize
, bool auto_finalize
,
3172 bool has_auto_dismiss
, bool auto_dismiss
,
3175 int job_flags
= JOB_DEFAULT
;
3180 if (!has_on_source_error
) {
3181 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3183 if (!has_on_target_error
) {
3184 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3186 if (!has_granularity
) {
3189 if (!has_buf_size
) {
3195 if (!has_filter_node_name
) {
3196 filter_node_name
= NULL
;
3198 if (!has_copy_mode
) {
3199 copy_mode
= MIRROR_COPY_MODE_BACKGROUND
;
3201 if (has_auto_finalize
&& !auto_finalize
) {
3202 job_flags
|= JOB_MANUAL_FINALIZE
;
3204 if (has_auto_dismiss
&& !auto_dismiss
) {
3205 job_flags
|= JOB_MANUAL_DISMISS
;
3208 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3209 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3210 "a value in range [512B, 64MB]");
3213 if (granularity
& (granularity
- 1)) {
3214 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3219 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3222 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3226 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3227 sync
= MIRROR_SYNC_MODE_FULL
;
3231 BlockDriverState
*to_replace_bs
;
3232 AioContext
*replace_aio_context
;
3233 int64_t bs_size
, replace_size
;
3235 bs_size
= bdrv_getlength(bs
);
3237 error_setg_errno(errp
, -bs_size
, "Failed to query device's size");
3241 to_replace_bs
= check_to_replace_node(bs
, replaces
, errp
);
3242 if (!to_replace_bs
) {
3246 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3247 aio_context_acquire(replace_aio_context
);
3248 replace_size
= bdrv_getlength(to_replace_bs
);
3249 aio_context_release(replace_aio_context
);
3251 if (replace_size
< 0) {
3252 error_setg_errno(errp
, -replace_size
,
3253 "Failed to query the replacement node's size");
3256 if (bs_size
!= replace_size
) {
3257 error_setg(errp
, "cannot replace image with a mirror image of "
3263 /* pass the node name to replace to mirror start since it's loose coupling
3264 * and will allow to check whether the node still exist at mirror completion
3266 mirror_start(job_id
, bs
, target
,
3267 has_replaces
? replaces
: NULL
, job_flags
,
3268 speed
, granularity
, buf_size
, sync
, backing_mode
, zero_target
,
3269 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3273 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3275 BlockDriverState
*bs
;
3276 BlockDriverState
*source
, *target_bs
;
3277 AioContext
*aio_context
;
3278 AioContext
*old_context
;
3279 BlockMirrorBackingMode backing_mode
;
3280 Error
*local_err
= NULL
;
3281 QDict
*options
= NULL
;
3284 const char *format
= arg
->format
;
3288 bs
= qmp_get_root_bs(arg
->device
, errp
);
3293 /* Early check to avoid creating target */
3294 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3298 aio_context
= bdrv_get_aio_context(bs
);
3299 aio_context_acquire(aio_context
);
3301 if (!arg
->has_mode
) {
3302 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3305 if (!arg
->has_format
) {
3306 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3307 ? NULL
: bs
->drv
->format_name
);
3310 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3311 source
= backing_bs(bs
);
3312 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3313 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3315 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3319 size
= bdrv_getlength(bs
);
3321 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3325 if (arg
->has_replaces
) {
3326 if (!arg
->has_node_name
) {
3327 error_setg(errp
, "a node-name must be provided when replacing a"
3328 " named node of the graph");
3333 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3334 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3336 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3339 /* Don't open backing image in create() */
3340 flags
|= BDRV_O_NO_BACKING
;
3342 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3343 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3345 /* create new image w/o backing file */
3347 bdrv_img_create(arg
->target
, format
,
3348 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3350 switch (arg
->mode
) {
3351 case NEW_IMAGE_MODE_EXISTING
:
3353 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3354 /* create new image with backing file */
3355 bdrv_refresh_filename(source
);
3356 bdrv_img_create(arg
->target
, format
,
3358 source
->drv
->format_name
,
3359 NULL
, size
, flags
, false, &local_err
);
3367 error_propagate(errp
, local_err
);
3371 options
= qdict_new();
3372 if (arg
->has_node_name
) {
3373 qdict_put_str(options
, "node-name", arg
->node_name
);
3376 qdict_put_str(options
, "driver", format
);
3379 /* Mirroring takes care of copy-on-write using the source's backing
3382 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3387 zero_target
= (arg
->sync
== MIRROR_SYNC_MODE_FULL
&&
3388 (arg
->mode
== NEW_IMAGE_MODE_EXISTING
||
3389 !bdrv_has_zero_init(target_bs
)));
3392 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3393 old_context
= bdrv_get_aio_context(target_bs
);
3394 aio_context_release(aio_context
);
3395 aio_context_acquire(old_context
);
3397 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3399 bdrv_unref(target_bs
);
3400 aio_context_release(old_context
);
3404 aio_context_release(old_context
);
3405 aio_context_acquire(aio_context
);
3407 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3408 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3409 backing_mode
, zero_target
,
3410 arg
->has_speed
, arg
->speed
,
3411 arg
->has_granularity
, arg
->granularity
,
3412 arg
->has_buf_size
, arg
->buf_size
,
3413 arg
->has_on_source_error
, arg
->on_source_error
,
3414 arg
->has_on_target_error
, arg
->on_target_error
,
3415 arg
->has_unmap
, arg
->unmap
,
3417 arg
->has_copy_mode
, arg
->copy_mode
,
3418 arg
->has_auto_finalize
, arg
->auto_finalize
,
3419 arg
->has_auto_dismiss
, arg
->auto_dismiss
,
3421 bdrv_unref(target_bs
);
3422 error_propagate(errp
, local_err
);
3424 aio_context_release(aio_context
);
3427 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3428 const char *device
, const char *target
,
3429 bool has_replaces
, const char *replaces
,
3430 MirrorSyncMode sync
,
3431 bool has_speed
, int64_t speed
,
3432 bool has_granularity
, uint32_t granularity
,
3433 bool has_buf_size
, int64_t buf_size
,
3434 bool has_on_source_error
,
3435 BlockdevOnError on_source_error
,
3436 bool has_on_target_error
,
3437 BlockdevOnError on_target_error
,
3438 bool has_filter_node_name
,
3439 const char *filter_node_name
,
3440 bool has_copy_mode
, MirrorCopyMode copy_mode
,
3441 bool has_auto_finalize
, bool auto_finalize
,
3442 bool has_auto_dismiss
, bool auto_dismiss
,
3445 BlockDriverState
*bs
;
3446 BlockDriverState
*target_bs
;
3447 AioContext
*aio_context
;
3448 AioContext
*old_context
;
3449 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3450 Error
*local_err
= NULL
;
3454 bs
= qmp_get_root_bs(device
, errp
);
3459 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3464 zero_target
= (sync
== MIRROR_SYNC_MODE_FULL
);
3466 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3467 old_context
= bdrv_get_aio_context(target_bs
);
3468 aio_context
= bdrv_get_aio_context(bs
);
3469 aio_context_acquire(old_context
);
3471 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3473 aio_context_release(old_context
);
3474 aio_context_acquire(aio_context
);
3480 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3481 has_replaces
, replaces
, sync
, backing_mode
,
3482 zero_target
, has_speed
, speed
,
3483 has_granularity
, granularity
,
3484 has_buf_size
, buf_size
,
3485 has_on_source_error
, on_source_error
,
3486 has_on_target_error
, on_target_error
,
3488 has_filter_node_name
, filter_node_name
,
3489 has_copy_mode
, copy_mode
,
3490 has_auto_finalize
, auto_finalize
,
3491 has_auto_dismiss
, auto_dismiss
,
3493 error_propagate(errp
, local_err
);
3495 aio_context_release(aio_context
);
3498 /* Get a block job using its ID and acquire its AioContext */
3499 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3506 *aio_context
= NULL
;
3508 job
= block_job_get(id
);
3511 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3512 "Block job '%s' not found", id
);
3516 *aio_context
= blk_get_aio_context(job
->blk
);
3517 aio_context_acquire(*aio_context
);
3522 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3524 AioContext
*aio_context
;
3525 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3531 block_job_set_speed(job
, speed
, errp
);
3532 aio_context_release(aio_context
);
3535 void qmp_block_job_cancel(const char *device
,
3536 bool has_force
, bool force
, Error
**errp
)
3538 AioContext
*aio_context
;
3539 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3549 if (job_user_paused(&job
->job
) && !force
) {
3550 error_setg(errp
, "The block job for device '%s' is currently paused",
3555 trace_qmp_block_job_cancel(job
);
3556 job_user_cancel(&job
->job
, force
, errp
);
3558 aio_context_release(aio_context
);
3561 void qmp_block_job_pause(const char *device
, Error
**errp
)
3563 AioContext
*aio_context
;
3564 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3570 trace_qmp_block_job_pause(job
);
3571 job_user_pause(&job
->job
, errp
);
3572 aio_context_release(aio_context
);
3575 void qmp_block_job_resume(const char *device
, Error
**errp
)
3577 AioContext
*aio_context
;
3578 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3584 trace_qmp_block_job_resume(job
);
3585 job_user_resume(&job
->job
, errp
);
3586 aio_context_release(aio_context
);
3589 void qmp_block_job_complete(const char *device
, Error
**errp
)
3591 AioContext
*aio_context
;
3592 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3598 trace_qmp_block_job_complete(job
);
3599 job_complete(&job
->job
, errp
);
3600 aio_context_release(aio_context
);
3603 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3605 AioContext
*aio_context
;
3606 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3612 trace_qmp_block_job_finalize(job
);
3614 job_finalize(&job
->job
, errp
);
3617 * Job's context might have changed via job_finalize (and job_txn_apply
3618 * automatically acquires the new one), so make sure we release the correct
3621 aio_context
= blk_get_aio_context(job
->blk
);
3622 job_unref(&job
->job
);
3623 aio_context_release(aio_context
);
3626 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3628 AioContext
*aio_context
;
3629 BlockJob
*bjob
= find_block_job(id
, &aio_context
, errp
);
3636 trace_qmp_block_job_dismiss(bjob
);
3638 job_dismiss(&job
, errp
);
3639 aio_context_release(aio_context
);
3642 void qmp_change_backing_file(const char *device
,
3643 const char *image_node_name
,
3644 const char *backing_file
,
3647 BlockDriverState
*bs
= NULL
;
3648 AioContext
*aio_context
;
3649 BlockDriverState
*image_bs
= NULL
;
3650 Error
*local_err
= NULL
;
3654 bs
= qmp_get_root_bs(device
, errp
);
3659 aio_context
= bdrv_get_aio_context(bs
);
3660 aio_context_acquire(aio_context
);
3662 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3664 error_propagate(errp
, local_err
);
3669 error_setg(errp
, "image file not found");
3673 if (bdrv_find_base(image_bs
) == image_bs
) {
3674 error_setg(errp
, "not allowing backing file change on an image "
3675 "without a backing file");
3679 /* even though we are not necessarily operating on bs, we need it to
3680 * determine if block ops are currently prohibited on the chain */
3681 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3685 /* final sanity check */
3686 if (!bdrv_chain_contains(bs
, image_bs
)) {
3687 error_setg(errp
, "'%s' and image file are not in the same chain",
3692 /* if not r/w, reopen to make r/w */
3693 ro
= bdrv_is_read_only(image_bs
);
3696 if (bdrv_reopen_set_read_only(image_bs
, false, errp
) != 0) {
3701 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3702 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3705 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3707 /* don't exit here, so we can try to restore open flags if
3712 bdrv_reopen_set_read_only(image_bs
, true, &local_err
);
3713 error_propagate(errp
, local_err
);
3717 aio_context_release(aio_context
);
3720 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3722 BlockDriverState
*bs
;
3724 Visitor
*v
= qobject_output_visitor_new(&obj
);
3727 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3728 visit_complete(v
, &obj
);
3729 qdict
= qobject_to(QDict
, obj
);
3731 qdict_flatten(qdict
);
3733 if (!qdict_get_try_str(qdict
, "node-name")) {
3734 error_setg(errp
, "'node-name' must be specified for the root node");
3738 bs
= bds_tree_init(qdict
, errp
);
3743 bdrv_set_monitor_owned(bs
);
3749 void qmp_x_blockdev_reopen(BlockdevOptions
*options
, Error
**errp
)
3751 BlockDriverState
*bs
;
3754 Visitor
*v
= qobject_output_visitor_new(&obj
);
3755 BlockReopenQueue
*queue
;
3758 /* Check for the selected node name */
3759 if (!options
->has_node_name
) {
3760 error_setg(errp
, "Node name not specified");
3764 bs
= bdrv_find_node(options
->node_name
);
3766 error_setg(errp
, "Cannot find node named '%s'", options
->node_name
);
3770 /* Put all options in a QDict and flatten it */
3771 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3772 visit_complete(v
, &obj
);
3773 qdict
= qobject_to(QDict
, obj
);
3775 qdict_flatten(qdict
);
3777 /* Perform the reopen operation */
3778 ctx
= bdrv_get_aio_context(bs
);
3779 aio_context_acquire(ctx
);
3780 bdrv_subtree_drained_begin(bs
);
3781 queue
= bdrv_reopen_queue(NULL
, bs
, qdict
, false);
3782 bdrv_reopen_multiple(queue
, errp
);
3783 bdrv_subtree_drained_end(bs
);
3784 aio_context_release(ctx
);
3790 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3792 AioContext
*aio_context
;
3793 BlockDriverState
*bs
;
3795 bs
= bdrv_find_node(node_name
);
3797 error_setg(errp
, "Cannot find node %s", node_name
);
3800 if (bdrv_has_blk(bs
)) {
3801 error_setg(errp
, "Node %s is in use", node_name
);
3804 aio_context
= bdrv_get_aio_context(bs
);
3805 aio_context_acquire(aio_context
);
3807 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3811 if (!QTAILQ_IN_USE(bs
, monitor_list
)) {
3812 error_setg(errp
, "Node %s is not owned by the monitor",
3817 if (bs
->refcnt
> 1) {
3818 error_setg(errp
, "Block device %s is in use",
3819 bdrv_get_device_or_node_name(bs
));
3823 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3827 aio_context_release(aio_context
);
3830 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3831 const char *child_name
)
3835 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3836 if (strcmp(child
->name
, child_name
) == 0) {
3844 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
3845 const char *child
, bool has_node
,
3846 const char *node
, Error
**errp
)
3848 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3851 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3856 if (has_child
== has_node
) {
3858 error_setg(errp
, "The parameters child and node are in conflict");
3860 error_setg(errp
, "Either child or node must be specified");
3866 p_child
= bdrv_find_child(parent_bs
, child
);
3868 error_setg(errp
, "Node '%s' does not have child '%s'",
3872 bdrv_del_child(parent_bs
, p_child
, errp
);
3876 new_bs
= bdrv_find_node(node
);
3878 error_setg(errp
, "Node '%s' not found", node
);
3881 bdrv_add_child(parent_bs
, new_bs
, errp
);
3885 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3887 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3890 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
3891 BlockJobInfoList
*elem
;
3892 AioContext
*aio_context
;
3894 if (block_job_is_internal(job
)) {
3897 elem
= g_new0(BlockJobInfoList
, 1);
3898 aio_context
= blk_get_aio_context(job
->blk
);
3899 aio_context_acquire(aio_context
);
3900 elem
->value
= block_job_query(job
, errp
);
3901 aio_context_release(aio_context
);
3904 qapi_free_BlockJobInfoList(head
);
3908 p_next
= &elem
->next
;
3914 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
3915 bool has_force
, bool force
, Error
**errp
)
3917 AioContext
*old_context
;
3918 AioContext
*new_context
;
3919 BlockDriverState
*bs
;
3921 bs
= bdrv_find_node(node_name
);
3923 error_setg(errp
, "Cannot find node %s", node_name
);
3927 /* Protects against accidents. */
3928 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
3929 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
3930 "be in use (use force=true to override this check)",
3935 if (iothread
->type
== QTYPE_QSTRING
) {
3936 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
3938 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
3942 new_context
= iothread_get_aio_context(obj
);
3944 new_context
= qemu_get_aio_context();
3947 old_context
= bdrv_get_aio_context(bs
);
3948 aio_context_acquire(old_context
);
3950 bdrv_try_set_aio_context(bs
, new_context
, errp
);
3952 aio_context_release(old_context
);
3955 QemuOptsList qemu_common_drive_opts
= {
3957 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3961 .type
= QEMU_OPT_BOOL
,
3962 .help
= "enable/disable snapshot mode",
3965 .type
= QEMU_OPT_STRING
,
3966 .help
= "host AIO implementation (threads, native, io_uring)",
3968 .name
= BDRV_OPT_CACHE_WB
,
3969 .type
= QEMU_OPT_BOOL
,
3970 .help
= "Enable writeback mode",
3973 .type
= QEMU_OPT_STRING
,
3974 .help
= "disk format (raw, qcow2, ...)",
3977 .type
= QEMU_OPT_STRING
,
3978 .help
= "read error action",
3981 .type
= QEMU_OPT_STRING
,
3982 .help
= "write error action",
3984 .name
= BDRV_OPT_READ_ONLY
,
3985 .type
= QEMU_OPT_BOOL
,
3986 .help
= "open drive file as read-only",
3992 .name
= "throttling.group",
3993 .type
= QEMU_OPT_STRING
,
3994 .help
= "name of the block throttling group",
3996 .name
= "copy-on-read",
3997 .type
= QEMU_OPT_BOOL
,
3998 .help
= "copy read data from backing file into image file",
4000 .name
= "detect-zeroes",
4001 .type
= QEMU_OPT_STRING
,
4002 .help
= "try to optimize zero writes (off, on, unmap)",
4004 .name
= "stats-account-invalid",
4005 .type
= QEMU_OPT_BOOL
,
4006 .help
= "whether to account for invalid I/O operations "
4007 "in the statistics",
4009 .name
= "stats-account-failed",
4010 .type
= QEMU_OPT_BOOL
,
4011 .help
= "whether to account for failed I/O operations "
4012 "in the statistics",
4014 { /* end of list */ }
4018 QemuOptsList qemu_drive_opts
= {
4020 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4023 * no elements => accept any params
4024 * validation will happen later
4026 { /* end of list */ }