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_mark_claimed_by_board(void)
247 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
248 dinfo
= blk_legacy_dinfo(blk
);
249 if (dinfo
&& blk_get_attached_dev(blk
)) {
250 dinfo
->claimed_by_board
= true;
255 void drive_check_orphaned(void)
260 bool orphans
= false;
262 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
263 dinfo
= blk_legacy_dinfo(blk
);
264 if (dinfo
->is_default
|| dinfo
->type
== IF_NONE
) {
267 if (!blk_get_attached_dev(blk
)) {
269 qemu_opts_loc_restore(dinfo
->opts
);
270 error_report("machine type does not support"
271 " if=%s,bus=%d,unit=%d",
272 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
277 if (!dinfo
->claimed_by_board
&& dinfo
->type
!= IF_VIRTIO
) {
279 qemu_opts_loc_restore(dinfo
->opts
);
280 warn_report("bogus if=%s is deprecated, use if=none",
281 if_name
[dinfo
->type
]);
291 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
293 return drive_get(type
,
294 drive_index_to_bus_id(type
, index
),
295 drive_index_to_unit_id(type
, index
));
298 int drive_get_max_bus(BlockInterfaceType type
)
305 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
306 dinfo
= blk_legacy_dinfo(blk
);
307 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
308 max_bus
= dinfo
->bus
;
314 /* Get a block device. This should only be used for single-drive devices
315 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
317 DriveInfo
*drive_get_next(BlockInterfaceType type
)
319 static int next_block_unit
[IF_COUNT
];
321 return drive_get(type
, 0, next_block_unit
[type
]++);
324 static void bdrv_format_print(void *opaque
, const char *name
)
326 qemu_printf(" %s", name
);
331 BlockDriverState
*bs
;
334 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
336 if (!strcmp(buf
, "ignore")) {
337 return BLOCKDEV_ON_ERROR_IGNORE
;
338 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
339 return BLOCKDEV_ON_ERROR_ENOSPC
;
340 } else if (!strcmp(buf
, "stop")) {
341 return BLOCKDEV_ON_ERROR_STOP
;
342 } else if (!strcmp(buf
, "report")) {
343 return BLOCKDEV_ON_ERROR_REPORT
;
345 error_setg(errp
, "'%s' invalid %s error action",
346 buf
, is_read
? "read" : "write");
351 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
354 const QListEntry
*entry
;
355 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
356 switch (qobject_type(entry
->value
)) {
358 case QTYPE_QSTRING
: {
359 unsigned long long length
;
360 const char *str
= qstring_get_str(qobject_to(QString
,
362 if (parse_uint_full(str
, &length
, 10) == 0 &&
363 length
> 0 && length
<= UINT_MAX
) {
364 block_acct_add_interval(stats
, (unsigned) length
);
366 error_setg(errp
, "Invalid interval length: %s", str
);
373 int64_t length
= qnum_get_int(qobject_to(QNum
, entry
->value
));
375 if (length
> 0 && length
<= UINT_MAX
) {
376 block_acct_add_interval(stats
, (unsigned) length
);
378 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
385 error_setg(errp
, "The specification of stats-intervals is invalid");
392 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
394 /* All parameters but @opts are optional and may be set to NULL. */
395 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
396 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
397 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
399 Error
*local_error
= NULL
;
403 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
404 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
407 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
408 if (bdrv_parse_aio(aio
, bdrv_flags
) < 0) {
409 error_setg(errp
, "invalid aio option");
415 /* disk I/O throttling */
416 if (throttling_group
) {
417 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
421 throttle_config_init(throttle_cfg
);
422 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
423 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
424 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
425 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
426 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
427 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
428 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
429 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
430 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
431 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
432 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
433 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
435 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
436 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
437 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
438 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
439 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
440 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
441 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
442 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
443 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
444 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
445 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
446 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
448 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
449 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
450 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
451 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
452 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
453 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
454 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
455 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
456 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
457 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
458 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
459 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
461 throttle_cfg
->op_size
=
462 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
464 if (!throttle_is_valid(throttle_cfg
, errp
)) {
471 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
472 qemu_opt_get(opts
, "detect-zeroes"),
473 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
476 error_propagate(errp
, local_error
);
482 /* Takes the ownership of bs_opts */
483 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
488 int on_read_error
, on_write_error
;
489 bool account_invalid
, account_failed
;
490 bool writethrough
, read_only
;
492 BlockDriverState
*bs
;
497 QDict
*interval_dict
= NULL
;
498 QList
*interval_list
= NULL
;
500 BlockdevDetectZeroesOptions detect_zeroes
=
501 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
502 const char *throttling_group
= NULL
;
504 /* Check common options by copying from bs_opts to opts, all other options
505 * stay in bs_opts for processing by bdrv_open(). */
506 id
= qdict_get_try_str(bs_opts
, "id");
507 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, errp
);
512 if (!qemu_opts_absorb_qdict(opts
, bs_opts
, errp
)) {
517 qdict_del(bs_opts
, "id");
520 /* extract parameters */
521 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
523 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
524 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
526 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
528 id
= qemu_opts_id(opts
);
530 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
531 qdict_array_split(interval_dict
, &interval_list
);
533 if (qdict_size(interval_dict
) != 0) {
534 error_setg(errp
, "Invalid option stats-intervals.%s",
535 qdict_first(interval_dict
)->key
);
539 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
540 &detect_zeroes
, &error
);
542 error_propagate(errp
, error
);
546 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
547 if (is_help_option(buf
)) {
548 qemu_printf("Supported formats:");
549 bdrv_iterate_format(bdrv_format_print
, NULL
, false);
550 qemu_printf("\nSupported formats (read-only):");
551 bdrv_iterate_format(bdrv_format_print
, NULL
, true);
556 if (qdict_haskey(bs_opts
, "driver")) {
557 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
560 qdict_put_str(bs_opts
, "driver", buf
);
563 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
564 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
565 on_write_error
= parse_block_error_action(buf
, 0, &error
);
567 error_propagate(errp
, error
);
572 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
573 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
574 on_read_error
= parse_block_error_action(buf
, 1, &error
);
576 error_propagate(errp
, error
);
582 bdrv_flags
|= BDRV_O_SNAPSHOT
;
585 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
588 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
589 BlockBackendRootState
*blk_rs
;
591 blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
592 blk_rs
= blk_get_root_state(blk
);
593 blk_rs
->open_flags
= bdrv_flags
;
594 blk_rs
->read_only
= read_only
;
595 blk_rs
->detect_zeroes
= detect_zeroes
;
597 qobject_unref(bs_opts
);
599 if (file
&& !*file
) {
603 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
604 * with other callers) rather than what we want as the real defaults.
605 * Apply the defaults here instead. */
606 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
607 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
608 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
609 read_only
? "on" : "off");
610 qdict_set_default_str(bs_opts
, BDRV_OPT_AUTO_READ_ONLY
, "on");
611 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
613 if (runstate_check(RUN_STATE_INMIGRATE
)) {
614 bdrv_flags
|= BDRV_O_INACTIVE
;
617 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
623 bs
->detect_zeroes
= detect_zeroes
;
625 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
627 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
634 /* disk I/O throttling */
635 if (throttle_enabled(&cfg
)) {
636 if (!throttling_group
) {
637 throttling_group
= id
;
639 blk_io_limits_enable(blk
, throttling_group
);
640 blk_set_io_limits(blk
, &cfg
);
643 blk_set_enable_write_cache(blk
, !writethrough
);
644 blk_set_on_error(blk
, on_read_error
, on_write_error
);
646 if (!monitor_add_blk(blk
, id
, errp
)) {
654 qobject_unref(interval_dict
);
655 qobject_unref(interval_list
);
660 qobject_unref(interval_dict
);
661 qobject_unref(interval_list
);
663 qobject_unref(bs_opts
);
667 /* Takes the ownership of bs_opts */
668 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
672 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
673 * with other callers) rather than what we want as the real defaults.
674 * Apply the defaults here instead. */
675 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
676 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
677 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
679 if (runstate_check(RUN_STATE_INMIGRATE
)) {
680 bdrv_flags
|= BDRV_O_INACTIVE
;
683 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
686 void blockdev_close_all_bdrv_states(void)
688 BlockDriverState
*bs
, *next_bs
;
690 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
691 AioContext
*ctx
= bdrv_get_aio_context(bs
);
693 aio_context_acquire(ctx
);
695 aio_context_release(ctx
);
699 /* Iterates over the list of monitor-owned BlockDriverStates */
700 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
702 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
703 : QTAILQ_FIRST(&monitor_bdrv_states
);
706 static bool qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
711 value
= qemu_opt_get(opts
, from
);
713 if (qemu_opt_find(opts
, to
)) {
714 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
715 "same time", to
, from
);
720 /* rename all items in opts */
721 while ((value
= qemu_opt_get(opts
, from
))) {
722 qemu_opt_set(opts
, to
, value
, &error_abort
);
723 qemu_opt_unset(opts
, from
);
728 QemuOptsList qemu_legacy_drive_opts
= {
730 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
734 .type
= QEMU_OPT_NUMBER
,
735 .help
= "bus number",
738 .type
= QEMU_OPT_NUMBER
,
739 .help
= "unit number (i.e. lun for scsi)",
742 .type
= QEMU_OPT_NUMBER
,
743 .help
= "index number",
746 .type
= QEMU_OPT_STRING
,
747 .help
= "media type (disk, cdrom)",
750 .type
= QEMU_OPT_STRING
,
751 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
754 .type
= QEMU_OPT_STRING
,
758 /* Options that are passed on, but have special semantics with -drive */
760 .name
= BDRV_OPT_READ_ONLY
,
761 .type
= QEMU_OPT_BOOL
,
762 .help
= "open drive file as read-only",
765 .type
= QEMU_OPT_STRING
,
766 .help
= "read error action",
769 .type
= QEMU_OPT_STRING
,
770 .help
= "write error action",
772 .name
= "copy-on-read",
773 .type
= QEMU_OPT_BOOL
,
774 .help
= "copy read data from backing file into image file",
777 { /* end of list */ }
781 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
,
786 DriveInfo
*dinfo
= NULL
;
788 QemuOpts
*legacy_opts
;
789 DriveMediaType media
= MEDIA_DISK
;
790 BlockInterfaceType type
;
791 int max_devs
, bus_id
, unit_id
, index
;
792 const char *werror
, *rerror
;
793 bool read_only
= false;
795 const char *filename
;
796 Error
*local_err
= NULL
;
799 /* Change legacy command line options into QMP ones */
800 static const struct {
804 { "iops", "throttling.iops-total" },
805 { "iops_rd", "throttling.iops-read" },
806 { "iops_wr", "throttling.iops-write" },
808 { "bps", "throttling.bps-total" },
809 { "bps_rd", "throttling.bps-read" },
810 { "bps_wr", "throttling.bps-write" },
812 { "iops_max", "throttling.iops-total-max" },
813 { "iops_rd_max", "throttling.iops-read-max" },
814 { "iops_wr_max", "throttling.iops-write-max" },
816 { "bps_max", "throttling.bps-total-max" },
817 { "bps_rd_max", "throttling.bps-read-max" },
818 { "bps_wr_max", "throttling.bps-write-max" },
820 { "iops_size", "throttling.iops-size" },
822 { "group", "throttling.group" },
824 { "readonly", BDRV_OPT_READ_ONLY
},
827 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
828 if (!qemu_opt_rename(all_opts
, opt_renames
[i
].from
,
829 opt_renames
[i
].to
, errp
)) {
834 value
= qemu_opt_get(all_opts
, "cache");
839 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
840 error_setg(errp
, "invalid cache option");
844 /* Specific options take precedence */
845 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
846 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
847 !writethrough
, &error_abort
);
849 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
850 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
851 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
853 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
854 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
855 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
857 qemu_opt_unset(all_opts
, "cache");
860 /* Get a QDict for processing the options */
861 bs_opts
= qdict_new();
862 qemu_opts_to_qdict(all_opts
, bs_opts
);
864 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
866 if (!qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, errp
)) {
871 value
= qemu_opt_get(legacy_opts
, "media");
873 if (!strcmp(value
, "disk")) {
875 } else if (!strcmp(value
, "cdrom")) {
879 error_setg(errp
, "'%s' invalid media", value
);
884 /* copy-on-read is disabled with a warning for read-only devices */
885 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
886 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
888 if (read_only
&& copy_on_read
) {
889 warn_report("disabling copy-on-read on read-only drive");
890 copy_on_read
= false;
893 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
894 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
896 /* Controller type */
897 value
= qemu_opt_get(legacy_opts
, "if");
900 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
903 if (type
== IF_COUNT
) {
904 error_setg(errp
, "unsupported bus type '%s'", value
);
908 type
= block_default_type
;
911 /* Device address specified by bus/unit or index.
912 * If none was specified, try to find the first free one. */
913 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
914 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
915 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
917 max_devs
= if_max_devs
[type
];
920 if (bus_id
!= 0 || unit_id
!= -1) {
921 error_setg(errp
, "index cannot be used with bus and unit");
924 bus_id
= drive_index_to_bus_id(type
, index
);
925 unit_id
= drive_index_to_unit_id(type
, index
);
930 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
932 if (max_devs
&& unit_id
>= max_devs
) {
939 if (max_devs
&& unit_id
>= max_devs
) {
940 error_setg(errp
, "unit %d too big (max is %d)", unit_id
, max_devs
- 1);
944 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
945 error_setg(errp
, "drive with bus=%d, unit=%d (index=%d) exists",
946 bus_id
, unit_id
, index
);
950 /* no id supplied -> create one */
951 if (qemu_opts_id(all_opts
) == NULL
) {
953 const char *mediastr
= "";
954 if (type
== IF_IDE
|| type
== IF_SCSI
) {
955 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
958 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
961 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
964 qdict_put_str(bs_opts
, "id", new_id
);
968 /* Add virtio block device */
969 if (type
== IF_VIRTIO
) {
971 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
973 if (arch_type
== QEMU_ARCH_S390X
) {
974 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
976 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
978 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
982 filename
= qemu_opt_get(legacy_opts
, "file");
984 /* Check werror/rerror compatibility with if=... */
985 werror
= qemu_opt_get(legacy_opts
, "werror");
986 if (werror
!= NULL
) {
987 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
989 error_setg(errp
, "werror is not supported by this bus type");
992 qdict_put_str(bs_opts
, "werror", werror
);
995 rerror
= qemu_opt_get(legacy_opts
, "rerror");
996 if (rerror
!= NULL
) {
997 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
999 error_setg(errp
, "rerror is not supported by this bus type");
1002 qdict_put_str(bs_opts
, "rerror", rerror
);
1005 /* Actual block device init: Functionality shared with blockdev-add */
1006 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1009 error_propagate(errp
, local_err
);
1015 /* Create legacy DriveInfo */
1016 dinfo
= g_malloc0(sizeof(*dinfo
));
1017 dinfo
->opts
= all_opts
;
1020 dinfo
->bus
= bus_id
;
1021 dinfo
->unit
= unit_id
;
1023 blk_set_legacy_dinfo(blk
, dinfo
);
1030 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1037 qemu_opts_del(legacy_opts
);
1038 qobject_unref(bs_opts
);
1042 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1044 BlockDriverState
*bs
;
1046 bs
= bdrv_lookup_bs(name
, name
, errp
);
1051 if (!bdrv_is_root_node(bs
)) {
1052 error_setg(errp
, "Need a root block node");
1056 if (!bdrv_is_inserted(bs
)) {
1057 error_setg(errp
, "Device has no medium");
1064 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1066 TransactionActionList list
;
1068 list
.value
= action
;
1070 qmp_transaction(&list
, false, NULL
, errp
);
1073 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1074 bool has_node_name
, const char *node_name
,
1075 const char *snapshot_file
,
1076 bool has_snapshot_node_name
,
1077 const char *snapshot_node_name
,
1078 bool has_format
, const char *format
,
1079 bool has_mode
, NewImageMode mode
, Error
**errp
)
1081 BlockdevSnapshotSync snapshot
= {
1082 .has_device
= has_device
,
1083 .device
= (char *) device
,
1084 .has_node_name
= has_node_name
,
1085 .node_name
= (char *) node_name
,
1086 .snapshot_file
= (char *) snapshot_file
,
1087 .has_snapshot_node_name
= has_snapshot_node_name
,
1088 .snapshot_node_name
= (char *) snapshot_node_name
,
1089 .has_format
= has_format
,
1090 .format
= (char *) format
,
1091 .has_mode
= has_mode
,
1094 TransactionAction action
= {
1095 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1096 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1098 blockdev_do_action(&action
, errp
);
1101 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1104 BlockdevSnapshot snapshot_data
= {
1105 .node
= (char *) node
,
1106 .overlay
= (char *) overlay
1108 TransactionAction action
= {
1109 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1110 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1112 blockdev_do_action(&action
, errp
);
1115 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1119 BlockdevSnapshotInternal snapshot
= {
1120 .device
= (char *) device
,
1121 .name
= (char *) name
1123 TransactionAction action
= {
1124 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1125 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1127 blockdev_do_action(&action
, errp
);
1130 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1137 BlockDriverState
*bs
;
1138 AioContext
*aio_context
;
1139 QEMUSnapshotInfo sn
;
1140 Error
*local_err
= NULL
;
1141 SnapshotInfo
*info
= NULL
;
1144 bs
= qmp_get_root_bs(device
, errp
);
1148 aio_context
= bdrv_get_aio_context(bs
);
1149 aio_context_acquire(aio_context
);
1160 error_setg(errp
, "Name or id must be provided");
1161 goto out_aio_context
;
1164 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1165 goto out_aio_context
;
1168 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1170 error_propagate(errp
, local_err
);
1171 goto out_aio_context
;
1175 "Snapshot with id '%s' and name '%s' does not exist on "
1177 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1178 goto out_aio_context
;
1181 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1183 error_propagate(errp
, local_err
);
1184 goto out_aio_context
;
1187 aio_context_release(aio_context
);
1189 info
= g_new0(SnapshotInfo
, 1);
1190 info
->id
= g_strdup(sn
.id_str
);
1191 info
->name
= g_strdup(sn
.name
);
1192 info
->date_nsec
= sn
.date_nsec
;
1193 info
->date_sec
= sn
.date_sec
;
1194 info
->vm_state_size
= sn
.vm_state_size
;
1195 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1196 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1201 aio_context_release(aio_context
);
1205 /* New and old BlockDriverState structs for atomic group operations */
1207 typedef struct BlkActionState BlkActionState
;
1211 * Table of operations that define an Action.
1213 * @instance_size: Size of state struct, in bytes.
1214 * @prepare: Prepare the work, must NOT be NULL.
1215 * @commit: Commit the changes, can be NULL.
1216 * @abort: Abort the changes on fail, can be NULL.
1217 * @clean: Clean up resources after all transaction actions have called
1218 * commit() or abort(). Can be NULL.
1220 * Only prepare() may fail. In a single transaction, only one of commit() or
1221 * abort() will be called. clean() will always be called if it is present.
1223 typedef struct BlkActionOps
{
1224 size_t instance_size
;
1225 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1226 void (*commit
)(BlkActionState
*common
);
1227 void (*abort
)(BlkActionState
*common
);
1228 void (*clean
)(BlkActionState
*common
);
1233 * Describes one Action's state within a Transaction.
1235 * @action: QAPI-defined enum identifying which Action to perform.
1236 * @ops: Table of ActionOps this Action can perform.
1237 * @block_job_txn: Transaction which this action belongs to.
1238 * @entry: List membership for all Actions in this Transaction.
1240 * This structure must be arranged as first member in a subclassed type,
1241 * assuming that the compiler will also arrange it to the same offsets as the
1244 struct BlkActionState
{
1245 TransactionAction
*action
;
1246 const BlkActionOps
*ops
;
1247 JobTxn
*block_job_txn
;
1248 TransactionProperties
*txn_props
;
1249 QTAILQ_ENTRY(BlkActionState
) entry
;
1252 /* internal snapshot private data */
1253 typedef struct InternalSnapshotState
{
1254 BlkActionState common
;
1255 BlockDriverState
*bs
;
1256 QEMUSnapshotInfo sn
;
1258 } InternalSnapshotState
;
1261 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1263 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1265 "Action '%s' does not support Transaction property "
1266 "completion-mode = %s",
1267 TransactionActionKind_str(s
->action
->type
),
1268 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1274 static void internal_snapshot_prepare(BlkActionState
*common
,
1277 Error
*local_err
= NULL
;
1280 BlockDriverState
*bs
;
1281 QEMUSnapshotInfo old_sn
, *sn
;
1284 BlockdevSnapshotInternal
*internal
;
1285 InternalSnapshotState
*state
;
1286 AioContext
*aio_context
;
1289 g_assert(common
->action
->type
==
1290 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1291 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1292 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1294 /* 1. parse input */
1295 device
= internal
->device
;
1296 name
= internal
->name
;
1298 /* 2. check for validation */
1299 if (action_check_completion_mode(common
, errp
) < 0) {
1303 bs
= qmp_get_root_bs(device
, errp
);
1308 aio_context
= bdrv_get_aio_context(bs
);
1309 aio_context_acquire(aio_context
);
1313 /* Paired with .clean() */
1314 bdrv_drained_begin(bs
);
1316 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1320 if (bdrv_is_read_only(bs
)) {
1321 error_setg(errp
, "Device '%s' is read only", device
);
1325 if (!bdrv_can_snapshot(bs
)) {
1326 error_setg(errp
, "Block format '%s' used by device '%s' "
1327 "does not support internal snapshots",
1328 bs
->drv
->format_name
, device
);
1332 if (!strlen(name
)) {
1333 error_setg(errp
, "Name is empty");
1337 /* check whether a snapshot with name exist */
1338 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1341 error_propagate(errp
, local_err
);
1345 "Snapshot with name '%s' already exists on device '%s'",
1350 /* 3. take the snapshot */
1352 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1353 qemu_gettimeofday(&tv
);
1354 sn
->date_sec
= tv
.tv_sec
;
1355 sn
->date_nsec
= tv
.tv_usec
* 1000;
1356 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1358 ret1
= bdrv_snapshot_create(bs
, sn
);
1360 error_setg_errno(errp
, -ret1
,
1361 "Failed to create snapshot '%s' on device '%s'",
1366 /* 4. succeed, mark a snapshot is created */
1367 state
->created
= true;
1370 aio_context_release(aio_context
);
1373 static void internal_snapshot_abort(BlkActionState
*common
)
1375 InternalSnapshotState
*state
=
1376 DO_UPCAST(InternalSnapshotState
, common
, common
);
1377 BlockDriverState
*bs
= state
->bs
;
1378 QEMUSnapshotInfo
*sn
= &state
->sn
;
1379 AioContext
*aio_context
;
1380 Error
*local_error
= NULL
;
1382 if (!state
->created
) {
1386 aio_context
= bdrv_get_aio_context(state
->bs
);
1387 aio_context_acquire(aio_context
);
1389 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1390 error_reportf_err(local_error
,
1391 "Failed to delete snapshot with id '%s' and "
1392 "name '%s' on device '%s' in abort: ",
1393 sn
->id_str
, sn
->name
,
1394 bdrv_get_device_name(bs
));
1397 aio_context_release(aio_context
);
1400 static void internal_snapshot_clean(BlkActionState
*common
)
1402 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1404 AioContext
*aio_context
;
1410 aio_context
= bdrv_get_aio_context(state
->bs
);
1411 aio_context_acquire(aio_context
);
1413 bdrv_drained_end(state
->bs
);
1415 aio_context_release(aio_context
);
1418 /* external snapshot private data */
1419 typedef struct ExternalSnapshotState
{
1420 BlkActionState common
;
1421 BlockDriverState
*old_bs
;
1422 BlockDriverState
*new_bs
;
1423 bool overlay_appended
;
1424 } ExternalSnapshotState
;
1426 static void external_snapshot_prepare(BlkActionState
*common
,
1430 QDict
*options
= NULL
;
1431 Error
*local_err
= NULL
;
1432 /* Device and node name of the image to generate the snapshot from */
1434 const char *node_name
;
1435 /* Reference to the new image (for 'blockdev-snapshot') */
1436 const char *snapshot_ref
;
1437 /* File name of the new image (for 'blockdev-snapshot-sync') */
1438 const char *new_image_file
;
1439 ExternalSnapshotState
*state
=
1440 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1441 TransactionAction
*action
= common
->action
;
1442 AioContext
*aio_context
;
1443 uint64_t perm
, shared
;
1445 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1446 * purpose but a different set of parameters */
1447 switch (action
->type
) {
1448 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1450 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1452 node_name
= s
->node
;
1453 new_image_file
= NULL
;
1454 snapshot_ref
= s
->overlay
;
1457 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1459 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1460 device
= s
->has_device
? s
->device
: NULL
;
1461 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1462 new_image_file
= s
->snapshot_file
;
1463 snapshot_ref
= NULL
;
1467 g_assert_not_reached();
1470 /* start processing */
1471 if (action_check_completion_mode(common
, errp
) < 0) {
1475 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1476 if (!state
->old_bs
) {
1480 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1481 aio_context_acquire(aio_context
);
1483 /* Paired with .clean() */
1484 bdrv_drained_begin(state
->old_bs
);
1486 if (!bdrv_is_inserted(state
->old_bs
)) {
1487 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1491 if (bdrv_op_is_blocked(state
->old_bs
,
1492 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1496 if (!bdrv_is_read_only(state
->old_bs
)) {
1497 if (bdrv_flush(state
->old_bs
)) {
1498 error_setg(errp
, QERR_IO_ERROR
);
1503 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1504 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1505 const char *format
= s
->has_format
? s
->format
: "qcow2";
1506 enum NewImageMode mode
;
1507 const char *snapshot_node_name
=
1508 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1510 if (node_name
&& !snapshot_node_name
) {
1511 error_setg(errp
, "New overlay node name missing");
1515 if (snapshot_node_name
&&
1516 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1517 error_setg(errp
, "New overlay node name already in use");
1521 flags
= state
->old_bs
->open_flags
;
1522 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1523 flags
|= BDRV_O_NO_BACKING
;
1525 /* create new image w/backing file */
1526 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1527 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1528 int64_t size
= bdrv_getlength(state
->old_bs
);
1530 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1533 bdrv_refresh_filename(state
->old_bs
);
1534 bdrv_img_create(new_image_file
, format
,
1535 state
->old_bs
->filename
,
1536 state
->old_bs
->drv
->format_name
,
1537 NULL
, size
, flags
, false, &local_err
);
1539 error_propagate(errp
, local_err
);
1544 options
= qdict_new();
1545 if (snapshot_node_name
) {
1546 qdict_put_str(options
, "node-name", snapshot_node_name
);
1548 qdict_put_str(options
, "driver", format
);
1551 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1553 /* We will manually add the backing_hd field to the bs later */
1554 if (!state
->new_bs
) {
1559 * Allow attaching a backing file to an overlay that's already in use only
1560 * if the parents don't assume that they are already seeing a valid image.
1561 * (Specifically, allow it as a mirror target, which is write-only access.)
1563 bdrv_get_cumulative_perm(state
->new_bs
, &perm
, &shared
);
1564 if (perm
& BLK_PERM_CONSISTENT_READ
) {
1565 error_setg(errp
, "The overlay is already in use");
1569 if (state
->new_bs
->backing
!= NULL
) {
1570 error_setg(errp
, "The overlay already has a backing image");
1574 if (!state
->new_bs
->drv
->supports_backing
) {
1575 error_setg(errp
, "The overlay does not support backing images");
1579 /* This removes our old bs and adds the new bs. This is an operation that
1580 * can fail, so we need to do it in .prepare; undoing it for abort is
1581 * always possible. */
1582 bdrv_ref(state
->new_bs
);
1583 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1585 error_propagate(errp
, local_err
);
1588 state
->overlay_appended
= true;
1591 aio_context_release(aio_context
);
1594 static void external_snapshot_commit(BlkActionState
*common
)
1596 ExternalSnapshotState
*state
=
1597 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1598 AioContext
*aio_context
;
1600 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1601 aio_context_acquire(aio_context
);
1603 /* We don't need (or want) to use the transactional
1604 * bdrv_reopen_multiple() across all the entries at once, because we
1605 * don't want to abort all of them if one of them fails the reopen */
1606 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1607 bdrv_reopen_set_read_only(state
->old_bs
, true, NULL
);
1610 aio_context_release(aio_context
);
1613 static void external_snapshot_abort(BlkActionState
*common
)
1615 ExternalSnapshotState
*state
=
1616 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1617 if (state
->new_bs
) {
1618 if (state
->overlay_appended
) {
1619 AioContext
*aio_context
;
1620 AioContext
*tmp_context
;
1623 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1624 aio_context_acquire(aio_context
);
1626 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1627 close state->old_bs; we need it */
1628 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1631 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1632 * the main AioContext. As we're still going to be using it, return
1633 * it to the AioContext it was before.
1635 tmp_context
= bdrv_get_aio_context(state
->old_bs
);
1636 if (aio_context
!= tmp_context
) {
1637 aio_context_release(aio_context
);
1638 aio_context_acquire(tmp_context
);
1640 ret
= bdrv_try_set_aio_context(state
->old_bs
,
1644 aio_context_release(tmp_context
);
1645 aio_context_acquire(aio_context
);
1648 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1649 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1651 aio_context_release(aio_context
);
1656 static void external_snapshot_clean(BlkActionState
*common
)
1658 ExternalSnapshotState
*state
=
1659 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1660 AioContext
*aio_context
;
1662 if (!state
->old_bs
) {
1666 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1667 aio_context_acquire(aio_context
);
1669 bdrv_drained_end(state
->old_bs
);
1670 bdrv_unref(state
->new_bs
);
1672 aio_context_release(aio_context
);
1675 typedef struct DriveBackupState
{
1676 BlkActionState common
;
1677 BlockDriverState
*bs
;
1681 static BlockJob
*do_backup_common(BackupCommon
*backup
,
1682 BlockDriverState
*bs
,
1683 BlockDriverState
*target_bs
,
1684 AioContext
*aio_context
,
1685 JobTxn
*txn
, Error
**errp
);
1687 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1689 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1690 DriveBackup
*backup
;
1691 BlockDriverState
*bs
;
1692 BlockDriverState
*target_bs
;
1693 BlockDriverState
*source
= NULL
;
1694 AioContext
*aio_context
;
1695 AioContext
*old_context
;
1697 Error
*local_err
= NULL
;
1700 bool set_backing_hd
= false;
1703 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1704 backup
= common
->action
->u
.drive_backup
.data
;
1706 if (!backup
->has_mode
) {
1707 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1710 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1716 error_setg(errp
, "Device has no medium");
1720 aio_context
= bdrv_get_aio_context(bs
);
1721 aio_context_acquire(aio_context
);
1723 /* Paired with .clean() */
1724 bdrv_drained_begin(bs
);
1726 if (!backup
->has_format
) {
1727 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
1728 NULL
: (char *) bs
->drv
->format_name
;
1731 /* Early check to avoid creating target */
1732 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
1736 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1739 * See if we have a backing HD we can use to create our new image
1742 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
1743 source
= backing_bs(bs
);
1745 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
1748 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
1750 flags
|= BDRV_O_NO_BACKING
;
1751 set_backing_hd
= true;
1754 size
= bdrv_getlength(bs
);
1756 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1760 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
1761 assert(backup
->format
);
1763 bdrv_refresh_filename(source
);
1764 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
1765 source
->drv
->format_name
, NULL
,
1766 size
, flags
, false, &local_err
);
1768 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
1769 size
, flags
, false, &local_err
);
1774 error_propagate(errp
, local_err
);
1778 options
= qdict_new();
1779 qdict_put_str(options
, "discard", "unmap");
1780 qdict_put_str(options
, "detect-zeroes", "unmap");
1781 if (backup
->format
) {
1782 qdict_put_str(options
, "driver", backup
->format
);
1785 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
1790 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1791 old_context
= bdrv_get_aio_context(target_bs
);
1792 aio_context_release(aio_context
);
1793 aio_context_acquire(old_context
);
1795 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1797 bdrv_unref(target_bs
);
1798 aio_context_release(old_context
);
1802 aio_context_release(old_context
);
1803 aio_context_acquire(aio_context
);
1805 if (set_backing_hd
) {
1806 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
1814 state
->job
= do_backup_common(qapi_DriveBackup_base(backup
),
1815 bs
, target_bs
, aio_context
,
1816 common
->block_job_txn
, errp
);
1819 bdrv_unref(target_bs
);
1821 aio_context_release(aio_context
);
1824 static void drive_backup_commit(BlkActionState
*common
)
1826 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1827 AioContext
*aio_context
;
1829 aio_context
= bdrv_get_aio_context(state
->bs
);
1830 aio_context_acquire(aio_context
);
1833 job_start(&state
->job
->job
);
1835 aio_context_release(aio_context
);
1838 static void drive_backup_abort(BlkActionState
*common
)
1840 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1843 AioContext
*aio_context
;
1845 aio_context
= bdrv_get_aio_context(state
->bs
);
1846 aio_context_acquire(aio_context
);
1848 job_cancel_sync(&state
->job
->job
);
1850 aio_context_release(aio_context
);
1854 static void drive_backup_clean(BlkActionState
*common
)
1856 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1857 AioContext
*aio_context
;
1863 aio_context
= bdrv_get_aio_context(state
->bs
);
1864 aio_context_acquire(aio_context
);
1866 bdrv_drained_end(state
->bs
);
1868 aio_context_release(aio_context
);
1871 typedef struct BlockdevBackupState
{
1872 BlkActionState common
;
1873 BlockDriverState
*bs
;
1875 } BlockdevBackupState
;
1877 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1879 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1880 BlockdevBackup
*backup
;
1881 BlockDriverState
*bs
;
1882 BlockDriverState
*target_bs
;
1883 AioContext
*aio_context
;
1884 AioContext
*old_context
;
1887 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1888 backup
= common
->action
->u
.blockdev_backup
.data
;
1890 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1895 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1900 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1901 aio_context
= bdrv_get_aio_context(bs
);
1902 old_context
= bdrv_get_aio_context(target_bs
);
1903 aio_context_acquire(old_context
);
1905 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1907 aio_context_release(old_context
);
1911 aio_context_release(old_context
);
1912 aio_context_acquire(aio_context
);
1915 /* Paired with .clean() */
1916 bdrv_drained_begin(state
->bs
);
1918 state
->job
= do_backup_common(qapi_BlockdevBackup_base(backup
),
1919 bs
, target_bs
, aio_context
,
1920 common
->block_job_txn
, errp
);
1922 aio_context_release(aio_context
);
1925 static void blockdev_backup_commit(BlkActionState
*common
)
1927 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1928 AioContext
*aio_context
;
1930 aio_context
= bdrv_get_aio_context(state
->bs
);
1931 aio_context_acquire(aio_context
);
1934 job_start(&state
->job
->job
);
1936 aio_context_release(aio_context
);
1939 static void blockdev_backup_abort(BlkActionState
*common
)
1941 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1944 AioContext
*aio_context
;
1946 aio_context
= bdrv_get_aio_context(state
->bs
);
1947 aio_context_acquire(aio_context
);
1949 job_cancel_sync(&state
->job
->job
);
1951 aio_context_release(aio_context
);
1955 static void blockdev_backup_clean(BlkActionState
*common
)
1957 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1958 AioContext
*aio_context
;
1964 aio_context
= bdrv_get_aio_context(state
->bs
);
1965 aio_context_acquire(aio_context
);
1967 bdrv_drained_end(state
->bs
);
1969 aio_context_release(aio_context
);
1972 typedef struct BlockDirtyBitmapState
{
1973 BlkActionState common
;
1974 BdrvDirtyBitmap
*bitmap
;
1975 BlockDriverState
*bs
;
1979 } BlockDirtyBitmapState
;
1981 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1984 Error
*local_err
= NULL
;
1985 BlockDirtyBitmapAdd
*action
;
1986 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1989 if (action_check_completion_mode(common
, errp
) < 0) {
1993 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1994 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1995 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1996 action
->has_granularity
, action
->granularity
,
1997 action
->has_persistent
, action
->persistent
,
1998 action
->has_disabled
, action
->disabled
,
2002 state
->prepared
= true;
2004 error_propagate(errp
, local_err
);
2008 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2010 BlockDirtyBitmapAdd
*action
;
2011 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2014 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2015 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2016 * then the node reference and bitmap name must have been valid.
2018 if (state
->prepared
) {
2019 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2023 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2026 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2028 BlockDirtyBitmap
*action
;
2030 if (action_check_completion_mode(common
, errp
) < 0) {
2034 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2035 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2039 if (!state
->bitmap
) {
2043 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
2047 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2050 static void block_dirty_bitmap_restore(BlkActionState
*common
)
2052 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2055 if (state
->backup
) {
2056 bdrv_restore_dirty_bitmap(state
->bitmap
, state
->backup
);
2060 static void block_dirty_bitmap_free_backup(BlkActionState
*common
)
2062 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2065 hbitmap_free(state
->backup
);
2068 static void block_dirty_bitmap_enable_prepare(BlkActionState
*common
,
2071 BlockDirtyBitmap
*action
;
2072 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2075 if (action_check_completion_mode(common
, errp
) < 0) {
2079 action
= common
->action
->u
.block_dirty_bitmap_enable
.data
;
2080 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2084 if (!state
->bitmap
) {
2088 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2092 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2093 bdrv_enable_dirty_bitmap(state
->bitmap
);
2096 static void block_dirty_bitmap_enable_abort(BlkActionState
*common
)
2098 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2101 if (!state
->was_enabled
) {
2102 bdrv_disable_dirty_bitmap(state
->bitmap
);
2106 static void block_dirty_bitmap_disable_prepare(BlkActionState
*common
,
2109 BlockDirtyBitmap
*action
;
2110 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2113 if (action_check_completion_mode(common
, errp
) < 0) {
2117 action
= common
->action
->u
.block_dirty_bitmap_disable
.data
;
2118 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2122 if (!state
->bitmap
) {
2126 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2130 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2131 bdrv_disable_dirty_bitmap(state
->bitmap
);
2134 static void block_dirty_bitmap_disable_abort(BlkActionState
*common
)
2136 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2139 if (state
->was_enabled
) {
2140 bdrv_enable_dirty_bitmap(state
->bitmap
);
2144 static void block_dirty_bitmap_merge_prepare(BlkActionState
*common
,
2147 BlockDirtyBitmapMerge
*action
;
2148 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2151 if (action_check_completion_mode(common
, errp
) < 0) {
2155 action
= common
->action
->u
.block_dirty_bitmap_merge
.data
;
2157 state
->bitmap
= block_dirty_bitmap_merge(action
->node
, action
->target
,
2158 action
->bitmaps
, &state
->backup
,
2162 static void block_dirty_bitmap_remove_prepare(BlkActionState
*common
,
2165 BlockDirtyBitmap
*action
;
2166 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2169 if (action_check_completion_mode(common
, errp
) < 0) {
2173 action
= common
->action
->u
.block_dirty_bitmap_remove
.data
;
2175 state
->bitmap
= block_dirty_bitmap_remove(action
->node
, action
->name
,
2176 false, &state
->bs
, errp
);
2177 if (state
->bitmap
) {
2178 bdrv_dirty_bitmap_skip_store(state
->bitmap
, true);
2179 bdrv_dirty_bitmap_set_busy(state
->bitmap
, true);
2183 static void block_dirty_bitmap_remove_abort(BlkActionState
*common
)
2185 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2188 if (state
->bitmap
) {
2189 bdrv_dirty_bitmap_skip_store(state
->bitmap
, false);
2190 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2194 static void block_dirty_bitmap_remove_commit(BlkActionState
*common
)
2196 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2199 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2200 bdrv_release_dirty_bitmap(state
->bitmap
);
2203 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2205 error_setg(errp
, "Transaction aborted using Abort action");
2208 static void abort_commit(BlkActionState
*common
)
2210 g_assert_not_reached(); /* this action never succeeds */
2213 static const BlkActionOps actions
[] = {
2214 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2215 .instance_size
= sizeof(ExternalSnapshotState
),
2216 .prepare
= external_snapshot_prepare
,
2217 .commit
= external_snapshot_commit
,
2218 .abort
= external_snapshot_abort
,
2219 .clean
= external_snapshot_clean
,
2221 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2222 .instance_size
= sizeof(ExternalSnapshotState
),
2223 .prepare
= external_snapshot_prepare
,
2224 .commit
= external_snapshot_commit
,
2225 .abort
= external_snapshot_abort
,
2226 .clean
= external_snapshot_clean
,
2228 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2229 .instance_size
= sizeof(DriveBackupState
),
2230 .prepare
= drive_backup_prepare
,
2231 .commit
= drive_backup_commit
,
2232 .abort
= drive_backup_abort
,
2233 .clean
= drive_backup_clean
,
2235 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2236 .instance_size
= sizeof(BlockdevBackupState
),
2237 .prepare
= blockdev_backup_prepare
,
2238 .commit
= blockdev_backup_commit
,
2239 .abort
= blockdev_backup_abort
,
2240 .clean
= blockdev_backup_clean
,
2242 [TRANSACTION_ACTION_KIND_ABORT
] = {
2243 .instance_size
= sizeof(BlkActionState
),
2244 .prepare
= abort_prepare
,
2245 .commit
= abort_commit
,
2247 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2248 .instance_size
= sizeof(InternalSnapshotState
),
2249 .prepare
= internal_snapshot_prepare
,
2250 .abort
= internal_snapshot_abort
,
2251 .clean
= internal_snapshot_clean
,
2253 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2254 .instance_size
= sizeof(BlockDirtyBitmapState
),
2255 .prepare
= block_dirty_bitmap_add_prepare
,
2256 .abort
= block_dirty_bitmap_add_abort
,
2258 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2259 .instance_size
= sizeof(BlockDirtyBitmapState
),
2260 .prepare
= block_dirty_bitmap_clear_prepare
,
2261 .commit
= block_dirty_bitmap_free_backup
,
2262 .abort
= block_dirty_bitmap_restore
,
2264 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE
] = {
2265 .instance_size
= sizeof(BlockDirtyBitmapState
),
2266 .prepare
= block_dirty_bitmap_enable_prepare
,
2267 .abort
= block_dirty_bitmap_enable_abort
,
2269 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE
] = {
2270 .instance_size
= sizeof(BlockDirtyBitmapState
),
2271 .prepare
= block_dirty_bitmap_disable_prepare
,
2272 .abort
= block_dirty_bitmap_disable_abort
,
2274 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE
] = {
2275 .instance_size
= sizeof(BlockDirtyBitmapState
),
2276 .prepare
= block_dirty_bitmap_merge_prepare
,
2277 .commit
= block_dirty_bitmap_free_backup
,
2278 .abort
= block_dirty_bitmap_restore
,
2280 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE
] = {
2281 .instance_size
= sizeof(BlockDirtyBitmapState
),
2282 .prepare
= block_dirty_bitmap_remove_prepare
,
2283 .commit
= block_dirty_bitmap_remove_commit
,
2284 .abort
= block_dirty_bitmap_remove_abort
,
2286 /* Where are transactions for MIRROR, COMMIT and STREAM?
2287 * Although these blockjobs use transaction callbacks like the backup job,
2288 * these jobs do not necessarily adhere to transaction semantics.
2289 * These jobs may not fully undo all of their actions on abort, nor do they
2290 * necessarily work in transactions with more than one job in them.
2295 * Allocate a TransactionProperties structure if necessary, and fill
2296 * that structure with desired defaults if they are unset.
2298 static TransactionProperties
*get_transaction_properties(
2299 TransactionProperties
*props
)
2302 props
= g_new0(TransactionProperties
, 1);
2305 if (!props
->has_completion_mode
) {
2306 props
->has_completion_mode
= true;
2307 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2314 * 'Atomic' group operations. The operations are performed as a set, and if
2315 * any fail then we roll back all operations in the group.
2317 void qmp_transaction(TransactionActionList
*dev_list
,
2319 struct TransactionProperties
*props
,
2322 TransactionActionList
*dev_entry
= dev_list
;
2323 JobTxn
*block_job_txn
= NULL
;
2324 BlkActionState
*state
, *next
;
2325 Error
*local_err
= NULL
;
2327 QTAILQ_HEAD(, BlkActionState
) snap_bdrv_states
;
2328 QTAILQ_INIT(&snap_bdrv_states
);
2330 /* Does this transaction get canceled as a group on failure?
2331 * If not, we don't really need to make a JobTxn.
2333 props
= get_transaction_properties(props
);
2334 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2335 block_job_txn
= job_txn_new();
2338 /* drain all i/o before any operations */
2341 /* We don't do anything in this loop that commits us to the operations */
2342 while (NULL
!= dev_entry
) {
2343 TransactionAction
*dev_info
= NULL
;
2344 const BlkActionOps
*ops
;
2346 dev_info
= dev_entry
->value
;
2347 dev_entry
= dev_entry
->next
;
2349 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2351 ops
= &actions
[dev_info
->type
];
2352 assert(ops
->instance_size
> 0);
2354 state
= g_malloc0(ops
->instance_size
);
2356 state
->action
= dev_info
;
2357 state
->block_job_txn
= block_job_txn
;
2358 state
->txn_props
= props
;
2359 QTAILQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2361 state
->ops
->prepare(state
, &local_err
);
2363 error_propagate(errp
, local_err
);
2364 goto delete_and_fail
;
2368 QTAILQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2369 if (state
->ops
->commit
) {
2370 state
->ops
->commit(state
);
2378 /* failure, and it is all-or-none; roll back all operations */
2379 QTAILQ_FOREACH_REVERSE(state
, &snap_bdrv_states
, entry
) {
2380 if (state
->ops
->abort
) {
2381 state
->ops
->abort(state
);
2385 QTAILQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2386 if (state
->ops
->clean
) {
2387 state
->ops
->clean(state
);
2392 qapi_free_TransactionProperties(props
);
2394 job_txn_unref(block_job_txn
);
2397 void qmp_block_passwd(bool has_device
, const char *device
,
2398 bool has_node_name
, const char *node_name
,
2399 const char *password
, Error
**errp
)
2402 "Setting block passwords directly is no longer supported");
2405 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2409 BdrvDirtyBitmap
*bitmap
;
2410 BlockDriverState
*bs
;
2411 BlockDirtyBitmapSha256
*ret
= NULL
;
2414 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2415 if (!bitmap
|| !bs
) {
2419 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2420 if (sha256
== NULL
) {
2424 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2425 ret
->sha256
= sha256
;
2430 void qmp_block_resize(bool has_device
, const char *device
,
2431 bool has_node_name
, const char *node_name
,
2432 int64_t size
, Error
**errp
)
2434 Error
*local_err
= NULL
;
2435 BlockBackend
*blk
= NULL
;
2436 BlockDriverState
*bs
;
2437 AioContext
*aio_context
;
2439 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2440 has_node_name
? node_name
: NULL
,
2443 error_propagate(errp
, local_err
);
2447 aio_context
= bdrv_get_aio_context(bs
);
2448 aio_context_acquire(aio_context
);
2451 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2455 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2456 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2460 blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
, errp
);
2465 bdrv_drained_begin(bs
);
2466 blk_truncate(blk
, size
, false, PREALLOC_MODE_OFF
, 0, errp
);
2467 bdrv_drained_end(bs
);
2471 aio_context_release(aio_context
);
2474 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
2475 bool has_base
, const char *base
,
2476 bool has_base_node
, const char *base_node
,
2477 bool has_backing_file
, const char *backing_file
,
2478 bool has_speed
, int64_t speed
,
2479 bool has_on_error
, BlockdevOnError on_error
,
2480 bool has_auto_finalize
, bool auto_finalize
,
2481 bool has_auto_dismiss
, bool auto_dismiss
,
2484 BlockDriverState
*bs
, *iter
;
2485 BlockDriverState
*base_bs
= NULL
;
2486 AioContext
*aio_context
;
2487 Error
*local_err
= NULL
;
2488 const char *base_name
= NULL
;
2489 int job_flags
= JOB_DEFAULT
;
2491 if (!has_on_error
) {
2492 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2495 bs
= bdrv_lookup_bs(device
, device
, errp
);
2500 aio_context
= bdrv_get_aio_context(bs
);
2501 aio_context_acquire(aio_context
);
2503 if (has_base
&& has_base_node
) {
2504 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2505 "at the same time");
2510 base_bs
= bdrv_find_backing_image(bs
, base
);
2511 if (base_bs
== NULL
) {
2512 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2515 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2519 if (has_base_node
) {
2520 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2524 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2525 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2529 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2530 bdrv_refresh_filename(base_bs
);
2531 base_name
= base_bs
->filename
;
2534 /* Check for op blockers in the whole chain between bs and base */
2535 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
2536 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2541 /* if we are streaming the entire chain, the result will have no backing
2542 * file, and specifying one is therefore an error */
2543 if (base_bs
== NULL
&& has_backing_file
) {
2544 error_setg(errp
, "backing file specified, but streaming the "
2549 /* backing_file string overrides base bs filename */
2550 base_name
= has_backing_file
? backing_file
: base_name
;
2552 if (has_auto_finalize
&& !auto_finalize
) {
2553 job_flags
|= JOB_MANUAL_FINALIZE
;
2555 if (has_auto_dismiss
&& !auto_dismiss
) {
2556 job_flags
|= JOB_MANUAL_DISMISS
;
2559 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
2560 job_flags
, has_speed
? speed
: 0, on_error
, &local_err
);
2562 error_propagate(errp
, local_err
);
2566 trace_qmp_block_stream(bs
);
2569 aio_context_release(aio_context
);
2572 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
2573 bool has_base_node
, const char *base_node
,
2574 bool has_base
, const char *base
,
2575 bool has_top_node
, const char *top_node
,
2576 bool has_top
, const char *top
,
2577 bool has_backing_file
, const char *backing_file
,
2578 bool has_speed
, int64_t speed
,
2579 bool has_on_error
, BlockdevOnError on_error
,
2580 bool has_filter_node_name
, const char *filter_node_name
,
2581 bool has_auto_finalize
, bool auto_finalize
,
2582 bool has_auto_dismiss
, bool auto_dismiss
,
2585 BlockDriverState
*bs
;
2586 BlockDriverState
*iter
;
2587 BlockDriverState
*base_bs
, *top_bs
;
2588 AioContext
*aio_context
;
2589 Error
*local_err
= NULL
;
2590 int job_flags
= JOB_DEFAULT
;
2595 if (!has_on_error
) {
2596 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2598 if (!has_filter_node_name
) {
2599 filter_node_name
= NULL
;
2601 if (has_auto_finalize
&& !auto_finalize
) {
2602 job_flags
|= JOB_MANUAL_FINALIZE
;
2604 if (has_auto_dismiss
&& !auto_dismiss
) {
2605 job_flags
|= JOB_MANUAL_DISMISS
;
2609 * libvirt relies on the DeviceNotFound error class in order to probe for
2610 * live commit feature versions; for this to work, we must make sure to
2611 * perform the device lookup before any generic errors that may occur in a
2612 * scenario in which all optional arguments are omitted. */
2613 bs
= qmp_get_root_bs(device
, &local_err
);
2615 bs
= bdrv_lookup_bs(device
, device
, NULL
);
2617 error_free(local_err
);
2618 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2619 "Device '%s' not found", device
);
2621 error_propagate(errp
, local_err
);
2626 aio_context
= bdrv_get_aio_context(bs
);
2627 aio_context_acquire(aio_context
);
2629 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2633 /* default top_bs is the active layer */
2636 if (has_top_node
&& has_top
) {
2637 error_setg(errp
, "'top-node' and 'top' are mutually exclusive");
2639 } else if (has_top_node
) {
2640 top_bs
= bdrv_lookup_bs(NULL
, top_node
, errp
);
2641 if (top_bs
== NULL
) {
2644 if (!bdrv_chain_contains(bs
, top_bs
)) {
2645 error_setg(errp
, "'%s' is not in this backing file chain",
2649 } else if (has_top
&& top
) {
2650 /* This strcmp() is just a shortcut, there is no need to
2651 * refresh @bs's filename. If it mismatches,
2652 * bdrv_find_backing_image() will do the refresh and may still
2654 if (strcmp(bs
->filename
, top
) != 0) {
2655 top_bs
= bdrv_find_backing_image(bs
, top
);
2659 if (top_bs
== NULL
) {
2660 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2664 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2666 if (has_base_node
&& has_base
) {
2667 error_setg(errp
, "'base-node' and 'base' are mutually exclusive");
2669 } else if (has_base_node
) {
2670 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2671 if (base_bs
== NULL
) {
2674 if (!bdrv_chain_contains(top_bs
, base_bs
)) {
2675 error_setg(errp
, "'%s' is not in this backing file chain",
2679 } else if (has_base
&& base
) {
2680 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2682 base_bs
= bdrv_find_base(top_bs
);
2685 if (base_bs
== NULL
) {
2686 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2690 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2692 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
2693 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2698 /* Do not allow attempts to commit an image into itself */
2699 if (top_bs
== base_bs
) {
2700 error_setg(errp
, "cannot commit an image into itself");
2705 if (has_backing_file
) {
2706 error_setg(errp
, "'backing-file' specified,"
2707 " but 'top' is the active layer");
2710 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
2711 job_flags
, speed
, on_error
,
2712 filter_node_name
, NULL
, NULL
, false, &local_err
);
2714 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
2715 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2718 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, job_flags
,
2719 speed
, on_error
, has_backing_file
? backing_file
: NULL
,
2720 filter_node_name
, &local_err
);
2722 if (local_err
!= NULL
) {
2723 error_propagate(errp
, local_err
);
2728 aio_context_release(aio_context
);
2731 /* Common QMP interface for drive-backup and blockdev-backup */
2732 static BlockJob
*do_backup_common(BackupCommon
*backup
,
2733 BlockDriverState
*bs
,
2734 BlockDriverState
*target_bs
,
2735 AioContext
*aio_context
,
2736 JobTxn
*txn
, Error
**errp
)
2738 BlockJob
*job
= NULL
;
2739 BdrvDirtyBitmap
*bmap
= NULL
;
2740 int job_flags
= JOB_DEFAULT
;
2742 if (!backup
->has_speed
) {
2745 if (!backup
->has_on_source_error
) {
2746 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2748 if (!backup
->has_on_target_error
) {
2749 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2751 if (!backup
->has_job_id
) {
2752 backup
->job_id
= NULL
;
2754 if (!backup
->has_auto_finalize
) {
2755 backup
->auto_finalize
= true;
2757 if (!backup
->has_auto_dismiss
) {
2758 backup
->auto_dismiss
= true;
2760 if (!backup
->has_compress
) {
2761 backup
->compress
= false;
2764 if ((backup
->sync
== MIRROR_SYNC_MODE_BITMAP
) ||
2765 (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
)) {
2766 /* done before desugaring 'incremental' to print the right message */
2767 if (!backup
->has_bitmap
) {
2768 error_setg(errp
, "must provide a valid bitmap name for "
2769 "'%s' sync mode", MirrorSyncMode_str(backup
->sync
));
2774 if (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
) {
2775 if (backup
->has_bitmap_mode
&&
2776 backup
->bitmap_mode
!= BITMAP_SYNC_MODE_ON_SUCCESS
) {
2777 error_setg(errp
, "Bitmap sync mode must be '%s' "
2778 "when using sync mode '%s'",
2779 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS
),
2780 MirrorSyncMode_str(backup
->sync
));
2783 backup
->has_bitmap_mode
= true;
2784 backup
->sync
= MIRROR_SYNC_MODE_BITMAP
;
2785 backup
->bitmap_mode
= BITMAP_SYNC_MODE_ON_SUCCESS
;
2788 if (backup
->has_bitmap
) {
2789 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
2791 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
2794 if (!backup
->has_bitmap_mode
) {
2795 error_setg(errp
, "Bitmap sync mode must be given "
2796 "when providing a bitmap");
2799 if (bdrv_dirty_bitmap_check(bmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2803 /* This does not produce a useful bitmap artifact: */
2804 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
2805 error_setg(errp
, "sync mode '%s' does not produce meaningful bitmap"
2806 " outputs", MirrorSyncMode_str(backup
->sync
));
2810 /* If the bitmap isn't used for input or output, this is useless: */
2811 if (backup
->bitmap_mode
== BITMAP_SYNC_MODE_NEVER
&&
2812 backup
->sync
!= MIRROR_SYNC_MODE_BITMAP
) {
2813 error_setg(errp
, "Bitmap sync mode '%s' has no meaningful effect"
2814 " when combined with sync mode '%s'",
2815 BitmapSyncMode_str(backup
->bitmap_mode
),
2816 MirrorSyncMode_str(backup
->sync
));
2821 if (!backup
->has_bitmap
&& backup
->has_bitmap_mode
) {
2822 error_setg(errp
, "Cannot specify bitmap sync mode without a bitmap");
2826 if (!backup
->auto_finalize
) {
2827 job_flags
|= JOB_MANUAL_FINALIZE
;
2829 if (!backup
->auto_dismiss
) {
2830 job_flags
|= JOB_MANUAL_DISMISS
;
2833 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
2834 backup
->sync
, bmap
, backup
->bitmap_mode
,
2836 backup
->filter_node_name
,
2837 backup
->on_source_error
,
2838 backup
->on_target_error
,
2839 job_flags
, NULL
, NULL
, txn
, errp
);
2843 void qmp_drive_backup(DriveBackup
*backup
, Error
**errp
)
2845 TransactionAction action
= {
2846 .type
= TRANSACTION_ACTION_KIND_DRIVE_BACKUP
,
2847 .u
.drive_backup
.data
= backup
,
2849 blockdev_do_action(&action
, errp
);
2852 BlockDeviceInfoList
*qmp_query_named_block_nodes(bool has_flat
,
2856 bool return_flat
= has_flat
&& flat
;
2858 return bdrv_named_nodes_list(return_flat
, errp
);
2861 XDbgBlockGraph
*qmp_x_debug_query_block_graph(Error
**errp
)
2863 return bdrv_get_xdbg_block_graph(errp
);
2866 void qmp_blockdev_backup(BlockdevBackup
*backup
, Error
**errp
)
2868 TransactionAction action
= {
2869 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
,
2870 .u
.blockdev_backup
.data
= backup
,
2872 blockdev_do_action(&action
, errp
);
2875 /* Parameter check and block job starting for drive mirroring.
2876 * Caller should hold @device and @target's aio context (must be the same).
2878 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
2879 BlockDriverState
*target
,
2880 bool has_replaces
, const char *replaces
,
2881 enum MirrorSyncMode sync
,
2882 BlockMirrorBackingMode backing_mode
,
2884 bool has_speed
, int64_t speed
,
2885 bool has_granularity
, uint32_t granularity
,
2886 bool has_buf_size
, int64_t buf_size
,
2887 bool has_on_source_error
,
2888 BlockdevOnError on_source_error
,
2889 bool has_on_target_error
,
2890 BlockdevOnError on_target_error
,
2891 bool has_unmap
, bool unmap
,
2892 bool has_filter_node_name
,
2893 const char *filter_node_name
,
2894 bool has_copy_mode
, MirrorCopyMode copy_mode
,
2895 bool has_auto_finalize
, bool auto_finalize
,
2896 bool has_auto_dismiss
, bool auto_dismiss
,
2899 int job_flags
= JOB_DEFAULT
;
2904 if (!has_on_source_error
) {
2905 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2907 if (!has_on_target_error
) {
2908 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2910 if (!has_granularity
) {
2913 if (!has_buf_size
) {
2919 if (!has_filter_node_name
) {
2920 filter_node_name
= NULL
;
2922 if (!has_copy_mode
) {
2923 copy_mode
= MIRROR_COPY_MODE_BACKGROUND
;
2925 if (has_auto_finalize
&& !auto_finalize
) {
2926 job_flags
|= JOB_MANUAL_FINALIZE
;
2928 if (has_auto_dismiss
&& !auto_dismiss
) {
2929 job_flags
|= JOB_MANUAL_DISMISS
;
2932 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2933 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2934 "a value in range [512B, 64MB]");
2937 if (granularity
& (granularity
- 1)) {
2938 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2943 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
2946 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
2950 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2951 sync
= MIRROR_SYNC_MODE_FULL
;
2955 BlockDriverState
*to_replace_bs
;
2956 AioContext
*replace_aio_context
;
2957 int64_t bs_size
, replace_size
;
2959 bs_size
= bdrv_getlength(bs
);
2961 error_setg_errno(errp
, -bs_size
, "Failed to query device's size");
2965 to_replace_bs
= check_to_replace_node(bs
, replaces
, errp
);
2966 if (!to_replace_bs
) {
2970 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
2971 aio_context_acquire(replace_aio_context
);
2972 replace_size
= bdrv_getlength(to_replace_bs
);
2973 aio_context_release(replace_aio_context
);
2975 if (replace_size
< 0) {
2976 error_setg_errno(errp
, -replace_size
,
2977 "Failed to query the replacement node's size");
2980 if (bs_size
!= replace_size
) {
2981 error_setg(errp
, "cannot replace image with a mirror image of "
2987 /* pass the node name to replace to mirror start since it's loose coupling
2988 * and will allow to check whether the node still exist at mirror completion
2990 mirror_start(job_id
, bs
, target
,
2991 has_replaces
? replaces
: NULL
, job_flags
,
2992 speed
, granularity
, buf_size
, sync
, backing_mode
, zero_target
,
2993 on_source_error
, on_target_error
, unmap
, filter_node_name
,
2997 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
2999 BlockDriverState
*bs
;
3000 BlockDriverState
*source
, *target_bs
;
3001 AioContext
*aio_context
;
3002 AioContext
*old_context
;
3003 BlockMirrorBackingMode backing_mode
;
3004 Error
*local_err
= NULL
;
3005 QDict
*options
= NULL
;
3008 const char *format
= arg
->format
;
3012 bs
= qmp_get_root_bs(arg
->device
, errp
);
3017 /* Early check to avoid creating target */
3018 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3022 aio_context
= bdrv_get_aio_context(bs
);
3023 aio_context_acquire(aio_context
);
3025 if (!arg
->has_mode
) {
3026 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3029 if (!arg
->has_format
) {
3030 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3031 ? NULL
: bs
->drv
->format_name
);
3034 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3035 source
= backing_bs(bs
);
3036 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3037 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3039 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3043 size
= bdrv_getlength(bs
);
3045 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3049 if (arg
->has_replaces
) {
3050 if (!arg
->has_node_name
) {
3051 error_setg(errp
, "a node-name must be provided when replacing a"
3052 " named node of the graph");
3057 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3058 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3060 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3063 /* Don't open backing image in create() */
3064 flags
|= BDRV_O_NO_BACKING
;
3066 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3067 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3069 /* create new image w/o backing file */
3071 bdrv_img_create(arg
->target
, format
,
3072 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3074 switch (arg
->mode
) {
3075 case NEW_IMAGE_MODE_EXISTING
:
3077 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3078 /* create new image with backing file */
3079 bdrv_refresh_filename(source
);
3080 bdrv_img_create(arg
->target
, format
,
3082 source
->drv
->format_name
,
3083 NULL
, size
, flags
, false, &local_err
);
3091 error_propagate(errp
, local_err
);
3095 options
= qdict_new();
3096 if (arg
->has_node_name
) {
3097 qdict_put_str(options
, "node-name", arg
->node_name
);
3100 qdict_put_str(options
, "driver", format
);
3103 /* Mirroring takes care of copy-on-write using the source's backing
3106 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3111 zero_target
= (arg
->sync
== MIRROR_SYNC_MODE_FULL
&&
3112 (arg
->mode
== NEW_IMAGE_MODE_EXISTING
||
3113 !bdrv_has_zero_init(target_bs
)));
3116 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3117 old_context
= bdrv_get_aio_context(target_bs
);
3118 aio_context_release(aio_context
);
3119 aio_context_acquire(old_context
);
3121 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3123 bdrv_unref(target_bs
);
3124 aio_context_release(old_context
);
3128 aio_context_release(old_context
);
3129 aio_context_acquire(aio_context
);
3131 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3132 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3133 backing_mode
, zero_target
,
3134 arg
->has_speed
, arg
->speed
,
3135 arg
->has_granularity
, arg
->granularity
,
3136 arg
->has_buf_size
, arg
->buf_size
,
3137 arg
->has_on_source_error
, arg
->on_source_error
,
3138 arg
->has_on_target_error
, arg
->on_target_error
,
3139 arg
->has_unmap
, arg
->unmap
,
3141 arg
->has_copy_mode
, arg
->copy_mode
,
3142 arg
->has_auto_finalize
, arg
->auto_finalize
,
3143 arg
->has_auto_dismiss
, arg
->auto_dismiss
,
3145 bdrv_unref(target_bs
);
3146 error_propagate(errp
, local_err
);
3148 aio_context_release(aio_context
);
3151 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3152 const char *device
, const char *target
,
3153 bool has_replaces
, const char *replaces
,
3154 MirrorSyncMode sync
,
3155 bool has_speed
, int64_t speed
,
3156 bool has_granularity
, uint32_t granularity
,
3157 bool has_buf_size
, int64_t buf_size
,
3158 bool has_on_source_error
,
3159 BlockdevOnError on_source_error
,
3160 bool has_on_target_error
,
3161 BlockdevOnError on_target_error
,
3162 bool has_filter_node_name
,
3163 const char *filter_node_name
,
3164 bool has_copy_mode
, MirrorCopyMode copy_mode
,
3165 bool has_auto_finalize
, bool auto_finalize
,
3166 bool has_auto_dismiss
, bool auto_dismiss
,
3169 BlockDriverState
*bs
;
3170 BlockDriverState
*target_bs
;
3171 AioContext
*aio_context
;
3172 AioContext
*old_context
;
3173 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3174 Error
*local_err
= NULL
;
3178 bs
= qmp_get_root_bs(device
, errp
);
3183 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3188 zero_target
= (sync
== MIRROR_SYNC_MODE_FULL
);
3190 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3191 old_context
= bdrv_get_aio_context(target_bs
);
3192 aio_context
= bdrv_get_aio_context(bs
);
3193 aio_context_acquire(old_context
);
3195 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3197 aio_context_release(old_context
);
3198 aio_context_acquire(aio_context
);
3204 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3205 has_replaces
, replaces
, sync
, backing_mode
,
3206 zero_target
, has_speed
, speed
,
3207 has_granularity
, granularity
,
3208 has_buf_size
, buf_size
,
3209 has_on_source_error
, on_source_error
,
3210 has_on_target_error
, on_target_error
,
3212 has_filter_node_name
, filter_node_name
,
3213 has_copy_mode
, copy_mode
,
3214 has_auto_finalize
, auto_finalize
,
3215 has_auto_dismiss
, auto_dismiss
,
3217 error_propagate(errp
, local_err
);
3219 aio_context_release(aio_context
);
3222 /* Get a block job using its ID and acquire its AioContext */
3223 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3230 *aio_context
= NULL
;
3232 job
= block_job_get(id
);
3235 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3236 "Block job '%s' not found", id
);
3240 *aio_context
= blk_get_aio_context(job
->blk
);
3241 aio_context_acquire(*aio_context
);
3246 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3248 AioContext
*aio_context
;
3249 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3255 block_job_set_speed(job
, speed
, errp
);
3256 aio_context_release(aio_context
);
3259 void qmp_block_job_cancel(const char *device
,
3260 bool has_force
, bool force
, Error
**errp
)
3262 AioContext
*aio_context
;
3263 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3273 if (job_user_paused(&job
->job
) && !force
) {
3274 error_setg(errp
, "The block job for device '%s' is currently paused",
3279 trace_qmp_block_job_cancel(job
);
3280 job_user_cancel(&job
->job
, force
, errp
);
3282 aio_context_release(aio_context
);
3285 void qmp_block_job_pause(const char *device
, Error
**errp
)
3287 AioContext
*aio_context
;
3288 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3294 trace_qmp_block_job_pause(job
);
3295 job_user_pause(&job
->job
, errp
);
3296 aio_context_release(aio_context
);
3299 void qmp_block_job_resume(const char *device
, Error
**errp
)
3301 AioContext
*aio_context
;
3302 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3308 trace_qmp_block_job_resume(job
);
3309 job_user_resume(&job
->job
, errp
);
3310 aio_context_release(aio_context
);
3313 void qmp_block_job_complete(const char *device
, Error
**errp
)
3315 AioContext
*aio_context
;
3316 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3322 trace_qmp_block_job_complete(job
);
3323 job_complete(&job
->job
, errp
);
3324 aio_context_release(aio_context
);
3327 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3329 AioContext
*aio_context
;
3330 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3336 trace_qmp_block_job_finalize(job
);
3338 job_finalize(&job
->job
, errp
);
3341 * Job's context might have changed via job_finalize (and job_txn_apply
3342 * automatically acquires the new one), so make sure we release the correct
3345 aio_context
= blk_get_aio_context(job
->blk
);
3346 job_unref(&job
->job
);
3347 aio_context_release(aio_context
);
3350 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3352 AioContext
*aio_context
;
3353 BlockJob
*bjob
= find_block_job(id
, &aio_context
, errp
);
3360 trace_qmp_block_job_dismiss(bjob
);
3362 job_dismiss(&job
, errp
);
3363 aio_context_release(aio_context
);
3366 void qmp_change_backing_file(const char *device
,
3367 const char *image_node_name
,
3368 const char *backing_file
,
3371 BlockDriverState
*bs
= NULL
;
3372 AioContext
*aio_context
;
3373 BlockDriverState
*image_bs
= NULL
;
3374 Error
*local_err
= NULL
;
3378 bs
= qmp_get_root_bs(device
, errp
);
3383 aio_context
= bdrv_get_aio_context(bs
);
3384 aio_context_acquire(aio_context
);
3386 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3388 error_propagate(errp
, local_err
);
3393 error_setg(errp
, "image file not found");
3397 if (bdrv_find_base(image_bs
) == image_bs
) {
3398 error_setg(errp
, "not allowing backing file change on an image "
3399 "without a backing file");
3403 /* even though we are not necessarily operating on bs, we need it to
3404 * determine if block ops are currently prohibited on the chain */
3405 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3409 /* final sanity check */
3410 if (!bdrv_chain_contains(bs
, image_bs
)) {
3411 error_setg(errp
, "'%s' and image file are not in the same chain",
3416 /* if not r/w, reopen to make r/w */
3417 ro
= bdrv_is_read_only(image_bs
);
3420 if (bdrv_reopen_set_read_only(image_bs
, false, errp
) != 0) {
3425 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3426 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3429 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3431 /* don't exit here, so we can try to restore open flags if
3436 bdrv_reopen_set_read_only(image_bs
, true, &local_err
);
3437 error_propagate(errp
, local_err
);
3441 aio_context_release(aio_context
);
3444 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3446 BlockDriverState
*bs
;
3448 Visitor
*v
= qobject_output_visitor_new(&obj
);
3451 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3452 visit_complete(v
, &obj
);
3453 qdict
= qobject_to(QDict
, obj
);
3455 qdict_flatten(qdict
);
3457 if (!qdict_get_try_str(qdict
, "node-name")) {
3458 error_setg(errp
, "'node-name' must be specified for the root node");
3462 bs
= bds_tree_init(qdict
, errp
);
3467 bdrv_set_monitor_owned(bs
);
3473 void qmp_x_blockdev_reopen(BlockdevOptions
*options
, Error
**errp
)
3475 BlockDriverState
*bs
;
3478 Visitor
*v
= qobject_output_visitor_new(&obj
);
3479 BlockReopenQueue
*queue
;
3482 /* Check for the selected node name */
3483 if (!options
->has_node_name
) {
3484 error_setg(errp
, "Node name not specified");
3488 bs
= bdrv_find_node(options
->node_name
);
3490 error_setg(errp
, "Cannot find node named '%s'", options
->node_name
);
3494 /* Put all options in a QDict and flatten it */
3495 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3496 visit_complete(v
, &obj
);
3497 qdict
= qobject_to(QDict
, obj
);
3499 qdict_flatten(qdict
);
3501 /* Perform the reopen operation */
3502 ctx
= bdrv_get_aio_context(bs
);
3503 aio_context_acquire(ctx
);
3504 bdrv_subtree_drained_begin(bs
);
3505 queue
= bdrv_reopen_queue(NULL
, bs
, qdict
, false);
3506 bdrv_reopen_multiple(queue
, errp
);
3507 bdrv_subtree_drained_end(bs
);
3508 aio_context_release(ctx
);
3514 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3516 AioContext
*aio_context
;
3517 BlockDriverState
*bs
;
3519 bs
= bdrv_find_node(node_name
);
3521 error_setg(errp
, "Cannot find node %s", node_name
);
3524 if (bdrv_has_blk(bs
)) {
3525 error_setg(errp
, "Node %s is in use", node_name
);
3528 aio_context
= bdrv_get_aio_context(bs
);
3529 aio_context_acquire(aio_context
);
3531 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3535 if (!QTAILQ_IN_USE(bs
, monitor_list
)) {
3536 error_setg(errp
, "Node %s is not owned by the monitor",
3541 if (bs
->refcnt
> 1) {
3542 error_setg(errp
, "Block device %s is in use",
3543 bdrv_get_device_or_node_name(bs
));
3547 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3551 aio_context_release(aio_context
);
3554 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3555 const char *child_name
)
3559 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3560 if (strcmp(child
->name
, child_name
) == 0) {
3568 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
3569 const char *child
, bool has_node
,
3570 const char *node
, Error
**errp
)
3572 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3575 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3580 if (has_child
== has_node
) {
3582 error_setg(errp
, "The parameters child and node are in conflict");
3584 error_setg(errp
, "Either child or node must be specified");
3590 p_child
= bdrv_find_child(parent_bs
, child
);
3592 error_setg(errp
, "Node '%s' does not have child '%s'",
3596 bdrv_del_child(parent_bs
, p_child
, errp
);
3600 new_bs
= bdrv_find_node(node
);
3602 error_setg(errp
, "Node '%s' not found", node
);
3605 bdrv_add_child(parent_bs
, new_bs
, errp
);
3609 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3611 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3614 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
3615 BlockJobInfoList
*elem
;
3616 AioContext
*aio_context
;
3618 if (block_job_is_internal(job
)) {
3621 elem
= g_new0(BlockJobInfoList
, 1);
3622 aio_context
= blk_get_aio_context(job
->blk
);
3623 aio_context_acquire(aio_context
);
3624 elem
->value
= block_job_query(job
, errp
);
3625 aio_context_release(aio_context
);
3628 qapi_free_BlockJobInfoList(head
);
3632 p_next
= &elem
->next
;
3638 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
3639 bool has_force
, bool force
, Error
**errp
)
3641 AioContext
*old_context
;
3642 AioContext
*new_context
;
3643 BlockDriverState
*bs
;
3645 bs
= bdrv_find_node(node_name
);
3647 error_setg(errp
, "Cannot find node %s", node_name
);
3651 /* Protects against accidents. */
3652 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
3653 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
3654 "be in use (use force=true to override this check)",
3659 if (iothread
->type
== QTYPE_QSTRING
) {
3660 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
3662 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
3666 new_context
= iothread_get_aio_context(obj
);
3668 new_context
= qemu_get_aio_context();
3671 old_context
= bdrv_get_aio_context(bs
);
3672 aio_context_acquire(old_context
);
3674 bdrv_try_set_aio_context(bs
, new_context
, errp
);
3676 aio_context_release(old_context
);
3679 QemuOptsList qemu_common_drive_opts
= {
3681 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3685 .type
= QEMU_OPT_BOOL
,
3686 .help
= "enable/disable snapshot mode",
3689 .type
= QEMU_OPT_STRING
,
3690 .help
= "host AIO implementation (threads, native, io_uring)",
3692 .name
= BDRV_OPT_CACHE_WB
,
3693 .type
= QEMU_OPT_BOOL
,
3694 .help
= "Enable writeback mode",
3697 .type
= QEMU_OPT_STRING
,
3698 .help
= "disk format (raw, qcow2, ...)",
3701 .type
= QEMU_OPT_STRING
,
3702 .help
= "read error action",
3705 .type
= QEMU_OPT_STRING
,
3706 .help
= "write error action",
3708 .name
= BDRV_OPT_READ_ONLY
,
3709 .type
= QEMU_OPT_BOOL
,
3710 .help
= "open drive file as read-only",
3716 .name
= "throttling.group",
3717 .type
= QEMU_OPT_STRING
,
3718 .help
= "name of the block throttling group",
3720 .name
= "copy-on-read",
3721 .type
= QEMU_OPT_BOOL
,
3722 .help
= "copy read data from backing file into image file",
3724 .name
= "detect-zeroes",
3725 .type
= QEMU_OPT_STRING
,
3726 .help
= "try to optimize zero writes (off, on, unmap)",
3728 .name
= "stats-account-invalid",
3729 .type
= QEMU_OPT_BOOL
,
3730 .help
= "whether to account for invalid I/O operations "
3731 "in the statistics",
3733 .name
= "stats-account-failed",
3734 .type
= QEMU_OPT_BOOL
,
3735 .help
= "whether to account for failed I/O operations "
3736 "in the statistics",
3738 { /* end of list */ }
3742 QemuOptsList qemu_drive_opts
= {
3744 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3747 * no elements => accept any params
3748 * validation will happen later
3750 { /* end of list */ }