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, &error
);
509 error_propagate(errp
, error
);
513 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
515 error_propagate(errp
, error
);
520 qdict_del(bs_opts
, "id");
523 /* extract parameters */
524 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
526 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
527 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
529 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
531 id
= qemu_opts_id(opts
);
533 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
534 qdict_array_split(interval_dict
, &interval_list
);
536 if (qdict_size(interval_dict
) != 0) {
537 error_setg(errp
, "Invalid option stats-intervals.%s",
538 qdict_first(interval_dict
)->key
);
542 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
543 &detect_zeroes
, &error
);
545 error_propagate(errp
, error
);
549 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
550 if (is_help_option(buf
)) {
551 qemu_printf("Supported formats:");
552 bdrv_iterate_format(bdrv_format_print
, NULL
, false);
553 qemu_printf("\nSupported formats (read-only):");
554 bdrv_iterate_format(bdrv_format_print
, NULL
, true);
559 if (qdict_haskey(bs_opts
, "driver")) {
560 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
563 qdict_put_str(bs_opts
, "driver", buf
);
566 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
567 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
568 on_write_error
= parse_block_error_action(buf
, 0, &error
);
570 error_propagate(errp
, error
);
575 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
576 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
577 on_read_error
= parse_block_error_action(buf
, 1, &error
);
579 error_propagate(errp
, error
);
585 bdrv_flags
|= BDRV_O_SNAPSHOT
;
588 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
591 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
592 BlockBackendRootState
*blk_rs
;
594 blk
= blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
595 blk_rs
= blk_get_root_state(blk
);
596 blk_rs
->open_flags
= bdrv_flags
;
597 blk_rs
->read_only
= read_only
;
598 blk_rs
->detect_zeroes
= detect_zeroes
;
600 qobject_unref(bs_opts
);
602 if (file
&& !*file
) {
606 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
607 * with other callers) rather than what we want as the real defaults.
608 * Apply the defaults here instead. */
609 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
610 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
611 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
612 read_only
? "on" : "off");
613 qdict_set_default_str(bs_opts
, BDRV_OPT_AUTO_READ_ONLY
, "on");
614 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
616 if (runstate_check(RUN_STATE_INMIGRATE
)) {
617 bdrv_flags
|= BDRV_O_INACTIVE
;
620 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
626 bs
->detect_zeroes
= detect_zeroes
;
628 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
630 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
637 /* disk I/O throttling */
638 if (throttle_enabled(&cfg
)) {
639 if (!throttling_group
) {
640 throttling_group
= id
;
642 blk_io_limits_enable(blk
, throttling_group
);
643 blk_set_io_limits(blk
, &cfg
);
646 blk_set_enable_write_cache(blk
, !writethrough
);
647 blk_set_on_error(blk
, on_read_error
, on_write_error
);
649 if (!monitor_add_blk(blk
, id
, errp
)) {
657 qobject_unref(interval_dict
);
658 qobject_unref(interval_list
);
663 qobject_unref(interval_dict
);
664 qobject_unref(interval_list
);
666 qobject_unref(bs_opts
);
670 /* Takes the ownership of bs_opts */
671 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
675 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
676 * with other callers) rather than what we want as the real defaults.
677 * Apply the defaults here instead. */
678 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
679 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
680 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
682 if (runstate_check(RUN_STATE_INMIGRATE
)) {
683 bdrv_flags
|= BDRV_O_INACTIVE
;
686 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
689 void blockdev_close_all_bdrv_states(void)
691 BlockDriverState
*bs
, *next_bs
;
693 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
694 AioContext
*ctx
= bdrv_get_aio_context(bs
);
696 aio_context_acquire(ctx
);
698 aio_context_release(ctx
);
702 /* Iterates over the list of monitor-owned BlockDriverStates */
703 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
705 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
706 : QTAILQ_FIRST(&monitor_bdrv_states
);
709 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
714 value
= qemu_opt_get(opts
, from
);
716 if (qemu_opt_find(opts
, to
)) {
717 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
718 "same time", to
, from
);
723 /* rename all items in opts */
724 while ((value
= qemu_opt_get(opts
, from
))) {
725 qemu_opt_set(opts
, to
, value
, &error_abort
);
726 qemu_opt_unset(opts
, from
);
730 QemuOptsList qemu_legacy_drive_opts
= {
732 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
736 .type
= QEMU_OPT_NUMBER
,
737 .help
= "bus number",
740 .type
= QEMU_OPT_NUMBER
,
741 .help
= "unit number (i.e. lun for scsi)",
744 .type
= QEMU_OPT_NUMBER
,
745 .help
= "index number",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "media type (disk, cdrom)",
752 .type
= QEMU_OPT_STRING
,
753 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
756 .type
= QEMU_OPT_STRING
,
760 /* Options that are passed on, but have special semantics with -drive */
762 .name
= BDRV_OPT_READ_ONLY
,
763 .type
= QEMU_OPT_BOOL
,
764 .help
= "open drive file as read-only",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "read error action",
771 .type
= QEMU_OPT_STRING
,
772 .help
= "write error action",
774 .name
= "copy-on-read",
775 .type
= QEMU_OPT_BOOL
,
776 .help
= "copy read data from backing file into image file",
779 { /* end of list */ }
783 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
,
788 DriveInfo
*dinfo
= NULL
;
790 QemuOpts
*legacy_opts
;
791 DriveMediaType media
= MEDIA_DISK
;
792 BlockInterfaceType type
;
793 int max_devs
, bus_id
, unit_id
, index
;
794 const char *werror
, *rerror
;
795 bool read_only
= false;
797 const char *filename
;
798 Error
*local_err
= NULL
;
801 /* Change legacy command line options into QMP ones */
802 static const struct {
806 { "iops", "throttling.iops-total" },
807 { "iops_rd", "throttling.iops-read" },
808 { "iops_wr", "throttling.iops-write" },
810 { "bps", "throttling.bps-total" },
811 { "bps_rd", "throttling.bps-read" },
812 { "bps_wr", "throttling.bps-write" },
814 { "iops_max", "throttling.iops-total-max" },
815 { "iops_rd_max", "throttling.iops-read-max" },
816 { "iops_wr_max", "throttling.iops-write-max" },
818 { "bps_max", "throttling.bps-total-max" },
819 { "bps_rd_max", "throttling.bps-read-max" },
820 { "bps_wr_max", "throttling.bps-write-max" },
822 { "iops_size", "throttling.iops-size" },
824 { "group", "throttling.group" },
826 { "readonly", BDRV_OPT_READ_ONLY
},
829 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
830 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
833 error_propagate(errp
, local_err
);
838 value
= qemu_opt_get(all_opts
, "cache");
843 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
844 error_setg(errp
, "invalid cache option");
848 /* Specific options take precedence */
849 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
850 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
851 !writethrough
, &error_abort
);
853 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
854 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
855 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
857 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
858 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
859 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
861 qemu_opt_unset(all_opts
, "cache");
864 /* Get a QDict for processing the options */
865 bs_opts
= qdict_new();
866 qemu_opts_to_qdict(all_opts
, bs_opts
);
868 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
870 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
872 error_propagate(errp
, local_err
);
877 value
= qemu_opt_get(legacy_opts
, "media");
879 if (!strcmp(value
, "disk")) {
881 } else if (!strcmp(value
, "cdrom")) {
885 error_setg(errp
, "'%s' invalid media", value
);
890 /* copy-on-read is disabled with a warning for read-only devices */
891 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
892 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
894 if (read_only
&& copy_on_read
) {
895 warn_report("disabling copy-on-read on read-only drive");
896 copy_on_read
= false;
899 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
900 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
902 /* Controller type */
903 value
= qemu_opt_get(legacy_opts
, "if");
906 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
909 if (type
== IF_COUNT
) {
910 error_setg(errp
, "unsupported bus type '%s'", value
);
914 type
= block_default_type
;
917 /* Device address specified by bus/unit or index.
918 * If none was specified, try to find the first free one. */
919 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
920 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
921 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
923 max_devs
= if_max_devs
[type
];
926 if (bus_id
!= 0 || unit_id
!= -1) {
927 error_setg(errp
, "index cannot be used with bus and unit");
930 bus_id
= drive_index_to_bus_id(type
, index
);
931 unit_id
= drive_index_to_unit_id(type
, index
);
936 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
938 if (max_devs
&& unit_id
>= max_devs
) {
945 if (max_devs
&& unit_id
>= max_devs
) {
946 error_setg(errp
, "unit %d too big (max is %d)", unit_id
, max_devs
- 1);
950 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
951 error_setg(errp
, "drive with bus=%d, unit=%d (index=%d) exists",
952 bus_id
, unit_id
, index
);
956 /* no id supplied -> create one */
957 if (qemu_opts_id(all_opts
) == NULL
) {
959 const char *mediastr
= "";
960 if (type
== IF_IDE
|| type
== IF_SCSI
) {
961 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
964 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
967 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
970 qdict_put_str(bs_opts
, "id", new_id
);
974 /* Add virtio block device */
975 if (type
== IF_VIRTIO
) {
977 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
979 if (arch_type
== QEMU_ARCH_S390X
) {
980 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
982 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
984 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
988 filename
= qemu_opt_get(legacy_opts
, "file");
990 /* Check werror/rerror compatibility with if=... */
991 werror
= qemu_opt_get(legacy_opts
, "werror");
992 if (werror
!= NULL
) {
993 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
995 error_setg(errp
, "werror is not supported by this bus type");
998 qdict_put_str(bs_opts
, "werror", werror
);
1001 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1002 if (rerror
!= NULL
) {
1003 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1005 error_setg(errp
, "rerror is not supported by this bus type");
1008 qdict_put_str(bs_opts
, "rerror", rerror
);
1011 /* Actual block device init: Functionality shared with blockdev-add */
1012 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1015 error_propagate(errp
, local_err
);
1021 /* Create legacy DriveInfo */
1022 dinfo
= g_malloc0(sizeof(*dinfo
));
1023 dinfo
->opts
= all_opts
;
1026 dinfo
->bus
= bus_id
;
1027 dinfo
->unit
= unit_id
;
1029 blk_set_legacy_dinfo(blk
, dinfo
);
1036 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1043 qemu_opts_del(legacy_opts
);
1044 qobject_unref(bs_opts
);
1048 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1050 BlockDriverState
*bs
;
1052 bs
= bdrv_lookup_bs(name
, name
, errp
);
1057 if (!bdrv_is_root_node(bs
)) {
1058 error_setg(errp
, "Need a root block node");
1062 if (!bdrv_is_inserted(bs
)) {
1063 error_setg(errp
, "Device has no medium");
1070 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1072 TransactionActionList list
;
1074 list
.value
= action
;
1076 qmp_transaction(&list
, false, NULL
, errp
);
1079 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1080 bool has_node_name
, const char *node_name
,
1081 const char *snapshot_file
,
1082 bool has_snapshot_node_name
,
1083 const char *snapshot_node_name
,
1084 bool has_format
, const char *format
,
1085 bool has_mode
, NewImageMode mode
, Error
**errp
)
1087 BlockdevSnapshotSync snapshot
= {
1088 .has_device
= has_device
,
1089 .device
= (char *) device
,
1090 .has_node_name
= has_node_name
,
1091 .node_name
= (char *) node_name
,
1092 .snapshot_file
= (char *) snapshot_file
,
1093 .has_snapshot_node_name
= has_snapshot_node_name
,
1094 .snapshot_node_name
= (char *) snapshot_node_name
,
1095 .has_format
= has_format
,
1096 .format
= (char *) format
,
1097 .has_mode
= has_mode
,
1100 TransactionAction action
= {
1101 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1102 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1104 blockdev_do_action(&action
, errp
);
1107 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1110 BlockdevSnapshot snapshot_data
= {
1111 .node
= (char *) node
,
1112 .overlay
= (char *) overlay
1114 TransactionAction action
= {
1115 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1116 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1118 blockdev_do_action(&action
, errp
);
1121 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1125 BlockdevSnapshotInternal snapshot
= {
1126 .device
= (char *) device
,
1127 .name
= (char *) name
1129 TransactionAction action
= {
1130 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1131 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1133 blockdev_do_action(&action
, errp
);
1136 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1143 BlockDriverState
*bs
;
1144 AioContext
*aio_context
;
1145 QEMUSnapshotInfo sn
;
1146 Error
*local_err
= NULL
;
1147 SnapshotInfo
*info
= NULL
;
1150 bs
= qmp_get_root_bs(device
, errp
);
1154 aio_context
= bdrv_get_aio_context(bs
);
1155 aio_context_acquire(aio_context
);
1166 error_setg(errp
, "Name or id must be provided");
1167 goto out_aio_context
;
1170 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1171 goto out_aio_context
;
1174 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1176 error_propagate(errp
, local_err
);
1177 goto out_aio_context
;
1181 "Snapshot with id '%s' and name '%s' does not exist on "
1183 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1184 goto out_aio_context
;
1187 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1189 error_propagate(errp
, local_err
);
1190 goto out_aio_context
;
1193 aio_context_release(aio_context
);
1195 info
= g_new0(SnapshotInfo
, 1);
1196 info
->id
= g_strdup(sn
.id_str
);
1197 info
->name
= g_strdup(sn
.name
);
1198 info
->date_nsec
= sn
.date_nsec
;
1199 info
->date_sec
= sn
.date_sec
;
1200 info
->vm_state_size
= sn
.vm_state_size
;
1201 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1202 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1207 aio_context_release(aio_context
);
1211 /* New and old BlockDriverState structs for atomic group operations */
1213 typedef struct BlkActionState BlkActionState
;
1217 * Table of operations that define an Action.
1219 * @instance_size: Size of state struct, in bytes.
1220 * @prepare: Prepare the work, must NOT be NULL.
1221 * @commit: Commit the changes, can be NULL.
1222 * @abort: Abort the changes on fail, can be NULL.
1223 * @clean: Clean up resources after all transaction actions have called
1224 * commit() or abort(). Can be NULL.
1226 * Only prepare() may fail. In a single transaction, only one of commit() or
1227 * abort() will be called. clean() will always be called if it is present.
1229 typedef struct BlkActionOps
{
1230 size_t instance_size
;
1231 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1232 void (*commit
)(BlkActionState
*common
);
1233 void (*abort
)(BlkActionState
*common
);
1234 void (*clean
)(BlkActionState
*common
);
1239 * Describes one Action's state within a Transaction.
1241 * @action: QAPI-defined enum identifying which Action to perform.
1242 * @ops: Table of ActionOps this Action can perform.
1243 * @block_job_txn: Transaction which this action belongs to.
1244 * @entry: List membership for all Actions in this Transaction.
1246 * This structure must be arranged as first member in a subclassed type,
1247 * assuming that the compiler will also arrange it to the same offsets as the
1250 struct BlkActionState
{
1251 TransactionAction
*action
;
1252 const BlkActionOps
*ops
;
1253 JobTxn
*block_job_txn
;
1254 TransactionProperties
*txn_props
;
1255 QTAILQ_ENTRY(BlkActionState
) entry
;
1258 /* internal snapshot private data */
1259 typedef struct InternalSnapshotState
{
1260 BlkActionState common
;
1261 BlockDriverState
*bs
;
1262 QEMUSnapshotInfo sn
;
1264 } InternalSnapshotState
;
1267 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1269 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1271 "Action '%s' does not support Transaction property "
1272 "completion-mode = %s",
1273 TransactionActionKind_str(s
->action
->type
),
1274 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1280 static void internal_snapshot_prepare(BlkActionState
*common
,
1283 Error
*local_err
= NULL
;
1286 BlockDriverState
*bs
;
1287 QEMUSnapshotInfo old_sn
, *sn
;
1290 BlockdevSnapshotInternal
*internal
;
1291 InternalSnapshotState
*state
;
1292 AioContext
*aio_context
;
1295 g_assert(common
->action
->type
==
1296 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1297 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1298 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1300 /* 1. parse input */
1301 device
= internal
->device
;
1302 name
= internal
->name
;
1304 /* 2. check for validation */
1305 if (action_check_completion_mode(common
, errp
) < 0) {
1309 bs
= qmp_get_root_bs(device
, errp
);
1314 aio_context
= bdrv_get_aio_context(bs
);
1315 aio_context_acquire(aio_context
);
1319 /* Paired with .clean() */
1320 bdrv_drained_begin(bs
);
1322 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1326 if (bdrv_is_read_only(bs
)) {
1327 error_setg(errp
, "Device '%s' is read only", device
);
1331 if (!bdrv_can_snapshot(bs
)) {
1332 error_setg(errp
, "Block format '%s' used by device '%s' "
1333 "does not support internal snapshots",
1334 bs
->drv
->format_name
, device
);
1338 if (!strlen(name
)) {
1339 error_setg(errp
, "Name is empty");
1343 /* check whether a snapshot with name exist */
1344 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1347 error_propagate(errp
, local_err
);
1351 "Snapshot with name '%s' already exists on device '%s'",
1356 /* 3. take the snapshot */
1358 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1359 qemu_gettimeofday(&tv
);
1360 sn
->date_sec
= tv
.tv_sec
;
1361 sn
->date_nsec
= tv
.tv_usec
* 1000;
1362 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1364 ret1
= bdrv_snapshot_create(bs
, sn
);
1366 error_setg_errno(errp
, -ret1
,
1367 "Failed to create snapshot '%s' on device '%s'",
1372 /* 4. succeed, mark a snapshot is created */
1373 state
->created
= true;
1376 aio_context_release(aio_context
);
1379 static void internal_snapshot_abort(BlkActionState
*common
)
1381 InternalSnapshotState
*state
=
1382 DO_UPCAST(InternalSnapshotState
, common
, common
);
1383 BlockDriverState
*bs
= state
->bs
;
1384 QEMUSnapshotInfo
*sn
= &state
->sn
;
1385 AioContext
*aio_context
;
1386 Error
*local_error
= NULL
;
1388 if (!state
->created
) {
1392 aio_context
= bdrv_get_aio_context(state
->bs
);
1393 aio_context_acquire(aio_context
);
1395 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1396 error_reportf_err(local_error
,
1397 "Failed to delete snapshot with id '%s' and "
1398 "name '%s' on device '%s' in abort: ",
1399 sn
->id_str
, sn
->name
,
1400 bdrv_get_device_name(bs
));
1403 aio_context_release(aio_context
);
1406 static void internal_snapshot_clean(BlkActionState
*common
)
1408 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1410 AioContext
*aio_context
;
1416 aio_context
= bdrv_get_aio_context(state
->bs
);
1417 aio_context_acquire(aio_context
);
1419 bdrv_drained_end(state
->bs
);
1421 aio_context_release(aio_context
);
1424 /* external snapshot private data */
1425 typedef struct ExternalSnapshotState
{
1426 BlkActionState common
;
1427 BlockDriverState
*old_bs
;
1428 BlockDriverState
*new_bs
;
1429 bool overlay_appended
;
1430 } ExternalSnapshotState
;
1432 static void external_snapshot_prepare(BlkActionState
*common
,
1436 QDict
*options
= NULL
;
1437 Error
*local_err
= NULL
;
1438 /* Device and node name of the image to generate the snapshot from */
1440 const char *node_name
;
1441 /* Reference to the new image (for 'blockdev-snapshot') */
1442 const char *snapshot_ref
;
1443 /* File name of the new image (for 'blockdev-snapshot-sync') */
1444 const char *new_image_file
;
1445 ExternalSnapshotState
*state
=
1446 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1447 TransactionAction
*action
= common
->action
;
1448 AioContext
*aio_context
;
1449 uint64_t perm
, shared
;
1451 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1452 * purpose but a different set of parameters */
1453 switch (action
->type
) {
1454 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1456 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1458 node_name
= s
->node
;
1459 new_image_file
= NULL
;
1460 snapshot_ref
= s
->overlay
;
1463 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1465 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1466 device
= s
->has_device
? s
->device
: NULL
;
1467 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1468 new_image_file
= s
->snapshot_file
;
1469 snapshot_ref
= NULL
;
1473 g_assert_not_reached();
1476 /* start processing */
1477 if (action_check_completion_mode(common
, errp
) < 0) {
1481 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1482 if (!state
->old_bs
) {
1486 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1487 aio_context_acquire(aio_context
);
1489 /* Paired with .clean() */
1490 bdrv_drained_begin(state
->old_bs
);
1492 if (!bdrv_is_inserted(state
->old_bs
)) {
1493 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1497 if (bdrv_op_is_blocked(state
->old_bs
,
1498 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1502 if (!bdrv_is_read_only(state
->old_bs
)) {
1503 if (bdrv_flush(state
->old_bs
)) {
1504 error_setg(errp
, QERR_IO_ERROR
);
1509 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1510 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1511 const char *format
= s
->has_format
? s
->format
: "qcow2";
1512 enum NewImageMode mode
;
1513 const char *snapshot_node_name
=
1514 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1516 if (node_name
&& !snapshot_node_name
) {
1517 error_setg(errp
, "New overlay node name missing");
1521 if (snapshot_node_name
&&
1522 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1523 error_setg(errp
, "New overlay node name already in use");
1527 flags
= state
->old_bs
->open_flags
;
1528 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1529 flags
|= BDRV_O_NO_BACKING
;
1531 /* create new image w/backing file */
1532 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1533 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1534 int64_t size
= bdrv_getlength(state
->old_bs
);
1536 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1539 bdrv_refresh_filename(state
->old_bs
);
1540 bdrv_img_create(new_image_file
, format
,
1541 state
->old_bs
->filename
,
1542 state
->old_bs
->drv
->format_name
,
1543 NULL
, size
, flags
, false, &local_err
);
1545 error_propagate(errp
, local_err
);
1550 options
= qdict_new();
1551 if (snapshot_node_name
) {
1552 qdict_put_str(options
, "node-name", snapshot_node_name
);
1554 qdict_put_str(options
, "driver", format
);
1557 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1559 /* We will manually add the backing_hd field to the bs later */
1560 if (!state
->new_bs
) {
1565 * Allow attaching a backing file to an overlay that's already in use only
1566 * if the parents don't assume that they are already seeing a valid image.
1567 * (Specifically, allow it as a mirror target, which is write-only access.)
1569 bdrv_get_cumulative_perm(state
->new_bs
, &perm
, &shared
);
1570 if (perm
& BLK_PERM_CONSISTENT_READ
) {
1571 error_setg(errp
, "The overlay is already in use");
1575 if (state
->new_bs
->backing
!= NULL
) {
1576 error_setg(errp
, "The overlay already has a backing image");
1580 if (!state
->new_bs
->drv
->supports_backing
) {
1581 error_setg(errp
, "The overlay does not support backing images");
1585 /* This removes our old bs and adds the new bs. This is an operation that
1586 * can fail, so we need to do it in .prepare; undoing it for abort is
1587 * always possible. */
1588 bdrv_ref(state
->new_bs
);
1589 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1591 error_propagate(errp
, local_err
);
1594 state
->overlay_appended
= true;
1597 aio_context_release(aio_context
);
1600 static void external_snapshot_commit(BlkActionState
*common
)
1602 ExternalSnapshotState
*state
=
1603 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1604 AioContext
*aio_context
;
1606 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1607 aio_context_acquire(aio_context
);
1609 /* We don't need (or want) to use the transactional
1610 * bdrv_reopen_multiple() across all the entries at once, because we
1611 * don't want to abort all of them if one of them fails the reopen */
1612 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1613 bdrv_reopen_set_read_only(state
->old_bs
, true, NULL
);
1616 aio_context_release(aio_context
);
1619 static void external_snapshot_abort(BlkActionState
*common
)
1621 ExternalSnapshotState
*state
=
1622 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1623 if (state
->new_bs
) {
1624 if (state
->overlay_appended
) {
1625 AioContext
*aio_context
;
1626 AioContext
*tmp_context
;
1629 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1630 aio_context_acquire(aio_context
);
1632 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1633 close state->old_bs; we need it */
1634 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1637 * The call to bdrv_set_backing_hd() above returns state->old_bs to
1638 * the main AioContext. As we're still going to be using it, return
1639 * it to the AioContext it was before.
1641 tmp_context
= bdrv_get_aio_context(state
->old_bs
);
1642 if (aio_context
!= tmp_context
) {
1643 aio_context_release(aio_context
);
1644 aio_context_acquire(tmp_context
);
1646 ret
= bdrv_try_set_aio_context(state
->old_bs
,
1650 aio_context_release(tmp_context
);
1651 aio_context_acquire(aio_context
);
1654 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1655 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1657 aio_context_release(aio_context
);
1662 static void external_snapshot_clean(BlkActionState
*common
)
1664 ExternalSnapshotState
*state
=
1665 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1666 AioContext
*aio_context
;
1668 if (!state
->old_bs
) {
1672 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1673 aio_context_acquire(aio_context
);
1675 bdrv_drained_end(state
->old_bs
);
1676 bdrv_unref(state
->new_bs
);
1678 aio_context_release(aio_context
);
1681 typedef struct DriveBackupState
{
1682 BlkActionState common
;
1683 BlockDriverState
*bs
;
1687 static BlockJob
*do_backup_common(BackupCommon
*backup
,
1688 BlockDriverState
*bs
,
1689 BlockDriverState
*target_bs
,
1690 AioContext
*aio_context
,
1691 JobTxn
*txn
, Error
**errp
);
1693 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1695 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1696 DriveBackup
*backup
;
1697 BlockDriverState
*bs
;
1698 BlockDriverState
*target_bs
;
1699 BlockDriverState
*source
= NULL
;
1700 AioContext
*aio_context
;
1701 AioContext
*old_context
;
1703 Error
*local_err
= NULL
;
1706 bool set_backing_hd
= false;
1709 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1710 backup
= common
->action
->u
.drive_backup
.data
;
1712 if (!backup
->has_mode
) {
1713 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1716 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1722 error_setg(errp
, "Device has no medium");
1726 aio_context
= bdrv_get_aio_context(bs
);
1727 aio_context_acquire(aio_context
);
1729 /* Paired with .clean() */
1730 bdrv_drained_begin(bs
);
1732 if (!backup
->has_format
) {
1733 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
1734 NULL
: (char *) bs
->drv
->format_name
;
1737 /* Early check to avoid creating target */
1738 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
1742 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1745 * See if we have a backing HD we can use to create our new image
1748 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
1749 source
= backing_bs(bs
);
1751 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
1754 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
1756 flags
|= BDRV_O_NO_BACKING
;
1757 set_backing_hd
= true;
1760 size
= bdrv_getlength(bs
);
1762 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1766 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
1767 assert(backup
->format
);
1769 bdrv_refresh_filename(source
);
1770 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
1771 source
->drv
->format_name
, NULL
,
1772 size
, flags
, false, &local_err
);
1774 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
1775 size
, flags
, false, &local_err
);
1780 error_propagate(errp
, local_err
);
1784 options
= qdict_new();
1785 qdict_put_str(options
, "discard", "unmap");
1786 qdict_put_str(options
, "detect-zeroes", "unmap");
1787 if (backup
->format
) {
1788 qdict_put_str(options
, "driver", backup
->format
);
1791 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
1796 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1797 old_context
= bdrv_get_aio_context(target_bs
);
1798 aio_context_release(aio_context
);
1799 aio_context_acquire(old_context
);
1801 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1803 bdrv_unref(target_bs
);
1804 aio_context_release(old_context
);
1808 aio_context_release(old_context
);
1809 aio_context_acquire(aio_context
);
1811 if (set_backing_hd
) {
1812 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
1820 state
->job
= do_backup_common(qapi_DriveBackup_base(backup
),
1821 bs
, target_bs
, aio_context
,
1822 common
->block_job_txn
, errp
);
1825 bdrv_unref(target_bs
);
1827 aio_context_release(aio_context
);
1830 static void drive_backup_commit(BlkActionState
*common
)
1832 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1833 AioContext
*aio_context
;
1835 aio_context
= bdrv_get_aio_context(state
->bs
);
1836 aio_context_acquire(aio_context
);
1839 job_start(&state
->job
->job
);
1841 aio_context_release(aio_context
);
1844 static void drive_backup_abort(BlkActionState
*common
)
1846 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1849 AioContext
*aio_context
;
1851 aio_context
= bdrv_get_aio_context(state
->bs
);
1852 aio_context_acquire(aio_context
);
1854 job_cancel_sync(&state
->job
->job
);
1856 aio_context_release(aio_context
);
1860 static void drive_backup_clean(BlkActionState
*common
)
1862 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1863 AioContext
*aio_context
;
1869 aio_context
= bdrv_get_aio_context(state
->bs
);
1870 aio_context_acquire(aio_context
);
1872 bdrv_drained_end(state
->bs
);
1874 aio_context_release(aio_context
);
1877 typedef struct BlockdevBackupState
{
1878 BlkActionState common
;
1879 BlockDriverState
*bs
;
1881 } BlockdevBackupState
;
1883 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1885 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1886 BlockdevBackup
*backup
;
1887 BlockDriverState
*bs
;
1888 BlockDriverState
*target_bs
;
1889 AioContext
*aio_context
;
1890 AioContext
*old_context
;
1893 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1894 backup
= common
->action
->u
.blockdev_backup
.data
;
1896 bs
= bdrv_lookup_bs(backup
->device
, backup
->device
, errp
);
1901 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1906 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
1907 aio_context
= bdrv_get_aio_context(bs
);
1908 old_context
= bdrv_get_aio_context(target_bs
);
1909 aio_context_acquire(old_context
);
1911 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
1913 aio_context_release(old_context
);
1917 aio_context_release(old_context
);
1918 aio_context_acquire(aio_context
);
1921 /* Paired with .clean() */
1922 bdrv_drained_begin(state
->bs
);
1924 state
->job
= do_backup_common(qapi_BlockdevBackup_base(backup
),
1925 bs
, target_bs
, aio_context
,
1926 common
->block_job_txn
, errp
);
1928 aio_context_release(aio_context
);
1931 static void blockdev_backup_commit(BlkActionState
*common
)
1933 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1934 AioContext
*aio_context
;
1936 aio_context
= bdrv_get_aio_context(state
->bs
);
1937 aio_context_acquire(aio_context
);
1940 job_start(&state
->job
->job
);
1942 aio_context_release(aio_context
);
1945 static void blockdev_backup_abort(BlkActionState
*common
)
1947 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1950 AioContext
*aio_context
;
1952 aio_context
= bdrv_get_aio_context(state
->bs
);
1953 aio_context_acquire(aio_context
);
1955 job_cancel_sync(&state
->job
->job
);
1957 aio_context_release(aio_context
);
1961 static void blockdev_backup_clean(BlkActionState
*common
)
1963 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1964 AioContext
*aio_context
;
1970 aio_context
= bdrv_get_aio_context(state
->bs
);
1971 aio_context_acquire(aio_context
);
1973 bdrv_drained_end(state
->bs
);
1975 aio_context_release(aio_context
);
1978 typedef struct BlockDirtyBitmapState
{
1979 BlkActionState common
;
1980 BdrvDirtyBitmap
*bitmap
;
1981 BlockDriverState
*bs
;
1985 } BlockDirtyBitmapState
;
1987 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1990 Error
*local_err
= NULL
;
1991 BlockDirtyBitmapAdd
*action
;
1992 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1995 if (action_check_completion_mode(common
, errp
) < 0) {
1999 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2000 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2001 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2002 action
->has_granularity
, action
->granularity
,
2003 action
->has_persistent
, action
->persistent
,
2004 action
->has_disabled
, action
->disabled
,
2008 state
->prepared
= true;
2010 error_propagate(errp
, local_err
);
2014 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2016 BlockDirtyBitmapAdd
*action
;
2017 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2020 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2021 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2022 * then the node reference and bitmap name must have been valid.
2024 if (state
->prepared
) {
2025 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2029 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2032 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2034 BlockDirtyBitmap
*action
;
2036 if (action_check_completion_mode(common
, errp
) < 0) {
2040 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2041 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2045 if (!state
->bitmap
) {
2049 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_DEFAULT
, errp
)) {
2053 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2056 static void block_dirty_bitmap_restore(BlkActionState
*common
)
2058 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2061 if (state
->backup
) {
2062 bdrv_restore_dirty_bitmap(state
->bitmap
, state
->backup
);
2066 static void block_dirty_bitmap_free_backup(BlkActionState
*common
)
2068 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2071 hbitmap_free(state
->backup
);
2074 static void block_dirty_bitmap_enable_prepare(BlkActionState
*common
,
2077 BlockDirtyBitmap
*action
;
2078 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2081 if (action_check_completion_mode(common
, errp
) < 0) {
2085 action
= common
->action
->u
.block_dirty_bitmap_enable
.data
;
2086 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2090 if (!state
->bitmap
) {
2094 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2098 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2099 bdrv_enable_dirty_bitmap(state
->bitmap
);
2102 static void block_dirty_bitmap_enable_abort(BlkActionState
*common
)
2104 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2107 if (!state
->was_enabled
) {
2108 bdrv_disable_dirty_bitmap(state
->bitmap
);
2112 static void block_dirty_bitmap_disable_prepare(BlkActionState
*common
,
2115 BlockDirtyBitmap
*action
;
2116 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2119 if (action_check_completion_mode(common
, errp
) < 0) {
2123 action
= common
->action
->u
.block_dirty_bitmap_disable
.data
;
2124 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2128 if (!state
->bitmap
) {
2132 if (bdrv_dirty_bitmap_check(state
->bitmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2136 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2137 bdrv_disable_dirty_bitmap(state
->bitmap
);
2140 static void block_dirty_bitmap_disable_abort(BlkActionState
*common
)
2142 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2145 if (state
->was_enabled
) {
2146 bdrv_enable_dirty_bitmap(state
->bitmap
);
2150 static void block_dirty_bitmap_merge_prepare(BlkActionState
*common
,
2153 BlockDirtyBitmapMerge
*action
;
2154 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2157 if (action_check_completion_mode(common
, errp
) < 0) {
2161 action
= common
->action
->u
.block_dirty_bitmap_merge
.data
;
2163 state
->bitmap
= block_dirty_bitmap_merge(action
->node
, action
->target
,
2164 action
->bitmaps
, &state
->backup
,
2168 static void block_dirty_bitmap_remove_prepare(BlkActionState
*common
,
2171 BlockDirtyBitmap
*action
;
2172 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2175 if (action_check_completion_mode(common
, errp
) < 0) {
2179 action
= common
->action
->u
.block_dirty_bitmap_remove
.data
;
2181 state
->bitmap
= block_dirty_bitmap_remove(action
->node
, action
->name
,
2182 false, &state
->bs
, errp
);
2183 if (state
->bitmap
) {
2184 bdrv_dirty_bitmap_skip_store(state
->bitmap
, true);
2185 bdrv_dirty_bitmap_set_busy(state
->bitmap
, true);
2189 static void block_dirty_bitmap_remove_abort(BlkActionState
*common
)
2191 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2194 if (state
->bitmap
) {
2195 bdrv_dirty_bitmap_skip_store(state
->bitmap
, false);
2196 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2200 static void block_dirty_bitmap_remove_commit(BlkActionState
*common
)
2202 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2205 bdrv_dirty_bitmap_set_busy(state
->bitmap
, false);
2206 bdrv_release_dirty_bitmap(state
->bitmap
);
2209 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2211 error_setg(errp
, "Transaction aborted using Abort action");
2214 static void abort_commit(BlkActionState
*common
)
2216 g_assert_not_reached(); /* this action never succeeds */
2219 static const BlkActionOps actions
[] = {
2220 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2221 .instance_size
= sizeof(ExternalSnapshotState
),
2222 .prepare
= external_snapshot_prepare
,
2223 .commit
= external_snapshot_commit
,
2224 .abort
= external_snapshot_abort
,
2225 .clean
= external_snapshot_clean
,
2227 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2228 .instance_size
= sizeof(ExternalSnapshotState
),
2229 .prepare
= external_snapshot_prepare
,
2230 .commit
= external_snapshot_commit
,
2231 .abort
= external_snapshot_abort
,
2232 .clean
= external_snapshot_clean
,
2234 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2235 .instance_size
= sizeof(DriveBackupState
),
2236 .prepare
= drive_backup_prepare
,
2237 .commit
= drive_backup_commit
,
2238 .abort
= drive_backup_abort
,
2239 .clean
= drive_backup_clean
,
2241 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2242 .instance_size
= sizeof(BlockdevBackupState
),
2243 .prepare
= blockdev_backup_prepare
,
2244 .commit
= blockdev_backup_commit
,
2245 .abort
= blockdev_backup_abort
,
2246 .clean
= blockdev_backup_clean
,
2248 [TRANSACTION_ACTION_KIND_ABORT
] = {
2249 .instance_size
= sizeof(BlkActionState
),
2250 .prepare
= abort_prepare
,
2251 .commit
= abort_commit
,
2253 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2254 .instance_size
= sizeof(InternalSnapshotState
),
2255 .prepare
= internal_snapshot_prepare
,
2256 .abort
= internal_snapshot_abort
,
2257 .clean
= internal_snapshot_clean
,
2259 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2260 .instance_size
= sizeof(BlockDirtyBitmapState
),
2261 .prepare
= block_dirty_bitmap_add_prepare
,
2262 .abort
= block_dirty_bitmap_add_abort
,
2264 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2265 .instance_size
= sizeof(BlockDirtyBitmapState
),
2266 .prepare
= block_dirty_bitmap_clear_prepare
,
2267 .commit
= block_dirty_bitmap_free_backup
,
2268 .abort
= block_dirty_bitmap_restore
,
2270 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ENABLE
] = {
2271 .instance_size
= sizeof(BlockDirtyBitmapState
),
2272 .prepare
= block_dirty_bitmap_enable_prepare
,
2273 .abort
= block_dirty_bitmap_enable_abort
,
2275 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_DISABLE
] = {
2276 .instance_size
= sizeof(BlockDirtyBitmapState
),
2277 .prepare
= block_dirty_bitmap_disable_prepare
,
2278 .abort
= block_dirty_bitmap_disable_abort
,
2280 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_MERGE
] = {
2281 .instance_size
= sizeof(BlockDirtyBitmapState
),
2282 .prepare
= block_dirty_bitmap_merge_prepare
,
2283 .commit
= block_dirty_bitmap_free_backup
,
2284 .abort
= block_dirty_bitmap_restore
,
2286 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_REMOVE
] = {
2287 .instance_size
= sizeof(BlockDirtyBitmapState
),
2288 .prepare
= block_dirty_bitmap_remove_prepare
,
2289 .commit
= block_dirty_bitmap_remove_commit
,
2290 .abort
= block_dirty_bitmap_remove_abort
,
2292 /* Where are transactions for MIRROR, COMMIT and STREAM?
2293 * Although these blockjobs use transaction callbacks like the backup job,
2294 * these jobs do not necessarily adhere to transaction semantics.
2295 * These jobs may not fully undo all of their actions on abort, nor do they
2296 * necessarily work in transactions with more than one job in them.
2301 * Allocate a TransactionProperties structure if necessary, and fill
2302 * that structure with desired defaults if they are unset.
2304 static TransactionProperties
*get_transaction_properties(
2305 TransactionProperties
*props
)
2308 props
= g_new0(TransactionProperties
, 1);
2311 if (!props
->has_completion_mode
) {
2312 props
->has_completion_mode
= true;
2313 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2320 * 'Atomic' group operations. The operations are performed as a set, and if
2321 * any fail then we roll back all operations in the group.
2323 void qmp_transaction(TransactionActionList
*dev_list
,
2325 struct TransactionProperties
*props
,
2328 TransactionActionList
*dev_entry
= dev_list
;
2329 JobTxn
*block_job_txn
= NULL
;
2330 BlkActionState
*state
, *next
;
2331 Error
*local_err
= NULL
;
2333 QTAILQ_HEAD(, BlkActionState
) snap_bdrv_states
;
2334 QTAILQ_INIT(&snap_bdrv_states
);
2336 /* Does this transaction get canceled as a group on failure?
2337 * If not, we don't really need to make a JobTxn.
2339 props
= get_transaction_properties(props
);
2340 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2341 block_job_txn
= job_txn_new();
2344 /* drain all i/o before any operations */
2347 /* We don't do anything in this loop that commits us to the operations */
2348 while (NULL
!= dev_entry
) {
2349 TransactionAction
*dev_info
= NULL
;
2350 const BlkActionOps
*ops
;
2352 dev_info
= dev_entry
->value
;
2353 dev_entry
= dev_entry
->next
;
2355 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2357 ops
= &actions
[dev_info
->type
];
2358 assert(ops
->instance_size
> 0);
2360 state
= g_malloc0(ops
->instance_size
);
2362 state
->action
= dev_info
;
2363 state
->block_job_txn
= block_job_txn
;
2364 state
->txn_props
= props
;
2365 QTAILQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2367 state
->ops
->prepare(state
, &local_err
);
2369 error_propagate(errp
, local_err
);
2370 goto delete_and_fail
;
2374 QTAILQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2375 if (state
->ops
->commit
) {
2376 state
->ops
->commit(state
);
2384 /* failure, and it is all-or-none; roll back all operations */
2385 QTAILQ_FOREACH_REVERSE(state
, &snap_bdrv_states
, entry
) {
2386 if (state
->ops
->abort
) {
2387 state
->ops
->abort(state
);
2391 QTAILQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2392 if (state
->ops
->clean
) {
2393 state
->ops
->clean(state
);
2398 qapi_free_TransactionProperties(props
);
2400 job_txn_unref(block_job_txn
);
2403 void qmp_block_passwd(bool has_device
, const char *device
,
2404 bool has_node_name
, const char *node_name
,
2405 const char *password
, Error
**errp
)
2408 "Setting block passwords directly is no longer supported");
2411 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2415 BdrvDirtyBitmap
*bitmap
;
2416 BlockDriverState
*bs
;
2417 BlockDirtyBitmapSha256
*ret
= NULL
;
2420 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2421 if (!bitmap
|| !bs
) {
2425 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2426 if (sha256
== NULL
) {
2430 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2431 ret
->sha256
= sha256
;
2436 void qmp_block_resize(bool has_device
, const char *device
,
2437 bool has_node_name
, const char *node_name
,
2438 int64_t size
, Error
**errp
)
2440 Error
*local_err
= NULL
;
2441 BlockBackend
*blk
= NULL
;
2442 BlockDriverState
*bs
;
2443 AioContext
*aio_context
;
2445 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2446 has_node_name
? node_name
: NULL
,
2449 error_propagate(errp
, local_err
);
2453 aio_context
= bdrv_get_aio_context(bs
);
2454 aio_context_acquire(aio_context
);
2457 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2461 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2462 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2466 blk
= blk_new_with_bs(bs
, BLK_PERM_RESIZE
, BLK_PERM_ALL
, errp
);
2471 bdrv_drained_begin(bs
);
2472 blk_truncate(blk
, size
, false, PREALLOC_MODE_OFF
, 0, errp
);
2473 bdrv_drained_end(bs
);
2477 aio_context_release(aio_context
);
2480 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
2481 bool has_base
, const char *base
,
2482 bool has_base_node
, const char *base_node
,
2483 bool has_backing_file
, const char *backing_file
,
2484 bool has_speed
, int64_t speed
,
2485 bool has_on_error
, BlockdevOnError on_error
,
2486 bool has_auto_finalize
, bool auto_finalize
,
2487 bool has_auto_dismiss
, bool auto_dismiss
,
2490 BlockDriverState
*bs
, *iter
;
2491 BlockDriverState
*base_bs
= NULL
;
2492 AioContext
*aio_context
;
2493 Error
*local_err
= NULL
;
2494 const char *base_name
= NULL
;
2495 int job_flags
= JOB_DEFAULT
;
2497 if (!has_on_error
) {
2498 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2501 bs
= bdrv_lookup_bs(device
, device
, errp
);
2506 aio_context
= bdrv_get_aio_context(bs
);
2507 aio_context_acquire(aio_context
);
2509 if (has_base
&& has_base_node
) {
2510 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2511 "at the same time");
2516 base_bs
= bdrv_find_backing_image(bs
, base
);
2517 if (base_bs
== NULL
) {
2518 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2521 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2525 if (has_base_node
) {
2526 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2530 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2531 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2535 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2536 bdrv_refresh_filename(base_bs
);
2537 base_name
= base_bs
->filename
;
2540 /* Check for op blockers in the whole chain between bs and base */
2541 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
2542 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2547 /* if we are streaming the entire chain, the result will have no backing
2548 * file, and specifying one is therefore an error */
2549 if (base_bs
== NULL
&& has_backing_file
) {
2550 error_setg(errp
, "backing file specified, but streaming the "
2555 /* backing_file string overrides base bs filename */
2556 base_name
= has_backing_file
? backing_file
: base_name
;
2558 if (has_auto_finalize
&& !auto_finalize
) {
2559 job_flags
|= JOB_MANUAL_FINALIZE
;
2561 if (has_auto_dismiss
&& !auto_dismiss
) {
2562 job_flags
|= JOB_MANUAL_DISMISS
;
2565 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
2566 job_flags
, has_speed
? speed
: 0, on_error
, &local_err
);
2568 error_propagate(errp
, local_err
);
2572 trace_qmp_block_stream(bs
);
2575 aio_context_release(aio_context
);
2578 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
2579 bool has_base_node
, const char *base_node
,
2580 bool has_base
, const char *base
,
2581 bool has_top_node
, const char *top_node
,
2582 bool has_top
, const char *top
,
2583 bool has_backing_file
, const char *backing_file
,
2584 bool has_speed
, int64_t speed
,
2585 bool has_on_error
, BlockdevOnError on_error
,
2586 bool has_filter_node_name
, const char *filter_node_name
,
2587 bool has_auto_finalize
, bool auto_finalize
,
2588 bool has_auto_dismiss
, bool auto_dismiss
,
2591 BlockDriverState
*bs
;
2592 BlockDriverState
*iter
;
2593 BlockDriverState
*base_bs
, *top_bs
;
2594 AioContext
*aio_context
;
2595 Error
*local_err
= NULL
;
2596 int job_flags
= JOB_DEFAULT
;
2601 if (!has_on_error
) {
2602 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2604 if (!has_filter_node_name
) {
2605 filter_node_name
= NULL
;
2607 if (has_auto_finalize
&& !auto_finalize
) {
2608 job_flags
|= JOB_MANUAL_FINALIZE
;
2610 if (has_auto_dismiss
&& !auto_dismiss
) {
2611 job_flags
|= JOB_MANUAL_DISMISS
;
2615 * libvirt relies on the DeviceNotFound error class in order to probe for
2616 * live commit feature versions; for this to work, we must make sure to
2617 * perform the device lookup before any generic errors that may occur in a
2618 * scenario in which all optional arguments are omitted. */
2619 bs
= qmp_get_root_bs(device
, &local_err
);
2621 bs
= bdrv_lookup_bs(device
, device
, NULL
);
2623 error_free(local_err
);
2624 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2625 "Device '%s' not found", device
);
2627 error_propagate(errp
, local_err
);
2632 aio_context
= bdrv_get_aio_context(bs
);
2633 aio_context_acquire(aio_context
);
2635 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2639 /* default top_bs is the active layer */
2642 if (has_top_node
&& has_top
) {
2643 error_setg(errp
, "'top-node' and 'top' are mutually exclusive");
2645 } else if (has_top_node
) {
2646 top_bs
= bdrv_lookup_bs(NULL
, top_node
, errp
);
2647 if (top_bs
== NULL
) {
2650 if (!bdrv_chain_contains(bs
, top_bs
)) {
2651 error_setg(errp
, "'%s' is not in this backing file chain",
2655 } else if (has_top
&& top
) {
2656 /* This strcmp() is just a shortcut, there is no need to
2657 * refresh @bs's filename. If it mismatches,
2658 * bdrv_find_backing_image() will do the refresh and may still
2660 if (strcmp(bs
->filename
, top
) != 0) {
2661 top_bs
= bdrv_find_backing_image(bs
, top
);
2665 if (top_bs
== NULL
) {
2666 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2670 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2672 if (has_base_node
&& has_base
) {
2673 error_setg(errp
, "'base-node' and 'base' are mutually exclusive");
2675 } else if (has_base_node
) {
2676 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2677 if (base_bs
== NULL
) {
2680 if (!bdrv_chain_contains(top_bs
, base_bs
)) {
2681 error_setg(errp
, "'%s' is not in this backing file chain",
2685 } else if (has_base
&& base
) {
2686 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2688 base_bs
= bdrv_find_base(top_bs
);
2691 if (base_bs
== NULL
) {
2692 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2696 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2698 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
2699 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2704 /* Do not allow attempts to commit an image into itself */
2705 if (top_bs
== base_bs
) {
2706 error_setg(errp
, "cannot commit an image into itself");
2711 if (has_backing_file
) {
2712 error_setg(errp
, "'backing-file' specified,"
2713 " but 'top' is the active layer");
2716 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
2717 job_flags
, speed
, on_error
,
2718 filter_node_name
, NULL
, NULL
, false, &local_err
);
2720 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
2721 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2724 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, job_flags
,
2725 speed
, on_error
, has_backing_file
? backing_file
: NULL
,
2726 filter_node_name
, &local_err
);
2728 if (local_err
!= NULL
) {
2729 error_propagate(errp
, local_err
);
2734 aio_context_release(aio_context
);
2737 /* Common QMP interface for drive-backup and blockdev-backup */
2738 static BlockJob
*do_backup_common(BackupCommon
*backup
,
2739 BlockDriverState
*bs
,
2740 BlockDriverState
*target_bs
,
2741 AioContext
*aio_context
,
2742 JobTxn
*txn
, Error
**errp
)
2744 BlockJob
*job
= NULL
;
2745 BdrvDirtyBitmap
*bmap
= NULL
;
2746 int job_flags
= JOB_DEFAULT
;
2748 if (!backup
->has_speed
) {
2751 if (!backup
->has_on_source_error
) {
2752 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2754 if (!backup
->has_on_target_error
) {
2755 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2757 if (!backup
->has_job_id
) {
2758 backup
->job_id
= NULL
;
2760 if (!backup
->has_auto_finalize
) {
2761 backup
->auto_finalize
= true;
2763 if (!backup
->has_auto_dismiss
) {
2764 backup
->auto_dismiss
= true;
2766 if (!backup
->has_compress
) {
2767 backup
->compress
= false;
2770 if ((backup
->sync
== MIRROR_SYNC_MODE_BITMAP
) ||
2771 (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
)) {
2772 /* done before desugaring 'incremental' to print the right message */
2773 if (!backup
->has_bitmap
) {
2774 error_setg(errp
, "must provide a valid bitmap name for "
2775 "'%s' sync mode", MirrorSyncMode_str(backup
->sync
));
2780 if (backup
->sync
== MIRROR_SYNC_MODE_INCREMENTAL
) {
2781 if (backup
->has_bitmap_mode
&&
2782 backup
->bitmap_mode
!= BITMAP_SYNC_MODE_ON_SUCCESS
) {
2783 error_setg(errp
, "Bitmap sync mode must be '%s' "
2784 "when using sync mode '%s'",
2785 BitmapSyncMode_str(BITMAP_SYNC_MODE_ON_SUCCESS
),
2786 MirrorSyncMode_str(backup
->sync
));
2789 backup
->has_bitmap_mode
= true;
2790 backup
->sync
= MIRROR_SYNC_MODE_BITMAP
;
2791 backup
->bitmap_mode
= BITMAP_SYNC_MODE_ON_SUCCESS
;
2794 if (backup
->has_bitmap
) {
2795 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
2797 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
2800 if (!backup
->has_bitmap_mode
) {
2801 error_setg(errp
, "Bitmap sync mode must be given "
2802 "when providing a bitmap");
2805 if (bdrv_dirty_bitmap_check(bmap
, BDRV_BITMAP_ALLOW_RO
, errp
)) {
2809 /* This does not produce a useful bitmap artifact: */
2810 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
2811 error_setg(errp
, "sync mode '%s' does not produce meaningful bitmap"
2812 " outputs", MirrorSyncMode_str(backup
->sync
));
2816 /* If the bitmap isn't used for input or output, this is useless: */
2817 if (backup
->bitmap_mode
== BITMAP_SYNC_MODE_NEVER
&&
2818 backup
->sync
!= MIRROR_SYNC_MODE_BITMAP
) {
2819 error_setg(errp
, "Bitmap sync mode '%s' has no meaningful effect"
2820 " when combined with sync mode '%s'",
2821 BitmapSyncMode_str(backup
->bitmap_mode
),
2822 MirrorSyncMode_str(backup
->sync
));
2827 if (!backup
->has_bitmap
&& backup
->has_bitmap_mode
) {
2828 error_setg(errp
, "Cannot specify bitmap sync mode without a bitmap");
2832 if (!backup
->auto_finalize
) {
2833 job_flags
|= JOB_MANUAL_FINALIZE
;
2835 if (!backup
->auto_dismiss
) {
2836 job_flags
|= JOB_MANUAL_DISMISS
;
2839 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
2840 backup
->sync
, bmap
, backup
->bitmap_mode
,
2842 backup
->filter_node_name
,
2843 backup
->on_source_error
,
2844 backup
->on_target_error
,
2845 job_flags
, NULL
, NULL
, txn
, errp
);
2849 void qmp_drive_backup(DriveBackup
*backup
, Error
**errp
)
2851 TransactionAction action
= {
2852 .type
= TRANSACTION_ACTION_KIND_DRIVE_BACKUP
,
2853 .u
.drive_backup
.data
= backup
,
2855 blockdev_do_action(&action
, errp
);
2858 BlockDeviceInfoList
*qmp_query_named_block_nodes(bool has_flat
,
2862 bool return_flat
= has_flat
&& flat
;
2864 return bdrv_named_nodes_list(return_flat
, errp
);
2867 XDbgBlockGraph
*qmp_x_debug_query_block_graph(Error
**errp
)
2869 return bdrv_get_xdbg_block_graph(errp
);
2872 void qmp_blockdev_backup(BlockdevBackup
*backup
, Error
**errp
)
2874 TransactionAction action
= {
2875 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
,
2876 .u
.blockdev_backup
.data
= backup
,
2878 blockdev_do_action(&action
, errp
);
2881 /* Parameter check and block job starting for drive mirroring.
2882 * Caller should hold @device and @target's aio context (must be the same).
2884 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
2885 BlockDriverState
*target
,
2886 bool has_replaces
, const char *replaces
,
2887 enum MirrorSyncMode sync
,
2888 BlockMirrorBackingMode backing_mode
,
2890 bool has_speed
, int64_t speed
,
2891 bool has_granularity
, uint32_t granularity
,
2892 bool has_buf_size
, int64_t buf_size
,
2893 bool has_on_source_error
,
2894 BlockdevOnError on_source_error
,
2895 bool has_on_target_error
,
2896 BlockdevOnError on_target_error
,
2897 bool has_unmap
, bool unmap
,
2898 bool has_filter_node_name
,
2899 const char *filter_node_name
,
2900 bool has_copy_mode
, MirrorCopyMode copy_mode
,
2901 bool has_auto_finalize
, bool auto_finalize
,
2902 bool has_auto_dismiss
, bool auto_dismiss
,
2905 int job_flags
= JOB_DEFAULT
;
2910 if (!has_on_source_error
) {
2911 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2913 if (!has_on_target_error
) {
2914 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2916 if (!has_granularity
) {
2919 if (!has_buf_size
) {
2925 if (!has_filter_node_name
) {
2926 filter_node_name
= NULL
;
2928 if (!has_copy_mode
) {
2929 copy_mode
= MIRROR_COPY_MODE_BACKGROUND
;
2931 if (has_auto_finalize
&& !auto_finalize
) {
2932 job_flags
|= JOB_MANUAL_FINALIZE
;
2934 if (has_auto_dismiss
&& !auto_dismiss
) {
2935 job_flags
|= JOB_MANUAL_DISMISS
;
2938 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2939 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2940 "a value in range [512B, 64MB]");
2943 if (granularity
& (granularity
- 1)) {
2944 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2949 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
2952 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
2956 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2957 sync
= MIRROR_SYNC_MODE_FULL
;
2961 BlockDriverState
*to_replace_bs
;
2962 AioContext
*replace_aio_context
;
2963 int64_t bs_size
, replace_size
;
2965 bs_size
= bdrv_getlength(bs
);
2967 error_setg_errno(errp
, -bs_size
, "Failed to query device's size");
2971 to_replace_bs
= check_to_replace_node(bs
, replaces
, errp
);
2972 if (!to_replace_bs
) {
2976 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
2977 aio_context_acquire(replace_aio_context
);
2978 replace_size
= bdrv_getlength(to_replace_bs
);
2979 aio_context_release(replace_aio_context
);
2981 if (replace_size
< 0) {
2982 error_setg_errno(errp
, -replace_size
,
2983 "Failed to query the replacement node's size");
2986 if (bs_size
!= replace_size
) {
2987 error_setg(errp
, "cannot replace image with a mirror image of "
2993 /* pass the node name to replace to mirror start since it's loose coupling
2994 * and will allow to check whether the node still exist at mirror completion
2996 mirror_start(job_id
, bs
, target
,
2997 has_replaces
? replaces
: NULL
, job_flags
,
2998 speed
, granularity
, buf_size
, sync
, backing_mode
, zero_target
,
2999 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3003 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3005 BlockDriverState
*bs
;
3006 BlockDriverState
*source
, *target_bs
;
3007 AioContext
*aio_context
;
3008 AioContext
*old_context
;
3009 BlockMirrorBackingMode backing_mode
;
3010 Error
*local_err
= NULL
;
3011 QDict
*options
= NULL
;
3014 const char *format
= arg
->format
;
3018 bs
= qmp_get_root_bs(arg
->device
, errp
);
3023 /* Early check to avoid creating target */
3024 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3028 aio_context
= bdrv_get_aio_context(bs
);
3029 aio_context_acquire(aio_context
);
3031 if (!arg
->has_mode
) {
3032 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3035 if (!arg
->has_format
) {
3036 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3037 ? NULL
: bs
->drv
->format_name
);
3040 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3041 source
= backing_bs(bs
);
3042 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3043 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3045 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3049 size
= bdrv_getlength(bs
);
3051 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3055 if (arg
->has_replaces
) {
3056 if (!arg
->has_node_name
) {
3057 error_setg(errp
, "a node-name must be provided when replacing a"
3058 " named node of the graph");
3063 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3064 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3066 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3069 /* Don't open backing image in create() */
3070 flags
|= BDRV_O_NO_BACKING
;
3072 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3073 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3075 /* create new image w/o backing file */
3077 bdrv_img_create(arg
->target
, format
,
3078 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3080 switch (arg
->mode
) {
3081 case NEW_IMAGE_MODE_EXISTING
:
3083 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3084 /* create new image with backing file */
3085 bdrv_refresh_filename(source
);
3086 bdrv_img_create(arg
->target
, format
,
3088 source
->drv
->format_name
,
3089 NULL
, size
, flags
, false, &local_err
);
3097 error_propagate(errp
, local_err
);
3101 options
= qdict_new();
3102 if (arg
->has_node_name
) {
3103 qdict_put_str(options
, "node-name", arg
->node_name
);
3106 qdict_put_str(options
, "driver", format
);
3109 /* Mirroring takes care of copy-on-write using the source's backing
3112 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3117 zero_target
= (arg
->sync
== MIRROR_SYNC_MODE_FULL
&&
3118 (arg
->mode
== NEW_IMAGE_MODE_EXISTING
||
3119 !bdrv_has_zero_init(target_bs
)));
3122 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3123 old_context
= bdrv_get_aio_context(target_bs
);
3124 aio_context_release(aio_context
);
3125 aio_context_acquire(old_context
);
3127 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3129 bdrv_unref(target_bs
);
3130 aio_context_release(old_context
);
3134 aio_context_release(old_context
);
3135 aio_context_acquire(aio_context
);
3137 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3138 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3139 backing_mode
, zero_target
,
3140 arg
->has_speed
, arg
->speed
,
3141 arg
->has_granularity
, arg
->granularity
,
3142 arg
->has_buf_size
, arg
->buf_size
,
3143 arg
->has_on_source_error
, arg
->on_source_error
,
3144 arg
->has_on_target_error
, arg
->on_target_error
,
3145 arg
->has_unmap
, arg
->unmap
,
3147 arg
->has_copy_mode
, arg
->copy_mode
,
3148 arg
->has_auto_finalize
, arg
->auto_finalize
,
3149 arg
->has_auto_dismiss
, arg
->auto_dismiss
,
3151 bdrv_unref(target_bs
);
3152 error_propagate(errp
, local_err
);
3154 aio_context_release(aio_context
);
3157 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3158 const char *device
, const char *target
,
3159 bool has_replaces
, const char *replaces
,
3160 MirrorSyncMode sync
,
3161 bool has_speed
, int64_t speed
,
3162 bool has_granularity
, uint32_t granularity
,
3163 bool has_buf_size
, int64_t buf_size
,
3164 bool has_on_source_error
,
3165 BlockdevOnError on_source_error
,
3166 bool has_on_target_error
,
3167 BlockdevOnError on_target_error
,
3168 bool has_filter_node_name
,
3169 const char *filter_node_name
,
3170 bool has_copy_mode
, MirrorCopyMode copy_mode
,
3171 bool has_auto_finalize
, bool auto_finalize
,
3172 bool has_auto_dismiss
, bool auto_dismiss
,
3175 BlockDriverState
*bs
;
3176 BlockDriverState
*target_bs
;
3177 AioContext
*aio_context
;
3178 AioContext
*old_context
;
3179 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3180 Error
*local_err
= NULL
;
3184 bs
= qmp_get_root_bs(device
, errp
);
3189 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3194 zero_target
= (sync
== MIRROR_SYNC_MODE_FULL
);
3196 /* Honor bdrv_try_set_aio_context() context acquisition requirements. */
3197 old_context
= bdrv_get_aio_context(target_bs
);
3198 aio_context
= bdrv_get_aio_context(bs
);
3199 aio_context_acquire(old_context
);
3201 ret
= bdrv_try_set_aio_context(target_bs
, aio_context
, errp
);
3203 aio_context_release(old_context
);
3204 aio_context_acquire(aio_context
);
3210 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3211 has_replaces
, replaces
, sync
, backing_mode
,
3212 zero_target
, has_speed
, speed
,
3213 has_granularity
, granularity
,
3214 has_buf_size
, buf_size
,
3215 has_on_source_error
, on_source_error
,
3216 has_on_target_error
, on_target_error
,
3218 has_filter_node_name
, filter_node_name
,
3219 has_copy_mode
, copy_mode
,
3220 has_auto_finalize
, auto_finalize
,
3221 has_auto_dismiss
, auto_dismiss
,
3223 error_propagate(errp
, local_err
);
3225 aio_context_release(aio_context
);
3228 /* Get a block job using its ID and acquire its AioContext */
3229 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3236 *aio_context
= NULL
;
3238 job
= block_job_get(id
);
3241 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3242 "Block job '%s' not found", id
);
3246 *aio_context
= blk_get_aio_context(job
->blk
);
3247 aio_context_acquire(*aio_context
);
3252 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3254 AioContext
*aio_context
;
3255 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3261 block_job_set_speed(job
, speed
, errp
);
3262 aio_context_release(aio_context
);
3265 void qmp_block_job_cancel(const char *device
,
3266 bool has_force
, bool force
, Error
**errp
)
3268 AioContext
*aio_context
;
3269 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3279 if (job_user_paused(&job
->job
) && !force
) {
3280 error_setg(errp
, "The block job for device '%s' is currently paused",
3285 trace_qmp_block_job_cancel(job
);
3286 job_user_cancel(&job
->job
, force
, errp
);
3288 aio_context_release(aio_context
);
3291 void qmp_block_job_pause(const char *device
, Error
**errp
)
3293 AioContext
*aio_context
;
3294 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3300 trace_qmp_block_job_pause(job
);
3301 job_user_pause(&job
->job
, errp
);
3302 aio_context_release(aio_context
);
3305 void qmp_block_job_resume(const char *device
, Error
**errp
)
3307 AioContext
*aio_context
;
3308 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3314 trace_qmp_block_job_resume(job
);
3315 job_user_resume(&job
->job
, errp
);
3316 aio_context_release(aio_context
);
3319 void qmp_block_job_complete(const char *device
, Error
**errp
)
3321 AioContext
*aio_context
;
3322 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3328 trace_qmp_block_job_complete(job
);
3329 job_complete(&job
->job
, errp
);
3330 aio_context_release(aio_context
);
3333 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3335 AioContext
*aio_context
;
3336 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3342 trace_qmp_block_job_finalize(job
);
3344 job_finalize(&job
->job
, errp
);
3347 * Job's context might have changed via job_finalize (and job_txn_apply
3348 * automatically acquires the new one), so make sure we release the correct
3351 aio_context
= blk_get_aio_context(job
->blk
);
3352 job_unref(&job
->job
);
3353 aio_context_release(aio_context
);
3356 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3358 AioContext
*aio_context
;
3359 BlockJob
*bjob
= find_block_job(id
, &aio_context
, errp
);
3366 trace_qmp_block_job_dismiss(bjob
);
3368 job_dismiss(&job
, errp
);
3369 aio_context_release(aio_context
);
3372 void qmp_change_backing_file(const char *device
,
3373 const char *image_node_name
,
3374 const char *backing_file
,
3377 BlockDriverState
*bs
= NULL
;
3378 AioContext
*aio_context
;
3379 BlockDriverState
*image_bs
= NULL
;
3380 Error
*local_err
= NULL
;
3384 bs
= qmp_get_root_bs(device
, errp
);
3389 aio_context
= bdrv_get_aio_context(bs
);
3390 aio_context_acquire(aio_context
);
3392 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3394 error_propagate(errp
, local_err
);
3399 error_setg(errp
, "image file not found");
3403 if (bdrv_find_base(image_bs
) == image_bs
) {
3404 error_setg(errp
, "not allowing backing file change on an image "
3405 "without a backing file");
3409 /* even though we are not necessarily operating on bs, we need it to
3410 * determine if block ops are currently prohibited on the chain */
3411 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3415 /* final sanity check */
3416 if (!bdrv_chain_contains(bs
, image_bs
)) {
3417 error_setg(errp
, "'%s' and image file are not in the same chain",
3422 /* if not r/w, reopen to make r/w */
3423 ro
= bdrv_is_read_only(image_bs
);
3426 if (bdrv_reopen_set_read_only(image_bs
, false, errp
) != 0) {
3431 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3432 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3435 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3437 /* don't exit here, so we can try to restore open flags if
3442 bdrv_reopen_set_read_only(image_bs
, true, &local_err
);
3443 error_propagate(errp
, local_err
);
3447 aio_context_release(aio_context
);
3450 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3452 BlockDriverState
*bs
;
3454 Visitor
*v
= qobject_output_visitor_new(&obj
);
3457 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3458 visit_complete(v
, &obj
);
3459 qdict
= qobject_to(QDict
, obj
);
3461 qdict_flatten(qdict
);
3463 if (!qdict_get_try_str(qdict
, "node-name")) {
3464 error_setg(errp
, "'node-name' must be specified for the root node");
3468 bs
= bds_tree_init(qdict
, errp
);
3473 bdrv_set_monitor_owned(bs
);
3479 void qmp_x_blockdev_reopen(BlockdevOptions
*options
, Error
**errp
)
3481 BlockDriverState
*bs
;
3484 Visitor
*v
= qobject_output_visitor_new(&obj
);
3485 BlockReopenQueue
*queue
;
3488 /* Check for the selected node name */
3489 if (!options
->has_node_name
) {
3490 error_setg(errp
, "Node name not specified");
3494 bs
= bdrv_find_node(options
->node_name
);
3496 error_setg(errp
, "Cannot find node named '%s'", options
->node_name
);
3500 /* Put all options in a QDict and flatten it */
3501 visit_type_BlockdevOptions(v
, NULL
, &options
, &error_abort
);
3502 visit_complete(v
, &obj
);
3503 qdict
= qobject_to(QDict
, obj
);
3505 qdict_flatten(qdict
);
3507 /* Perform the reopen operation */
3508 ctx
= bdrv_get_aio_context(bs
);
3509 aio_context_acquire(ctx
);
3510 bdrv_subtree_drained_begin(bs
);
3511 queue
= bdrv_reopen_queue(NULL
, bs
, qdict
, false);
3512 bdrv_reopen_multiple(queue
, errp
);
3513 bdrv_subtree_drained_end(bs
);
3514 aio_context_release(ctx
);
3520 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3522 AioContext
*aio_context
;
3523 BlockDriverState
*bs
;
3525 bs
= bdrv_find_node(node_name
);
3527 error_setg(errp
, "Cannot find node %s", node_name
);
3530 if (bdrv_has_blk(bs
)) {
3531 error_setg(errp
, "Node %s is in use", node_name
);
3534 aio_context
= bdrv_get_aio_context(bs
);
3535 aio_context_acquire(aio_context
);
3537 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3541 if (!QTAILQ_IN_USE(bs
, monitor_list
)) {
3542 error_setg(errp
, "Node %s is not owned by the monitor",
3547 if (bs
->refcnt
> 1) {
3548 error_setg(errp
, "Block device %s is in use",
3549 bdrv_get_device_or_node_name(bs
));
3553 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3557 aio_context_release(aio_context
);
3560 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3561 const char *child_name
)
3565 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3566 if (strcmp(child
->name
, child_name
) == 0) {
3574 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
3575 const char *child
, bool has_node
,
3576 const char *node
, Error
**errp
)
3578 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3581 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3586 if (has_child
== has_node
) {
3588 error_setg(errp
, "The parameters child and node are in conflict");
3590 error_setg(errp
, "Either child or node must be specified");
3596 p_child
= bdrv_find_child(parent_bs
, child
);
3598 error_setg(errp
, "Node '%s' does not have child '%s'",
3602 bdrv_del_child(parent_bs
, p_child
, errp
);
3606 new_bs
= bdrv_find_node(node
);
3608 error_setg(errp
, "Node '%s' not found", node
);
3611 bdrv_add_child(parent_bs
, new_bs
, errp
);
3615 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3617 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3620 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
3621 BlockJobInfoList
*elem
;
3622 AioContext
*aio_context
;
3624 if (block_job_is_internal(job
)) {
3627 elem
= g_new0(BlockJobInfoList
, 1);
3628 aio_context
= blk_get_aio_context(job
->blk
);
3629 aio_context_acquire(aio_context
);
3630 elem
->value
= block_job_query(job
, errp
);
3631 aio_context_release(aio_context
);
3634 qapi_free_BlockJobInfoList(head
);
3638 p_next
= &elem
->next
;
3644 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
3645 bool has_force
, bool force
, Error
**errp
)
3647 AioContext
*old_context
;
3648 AioContext
*new_context
;
3649 BlockDriverState
*bs
;
3651 bs
= bdrv_find_node(node_name
);
3653 error_setg(errp
, "Cannot find node %s", node_name
);
3657 /* Protects against accidents. */
3658 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
3659 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
3660 "be in use (use force=true to override this check)",
3665 if (iothread
->type
== QTYPE_QSTRING
) {
3666 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
3668 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
3672 new_context
= iothread_get_aio_context(obj
);
3674 new_context
= qemu_get_aio_context();
3677 old_context
= bdrv_get_aio_context(bs
);
3678 aio_context_acquire(old_context
);
3680 bdrv_try_set_aio_context(bs
, new_context
, errp
);
3682 aio_context_release(old_context
);
3685 QemuOptsList qemu_common_drive_opts
= {
3687 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3691 .type
= QEMU_OPT_BOOL
,
3692 .help
= "enable/disable snapshot mode",
3695 .type
= QEMU_OPT_STRING
,
3696 .help
= "host AIO implementation (threads, native, io_uring)",
3698 .name
= BDRV_OPT_CACHE_WB
,
3699 .type
= QEMU_OPT_BOOL
,
3700 .help
= "Enable writeback mode",
3703 .type
= QEMU_OPT_STRING
,
3704 .help
= "disk format (raw, qcow2, ...)",
3707 .type
= QEMU_OPT_STRING
,
3708 .help
= "read error action",
3711 .type
= QEMU_OPT_STRING
,
3712 .help
= "write error action",
3714 .name
= BDRV_OPT_READ_ONLY
,
3715 .type
= QEMU_OPT_BOOL
,
3716 .help
= "open drive file as read-only",
3722 .name
= "throttling.group",
3723 .type
= QEMU_OPT_STRING
,
3724 .help
= "name of the block throttling group",
3726 .name
= "copy-on-read",
3727 .type
= QEMU_OPT_BOOL
,
3728 .help
= "copy read data from backing file into image file",
3730 .name
= "detect-zeroes",
3731 .type
= QEMU_OPT_STRING
,
3732 .help
= "try to optimize zero writes (off, on, unmap)",
3734 .name
= "stats-account-invalid",
3735 .type
= QEMU_OPT_BOOL
,
3736 .help
= "whether to account for invalid I/O operations "
3737 "in the statistics",
3739 .name
= "stats-account-failed",
3740 .type
= QEMU_OPT_BOOL
,
3741 .help
= "whether to account for failed I/O operations "
3742 "in the statistics",
3744 { /* end of list */ }
3748 QemuOptsList qemu_drive_opts
= {
3750 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3753 * no elements => accept any params
3754 * validation will happen later
3756 { /* end of list */ }