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/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qapi-commands-block.h"
44 #include "qapi/qapi-commands-transaction.h"
45 #include "qapi/qapi-visit-block-core.h"
46 #include "qapi/qmp/qdict.h"
47 #include "qapi/qmp/qnum.h"
48 #include "qapi/qmp/qstring.h"
49 #include "qapi/error.h"
50 #include "qapi/qmp/qerror.h"
51 #include "qapi/qmp/qlist.h"
52 #include "qapi/qobject-output-visitor.h"
53 #include "sysemu/sysemu.h"
54 #include "sysemu/iothread.h"
55 #include "block/block_int.h"
56 #include "block/trace.h"
57 #include "sysemu/arch_init.h"
58 #include "sysemu/qtest.h"
59 #include "qemu/cutils.h"
60 #include "qemu/help_option.h"
61 #include "qemu/throttle-options.h"
63 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
64 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
66 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
67 bool force
, Error
**errp
);
68 static void blockdev_remove_medium(bool has_device
, const char *device
,
69 bool has_id
, const char *id
, Error
**errp
);
70 static void blockdev_insert_medium(bool has_device
, const char *device
,
71 bool has_id
, const char *id
,
72 const char *node_name
, Error
**errp
);
74 static const char *const if_name
[IF_COUNT
] = {
78 [IF_FLOPPY
] = "floppy",
79 [IF_PFLASH
] = "pflash",
82 [IF_VIRTIO
] = "virtio",
86 static int if_max_devs
[IF_COUNT
] = {
88 * Do not change these numbers! They govern how drive option
89 * index maps to unit and bus. That mapping is ABI.
91 * All controllers used to implement if=T drives need to support
92 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
93 * Otherwise, some index values map to "impossible" bus, unit
96 * For instance, if you change [IF_SCSI] to 255, -drive
97 * if=scsi,index=12 no longer means bus=1,unit=5, but
98 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
99 * the drive can't be set up. Regression.
106 * Boards may call this to offer board-by-board overrides
107 * of the default, global values.
109 void override_max_devs(BlockInterfaceType type
, int max_devs
)
118 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
119 dinfo
= blk_legacy_dinfo(blk
);
120 if (dinfo
->type
== type
) {
121 fprintf(stderr
, "Cannot override units-per-bus property of"
122 " the %s interface, because a drive of that type has"
123 " already been added.\n", if_name
[type
]);
124 g_assert_not_reached();
128 if_max_devs
[type
] = max_devs
;
132 * We automatically delete the drive when a device using it gets
133 * unplugged. Questionable feature, but we can't just drop it.
134 * Device models call blockdev_mark_auto_del() to schedule the
135 * automatic deletion, and generic qdev code calls blockdev_auto_del()
136 * when deletion is actually safe.
138 void blockdev_mark_auto_del(BlockBackend
*blk
)
140 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
141 BlockDriverState
*bs
= blk_bs(blk
);
142 AioContext
*aio_context
;
149 aio_context
= bdrv_get_aio_context(bs
);
150 aio_context_acquire(aio_context
);
153 block_job_cancel(bs
->job
, false);
156 aio_context_release(aio_context
);
162 void blockdev_auto_del(BlockBackend
*blk
)
164 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
166 if (dinfo
&& dinfo
->auto_del
) {
167 monitor_remove_blk(blk
);
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
180 int drive_get_max_devs(BlockInterfaceType type
)
182 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
183 return if_max_devs
[type
];
189 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
191 int max_devs
= if_max_devs
[type
];
192 return max_devs
? index
/ max_devs
: 0;
195 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
197 int max_devs
= if_max_devs
[type
];
198 return max_devs
? index
% max_devs
: index
;
201 QemuOpts
*drive_def(const char *optstr
)
203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
206 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
211 opts
= drive_def(optstr
);
215 if (type
!= IF_DEFAULT
) {
216 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
219 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
222 qemu_opt_set(opts
, "file", file
, &error_abort
);
226 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
231 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
232 dinfo
= blk_legacy_dinfo(blk
);
233 if (dinfo
&& dinfo
->type
== type
234 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
242 void drive_check_orphaned(void)
247 bool orphans
= false;
249 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
250 dinfo
= blk_legacy_dinfo(blk
);
251 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
252 dinfo
->type
!= IF_NONE
) {
254 qemu_opts_loc_restore(dinfo
->opts
);
255 error_report("machine type does not support"
256 " if=%s,bus=%d,unit=%d",
257 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
268 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
270 return drive_get(type
,
271 drive_index_to_bus_id(type
, index
),
272 drive_index_to_unit_id(type
, index
));
275 int drive_get_max_bus(BlockInterfaceType type
)
282 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
283 dinfo
= blk_legacy_dinfo(blk
);
284 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
285 max_bus
= dinfo
->bus
;
291 /* Get a block device. This should only be used for single-drive devices
292 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
294 DriveInfo
*drive_get_next(BlockInterfaceType type
)
296 static int next_block_unit
[IF_COUNT
];
298 return drive_get(type
, 0, next_block_unit
[type
]++);
301 static void bdrv_format_print(void *opaque
, const char *name
)
303 error_printf(" %s", name
);
308 BlockDriverState
*bs
;
311 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
313 if (!strcmp(buf
, "ignore")) {
314 return BLOCKDEV_ON_ERROR_IGNORE
;
315 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
316 return BLOCKDEV_ON_ERROR_ENOSPC
;
317 } else if (!strcmp(buf
, "stop")) {
318 return BLOCKDEV_ON_ERROR_STOP
;
319 } else if (!strcmp(buf
, "report")) {
320 return BLOCKDEV_ON_ERROR_REPORT
;
322 error_setg(errp
, "'%s' invalid %s error action",
323 buf
, is_read
? "read" : "write");
328 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
331 const QListEntry
*entry
;
332 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
333 switch (qobject_type(entry
->value
)) {
335 case QTYPE_QSTRING
: {
336 unsigned long long length
;
337 const char *str
= qstring_get_str(qobject_to(QString
,
339 if (parse_uint_full(str
, &length
, 10) == 0 &&
340 length
> 0 && length
<= UINT_MAX
) {
341 block_acct_add_interval(stats
, (unsigned) length
);
343 error_setg(errp
, "Invalid interval length: %s", str
);
350 int64_t length
= qnum_get_int(qobject_to(QNum
, entry
->value
));
352 if (length
> 0 && length
<= UINT_MAX
) {
353 block_acct_add_interval(stats
, (unsigned) length
);
355 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
362 error_setg(errp
, "The specification of stats-intervals is invalid");
369 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
371 /* All parameters but @opts are optional and may be set to NULL. */
372 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
373 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
374 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
376 Error
*local_error
= NULL
;
380 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
381 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
384 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
385 if (!strcmp(aio
, "native")) {
386 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
387 } else if (!strcmp(aio
, "threads")) {
388 /* this is the default */
390 error_setg(errp
, "invalid aio option");
396 /* disk I/O throttling */
397 if (throttling_group
) {
398 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
402 throttle_config_init(throttle_cfg
);
403 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
404 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
405 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
406 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
407 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
408 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
409 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
410 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
411 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
412 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
413 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
414 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
416 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
417 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
419 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
420 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
421 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
422 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
423 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
424 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
425 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
426 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
427 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
429 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
430 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
431 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
432 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
433 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
434 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
435 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
436 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
437 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
438 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
439 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
440 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
442 throttle_cfg
->op_size
=
443 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
445 if (!throttle_is_valid(throttle_cfg
, errp
)) {
452 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
453 qemu_opt_get(opts
, "detect-zeroes"),
454 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
457 error_propagate(errp
, local_error
);
463 /* Takes the ownership of bs_opts */
464 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
469 int on_read_error
, on_write_error
;
470 bool account_invalid
, account_failed
;
471 bool writethrough
, read_only
;
473 BlockDriverState
*bs
;
478 QDict
*interval_dict
= NULL
;
479 QList
*interval_list
= NULL
;
481 BlockdevDetectZeroesOptions detect_zeroes
=
482 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
483 const char *throttling_group
= NULL
;
485 /* Check common options by copying from bs_opts to opts, all other options
486 * stay in bs_opts for processing by bdrv_open(). */
487 id
= qdict_get_try_str(bs_opts
, "id");
488 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
490 error_propagate(errp
, error
);
494 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
496 error_propagate(errp
, error
);
501 qdict_del(bs_opts
, "id");
504 /* extract parameters */
505 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
507 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
508 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
510 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
512 id
= qemu_opts_id(opts
);
514 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
515 qdict_array_split(interval_dict
, &interval_list
);
517 if (qdict_size(interval_dict
) != 0) {
518 error_setg(errp
, "Invalid option stats-intervals.%s",
519 qdict_first(interval_dict
)->key
);
523 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
524 &detect_zeroes
, &error
);
526 error_propagate(errp
, error
);
530 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
531 if (is_help_option(buf
)) {
532 error_printf("Supported formats:");
533 bdrv_iterate_format(bdrv_format_print
, NULL
);
538 if (qdict_haskey(bs_opts
, "driver")) {
539 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
542 qdict_put_str(bs_opts
, "driver", buf
);
545 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
546 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
547 on_write_error
= parse_block_error_action(buf
, 0, &error
);
549 error_propagate(errp
, error
);
554 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
555 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
556 on_read_error
= parse_block_error_action(buf
, 1, &error
);
558 error_propagate(errp
, error
);
564 bdrv_flags
|= BDRV_O_SNAPSHOT
;
567 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
570 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
571 BlockBackendRootState
*blk_rs
;
573 blk
= blk_new(0, BLK_PERM_ALL
);
574 blk_rs
= blk_get_root_state(blk
);
575 blk_rs
->open_flags
= bdrv_flags
;
576 blk_rs
->read_only
= read_only
;
577 blk_rs
->detect_zeroes
= detect_zeroes
;
579 qobject_unref(bs_opts
);
581 if (file
&& !*file
) {
585 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
586 * with other callers) rather than what we want as the real defaults.
587 * Apply the defaults here instead. */
588 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
589 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
590 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
591 read_only
? "on" : "off");
592 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
594 if (runstate_check(RUN_STATE_INMIGRATE
)) {
595 bdrv_flags
|= BDRV_O_INACTIVE
;
598 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
604 bs
->detect_zeroes
= detect_zeroes
;
606 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
608 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
615 /* disk I/O throttling */
616 if (throttle_enabled(&cfg
)) {
617 if (!throttling_group
) {
618 throttling_group
= id
;
620 blk_io_limits_enable(blk
, throttling_group
);
621 blk_set_io_limits(blk
, &cfg
);
624 blk_set_enable_write_cache(blk
, !writethrough
);
625 blk_set_on_error(blk
, on_read_error
, on_write_error
);
627 if (!monitor_add_blk(blk
, id
, errp
)) {
635 qobject_unref(interval_dict
);
636 qobject_unref(interval_list
);
641 qobject_unref(interval_dict
);
642 qobject_unref(interval_list
);
644 qobject_unref(bs_opts
);
648 /* Takes the ownership of bs_opts */
649 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
653 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
654 * with other callers) rather than what we want as the real defaults.
655 * Apply the defaults here instead. */
656 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
657 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
658 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
660 if (runstate_check(RUN_STATE_INMIGRATE
)) {
661 bdrv_flags
|= BDRV_O_INACTIVE
;
664 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
667 void blockdev_close_all_bdrv_states(void)
669 BlockDriverState
*bs
, *next_bs
;
671 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
672 AioContext
*ctx
= bdrv_get_aio_context(bs
);
674 aio_context_acquire(ctx
);
676 aio_context_release(ctx
);
680 /* Iterates over the list of monitor-owned BlockDriverStates */
681 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
683 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
684 : QTAILQ_FIRST(&monitor_bdrv_states
);
687 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
692 value
= qemu_opt_get(opts
, from
);
694 if (qemu_opt_find(opts
, to
)) {
695 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
696 "same time", to
, from
);
701 /* rename all items in opts */
702 while ((value
= qemu_opt_get(opts
, from
))) {
703 qemu_opt_set(opts
, to
, value
, &error_abort
);
704 qemu_opt_unset(opts
, from
);
708 QemuOptsList qemu_legacy_drive_opts
= {
710 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
714 .type
= QEMU_OPT_NUMBER
,
715 .help
= "bus number",
718 .type
= QEMU_OPT_NUMBER
,
719 .help
= "unit number (i.e. lun for scsi)",
722 .type
= QEMU_OPT_NUMBER
,
723 .help
= "index number",
726 .type
= QEMU_OPT_STRING
,
727 .help
= "media type (disk, cdrom)",
730 .type
= QEMU_OPT_STRING
,
731 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
734 .type
= QEMU_OPT_NUMBER
,
735 .help
= "number of cylinders (ide disk geometry)",
738 .type
= QEMU_OPT_NUMBER
,
739 .help
= "number of heads (ide disk geometry)",
742 .type
= QEMU_OPT_NUMBER
,
743 .help
= "number of sectors (ide disk geometry)",
746 .type
= QEMU_OPT_STRING
,
747 .help
= "chs translation (auto, lba, none)",
750 .type
= QEMU_OPT_STRING
,
751 .help
= "pci address (virtio only)",
754 .type
= QEMU_OPT_STRING
,
755 .help
= "disk serial number",
758 .type
= QEMU_OPT_STRING
,
762 /* Options that are passed on, but have special semantics with -drive */
764 .name
= BDRV_OPT_READ_ONLY
,
765 .type
= QEMU_OPT_BOOL
,
766 .help
= "open drive file as read-only",
769 .type
= QEMU_OPT_STRING
,
770 .help
= "read error action",
773 .type
= QEMU_OPT_STRING
,
774 .help
= "write error action",
776 .name
= "copy-on-read",
777 .type
= QEMU_OPT_BOOL
,
778 .help
= "copy read data from backing file into image file",
781 { /* end of list */ }
785 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
789 DriveInfo
*dinfo
= NULL
;
791 QemuOpts
*legacy_opts
;
792 DriveMediaType media
= MEDIA_DISK
;
793 BlockInterfaceType type
;
794 int cyls
, heads
, secs
, translation
;
795 int max_devs
, bus_id
, unit_id
, index
;
797 const char *werror
, *rerror
;
798 bool read_only
= false;
801 const char *filename
;
802 Error
*local_err
= NULL
;
804 const char *deprecated
[] = {
805 "serial", "trans", "secs", "heads", "cyls", "addr"
808 /* Change legacy command line options into QMP ones */
809 static const struct {
813 { "iops", "throttling.iops-total" },
814 { "iops_rd", "throttling.iops-read" },
815 { "iops_wr", "throttling.iops-write" },
817 { "bps", "throttling.bps-total" },
818 { "bps_rd", "throttling.bps-read" },
819 { "bps_wr", "throttling.bps-write" },
821 { "iops_max", "throttling.iops-total-max" },
822 { "iops_rd_max", "throttling.iops-read-max" },
823 { "iops_wr_max", "throttling.iops-write-max" },
825 { "bps_max", "throttling.bps-total-max" },
826 { "bps_rd_max", "throttling.bps-read-max" },
827 { "bps_wr_max", "throttling.bps-write-max" },
829 { "iops_size", "throttling.iops-size" },
831 { "group", "throttling.group" },
833 { "readonly", BDRV_OPT_READ_ONLY
},
836 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
837 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
840 error_report_err(local_err
);
845 value
= qemu_opt_get(all_opts
, "cache");
850 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
851 error_report("invalid cache option");
855 /* Specific options take precedence */
856 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
857 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
858 !writethrough
, &error_abort
);
860 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
861 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
862 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
864 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
865 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
866 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
868 qemu_opt_unset(all_opts
, "cache");
871 /* Get a QDict for processing the options */
872 bs_opts
= qdict_new();
873 qemu_opts_to_qdict(all_opts
, bs_opts
);
875 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
877 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
879 error_report_err(local_err
);
883 /* Other deprecated options */
884 if (!qtest_enabled()) {
885 for (i
= 0; i
< ARRAY_SIZE(deprecated
); i
++) {
886 if (qemu_opt_get(legacy_opts
, deprecated
[i
]) != NULL
) {
887 error_report("'%s' is deprecated, please use the corresponding "
888 "option of '-device' instead", deprecated
[i
]);
894 value
= qemu_opt_get(legacy_opts
, "media");
896 if (!strcmp(value
, "disk")) {
898 } else if (!strcmp(value
, "cdrom")) {
902 error_report("'%s' invalid media", value
);
907 /* copy-on-read is disabled with a warning for read-only devices */
908 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
909 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
911 if (read_only
&& copy_on_read
) {
912 warn_report("disabling copy-on-read on read-only drive");
913 copy_on_read
= false;
916 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
917 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
919 /* Controller type */
920 value
= qemu_opt_get(legacy_opts
, "if");
923 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
926 if (type
== IF_COUNT
) {
927 error_report("unsupported bus type '%s'", value
);
931 type
= block_default_type
;
935 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
936 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
937 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
939 if (cyls
|| heads
|| secs
) {
941 error_report("invalid physical cyls number");
945 error_report("invalid physical heads number");
949 error_report("invalid physical secs number");
954 translation
= BIOS_ATA_TRANSLATION_AUTO
;
955 value
= qemu_opt_get(legacy_opts
, "trans");
958 error_report("'%s' trans must be used with cyls, heads and secs",
962 if (!strcmp(value
, "none")) {
963 translation
= BIOS_ATA_TRANSLATION_NONE
;
964 } else if (!strcmp(value
, "lba")) {
965 translation
= BIOS_ATA_TRANSLATION_LBA
;
966 } else if (!strcmp(value
, "large")) {
967 translation
= BIOS_ATA_TRANSLATION_LARGE
;
968 } else if (!strcmp(value
, "rechs")) {
969 translation
= BIOS_ATA_TRANSLATION_RECHS
;
970 } else if (!strcmp(value
, "auto")) {
971 translation
= BIOS_ATA_TRANSLATION_AUTO
;
973 error_report("'%s' invalid translation type", value
);
978 if (media
== MEDIA_CDROM
) {
979 if (cyls
|| secs
|| heads
) {
980 error_report("CHS can't be set with media=cdrom");
985 /* Device address specified by bus/unit or index.
986 * If none was specified, try to find the first free one. */
987 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
988 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
989 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
991 max_devs
= if_max_devs
[type
];
994 if (bus_id
!= 0 || unit_id
!= -1) {
995 error_report("index cannot be used with bus and unit");
998 bus_id
= drive_index_to_bus_id(type
, index
);
999 unit_id
= drive_index_to_unit_id(type
, index
);
1002 if (unit_id
== -1) {
1004 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1006 if (max_devs
&& unit_id
>= max_devs
) {
1007 unit_id
-= max_devs
;
1013 if (max_devs
&& unit_id
>= max_devs
) {
1014 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1018 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1019 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1020 bus_id
, unit_id
, index
);
1025 serial
= qemu_opt_get(legacy_opts
, "serial");
1027 /* no id supplied -> create one */
1028 if (qemu_opts_id(all_opts
) == NULL
) {
1030 const char *mediastr
= "";
1031 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1032 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1035 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1038 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1041 qdict_put_str(bs_opts
, "id", new_id
);
1045 /* Add virtio block device */
1046 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1047 if (devaddr
&& type
!= IF_VIRTIO
) {
1048 error_report("addr is not supported by this bus type");
1052 if (type
== IF_VIRTIO
) {
1054 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1056 if (arch_type
== QEMU_ARCH_S390X
) {
1057 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1059 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1061 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1064 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1068 filename
= qemu_opt_get(legacy_opts
, "file");
1070 /* Check werror/rerror compatibility with if=... */
1071 werror
= qemu_opt_get(legacy_opts
, "werror");
1072 if (werror
!= NULL
) {
1073 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1075 error_report("werror is not supported by this bus type");
1078 qdict_put_str(bs_opts
, "werror", werror
);
1081 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1082 if (rerror
!= NULL
) {
1083 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1085 error_report("rerror is not supported by this bus type");
1088 qdict_put_str(bs_opts
, "rerror", rerror
);
1091 /* Actual block device init: Functionality shared with blockdev-add */
1092 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1096 error_report_err(local_err
);
1103 /* Create legacy DriveInfo */
1104 dinfo
= g_malloc0(sizeof(*dinfo
));
1105 dinfo
->opts
= all_opts
;
1108 dinfo
->heads
= heads
;
1110 dinfo
->trans
= translation
;
1113 dinfo
->bus
= bus_id
;
1114 dinfo
->unit
= unit_id
;
1115 dinfo
->devaddr
= devaddr
;
1116 dinfo
->serial
= g_strdup(serial
);
1118 blk_set_legacy_dinfo(blk
, dinfo
);
1125 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1132 qemu_opts_del(legacy_opts
);
1133 qobject_unref(bs_opts
);
1137 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1139 BlockDriverState
*bs
;
1141 bs
= bdrv_lookup_bs(name
, name
, errp
);
1146 if (!bdrv_is_root_node(bs
)) {
1147 error_setg(errp
, "Need a root block node");
1151 if (!bdrv_is_inserted(bs
)) {
1152 error_setg(errp
, "Device has no medium");
1159 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1164 if (!blk_name
== !qdev_id
) {
1165 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1170 blk
= blk_by_qdev_id(qdev_id
, errp
);
1172 blk
= blk_by_name(blk_name
);
1174 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1175 "Device '%s' not found", blk_name
);
1182 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1184 const char *device
= qdict_get_str(qdict
, "device");
1188 if (!strcmp(device
, "all")) {
1189 ret
= blk_commit_all();
1191 BlockDriverState
*bs
;
1192 AioContext
*aio_context
;
1194 blk
= blk_by_name(device
);
1196 monitor_printf(mon
, "Device '%s' not found\n", device
);
1199 if (!blk_is_available(blk
)) {
1200 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1205 aio_context
= bdrv_get_aio_context(bs
);
1206 aio_context_acquire(aio_context
);
1208 ret
= bdrv_commit(bs
);
1210 aio_context_release(aio_context
);
1213 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1218 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1220 TransactionActionList list
;
1222 list
.value
= action
;
1224 qmp_transaction(&list
, false, NULL
, errp
);
1227 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1228 bool has_node_name
, const char *node_name
,
1229 const char *snapshot_file
,
1230 bool has_snapshot_node_name
,
1231 const char *snapshot_node_name
,
1232 bool has_format
, const char *format
,
1233 bool has_mode
, NewImageMode mode
, Error
**errp
)
1235 BlockdevSnapshotSync snapshot
= {
1236 .has_device
= has_device
,
1237 .device
= (char *) device
,
1238 .has_node_name
= has_node_name
,
1239 .node_name
= (char *) node_name
,
1240 .snapshot_file
= (char *) snapshot_file
,
1241 .has_snapshot_node_name
= has_snapshot_node_name
,
1242 .snapshot_node_name
= (char *) snapshot_node_name
,
1243 .has_format
= has_format
,
1244 .format
= (char *) format
,
1245 .has_mode
= has_mode
,
1248 TransactionAction action
= {
1249 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1250 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1252 blockdev_do_action(&action
, errp
);
1255 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1258 BlockdevSnapshot snapshot_data
= {
1259 .node
= (char *) node
,
1260 .overlay
= (char *) overlay
1262 TransactionAction action
= {
1263 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1264 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1266 blockdev_do_action(&action
, errp
);
1269 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1273 BlockdevSnapshotInternal snapshot
= {
1274 .device
= (char *) device
,
1275 .name
= (char *) name
1277 TransactionAction action
= {
1278 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1279 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1281 blockdev_do_action(&action
, errp
);
1284 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1291 BlockDriverState
*bs
;
1292 AioContext
*aio_context
;
1293 QEMUSnapshotInfo sn
;
1294 Error
*local_err
= NULL
;
1295 SnapshotInfo
*info
= NULL
;
1298 bs
= qmp_get_root_bs(device
, errp
);
1302 aio_context
= bdrv_get_aio_context(bs
);
1303 aio_context_acquire(aio_context
);
1314 error_setg(errp
, "Name or id must be provided");
1315 goto out_aio_context
;
1318 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1319 goto out_aio_context
;
1322 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1324 error_propagate(errp
, local_err
);
1325 goto out_aio_context
;
1329 "Snapshot with id '%s' and name '%s' does not exist on "
1331 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1332 goto out_aio_context
;
1335 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1337 error_propagate(errp
, local_err
);
1338 goto out_aio_context
;
1341 aio_context_release(aio_context
);
1343 info
= g_new0(SnapshotInfo
, 1);
1344 info
->id
= g_strdup(sn
.id_str
);
1345 info
->name
= g_strdup(sn
.name
);
1346 info
->date_nsec
= sn
.date_nsec
;
1347 info
->date_sec
= sn
.date_sec
;
1348 info
->vm_state_size
= sn
.vm_state_size
;
1349 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1350 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1355 aio_context_release(aio_context
);
1360 * block_dirty_bitmap_lookup:
1361 * Return a dirty bitmap (if present), after validating
1362 * the node reference and bitmap names.
1364 * @node: The name of the BDS node to search for bitmaps
1365 * @name: The name of the bitmap to search for
1366 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1367 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1368 * @errp: Output pointer for error information. Can be NULL.
1370 * @return: A bitmap object on success, or NULL on failure.
1372 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1374 BlockDriverState
**pbs
,
1377 BlockDriverState
*bs
;
1378 BdrvDirtyBitmap
*bitmap
;
1381 error_setg(errp
, "Node cannot be NULL");
1385 error_setg(errp
, "Bitmap name cannot be NULL");
1388 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1390 error_setg(errp
, "Node '%s' not found", node
);
1394 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1396 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1407 /* New and old BlockDriverState structs for atomic group operations */
1409 typedef struct BlkActionState BlkActionState
;
1413 * Table of operations that define an Action.
1415 * @instance_size: Size of state struct, in bytes.
1416 * @prepare: Prepare the work, must NOT be NULL.
1417 * @commit: Commit the changes, can be NULL.
1418 * @abort: Abort the changes on fail, can be NULL.
1419 * @clean: Clean up resources after all transaction actions have called
1420 * commit() or abort(). Can be NULL.
1422 * Only prepare() may fail. In a single transaction, only one of commit() or
1423 * abort() will be called. clean() will always be called if it is present.
1425 typedef struct BlkActionOps
{
1426 size_t instance_size
;
1427 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1428 void (*commit
)(BlkActionState
*common
);
1429 void (*abort
)(BlkActionState
*common
);
1430 void (*clean
)(BlkActionState
*common
);
1435 * Describes one Action's state within a Transaction.
1437 * @action: QAPI-defined enum identifying which Action to perform.
1438 * @ops: Table of ActionOps this Action can perform.
1439 * @block_job_txn: Transaction which this action belongs to.
1440 * @entry: List membership for all Actions in this Transaction.
1442 * This structure must be arranged as first member in a subclassed type,
1443 * assuming that the compiler will also arrange it to the same offsets as the
1446 struct BlkActionState
{
1447 TransactionAction
*action
;
1448 const BlkActionOps
*ops
;
1449 BlockJobTxn
*block_job_txn
;
1450 TransactionProperties
*txn_props
;
1451 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1454 /* internal snapshot private data */
1455 typedef struct InternalSnapshotState
{
1456 BlkActionState common
;
1457 BlockDriverState
*bs
;
1458 QEMUSnapshotInfo sn
;
1460 } InternalSnapshotState
;
1463 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1465 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1467 "Action '%s' does not support Transaction property "
1468 "completion-mode = %s",
1469 TransactionActionKind_str(s
->action
->type
),
1470 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1476 static void internal_snapshot_prepare(BlkActionState
*common
,
1479 Error
*local_err
= NULL
;
1482 BlockDriverState
*bs
;
1483 QEMUSnapshotInfo old_sn
, *sn
;
1486 BlockdevSnapshotInternal
*internal
;
1487 InternalSnapshotState
*state
;
1488 AioContext
*aio_context
;
1491 g_assert(common
->action
->type
==
1492 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1493 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1494 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1496 /* 1. parse input */
1497 device
= internal
->device
;
1498 name
= internal
->name
;
1500 /* 2. check for validation */
1501 if (action_check_completion_mode(common
, errp
) < 0) {
1505 bs
= qmp_get_root_bs(device
, errp
);
1510 aio_context
= bdrv_get_aio_context(bs
);
1511 aio_context_acquire(aio_context
);
1515 /* Paired with .clean() */
1516 bdrv_drained_begin(bs
);
1518 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1522 if (bdrv_is_read_only(bs
)) {
1523 error_setg(errp
, "Device '%s' is read only", device
);
1527 if (!bdrv_can_snapshot(bs
)) {
1528 error_setg(errp
, "Block format '%s' used by device '%s' "
1529 "does not support internal snapshots",
1530 bs
->drv
->format_name
, device
);
1534 if (!strlen(name
)) {
1535 error_setg(errp
, "Name is empty");
1539 /* check whether a snapshot with name exist */
1540 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1543 error_propagate(errp
, local_err
);
1547 "Snapshot with name '%s' already exists on device '%s'",
1552 /* 3. take the snapshot */
1554 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1555 qemu_gettimeofday(&tv
);
1556 sn
->date_sec
= tv
.tv_sec
;
1557 sn
->date_nsec
= tv
.tv_usec
* 1000;
1558 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1560 ret1
= bdrv_snapshot_create(bs
, sn
);
1562 error_setg_errno(errp
, -ret1
,
1563 "Failed to create snapshot '%s' on device '%s'",
1568 /* 4. succeed, mark a snapshot is created */
1569 state
->created
= true;
1572 aio_context_release(aio_context
);
1575 static void internal_snapshot_abort(BlkActionState
*common
)
1577 InternalSnapshotState
*state
=
1578 DO_UPCAST(InternalSnapshotState
, common
, common
);
1579 BlockDriverState
*bs
= state
->bs
;
1580 QEMUSnapshotInfo
*sn
= &state
->sn
;
1581 AioContext
*aio_context
;
1582 Error
*local_error
= NULL
;
1584 if (!state
->created
) {
1588 aio_context
= bdrv_get_aio_context(state
->bs
);
1589 aio_context_acquire(aio_context
);
1591 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1592 error_reportf_err(local_error
,
1593 "Failed to delete snapshot with id '%s' and "
1594 "name '%s' on device '%s' in abort: ",
1595 sn
->id_str
, sn
->name
,
1596 bdrv_get_device_name(bs
));
1599 aio_context_release(aio_context
);
1602 static void internal_snapshot_clean(BlkActionState
*common
)
1604 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1606 AioContext
*aio_context
;
1612 aio_context
= bdrv_get_aio_context(state
->bs
);
1613 aio_context_acquire(aio_context
);
1615 bdrv_drained_end(state
->bs
);
1617 aio_context_release(aio_context
);
1620 /* external snapshot private data */
1621 typedef struct ExternalSnapshotState
{
1622 BlkActionState common
;
1623 BlockDriverState
*old_bs
;
1624 BlockDriverState
*new_bs
;
1625 bool overlay_appended
;
1626 } ExternalSnapshotState
;
1628 static void external_snapshot_prepare(BlkActionState
*common
,
1632 QDict
*options
= NULL
;
1633 Error
*local_err
= NULL
;
1634 /* Device and node name of the image to generate the snapshot from */
1636 const char *node_name
;
1637 /* Reference to the new image (for 'blockdev-snapshot') */
1638 const char *snapshot_ref
;
1639 /* File name of the new image (for 'blockdev-snapshot-sync') */
1640 const char *new_image_file
;
1641 ExternalSnapshotState
*state
=
1642 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1643 TransactionAction
*action
= common
->action
;
1644 AioContext
*aio_context
;
1646 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1647 * purpose but a different set of parameters */
1648 switch (action
->type
) {
1649 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1651 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1653 node_name
= s
->node
;
1654 new_image_file
= NULL
;
1655 snapshot_ref
= s
->overlay
;
1658 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1660 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1661 device
= s
->has_device
? s
->device
: NULL
;
1662 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1663 new_image_file
= s
->snapshot_file
;
1664 snapshot_ref
= NULL
;
1668 g_assert_not_reached();
1671 /* start processing */
1672 if (action_check_completion_mode(common
, errp
) < 0) {
1676 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1677 if (!state
->old_bs
) {
1681 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1682 aio_context_acquire(aio_context
);
1684 /* Paired with .clean() */
1685 bdrv_drained_begin(state
->old_bs
);
1687 if (!bdrv_is_inserted(state
->old_bs
)) {
1688 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1692 if (bdrv_op_is_blocked(state
->old_bs
,
1693 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1697 if (!bdrv_is_read_only(state
->old_bs
)) {
1698 if (bdrv_flush(state
->old_bs
)) {
1699 error_setg(errp
, QERR_IO_ERROR
);
1704 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1705 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1709 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1710 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1711 const char *format
= s
->has_format
? s
->format
: "qcow2";
1712 enum NewImageMode mode
;
1713 const char *snapshot_node_name
=
1714 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1716 if (node_name
&& !snapshot_node_name
) {
1717 error_setg(errp
, "New snapshot node name missing");
1721 if (snapshot_node_name
&&
1722 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1723 error_setg(errp
, "New snapshot node name already in use");
1727 flags
= state
->old_bs
->open_flags
;
1728 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1729 flags
|= BDRV_O_NO_BACKING
;
1731 /* create new image w/backing file */
1732 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1733 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1734 int64_t size
= bdrv_getlength(state
->old_bs
);
1736 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1739 bdrv_img_create(new_image_file
, format
,
1740 state
->old_bs
->filename
,
1741 state
->old_bs
->drv
->format_name
,
1742 NULL
, size
, flags
, false, &local_err
);
1744 error_propagate(errp
, local_err
);
1749 options
= qdict_new();
1750 if (s
->has_snapshot_node_name
) {
1751 qdict_put_str(options
, "node-name", snapshot_node_name
);
1753 qdict_put_str(options
, "driver", format
);
1756 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1758 /* We will manually add the backing_hd field to the bs later */
1759 if (!state
->new_bs
) {
1763 if (bdrv_has_blk(state
->new_bs
)) {
1764 error_setg(errp
, "The snapshot is already in use");
1768 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1773 if (state
->new_bs
->backing
!= NULL
) {
1774 error_setg(errp
, "The snapshot already has a backing image");
1778 if (!state
->new_bs
->drv
->supports_backing
) {
1779 error_setg(errp
, "The snapshot does not support backing images");
1783 bdrv_set_aio_context(state
->new_bs
, aio_context
);
1785 /* This removes our old bs and adds the new bs. This is an operation that
1786 * can fail, so we need to do it in .prepare; undoing it for abort is
1787 * always possible. */
1788 bdrv_ref(state
->new_bs
);
1789 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1791 error_propagate(errp
, local_err
);
1794 state
->overlay_appended
= true;
1797 aio_context_release(aio_context
);
1800 static void external_snapshot_commit(BlkActionState
*common
)
1802 ExternalSnapshotState
*state
=
1803 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1804 AioContext
*aio_context
;
1806 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1807 aio_context_acquire(aio_context
);
1809 /* We don't need (or want) to use the transactional
1810 * bdrv_reopen_multiple() across all the entries at once, because we
1811 * don't want to abort all of them if one of them fails the reopen */
1812 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1813 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1817 aio_context_release(aio_context
);
1820 static void external_snapshot_abort(BlkActionState
*common
)
1822 ExternalSnapshotState
*state
=
1823 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1824 if (state
->new_bs
) {
1825 if (state
->overlay_appended
) {
1826 AioContext
*aio_context
;
1828 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1829 aio_context_acquire(aio_context
);
1831 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1832 close state->old_bs; we need it */
1833 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1834 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1835 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1837 aio_context_release(aio_context
);
1842 static void external_snapshot_clean(BlkActionState
*common
)
1844 ExternalSnapshotState
*state
=
1845 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1846 AioContext
*aio_context
;
1848 if (!state
->old_bs
) {
1852 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1853 aio_context_acquire(aio_context
);
1855 bdrv_drained_end(state
->old_bs
);
1856 bdrv_unref(state
->new_bs
);
1858 aio_context_release(aio_context
);
1861 typedef struct DriveBackupState
{
1862 BlkActionState common
;
1863 BlockDriverState
*bs
;
1867 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
1870 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1872 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1873 BlockDriverState
*bs
;
1874 DriveBackup
*backup
;
1875 AioContext
*aio_context
;
1876 Error
*local_err
= NULL
;
1878 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1879 backup
= common
->action
->u
.drive_backup
.data
;
1881 bs
= qmp_get_root_bs(backup
->device
, errp
);
1886 aio_context
= bdrv_get_aio_context(bs
);
1887 aio_context_acquire(aio_context
);
1889 /* Paired with .clean() */
1890 bdrv_drained_begin(bs
);
1894 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1896 error_propagate(errp
, local_err
);
1901 aio_context_release(aio_context
);
1904 static void drive_backup_commit(BlkActionState
*common
)
1906 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1907 AioContext
*aio_context
;
1909 aio_context
= bdrv_get_aio_context(state
->bs
);
1910 aio_context_acquire(aio_context
);
1913 block_job_start(state
->job
);
1915 aio_context_release(aio_context
);
1918 static void drive_backup_abort(BlkActionState
*common
)
1920 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1923 AioContext
*aio_context
;
1925 aio_context
= bdrv_get_aio_context(state
->bs
);
1926 aio_context_acquire(aio_context
);
1928 block_job_cancel_sync(state
->job
);
1930 aio_context_release(aio_context
);
1934 static void drive_backup_clean(BlkActionState
*common
)
1936 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1937 AioContext
*aio_context
;
1943 aio_context
= bdrv_get_aio_context(state
->bs
);
1944 aio_context_acquire(aio_context
);
1946 bdrv_drained_end(state
->bs
);
1948 aio_context_release(aio_context
);
1951 typedef struct BlockdevBackupState
{
1952 BlkActionState common
;
1953 BlockDriverState
*bs
;
1955 } BlockdevBackupState
;
1957 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
1960 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1962 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1963 BlockdevBackup
*backup
;
1964 BlockDriverState
*bs
, *target
;
1965 AioContext
*aio_context
;
1966 Error
*local_err
= NULL
;
1968 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1969 backup
= common
->action
->u
.blockdev_backup
.data
;
1971 bs
= qmp_get_root_bs(backup
->device
, errp
);
1976 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1981 aio_context
= bdrv_get_aio_context(bs
);
1982 if (aio_context
!= bdrv_get_aio_context(target
)) {
1983 error_setg(errp
, "Backup between two IO threads is not implemented");
1986 aio_context_acquire(aio_context
);
1989 /* Paired with .clean() */
1990 bdrv_drained_begin(state
->bs
);
1992 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1994 error_propagate(errp
, local_err
);
1999 aio_context_release(aio_context
);
2002 static void blockdev_backup_commit(BlkActionState
*common
)
2004 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2005 AioContext
*aio_context
;
2007 aio_context
= bdrv_get_aio_context(state
->bs
);
2008 aio_context_acquire(aio_context
);
2011 block_job_start(state
->job
);
2013 aio_context_release(aio_context
);
2016 static void blockdev_backup_abort(BlkActionState
*common
)
2018 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2021 AioContext
*aio_context
;
2023 aio_context
= bdrv_get_aio_context(state
->bs
);
2024 aio_context_acquire(aio_context
);
2026 block_job_cancel_sync(state
->job
);
2028 aio_context_release(aio_context
);
2032 static void blockdev_backup_clean(BlkActionState
*common
)
2034 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2035 AioContext
*aio_context
;
2041 aio_context
= bdrv_get_aio_context(state
->bs
);
2042 aio_context_acquire(aio_context
);
2044 bdrv_drained_end(state
->bs
);
2046 aio_context_release(aio_context
);
2049 typedef struct BlockDirtyBitmapState
{
2050 BlkActionState common
;
2051 BdrvDirtyBitmap
*bitmap
;
2052 BlockDriverState
*bs
;
2055 } BlockDirtyBitmapState
;
2057 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2060 Error
*local_err
= NULL
;
2061 BlockDirtyBitmapAdd
*action
;
2062 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2065 if (action_check_completion_mode(common
, errp
) < 0) {
2069 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2070 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2071 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2072 action
->has_granularity
, action
->granularity
,
2073 action
->has_persistent
, action
->persistent
,
2074 action
->has_autoload
, action
->autoload
,
2078 state
->prepared
= true;
2080 error_propagate(errp
, local_err
);
2084 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2086 BlockDirtyBitmapAdd
*action
;
2087 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2090 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2091 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2092 * then the node reference and bitmap name must have been valid.
2094 if (state
->prepared
) {
2095 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2099 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2102 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2104 BlockDirtyBitmap
*action
;
2106 if (action_check_completion_mode(common
, errp
) < 0) {
2110 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2111 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2115 if (!state
->bitmap
) {
2119 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2120 error_setg(errp
, "Cannot modify a frozen bitmap");
2122 } else if (bdrv_dirty_bitmap_qmp_locked(state
->bitmap
)) {
2123 error_setg(errp
, "Cannot modify a locked bitmap");
2125 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2126 error_setg(errp
, "Cannot clear a disabled bitmap");
2128 } else if (bdrv_dirty_bitmap_readonly(state
->bitmap
)) {
2129 error_setg(errp
, "Cannot clear a readonly bitmap");
2133 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2136 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2138 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2141 if (state
->backup
) {
2142 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2146 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2148 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2151 hbitmap_free(state
->backup
);
2154 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2156 error_setg(errp
, "Transaction aborted using Abort action");
2159 static void abort_commit(BlkActionState
*common
)
2161 g_assert_not_reached(); /* this action never succeeds */
2164 static const BlkActionOps actions
[] = {
2165 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2166 .instance_size
= sizeof(ExternalSnapshotState
),
2167 .prepare
= external_snapshot_prepare
,
2168 .commit
= external_snapshot_commit
,
2169 .abort
= external_snapshot_abort
,
2170 .clean
= external_snapshot_clean
,
2172 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2173 .instance_size
= sizeof(ExternalSnapshotState
),
2174 .prepare
= external_snapshot_prepare
,
2175 .commit
= external_snapshot_commit
,
2176 .abort
= external_snapshot_abort
,
2177 .clean
= external_snapshot_clean
,
2179 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2180 .instance_size
= sizeof(DriveBackupState
),
2181 .prepare
= drive_backup_prepare
,
2182 .commit
= drive_backup_commit
,
2183 .abort
= drive_backup_abort
,
2184 .clean
= drive_backup_clean
,
2186 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2187 .instance_size
= sizeof(BlockdevBackupState
),
2188 .prepare
= blockdev_backup_prepare
,
2189 .commit
= blockdev_backup_commit
,
2190 .abort
= blockdev_backup_abort
,
2191 .clean
= blockdev_backup_clean
,
2193 [TRANSACTION_ACTION_KIND_ABORT
] = {
2194 .instance_size
= sizeof(BlkActionState
),
2195 .prepare
= abort_prepare
,
2196 .commit
= abort_commit
,
2198 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2199 .instance_size
= sizeof(InternalSnapshotState
),
2200 .prepare
= internal_snapshot_prepare
,
2201 .abort
= internal_snapshot_abort
,
2202 .clean
= internal_snapshot_clean
,
2204 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2205 .instance_size
= sizeof(BlockDirtyBitmapState
),
2206 .prepare
= block_dirty_bitmap_add_prepare
,
2207 .abort
= block_dirty_bitmap_add_abort
,
2209 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2210 .instance_size
= sizeof(BlockDirtyBitmapState
),
2211 .prepare
= block_dirty_bitmap_clear_prepare
,
2212 .commit
= block_dirty_bitmap_clear_commit
,
2213 .abort
= block_dirty_bitmap_clear_abort
,
2218 * Allocate a TransactionProperties structure if necessary, and fill
2219 * that structure with desired defaults if they are unset.
2221 static TransactionProperties
*get_transaction_properties(
2222 TransactionProperties
*props
)
2225 props
= g_new0(TransactionProperties
, 1);
2228 if (!props
->has_completion_mode
) {
2229 props
->has_completion_mode
= true;
2230 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2237 * 'Atomic' group operations. The operations are performed as a set, and if
2238 * any fail then we roll back all operations in the group.
2240 void qmp_transaction(TransactionActionList
*dev_list
,
2242 struct TransactionProperties
*props
,
2245 TransactionActionList
*dev_entry
= dev_list
;
2246 BlockJobTxn
*block_job_txn
= NULL
;
2247 BlkActionState
*state
, *next
;
2248 Error
*local_err
= NULL
;
2250 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2251 QSIMPLEQ_INIT(&snap_bdrv_states
);
2253 /* Does this transaction get canceled as a group on failure?
2254 * If not, we don't really need to make a BlockJobTxn.
2256 props
= get_transaction_properties(props
);
2257 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2258 block_job_txn
= block_job_txn_new();
2261 /* drain all i/o before any operations */
2264 /* We don't do anything in this loop that commits us to the operations */
2265 while (NULL
!= dev_entry
) {
2266 TransactionAction
*dev_info
= NULL
;
2267 const BlkActionOps
*ops
;
2269 dev_info
= dev_entry
->value
;
2270 dev_entry
= dev_entry
->next
;
2272 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2274 ops
= &actions
[dev_info
->type
];
2275 assert(ops
->instance_size
> 0);
2277 state
= g_malloc0(ops
->instance_size
);
2279 state
->action
= dev_info
;
2280 state
->block_job_txn
= block_job_txn
;
2281 state
->txn_props
= props
;
2282 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2284 state
->ops
->prepare(state
, &local_err
);
2286 error_propagate(errp
, local_err
);
2287 goto delete_and_fail
;
2291 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2292 if (state
->ops
->commit
) {
2293 state
->ops
->commit(state
);
2301 /* failure, and it is all-or-none; roll back all operations */
2302 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2303 if (state
->ops
->abort
) {
2304 state
->ops
->abort(state
);
2308 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2309 if (state
->ops
->clean
) {
2310 state
->ops
->clean(state
);
2315 qapi_free_TransactionProperties(props
);
2317 block_job_txn_unref(block_job_txn
);
2320 void qmp_eject(bool has_device
, const char *device
,
2321 bool has_id
, const char *id
,
2322 bool has_force
, bool force
, Error
**errp
)
2324 Error
*local_err
= NULL
;
2331 rc
= do_open_tray(has_device
? device
: NULL
,
2334 if (rc
&& rc
!= -ENOSYS
) {
2335 error_propagate(errp
, local_err
);
2338 error_free(local_err
);
2340 blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2343 void qmp_block_passwd(bool has_device
, const char *device
,
2344 bool has_node_name
, const char *node_name
,
2345 const char *password
, Error
**errp
)
2348 "Setting block passwords directly is no longer supported");
2352 * Attempt to open the tray of @device.
2353 * If @force, ignore its tray lock.
2354 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2355 * On error, store an error through @errp and return -errno.
2356 * If @device does not exist, return -ENODEV.
2357 * If it has no removable media, return -ENOTSUP.
2358 * If it has no tray, return -ENOSYS.
2359 * If the guest was asked to open the tray, return -EINPROGRESS.
2362 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2363 bool force
, Error
**errp
)
2366 const char *device
= qdev_id
?: blk_name
;
2369 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2374 if (!blk_dev_has_removable_media(blk
)) {
2375 error_setg(errp
, "Device '%s' is not removable", device
);
2379 if (!blk_dev_has_tray(blk
)) {
2380 error_setg(errp
, "Device '%s' does not have a tray", device
);
2384 if (blk_dev_is_tray_open(blk
)) {
2388 locked
= blk_dev_is_medium_locked(blk
);
2390 blk_dev_eject_request(blk
, force
);
2393 if (!locked
|| force
) {
2394 blk_dev_change_media_cb(blk
, false, &error_abort
);
2397 if (locked
&& !force
) {
2398 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2399 "wait for tray to open and try again", device
);
2400 return -EINPROGRESS
;
2406 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2407 bool has_id
, const char *id
,
2408 bool has_force
, bool force
,
2411 Error
*local_err
= NULL
;
2417 rc
= do_open_tray(has_device
? device
: NULL
,
2420 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2421 error_propagate(errp
, local_err
);
2424 error_free(local_err
);
2427 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2428 bool has_id
, const char *id
,
2432 Error
*local_err
= NULL
;
2434 device
= has_device
? device
: NULL
;
2435 id
= has_id
? id
: NULL
;
2437 blk
= qmp_get_blk(device
, id
, errp
);
2442 if (!blk_dev_has_removable_media(blk
)) {
2443 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2447 if (!blk_dev_has_tray(blk
)) {
2448 /* Ignore this command on tray-less devices */
2452 if (!blk_dev_is_tray_open(blk
)) {
2456 blk_dev_change_media_cb(blk
, true, &local_err
);
2458 error_propagate(errp
, local_err
);
2463 static void blockdev_remove_medium(bool has_device
, const char *device
,
2464 bool has_id
, const char *id
, Error
**errp
)
2467 BlockDriverState
*bs
;
2468 AioContext
*aio_context
;
2469 bool has_attached_device
;
2471 device
= has_device
? device
: NULL
;
2472 id
= has_id
? id
: NULL
;
2474 blk
= qmp_get_blk(device
, id
, errp
);
2479 /* For BBs without a device, we can exchange the BDS tree at will */
2480 has_attached_device
= blk_get_attached_dev(blk
);
2482 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2483 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2487 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2488 !blk_dev_is_tray_open(blk
))
2490 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2499 aio_context
= bdrv_get_aio_context(bs
);
2500 aio_context_acquire(aio_context
);
2502 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2508 if (!blk_dev_has_tray(blk
)) {
2509 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2510 * called at all); therefore, the medium needs to be ejected here.
2511 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2512 * value passed here (i.e. false). */
2513 blk_dev_change_media_cb(blk
, false, &error_abort
);
2517 aio_context_release(aio_context
);
2520 void qmp_blockdev_remove_medium(const char *id
, Error
**errp
)
2522 blockdev_remove_medium(false, NULL
, true, id
, errp
);
2525 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2526 BlockDriverState
*bs
, Error
**errp
)
2528 Error
*local_err
= NULL
;
2532 /* For BBs without a device, we can exchange the BDS tree at will */
2533 has_device
= blk_get_attached_dev(blk
);
2535 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2536 error_setg(errp
, "Device is not removable");
2540 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2541 error_setg(errp
, "Tray of the device is not open");
2546 error_setg(errp
, "There already is a medium in the device");
2550 ret
= blk_insert_bs(blk
, bs
, errp
);
2555 if (!blk_dev_has_tray(blk
)) {
2556 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2557 * called at all); therefore, the medium needs to be pushed into the
2559 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2560 * value passed here (i.e. true). */
2561 blk_dev_change_media_cb(blk
, true, &local_err
);
2563 error_propagate(errp
, local_err
);
2570 static void blockdev_insert_medium(bool has_device
, const char *device
,
2571 bool has_id
, const char *id
,
2572 const char *node_name
, Error
**errp
)
2575 BlockDriverState
*bs
;
2577 blk
= qmp_get_blk(has_device
? device
: NULL
,
2584 bs
= bdrv_find_node(node_name
);
2586 error_setg(errp
, "Node '%s' not found", node_name
);
2590 if (bdrv_has_blk(bs
)) {
2591 error_setg(errp
, "Node '%s' is already in use", node_name
);
2595 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2598 void qmp_blockdev_insert_medium(const char *id
, const char *node_name
,
2601 blockdev_insert_medium(false, NULL
, true, id
, node_name
, errp
);
2604 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2605 bool has_id
, const char *id
,
2606 const char *filename
,
2607 bool has_format
, const char *format
,
2609 BlockdevChangeReadOnlyMode read_only
,
2613 BlockDriverState
*medium_bs
= NULL
;
2617 QDict
*options
= NULL
;
2620 blk
= qmp_get_blk(has_device
? device
: NULL
,
2628 blk_update_root_state(blk
);
2631 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2632 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2635 if (!has_read_only
) {
2636 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2639 switch (read_only
) {
2640 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2643 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2644 bdrv_flags
&= ~BDRV_O_RDWR
;
2647 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2648 bdrv_flags
|= BDRV_O_RDWR
;
2655 options
= qdict_new();
2656 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2657 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2660 qdict_put_str(options
, "driver", format
);
2663 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2668 rc
= do_open_tray(has_device
? device
: NULL
,
2671 if (rc
&& rc
!= -ENOSYS
) {
2672 error_propagate(errp
, err
);
2678 blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2680 error_propagate(errp
, err
);
2684 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2686 error_propagate(errp
, err
);
2690 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2693 /* If the medium has been inserted, the device has its own reference, so
2694 * ours must be relinquished; and if it has not been inserted successfully,
2695 * the reference must be relinquished anyway */
2696 bdrv_unref(medium_bs
);
2699 /* throttling disk I/O limits */
2700 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2703 BlockDriverState
*bs
;
2705 AioContext
*aio_context
;
2707 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2708 arg
->has_id
? arg
->id
: NULL
,
2714 aio_context
= blk_get_aio_context(blk
);
2715 aio_context_acquire(aio_context
);
2719 error_setg(errp
, "Device has no medium");
2723 throttle_config_init(&cfg
);
2724 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2725 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2726 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2728 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2729 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2730 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2732 if (arg
->has_bps_max
) {
2733 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2735 if (arg
->has_bps_rd_max
) {
2736 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2738 if (arg
->has_bps_wr_max
) {
2739 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2741 if (arg
->has_iops_max
) {
2742 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2744 if (arg
->has_iops_rd_max
) {
2745 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2747 if (arg
->has_iops_wr_max
) {
2748 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2751 if (arg
->has_bps_max_length
) {
2752 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2754 if (arg
->has_bps_rd_max_length
) {
2755 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2757 if (arg
->has_bps_wr_max_length
) {
2758 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2760 if (arg
->has_iops_max_length
) {
2761 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2763 if (arg
->has_iops_rd_max_length
) {
2764 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2766 if (arg
->has_iops_wr_max_length
) {
2767 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2770 if (arg
->has_iops_size
) {
2771 cfg
.op_size
= arg
->iops_size
;
2774 if (!throttle_is_valid(&cfg
, errp
)) {
2778 if (throttle_enabled(&cfg
)) {
2779 /* Enable I/O limits if they're not enabled yet, otherwise
2780 * just update the throttling group. */
2781 if (!blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2782 blk_io_limits_enable(blk
,
2783 arg
->has_group
? arg
->group
:
2784 arg
->has_device
? arg
->device
:
2786 } else if (arg
->has_group
) {
2787 blk_io_limits_update_group(blk
, arg
->group
);
2789 /* Set the new throttling configuration */
2790 blk_set_io_limits(blk
, &cfg
);
2791 } else if (blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2792 /* If all throttling settings are set to 0, disable I/O limits */
2793 blk_io_limits_disable(blk
);
2797 aio_context_release(aio_context
);
2800 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2801 bool has_granularity
, uint32_t granularity
,
2802 bool has_persistent
, bool persistent
,
2803 bool has_autoload
, bool autoload
,
2806 BlockDriverState
*bs
;
2807 BdrvDirtyBitmap
*bitmap
;
2809 if (!name
|| name
[0] == '\0') {
2810 error_setg(errp
, "Bitmap name cannot be empty");
2814 bs
= bdrv_lookup_bs(node
, node
, errp
);
2819 if (has_granularity
) {
2820 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2821 error_setg(errp
, "Granularity must be power of 2 "
2822 "and at least 512");
2826 /* Default to cluster size, if available: */
2827 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2830 if (!has_persistent
) {
2835 warn_report("Autoload option is deprecated and its value is ignored");
2839 !bdrv_can_store_new_dirty_bitmap(bs
, name
, granularity
, errp
))
2844 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2845 if (bitmap
== NULL
) {
2849 bdrv_dirty_bitmap_set_persistance(bitmap
, persistent
);
2852 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2855 BlockDriverState
*bs
;
2856 BdrvDirtyBitmap
*bitmap
;
2857 Error
*local_err
= NULL
;
2859 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2860 if (!bitmap
|| !bs
) {
2864 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2866 "Bitmap '%s' is currently frozen and cannot be removed",
2869 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2871 "Bitmap '%s' is currently locked and cannot be removed",
2876 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
2877 bdrv_remove_persistent_dirty_bitmap(bs
, name
, &local_err
);
2878 if (local_err
!= NULL
) {
2879 error_propagate(errp
, local_err
);
2884 bdrv_dirty_bitmap_make_anon(bitmap
);
2885 bdrv_release_dirty_bitmap(bs
, bitmap
);
2889 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2890 * immediately after a full backup operation.
2892 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2895 BdrvDirtyBitmap
*bitmap
;
2896 BlockDriverState
*bs
;
2898 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2899 if (!bitmap
|| !bs
) {
2903 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2905 "Bitmap '%s' is currently frozen and cannot be modified",
2908 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2910 "Bitmap '%s' is currently locked and cannot be modified",
2913 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2915 "Bitmap '%s' is currently disabled and cannot be cleared",
2918 } else if (bdrv_dirty_bitmap_readonly(bitmap
)) {
2919 error_setg(errp
, "Bitmap '%s' is readonly and cannot be cleared", name
);
2923 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2926 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2930 BdrvDirtyBitmap
*bitmap
;
2931 BlockDriverState
*bs
;
2932 BlockDirtyBitmapSha256
*ret
= NULL
;
2935 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2936 if (!bitmap
|| !bs
) {
2940 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2941 if (sha256
== NULL
) {
2945 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2946 ret
->sha256
= sha256
;
2951 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2953 const char *id
= qdict_get_str(qdict
, "id");
2955 BlockDriverState
*bs
;
2956 AioContext
*aio_context
;
2957 Error
*local_err
= NULL
;
2959 bs
= bdrv_find_node(id
);
2961 qmp_blockdev_del(id
, &local_err
);
2963 error_report_err(local_err
);
2968 blk
= blk_by_name(id
);
2970 error_report("Device '%s' not found", id
);
2974 if (!blk_legacy_dinfo(blk
)) {
2975 error_report("Deleting device added with blockdev-add"
2976 " is not supported");
2980 aio_context
= blk_get_aio_context(blk
);
2981 aio_context_acquire(aio_context
);
2985 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2986 error_report_err(local_err
);
2987 aio_context_release(aio_context
);
2994 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2995 monitor_remove_blk(blk
);
2997 /* If this BlockBackend has a device attached to it, its refcount will be
2998 * decremented when the device is removed; otherwise we have to do so here.
3000 if (blk_get_attached_dev(blk
)) {
3001 /* Further I/O must not pause the guest */
3002 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
3003 BLOCKDEV_ON_ERROR_REPORT
);
3008 aio_context_release(aio_context
);
3011 void qmp_block_resize(bool has_device
, const char *device
,
3012 bool has_node_name
, const char *node_name
,
3013 int64_t size
, Error
**errp
)
3015 Error
*local_err
= NULL
;
3016 BlockBackend
*blk
= NULL
;
3017 BlockDriverState
*bs
;
3018 AioContext
*aio_context
;
3021 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
3022 has_node_name
? node_name
: NULL
,
3025 error_propagate(errp
, local_err
);
3029 aio_context
= bdrv_get_aio_context(bs
);
3030 aio_context_acquire(aio_context
);
3032 if (!bdrv_is_first_non_filter(bs
)) {
3033 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
3038 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
3042 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
3043 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
3047 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3048 ret
= blk_insert_bs(blk
, bs
, errp
);
3053 bdrv_drained_begin(bs
);
3054 ret
= blk_truncate(blk
, size
, PREALLOC_MODE_OFF
, errp
);
3055 bdrv_drained_end(bs
);
3059 aio_context_release(aio_context
);
3062 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3063 bool has_base
, const char *base
,
3064 bool has_base_node
, const char *base_node
,
3065 bool has_backing_file
, const char *backing_file
,
3066 bool has_speed
, int64_t speed
,
3067 bool has_on_error
, BlockdevOnError on_error
,
3070 BlockDriverState
*bs
, *iter
;
3071 BlockDriverState
*base_bs
= NULL
;
3072 AioContext
*aio_context
;
3073 Error
*local_err
= NULL
;
3074 const char *base_name
= NULL
;
3076 if (!has_on_error
) {
3077 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3080 bs
= bdrv_lookup_bs(device
, device
, errp
);
3085 aio_context
= bdrv_get_aio_context(bs
);
3086 aio_context_acquire(aio_context
);
3088 if (has_base
&& has_base_node
) {
3089 error_setg(errp
, "'base' and 'base-node' cannot be specified "
3090 "at the same time");
3095 base_bs
= bdrv_find_backing_image(bs
, base
);
3096 if (base_bs
== NULL
) {
3097 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3100 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3104 if (has_base_node
) {
3105 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
3109 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
3110 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
3114 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3115 base_name
= base_bs
->filename
;
3118 /* Check for op blockers in the whole chain between bs and base */
3119 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
3120 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3125 /* if we are streaming the entire chain, the result will have no backing
3126 * file, and specifying one is therefore an error */
3127 if (base_bs
== NULL
&& has_backing_file
) {
3128 error_setg(errp
, "backing file specified, but streaming the "
3133 /* backing_file string overrides base bs filename */
3134 base_name
= has_backing_file
? backing_file
: base_name
;
3136 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3137 has_speed
? speed
: 0, on_error
, &local_err
);
3139 error_propagate(errp
, local_err
);
3143 trace_qmp_block_stream(bs
, bs
->job
);
3146 aio_context_release(aio_context
);
3149 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3150 bool has_base
, const char *base
,
3151 bool has_top
, const char *top
,
3152 bool has_backing_file
, const char *backing_file
,
3153 bool has_speed
, int64_t speed
,
3154 bool has_filter_node_name
, const char *filter_node_name
,
3157 BlockDriverState
*bs
;
3158 BlockDriverState
*iter
;
3159 BlockDriverState
*base_bs
, *top_bs
;
3160 AioContext
*aio_context
;
3161 Error
*local_err
= NULL
;
3162 /* This will be part of the QMP command, if/when the
3163 * BlockdevOnError change for blkmirror makes it in
3165 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3170 if (!has_filter_node_name
) {
3171 filter_node_name
= NULL
;
3175 * libvirt relies on the DeviceNotFound error class in order to probe for
3176 * live commit feature versions; for this to work, we must make sure to
3177 * perform the device lookup before any generic errors that may occur in a
3178 * scenario in which all optional arguments are omitted. */
3179 bs
= qmp_get_root_bs(device
, &local_err
);
3181 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3183 error_free(local_err
);
3184 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3185 "Device '%s' not found", device
);
3187 error_propagate(errp
, local_err
);
3192 aio_context
= bdrv_get_aio_context(bs
);
3193 aio_context_acquire(aio_context
);
3195 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3199 /* default top_bs is the active layer */
3202 if (has_top
&& top
) {
3203 if (strcmp(bs
->filename
, top
) != 0) {
3204 top_bs
= bdrv_find_backing_image(bs
, top
);
3208 if (top_bs
== NULL
) {
3209 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3213 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3215 if (has_base
&& base
) {
3216 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3218 base_bs
= bdrv_find_base(top_bs
);
3221 if (base_bs
== NULL
) {
3222 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3226 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3228 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3229 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3234 /* Do not allow attempts to commit an image into itself */
3235 if (top_bs
== base_bs
) {
3236 error_setg(errp
, "cannot commit an image into itself");
3241 if (has_backing_file
) {
3242 error_setg(errp
, "'backing-file' specified,"
3243 " but 'top' is the active layer");
3246 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3247 BLOCK_JOB_DEFAULT
, speed
, on_error
,
3248 filter_node_name
, NULL
, NULL
, false, &local_err
);
3250 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3251 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3254 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3255 on_error
, has_backing_file
? backing_file
: NULL
,
3256 filter_node_name
, &local_err
);
3258 if (local_err
!= NULL
) {
3259 error_propagate(errp
, local_err
);
3264 aio_context_release(aio_context
);
3267 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
3270 BlockDriverState
*bs
;
3271 BlockDriverState
*target_bs
;
3272 BlockDriverState
*source
= NULL
;
3273 BlockJob
*job
= NULL
;
3274 BdrvDirtyBitmap
*bmap
= NULL
;
3275 AioContext
*aio_context
;
3276 QDict
*options
= NULL
;
3277 Error
*local_err
= NULL
;
3278 int flags
, job_flags
= BLOCK_JOB_DEFAULT
;
3280 bool set_backing_hd
= false;
3282 if (!backup
->has_speed
) {
3285 if (!backup
->has_on_source_error
) {
3286 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3288 if (!backup
->has_on_target_error
) {
3289 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3291 if (!backup
->has_mode
) {
3292 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3294 if (!backup
->has_job_id
) {
3295 backup
->job_id
= NULL
;
3297 if (!backup
->has_auto_finalize
) {
3298 backup
->auto_finalize
= true;
3300 if (!backup
->has_auto_dismiss
) {
3301 backup
->auto_dismiss
= true;
3303 if (!backup
->has_compress
) {
3304 backup
->compress
= false;
3307 bs
= qmp_get_root_bs(backup
->device
, errp
);
3312 aio_context
= bdrv_get_aio_context(bs
);
3313 aio_context_acquire(aio_context
);
3315 if (!backup
->has_format
) {
3316 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3317 NULL
: (char*) bs
->drv
->format_name
;
3320 /* Early check to avoid creating target */
3321 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3325 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3327 /* See if we have a backing HD we can use to create our new image
3329 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3330 source
= backing_bs(bs
);
3332 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3335 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3337 flags
|= BDRV_O_NO_BACKING
;
3338 set_backing_hd
= true;
3341 size
= bdrv_getlength(bs
);
3343 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3347 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3348 assert(backup
->format
);
3350 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3351 source
->drv
->format_name
, NULL
,
3352 size
, flags
, false, &local_err
);
3354 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3355 size
, flags
, false, &local_err
);
3360 error_propagate(errp
, local_err
);
3364 if (backup
->format
) {
3366 options
= qdict_new();
3368 qdict_put_str(options
, "driver", backup
->format
);
3371 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3376 bdrv_set_aio_context(target_bs
, aio_context
);
3378 if (set_backing_hd
) {
3379 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3381 bdrv_unref(target_bs
);
3386 if (backup
->has_bitmap
) {
3387 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3389 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3390 bdrv_unref(target_bs
);
3393 if (bdrv_dirty_bitmap_qmp_locked(bmap
)) {
3395 "Bitmap '%s' is currently locked and cannot be used for "
3396 "backup", backup
->bitmap
);
3400 if (!backup
->auto_finalize
) {
3401 job_flags
|= BLOCK_JOB_MANUAL_FINALIZE
;
3403 if (!backup
->auto_dismiss
) {
3404 job_flags
|= BLOCK_JOB_MANUAL_DISMISS
;
3407 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3408 backup
->sync
, bmap
, backup
->compress
,
3409 backup
->on_source_error
, backup
->on_target_error
,
3410 job_flags
, NULL
, NULL
, txn
, &local_err
);
3411 bdrv_unref(target_bs
);
3412 if (local_err
!= NULL
) {
3413 error_propagate(errp
, local_err
);
3418 aio_context_release(aio_context
);
3422 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3426 job
= do_drive_backup(arg
, NULL
, errp
);
3428 block_job_start(job
);
3432 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3434 return bdrv_named_nodes_list(errp
);
3437 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
3440 BlockDriverState
*bs
;
3441 BlockDriverState
*target_bs
;
3442 Error
*local_err
= NULL
;
3443 AioContext
*aio_context
;
3444 BlockJob
*job
= NULL
;
3445 int job_flags
= BLOCK_JOB_DEFAULT
;
3447 if (!backup
->has_speed
) {
3450 if (!backup
->has_on_source_error
) {
3451 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3453 if (!backup
->has_on_target_error
) {
3454 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3456 if (!backup
->has_job_id
) {
3457 backup
->job_id
= NULL
;
3459 if (!backup
->has_auto_finalize
) {
3460 backup
->auto_finalize
= true;
3462 if (!backup
->has_auto_dismiss
) {
3463 backup
->auto_dismiss
= true;
3465 if (!backup
->has_compress
) {
3466 backup
->compress
= false;
3469 bs
= qmp_get_root_bs(backup
->device
, errp
);
3474 aio_context
= bdrv_get_aio_context(bs
);
3475 aio_context_acquire(aio_context
);
3477 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3482 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3483 if (!bdrv_has_blk(target_bs
)) {
3484 /* The target BDS is not attached, we can safely move it to another
3486 bdrv_set_aio_context(target_bs
, aio_context
);
3488 error_setg(errp
, "Target is attached to a different thread from "
3493 if (!backup
->auto_finalize
) {
3494 job_flags
|= BLOCK_JOB_MANUAL_FINALIZE
;
3496 if (!backup
->auto_dismiss
) {
3497 job_flags
|= BLOCK_JOB_MANUAL_DISMISS
;
3499 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3500 backup
->sync
, NULL
, backup
->compress
,
3501 backup
->on_source_error
, backup
->on_target_error
,
3502 job_flags
, NULL
, NULL
, txn
, &local_err
);
3503 if (local_err
!= NULL
) {
3504 error_propagate(errp
, local_err
);
3507 aio_context_release(aio_context
);
3511 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3514 job
= do_blockdev_backup(arg
, NULL
, errp
);
3516 block_job_start(job
);
3520 /* Parameter check and block job starting for drive mirroring.
3521 * Caller should hold @device and @target's aio context (must be the same).
3523 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3524 BlockDriverState
*target
,
3525 bool has_replaces
, const char *replaces
,
3526 enum MirrorSyncMode sync
,
3527 BlockMirrorBackingMode backing_mode
,
3528 bool has_speed
, int64_t speed
,
3529 bool has_granularity
, uint32_t granularity
,
3530 bool has_buf_size
, int64_t buf_size
,
3531 bool has_on_source_error
,
3532 BlockdevOnError on_source_error
,
3533 bool has_on_target_error
,
3534 BlockdevOnError on_target_error
,
3535 bool has_unmap
, bool unmap
,
3536 bool has_filter_node_name
,
3537 const char *filter_node_name
,
3544 if (!has_on_source_error
) {
3545 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3547 if (!has_on_target_error
) {
3548 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3550 if (!has_granularity
) {
3553 if (!has_buf_size
) {
3559 if (!has_filter_node_name
) {
3560 filter_node_name
= NULL
;
3563 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3564 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3565 "a value in range [512B, 64MB]");
3568 if (granularity
& (granularity
- 1)) {
3569 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3574 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3577 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3581 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3582 sync
= MIRROR_SYNC_MODE_FULL
;
3585 /* pass the node name to replace to mirror start since it's loose coupling
3586 * and will allow to check whether the node still exist at mirror completion
3588 mirror_start(job_id
, bs
, target
,
3589 has_replaces
? replaces
: NULL
,
3590 speed
, granularity
, buf_size
, sync
, backing_mode
,
3591 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3595 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3597 BlockDriverState
*bs
;
3598 BlockDriverState
*source
, *target_bs
;
3599 AioContext
*aio_context
;
3600 BlockMirrorBackingMode backing_mode
;
3601 Error
*local_err
= NULL
;
3602 QDict
*options
= NULL
;
3605 const char *format
= arg
->format
;
3607 bs
= qmp_get_root_bs(arg
->device
, errp
);
3612 /* Early check to avoid creating target */
3613 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3617 aio_context
= bdrv_get_aio_context(bs
);
3618 aio_context_acquire(aio_context
);
3620 if (!arg
->has_mode
) {
3621 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3624 if (!arg
->has_format
) {
3625 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3626 ? NULL
: bs
->drv
->format_name
);
3629 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3630 source
= backing_bs(bs
);
3631 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3632 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3634 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3638 size
= bdrv_getlength(bs
);
3640 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3644 if (arg
->has_replaces
) {
3645 BlockDriverState
*to_replace_bs
;
3646 AioContext
*replace_aio_context
;
3647 int64_t replace_size
;
3649 if (!arg
->has_node_name
) {
3650 error_setg(errp
, "a node-name must be provided when replacing a"
3651 " named node of the graph");
3655 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3657 if (!to_replace_bs
) {
3658 error_propagate(errp
, local_err
);
3662 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3663 aio_context_acquire(replace_aio_context
);
3664 replace_size
= bdrv_getlength(to_replace_bs
);
3665 aio_context_release(replace_aio_context
);
3667 if (size
!= replace_size
) {
3668 error_setg(errp
, "cannot replace image with a mirror image of "
3674 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3675 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3677 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3680 /* Don't open backing image in create() */
3681 flags
|= BDRV_O_NO_BACKING
;
3683 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3684 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3686 /* create new image w/o backing file */
3688 bdrv_img_create(arg
->target
, format
,
3689 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3691 switch (arg
->mode
) {
3692 case NEW_IMAGE_MODE_EXISTING
:
3694 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3695 /* create new image with backing file */
3696 bdrv_img_create(arg
->target
, format
,
3698 source
->drv
->format_name
,
3699 NULL
, size
, flags
, false, &local_err
);
3707 error_propagate(errp
, local_err
);
3711 options
= qdict_new();
3712 if (arg
->has_node_name
) {
3713 qdict_put_str(options
, "node-name", arg
->node_name
);
3716 qdict_put_str(options
, "driver", format
);
3719 /* Mirroring takes care of copy-on-write using the source's backing
3722 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3727 bdrv_set_aio_context(target_bs
, aio_context
);
3729 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3730 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3731 backing_mode
, arg
->has_speed
, arg
->speed
,
3732 arg
->has_granularity
, arg
->granularity
,
3733 arg
->has_buf_size
, arg
->buf_size
,
3734 arg
->has_on_source_error
, arg
->on_source_error
,
3735 arg
->has_on_target_error
, arg
->on_target_error
,
3736 arg
->has_unmap
, arg
->unmap
,
3739 bdrv_unref(target_bs
);
3740 error_propagate(errp
, local_err
);
3742 aio_context_release(aio_context
);
3745 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3746 const char *device
, const char *target
,
3747 bool has_replaces
, const char *replaces
,
3748 MirrorSyncMode sync
,
3749 bool has_speed
, int64_t speed
,
3750 bool has_granularity
, uint32_t granularity
,
3751 bool has_buf_size
, int64_t buf_size
,
3752 bool has_on_source_error
,
3753 BlockdevOnError on_source_error
,
3754 bool has_on_target_error
,
3755 BlockdevOnError on_target_error
,
3756 bool has_filter_node_name
,
3757 const char *filter_node_name
,
3760 BlockDriverState
*bs
;
3761 BlockDriverState
*target_bs
;
3762 AioContext
*aio_context
;
3763 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3764 Error
*local_err
= NULL
;
3766 bs
= qmp_get_root_bs(device
, errp
);
3771 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3776 aio_context
= bdrv_get_aio_context(bs
);
3777 aio_context_acquire(aio_context
);
3779 bdrv_set_aio_context(target_bs
, aio_context
);
3781 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3782 has_replaces
, replaces
, sync
, backing_mode
,
3784 has_granularity
, granularity
,
3785 has_buf_size
, buf_size
,
3786 has_on_source_error
, on_source_error
,
3787 has_on_target_error
, on_target_error
,
3789 has_filter_node_name
, filter_node_name
,
3791 error_propagate(errp
, local_err
);
3793 aio_context_release(aio_context
);
3796 /* Get a block job using its ID and acquire its AioContext */
3797 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3804 *aio_context
= NULL
;
3806 job
= block_job_get(id
);
3809 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3810 "Block job '%s' not found", id
);
3814 *aio_context
= blk_get_aio_context(job
->blk
);
3815 aio_context_acquire(*aio_context
);
3820 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3822 AioContext
*aio_context
;
3823 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3829 block_job_set_speed(job
, speed
, errp
);
3830 aio_context_release(aio_context
);
3833 void qmp_block_job_cancel(const char *device
,
3834 bool has_force
, bool force
, Error
**errp
)
3836 AioContext
*aio_context
;
3837 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3847 if (block_job_user_paused(job
) && !force
) {
3848 error_setg(errp
, "The block job for device '%s' is currently paused",
3853 trace_qmp_block_job_cancel(job
);
3854 block_job_user_cancel(job
, force
, errp
);
3856 aio_context_release(aio_context
);
3859 void qmp_block_job_pause(const char *device
, Error
**errp
)
3861 AioContext
*aio_context
;
3862 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3868 trace_qmp_block_job_pause(job
);
3869 block_job_user_pause(job
, errp
);
3870 aio_context_release(aio_context
);
3873 void qmp_block_job_resume(const char *device
, Error
**errp
)
3875 AioContext
*aio_context
;
3876 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3882 trace_qmp_block_job_resume(job
);
3883 block_job_user_resume(job
, errp
);
3884 aio_context_release(aio_context
);
3887 void qmp_block_job_complete(const char *device
, Error
**errp
)
3889 AioContext
*aio_context
;
3890 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3896 trace_qmp_block_job_complete(job
);
3897 block_job_complete(job
, errp
);
3898 aio_context_release(aio_context
);
3901 void qmp_block_job_finalize(const char *id
, Error
**errp
)
3903 AioContext
*aio_context
;
3904 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3910 trace_qmp_block_job_finalize(job
);
3911 block_job_finalize(job
, errp
);
3912 aio_context_release(aio_context
);
3915 void qmp_block_job_dismiss(const char *id
, Error
**errp
)
3917 AioContext
*aio_context
;
3918 BlockJob
*job
= find_block_job(id
, &aio_context
, errp
);
3924 trace_qmp_block_job_dismiss(job
);
3925 block_job_dismiss(&job
, errp
);
3926 aio_context_release(aio_context
);
3929 void qmp_change_backing_file(const char *device
,
3930 const char *image_node_name
,
3931 const char *backing_file
,
3934 BlockDriverState
*bs
= NULL
;
3935 AioContext
*aio_context
;
3936 BlockDriverState
*image_bs
= NULL
;
3937 Error
*local_err
= NULL
;
3942 bs
= qmp_get_root_bs(device
, errp
);
3947 aio_context
= bdrv_get_aio_context(bs
);
3948 aio_context_acquire(aio_context
);
3950 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3952 error_propagate(errp
, local_err
);
3957 error_setg(errp
, "image file not found");
3961 if (bdrv_find_base(image_bs
) == image_bs
) {
3962 error_setg(errp
, "not allowing backing file change on an image "
3963 "without a backing file");
3967 /* even though we are not necessarily operating on bs, we need it to
3968 * determine if block ops are currently prohibited on the chain */
3969 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3973 /* final sanity check */
3974 if (!bdrv_chain_contains(bs
, image_bs
)) {
3975 error_setg(errp
, "'%s' and image file are not in the same chain",
3980 /* if not r/w, reopen to make r/w */
3981 open_flags
= image_bs
->open_flags
;
3982 ro
= bdrv_is_read_only(image_bs
);
3985 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3987 error_propagate(errp
, local_err
);
3992 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3993 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3996 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3998 /* don't exit here, so we can try to restore open flags if
4003 bdrv_reopen(image_bs
, open_flags
, &local_err
);
4004 error_propagate(errp
, local_err
);
4008 aio_context_release(aio_context
);
4011 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
4015 Error
*local_err
= NULL
;
4017 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
4022 qdict
= qemu_opts_to_qdict(opts
, NULL
);
4024 if (!qdict_get_try_str(qdict
, "node-name")) {
4025 qobject_unref(qdict
);
4026 error_report("'node-name' needs to be specified");
4030 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
4032 error_report_err(local_err
);
4036 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4039 qemu_opts_del(opts
);
4042 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
4044 BlockDriverState
*bs
;
4046 Visitor
*v
= qobject_output_visitor_new(&obj
);
4048 Error
*local_err
= NULL
;
4050 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
4052 error_propagate(errp
, local_err
);
4056 visit_complete(v
, &obj
);
4057 qdict
= qobject_to(QDict
, obj
);
4059 qdict_flatten(qdict
);
4061 if (!qdict_get_try_str(qdict
, "node-name")) {
4062 error_setg(errp
, "'node-name' must be specified for the root node");
4066 bs
= bds_tree_init(qdict
, errp
);
4071 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4077 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
4079 AioContext
*aio_context
;
4080 BlockDriverState
*bs
;
4082 bs
= bdrv_find_node(node_name
);
4084 error_setg(errp
, "Cannot find node %s", node_name
);
4087 if (bdrv_has_blk(bs
)) {
4088 error_setg(errp
, "Node %s is in use", node_name
);
4091 aio_context
= bdrv_get_aio_context(bs
);
4092 aio_context_acquire(aio_context
);
4094 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4098 if (!bs
->monitor_list
.tqe_prev
) {
4099 error_setg(errp
, "Node %s is not owned by the monitor",
4104 if (bs
->refcnt
> 1) {
4105 error_setg(errp
, "Block device %s is in use",
4106 bdrv_get_device_or_node_name(bs
));
4110 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4114 aio_context_release(aio_context
);
4117 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4118 const char *child_name
)
4122 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4123 if (strcmp(child
->name
, child_name
) == 0) {
4131 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4132 const char *child
, bool has_node
,
4133 const char *node
, Error
**errp
)
4135 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4138 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4143 if (has_child
== has_node
) {
4145 error_setg(errp
, "The parameters child and node are in conflict");
4147 error_setg(errp
, "Either child or node must be specified");
4153 p_child
= bdrv_find_child(parent_bs
, child
);
4155 error_setg(errp
, "Node '%s' does not have child '%s'",
4159 bdrv_del_child(parent_bs
, p_child
, errp
);
4163 new_bs
= bdrv_find_node(node
);
4165 error_setg(errp
, "Node '%s' not found", node
);
4168 bdrv_add_child(parent_bs
, new_bs
, errp
);
4172 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4174 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4177 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4178 BlockJobInfoList
*elem
;
4179 AioContext
*aio_context
;
4181 if (block_job_is_internal(job
)) {
4184 elem
= g_new0(BlockJobInfoList
, 1);
4185 aio_context
= blk_get_aio_context(job
->blk
);
4186 aio_context_acquire(aio_context
);
4187 elem
->value
= block_job_query(job
, errp
);
4188 aio_context_release(aio_context
);
4191 qapi_free_BlockJobInfoList(head
);
4195 p_next
= &elem
->next
;
4201 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
4202 bool has_force
, bool force
, Error
**errp
)
4204 AioContext
*old_context
;
4205 AioContext
*new_context
;
4206 BlockDriverState
*bs
;
4208 bs
= bdrv_find_node(node_name
);
4210 error_setg(errp
, "Cannot find node %s", node_name
);
4214 /* Protects against accidents. */
4215 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
4216 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
4217 "be in use (use force=true to override this check)",
4222 if (iothread
->type
== QTYPE_QSTRING
) {
4223 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
4225 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
4229 new_context
= iothread_get_aio_context(obj
);
4231 new_context
= qemu_get_aio_context();
4234 old_context
= bdrv_get_aio_context(bs
);
4235 aio_context_acquire(old_context
);
4237 bdrv_set_aio_context(bs
, new_context
);
4239 aio_context_release(old_context
);
4242 void qmp_x_block_latency_histogram_set(
4244 bool has_boundaries
, uint64List
*boundaries
,
4245 bool has_boundaries_read
, uint64List
*boundaries_read
,
4246 bool has_boundaries_write
, uint64List
*boundaries_write
,
4247 bool has_boundaries_flush
, uint64List
*boundaries_flush
,
4250 BlockBackend
*blk
= blk_by_name(device
);
4251 BlockAcctStats
*stats
;
4254 error_setg(errp
, "Device '%s' not found", device
);
4257 stats
= blk_get_stats(blk
);
4259 if (!has_boundaries
&& !has_boundaries_read
&& !has_boundaries_write
&&
4260 !has_boundaries_flush
)
4262 block_latency_histograms_clear(stats
);
4266 if (has_boundaries
|| has_boundaries_read
) {
4267 block_latency_histogram_set(
4268 stats
, BLOCK_ACCT_READ
,
4269 has_boundaries_read
? boundaries_read
: boundaries
);
4272 if (has_boundaries
|| has_boundaries_write
) {
4273 block_latency_histogram_set(
4274 stats
, BLOCK_ACCT_WRITE
,
4275 has_boundaries_write
? boundaries_write
: boundaries
);
4278 if (has_boundaries
|| has_boundaries_flush
) {
4279 block_latency_histogram_set(
4280 stats
, BLOCK_ACCT_FLUSH
,
4281 has_boundaries_flush
? boundaries_flush
: boundaries
);
4285 QemuOptsList qemu_common_drive_opts
= {
4287 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4291 .type
= QEMU_OPT_BOOL
,
4292 .help
= "enable/disable snapshot mode",
4295 .type
= QEMU_OPT_STRING
,
4296 .help
= "host AIO implementation (threads, native)",
4298 .name
= BDRV_OPT_CACHE_WB
,
4299 .type
= QEMU_OPT_BOOL
,
4300 .help
= "Enable writeback mode",
4303 .type
= QEMU_OPT_STRING
,
4304 .help
= "disk format (raw, qcow2, ...)",
4307 .type
= QEMU_OPT_STRING
,
4308 .help
= "read error action",
4311 .type
= QEMU_OPT_STRING
,
4312 .help
= "write error action",
4314 .name
= BDRV_OPT_READ_ONLY
,
4315 .type
= QEMU_OPT_BOOL
,
4316 .help
= "open drive file as read-only",
4322 .name
= "throttling.group",
4323 .type
= QEMU_OPT_STRING
,
4324 .help
= "name of the block throttling group",
4326 .name
= "copy-on-read",
4327 .type
= QEMU_OPT_BOOL
,
4328 .help
= "copy read data from backing file into image file",
4330 .name
= "detect-zeroes",
4331 .type
= QEMU_OPT_STRING
,
4332 .help
= "try to optimize zero writes (off, on, unmap)",
4334 .name
= "stats-account-invalid",
4335 .type
= QEMU_OPT_BOOL
,
4336 .help
= "whether to account for invalid I/O operations "
4337 "in the statistics",
4339 .name
= "stats-account-failed",
4340 .type
= QEMU_OPT_BOOL
,
4341 .help
= "whether to account for failed I/O operations "
4342 "in the statistics",
4344 { /* end of list */ }
4348 QemuOptsList qemu_drive_opts
= {
4350 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4353 * no elements => accept any params
4354 * validation will happen later
4356 { /* end of list */ }