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/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qmp-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
52 #include "sysemu/arch_init.h"
53 #include "qemu/cutils.h"
54 #include "qemu/help_option.h"
56 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
57 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
59 static int do_open_tray(const char *device
, bool force
, Error
**errp
);
61 static const char *const if_name
[IF_COUNT
] = {
65 [IF_FLOPPY
] = "floppy",
66 [IF_PFLASH
] = "pflash",
69 [IF_VIRTIO
] = "virtio",
73 static int if_max_devs
[IF_COUNT
] = {
75 * Do not change these numbers! They govern how drive option
76 * index maps to unit and bus. That mapping is ABI.
78 * All controllers used to implement if=T drives need to support
79 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
80 * Otherwise, some index values map to "impossible" bus, unit
83 * For instance, if you change [IF_SCSI] to 255, -drive
84 * if=scsi,index=12 no longer means bus=1,unit=5, but
85 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
86 * the drive can't be set up. Regression.
93 * Boards may call this to offer board-by-board overrides
94 * of the default, global values.
96 void override_max_devs(BlockInterfaceType type
, int max_devs
)
105 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
106 dinfo
= blk_legacy_dinfo(blk
);
107 if (dinfo
->type
== type
) {
108 fprintf(stderr
, "Cannot override units-per-bus property of"
109 " the %s interface, because a drive of that type has"
110 " already been added.\n", if_name
[type
]);
111 g_assert_not_reached();
115 if_max_devs
[type
] = max_devs
;
119 * We automatically delete the drive when a device using it gets
120 * unplugged. Questionable feature, but we can't just drop it.
121 * Device models call blockdev_mark_auto_del() to schedule the
122 * automatic deletion, and generic qdev code calls blockdev_auto_del()
123 * when deletion is actually safe.
125 void blockdev_mark_auto_del(BlockBackend
*blk
)
127 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
128 BlockDriverState
*bs
= blk_bs(blk
);
129 AioContext
*aio_context
;
136 aio_context
= bdrv_get_aio_context(bs
);
137 aio_context_acquire(aio_context
);
140 block_job_cancel(bs
->job
);
143 aio_context_release(aio_context
);
149 void blockdev_auto_del(BlockBackend
*blk
)
151 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
153 if (dinfo
&& dinfo
->auto_del
) {
154 monitor_remove_blk(blk
);
160 * Returns the current mapping of how many units per bus
161 * a particular interface can support.
163 * A positive integer indicates n units per bus.
164 * 0 implies the mapping has not been established.
165 * -1 indicates an invalid BlockInterfaceType was given.
167 int drive_get_max_devs(BlockInterfaceType type
)
169 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
170 return if_max_devs
[type
];
176 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
178 int max_devs
= if_max_devs
[type
];
179 return max_devs
? index
/ max_devs
: 0;
182 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
184 int max_devs
= if_max_devs
[type
];
185 return max_devs
? index
% max_devs
: index
;
188 QemuOpts
*drive_def(const char *optstr
)
190 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
193 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
198 opts
= drive_def(optstr
);
202 if (type
!= IF_DEFAULT
) {
203 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
206 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
209 qemu_opt_set(opts
, "file", file
, &error_abort
);
213 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
218 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
219 dinfo
= blk_legacy_dinfo(blk
);
220 if (dinfo
&& dinfo
->type
== type
221 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
229 bool drive_check_orphaned(void)
235 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
236 dinfo
= blk_legacy_dinfo(blk
);
237 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
238 /* Unless this is a default drive, this may be an oversight. */
239 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
240 dinfo
->type
!= IF_NONE
) {
241 fprintf(stderr
, "Warning: Orphaned drive without device: "
242 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
243 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
244 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
252 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
254 return drive_get(type
,
255 drive_index_to_bus_id(type
, index
),
256 drive_index_to_unit_id(type
, index
));
259 int drive_get_max_bus(BlockInterfaceType type
)
266 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
267 dinfo
= blk_legacy_dinfo(blk
);
268 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
269 max_bus
= dinfo
->bus
;
275 /* Get a block device. This should only be used for single-drive devices
276 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
278 DriveInfo
*drive_get_next(BlockInterfaceType type
)
280 static int next_block_unit
[IF_COUNT
];
282 return drive_get(type
, 0, next_block_unit
[type
]++);
285 static void bdrv_format_print(void *opaque
, const char *name
)
287 error_printf(" %s", name
);
292 BlockDriverState
*bs
;
295 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
297 if (!strcmp(buf
, "ignore")) {
298 return BLOCKDEV_ON_ERROR_IGNORE
;
299 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
300 return BLOCKDEV_ON_ERROR_ENOSPC
;
301 } else if (!strcmp(buf
, "stop")) {
302 return BLOCKDEV_ON_ERROR_STOP
;
303 } else if (!strcmp(buf
, "report")) {
304 return BLOCKDEV_ON_ERROR_REPORT
;
306 error_setg(errp
, "'%s' invalid %s error action",
307 buf
, is_read
? "read" : "write");
312 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
315 const QListEntry
*entry
;
316 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
317 switch (qobject_type(entry
->value
)) {
319 case QTYPE_QSTRING
: {
320 unsigned long long length
;
321 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
322 if (parse_uint_full(str
, &length
, 10) == 0 &&
323 length
> 0 && length
<= UINT_MAX
) {
324 block_acct_add_interval(stats
, (unsigned) length
);
326 error_setg(errp
, "Invalid interval length: %s", str
);
333 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
334 if (length
> 0 && length
<= UINT_MAX
) {
335 block_acct_add_interval(stats
, (unsigned) length
);
337 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
344 error_setg(errp
, "The specification of stats-intervals is invalid");
351 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
353 /* All parameters but @opts are optional and may be set to NULL. */
354 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
355 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
356 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
359 Error
*local_error
= NULL
;
363 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
364 *bdrv_flags
|= BDRV_O_RDWR
;
366 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
367 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
370 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
371 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
372 error_setg(errp
, "Invalid discard option");
377 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
378 if (!strcmp(aio
, "native")) {
379 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
380 } else if (!strcmp(aio
, "threads")) {
381 /* this is the default */
383 error_setg(errp
, "invalid aio option");
389 /* disk I/O throttling */
390 if (throttling_group
) {
391 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
395 throttle_config_init(throttle_cfg
);
396 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
397 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
398 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
399 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
400 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
401 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
402 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
403 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
404 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
405 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
406 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
407 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
409 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
410 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
411 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
412 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
413 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
414 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
415 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
416 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
417 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
418 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
419 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
420 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
422 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
423 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
424 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
425 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
426 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
427 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
428 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
429 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
430 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
431 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
432 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
433 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
435 throttle_cfg
->op_size
=
436 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
438 if (!throttle_is_valid(throttle_cfg
, errp
)) {
445 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
446 qemu_opt_get(opts
, "detect-zeroes"),
447 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
451 error_propagate(errp
, local_error
);
456 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
457 !(*bdrv_flags
& BDRV_O_UNMAP
))
459 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
460 "without setting discard operation to unmap");
466 /* Takes the ownership of bs_opts */
467 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
472 int on_read_error
, on_write_error
;
473 bool account_invalid
, account_failed
;
476 BlockDriverState
*bs
;
481 QDict
*interval_dict
= NULL
;
482 QList
*interval_list
= NULL
;
484 BlockdevDetectZeroesOptions detect_zeroes
=
485 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
486 const char *throttling_group
= NULL
;
488 /* Check common options by copying from bs_opts to opts, all other options
489 * stay in bs_opts for processing by bdrv_open(). */
490 id
= qdict_get_try_str(bs_opts
, "id");
491 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
493 error_propagate(errp
, error
);
497 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
499 error_propagate(errp
, error
);
504 qdict_del(bs_opts
, "id");
507 /* extract parameters */
508 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
510 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
511 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
513 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
515 id
= qemu_opts_id(opts
);
517 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
518 qdict_array_split(interval_dict
, &interval_list
);
520 if (qdict_size(interval_dict
) != 0) {
521 error_setg(errp
, "Invalid option stats-intervals.%s",
522 qdict_first(interval_dict
)->key
);
526 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
527 &detect_zeroes
, &error
);
529 error_propagate(errp
, error
);
533 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
534 if (is_help_option(buf
)) {
535 error_printf("Supported formats:");
536 bdrv_iterate_format(bdrv_format_print
, NULL
);
541 if (qdict_haskey(bs_opts
, "driver")) {
542 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
545 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
548 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
549 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
550 on_write_error
= parse_block_error_action(buf
, 0, &error
);
552 error_propagate(errp
, error
);
557 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
558 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
559 on_read_error
= parse_block_error_action(buf
, 1, &error
);
561 error_propagate(errp
, error
);
567 bdrv_flags
|= BDRV_O_SNAPSHOT
;
571 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
572 BlockBackendRootState
*blk_rs
;
575 blk_rs
= blk_get_root_state(blk
);
576 blk_rs
->open_flags
= bdrv_flags
;
577 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
578 blk_rs
->detect_zeroes
= detect_zeroes
;
582 if (file
&& !*file
) {
586 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
587 * with other callers) rather than what we want as the real defaults.
588 * Apply the defaults here instead. */
589 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
590 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
591 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
593 if (runstate_check(RUN_STATE_INMIGRATE
)) {
594 bdrv_flags
|= BDRV_O_INACTIVE
;
597 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
603 bs
->detect_zeroes
= detect_zeroes
;
605 if (bdrv_key_required(bs
)) {
609 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
611 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
618 /* disk I/O throttling */
619 if (throttle_enabled(&cfg
)) {
620 if (!throttling_group
) {
621 throttling_group
= id
;
623 blk_io_limits_enable(blk
, throttling_group
);
624 blk_set_io_limits(blk
, &cfg
);
627 blk_set_enable_write_cache(blk
, !writethrough
);
628 blk_set_on_error(blk
, on_read_error
, on_write_error
);
630 if (!monitor_add_blk(blk
, id
, errp
)) {
638 QDECREF(interval_dict
);
639 QDECREF(interval_list
);
644 QDECREF(interval_dict
);
645 QDECREF(interval_list
);
651 static QemuOptsList qemu_root_bds_opts
;
653 /* Takes the ownership of bs_opts */
654 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
656 BlockDriverState
*bs
;
658 Error
*local_error
= NULL
;
659 BlockdevDetectZeroesOptions detect_zeroes
;
662 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
667 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
669 error_propagate(errp
, local_error
);
673 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
674 &detect_zeroes
, &local_error
);
676 error_propagate(errp
, local_error
);
680 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
681 * with other callers) rather than what we want as the real defaults.
682 * Apply the defaults here instead. */
683 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
684 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
686 if (runstate_check(RUN_STATE_INMIGRATE
)) {
687 bdrv_flags
|= BDRV_O_INACTIVE
;
690 bs
= bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
692 goto fail_no_bs_opts
;
695 bs
->detect_zeroes
= detect_zeroes
;
707 void blockdev_close_all_bdrv_states(void)
709 BlockDriverState
*bs
, *next_bs
;
711 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
712 AioContext
*ctx
= bdrv_get_aio_context(bs
);
714 aio_context_acquire(ctx
);
716 aio_context_release(ctx
);
720 /* Iterates over the list of monitor-owned BlockDriverStates */
721 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
723 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
724 : QTAILQ_FIRST(&monitor_bdrv_states
);
727 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
732 value
= qemu_opt_get(opts
, from
);
734 if (qemu_opt_find(opts
, to
)) {
735 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
736 "same time", to
, from
);
741 /* rename all items in opts */
742 while ((value
= qemu_opt_get(opts
, from
))) {
743 qemu_opt_set(opts
, to
, value
, &error_abort
);
744 qemu_opt_unset(opts
, from
);
748 QemuOptsList qemu_legacy_drive_opts
= {
750 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
754 .type
= QEMU_OPT_NUMBER
,
755 .help
= "bus number",
758 .type
= QEMU_OPT_NUMBER
,
759 .help
= "unit number (i.e. lun for scsi)",
762 .type
= QEMU_OPT_NUMBER
,
763 .help
= "index number",
766 .type
= QEMU_OPT_STRING
,
767 .help
= "media type (disk, cdrom)",
770 .type
= QEMU_OPT_STRING
,
771 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
774 .type
= QEMU_OPT_NUMBER
,
775 .help
= "number of cylinders (ide disk geometry)",
778 .type
= QEMU_OPT_NUMBER
,
779 .help
= "number of heads (ide disk geometry)",
782 .type
= QEMU_OPT_NUMBER
,
783 .help
= "number of sectors (ide disk geometry)",
786 .type
= QEMU_OPT_STRING
,
787 .help
= "chs translation (auto, lba, none)",
790 .type
= QEMU_OPT_BOOL
,
791 .help
= "(deprecated, ignored)",
794 .type
= QEMU_OPT_STRING
,
795 .help
= "pci address (virtio only)",
798 .type
= QEMU_OPT_STRING
,
799 .help
= "disk serial number",
802 .type
= QEMU_OPT_STRING
,
806 /* Options that are passed on, but have special semantics with -drive */
809 .type
= QEMU_OPT_BOOL
,
810 .help
= "open drive file as read-only",
813 .type
= QEMU_OPT_STRING
,
814 .help
= "read error action",
817 .type
= QEMU_OPT_STRING
,
818 .help
= "write error action",
820 .name
= "copy-on-read",
821 .type
= QEMU_OPT_BOOL
,
822 .help
= "copy read data from backing file into image file",
825 { /* end of list */ }
829 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
833 DriveInfo
*dinfo
= NULL
;
835 QemuOpts
*legacy_opts
;
836 DriveMediaType media
= MEDIA_DISK
;
837 BlockInterfaceType type
;
838 int cyls
, heads
, secs
, translation
;
839 int max_devs
, bus_id
, unit_id
, index
;
841 const char *werror
, *rerror
;
842 bool read_only
= false;
845 const char *filename
;
846 Error
*local_err
= NULL
;
849 /* Change legacy command line options into QMP ones */
850 static const struct {
854 { "iops", "throttling.iops-total" },
855 { "iops_rd", "throttling.iops-read" },
856 { "iops_wr", "throttling.iops-write" },
858 { "bps", "throttling.bps-total" },
859 { "bps_rd", "throttling.bps-read" },
860 { "bps_wr", "throttling.bps-write" },
862 { "iops_max", "throttling.iops-total-max" },
863 { "iops_rd_max", "throttling.iops-read-max" },
864 { "iops_wr_max", "throttling.iops-write-max" },
866 { "bps_max", "throttling.bps-total-max" },
867 { "bps_rd_max", "throttling.bps-read-max" },
868 { "bps_wr_max", "throttling.bps-write-max" },
870 { "iops_size", "throttling.iops-size" },
872 { "group", "throttling.group" },
874 { "readonly", "read-only" },
877 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
878 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
881 error_report_err(local_err
);
886 value
= qemu_opt_get(all_opts
, "cache");
891 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
892 error_report("invalid cache option");
896 /* Specific options take precedence */
897 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
898 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
899 !writethrough
, &error_abort
);
901 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
902 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
903 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
905 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
906 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
907 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
909 qemu_opt_unset(all_opts
, "cache");
912 /* Get a QDict for processing the options */
913 bs_opts
= qdict_new();
914 qemu_opts_to_qdict(all_opts
, bs_opts
);
916 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
918 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
920 error_report_err(local_err
);
924 /* Deprecated option boot=[on|off] */
925 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
926 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
927 "ignored. Future versions will reject this parameter. Please "
928 "update your scripts.\n");
932 value
= qemu_opt_get(legacy_opts
, "media");
934 if (!strcmp(value
, "disk")) {
936 } else if (!strcmp(value
, "cdrom")) {
940 error_report("'%s' invalid media", value
);
945 /* copy-on-read is disabled with a warning for read-only devices */
946 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
947 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
949 if (read_only
&& copy_on_read
) {
950 error_report("warning: disabling copy-on-read on read-only drive");
951 copy_on_read
= false;
954 qdict_put(bs_opts
, "read-only",
955 qstring_from_str(read_only
? "on" : "off"));
956 qdict_put(bs_opts
, "copy-on-read",
957 qstring_from_str(copy_on_read
? "on" :"off"));
959 /* Controller type */
960 value
= qemu_opt_get(legacy_opts
, "if");
963 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
966 if (type
== IF_COUNT
) {
967 error_report("unsupported bus type '%s'", value
);
971 type
= block_default_type
;
975 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
976 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
977 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
979 if (cyls
|| heads
|| secs
) {
981 error_report("invalid physical cyls number");
985 error_report("invalid physical heads number");
989 error_report("invalid physical secs number");
994 translation
= BIOS_ATA_TRANSLATION_AUTO
;
995 value
= qemu_opt_get(legacy_opts
, "trans");
998 error_report("'%s' trans must be used with cyls, heads and secs",
1002 if (!strcmp(value
, "none")) {
1003 translation
= BIOS_ATA_TRANSLATION_NONE
;
1004 } else if (!strcmp(value
, "lba")) {
1005 translation
= BIOS_ATA_TRANSLATION_LBA
;
1006 } else if (!strcmp(value
, "large")) {
1007 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1008 } else if (!strcmp(value
, "rechs")) {
1009 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1010 } else if (!strcmp(value
, "auto")) {
1011 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1013 error_report("'%s' invalid translation type", value
);
1018 if (media
== MEDIA_CDROM
) {
1019 if (cyls
|| secs
|| heads
) {
1020 error_report("CHS can't be set with media=cdrom");
1025 /* Device address specified by bus/unit or index.
1026 * If none was specified, try to find the first free one. */
1027 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1028 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1029 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1031 max_devs
= if_max_devs
[type
];
1034 if (bus_id
!= 0 || unit_id
!= -1) {
1035 error_report("index cannot be used with bus and unit");
1038 bus_id
= drive_index_to_bus_id(type
, index
);
1039 unit_id
= drive_index_to_unit_id(type
, index
);
1042 if (unit_id
== -1) {
1044 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1046 if (max_devs
&& unit_id
>= max_devs
) {
1047 unit_id
-= max_devs
;
1053 if (max_devs
&& unit_id
>= max_devs
) {
1054 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1058 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1059 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1060 bus_id
, unit_id
, index
);
1065 serial
= qemu_opt_get(legacy_opts
, "serial");
1067 /* no id supplied -> create one */
1068 if (qemu_opts_id(all_opts
) == NULL
) {
1070 const char *mediastr
= "";
1071 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1072 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1075 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1078 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1081 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1085 /* Add virtio block device */
1086 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1087 if (devaddr
&& type
!= IF_VIRTIO
) {
1088 error_report("addr is not supported by this bus type");
1092 if (type
== IF_VIRTIO
) {
1094 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1096 if (arch_type
== QEMU_ARCH_S390X
) {
1097 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1099 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1101 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1104 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1108 filename
= qemu_opt_get(legacy_opts
, "file");
1110 /* Check werror/rerror compatibility with if=... */
1111 werror
= qemu_opt_get(legacy_opts
, "werror");
1112 if (werror
!= NULL
) {
1113 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1115 error_report("werror is not supported by this bus type");
1118 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1121 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1122 if (rerror
!= NULL
) {
1123 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1125 error_report("rerror is not supported by this bus type");
1128 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1131 /* Actual block device init: Functionality shared with blockdev-add */
1132 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1136 error_report_err(local_err
);
1143 /* Create legacy DriveInfo */
1144 dinfo
= g_malloc0(sizeof(*dinfo
));
1145 dinfo
->opts
= all_opts
;
1148 dinfo
->heads
= heads
;
1150 dinfo
->trans
= translation
;
1153 dinfo
->bus
= bus_id
;
1154 dinfo
->unit
= unit_id
;
1155 dinfo
->devaddr
= devaddr
;
1156 dinfo
->serial
= g_strdup(serial
);
1158 blk_set_legacy_dinfo(blk
, dinfo
);
1165 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1172 qemu_opts_del(legacy_opts
);
1177 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1179 BlockDriverState
*bs
;
1181 bs
= bdrv_lookup_bs(name
, name
, errp
);
1186 if (!bdrv_is_root_node(bs
)) {
1187 error_setg(errp
, "Need a root block node");
1191 if (!bdrv_is_inserted(bs
)) {
1192 error_setg(errp
, "Device has no medium");
1199 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1201 const char *device
= qdict_get_str(qdict
, "device");
1205 if (!strcmp(device
, "all")) {
1206 ret
= blk_commit_all();
1208 BlockDriverState
*bs
;
1209 AioContext
*aio_context
;
1211 blk
= blk_by_name(device
);
1213 monitor_printf(mon
, "Device '%s' not found\n", device
);
1216 if (!blk_is_available(blk
)) {
1217 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1222 aio_context
= bdrv_get_aio_context(bs
);
1223 aio_context_acquire(aio_context
);
1225 ret
= bdrv_commit(bs
);
1227 aio_context_release(aio_context
);
1230 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1235 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1237 TransactionActionList list
;
1239 list
.value
= action
;
1241 qmp_transaction(&list
, false, NULL
, errp
);
1244 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1245 bool has_node_name
, const char *node_name
,
1246 const char *snapshot_file
,
1247 bool has_snapshot_node_name
,
1248 const char *snapshot_node_name
,
1249 bool has_format
, const char *format
,
1250 bool has_mode
, NewImageMode mode
, Error
**errp
)
1252 BlockdevSnapshotSync snapshot
= {
1253 .has_device
= has_device
,
1254 .device
= (char *) device
,
1255 .has_node_name
= has_node_name
,
1256 .node_name
= (char *) node_name
,
1257 .snapshot_file
= (char *) snapshot_file
,
1258 .has_snapshot_node_name
= has_snapshot_node_name
,
1259 .snapshot_node_name
= (char *) snapshot_node_name
,
1260 .has_format
= has_format
,
1261 .format
= (char *) format
,
1262 .has_mode
= has_mode
,
1265 TransactionAction action
= {
1266 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1267 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1269 blockdev_do_action(&action
, errp
);
1272 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1275 BlockdevSnapshot snapshot_data
= {
1276 .node
= (char *) node
,
1277 .overlay
= (char *) overlay
1279 TransactionAction action
= {
1280 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1281 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1283 blockdev_do_action(&action
, errp
);
1286 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1290 BlockdevSnapshotInternal snapshot
= {
1291 .device
= (char *) device
,
1292 .name
= (char *) name
1294 TransactionAction action
= {
1295 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1296 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1298 blockdev_do_action(&action
, errp
);
1301 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1308 BlockDriverState
*bs
;
1310 AioContext
*aio_context
;
1311 QEMUSnapshotInfo sn
;
1312 Error
*local_err
= NULL
;
1313 SnapshotInfo
*info
= NULL
;
1316 blk
= blk_by_name(device
);
1318 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1319 "Device '%s' not found", device
);
1323 aio_context
= blk_get_aio_context(blk
);
1324 aio_context_acquire(aio_context
);
1335 error_setg(errp
, "Name or id must be provided");
1336 goto out_aio_context
;
1339 if (!blk_is_available(blk
)) {
1340 error_setg(errp
, "Device '%s' has no medium", device
);
1341 goto out_aio_context
;
1345 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1346 goto out_aio_context
;
1349 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1351 error_propagate(errp
, local_err
);
1352 goto out_aio_context
;
1356 "Snapshot with id '%s' and name '%s' does not exist on "
1358 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1359 goto out_aio_context
;
1362 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1364 error_propagate(errp
, local_err
);
1365 goto out_aio_context
;
1368 aio_context_release(aio_context
);
1370 info
= g_new0(SnapshotInfo
, 1);
1371 info
->id
= g_strdup(sn
.id_str
);
1372 info
->name
= g_strdup(sn
.name
);
1373 info
->date_nsec
= sn
.date_nsec
;
1374 info
->date_sec
= sn
.date_sec
;
1375 info
->vm_state_size
= sn
.vm_state_size
;
1376 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1377 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1382 aio_context_release(aio_context
);
1387 * block_dirty_bitmap_lookup:
1388 * Return a dirty bitmap (if present), after validating
1389 * the node reference and bitmap names.
1391 * @node: The name of the BDS node to search for bitmaps
1392 * @name: The name of the bitmap to search for
1393 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1394 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1395 * @errp: Output pointer for error information. Can be NULL.
1397 * @return: A bitmap object on success, or NULL on failure.
1399 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1401 BlockDriverState
**pbs
,
1405 BlockDriverState
*bs
;
1406 BdrvDirtyBitmap
*bitmap
;
1407 AioContext
*aio_context
;
1410 error_setg(errp
, "Node cannot be NULL");
1414 error_setg(errp
, "Bitmap name cannot be NULL");
1417 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1419 error_setg(errp
, "Node '%s' not found", node
);
1423 aio_context
= bdrv_get_aio_context(bs
);
1424 aio_context_acquire(aio_context
);
1426 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1428 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1436 *paio
= aio_context
;
1438 aio_context_release(aio_context
);
1444 aio_context_release(aio_context
);
1448 /* New and old BlockDriverState structs for atomic group operations */
1450 typedef struct BlkActionState BlkActionState
;
1454 * Table of operations that define an Action.
1456 * @instance_size: Size of state struct, in bytes.
1457 * @prepare: Prepare the work, must NOT be NULL.
1458 * @commit: Commit the changes, can be NULL.
1459 * @abort: Abort the changes on fail, can be NULL.
1460 * @clean: Clean up resources after all transaction actions have called
1461 * commit() or abort(). Can be NULL.
1463 * Only prepare() may fail. In a single transaction, only one of commit() or
1464 * abort() will be called. clean() will always be called if it is present.
1466 typedef struct BlkActionOps
{
1467 size_t instance_size
;
1468 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1469 void (*commit
)(BlkActionState
*common
);
1470 void (*abort
)(BlkActionState
*common
);
1471 void (*clean
)(BlkActionState
*common
);
1476 * Describes one Action's state within a Transaction.
1478 * @action: QAPI-defined enum identifying which Action to perform.
1479 * @ops: Table of ActionOps this Action can perform.
1480 * @block_job_txn: Transaction which this action belongs to.
1481 * @entry: List membership for all Actions in this Transaction.
1483 * This structure must be arranged as first member in a subclassed type,
1484 * assuming that the compiler will also arrange it to the same offsets as the
1487 struct BlkActionState
{
1488 TransactionAction
*action
;
1489 const BlkActionOps
*ops
;
1490 BlockJobTxn
*block_job_txn
;
1491 TransactionProperties
*txn_props
;
1492 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1495 /* internal snapshot private data */
1496 typedef struct InternalSnapshotState
{
1497 BlkActionState common
;
1498 BlockDriverState
*bs
;
1499 AioContext
*aio_context
;
1500 QEMUSnapshotInfo sn
;
1502 } InternalSnapshotState
;
1505 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1507 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1509 "Action '%s' does not support Transaction property "
1510 "completion-mode = %s",
1511 TransactionActionKind_lookup
[s
->action
->type
],
1512 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1518 static void internal_snapshot_prepare(BlkActionState
*common
,
1521 Error
*local_err
= NULL
;
1525 BlockDriverState
*bs
;
1526 QEMUSnapshotInfo old_sn
, *sn
;
1529 BlockdevSnapshotInternal
*internal
;
1530 InternalSnapshotState
*state
;
1533 g_assert(common
->action
->type
==
1534 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1535 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1536 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1538 /* 1. parse input */
1539 device
= internal
->device
;
1540 name
= internal
->name
;
1542 /* 2. check for validation */
1543 if (action_check_completion_mode(common
, errp
) < 0) {
1547 blk
= blk_by_name(device
);
1549 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1550 "Device '%s' not found", device
);
1554 /* AioContext is released in .clean() */
1555 state
->aio_context
= blk_get_aio_context(blk
);
1556 aio_context_acquire(state
->aio_context
);
1558 if (!blk_is_available(blk
)) {
1559 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1565 bdrv_drained_begin(bs
);
1567 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1571 if (bdrv_is_read_only(bs
)) {
1572 error_setg(errp
, "Device '%s' is read only", device
);
1576 if (!bdrv_can_snapshot(bs
)) {
1577 error_setg(errp
, "Block format '%s' used by device '%s' "
1578 "does not support internal snapshots",
1579 bs
->drv
->format_name
, device
);
1583 if (!strlen(name
)) {
1584 error_setg(errp
, "Name is empty");
1588 /* check whether a snapshot with name exist */
1589 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1592 error_propagate(errp
, local_err
);
1596 "Snapshot with name '%s' already exists on device '%s'",
1601 /* 3. take the snapshot */
1603 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1604 qemu_gettimeofday(&tv
);
1605 sn
->date_sec
= tv
.tv_sec
;
1606 sn
->date_nsec
= tv
.tv_usec
* 1000;
1607 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1609 ret1
= bdrv_snapshot_create(bs
, sn
);
1611 error_setg_errno(errp
, -ret1
,
1612 "Failed to create snapshot '%s' on device '%s'",
1617 /* 4. succeed, mark a snapshot is created */
1618 state
->created
= true;
1621 static void internal_snapshot_abort(BlkActionState
*common
)
1623 InternalSnapshotState
*state
=
1624 DO_UPCAST(InternalSnapshotState
, common
, common
);
1625 BlockDriverState
*bs
= state
->bs
;
1626 QEMUSnapshotInfo
*sn
= &state
->sn
;
1627 Error
*local_error
= NULL
;
1629 if (!state
->created
) {
1633 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1634 error_reportf_err(local_error
,
1635 "Failed to delete snapshot with id '%s' and "
1636 "name '%s' on device '%s' in abort: ",
1637 sn
->id_str
, sn
->name
,
1638 bdrv_get_device_name(bs
));
1642 static void internal_snapshot_clean(BlkActionState
*common
)
1644 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1647 if (state
->aio_context
) {
1649 bdrv_drained_end(state
->bs
);
1651 aio_context_release(state
->aio_context
);
1655 /* external snapshot private data */
1656 typedef struct ExternalSnapshotState
{
1657 BlkActionState common
;
1658 BlockDriverState
*old_bs
;
1659 BlockDriverState
*new_bs
;
1660 AioContext
*aio_context
;
1661 } ExternalSnapshotState
;
1663 static void external_snapshot_prepare(BlkActionState
*common
,
1667 QDict
*options
= NULL
;
1668 Error
*local_err
= NULL
;
1669 /* Device and node name of the image to generate the snapshot from */
1671 const char *node_name
;
1672 /* Reference to the new image (for 'blockdev-snapshot') */
1673 const char *snapshot_ref
;
1674 /* File name of the new image (for 'blockdev-snapshot-sync') */
1675 const char *new_image_file
;
1676 ExternalSnapshotState
*state
=
1677 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1678 TransactionAction
*action
= common
->action
;
1680 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1681 * purpose but a different set of parameters */
1682 switch (action
->type
) {
1683 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1685 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1687 node_name
= s
->node
;
1688 new_image_file
= NULL
;
1689 snapshot_ref
= s
->overlay
;
1692 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1694 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1695 device
= s
->has_device
? s
->device
: NULL
;
1696 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1697 new_image_file
= s
->snapshot_file
;
1698 snapshot_ref
= NULL
;
1702 g_assert_not_reached();
1705 /* start processing */
1706 if (action_check_completion_mode(common
, errp
) < 0) {
1710 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1711 if (!state
->old_bs
) {
1715 /* Acquire AioContext now so any threads operating on old_bs stop */
1716 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1717 aio_context_acquire(state
->aio_context
);
1718 bdrv_drained_begin(state
->old_bs
);
1720 if (!bdrv_is_inserted(state
->old_bs
)) {
1721 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1725 if (bdrv_op_is_blocked(state
->old_bs
,
1726 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1730 if (!bdrv_is_read_only(state
->old_bs
)) {
1731 if (bdrv_flush(state
->old_bs
)) {
1732 error_setg(errp
, QERR_IO_ERROR
);
1737 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1738 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1742 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1743 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1744 const char *format
= s
->has_format
? s
->format
: "qcow2";
1745 enum NewImageMode mode
;
1746 const char *snapshot_node_name
=
1747 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1749 if (node_name
&& !snapshot_node_name
) {
1750 error_setg(errp
, "New snapshot node name missing");
1754 if (snapshot_node_name
&&
1755 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1756 error_setg(errp
, "New snapshot node name already in use");
1760 flags
= state
->old_bs
->open_flags
;
1761 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
| BDRV_O_COPY_ON_READ
);
1763 /* create new image w/backing file */
1764 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1765 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1766 int64_t size
= bdrv_getlength(state
->old_bs
);
1768 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1771 bdrv_img_create(new_image_file
, format
,
1772 state
->old_bs
->filename
,
1773 state
->old_bs
->drv
->format_name
,
1774 NULL
, size
, flags
, &local_err
, false);
1776 error_propagate(errp
, local_err
);
1781 options
= qdict_new();
1782 if (s
->has_snapshot_node_name
) {
1783 qdict_put(options
, "node-name",
1784 qstring_from_str(snapshot_node_name
));
1786 qdict_put(options
, "driver", qstring_from_str(format
));
1788 flags
|= BDRV_O_NO_BACKING
;
1791 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1793 /* We will manually add the backing_hd field to the bs later */
1794 if (!state
->new_bs
) {
1798 if (bdrv_has_blk(state
->new_bs
)) {
1799 error_setg(errp
, "The snapshot is already in use by %s",
1800 bdrv_get_parent_name(state
->new_bs
));
1804 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1809 if (state
->new_bs
->backing
!= NULL
) {
1810 error_setg(errp
, "The snapshot already has a backing image");
1814 if (!state
->new_bs
->drv
->supports_backing
) {
1815 error_setg(errp
, "The snapshot does not support backing images");
1819 static void external_snapshot_commit(BlkActionState
*common
)
1821 ExternalSnapshotState
*state
=
1822 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1824 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1826 /* This removes our old bs and adds the new bs */
1827 bdrv_append(state
->new_bs
, state
->old_bs
);
1828 /* We don't need (or want) to use the transactional
1829 * bdrv_reopen_multiple() across all the entries at once, because we
1830 * don't want to abort all of them if one of them fails the reopen */
1831 if (!state
->old_bs
->copy_on_read
) {
1832 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1837 static void external_snapshot_abort(BlkActionState
*common
)
1839 ExternalSnapshotState
*state
=
1840 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1841 if (state
->new_bs
) {
1842 bdrv_unref(state
->new_bs
);
1846 static void external_snapshot_clean(BlkActionState
*common
)
1848 ExternalSnapshotState
*state
=
1849 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1850 if (state
->aio_context
) {
1851 bdrv_drained_end(state
->old_bs
);
1852 aio_context_release(state
->aio_context
);
1856 typedef struct DriveBackupState
{
1857 BlkActionState common
;
1858 BlockDriverState
*bs
;
1859 AioContext
*aio_context
;
1863 static void do_drive_backup(const char *job_id
, const char *device
,
1864 const char *target
, bool has_format
,
1865 const char *format
, enum MirrorSyncMode sync
,
1866 bool has_mode
, enum NewImageMode mode
,
1867 bool has_speed
, int64_t speed
,
1868 bool has_bitmap
, const char *bitmap
,
1869 bool has_on_source_error
,
1870 BlockdevOnError on_source_error
,
1871 bool has_on_target_error
,
1872 BlockdevOnError on_target_error
,
1873 BlockJobTxn
*txn
, Error
**errp
);
1875 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1877 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1879 DriveBackup
*backup
;
1880 Error
*local_err
= NULL
;
1882 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1883 backup
= common
->action
->u
.drive_backup
.data
;
1885 blk
= blk_by_name(backup
->device
);
1887 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1888 "Device '%s' not found", backup
->device
);
1892 if (!blk_is_available(blk
)) {
1893 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1897 /* AioContext is released in .clean() */
1898 state
->aio_context
= blk_get_aio_context(blk
);
1899 aio_context_acquire(state
->aio_context
);
1900 bdrv_drained_begin(blk_bs(blk
));
1901 state
->bs
= blk_bs(blk
);
1903 do_drive_backup(backup
->has_job_id
? backup
->job_id
: NULL
,
1904 backup
->device
, backup
->target
,
1905 backup
->has_format
, backup
->format
,
1907 backup
->has_mode
, backup
->mode
,
1908 backup
->has_speed
, backup
->speed
,
1909 backup
->has_bitmap
, backup
->bitmap
,
1910 backup
->has_on_source_error
, backup
->on_source_error
,
1911 backup
->has_on_target_error
, backup
->on_target_error
,
1912 common
->block_job_txn
, &local_err
);
1914 error_propagate(errp
, local_err
);
1918 state
->job
= state
->bs
->job
;
1921 static void drive_backup_abort(BlkActionState
*common
)
1923 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1924 BlockDriverState
*bs
= state
->bs
;
1926 /* Only cancel if it's the job we started */
1927 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1928 block_job_cancel_sync(bs
->job
);
1932 static void drive_backup_clean(BlkActionState
*common
)
1934 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1936 if (state
->aio_context
) {
1937 bdrv_drained_end(state
->bs
);
1938 aio_context_release(state
->aio_context
);
1942 typedef struct BlockdevBackupState
{
1943 BlkActionState common
;
1944 BlockDriverState
*bs
;
1946 AioContext
*aio_context
;
1947 } BlockdevBackupState
;
1949 static void do_blockdev_backup(const char *job_id
, const char *device
,
1950 const char *target
, enum MirrorSyncMode sync
,
1951 bool has_speed
, int64_t speed
,
1952 bool has_on_source_error
,
1953 BlockdevOnError on_source_error
,
1954 bool has_on_target_error
,
1955 BlockdevOnError on_target_error
,
1956 BlockJobTxn
*txn
, Error
**errp
);
1958 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1960 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1961 BlockdevBackup
*backup
;
1963 BlockDriverState
*target
;
1964 Error
*local_err
= NULL
;
1966 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1967 backup
= common
->action
->u
.blockdev_backup
.data
;
1969 blk
= blk_by_name(backup
->device
);
1971 error_setg(errp
, "Device '%s' not found", backup
->device
);
1975 if (!blk_is_available(blk
)) {
1976 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1980 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1985 /* AioContext is released in .clean() */
1986 state
->aio_context
= blk_get_aio_context(blk
);
1987 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1988 state
->aio_context
= NULL
;
1989 error_setg(errp
, "Backup between two IO threads is not implemented");
1992 aio_context_acquire(state
->aio_context
);
1993 state
->bs
= blk_bs(blk
);
1994 bdrv_drained_begin(state
->bs
);
1996 do_blockdev_backup(backup
->has_job_id
? backup
->job_id
: NULL
,
1997 backup
->device
, backup
->target
, backup
->sync
,
1998 backup
->has_speed
, backup
->speed
,
1999 backup
->has_on_source_error
, backup
->on_source_error
,
2000 backup
->has_on_target_error
, backup
->on_target_error
,
2001 common
->block_job_txn
, &local_err
);
2003 error_propagate(errp
, local_err
);
2007 state
->job
= state
->bs
->job
;
2010 static void blockdev_backup_abort(BlkActionState
*common
)
2012 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2013 BlockDriverState
*bs
= state
->bs
;
2015 /* Only cancel if it's the job we started */
2016 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
2017 block_job_cancel_sync(bs
->job
);
2021 static void blockdev_backup_clean(BlkActionState
*common
)
2023 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2025 if (state
->aio_context
) {
2026 bdrv_drained_end(state
->bs
);
2027 aio_context_release(state
->aio_context
);
2031 typedef struct BlockDirtyBitmapState
{
2032 BlkActionState common
;
2033 BdrvDirtyBitmap
*bitmap
;
2034 BlockDriverState
*bs
;
2035 AioContext
*aio_context
;
2038 } BlockDirtyBitmapState
;
2040 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2043 Error
*local_err
= NULL
;
2044 BlockDirtyBitmapAdd
*action
;
2045 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2048 if (action_check_completion_mode(common
, errp
) < 0) {
2052 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2053 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2054 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2055 action
->has_granularity
, action
->granularity
,
2059 state
->prepared
= true;
2061 error_propagate(errp
, local_err
);
2065 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2067 BlockDirtyBitmapAdd
*action
;
2068 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2071 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2072 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2073 * then the node reference and bitmap name must have been valid.
2075 if (state
->prepared
) {
2076 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2080 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2083 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2085 BlockDirtyBitmap
*action
;
2087 if (action_check_completion_mode(common
, errp
) < 0) {
2091 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2092 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2095 &state
->aio_context
,
2097 if (!state
->bitmap
) {
2101 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2102 error_setg(errp
, "Cannot modify a frozen bitmap");
2104 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2105 error_setg(errp
, "Cannot clear a disabled bitmap");
2109 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2110 /* AioContext is released in .clean() */
2113 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2115 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2118 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2121 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2123 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2126 hbitmap_free(state
->backup
);
2129 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2131 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2134 if (state
->aio_context
) {
2135 aio_context_release(state
->aio_context
);
2139 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2141 error_setg(errp
, "Transaction aborted using Abort action");
2144 static void abort_commit(BlkActionState
*common
)
2146 g_assert_not_reached(); /* this action never succeeds */
2149 static const BlkActionOps actions
[] = {
2150 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2151 .instance_size
= sizeof(ExternalSnapshotState
),
2152 .prepare
= external_snapshot_prepare
,
2153 .commit
= external_snapshot_commit
,
2154 .abort
= external_snapshot_abort
,
2155 .clean
= external_snapshot_clean
,
2157 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2158 .instance_size
= sizeof(ExternalSnapshotState
),
2159 .prepare
= external_snapshot_prepare
,
2160 .commit
= external_snapshot_commit
,
2161 .abort
= external_snapshot_abort
,
2162 .clean
= external_snapshot_clean
,
2164 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2165 .instance_size
= sizeof(DriveBackupState
),
2166 .prepare
= drive_backup_prepare
,
2167 .abort
= drive_backup_abort
,
2168 .clean
= drive_backup_clean
,
2170 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2171 .instance_size
= sizeof(BlockdevBackupState
),
2172 .prepare
= blockdev_backup_prepare
,
2173 .abort
= blockdev_backup_abort
,
2174 .clean
= blockdev_backup_clean
,
2176 [TRANSACTION_ACTION_KIND_ABORT
] = {
2177 .instance_size
= sizeof(BlkActionState
),
2178 .prepare
= abort_prepare
,
2179 .commit
= abort_commit
,
2181 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2182 .instance_size
= sizeof(InternalSnapshotState
),
2183 .prepare
= internal_snapshot_prepare
,
2184 .abort
= internal_snapshot_abort
,
2185 .clean
= internal_snapshot_clean
,
2187 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2188 .instance_size
= sizeof(BlockDirtyBitmapState
),
2189 .prepare
= block_dirty_bitmap_add_prepare
,
2190 .abort
= block_dirty_bitmap_add_abort
,
2192 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2193 .instance_size
= sizeof(BlockDirtyBitmapState
),
2194 .prepare
= block_dirty_bitmap_clear_prepare
,
2195 .commit
= block_dirty_bitmap_clear_commit
,
2196 .abort
= block_dirty_bitmap_clear_abort
,
2197 .clean
= block_dirty_bitmap_clear_clean
,
2202 * Allocate a TransactionProperties structure if necessary, and fill
2203 * that structure with desired defaults if they are unset.
2205 static TransactionProperties
*get_transaction_properties(
2206 TransactionProperties
*props
)
2209 props
= g_new0(TransactionProperties
, 1);
2212 if (!props
->has_completion_mode
) {
2213 props
->has_completion_mode
= true;
2214 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2221 * 'Atomic' group operations. The operations are performed as a set, and if
2222 * any fail then we roll back all operations in the group.
2224 void qmp_transaction(TransactionActionList
*dev_list
,
2226 struct TransactionProperties
*props
,
2229 TransactionActionList
*dev_entry
= dev_list
;
2230 BlockJobTxn
*block_job_txn
= NULL
;
2231 BlkActionState
*state
, *next
;
2232 Error
*local_err
= NULL
;
2234 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2235 QSIMPLEQ_INIT(&snap_bdrv_states
);
2237 /* Does this transaction get canceled as a group on failure?
2238 * If not, we don't really need to make a BlockJobTxn.
2240 props
= get_transaction_properties(props
);
2241 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2242 block_job_txn
= block_job_txn_new();
2245 /* drain all i/o before any operations */
2248 /* We don't do anything in this loop that commits us to the operations */
2249 while (NULL
!= dev_entry
) {
2250 TransactionAction
*dev_info
= NULL
;
2251 const BlkActionOps
*ops
;
2253 dev_info
= dev_entry
->value
;
2254 dev_entry
= dev_entry
->next
;
2256 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2258 ops
= &actions
[dev_info
->type
];
2259 assert(ops
->instance_size
> 0);
2261 state
= g_malloc0(ops
->instance_size
);
2263 state
->action
= dev_info
;
2264 state
->block_job_txn
= block_job_txn
;
2265 state
->txn_props
= props
;
2266 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2268 state
->ops
->prepare(state
, &local_err
);
2270 error_propagate(errp
, local_err
);
2271 goto delete_and_fail
;
2275 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2276 if (state
->ops
->commit
) {
2277 state
->ops
->commit(state
);
2285 /* failure, and it is all-or-none; roll back all operations */
2286 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2287 if (state
->ops
->abort
) {
2288 state
->ops
->abort(state
);
2292 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2293 if (state
->ops
->clean
) {
2294 state
->ops
->clean(state
);
2299 qapi_free_TransactionProperties(props
);
2301 block_job_txn_unref(block_job_txn
);
2304 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2306 Error
*local_err
= NULL
;
2313 rc
= do_open_tray(device
, force
, &local_err
);
2314 if (rc
&& rc
!= -ENOSYS
) {
2315 error_propagate(errp
, local_err
);
2318 error_free(local_err
);
2320 qmp_x_blockdev_remove_medium(device
, errp
);
2323 void qmp_block_passwd(bool has_device
, const char *device
,
2324 bool has_node_name
, const char *node_name
,
2325 const char *password
, Error
**errp
)
2327 Error
*local_err
= NULL
;
2328 BlockDriverState
*bs
;
2329 AioContext
*aio_context
;
2331 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2332 has_node_name
? node_name
: NULL
,
2335 error_propagate(errp
, local_err
);
2339 aio_context
= bdrv_get_aio_context(bs
);
2340 aio_context_acquire(aio_context
);
2342 bdrv_add_key(bs
, password
, errp
);
2344 aio_context_release(aio_context
);
2348 * Attempt to open the tray of @device.
2349 * If @force, ignore its tray lock.
2350 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2351 * On error, store an error through @errp and return -errno.
2352 * If @device does not exist, return -ENODEV.
2353 * If it has no removable media, return -ENOTSUP.
2354 * If it has no tray, return -ENOSYS.
2355 * If the guest was asked to open the tray, return -EINPROGRESS.
2358 static int do_open_tray(const char *device
, bool force
, Error
**errp
)
2363 blk
= blk_by_name(device
);
2365 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2366 "Device '%s' not found", device
);
2370 if (!blk_dev_has_removable_media(blk
)) {
2371 error_setg(errp
, "Device '%s' is not removable", device
);
2375 if (!blk_dev_has_tray(blk
)) {
2376 error_setg(errp
, "Device '%s' does not have a tray", device
);
2380 if (blk_dev_is_tray_open(blk
)) {
2384 locked
= blk_dev_is_medium_locked(blk
);
2386 blk_dev_eject_request(blk
, force
);
2389 if (!locked
|| force
) {
2390 blk_dev_change_media_cb(blk
, false);
2393 if (locked
&& !force
) {
2394 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2395 "wait for tray to open and try again", device
);
2396 return -EINPROGRESS
;
2402 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2405 Error
*local_err
= NULL
;
2411 rc
= do_open_tray(device
, force
, &local_err
);
2412 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2413 error_propagate(errp
, local_err
);
2416 error_free(local_err
);
2419 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2423 blk
= blk_by_name(device
);
2425 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2426 "Device '%s' not found", device
);
2430 if (!blk_dev_has_removable_media(blk
)) {
2431 error_setg(errp
, "Device '%s' is not removable", device
);
2435 if (!blk_dev_has_tray(blk
)) {
2436 /* Ignore this command on tray-less devices */
2440 if (!blk_dev_is_tray_open(blk
)) {
2444 blk_dev_change_media_cb(blk
, true);
2447 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2450 BlockDriverState
*bs
;
2451 AioContext
*aio_context
;
2454 blk
= blk_by_name(device
);
2456 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2457 "Device '%s' not found", device
);
2461 /* For BBs without a device, we can exchange the BDS tree at will */
2462 has_device
= blk_get_attached_dev(blk
);
2464 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2465 error_setg(errp
, "Device '%s' is not removable", device
);
2469 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2470 error_setg(errp
, "Tray of device '%s' is not open", device
);
2479 aio_context
= bdrv_get_aio_context(bs
);
2480 aio_context_acquire(aio_context
);
2482 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2488 if (!blk_dev_has_tray(blk
)) {
2489 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2490 * called at all); therefore, the medium needs to be ejected here.
2491 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2492 * value passed here (i.e. false). */
2493 blk_dev_change_media_cb(blk
, false);
2497 aio_context_release(aio_context
);
2500 static void qmp_blockdev_insert_anon_medium(const char *device
,
2501 BlockDriverState
*bs
, Error
**errp
)
2506 blk
= blk_by_name(device
);
2508 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2509 "Device '%s' not found", device
);
2513 /* For BBs without a device, we can exchange the BDS tree at will */
2514 has_device
= blk_get_attached_dev(blk
);
2516 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2517 error_setg(errp
, "Device '%s' is not removable", device
);
2521 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2522 error_setg(errp
, "Tray of device '%s' is not open", device
);
2527 error_setg(errp
, "There already is a medium in device '%s'", device
);
2531 blk_insert_bs(blk
, bs
);
2533 if (!blk_dev_has_tray(blk
)) {
2534 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2535 * called at all); therefore, the medium needs to be pushed into the
2537 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2538 * value passed here (i.e. true). */
2539 blk_dev_change_media_cb(blk
, true);
2543 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2546 BlockDriverState
*bs
;
2548 bs
= bdrv_find_node(node_name
);
2550 error_setg(errp
, "Node '%s' not found", node_name
);
2554 if (bdrv_has_blk(bs
)) {
2555 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2556 bdrv_get_parent_name(bs
));
2560 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2563 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2564 bool has_format
, const char *format
,
2566 BlockdevChangeReadOnlyMode read_only
,
2570 BlockDriverState
*medium_bs
= NULL
;
2573 QDict
*options
= NULL
;
2576 blk
= blk_by_name(device
);
2578 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2579 "Device '%s' not found", device
);
2584 blk_update_root_state(blk
);
2587 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2588 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2591 if (!has_read_only
) {
2592 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2595 switch (read_only
) {
2596 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2599 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2600 bdrv_flags
&= ~BDRV_O_RDWR
;
2603 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2604 bdrv_flags
|= BDRV_O_RDWR
;
2612 options
= qdict_new();
2613 qdict_put(options
, "driver", qstring_from_str(format
));
2616 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2621 bdrv_add_key(medium_bs
, NULL
, &err
);
2623 error_propagate(errp
, err
);
2627 rc
= do_open_tray(device
, false, &err
);
2628 if (rc
&& rc
!= -ENOSYS
) {
2629 error_propagate(errp
, err
);
2635 qmp_x_blockdev_remove_medium(device
, &err
);
2637 error_propagate(errp
, err
);
2641 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2643 error_propagate(errp
, err
);
2647 blk_apply_root_state(blk
, medium_bs
);
2649 qmp_blockdev_close_tray(device
, errp
);
2652 /* If the medium has been inserted, the device has its own reference, so
2653 * ours must be relinquished; and if it has not been inserted successfully,
2654 * the reference must be relinquished anyway */
2655 bdrv_unref(medium_bs
);
2658 /* throttling disk I/O limits */
2659 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2662 BlockDriverState
*bs
;
2664 AioContext
*aio_context
;
2666 blk
= blk_by_name(arg
->device
);
2668 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2669 "Device '%s' not found", arg
->device
);
2673 aio_context
= blk_get_aio_context(blk
);
2674 aio_context_acquire(aio_context
);
2678 error_setg(errp
, "Device '%s' has no medium", arg
->device
);
2682 throttle_config_init(&cfg
);
2683 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2684 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2685 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2687 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2688 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2689 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2691 if (arg
->has_bps_max
) {
2692 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2694 if (arg
->has_bps_rd_max
) {
2695 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2697 if (arg
->has_bps_wr_max
) {
2698 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2700 if (arg
->has_iops_max
) {
2701 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2703 if (arg
->has_iops_rd_max
) {
2704 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2706 if (arg
->has_iops_wr_max
) {
2707 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2710 if (arg
->has_bps_max_length
) {
2711 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2713 if (arg
->has_bps_rd_max_length
) {
2714 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2716 if (arg
->has_bps_wr_max_length
) {
2717 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2719 if (arg
->has_iops_max_length
) {
2720 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2722 if (arg
->has_iops_rd_max_length
) {
2723 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2725 if (arg
->has_iops_wr_max_length
) {
2726 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2729 if (arg
->has_iops_size
) {
2730 cfg
.op_size
= arg
->iops_size
;
2733 if (!throttle_is_valid(&cfg
, errp
)) {
2737 if (throttle_enabled(&cfg
)) {
2738 /* Enable I/O limits if they're not enabled yet, otherwise
2739 * just update the throttling group. */
2740 if (!blk_get_public(blk
)->throttle_state
) {
2741 blk_io_limits_enable(blk
,
2742 arg
->has_group
? arg
->group
: arg
->device
);
2743 } else if (arg
->has_group
) {
2744 blk_io_limits_update_group(blk
, arg
->group
);
2746 /* Set the new throttling configuration */
2747 blk_set_io_limits(blk
, &cfg
);
2748 } else if (blk_get_public(blk
)->throttle_state
) {
2749 /* If all throttling settings are set to 0, disable I/O limits */
2750 blk_io_limits_disable(blk
);
2754 aio_context_release(aio_context
);
2757 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2758 bool has_granularity
, uint32_t granularity
,
2761 AioContext
*aio_context
;
2762 BlockDriverState
*bs
;
2764 if (!name
|| name
[0] == '\0') {
2765 error_setg(errp
, "Bitmap name cannot be empty");
2769 bs
= bdrv_lookup_bs(node
, node
, errp
);
2774 aio_context
= bdrv_get_aio_context(bs
);
2775 aio_context_acquire(aio_context
);
2777 if (has_granularity
) {
2778 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2779 error_setg(errp
, "Granularity must be power of 2 "
2780 "and at least 512");
2784 /* Default to cluster size, if available: */
2785 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2788 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2791 aio_context_release(aio_context
);
2794 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2797 AioContext
*aio_context
;
2798 BlockDriverState
*bs
;
2799 BdrvDirtyBitmap
*bitmap
;
2801 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2802 if (!bitmap
|| !bs
) {
2806 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2808 "Bitmap '%s' is currently frozen and cannot be removed",
2812 bdrv_dirty_bitmap_make_anon(bitmap
);
2813 bdrv_release_dirty_bitmap(bs
, bitmap
);
2816 aio_context_release(aio_context
);
2820 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2821 * immediately after a full backup operation.
2823 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2826 AioContext
*aio_context
;
2827 BdrvDirtyBitmap
*bitmap
;
2828 BlockDriverState
*bs
;
2830 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2831 if (!bitmap
|| !bs
) {
2835 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2837 "Bitmap '%s' is currently frozen and cannot be modified",
2840 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2842 "Bitmap '%s' is currently disabled and cannot be cleared",
2847 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2850 aio_context_release(aio_context
);
2853 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2855 const char *id
= qdict_get_str(qdict
, "id");
2857 BlockDriverState
*bs
;
2858 AioContext
*aio_context
;
2859 Error
*local_err
= NULL
;
2861 bs
= bdrv_find_node(id
);
2863 qmp_x_blockdev_del(false, NULL
, true, id
, &local_err
);
2865 error_report_err(local_err
);
2870 blk
= blk_by_name(id
);
2872 error_report("Device '%s' not found", id
);
2876 if (!blk_legacy_dinfo(blk
)) {
2877 error_report("Deleting device added with blockdev-add"
2878 " is not supported");
2882 aio_context
= blk_get_aio_context(blk
);
2883 aio_context_acquire(aio_context
);
2887 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2888 error_report_err(local_err
);
2889 aio_context_release(aio_context
);
2896 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2897 monitor_remove_blk(blk
);
2899 /* If this BlockBackend has a device attached to it, its refcount will be
2900 * decremented when the device is removed; otherwise we have to do so here.
2902 if (blk_get_attached_dev(blk
)) {
2903 /* Further I/O must not pause the guest */
2904 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2905 BLOCKDEV_ON_ERROR_REPORT
);
2910 aio_context_release(aio_context
);
2913 void qmp_block_resize(bool has_device
, const char *device
,
2914 bool has_node_name
, const char *node_name
,
2915 int64_t size
, Error
**errp
)
2917 Error
*local_err
= NULL
;
2918 BlockDriverState
*bs
;
2919 AioContext
*aio_context
;
2922 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2923 has_node_name
? node_name
: NULL
,
2926 error_propagate(errp
, local_err
);
2930 aio_context
= bdrv_get_aio_context(bs
);
2931 aio_context_acquire(aio_context
);
2933 if (!bdrv_is_first_non_filter(bs
)) {
2934 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2939 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2943 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2944 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2948 /* complete all in-flight operations before resizing the device */
2951 ret
= bdrv_truncate(bs
, size
);
2956 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2959 error_setg(errp
, QERR_UNSUPPORTED
);
2962 error_setg(errp
, "Device '%s' is read only", device
);
2965 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2968 error_setg_errno(errp
, -ret
, "Could not resize");
2973 aio_context_release(aio_context
);
2976 static void block_job_cb(void *opaque
, int ret
)
2978 /* Note that this function may be executed from another AioContext besides
2979 * the QEMU main loop. If you need to access anything that assumes the
2980 * QEMU global mutex, use a BH or introduce a mutex.
2983 BlockDriverState
*bs
= opaque
;
2984 const char *msg
= NULL
;
2986 trace_block_job_cb(bs
, bs
->job
, ret
);
2991 msg
= strerror(-ret
);
2994 if (block_job_is_cancelled(bs
->job
)) {
2995 block_job_event_cancelled(bs
->job
);
2997 block_job_event_completed(bs
->job
, msg
);
3001 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3002 bool has_base
, const char *base
,
3003 bool has_backing_file
, const char *backing_file
,
3004 bool has_speed
, int64_t speed
,
3005 bool has_on_error
, BlockdevOnError on_error
,
3008 BlockDriverState
*bs
;
3009 BlockDriverState
*base_bs
= NULL
;
3010 AioContext
*aio_context
;
3011 Error
*local_err
= NULL
;
3012 const char *base_name
= NULL
;
3014 if (!has_on_error
) {
3015 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3018 bs
= qmp_get_root_bs(device
, errp
);
3023 aio_context
= bdrv_get_aio_context(bs
);
3024 aio_context_acquire(aio_context
);
3026 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3031 base_bs
= bdrv_find_backing_image(bs
, base
);
3032 if (base_bs
== NULL
) {
3033 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3036 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3040 /* if we are streaming the entire chain, the result will have no backing
3041 * file, and specifying one is therefore an error */
3042 if (base_bs
== NULL
&& has_backing_file
) {
3043 error_setg(errp
, "backing file specified, but streaming the "
3048 /* backing_file string overrides base bs filename */
3049 base_name
= has_backing_file
? backing_file
: base_name
;
3051 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3052 has_speed
? speed
: 0, on_error
, block_job_cb
, bs
, &local_err
);
3054 error_propagate(errp
, local_err
);
3058 trace_qmp_block_stream(bs
, bs
->job
);
3061 aio_context_release(aio_context
);
3064 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3065 bool has_base
, const char *base
,
3066 bool has_top
, const char *top
,
3067 bool has_backing_file
, const char *backing_file
,
3068 bool has_speed
, int64_t speed
,
3071 BlockDriverState
*bs
;
3072 BlockDriverState
*base_bs
, *top_bs
;
3073 AioContext
*aio_context
;
3074 Error
*local_err
= NULL
;
3075 /* This will be part of the QMP command, if/when the
3076 * BlockdevOnError change for blkmirror makes it in
3078 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3085 * libvirt relies on the DeviceNotFound error class in order to probe for
3086 * live commit feature versions; for this to work, we must make sure to
3087 * perform the device lookup before any generic errors that may occur in a
3088 * scenario in which all optional arguments are omitted. */
3089 bs
= qmp_get_root_bs(device
, &local_err
);
3091 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3093 error_free(local_err
);
3094 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3095 "Device '%s' not found", device
);
3097 error_propagate(errp
, local_err
);
3102 aio_context
= bdrv_get_aio_context(bs
);
3103 aio_context_acquire(aio_context
);
3105 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3109 /* default top_bs is the active layer */
3112 if (has_top
&& top
) {
3113 if (strcmp(bs
->filename
, top
) != 0) {
3114 top_bs
= bdrv_find_backing_image(bs
, top
);
3118 if (top_bs
== NULL
) {
3119 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3123 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3125 if (has_base
&& base
) {
3126 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3128 base_bs
= bdrv_find_base(top_bs
);
3131 if (base_bs
== NULL
) {
3132 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3136 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3138 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3142 /* Do not allow attempts to commit an image into itself */
3143 if (top_bs
== base_bs
) {
3144 error_setg(errp
, "cannot commit an image into itself");
3149 if (has_backing_file
) {
3150 error_setg(errp
, "'backing-file' specified,"
3151 " but 'top' is the active layer");
3154 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, speed
,
3155 on_error
, block_job_cb
, bs
, &local_err
);
3157 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3158 on_error
, block_job_cb
, bs
,
3159 has_backing_file
? backing_file
: NULL
, &local_err
);
3161 if (local_err
!= NULL
) {
3162 error_propagate(errp
, local_err
);
3167 aio_context_release(aio_context
);
3170 static void do_drive_backup(const char *job_id
, const char *device
,
3171 const char *target
, bool has_format
,
3172 const char *format
, enum MirrorSyncMode sync
,
3173 bool has_mode
, enum NewImageMode mode
,
3174 bool has_speed
, int64_t speed
,
3175 bool has_bitmap
, const char *bitmap
,
3176 bool has_on_source_error
,
3177 BlockdevOnError on_source_error
,
3178 bool has_on_target_error
,
3179 BlockdevOnError on_target_error
,
3180 BlockJobTxn
*txn
, Error
**errp
)
3183 BlockDriverState
*bs
;
3184 BlockDriverState
*target_bs
;
3185 BlockDriverState
*source
= NULL
;
3186 BdrvDirtyBitmap
*bmap
= NULL
;
3187 AioContext
*aio_context
;
3188 QDict
*options
= NULL
;
3189 Error
*local_err
= NULL
;
3196 if (!has_on_source_error
) {
3197 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3199 if (!has_on_target_error
) {
3200 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3203 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3206 blk
= blk_by_name(device
);
3208 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3209 "Device '%s' not found", device
);
3213 aio_context
= blk_get_aio_context(blk
);
3214 aio_context_acquire(aio_context
);
3216 /* Although backup_run has this check too, we need to use bs->drv below, so
3217 * do an early check redundantly. */
3218 if (!blk_is_available(blk
)) {
3219 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3225 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3228 /* Early check to avoid creating target */
3229 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3233 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3235 /* See if we have a backing HD we can use to create our new image
3237 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3238 source
= backing_bs(bs
);
3240 sync
= MIRROR_SYNC_MODE_FULL
;
3243 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3247 size
= bdrv_getlength(bs
);
3249 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3253 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3256 bdrv_img_create(target
, format
, source
->filename
,
3257 source
->drv
->format_name
, NULL
,
3258 size
, flags
, &local_err
, false);
3260 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3261 size
, flags
, &local_err
, false);
3266 error_propagate(errp
, local_err
);
3271 options
= qdict_new();
3272 qdict_put(options
, "driver", qstring_from_str(format
));
3275 target_bs
= bdrv_open(target
, NULL
, options
, flags
, errp
);
3280 bdrv_set_aio_context(target_bs
, aio_context
);
3283 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3285 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3286 bdrv_unref(target_bs
);
3291 backup_start(job_id
, bs
, target_bs
, speed
, sync
, bmap
,
3292 on_source_error
, on_target_error
,
3293 block_job_cb
, bs
, txn
, &local_err
);
3294 bdrv_unref(target_bs
);
3295 if (local_err
!= NULL
) {
3296 error_propagate(errp
, local_err
);
3301 aio_context_release(aio_context
);
3304 void qmp_drive_backup(bool has_job_id
, const char *job_id
,
3305 const char *device
, const char *target
,
3306 bool has_format
, const char *format
,
3307 enum MirrorSyncMode sync
,
3308 bool has_mode
, enum NewImageMode mode
,
3309 bool has_speed
, int64_t speed
,
3310 bool has_bitmap
, const char *bitmap
,
3311 bool has_on_source_error
, BlockdevOnError on_source_error
,
3312 bool has_on_target_error
, BlockdevOnError on_target_error
,
3315 return do_drive_backup(has_job_id
? job_id
: NULL
, device
, target
,
3316 has_format
, format
, sync
,
3317 has_mode
, mode
, has_speed
, speed
,
3319 has_on_source_error
, on_source_error
,
3320 has_on_target_error
, on_target_error
,
3324 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3326 return bdrv_named_nodes_list(errp
);
3329 void do_blockdev_backup(const char *job_id
, const char *device
,
3330 const char *target
, enum MirrorSyncMode sync
,
3331 bool has_speed
, int64_t speed
,
3332 bool has_on_source_error
,
3333 BlockdevOnError on_source_error
,
3334 bool has_on_target_error
,
3335 BlockdevOnError on_target_error
,
3336 BlockJobTxn
*txn
, Error
**errp
)
3339 BlockDriverState
*bs
;
3340 BlockDriverState
*target_bs
;
3341 Error
*local_err
= NULL
;
3342 AioContext
*aio_context
;
3347 if (!has_on_source_error
) {
3348 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3350 if (!has_on_target_error
) {
3351 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3354 blk
= blk_by_name(device
);
3356 error_setg(errp
, "Device '%s' not found", device
);
3360 aio_context
= blk_get_aio_context(blk
);
3361 aio_context_acquire(aio_context
);
3363 if (!blk_is_available(blk
)) {
3364 error_setg(errp
, "Device '%s' has no medium", device
);
3369 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3374 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3375 if (!bdrv_has_blk(target_bs
)) {
3376 /* The target BDS is not attached, we can safely move it to another
3378 bdrv_set_aio_context(target_bs
, aio_context
);
3380 error_setg(errp
, "Target is attached to a different thread from "
3385 backup_start(job_id
, bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3386 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3387 if (local_err
!= NULL
) {
3388 error_propagate(errp
, local_err
);
3391 aio_context_release(aio_context
);
3394 void qmp_blockdev_backup(bool has_job_id
, const char *job_id
,
3395 const char *device
, const char *target
,
3396 enum MirrorSyncMode sync
,
3397 bool has_speed
, int64_t speed
,
3398 bool has_on_source_error
,
3399 BlockdevOnError on_source_error
,
3400 bool has_on_target_error
,
3401 BlockdevOnError on_target_error
,
3404 do_blockdev_backup(has_job_id
? job_id
: NULL
, device
, target
,
3405 sync
, has_speed
, speed
,
3406 has_on_source_error
, on_source_error
,
3407 has_on_target_error
, on_target_error
,
3411 /* Parameter check and block job starting for drive mirroring.
3412 * Caller should hold @device and @target's aio context (must be the same).
3414 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3415 BlockDriverState
*target
,
3416 bool has_replaces
, const char *replaces
,
3417 enum MirrorSyncMode sync
,
3418 BlockMirrorBackingMode backing_mode
,
3419 bool has_speed
, int64_t speed
,
3420 bool has_granularity
, uint32_t granularity
,
3421 bool has_buf_size
, int64_t buf_size
,
3422 bool has_on_source_error
,
3423 BlockdevOnError on_source_error
,
3424 bool has_on_target_error
,
3425 BlockdevOnError on_target_error
,
3426 bool has_unmap
, bool unmap
,
3433 if (!has_on_source_error
) {
3434 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3436 if (!has_on_target_error
) {
3437 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3439 if (!has_granularity
) {
3442 if (!has_buf_size
) {
3449 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3450 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3451 "a value in range [512B, 64MB]");
3454 if (granularity
& (granularity
- 1)) {
3455 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3460 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3463 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3467 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3468 sync
= MIRROR_SYNC_MODE_FULL
;
3471 /* pass the node name to replace to mirror start since it's loose coupling
3472 * and will allow to check whether the node still exist at mirror completion
3474 mirror_start(job_id
, bs
, target
,
3475 has_replaces
? replaces
: NULL
,
3476 speed
, granularity
, buf_size
, sync
, backing_mode
,
3477 on_source_error
, on_target_error
, unmap
,
3478 block_job_cb
, bs
, errp
);
3481 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3483 BlockDriverState
*bs
;
3485 BlockDriverState
*source
, *target_bs
;
3486 AioContext
*aio_context
;
3487 BlockMirrorBackingMode backing_mode
;
3488 Error
*local_err
= NULL
;
3489 QDict
*options
= NULL
;
3492 const char *format
= arg
->format
;
3494 blk
= blk_by_name(arg
->device
);
3496 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3497 "Device '%s' not found", arg
->device
);
3501 aio_context
= blk_get_aio_context(blk
);
3502 aio_context_acquire(aio_context
);
3504 if (!blk_is_available(blk
)) {
3505 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, arg
->device
);
3509 if (!arg
->has_mode
) {
3510 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3513 if (!arg
->has_format
) {
3514 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3515 ? NULL
: bs
->drv
->format_name
);
3518 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3519 source
= backing_bs(bs
);
3520 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3521 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3523 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3527 size
= bdrv_getlength(bs
);
3529 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3533 if (arg
->has_replaces
) {
3534 BlockDriverState
*to_replace_bs
;
3535 AioContext
*replace_aio_context
;
3536 int64_t replace_size
;
3538 if (!arg
->has_node_name
) {
3539 error_setg(errp
, "a node-name must be provided when replacing a"
3540 " named node of the graph");
3544 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3546 if (!to_replace_bs
) {
3547 error_propagate(errp
, local_err
);
3551 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3552 aio_context_acquire(replace_aio_context
);
3553 replace_size
= bdrv_getlength(to_replace_bs
);
3554 aio_context_release(replace_aio_context
);
3556 if (size
!= replace_size
) {
3557 error_setg(errp
, "cannot replace image with a mirror image of "
3563 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3564 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3566 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3569 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3570 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3572 /* create new image w/o backing file */
3574 bdrv_img_create(arg
->target
, format
,
3575 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3577 switch (arg
->mode
) {
3578 case NEW_IMAGE_MODE_EXISTING
:
3580 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3581 /* create new image with backing file */
3582 bdrv_img_create(arg
->target
, format
,
3584 source
->drv
->format_name
,
3585 NULL
, size
, flags
, &local_err
, false);
3593 error_propagate(errp
, local_err
);
3597 options
= qdict_new();
3598 if (arg
->has_node_name
) {
3599 qdict_put(options
, "node-name", qstring_from_str(arg
->node_name
));
3602 qdict_put(options
, "driver", qstring_from_str(format
));
3605 /* Mirroring takes care of copy-on-write using the source's backing
3608 target_bs
= bdrv_open(arg
->target
, NULL
, options
,
3609 flags
| BDRV_O_NO_BACKING
, errp
);
3614 bdrv_set_aio_context(target_bs
, aio_context
);
3616 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3617 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3618 backing_mode
, arg
->has_speed
, arg
->speed
,
3619 arg
->has_granularity
, arg
->granularity
,
3620 arg
->has_buf_size
, arg
->buf_size
,
3621 arg
->has_on_source_error
, arg
->on_source_error
,
3622 arg
->has_on_target_error
, arg
->on_target_error
,
3623 arg
->has_unmap
, arg
->unmap
,
3625 bdrv_unref(target_bs
);
3626 error_propagate(errp
, local_err
);
3628 aio_context_release(aio_context
);
3631 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3632 const char *device
, const char *target
,
3633 bool has_replaces
, const char *replaces
,
3634 MirrorSyncMode sync
,
3635 bool has_speed
, int64_t speed
,
3636 bool has_granularity
, uint32_t granularity
,
3637 bool has_buf_size
, int64_t buf_size
,
3638 bool has_on_source_error
,
3639 BlockdevOnError on_source_error
,
3640 bool has_on_target_error
,
3641 BlockdevOnError on_target_error
,
3644 BlockDriverState
*bs
;
3646 BlockDriverState
*target_bs
;
3647 AioContext
*aio_context
;
3648 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3649 Error
*local_err
= NULL
;
3651 blk
= blk_by_name(device
);
3653 error_setg(errp
, "Device '%s' not found", device
);
3659 error_setg(errp
, "Device '%s' has no media", device
);
3663 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3668 aio_context
= bdrv_get_aio_context(bs
);
3669 aio_context_acquire(aio_context
);
3671 bdrv_set_aio_context(target_bs
, aio_context
);
3673 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3674 has_replaces
, replaces
, sync
, backing_mode
,
3676 has_granularity
, granularity
,
3677 has_buf_size
, buf_size
,
3678 has_on_source_error
, on_source_error
,
3679 has_on_target_error
, on_target_error
,
3682 error_propagate(errp
, local_err
);
3684 aio_context_release(aio_context
);
3687 /* Get a block job using its ID and acquire its AioContext */
3688 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3695 *aio_context
= NULL
;
3697 job
= block_job_get(id
);
3700 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3701 "Block job '%s' not found", id
);
3705 *aio_context
= blk_get_aio_context(job
->blk
);
3706 aio_context_acquire(*aio_context
);
3711 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3713 AioContext
*aio_context
;
3714 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3720 block_job_set_speed(job
, speed
, errp
);
3721 aio_context_release(aio_context
);
3724 void qmp_block_job_cancel(const char *device
,
3725 bool has_force
, bool force
, Error
**errp
)
3727 AioContext
*aio_context
;
3728 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3738 if (job
->user_paused
&& !force
) {
3739 error_setg(errp
, "The block job for device '%s' is currently paused",
3744 trace_qmp_block_job_cancel(job
);
3745 block_job_cancel(job
);
3747 aio_context_release(aio_context
);
3750 void qmp_block_job_pause(const char *device
, Error
**errp
)
3752 AioContext
*aio_context
;
3753 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3755 if (!job
|| job
->user_paused
) {
3759 job
->user_paused
= true;
3760 trace_qmp_block_job_pause(job
);
3761 block_job_pause(job
);
3762 aio_context_release(aio_context
);
3765 void qmp_block_job_resume(const char *device
, Error
**errp
)
3767 AioContext
*aio_context
;
3768 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3770 if (!job
|| !job
->user_paused
) {
3774 job
->user_paused
= false;
3775 trace_qmp_block_job_resume(job
);
3776 block_job_iostatus_reset(job
);
3777 block_job_resume(job
);
3778 aio_context_release(aio_context
);
3781 void qmp_block_job_complete(const char *device
, Error
**errp
)
3783 AioContext
*aio_context
;
3784 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3790 trace_qmp_block_job_complete(job
);
3791 block_job_complete(job
, errp
);
3792 aio_context_release(aio_context
);
3795 void qmp_change_backing_file(const char *device
,
3796 const char *image_node_name
,
3797 const char *backing_file
,
3801 BlockDriverState
*bs
= NULL
;
3802 AioContext
*aio_context
;
3803 BlockDriverState
*image_bs
= NULL
;
3804 Error
*local_err
= NULL
;
3809 blk
= blk_by_name(device
);
3811 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3812 "Device '%s' not found", device
);
3816 aio_context
= blk_get_aio_context(blk
);
3817 aio_context_acquire(aio_context
);
3819 if (!blk_is_available(blk
)) {
3820 error_setg(errp
, "Device '%s' has no medium", device
);
3825 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3827 error_propagate(errp
, local_err
);
3832 error_setg(errp
, "image file not found");
3836 if (bdrv_find_base(image_bs
) == image_bs
) {
3837 error_setg(errp
, "not allowing backing file change on an image "
3838 "without a backing file");
3842 /* even though we are not necessarily operating on bs, we need it to
3843 * determine if block ops are currently prohibited on the chain */
3844 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3848 /* final sanity check */
3849 if (!bdrv_chain_contains(bs
, image_bs
)) {
3850 error_setg(errp
, "'%s' and image file are not in the same chain",
3855 /* if not r/w, reopen to make r/w */
3856 open_flags
= image_bs
->open_flags
;
3857 ro
= bdrv_is_read_only(image_bs
);
3860 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3862 error_propagate(errp
, local_err
);
3867 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3868 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3871 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3873 /* don't exit here, so we can try to restore open flags if
3878 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3879 error_propagate(errp
, local_err
);
3883 aio_context_release(aio_context
);
3886 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3890 Error
*local_err
= NULL
;
3892 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3897 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3899 if (!qdict_get_try_str(qdict
, "node-name")) {
3901 error_report("'node-name' needs to be specified");
3905 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3907 error_report_err(local_err
);
3911 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3914 qemu_opts_del(opts
);
3917 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3919 BlockDriverState
*bs
;
3920 BlockBackend
*blk
= NULL
;
3922 Visitor
*v
= qmp_output_visitor_new(&obj
);
3924 Error
*local_err
= NULL
;
3926 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3927 * cache.direct=false instead of silently switching to aio=threads, except
3928 * when called from drive_new().
3930 * For now, simply forbidding the combination for all drivers will do. */
3931 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3932 bool direct
= options
->has_cache
&&
3933 options
->cache
->has_direct
&&
3934 options
->cache
->direct
;
3936 error_setg(errp
, "aio=native requires cache.direct=true");
3941 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3943 error_propagate(errp
, local_err
);
3947 visit_complete(v
, &obj
);
3948 qdict
= qobject_to_qdict(obj
);
3950 qdict_flatten(qdict
);
3952 if (options
->has_id
) {
3953 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3955 error_propagate(errp
, local_err
);
3961 if (!qdict_get_try_str(qdict
, "node-name")) {
3962 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3967 bs
= bds_tree_init(qdict
, errp
);
3972 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3975 if (bs
&& bdrv_key_required(bs
)) {
3977 monitor_remove_blk(blk
);
3980 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3983 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3991 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3992 bool has_node_name
, const char *node_name
, Error
**errp
)
3994 AioContext
*aio_context
;
3996 BlockDriverState
*bs
;
3998 if (has_id
&& has_node_name
) {
3999 error_setg(errp
, "Only one of id and node-name must be specified");
4001 } else if (!has_id
&& !has_node_name
) {
4002 error_setg(errp
, "No block device specified");
4007 /* blk_by_name() never returns a BB that is not owned by the monitor */
4008 blk
= blk_by_name(id
);
4010 error_setg(errp
, "Cannot find block backend %s", id
);
4013 if (blk_legacy_dinfo(blk
)) {
4014 error_setg(errp
, "Deleting block backend added with drive-add"
4015 " is not supported");
4018 if (blk_get_refcnt(blk
) > 1) {
4019 error_setg(errp
, "Block backend %s is in use", id
);
4023 aio_context
= blk_get_aio_context(blk
);
4026 bs
= bdrv_find_node(node_name
);
4028 error_setg(errp
, "Cannot find node %s", node_name
);
4031 if (bdrv_has_blk(bs
)) {
4032 error_setg(errp
, "Node %s is in use by %s",
4033 node_name
, bdrv_get_parent_name(bs
));
4036 aio_context
= bdrv_get_aio_context(bs
);
4039 aio_context_acquire(aio_context
);
4042 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4046 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
4047 error_setg(errp
, "Node %s is not owned by the monitor",
4052 if (bs
->refcnt
> 1) {
4053 error_setg(errp
, "Block device %s is in use",
4054 bdrv_get_device_or_node_name(bs
));
4060 monitor_remove_blk(blk
);
4063 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4068 aio_context_release(aio_context
);
4071 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4072 const char *child_name
)
4076 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4077 if (strcmp(child
->name
, child_name
) == 0) {
4085 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4086 const char *child
, bool has_node
,
4087 const char *node
, Error
**errp
)
4089 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4092 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4097 if (has_child
== has_node
) {
4099 error_setg(errp
, "The parameters child and node are in conflict");
4101 error_setg(errp
, "Either child or node must be specified");
4107 p_child
= bdrv_find_child(parent_bs
, child
);
4109 error_setg(errp
, "Node '%s' does not have child '%s'",
4113 bdrv_del_child(parent_bs
, p_child
, errp
);
4117 new_bs
= bdrv_find_node(node
);
4119 error_setg(errp
, "Node '%s' not found", node
);
4122 bdrv_add_child(parent_bs
, new_bs
, errp
);
4126 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4128 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4131 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4132 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4133 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
4135 aio_context_acquire(aio_context
);
4136 elem
->value
= block_job_query(job
);
4137 aio_context_release(aio_context
);
4140 p_next
= &elem
->next
;
4146 QemuOptsList qemu_common_drive_opts
= {
4148 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4152 .type
= QEMU_OPT_BOOL
,
4153 .help
= "enable/disable snapshot mode",
4156 .type
= QEMU_OPT_STRING
,
4157 .help
= "discard operation (ignore/off, unmap/on)",
4160 .type
= QEMU_OPT_STRING
,
4161 .help
= "host AIO implementation (threads, native)",
4163 .name
= BDRV_OPT_CACHE_WB
,
4164 .type
= QEMU_OPT_BOOL
,
4165 .help
= "Enable writeback mode",
4168 .type
= QEMU_OPT_STRING
,
4169 .help
= "disk format (raw, qcow2, ...)",
4172 .type
= QEMU_OPT_STRING
,
4173 .help
= "read error action",
4176 .type
= QEMU_OPT_STRING
,
4177 .help
= "write error action",
4179 .name
= "read-only",
4180 .type
= QEMU_OPT_BOOL
,
4181 .help
= "open drive file as read-only",
4183 .name
= "throttling.iops-total",
4184 .type
= QEMU_OPT_NUMBER
,
4185 .help
= "limit total I/O operations per second",
4187 .name
= "throttling.iops-read",
4188 .type
= QEMU_OPT_NUMBER
,
4189 .help
= "limit read operations per second",
4191 .name
= "throttling.iops-write",
4192 .type
= QEMU_OPT_NUMBER
,
4193 .help
= "limit write operations per second",
4195 .name
= "throttling.bps-total",
4196 .type
= QEMU_OPT_NUMBER
,
4197 .help
= "limit total bytes per second",
4199 .name
= "throttling.bps-read",
4200 .type
= QEMU_OPT_NUMBER
,
4201 .help
= "limit read bytes per second",
4203 .name
= "throttling.bps-write",
4204 .type
= QEMU_OPT_NUMBER
,
4205 .help
= "limit write bytes per second",
4207 .name
= "throttling.iops-total-max",
4208 .type
= QEMU_OPT_NUMBER
,
4209 .help
= "I/O operations burst",
4211 .name
= "throttling.iops-read-max",
4212 .type
= QEMU_OPT_NUMBER
,
4213 .help
= "I/O operations read burst",
4215 .name
= "throttling.iops-write-max",
4216 .type
= QEMU_OPT_NUMBER
,
4217 .help
= "I/O operations write burst",
4219 .name
= "throttling.bps-total-max",
4220 .type
= QEMU_OPT_NUMBER
,
4221 .help
= "total bytes burst",
4223 .name
= "throttling.bps-read-max",
4224 .type
= QEMU_OPT_NUMBER
,
4225 .help
= "total bytes read burst",
4227 .name
= "throttling.bps-write-max",
4228 .type
= QEMU_OPT_NUMBER
,
4229 .help
= "total bytes write burst",
4231 .name
= "throttling.iops-total-max-length",
4232 .type
= QEMU_OPT_NUMBER
,
4233 .help
= "length of the iops-total-max burst period, in seconds",
4235 .name
= "throttling.iops-read-max-length",
4236 .type
= QEMU_OPT_NUMBER
,
4237 .help
= "length of the iops-read-max burst period, in seconds",
4239 .name
= "throttling.iops-write-max-length",
4240 .type
= QEMU_OPT_NUMBER
,
4241 .help
= "length of the iops-write-max burst period, in seconds",
4243 .name
= "throttling.bps-total-max-length",
4244 .type
= QEMU_OPT_NUMBER
,
4245 .help
= "length of the bps-total-max burst period, in seconds",
4247 .name
= "throttling.bps-read-max-length",
4248 .type
= QEMU_OPT_NUMBER
,
4249 .help
= "length of the bps-read-max burst period, in seconds",
4251 .name
= "throttling.bps-write-max-length",
4252 .type
= QEMU_OPT_NUMBER
,
4253 .help
= "length of the bps-write-max burst period, in seconds",
4255 .name
= "throttling.iops-size",
4256 .type
= QEMU_OPT_NUMBER
,
4257 .help
= "when limiting by iops max size of an I/O in bytes",
4259 .name
= "throttling.group",
4260 .type
= QEMU_OPT_STRING
,
4261 .help
= "name of the block throttling group",
4263 .name
= "copy-on-read",
4264 .type
= QEMU_OPT_BOOL
,
4265 .help
= "copy read data from backing file into image file",
4267 .name
= "detect-zeroes",
4268 .type
= QEMU_OPT_STRING
,
4269 .help
= "try to optimize zero writes (off, on, unmap)",
4271 .name
= "stats-account-invalid",
4272 .type
= QEMU_OPT_BOOL
,
4273 .help
= "whether to account for invalid I/O operations "
4274 "in the statistics",
4276 .name
= "stats-account-failed",
4277 .type
= QEMU_OPT_BOOL
,
4278 .help
= "whether to account for failed I/O operations "
4279 "in the statistics",
4281 { /* end of list */ }
4285 static QemuOptsList qemu_root_bds_opts
= {
4287 .head
= QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts
.head
),
4291 .type
= QEMU_OPT_STRING
,
4292 .help
= "discard operation (ignore/off, unmap/on)",
4295 .type
= QEMU_OPT_STRING
,
4296 .help
= "host AIO implementation (threads, native)",
4298 .name
= "read-only",
4299 .type
= QEMU_OPT_BOOL
,
4300 .help
= "open drive file as read-only",
4302 .name
= "copy-on-read",
4303 .type
= QEMU_OPT_BOOL
,
4304 .help
= "copy read data from backing file into image file",
4306 .name
= "detect-zeroes",
4307 .type
= QEMU_OPT_STRING
,
4308 .help
= "try to optimize zero writes (off, on, unmap)",
4310 { /* end of list */ }
4314 QemuOptsList qemu_drive_opts
= {
4316 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4319 * no elements => accept any params
4320 * validation will happen later
4322 { /* end of list */ }