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/config-file.h"
44 #include "qapi/qapi-commands-block.h"
45 #include "qapi/qapi-commands-transaction.h"
46 #include "qapi/qapi-visit-block-core.h"
47 #include "qapi/qmp/qdict.h"
48 #include "qapi/qmp/qnum.h"
49 #include "qapi/qmp/qstring.h"
50 #include "qapi/error.h"
51 #include "qapi/qmp/qerror.h"
52 #include "qapi/qmp/qlist.h"
53 #include "qapi/qobject-output-visitor.h"
54 #include "sysemu/sysemu.h"
55 #include "sysemu/iothread.h"
56 #include "block/block_int.h"
57 #include "block/trace.h"
58 #include "sysemu/arch_init.h"
59 #include "sysemu/qtest.h"
60 #include "qemu/cutils.h"
61 #include "qemu/help_option.h"
62 #include "qemu/throttle-options.h"
64 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
65 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
67 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
68 bool force
, Error
**errp
);
69 static void blockdev_remove_medium(bool has_device
, const char *device
,
70 bool has_id
, const char *id
, Error
**errp
);
71 static void blockdev_insert_medium(bool has_device
, const char *device
,
72 bool has_id
, const char *id
,
73 const char *node_name
, Error
**errp
);
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
);
142 BlockDriverState
*bs
= blk_bs(blk
);
143 AioContext
*aio_context
;
150 aio_context
= bdrv_get_aio_context(bs
);
151 aio_context_acquire(aio_context
);
154 job_cancel(&bs
->job
->job
, false);
157 aio_context_release(aio_context
);
163 void blockdev_auto_del(BlockBackend
*blk
)
165 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
167 if (dinfo
&& dinfo
->auto_del
) {
168 monitor_remove_blk(blk
);
174 * Returns the current mapping of how many units per bus
175 * a particular interface can support.
177 * A positive integer indicates n units per bus.
178 * 0 implies the mapping has not been established.
179 * -1 indicates an invalid BlockInterfaceType was given.
181 int drive_get_max_devs(BlockInterfaceType type
)
183 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
184 return if_max_devs
[type
];
190 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
192 int max_devs
= if_max_devs
[type
];
193 return max_devs
? index
/ max_devs
: 0;
196 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
198 int max_devs
= if_max_devs
[type
];
199 return max_devs
? index
% max_devs
: index
;
202 QemuOpts
*drive_def(const char *optstr
)
204 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
207 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
212 opts
= drive_def(optstr
);
216 if (type
!= IF_DEFAULT
) {
217 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
220 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
223 qemu_opt_set(opts
, "file", file
, &error_abort
);
227 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
232 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
233 dinfo
= blk_legacy_dinfo(blk
);
234 if (dinfo
&& dinfo
->type
== type
235 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
243 void drive_check_orphaned(void)
248 bool orphans
= false;
250 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
251 dinfo
= blk_legacy_dinfo(blk
);
252 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
253 dinfo
->type
!= IF_NONE
) {
255 qemu_opts_loc_restore(dinfo
->opts
);
256 error_report("machine type does not support"
257 " if=%s,bus=%d,unit=%d",
258 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
269 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
271 return drive_get(type
,
272 drive_index_to_bus_id(type
, index
),
273 drive_index_to_unit_id(type
, index
));
276 int drive_get_max_bus(BlockInterfaceType type
)
283 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
284 dinfo
= blk_legacy_dinfo(blk
);
285 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
286 max_bus
= dinfo
->bus
;
292 /* Get a block device. This should only be used for single-drive devices
293 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
295 DriveInfo
*drive_get_next(BlockInterfaceType type
)
297 static int next_block_unit
[IF_COUNT
];
299 return drive_get(type
, 0, next_block_unit
[type
]++);
302 static void bdrv_format_print(void *opaque
, const char *name
)
304 error_printf(" %s", name
);
309 BlockDriverState
*bs
;
312 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
314 if (!strcmp(buf
, "ignore")) {
315 return BLOCKDEV_ON_ERROR_IGNORE
;
316 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
317 return BLOCKDEV_ON_ERROR_ENOSPC
;
318 } else if (!strcmp(buf
, "stop")) {
319 return BLOCKDEV_ON_ERROR_STOP
;
320 } else if (!strcmp(buf
, "report")) {
321 return BLOCKDEV_ON_ERROR_REPORT
;
323 error_setg(errp
, "'%s' invalid %s error action",
324 buf
, is_read
? "read" : "write");
329 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
332 const QListEntry
*entry
;
333 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
334 switch (qobject_type(entry
->value
)) {
336 case QTYPE_QSTRING
: {
337 unsigned long long length
;
338 const char *str
= qstring_get_str(qobject_to(QString
,
340 if (parse_uint_full(str
, &length
, 10) == 0 &&
341 length
> 0 && length
<= UINT_MAX
) {
342 block_acct_add_interval(stats
, (unsigned) length
);
344 error_setg(errp
, "Invalid interval length: %s", str
);
351 int64_t length
= qnum_get_int(qobject_to(QNum
, entry
->value
));
353 if (length
> 0 && length
<= UINT_MAX
) {
354 block_acct_add_interval(stats
, (unsigned) length
);
356 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
363 error_setg(errp
, "The specification of stats-intervals is invalid");
370 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
372 /* All parameters but @opts are optional and may be set to NULL. */
373 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
374 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
375 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
377 Error
*local_error
= NULL
;
381 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
382 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
385 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
386 if (!strcmp(aio
, "native")) {
387 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
388 } else if (!strcmp(aio
, "threads")) {
389 /* this is the default */
391 error_setg(errp
, "invalid aio option");
397 /* disk I/O throttling */
398 if (throttling_group
) {
399 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
403 throttle_config_init(throttle_cfg
);
404 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
405 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
406 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
407 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
408 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
409 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
410 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
411 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
412 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
413 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
414 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
415 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
417 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
418 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
419 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
420 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
421 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
422 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
424 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
425 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
426 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
427 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
428 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
430 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
431 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
432 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
433 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
434 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
435 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
436 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
437 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
438 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
439 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
440 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
441 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
443 throttle_cfg
->op_size
=
444 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
446 if (!throttle_is_valid(throttle_cfg
, errp
)) {
453 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
454 qemu_opt_get(opts
, "detect-zeroes"),
455 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
458 error_propagate(errp
, local_error
);
464 /* Takes the ownership of bs_opts */
465 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
470 int on_read_error
, on_write_error
;
471 bool account_invalid
, account_failed
;
472 bool writethrough
, read_only
;
474 BlockDriverState
*bs
;
479 QDict
*interval_dict
= NULL
;
480 QList
*interval_list
= NULL
;
482 BlockdevDetectZeroesOptions detect_zeroes
=
483 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
484 const char *throttling_group
= NULL
;
486 /* Check common options by copying from bs_opts to opts, all other options
487 * stay in bs_opts for processing by bdrv_open(). */
488 id
= qdict_get_try_str(bs_opts
, "id");
489 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
491 error_propagate(errp
, error
);
495 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
497 error_propagate(errp
, error
);
502 qdict_del(bs_opts
, "id");
505 /* extract parameters */
506 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
508 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
509 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
511 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
513 id
= qemu_opts_id(opts
);
515 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
516 qdict_array_split(interval_dict
, &interval_list
);
518 if (qdict_size(interval_dict
) != 0) {
519 error_setg(errp
, "Invalid option stats-intervals.%s",
520 qdict_first(interval_dict
)->key
);
524 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
525 &detect_zeroes
, &error
);
527 error_propagate(errp
, error
);
531 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
532 if (is_help_option(buf
)) {
533 error_printf("Supported formats:");
534 bdrv_iterate_format(bdrv_format_print
, NULL
);
539 if (qdict_haskey(bs_opts
, "driver")) {
540 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
543 qdict_put_str(bs_opts
, "driver", buf
);
546 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
547 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
548 on_write_error
= parse_block_error_action(buf
, 0, &error
);
550 error_propagate(errp
, error
);
555 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
556 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
557 on_read_error
= parse_block_error_action(buf
, 1, &error
);
559 error_propagate(errp
, error
);
565 bdrv_flags
|= BDRV_O_SNAPSHOT
;
568 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
571 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
572 BlockBackendRootState
*blk_rs
;
574 blk
= blk_new(0, BLK_PERM_ALL
);
575 blk_rs
= blk_get_root_state(blk
);
576 blk_rs
->open_flags
= bdrv_flags
;
577 blk_rs
->read_only
= read_only
;
578 blk_rs
->detect_zeroes
= detect_zeroes
;
580 qobject_unref(bs_opts
);
582 if (file
&& !*file
) {
586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
589 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
590 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
591 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
592 read_only
? "on" : "off");
593 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
595 if (runstate_check(RUN_STATE_INMIGRATE
)) {
596 bdrv_flags
|= BDRV_O_INACTIVE
;
599 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
605 bs
->detect_zeroes
= detect_zeroes
;
607 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
609 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
616 /* disk I/O throttling */
617 if (throttle_enabled(&cfg
)) {
618 if (!throttling_group
) {
619 throttling_group
= id
;
621 blk_io_limits_enable(blk
, throttling_group
);
622 blk_set_io_limits(blk
, &cfg
);
625 blk_set_enable_write_cache(blk
, !writethrough
);
626 blk_set_on_error(blk
, on_read_error
, on_write_error
);
628 if (!monitor_add_blk(blk
, id
, errp
)) {
636 qobject_unref(interval_dict
);
637 qobject_unref(interval_list
);
642 qobject_unref(interval_dict
);
643 qobject_unref(interval_list
);
645 qobject_unref(bs_opts
);
649 /* Takes the ownership of bs_opts */
650 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
654 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
655 * with other callers) rather than what we want as the real defaults.
656 * Apply the defaults here instead. */
657 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
658 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
659 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
661 if (runstate_check(RUN_STATE_INMIGRATE
)) {
662 bdrv_flags
|= BDRV_O_INACTIVE
;
665 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
668 void blockdev_close_all_bdrv_states(void)
670 BlockDriverState
*bs
, *next_bs
;
672 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
673 AioContext
*ctx
= bdrv_get_aio_context(bs
);
675 aio_context_acquire(ctx
);
677 aio_context_release(ctx
);
681 /* Iterates over the list of monitor-owned BlockDriverStates */
682 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
684 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
685 : QTAILQ_FIRST(&monitor_bdrv_states
);
688 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
693 value
= qemu_opt_get(opts
, from
);
695 if (qemu_opt_find(opts
, to
)) {
696 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
697 "same time", to
, from
);
702 /* rename all items in opts */
703 while ((value
= qemu_opt_get(opts
, from
))) {
704 qemu_opt_set(opts
, to
, value
, &error_abort
);
705 qemu_opt_unset(opts
, from
);
709 QemuOptsList qemu_legacy_drive_opts
= {
711 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
715 .type
= QEMU_OPT_NUMBER
,
716 .help
= "bus number",
719 .type
= QEMU_OPT_NUMBER
,
720 .help
= "unit number (i.e. lun for scsi)",
723 .type
= QEMU_OPT_NUMBER
,
724 .help
= "index number",
727 .type
= QEMU_OPT_STRING
,
728 .help
= "media type (disk, cdrom)",
731 .type
= QEMU_OPT_STRING
,
732 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
735 .type
= QEMU_OPT_STRING
,
736 .help
= "disk serial number",
739 .type
= QEMU_OPT_STRING
,
743 /* Options that are passed on, but have special semantics with -drive */
745 .name
= BDRV_OPT_READ_ONLY
,
746 .type
= QEMU_OPT_BOOL
,
747 .help
= "open drive file as read-only",
750 .type
= QEMU_OPT_STRING
,
751 .help
= "read error action",
754 .type
= QEMU_OPT_STRING
,
755 .help
= "write error action",
757 .name
= "copy-on-read",
758 .type
= QEMU_OPT_BOOL
,
759 .help
= "copy read data from backing file into image file",
762 { /* end of list */ }
766 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
770 DriveInfo
*dinfo
= NULL
;
772 QemuOpts
*legacy_opts
;
773 DriveMediaType media
= MEDIA_DISK
;
774 BlockInterfaceType type
;
775 int max_devs
, bus_id
, unit_id
, index
;
776 const char *werror
, *rerror
;
777 bool read_only
= false;
780 const char *filename
;
781 Error
*local_err
= NULL
;
783 const char *deprecated
[] = {
787 /* Change legacy command line options into QMP ones */
788 static const struct {
792 { "iops", "throttling.iops-total" },
793 { "iops_rd", "throttling.iops-read" },
794 { "iops_wr", "throttling.iops-write" },
796 { "bps", "throttling.bps-total" },
797 { "bps_rd", "throttling.bps-read" },
798 { "bps_wr", "throttling.bps-write" },
800 { "iops_max", "throttling.iops-total-max" },
801 { "iops_rd_max", "throttling.iops-read-max" },
802 { "iops_wr_max", "throttling.iops-write-max" },
804 { "bps_max", "throttling.bps-total-max" },
805 { "bps_rd_max", "throttling.bps-read-max" },
806 { "bps_wr_max", "throttling.bps-write-max" },
808 { "iops_size", "throttling.iops-size" },
810 { "group", "throttling.group" },
812 { "readonly", BDRV_OPT_READ_ONLY
},
815 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
816 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
819 error_report_err(local_err
);
824 value
= qemu_opt_get(all_opts
, "cache");
829 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
830 error_report("invalid cache option");
834 /* Specific options take precedence */
835 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
836 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
837 !writethrough
, &error_abort
);
839 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
840 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
841 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
843 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
844 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
845 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
847 qemu_opt_unset(all_opts
, "cache");
850 /* Get a QDict for processing the options */
851 bs_opts
= qdict_new();
852 qemu_opts_to_qdict(all_opts
, bs_opts
);
854 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
856 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
858 error_report_err(local_err
);
862 /* Other deprecated options */
863 if (!qtest_enabled()) {
864 for (i
= 0; i
< ARRAY_SIZE(deprecated
); i
++) {
865 if (qemu_opt_get(legacy_opts
, deprecated
[i
]) != NULL
) {
866 error_report("'%s' is deprecated, please use the corresponding "
867 "option of '-device' instead", deprecated
[i
]);
873 value
= qemu_opt_get(legacy_opts
, "media");
875 if (!strcmp(value
, "disk")) {
877 } else if (!strcmp(value
, "cdrom")) {
881 error_report("'%s' invalid media", value
);
886 /* copy-on-read is disabled with a warning for read-only devices */
887 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
888 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
890 if (read_only
&& copy_on_read
) {
891 warn_report("disabling copy-on-read on read-only drive");
892 copy_on_read
= false;
895 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
896 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
898 /* Controller type */
899 value
= qemu_opt_get(legacy_opts
, "if");
902 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
905 if (type
== IF_COUNT
) {
906 error_report("unsupported bus type '%s'", value
);
910 type
= block_default_type
;
913 /* Device address specified by bus/unit or index.
914 * If none was specified, try to find the first free one. */
915 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
916 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
917 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
919 max_devs
= if_max_devs
[type
];
922 if (bus_id
!= 0 || unit_id
!= -1) {
923 error_report("index cannot be used with bus and unit");
926 bus_id
= drive_index_to_bus_id(type
, index
);
927 unit_id
= drive_index_to_unit_id(type
, index
);
932 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
934 if (max_devs
&& unit_id
>= max_devs
) {
941 if (max_devs
&& unit_id
>= max_devs
) {
942 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
946 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
947 error_report("drive with bus=%d, unit=%d (index=%d) exists",
948 bus_id
, unit_id
, index
);
953 serial
= qemu_opt_get(legacy_opts
, "serial");
955 /* no id supplied -> create one */
956 if (qemu_opts_id(all_opts
) == NULL
) {
958 const char *mediastr
= "";
959 if (type
== IF_IDE
|| type
== IF_SCSI
) {
960 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
963 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
966 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
969 qdict_put_str(bs_opts
, "id", new_id
);
973 /* Add virtio block device */
974 if (type
== IF_VIRTIO
) {
976 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
978 if (arch_type
== QEMU_ARCH_S390X
) {
979 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
981 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
983 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
987 filename
= qemu_opt_get(legacy_opts
, "file");
989 /* Check werror/rerror compatibility with if=... */
990 werror
= qemu_opt_get(legacy_opts
, "werror");
991 if (werror
!= NULL
) {
992 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
994 error_report("werror is not supported by this bus type");
997 qdict_put_str(bs_opts
, "werror", werror
);
1000 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1001 if (rerror
!= NULL
) {
1002 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1004 error_report("rerror is not supported by this bus type");
1007 qdict_put_str(bs_opts
, "rerror", rerror
);
1010 /* Actual block device init: Functionality shared with blockdev-add */
1011 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1015 error_report_err(local_err
);
1022 /* Create legacy DriveInfo */
1023 dinfo
= g_malloc0(sizeof(*dinfo
));
1024 dinfo
->opts
= all_opts
;
1027 dinfo
->bus
= bus_id
;
1028 dinfo
->unit
= unit_id
;
1029 dinfo
->serial
= g_strdup(serial
);
1031 blk_set_legacy_dinfo(blk
, dinfo
);
1038 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1045 qemu_opts_del(legacy_opts
);
1046 qobject_unref(bs_opts
);
1050 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1052 BlockDriverState
*bs
;
1054 bs
= bdrv_lookup_bs(name
, name
, errp
);
1059 if (!bdrv_is_root_node(bs
)) {
1060 error_setg(errp
, "Need a root block node");
1064 if (!bdrv_is_inserted(bs
)) {
1065 error_setg(errp
, "Device has no medium");
1072 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1077 if (!blk_name
== !qdev_id
) {
1078 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1083 blk
= blk_by_qdev_id(qdev_id
, errp
);
1085 blk
= blk_by_name(blk_name
);
1087 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1088 "Device '%s' not found", blk_name
);
1095 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1097 const char *device
= qdict_get_str(qdict
, "device");
1101 if (!strcmp(device
, "all")) {
1102 ret
= blk_commit_all();
1104 BlockDriverState
*bs
;
1105 AioContext
*aio_context
;
1107 blk
= blk_by_name(device
);
1109 monitor_printf(mon
, "Device '%s' not found\n", device
);
1112 if (!blk_is_available(blk
)) {
1113 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1118 aio_context
= bdrv_get_aio_context(bs
);
1119 aio_context_acquire(aio_context
);
1121 ret
= bdrv_commit(bs
);
1123 aio_context_release(aio_context
);
1126 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1131 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1133 TransactionActionList list
;
1135 list
.value
= action
;
1137 qmp_transaction(&list
, false, NULL
, errp
);
1140 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1141 bool has_node_name
, const char *node_name
,
1142 const char *snapshot_file
,
1143 bool has_snapshot_node_name
,
1144 const char *snapshot_node_name
,
1145 bool has_format
, const char *format
,
1146 bool has_mode
, NewImageMode mode
, Error
**errp
)
1148 BlockdevSnapshotSync snapshot
= {
1149 .has_device
= has_device
,
1150 .device
= (char *) device
,
1151 .has_node_name
= has_node_name
,
1152 .node_name
= (char *) node_name
,
1153 .snapshot_file
= (char *) snapshot_file
,
1154 .has_snapshot_node_name
= has_snapshot_node_name
,
1155 .snapshot_node_name
= (char *) snapshot_node_name
,
1156 .has_format
= has_format
,
1157 .format
= (char *) format
,
1158 .has_mode
= has_mode
,
1161 TransactionAction action
= {
1162 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1163 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1165 blockdev_do_action(&action
, errp
);
1168 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1171 BlockdevSnapshot snapshot_data
= {
1172 .node
= (char *) node
,
1173 .overlay
= (char *) overlay
1175 TransactionAction action
= {
1176 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1177 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1179 blockdev_do_action(&action
, errp
);
1182 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1186 BlockdevSnapshotInternal snapshot
= {
1187 .device
= (char *) device
,
1188 .name
= (char *) name
1190 TransactionAction action
= {
1191 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1192 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1194 blockdev_do_action(&action
, errp
);
1197 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1204 BlockDriverState
*bs
;
1205 AioContext
*aio_context
;
1206 QEMUSnapshotInfo sn
;
1207 Error
*local_err
= NULL
;
1208 SnapshotInfo
*info
= NULL
;
1211 bs
= qmp_get_root_bs(device
, errp
);
1215 aio_context
= bdrv_get_aio_context(bs
);
1216 aio_context_acquire(aio_context
);
1227 error_setg(errp
, "Name or id must be provided");
1228 goto out_aio_context
;
1231 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1232 goto out_aio_context
;
1235 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1237 error_propagate(errp
, local_err
);
1238 goto out_aio_context
;
1242 "Snapshot with id '%s' and name '%s' does not exist on "
1244 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1245 goto out_aio_context
;
1248 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1250 error_propagate(errp
, local_err
);
1251 goto out_aio_context
;
1254 aio_context_release(aio_context
);
1256 info
= g_new0(SnapshotInfo
, 1);
1257 info
->id
= g_strdup(sn
.id_str
);
1258 info
->name
= g_strdup(sn
.name
);
1259 info
->date_nsec
= sn
.date_nsec
;
1260 info
->date_sec
= sn
.date_sec
;
1261 info
->vm_state_size
= sn
.vm_state_size
;
1262 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1263 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1268 aio_context_release(aio_context
);
1273 * block_dirty_bitmap_lookup:
1274 * Return a dirty bitmap (if present), after validating
1275 * the node reference and bitmap names.
1277 * @node: The name of the BDS node to search for bitmaps
1278 * @name: The name of the bitmap to search for
1279 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1280 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1281 * @errp: Output pointer for error information. Can be NULL.
1283 * @return: A bitmap object on success, or NULL on failure.
1285 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1287 BlockDriverState
**pbs
,
1290 BlockDriverState
*bs
;
1291 BdrvDirtyBitmap
*bitmap
;
1294 error_setg(errp
, "Node cannot be NULL");
1298 error_setg(errp
, "Bitmap name cannot be NULL");
1301 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1303 error_setg(errp
, "Node '%s' not found", node
);
1307 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1309 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1320 /* New and old BlockDriverState structs for atomic group operations */
1322 typedef struct BlkActionState BlkActionState
;
1326 * Table of operations that define an Action.
1328 * @instance_size: Size of state struct, in bytes.
1329 * @prepare: Prepare the work, must NOT be NULL.
1330 * @commit: Commit the changes, can be NULL.
1331 * @abort: Abort the changes on fail, can be NULL.
1332 * @clean: Clean up resources after all transaction actions have called
1333 * commit() or abort(). Can be NULL.
1335 * Only prepare() may fail. In a single transaction, only one of commit() or
1336 * abort() will be called. clean() will always be called if it is present.
1338 typedef struct BlkActionOps
{
1339 size_t instance_size
;
1340 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1341 void (*commit
)(BlkActionState
*common
);
1342 void (*abort
)(BlkActionState
*common
);
1343 void (*clean
)(BlkActionState
*common
);
1348 * Describes one Action's state within a Transaction.
1350 * @action: QAPI-defined enum identifying which Action to perform.
1351 * @ops: Table of ActionOps this Action can perform.
1352 * @block_job_txn: Transaction which this action belongs to.
1353 * @entry: List membership for all Actions in this Transaction.
1355 * This structure must be arranged as first member in a subclassed type,
1356 * assuming that the compiler will also arrange it to the same offsets as the
1359 struct BlkActionState
{
1360 TransactionAction
*action
;
1361 const BlkActionOps
*ops
;
1362 JobTxn
*block_job_txn
;
1363 TransactionProperties
*txn_props
;
1364 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1367 /* internal snapshot private data */
1368 typedef struct InternalSnapshotState
{
1369 BlkActionState common
;
1370 BlockDriverState
*bs
;
1371 QEMUSnapshotInfo sn
;
1373 } InternalSnapshotState
;
1376 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1378 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1380 "Action '%s' does not support Transaction property "
1381 "completion-mode = %s",
1382 TransactionActionKind_str(s
->action
->type
),
1383 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1389 static void internal_snapshot_prepare(BlkActionState
*common
,
1392 Error
*local_err
= NULL
;
1395 BlockDriverState
*bs
;
1396 QEMUSnapshotInfo old_sn
, *sn
;
1399 BlockdevSnapshotInternal
*internal
;
1400 InternalSnapshotState
*state
;
1401 AioContext
*aio_context
;
1404 g_assert(common
->action
->type
==
1405 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1406 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1407 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1409 /* 1. parse input */
1410 device
= internal
->device
;
1411 name
= internal
->name
;
1413 /* 2. check for validation */
1414 if (action_check_completion_mode(common
, errp
) < 0) {
1418 bs
= qmp_get_root_bs(device
, errp
);
1423 aio_context
= bdrv_get_aio_context(bs
);
1424 aio_context_acquire(aio_context
);
1428 /* Paired with .clean() */
1429 bdrv_drained_begin(bs
);
1431 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1435 if (bdrv_is_read_only(bs
)) {
1436 error_setg(errp
, "Device '%s' is read only", device
);
1440 if (!bdrv_can_snapshot(bs
)) {
1441 error_setg(errp
, "Block format '%s' used by device '%s' "
1442 "does not support internal snapshots",
1443 bs
->drv
->format_name
, device
);
1447 if (!strlen(name
)) {
1448 error_setg(errp
, "Name is empty");
1452 /* check whether a snapshot with name exist */
1453 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1456 error_propagate(errp
, local_err
);
1460 "Snapshot with name '%s' already exists on device '%s'",
1465 /* 3. take the snapshot */
1467 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1468 qemu_gettimeofday(&tv
);
1469 sn
->date_sec
= tv
.tv_sec
;
1470 sn
->date_nsec
= tv
.tv_usec
* 1000;
1471 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1473 ret1
= bdrv_snapshot_create(bs
, sn
);
1475 error_setg_errno(errp
, -ret1
,
1476 "Failed to create snapshot '%s' on device '%s'",
1481 /* 4. succeed, mark a snapshot is created */
1482 state
->created
= true;
1485 aio_context_release(aio_context
);
1488 static void internal_snapshot_abort(BlkActionState
*common
)
1490 InternalSnapshotState
*state
=
1491 DO_UPCAST(InternalSnapshotState
, common
, common
);
1492 BlockDriverState
*bs
= state
->bs
;
1493 QEMUSnapshotInfo
*sn
= &state
->sn
;
1494 AioContext
*aio_context
;
1495 Error
*local_error
= NULL
;
1497 if (!state
->created
) {
1501 aio_context
= bdrv_get_aio_context(state
->bs
);
1502 aio_context_acquire(aio_context
);
1504 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1505 error_reportf_err(local_error
,
1506 "Failed to delete snapshot with id '%s' and "
1507 "name '%s' on device '%s' in abort: ",
1508 sn
->id_str
, sn
->name
,
1509 bdrv_get_device_name(bs
));
1512 aio_context_release(aio_context
);
1515 static void internal_snapshot_clean(BlkActionState
*common
)
1517 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1519 AioContext
*aio_context
;
1525 aio_context
= bdrv_get_aio_context(state
->bs
);
1526 aio_context_acquire(aio_context
);
1528 bdrv_drained_end(state
->bs
);
1530 aio_context_release(aio_context
);
1533 /* external snapshot private data */
1534 typedef struct ExternalSnapshotState
{
1535 BlkActionState common
;
1536 BlockDriverState
*old_bs
;
1537 BlockDriverState
*new_bs
;
1538 bool overlay_appended
;
1539 } ExternalSnapshotState
;
1541 static void external_snapshot_prepare(BlkActionState
*common
,
1545 QDict
*options
= NULL
;
1546 Error
*local_err
= NULL
;
1547 /* Device and node name of the image to generate the snapshot from */
1549 const char *node_name
;
1550 /* Reference to the new image (for 'blockdev-snapshot') */
1551 const char *snapshot_ref
;
1552 /* File name of the new image (for 'blockdev-snapshot-sync') */
1553 const char *new_image_file
;
1554 ExternalSnapshotState
*state
=
1555 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1556 TransactionAction
*action
= common
->action
;
1557 AioContext
*aio_context
;
1559 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1560 * purpose but a different set of parameters */
1561 switch (action
->type
) {
1562 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1564 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1566 node_name
= s
->node
;
1567 new_image_file
= NULL
;
1568 snapshot_ref
= s
->overlay
;
1571 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1573 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1574 device
= s
->has_device
? s
->device
: NULL
;
1575 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1576 new_image_file
= s
->snapshot_file
;
1577 snapshot_ref
= NULL
;
1581 g_assert_not_reached();
1584 /* start processing */
1585 if (action_check_completion_mode(common
, errp
) < 0) {
1589 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1590 if (!state
->old_bs
) {
1594 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1595 aio_context_acquire(aio_context
);
1597 /* Paired with .clean() */
1598 bdrv_drained_begin(state
->old_bs
);
1600 if (!bdrv_is_inserted(state
->old_bs
)) {
1601 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1605 if (bdrv_op_is_blocked(state
->old_bs
,
1606 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1610 if (!bdrv_is_read_only(state
->old_bs
)) {
1611 if (bdrv_flush(state
->old_bs
)) {
1612 error_setg(errp
, QERR_IO_ERROR
);
1617 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1618 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1622 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1623 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1624 const char *format
= s
->has_format
? s
->format
: "qcow2";
1625 enum NewImageMode mode
;
1626 const char *snapshot_node_name
=
1627 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1629 if (node_name
&& !snapshot_node_name
) {
1630 error_setg(errp
, "New snapshot node name missing");
1634 if (snapshot_node_name
&&
1635 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1636 error_setg(errp
, "New snapshot node name already in use");
1640 flags
= state
->old_bs
->open_flags
;
1641 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1642 flags
|= BDRV_O_NO_BACKING
;
1644 /* create new image w/backing file */
1645 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1646 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1647 int64_t size
= bdrv_getlength(state
->old_bs
);
1649 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1652 bdrv_img_create(new_image_file
, format
,
1653 state
->old_bs
->filename
,
1654 state
->old_bs
->drv
->format_name
,
1655 NULL
, size
, flags
, false, &local_err
);
1657 error_propagate(errp
, local_err
);
1662 options
= qdict_new();
1663 if (s
->has_snapshot_node_name
) {
1664 qdict_put_str(options
, "node-name", snapshot_node_name
);
1666 qdict_put_str(options
, "driver", format
);
1669 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1671 /* We will manually add the backing_hd field to the bs later */
1672 if (!state
->new_bs
) {
1676 if (bdrv_has_blk(state
->new_bs
)) {
1677 error_setg(errp
, "The snapshot is already in use");
1681 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1686 if (state
->new_bs
->backing
!= NULL
) {
1687 error_setg(errp
, "The snapshot already has a backing image");
1691 if (!state
->new_bs
->drv
->supports_backing
) {
1692 error_setg(errp
, "The snapshot does not support backing images");
1696 bdrv_set_aio_context(state
->new_bs
, aio_context
);
1698 /* This removes our old bs and adds the new bs. This is an operation that
1699 * can fail, so we need to do it in .prepare; undoing it for abort is
1700 * always possible. */
1701 bdrv_ref(state
->new_bs
);
1702 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1704 error_propagate(errp
, local_err
);
1707 state
->overlay_appended
= true;
1710 aio_context_release(aio_context
);
1713 static void external_snapshot_commit(BlkActionState
*common
)
1715 ExternalSnapshotState
*state
=
1716 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1717 AioContext
*aio_context
;
1719 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1720 aio_context_acquire(aio_context
);
1722 /* We don't need (or want) to use the transactional
1723 * bdrv_reopen_multiple() across all the entries at once, because we
1724 * don't want to abort all of them if one of them fails the reopen */
1725 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1726 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1730 aio_context_release(aio_context
);
1733 static void external_snapshot_abort(BlkActionState
*common
)
1735 ExternalSnapshotState
*state
=
1736 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1737 if (state
->new_bs
) {
1738 if (state
->overlay_appended
) {
1739 AioContext
*aio_context
;
1741 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1742 aio_context_acquire(aio_context
);
1744 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1745 close state->old_bs; we need it */
1746 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1747 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1748 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1750 aio_context_release(aio_context
);
1755 static void external_snapshot_clean(BlkActionState
*common
)
1757 ExternalSnapshotState
*state
=
1758 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1759 AioContext
*aio_context
;
1761 if (!state
->old_bs
) {
1765 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1766 aio_context_acquire(aio_context
);
1768 bdrv_drained_end(state
->old_bs
);
1769 bdrv_unref(state
->new_bs
);
1771 aio_context_release(aio_context
);
1774 typedef struct DriveBackupState
{
1775 BlkActionState common
;
1776 BlockDriverState
*bs
;
1780 static BlockJob
*do_drive_backup(DriveBackup
*backup
, JobTxn
*txn
,
1783 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1785 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1786 BlockDriverState
*bs
;
1787 DriveBackup
*backup
;
1788 AioContext
*aio_context
;
1789 Error
*local_err
= NULL
;
1791 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1792 backup
= common
->action
->u
.drive_backup
.data
;
1794 bs
= qmp_get_root_bs(backup
->device
, errp
);
1799 aio_context
= bdrv_get_aio_context(bs
);
1800 aio_context_acquire(aio_context
);
1802 /* Paired with .clean() */
1803 bdrv_drained_begin(bs
);
1807 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1809 error_propagate(errp
, local_err
);
1814 aio_context_release(aio_context
);
1817 static void drive_backup_commit(BlkActionState
*common
)
1819 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1820 AioContext
*aio_context
;
1822 aio_context
= bdrv_get_aio_context(state
->bs
);
1823 aio_context_acquire(aio_context
);
1826 job_start(&state
->job
->job
);
1828 aio_context_release(aio_context
);
1831 static void drive_backup_abort(BlkActionState
*common
)
1833 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1836 AioContext
*aio_context
;
1838 aio_context
= bdrv_get_aio_context(state
->bs
);
1839 aio_context_acquire(aio_context
);
1841 job_cancel_sync(&state
->job
->job
);
1843 aio_context_release(aio_context
);
1847 static void drive_backup_clean(BlkActionState
*common
)
1849 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1850 AioContext
*aio_context
;
1856 aio_context
= bdrv_get_aio_context(state
->bs
);
1857 aio_context_acquire(aio_context
);
1859 bdrv_drained_end(state
->bs
);
1861 aio_context_release(aio_context
);
1864 typedef struct BlockdevBackupState
{
1865 BlkActionState common
;
1866 BlockDriverState
*bs
;
1868 } BlockdevBackupState
;
1870 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, JobTxn
*txn
,
1873 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1875 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1876 BlockdevBackup
*backup
;
1877 BlockDriverState
*bs
, *target
;
1878 AioContext
*aio_context
;
1879 Error
*local_err
= NULL
;
1881 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1882 backup
= common
->action
->u
.blockdev_backup
.data
;
1884 bs
= qmp_get_root_bs(backup
->device
, errp
);
1889 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1894 aio_context
= bdrv_get_aio_context(bs
);
1895 if (aio_context
!= bdrv_get_aio_context(target
)) {
1896 error_setg(errp
, "Backup between two IO threads is not implemented");
1899 aio_context_acquire(aio_context
);
1902 /* Paired with .clean() */
1903 bdrv_drained_begin(state
->bs
);
1905 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1907 error_propagate(errp
, local_err
);
1912 aio_context_release(aio_context
);
1915 static void blockdev_backup_commit(BlkActionState
*common
)
1917 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1918 AioContext
*aio_context
;
1920 aio_context
= bdrv_get_aio_context(state
->bs
);
1921 aio_context_acquire(aio_context
);
1924 job_start(&state
->job
->job
);
1926 aio_context_release(aio_context
);
1929 static void blockdev_backup_abort(BlkActionState
*common
)
1931 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
);
1939 job_cancel_sync(&state
->job
->job
);
1941 aio_context_release(aio_context
);
1945 static void blockdev_backup_clean(BlkActionState
*common
)
1947 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1948 AioContext
*aio_context
;
1954 aio_context
= bdrv_get_aio_context(state
->bs
);
1955 aio_context_acquire(aio_context
);
1957 bdrv_drained_end(state
->bs
);
1959 aio_context_release(aio_context
);
1962 typedef struct BlockDirtyBitmapState
{
1963 BlkActionState common
;
1964 BdrvDirtyBitmap
*bitmap
;
1965 BlockDriverState
*bs
;
1969 } BlockDirtyBitmapState
;
1971 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1974 Error
*local_err
= NULL
;
1975 BlockDirtyBitmapAdd
*action
;
1976 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1979 if (action_check_completion_mode(common
, errp
) < 0) {
1983 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1984 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1985 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1986 action
->has_granularity
, action
->granularity
,
1987 action
->has_persistent
, action
->persistent
,
1988 action
->has_autoload
, action
->autoload
,
1989 action
->has_x_disabled
, action
->x_disabled
,
1993 state
->prepared
= true;
1995 error_propagate(errp
, local_err
);
1999 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2001 BlockDirtyBitmapAdd
*action
;
2002 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2005 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2006 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2007 * then the node reference and bitmap name must have been valid.
2009 if (state
->prepared
) {
2010 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2014 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2017 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2019 BlockDirtyBitmap
*action
;
2021 if (action_check_completion_mode(common
, errp
) < 0) {
2025 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2026 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2030 if (!state
->bitmap
) {
2034 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2035 error_setg(errp
, "Cannot modify a frozen bitmap");
2037 } else if (bdrv_dirty_bitmap_qmp_locked(state
->bitmap
)) {
2038 error_setg(errp
, "Cannot modify a locked bitmap");
2040 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2041 error_setg(errp
, "Cannot clear a disabled bitmap");
2043 } else if (bdrv_dirty_bitmap_readonly(state
->bitmap
)) {
2044 error_setg(errp
, "Cannot clear a readonly bitmap");
2048 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2051 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2053 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2056 if (state
->backup
) {
2057 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2061 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2063 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2066 hbitmap_free(state
->backup
);
2069 static void block_dirty_bitmap_enable_prepare(BlkActionState
*common
,
2072 BlockDirtyBitmap
*action
;
2073 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2076 if (action_check_completion_mode(common
, errp
) < 0) {
2080 action
= common
->action
->u
.x_block_dirty_bitmap_enable
.data
;
2081 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2085 if (!state
->bitmap
) {
2089 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2090 bdrv_enable_dirty_bitmap(state
->bitmap
);
2093 static void block_dirty_bitmap_enable_abort(BlkActionState
*common
)
2095 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2098 if (!state
->was_enabled
) {
2099 bdrv_disable_dirty_bitmap(state
->bitmap
);
2103 static void block_dirty_bitmap_disable_prepare(BlkActionState
*common
,
2106 BlockDirtyBitmap
*action
;
2107 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2110 if (action_check_completion_mode(common
, errp
) < 0) {
2114 action
= common
->action
->u
.x_block_dirty_bitmap_disable
.data
;
2115 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2119 if (!state
->bitmap
) {
2123 state
->was_enabled
= bdrv_dirty_bitmap_enabled(state
->bitmap
);
2124 bdrv_disable_dirty_bitmap(state
->bitmap
);
2127 static void block_dirty_bitmap_disable_abort(BlkActionState
*common
)
2129 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2132 if (state
->was_enabled
) {
2133 bdrv_enable_dirty_bitmap(state
->bitmap
);
2137 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2139 error_setg(errp
, "Transaction aborted using Abort action");
2142 static void abort_commit(BlkActionState
*common
)
2144 g_assert_not_reached(); /* this action never succeeds */
2147 static const BlkActionOps actions
[] = {
2148 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2149 .instance_size
= sizeof(ExternalSnapshotState
),
2150 .prepare
= external_snapshot_prepare
,
2151 .commit
= external_snapshot_commit
,
2152 .abort
= external_snapshot_abort
,
2153 .clean
= external_snapshot_clean
,
2155 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2156 .instance_size
= sizeof(ExternalSnapshotState
),
2157 .prepare
= external_snapshot_prepare
,
2158 .commit
= external_snapshot_commit
,
2159 .abort
= external_snapshot_abort
,
2160 .clean
= external_snapshot_clean
,
2162 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2163 .instance_size
= sizeof(DriveBackupState
),
2164 .prepare
= drive_backup_prepare
,
2165 .commit
= drive_backup_commit
,
2166 .abort
= drive_backup_abort
,
2167 .clean
= drive_backup_clean
,
2169 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2170 .instance_size
= sizeof(BlockdevBackupState
),
2171 .prepare
= blockdev_backup_prepare
,
2172 .commit
= blockdev_backup_commit
,
2173 .abort
= blockdev_backup_abort
,
2174 .clean
= blockdev_backup_clean
,
2176 [TRANSACTION_ACTION_KIND_ABORT
] = {
2177 .instance_size
= sizeof(BlkActionState
),
2178 .prepare
= abort_prepare
,
2179 .commit
= abort_commit
,
2181 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2182 .instance_size
= sizeof(InternalSnapshotState
),
2183 .prepare
= internal_snapshot_prepare
,
2184 .abort
= internal_snapshot_abort
,
2185 .clean
= internal_snapshot_clean
,
2187 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2188 .instance_size
= sizeof(BlockDirtyBitmapState
),
2189 .prepare
= block_dirty_bitmap_add_prepare
,
2190 .abort
= block_dirty_bitmap_add_abort
,
2192 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2193 .instance_size
= sizeof(BlockDirtyBitmapState
),
2194 .prepare
= block_dirty_bitmap_clear_prepare
,
2195 .commit
= block_dirty_bitmap_clear_commit
,
2196 .abort
= block_dirty_bitmap_clear_abort
,
2198 [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_ENABLE
] = {
2199 .instance_size
= sizeof(BlockDirtyBitmapState
),
2200 .prepare
= block_dirty_bitmap_enable_prepare
,
2201 .abort
= block_dirty_bitmap_enable_abort
,
2203 [TRANSACTION_ACTION_KIND_X_BLOCK_DIRTY_BITMAP_DISABLE
] = {
2204 .instance_size
= sizeof(BlockDirtyBitmapState
),
2205 .prepare
= block_dirty_bitmap_disable_prepare
,
2206 .abort
= block_dirty_bitmap_disable_abort
,
2211 * Allocate a TransactionProperties structure if necessary, and fill
2212 * that structure with desired defaults if they are unset.
2214 static TransactionProperties
*get_transaction_properties(
2215 TransactionProperties
*props
)
2218 props
= g_new0(TransactionProperties
, 1);
2221 if (!props
->has_completion_mode
) {
2222 props
->has_completion_mode
= true;
2223 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2230 * 'Atomic' group operations. The operations are performed as a set, and if
2231 * any fail then we roll back all operations in the group.
2233 void qmp_transaction(TransactionActionList
*dev_list
,
2235 struct TransactionProperties
*props
,
2238 TransactionActionList
*dev_entry
= dev_list
;
2239 JobTxn
*block_job_txn
= NULL
;
2240 BlkActionState
*state
, *next
;
2241 Error
*local_err
= NULL
;
2243 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2244 QSIMPLEQ_INIT(&snap_bdrv_states
);
2246 /* Does this transaction get canceled as a group on failure?
2247 * If not, we don't really need to make a JobTxn.
2249 props
= get_transaction_properties(props
);
2250 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2251 block_job_txn
= job_txn_new();
2254 /* drain all i/o before any operations */
2257 /* We don't do anything in this loop that commits us to the operations */
2258 while (NULL
!= dev_entry
) {
2259 TransactionAction
*dev_info
= NULL
;
2260 const BlkActionOps
*ops
;
2262 dev_info
= dev_entry
->value
;
2263 dev_entry
= dev_entry
->next
;
2265 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2267 ops
= &actions
[dev_info
->type
];
2268 assert(ops
->instance_size
> 0);
2270 state
= g_malloc0(ops
->instance_size
);
2272 state
->action
= dev_info
;
2273 state
->block_job_txn
= block_job_txn
;
2274 state
->txn_props
= props
;
2275 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2277 state
->ops
->prepare(state
, &local_err
);
2279 error_propagate(errp
, local_err
);
2280 goto delete_and_fail
;
2284 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2285 if (state
->ops
->commit
) {
2286 state
->ops
->commit(state
);
2294 /* failure, and it is all-or-none; roll back all operations */
2295 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2296 if (state
->ops
->abort
) {
2297 state
->ops
->abort(state
);
2301 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2302 if (state
->ops
->clean
) {
2303 state
->ops
->clean(state
);
2308 qapi_free_TransactionProperties(props
);
2310 job_txn_unref(block_job_txn
);
2313 void qmp_eject(bool has_device
, const char *device
,
2314 bool has_id
, const char *id
,
2315 bool has_force
, bool force
, Error
**errp
)
2317 Error
*local_err
= NULL
;
2324 rc
= do_open_tray(has_device
? device
: NULL
,
2327 if (rc
&& rc
!= -ENOSYS
) {
2328 error_propagate(errp
, local_err
);
2331 error_free(local_err
);
2333 blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2336 void qmp_block_passwd(bool has_device
, const char *device
,
2337 bool has_node_name
, const char *node_name
,
2338 const char *password
, Error
**errp
)
2341 "Setting block passwords directly is no longer supported");
2345 * Attempt to open the tray of @device.
2346 * If @force, ignore its tray lock.
2347 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2348 * On error, store an error through @errp and return -errno.
2349 * If @device does not exist, return -ENODEV.
2350 * If it has no removable media, return -ENOTSUP.
2351 * If it has no tray, return -ENOSYS.
2352 * If the guest was asked to open the tray, return -EINPROGRESS.
2355 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2356 bool force
, Error
**errp
)
2359 const char *device
= qdev_id
?: blk_name
;
2362 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2367 if (!blk_dev_has_removable_media(blk
)) {
2368 error_setg(errp
, "Device '%s' is not removable", device
);
2372 if (!blk_dev_has_tray(blk
)) {
2373 error_setg(errp
, "Device '%s' does not have a tray", device
);
2377 if (blk_dev_is_tray_open(blk
)) {
2381 locked
= blk_dev_is_medium_locked(blk
);
2383 blk_dev_eject_request(blk
, force
);
2386 if (!locked
|| force
) {
2387 blk_dev_change_media_cb(blk
, false, &error_abort
);
2390 if (locked
&& !force
) {
2391 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2392 "wait for tray to open and try again", device
);
2393 return -EINPROGRESS
;
2399 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2400 bool has_id
, const char *id
,
2401 bool has_force
, bool force
,
2404 Error
*local_err
= NULL
;
2410 rc
= do_open_tray(has_device
? device
: NULL
,
2413 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2414 error_propagate(errp
, local_err
);
2417 error_free(local_err
);
2420 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2421 bool has_id
, const char *id
,
2425 Error
*local_err
= NULL
;
2427 device
= has_device
? device
: NULL
;
2428 id
= has_id
? id
: NULL
;
2430 blk
= qmp_get_blk(device
, id
, errp
);
2435 if (!blk_dev_has_removable_media(blk
)) {
2436 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2440 if (!blk_dev_has_tray(blk
)) {
2441 /* Ignore this command on tray-less devices */
2445 if (!blk_dev_is_tray_open(blk
)) {
2449 blk_dev_change_media_cb(blk
, true, &local_err
);
2451 error_propagate(errp
, local_err
);
2456 static void blockdev_remove_medium(bool has_device
, const char *device
,
2457 bool has_id
, const char *id
, Error
**errp
)
2460 BlockDriverState
*bs
;
2461 AioContext
*aio_context
;
2462 bool has_attached_device
;
2464 device
= has_device
? device
: NULL
;
2465 id
= has_id
? id
: NULL
;
2467 blk
= qmp_get_blk(device
, id
, errp
);
2472 /* For BBs without a device, we can exchange the BDS tree at will */
2473 has_attached_device
= blk_get_attached_dev(blk
);
2475 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2476 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2480 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2481 !blk_dev_is_tray_open(blk
))
2483 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2492 aio_context
= bdrv_get_aio_context(bs
);
2493 aio_context_acquire(aio_context
);
2495 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2501 if (!blk_dev_has_tray(blk
)) {
2502 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2503 * called at all); therefore, the medium needs to be ejected here.
2504 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2505 * value passed here (i.e. false). */
2506 blk_dev_change_media_cb(blk
, false, &error_abort
);
2510 aio_context_release(aio_context
);
2513 void qmp_blockdev_remove_medium(const char *id
, Error
**errp
)
2515 blockdev_remove_medium(false, NULL
, true, id
, errp
);
2518 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2519 BlockDriverState
*bs
, Error
**errp
)
2521 Error
*local_err
= NULL
;
2525 /* For BBs without a device, we can exchange the BDS tree at will */
2526 has_device
= blk_get_attached_dev(blk
);
2528 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2529 error_setg(errp
, "Device is not removable");
2533 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2534 error_setg(errp
, "Tray of the device is not open");
2539 error_setg(errp
, "There already is a medium in the device");
2543 ret
= blk_insert_bs(blk
, bs
, errp
);
2548 if (!blk_dev_has_tray(blk
)) {
2549 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2550 * called at all); therefore, the medium needs to be pushed into the
2552 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2553 * value passed here (i.e. true). */
2554 blk_dev_change_media_cb(blk
, true, &local_err
);
2556 error_propagate(errp
, local_err
);
2563 static void blockdev_insert_medium(bool has_device
, const char *device
,
2564 bool has_id
, const char *id
,
2565 const char *node_name
, Error
**errp
)
2568 BlockDriverState
*bs
;
2570 blk
= qmp_get_blk(has_device
? device
: NULL
,
2577 bs
= bdrv_find_node(node_name
);
2579 error_setg(errp
, "Node '%s' not found", node_name
);
2583 if (bdrv_has_blk(bs
)) {
2584 error_setg(errp
, "Node '%s' is already in use", node_name
);
2588 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2591 void qmp_blockdev_insert_medium(const char *id
, const char *node_name
,
2594 blockdev_insert_medium(false, NULL
, true, id
, node_name
, errp
);
2597 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2598 bool has_id
, const char *id
,
2599 const char *filename
,
2600 bool has_format
, const char *format
,
2602 BlockdevChangeReadOnlyMode read_only
,
2606 BlockDriverState
*medium_bs
= NULL
;
2610 QDict
*options
= NULL
;
2613 blk
= qmp_get_blk(has_device
? device
: NULL
,
2621 blk_update_root_state(blk
);
2624 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2625 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2628 if (!has_read_only
) {
2629 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2632 switch (read_only
) {
2633 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2636 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2637 bdrv_flags
&= ~BDRV_O_RDWR
;
2640 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2641 bdrv_flags
|= BDRV_O_RDWR
;
2648 options
= qdict_new();
2649 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2650 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2653 qdict_put_str(options
, "driver", format
);
2656 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2661 rc
= do_open_tray(has_device
? device
: NULL
,
2664 if (rc
&& rc
!= -ENOSYS
) {
2665 error_propagate(errp
, err
);
2671 blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2673 error_propagate(errp
, err
);
2677 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2679 error_propagate(errp
, err
);
2683 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2686 /* If the medium has been inserted, the device has its own reference, so
2687 * ours must be relinquished; and if it has not been inserted successfully,
2688 * the reference must be relinquished anyway */
2689 bdrv_unref(medium_bs
);
2692 /* throttling disk I/O limits */
2693 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2696 BlockDriverState
*bs
;
2698 AioContext
*aio_context
;
2700 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2701 arg
->has_id
? arg
->id
: NULL
,
2707 aio_context
= blk_get_aio_context(blk
);
2708 aio_context_acquire(aio_context
);
2712 error_setg(errp
, "Device has no medium");
2716 throttle_config_init(&cfg
);
2717 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2718 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2719 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2721 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2722 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2723 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2725 if (arg
->has_bps_max
) {
2726 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2728 if (arg
->has_bps_rd_max
) {
2729 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2731 if (arg
->has_bps_wr_max
) {
2732 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2734 if (arg
->has_iops_max
) {
2735 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2737 if (arg
->has_iops_rd_max
) {
2738 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2740 if (arg
->has_iops_wr_max
) {
2741 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2744 if (arg
->has_bps_max_length
) {
2745 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2747 if (arg
->has_bps_rd_max_length
) {
2748 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2750 if (arg
->has_bps_wr_max_length
) {
2751 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2753 if (arg
->has_iops_max_length
) {
2754 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2756 if (arg
->has_iops_rd_max_length
) {
2757 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2759 if (arg
->has_iops_wr_max_length
) {
2760 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2763 if (arg
->has_iops_size
) {
2764 cfg
.op_size
= arg
->iops_size
;
2767 if (!throttle_is_valid(&cfg
, errp
)) {
2771 if (throttle_enabled(&cfg
)) {
2772 /* Enable I/O limits if they're not enabled yet, otherwise
2773 * just update the throttling group. */
2774 if (!blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2775 blk_io_limits_enable(blk
,
2776 arg
->has_group
? arg
->group
:
2777 arg
->has_device
? arg
->device
:
2779 } else if (arg
->has_group
) {
2780 blk_io_limits_update_group(blk
, arg
->group
);
2782 /* Set the new throttling configuration */
2783 blk_set_io_limits(blk
, &cfg
);
2784 } else if (blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2785 /* If all throttling settings are set to 0, disable I/O limits */
2786 blk_io_limits_disable(blk
);
2790 aio_context_release(aio_context
);
2793 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2794 bool has_granularity
, uint32_t granularity
,
2795 bool has_persistent
, bool persistent
,
2796 bool has_autoload
, bool autoload
,
2797 bool has_disabled
, bool disabled
,
2800 BlockDriverState
*bs
;
2801 BdrvDirtyBitmap
*bitmap
;
2803 if (!name
|| name
[0] == '\0') {
2804 error_setg(errp
, "Bitmap name cannot be empty");
2808 bs
= bdrv_lookup_bs(node
, node
, errp
);
2813 if (has_granularity
) {
2814 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2815 error_setg(errp
, "Granularity must be power of 2 "
2816 "and at least 512");
2820 /* Default to cluster size, if available: */
2821 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2824 if (!has_persistent
) {
2829 warn_report("Autoload option is deprecated and its value is ignored");
2832 if (!has_disabled
) {
2837 !bdrv_can_store_new_dirty_bitmap(bs
, name
, granularity
, errp
))
2842 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2843 if (bitmap
== NULL
) {
2848 bdrv_disable_dirty_bitmap(bitmap
);
2851 bdrv_dirty_bitmap_set_persistance(bitmap
, persistent
);
2854 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2857 BlockDriverState
*bs
;
2858 BdrvDirtyBitmap
*bitmap
;
2859 Error
*local_err
= NULL
;
2861 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2862 if (!bitmap
|| !bs
) {
2866 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2868 "Bitmap '%s' is currently frozen and cannot be removed",
2871 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2873 "Bitmap '%s' is currently locked and cannot be removed",
2878 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
2879 bdrv_remove_persistent_dirty_bitmap(bs
, name
, &local_err
);
2880 if (local_err
!= NULL
) {
2881 error_propagate(errp
, local_err
);
2886 bdrv_release_dirty_bitmap(bs
, bitmap
);
2890 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2891 * immediately after a full backup operation.
2893 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2896 BdrvDirtyBitmap
*bitmap
;
2897 BlockDriverState
*bs
;
2899 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2900 if (!bitmap
|| !bs
) {
2904 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2906 "Bitmap '%s' is currently frozen and cannot be modified",
2909 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2911 "Bitmap '%s' is currently locked and cannot be modified",
2914 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2916 "Bitmap '%s' is currently disabled and cannot be cleared",
2919 } else if (bdrv_dirty_bitmap_readonly(bitmap
)) {
2920 error_setg(errp
, "Bitmap '%s' is readonly and cannot be cleared", name
);
2924 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2927 void qmp_x_block_dirty_bitmap_enable(const char *node
, const char *name
,
2930 BlockDriverState
*bs
;
2931 BdrvDirtyBitmap
*bitmap
;
2933 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2938 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2940 "Bitmap '%s' is currently frozen and cannot be enabled",
2945 bdrv_enable_dirty_bitmap(bitmap
);
2948 void qmp_x_block_dirty_bitmap_disable(const char *node
, const char *name
,
2951 BlockDriverState
*bs
;
2952 BdrvDirtyBitmap
*bitmap
;
2954 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2959 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2961 "Bitmap '%s' is currently frozen and cannot be disabled",
2966 bdrv_disable_dirty_bitmap(bitmap
);
2969 void qmp_x_block_dirty_bitmap_merge(const char *node
, const char *dst_name
,
2970 const char *src_name
, Error
**errp
)
2972 BlockDriverState
*bs
;
2973 BdrvDirtyBitmap
*dst
, *src
;
2975 dst
= block_dirty_bitmap_lookup(node
, dst_name
, &bs
, errp
);
2980 if (bdrv_dirty_bitmap_frozen(dst
)) {
2981 error_setg(errp
, "Bitmap '%s' is frozen and cannot be modified",
2984 } else if (bdrv_dirty_bitmap_readonly(dst
)) {
2985 error_setg(errp
, "Bitmap '%s' is readonly and cannot be modified",
2990 src
= bdrv_find_dirty_bitmap(bs
, src_name
);
2992 error_setg(errp
, "Dirty bitmap '%s' not found", src_name
);
2996 bdrv_merge_dirty_bitmap(dst
, src
, errp
);
2999 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
3003 BdrvDirtyBitmap
*bitmap
;
3004 BlockDriverState
*bs
;
3005 BlockDirtyBitmapSha256
*ret
= NULL
;
3008 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
3009 if (!bitmap
|| !bs
) {
3013 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
3014 if (sha256
== NULL
) {
3018 ret
= g_new(BlockDirtyBitmapSha256
, 1);
3019 ret
->sha256
= sha256
;
3024 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
3026 const char *id
= qdict_get_str(qdict
, "id");
3028 BlockDriverState
*bs
;
3029 AioContext
*aio_context
;
3030 Error
*local_err
= NULL
;
3032 bs
= bdrv_find_node(id
);
3034 qmp_blockdev_del(id
, &local_err
);
3036 error_report_err(local_err
);
3041 blk
= blk_by_name(id
);
3043 error_report("Device '%s' not found", id
);
3047 if (!blk_legacy_dinfo(blk
)) {
3048 error_report("Deleting device added with blockdev-add"
3049 " is not supported");
3053 aio_context
= blk_get_aio_context(blk
);
3054 aio_context_acquire(aio_context
);
3058 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
3059 error_report_err(local_err
);
3060 aio_context_release(aio_context
);
3067 /* Make the BlockBackend and the attached BlockDriverState anonymous */
3068 monitor_remove_blk(blk
);
3070 /* If this BlockBackend has a device attached to it, its refcount will be
3071 * decremented when the device is removed; otherwise we have to do so here.
3073 if (blk_get_attached_dev(blk
)) {
3074 /* Further I/O must not pause the guest */
3075 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
3076 BLOCKDEV_ON_ERROR_REPORT
);
3081 aio_context_release(aio_context
);
3084 void qmp_block_resize(bool has_device
, const char *device
,
3085 bool has_node_name
, const char *node_name
,
3086 int64_t size
, Error
**errp
)
3088 Error
*local_err
= NULL
;
3089 BlockBackend
*blk
= NULL
;
3090 BlockDriverState
*bs
;
3091 AioContext
*aio_context
;
3094 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
3095 has_node_name
? node_name
: NULL
,
3098 error_propagate(errp
, local_err
);
3102 aio_context
= bdrv_get_aio_context(bs
);
3103 aio_context_acquire(aio_context
);
3105 if (!bdrv_is_first_non_filter(bs
)) {
3106 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
3111 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
3115 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
3116 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
3120 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3121 ret
= blk_insert_bs(blk
, bs
, errp
);
3126 bdrv_drained_begin(bs
);
3127 ret
= blk_truncate(blk
, size
, PREALLOC_MODE_OFF
, errp
);
3128 bdrv_drained_end(bs
);
3132 aio_context_release(aio_context
);
3135 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3136 bool has_base
, const char *base
,
3137 bool has_base_node
, const char *base_node
,
3138 bool has_backing_file
, const char *backing_file
,
3139 bool has_speed
, int64_t speed
,
3140 bool has_on_error
, BlockdevOnError on_error
,
3143 BlockDriverState
*bs
, *iter
;
3144 BlockDriverState
*base_bs
= NULL
;
3145 AioContext
*aio_context
;
3146 Error
*local_err
= NULL
;
3147 const char *base_name
= NULL
;
3149 if (!has_on_error
) {
3150 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3153 bs
= bdrv_lookup_bs(device
, device
, errp
);
3158 aio_context
= bdrv_get_aio_context(bs
);
3159 aio_context_acquire(aio_context
);
3161 if (has_base
&& has_base_node
) {
3162 error_setg(errp
, "'base' and 'base-node' cannot be specified "
3163 "at the same time");
3168 base_bs
= bdrv_find_backing_image(bs
, base
);
3169 if (base_bs
== NULL
) {
3170 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3173 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3177 if (has_base_node
) {
3178 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
3182 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
3183 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
3187 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3188 base_name
= base_bs
->filename
;
3191 /* Check for op blockers in the whole chain between bs and base */
3192 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
3193 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3198 /* if we are streaming the entire chain, the result will have no backing
3199 * file, and specifying one is therefore an error */
3200 if (base_bs
== NULL
&& has_backing_file
) {
3201 error_setg(errp
, "backing file specified, but streaming the "
3206 /* backing_file string overrides base bs filename */
3207 base_name
= has_backing_file
? backing_file
: base_name
;
3209 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3210 has_speed
? speed
: 0, on_error
, &local_err
);
3212 error_propagate(errp
, local_err
);
3216 trace_qmp_block_stream(bs
, bs
->job
);
3219 aio_context_release(aio_context
);
3222 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3223 bool has_base
, const char *base
,
3224 bool has_top
, const char *top
,
3225 bool has_backing_file
, const char *backing_file
,
3226 bool has_speed
, int64_t speed
,
3227 bool has_filter_node_name
, const char *filter_node_name
,
3230 BlockDriverState
*bs
;
3231 BlockDriverState
*iter
;
3232 BlockDriverState
*base_bs
, *top_bs
;
3233 AioContext
*aio_context
;
3234 Error
*local_err
= NULL
;
3235 /* This will be part of the QMP command, if/when the
3236 * BlockdevOnError change for blkmirror makes it in
3238 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3243 if (!has_filter_node_name
) {
3244 filter_node_name
= NULL
;
3248 * libvirt relies on the DeviceNotFound error class in order to probe for
3249 * live commit feature versions; for this to work, we must make sure to
3250 * perform the device lookup before any generic errors that may occur in a
3251 * scenario in which all optional arguments are omitted. */
3252 bs
= qmp_get_root_bs(device
, &local_err
);
3254 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3256 error_free(local_err
);
3257 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3258 "Device '%s' not found", device
);
3260 error_propagate(errp
, local_err
);
3265 aio_context
= bdrv_get_aio_context(bs
);
3266 aio_context_acquire(aio_context
);
3268 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3272 /* default top_bs is the active layer */
3275 if (has_top
&& top
) {
3276 if (strcmp(bs
->filename
, top
) != 0) {
3277 top_bs
= bdrv_find_backing_image(bs
, top
);
3281 if (top_bs
== NULL
) {
3282 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3286 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3288 if (has_base
&& base
) {
3289 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3291 base_bs
= bdrv_find_base(top_bs
);
3294 if (base_bs
== NULL
) {
3295 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3299 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3301 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3302 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3307 /* Do not allow attempts to commit an image into itself */
3308 if (top_bs
== base_bs
) {
3309 error_setg(errp
, "cannot commit an image into itself");
3314 if (has_backing_file
) {
3315 error_setg(errp
, "'backing-file' specified,"
3316 " but 'top' is the active layer");
3319 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3320 JOB_DEFAULT
, speed
, on_error
,
3321 filter_node_name
, NULL
, NULL
, false, &local_err
);
3323 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3324 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3327 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3328 on_error
, has_backing_file
? backing_file
: NULL
,
3329 filter_node_name
, &local_err
);
3331 if (local_err
!= NULL
) {
3332 error_propagate(errp
, local_err
);
3337 aio_context_release(aio_context
);
3340 static BlockJob
*do_drive_backup(DriveBackup
*backup
, JobTxn
*txn
,
3343 BlockDriverState
*bs
;
3344 BlockDriverState
*target_bs
;
3345 BlockDriverState
*source
= NULL
;
3346 BlockJob
*job
= NULL
;
3347 BdrvDirtyBitmap
*bmap
= NULL
;
3348 AioContext
*aio_context
;
3349 QDict
*options
= NULL
;
3350 Error
*local_err
= NULL
;
3351 int flags
, job_flags
= JOB_DEFAULT
;
3353 bool set_backing_hd
= false;
3355 if (!backup
->has_speed
) {
3358 if (!backup
->has_on_source_error
) {
3359 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3361 if (!backup
->has_on_target_error
) {
3362 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3364 if (!backup
->has_mode
) {
3365 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3367 if (!backup
->has_job_id
) {
3368 backup
->job_id
= NULL
;
3370 if (!backup
->has_auto_finalize
) {
3371 backup
->auto_finalize
= true;
3373 if (!backup
->has_auto_dismiss
) {
3374 backup
->auto_dismiss
= true;
3376 if (!backup
->has_compress
) {
3377 backup
->compress
= false;
3380 bs
= qmp_get_root_bs(backup
->device
, errp
);
3385 aio_context
= bdrv_get_aio_context(bs
);
3386 aio_context_acquire(aio_context
);
3388 if (!backup
->has_format
) {
3389 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3390 NULL
: (char*) bs
->drv
->format_name
;
3393 /* Early check to avoid creating target */
3394 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3398 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3400 /* See if we have a backing HD we can use to create our new image
3402 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3403 source
= backing_bs(bs
);
3405 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3408 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3410 flags
|= BDRV_O_NO_BACKING
;
3411 set_backing_hd
= true;
3414 size
= bdrv_getlength(bs
);
3416 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3420 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3421 assert(backup
->format
);
3423 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3424 source
->drv
->format_name
, NULL
,
3425 size
, flags
, false, &local_err
);
3427 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3428 size
, flags
, false, &local_err
);
3433 error_propagate(errp
, local_err
);
3437 if (backup
->format
) {
3439 options
= qdict_new();
3441 qdict_put_str(options
, "driver", backup
->format
);
3444 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3449 bdrv_set_aio_context(target_bs
, aio_context
);
3451 if (set_backing_hd
) {
3452 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3454 bdrv_unref(target_bs
);
3459 if (backup
->has_bitmap
) {
3460 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3462 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3463 bdrv_unref(target_bs
);
3466 if (bdrv_dirty_bitmap_qmp_locked(bmap
)) {
3468 "Bitmap '%s' is currently locked and cannot be used for "
3469 "backup", backup
->bitmap
);
3473 if (!backup
->auto_finalize
) {
3474 job_flags
|= JOB_MANUAL_FINALIZE
;
3476 if (!backup
->auto_dismiss
) {
3477 job_flags
|= JOB_MANUAL_DISMISS
;
3480 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3481 backup
->sync
, bmap
, backup
->compress
,
3482 backup
->on_source_error
, backup
->on_target_error
,
3483 job_flags
, NULL
, NULL
, txn
, &local_err
);
3484 bdrv_unref(target_bs
);
3485 if (local_err
!= NULL
) {
3486 error_propagate(errp
, local_err
);
3491 aio_context_release(aio_context
);
3495 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3499 job
= do_drive_backup(arg
, NULL
, errp
);
3501 job_start(&job
->job
);
3505 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3507 return bdrv_named_nodes_list(errp
);
3510 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, JobTxn
*txn
,
3513 BlockDriverState
*bs
;
3514 BlockDriverState
*target_bs
;
3515 Error
*local_err
= NULL
;
3516 AioContext
*aio_context
;
3517 BlockJob
*job
= NULL
;
3518 int job_flags
= JOB_DEFAULT
;
3520 if (!backup
->has_speed
) {
3523 if (!backup
->has_on_source_error
) {
3524 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3526 if (!backup
->has_on_target_error
) {
3527 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3529 if (!backup
->has_job_id
) {
3530 backup
->job_id
= NULL
;
3532 if (!backup
->has_auto_finalize
) {
3533 backup
->auto_finalize
= true;
3535 if (!backup
->has_auto_dismiss
) {
3536 backup
->auto_dismiss
= true;
3538 if (!backup
->has_compress
) {
3539 backup
->compress
= false;
3542 bs
= qmp_get_root_bs(backup
->device
, errp
);
3547 aio_context
= bdrv_get_aio_context(bs
);
3548 aio_context_acquire(aio_context
);
3550 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3555 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3556 if (!bdrv_has_blk(target_bs
)) {
3557 /* The target BDS is not attached, we can safely move it to another
3559 bdrv_set_aio_context(target_bs
, aio_context
);
3561 error_setg(errp
, "Target is attached to a different thread from "
3566 if (!backup
->auto_finalize
) {
3567 job_flags
|= JOB_MANUAL_FINALIZE
;
3569 if (!backup
->auto_dismiss
) {
3570 job_flags
|= JOB_MANUAL_DISMISS
;
3572 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3573 backup
->sync
, NULL
, backup
->compress
,
3574 backup
->on_source_error
, backup
->on_target_error
,
3575 job_flags
, NULL
, NULL
, txn
, &local_err
);
3576 if (local_err
!= NULL
) {
3577 error_propagate(errp
, local_err
);
3580 aio_context_release(aio_context
);
3584 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3587 job
= do_blockdev_backup(arg
, NULL
, errp
);
3589 job_start(&job
->job
);
3593 /* Parameter check and block job starting for drive mirroring.
3594 * Caller should hold @device and @target's aio context (must be the same).
3596 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3597 BlockDriverState
*target
,
3598 bool has_replaces
, const char *replaces
,
3599 enum MirrorSyncMode sync
,
3600 BlockMirrorBackingMode backing_mode
,
3601 bool has_speed
, int64_t speed
,
3602 bool has_granularity
, uint32_t granularity
,
3603 bool has_buf_size
, int64_t buf_size
,
3604 bool has_on_source_error
,
3605 BlockdevOnError on_source_error
,
3606 bool has_on_target_error
,
3607 BlockdevOnError on_target_error
,
3608 bool has_unmap
, bool unmap
,
3609 bool has_filter_node_name
,
3610 const char *filter_node_name
,
3617 if (!has_on_source_error
) {
3618 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3620 if (!has_on_target_error
) {
3621 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3623 if (!has_granularity
) {
3626 if (!has_buf_size
) {
3632 if (!has_filter_node_name
) {
3633 filter_node_name
= NULL
;
3636 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3637 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3638 "a value in range [512B, 64MB]");
3641 if (granularity
& (granularity
- 1)) {
3642 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3647 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3650 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3654 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3655 sync
= MIRROR_SYNC_MODE_FULL
;
3658 /* pass the node name to replace to mirror start since it's loose coupling
3659 * and will allow to check whether the node still exist at mirror completion
3661 mirror_start(job_id
, bs
, target
,
3662 has_replaces
? replaces
: NULL
,
3663 speed
, granularity
, buf_size
, sync
, backing_mode
,
3664 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3668 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3670 BlockDriverState
*bs
;
3671 BlockDriverState
*source
, *target_bs
;
3672 AioContext
*aio_context
;
3673 BlockMirrorBackingMode backing_mode
;
3674 Error
*local_err
= NULL
;
3675 QDict
*options
= NULL
;
3678 const char *format
= arg
->format
;
3680 bs
= qmp_get_root_bs(arg
->device
, errp
);
3685 /* Early check to avoid creating target */
3686 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3690 aio_context
= bdrv_get_aio_context(bs
);
3691 aio_context_acquire(aio_context
);
3693 if (!arg
->has_mode
) {
3694 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3697 if (!arg
->has_format
) {
3698 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3699 ? NULL
: bs
->drv
->format_name
);
3702 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3703 source
= backing_bs(bs
);
3704 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3705 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3707 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3711 size
= bdrv_getlength(bs
);
3713 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3717 if (arg
->has_replaces
) {
3718 BlockDriverState
*to_replace_bs
;
3719 AioContext
*replace_aio_context
;
3720 int64_t replace_size
;
3722 if (!arg
->has_node_name
) {
3723 error_setg(errp
, "a node-name must be provided when replacing a"
3724 " named node of the graph");
3728 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3730 if (!to_replace_bs
) {
3731 error_propagate(errp
, local_err
);
3735 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3736 aio_context_acquire(replace_aio_context
);
3737 replace_size
= bdrv_getlength(to_replace_bs
);
3738 aio_context_release(replace_aio_context
);
3740 if (size
!= replace_size
) {
3741 error_setg(errp
, "cannot replace image with a mirror image of "
3747 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3748 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3750 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3753 /* Don't open backing image in create() */
3754 flags
|= BDRV_O_NO_BACKING
;
3756 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3757 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3759 /* create new image w/o backing file */
3761 bdrv_img_create(arg
->target
, format
,
3762 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3764 switch (arg
->mode
) {
3765 case NEW_IMAGE_MODE_EXISTING
:
3767 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3768 /* create new image with backing file */
3769 bdrv_img_create(arg
->target
, format
,
3771 source
->drv
->format_name
,
3772 NULL
, size
, flags
, false, &local_err
);
3780 error_propagate(errp
, local_err
);
3784 options
= qdict_new();
3785 if (arg
->has_node_name
) {
3786 qdict_put_str(options
, "node-name", arg
->node_name
);
3789 qdict_put_str(options
, "driver", format
);
3792 /* Mirroring takes care of copy-on-write using the source's backing
3795 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3800 bdrv_set_aio_context(target_bs
, aio_context
);
3802 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3803 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3804 backing_mode
, arg
->has_speed
, arg
->speed
,
3805 arg
->has_granularity
, arg
->granularity
,
3806 arg
->has_buf_size
, arg
->buf_size
,
3807 arg
->has_on_source_error
, arg
->on_source_error
,
3808 arg
->has_on_target_error
, arg
->on_target_error
,
3809 arg
->has_unmap
, arg
->unmap
,
3812 bdrv_unref(target_bs
);
3813 error_propagate(errp
, local_err
);
3815 aio_context_release(aio_context
);
3818 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3819 const char *device
, const char *target
,
3820 bool has_replaces
, const char *replaces
,
3821 MirrorSyncMode sync
,
3822 bool has_speed
, int64_t speed
,
3823 bool has_granularity
, uint32_t granularity
,
3824 bool has_buf_size
, int64_t buf_size
,
3825 bool has_on_source_error
,
3826 BlockdevOnError on_source_error
,
3827 bool has_on_target_error
,
3828 BlockdevOnError on_target_error
,
3829 bool has_filter_node_name
,
3830 const char *filter_node_name
,
3833 BlockDriverState
*bs
;
3834 BlockDriverState
*target_bs
;
3835 AioContext
*aio_context
;
3836 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3837 Error
*local_err
= NULL
;
3839 bs
= qmp_get_root_bs(device
, errp
);
3844 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3849 aio_context
= bdrv_get_aio_context(bs
);
3850 aio_context_acquire(aio_context
);
3852 bdrv_set_aio_context(target_bs
, aio_context
);
3854 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3855 has_replaces
, replaces
, sync
, backing_mode
,
3857 has_granularity
, granularity
,
3858 has_buf_size
, buf_size
,
3859 has_on_source_error
, on_source_error
,
3860 has_on_target_error
, on_target_error
,
3862 has_filter_node_name
, filter_node_name
,
3864 error_propagate(errp
, local_err
);
3866 aio_context_release(aio_context
);
3869 /* Get a block job using its ID and acquire its AioContext */
3870 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3877 *aio_context
= NULL
;
3879 job
= block_job_get(id
);
3882 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3883 "Block job '%s' not found", id
);
3887 *aio_context
= blk_get_aio_context(job
->blk
);
3888 aio_context_acquire(*aio_context
);
3893 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3895 AioContext
*aio_context
;
3896 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3902 block_job_set_speed(job
, speed
, errp
);
3903 aio_context_release(aio_context
);
3906 void qmp_block_job_cancel(const char *device
,
3907 bool has_force
, bool force
, Error
**errp
)
3909 AioContext
*aio_context
;
3910 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3920 if (job_user_paused(&job
->job
) && !force
) {
3921 error_setg(errp
, "The block job for device '%s' is currently paused",
3926 trace_qmp_block_job_cancel(job
);
3927 job_user_cancel(&job
->job
, force
, errp
);
3929 aio_context_release(aio_context
);
3932 void qmp_block_job_pause(const char *device
, Error
**errp
)
3934 AioContext
*aio_context
;
3935 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3941 trace_qmp_block_job_pause(job
);
3942 job_user_pause(&job
->job
, errp
);
3943 aio_context_release(aio_context
);
3946 void qmp_block_job_resume(const char *device
, Error
**errp
)
3948 AioContext
*aio_context
;
3949 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3955 trace_qmp_block_job_resume(job
);
3956 job_user_resume(&job
->job
, errp
);
3957 aio_context_release(aio_context
);
3960 void qmp_block_job_complete(const char *device
, Error
**errp
)
3962 AioContext
*aio_context
;
3963 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3969 trace_qmp_block_job_complete(job
);
3970 job_complete(&job
->job
, errp
);
3971 aio_context_release(aio_context
);
3974 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3976 AioContext
*aio_context
;
3977 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3983 trace_qmp_block_job_finalize(job
);
3984 job_finalize(&job
->job
, errp
);
3985 aio_context_release(aio_context
);
3988 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3990 AioContext
*aio_context
;
3991 BlockJob
*bjob
= find_block_job(id
, &aio_context
, errp
);
3998 trace_qmp_block_job_dismiss(bjob
);
4000 job_dismiss(&job
, errp
);
4001 aio_context_release(aio_context
);
4004 void qmp_change_backing_file(const char *device
,
4005 const char *image_node_name
,
4006 const char *backing_file
,
4009 BlockDriverState
*bs
= NULL
;
4010 AioContext
*aio_context
;
4011 BlockDriverState
*image_bs
= NULL
;
4012 Error
*local_err
= NULL
;
4017 bs
= qmp_get_root_bs(device
, errp
);
4022 aio_context
= bdrv_get_aio_context(bs
);
4023 aio_context_acquire(aio_context
);
4025 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
4027 error_propagate(errp
, local_err
);
4032 error_setg(errp
, "image file not found");
4036 if (bdrv_find_base(image_bs
) == image_bs
) {
4037 error_setg(errp
, "not allowing backing file change on an image "
4038 "without a backing file");
4042 /* even though we are not necessarily operating on bs, we need it to
4043 * determine if block ops are currently prohibited on the chain */
4044 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
4048 /* final sanity check */
4049 if (!bdrv_chain_contains(bs
, image_bs
)) {
4050 error_setg(errp
, "'%s' and image file are not in the same chain",
4055 /* if not r/w, reopen to make r/w */
4056 open_flags
= image_bs
->open_flags
;
4057 ro
= bdrv_is_read_only(image_bs
);
4060 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
4062 error_propagate(errp
, local_err
);
4067 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
4068 image_bs
->drv
? image_bs
->drv
->format_name
: "");
4071 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
4073 /* don't exit here, so we can try to restore open flags if
4078 bdrv_reopen(image_bs
, open_flags
, &local_err
);
4079 error_propagate(errp
, local_err
);
4083 aio_context_release(aio_context
);
4086 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
4090 Error
*local_err
= NULL
;
4092 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
4097 qdict
= qemu_opts_to_qdict(opts
, NULL
);
4099 if (!qdict_get_try_str(qdict
, "node-name")) {
4100 qobject_unref(qdict
);
4101 error_report("'node-name' needs to be specified");
4105 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
4107 error_report_err(local_err
);
4111 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4114 qemu_opts_del(opts
);
4117 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
4119 BlockDriverState
*bs
;
4121 Visitor
*v
= qobject_output_visitor_new(&obj
);
4123 Error
*local_err
= NULL
;
4125 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
4127 error_propagate(errp
, local_err
);
4131 visit_complete(v
, &obj
);
4132 qdict
= qobject_to(QDict
, obj
);
4134 qdict_flatten(qdict
);
4136 if (!qdict_get_try_str(qdict
, "node-name")) {
4137 error_setg(errp
, "'node-name' must be specified for the root node");
4141 bs
= bds_tree_init(qdict
, errp
);
4146 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4152 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
4154 AioContext
*aio_context
;
4155 BlockDriverState
*bs
;
4157 bs
= bdrv_find_node(node_name
);
4159 error_setg(errp
, "Cannot find node %s", node_name
);
4162 if (bdrv_has_blk(bs
)) {
4163 error_setg(errp
, "Node %s is in use", node_name
);
4166 aio_context
= bdrv_get_aio_context(bs
);
4167 aio_context_acquire(aio_context
);
4169 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4173 if (!bs
->monitor_list
.tqe_prev
) {
4174 error_setg(errp
, "Node %s is not owned by the monitor",
4179 if (bs
->refcnt
> 1) {
4180 error_setg(errp
, "Block device %s is in use",
4181 bdrv_get_device_or_node_name(bs
));
4185 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4189 aio_context_release(aio_context
);
4192 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4193 const char *child_name
)
4197 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4198 if (strcmp(child
->name
, child_name
) == 0) {
4206 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4207 const char *child
, bool has_node
,
4208 const char *node
, Error
**errp
)
4210 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4213 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4218 if (has_child
== has_node
) {
4220 error_setg(errp
, "The parameters child and node are in conflict");
4222 error_setg(errp
, "Either child or node must be specified");
4228 p_child
= bdrv_find_child(parent_bs
, child
);
4230 error_setg(errp
, "Node '%s' does not have child '%s'",
4234 bdrv_del_child(parent_bs
, p_child
, errp
);
4238 new_bs
= bdrv_find_node(node
);
4240 error_setg(errp
, "Node '%s' not found", node
);
4243 bdrv_add_child(parent_bs
, new_bs
, errp
);
4247 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4249 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4252 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4253 BlockJobInfoList
*elem
;
4254 AioContext
*aio_context
;
4256 if (block_job_is_internal(job
)) {
4259 elem
= g_new0(BlockJobInfoList
, 1);
4260 aio_context
= blk_get_aio_context(job
->blk
);
4261 aio_context_acquire(aio_context
);
4262 elem
->value
= block_job_query(job
, errp
);
4263 aio_context_release(aio_context
);
4266 qapi_free_BlockJobInfoList(head
);
4270 p_next
= &elem
->next
;
4276 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
4277 bool has_force
, bool force
, Error
**errp
)
4279 AioContext
*old_context
;
4280 AioContext
*new_context
;
4281 BlockDriverState
*bs
;
4283 bs
= bdrv_find_node(node_name
);
4285 error_setg(errp
, "Cannot find node %s", node_name
);
4289 /* Protects against accidents. */
4290 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
4291 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
4292 "be in use (use force=true to override this check)",
4297 if (iothread
->type
== QTYPE_QSTRING
) {
4298 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
4300 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
4304 new_context
= iothread_get_aio_context(obj
);
4306 new_context
= qemu_get_aio_context();
4309 old_context
= bdrv_get_aio_context(bs
);
4310 aio_context_acquire(old_context
);
4312 bdrv_set_aio_context(bs
, new_context
);
4314 aio_context_release(old_context
);
4317 void qmp_x_block_latency_histogram_set(
4319 bool has_boundaries
, uint64List
*boundaries
,
4320 bool has_boundaries_read
, uint64List
*boundaries_read
,
4321 bool has_boundaries_write
, uint64List
*boundaries_write
,
4322 bool has_boundaries_flush
, uint64List
*boundaries_flush
,
4325 BlockBackend
*blk
= blk_by_name(device
);
4326 BlockAcctStats
*stats
;
4329 error_setg(errp
, "Device '%s' not found", device
);
4332 stats
= blk_get_stats(blk
);
4334 if (!has_boundaries
&& !has_boundaries_read
&& !has_boundaries_write
&&
4335 !has_boundaries_flush
)
4337 block_latency_histograms_clear(stats
);
4341 if (has_boundaries
|| has_boundaries_read
) {
4342 block_latency_histogram_set(
4343 stats
, BLOCK_ACCT_READ
,
4344 has_boundaries_read
? boundaries_read
: boundaries
);
4347 if (has_boundaries
|| has_boundaries_write
) {
4348 block_latency_histogram_set(
4349 stats
, BLOCK_ACCT_WRITE
,
4350 has_boundaries_write
? boundaries_write
: boundaries
);
4353 if (has_boundaries
|| has_boundaries_flush
) {
4354 block_latency_histogram_set(
4355 stats
, BLOCK_ACCT_FLUSH
,
4356 has_boundaries_flush
? boundaries_flush
: boundaries
);
4360 QemuOptsList qemu_common_drive_opts
= {
4362 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4366 .type
= QEMU_OPT_BOOL
,
4367 .help
= "enable/disable snapshot mode",
4370 .type
= QEMU_OPT_STRING
,
4371 .help
= "host AIO implementation (threads, native)",
4373 .name
= BDRV_OPT_CACHE_WB
,
4374 .type
= QEMU_OPT_BOOL
,
4375 .help
= "Enable writeback mode",
4378 .type
= QEMU_OPT_STRING
,
4379 .help
= "disk format (raw, qcow2, ...)",
4382 .type
= QEMU_OPT_STRING
,
4383 .help
= "read error action",
4386 .type
= QEMU_OPT_STRING
,
4387 .help
= "write error action",
4389 .name
= BDRV_OPT_READ_ONLY
,
4390 .type
= QEMU_OPT_BOOL
,
4391 .help
= "open drive file as read-only",
4397 .name
= "throttling.group",
4398 .type
= QEMU_OPT_STRING
,
4399 .help
= "name of the block throttling group",
4401 .name
= "copy-on-read",
4402 .type
= QEMU_OPT_BOOL
,
4403 .help
= "copy read data from backing file into image file",
4405 .name
= "detect-zeroes",
4406 .type
= QEMU_OPT_STRING
,
4407 .help
= "try to optimize zero writes (off, on, unmap)",
4409 .name
= "stats-account-invalid",
4410 .type
= QEMU_OPT_BOOL
,
4411 .help
= "whether to account for invalid I/O operations "
4412 "in the statistics",
4414 .name
= "stats-account-failed",
4415 .type
= QEMU_OPT_BOOL
,
4416 .help
= "whether to account for failed I/O operations "
4417 "in the statistics",
4419 { /* end of list */ }
4423 QemuOptsList qemu_drive_opts
= {
4425 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4428 * no elements => accept any params
4429 * validation will happen later
4431 { /* end of list */ }