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 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
516 qdict_array_split(interval_dict
, &interval_list
);
518 if (qdict_size(interval_dict
) != 0) {
519 error_setg(errp
, "Invalid option stats-intervals.%s",
520 qdict_first(interval_dict
)->key
);
524 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
525 &detect_zeroes
, &error
);
527 error_propagate(errp
, error
);
531 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
532 if (is_help_option(buf
)) {
533 error_printf("Supported formats:");
534 bdrv_iterate_format(bdrv_format_print
, NULL
);
539 if (qdict_haskey(bs_opts
, "driver")) {
540 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
543 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
546 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
547 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
548 on_write_error
= parse_block_error_action(buf
, 0, &error
);
550 error_propagate(errp
, error
);
555 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
556 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
557 on_read_error
= parse_block_error_action(buf
, 1, &error
);
559 error_propagate(errp
, error
);
565 bdrv_flags
|= BDRV_O_SNAPSHOT
;
569 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
570 BlockBackendRootState
*blk_rs
;
573 blk_rs
= blk_get_root_state(blk
);
574 blk_rs
->open_flags
= bdrv_flags
;
575 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
576 blk_rs
->detect_zeroes
= detect_zeroes
;
580 if (file
&& !*file
) {
584 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
585 * with other callers) rather than what we want as the real defaults.
586 * Apply the defaults here instead. */
587 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
588 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
589 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
591 if (runstate_check(RUN_STATE_INMIGRATE
)) {
592 bdrv_flags
|= BDRV_O_INACTIVE
;
595 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
601 bs
->detect_zeroes
= detect_zeroes
;
603 if (bdrv_key_required(bs
)) {
607 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
609 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
616 /* disk I/O throttling */
617 if (throttle_enabled(&cfg
)) {
618 if (!throttling_group
) {
619 throttling_group
= blk_name(blk
);
621 blk_io_limits_enable(blk
, throttling_group
);
622 blk_set_io_limits(blk
, &cfg
);
625 blk_set_enable_write_cache(blk
, !writethrough
);
626 blk_set_on_error(blk
, on_read_error
, on_write_error
);
628 if (!monitor_add_blk(blk
, qemu_opts_id(opts
), errp
)) {
636 QDECREF(interval_dict
);
637 QDECREF(interval_list
);
642 QDECREF(interval_dict
);
643 QDECREF(interval_list
);
649 static QemuOptsList qemu_root_bds_opts
;
651 /* Takes the ownership of bs_opts */
652 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
654 BlockDriverState
*bs
;
656 Error
*local_error
= NULL
;
657 BlockdevDetectZeroesOptions detect_zeroes
;
660 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
665 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
667 error_propagate(errp
, local_error
);
671 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
672 &detect_zeroes
, &local_error
);
674 error_propagate(errp
, local_error
);
678 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
679 * with other callers) rather than what we want as the real defaults.
680 * Apply the defaults here instead. */
681 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
682 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
684 if (runstate_check(RUN_STATE_INMIGRATE
)) {
685 bdrv_flags
|= BDRV_O_INACTIVE
;
688 bs
= bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
690 goto fail_no_bs_opts
;
693 bs
->detect_zeroes
= detect_zeroes
;
705 void blockdev_close_all_bdrv_states(void)
707 BlockDriverState
*bs
, *next_bs
;
709 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
710 AioContext
*ctx
= bdrv_get_aio_context(bs
);
712 aio_context_acquire(ctx
);
714 aio_context_release(ctx
);
718 /* Iterates over the list of monitor-owned BlockDriverStates */
719 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
721 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
722 : QTAILQ_FIRST(&monitor_bdrv_states
);
725 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
730 value
= qemu_opt_get(opts
, from
);
732 if (qemu_opt_find(opts
, to
)) {
733 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
734 "same time", to
, from
);
739 /* rename all items in opts */
740 while ((value
= qemu_opt_get(opts
, from
))) {
741 qemu_opt_set(opts
, to
, value
, &error_abort
);
742 qemu_opt_unset(opts
, from
);
746 QemuOptsList qemu_legacy_drive_opts
= {
748 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
752 .type
= QEMU_OPT_NUMBER
,
753 .help
= "bus number",
756 .type
= QEMU_OPT_NUMBER
,
757 .help
= "unit number (i.e. lun for scsi)",
760 .type
= QEMU_OPT_NUMBER
,
761 .help
= "index number",
764 .type
= QEMU_OPT_STRING
,
765 .help
= "media type (disk, cdrom)",
768 .type
= QEMU_OPT_STRING
,
769 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
772 .type
= QEMU_OPT_NUMBER
,
773 .help
= "number of cylinders (ide disk geometry)",
776 .type
= QEMU_OPT_NUMBER
,
777 .help
= "number of heads (ide disk geometry)",
780 .type
= QEMU_OPT_NUMBER
,
781 .help
= "number of sectors (ide disk geometry)",
784 .type
= QEMU_OPT_STRING
,
785 .help
= "chs translation (auto, lba, none)",
788 .type
= QEMU_OPT_BOOL
,
789 .help
= "(deprecated, ignored)",
792 .type
= QEMU_OPT_STRING
,
793 .help
= "pci address (virtio only)",
796 .type
= QEMU_OPT_STRING
,
797 .help
= "disk serial number",
800 .type
= QEMU_OPT_STRING
,
804 /* Options that are passed on, but have special semantics with -drive */
807 .type
= QEMU_OPT_BOOL
,
808 .help
= "open drive file as read-only",
811 .type
= QEMU_OPT_STRING
,
812 .help
= "read error action",
815 .type
= QEMU_OPT_STRING
,
816 .help
= "write error action",
818 .name
= "copy-on-read",
819 .type
= QEMU_OPT_BOOL
,
820 .help
= "copy read data from backing file into image file",
823 { /* end of list */ }
827 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
831 DriveInfo
*dinfo
= NULL
;
833 QemuOpts
*legacy_opts
;
834 DriveMediaType media
= MEDIA_DISK
;
835 BlockInterfaceType type
;
836 int cyls
, heads
, secs
, translation
;
837 int max_devs
, bus_id
, unit_id
, index
;
839 const char *werror
, *rerror
;
840 bool read_only
= false;
843 const char *filename
;
844 Error
*local_err
= NULL
;
847 /* Change legacy command line options into QMP ones */
848 static const struct {
852 { "iops", "throttling.iops-total" },
853 { "iops_rd", "throttling.iops-read" },
854 { "iops_wr", "throttling.iops-write" },
856 { "bps", "throttling.bps-total" },
857 { "bps_rd", "throttling.bps-read" },
858 { "bps_wr", "throttling.bps-write" },
860 { "iops_max", "throttling.iops-total-max" },
861 { "iops_rd_max", "throttling.iops-read-max" },
862 { "iops_wr_max", "throttling.iops-write-max" },
864 { "bps_max", "throttling.bps-total-max" },
865 { "bps_rd_max", "throttling.bps-read-max" },
866 { "bps_wr_max", "throttling.bps-write-max" },
868 { "iops_size", "throttling.iops-size" },
870 { "group", "throttling.group" },
872 { "readonly", "read-only" },
875 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
876 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
879 error_report_err(local_err
);
884 value
= qemu_opt_get(all_opts
, "cache");
889 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
890 error_report("invalid cache option");
894 /* Specific options take precedence */
895 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
896 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
897 !writethrough
, &error_abort
);
899 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
900 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
901 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
903 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
904 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
905 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
907 qemu_opt_unset(all_opts
, "cache");
910 /* Get a QDict for processing the options */
911 bs_opts
= qdict_new();
912 qemu_opts_to_qdict(all_opts
, bs_opts
);
914 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
916 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
918 error_report_err(local_err
);
922 /* Deprecated option boot=[on|off] */
923 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
924 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
925 "ignored. Future versions will reject this parameter. Please "
926 "update your scripts.\n");
930 value
= qemu_opt_get(legacy_opts
, "media");
932 if (!strcmp(value
, "disk")) {
934 } else if (!strcmp(value
, "cdrom")) {
938 error_report("'%s' invalid media", value
);
943 /* copy-on-read is disabled with a warning for read-only devices */
944 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
945 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
947 if (read_only
&& copy_on_read
) {
948 error_report("warning: disabling copy-on-read on read-only drive");
949 copy_on_read
= false;
952 qdict_put(bs_opts
, "read-only",
953 qstring_from_str(read_only
? "on" : "off"));
954 qdict_put(bs_opts
, "copy-on-read",
955 qstring_from_str(copy_on_read
? "on" :"off"));
957 /* Controller type */
958 value
= qemu_opt_get(legacy_opts
, "if");
961 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
964 if (type
== IF_COUNT
) {
965 error_report("unsupported bus type '%s'", value
);
969 type
= block_default_type
;
973 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
974 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
975 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
977 if (cyls
|| heads
|| secs
) {
979 error_report("invalid physical cyls number");
983 error_report("invalid physical heads number");
987 error_report("invalid physical secs number");
992 translation
= BIOS_ATA_TRANSLATION_AUTO
;
993 value
= qemu_opt_get(legacy_opts
, "trans");
996 error_report("'%s' trans must be used with cyls, heads and secs",
1000 if (!strcmp(value
, "none")) {
1001 translation
= BIOS_ATA_TRANSLATION_NONE
;
1002 } else if (!strcmp(value
, "lba")) {
1003 translation
= BIOS_ATA_TRANSLATION_LBA
;
1004 } else if (!strcmp(value
, "large")) {
1005 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1006 } else if (!strcmp(value
, "rechs")) {
1007 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1008 } else if (!strcmp(value
, "auto")) {
1009 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1011 error_report("'%s' invalid translation type", value
);
1016 if (media
== MEDIA_CDROM
) {
1017 if (cyls
|| secs
|| heads
) {
1018 error_report("CHS can't be set with media=cdrom");
1023 /* Device address specified by bus/unit or index.
1024 * If none was specified, try to find the first free one. */
1025 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1026 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1027 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1029 max_devs
= if_max_devs
[type
];
1032 if (bus_id
!= 0 || unit_id
!= -1) {
1033 error_report("index cannot be used with bus and unit");
1036 bus_id
= drive_index_to_bus_id(type
, index
);
1037 unit_id
= drive_index_to_unit_id(type
, index
);
1040 if (unit_id
== -1) {
1042 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1044 if (max_devs
&& unit_id
>= max_devs
) {
1045 unit_id
-= max_devs
;
1051 if (max_devs
&& unit_id
>= max_devs
) {
1052 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1056 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1057 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1058 bus_id
, unit_id
, index
);
1063 serial
= qemu_opt_get(legacy_opts
, "serial");
1065 /* no id supplied -> create one */
1066 if (qemu_opts_id(all_opts
) == NULL
) {
1068 const char *mediastr
= "";
1069 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1070 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1073 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1076 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1079 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1083 /* Add virtio block device */
1084 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1085 if (devaddr
&& type
!= IF_VIRTIO
) {
1086 error_report("addr is not supported by this bus type");
1090 if (type
== IF_VIRTIO
) {
1092 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1094 if (arch_type
== QEMU_ARCH_S390X
) {
1095 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1097 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1099 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1102 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1106 filename
= qemu_opt_get(legacy_opts
, "file");
1108 /* Check werror/rerror compatibility with if=... */
1109 werror
= qemu_opt_get(legacy_opts
, "werror");
1110 if (werror
!= NULL
) {
1111 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1113 error_report("werror is not supported by this bus type");
1116 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1119 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1120 if (rerror
!= NULL
) {
1121 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1123 error_report("rerror is not supported by this bus type");
1126 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1129 /* Actual block device init: Functionality shared with blockdev-add */
1130 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1134 error_report_err(local_err
);
1141 /* Create legacy DriveInfo */
1142 dinfo
= g_malloc0(sizeof(*dinfo
));
1143 dinfo
->opts
= all_opts
;
1146 dinfo
->heads
= heads
;
1148 dinfo
->trans
= translation
;
1151 dinfo
->bus
= bus_id
;
1152 dinfo
->unit
= unit_id
;
1153 dinfo
->devaddr
= devaddr
;
1154 dinfo
->serial
= g_strdup(serial
);
1156 blk_set_legacy_dinfo(blk
, dinfo
);
1163 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1170 qemu_opts_del(legacy_opts
);
1175 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1177 const char *device
= qdict_get_str(qdict
, "device");
1181 if (!strcmp(device
, "all")) {
1182 ret
= blk_commit_all();
1184 BlockDriverState
*bs
;
1185 AioContext
*aio_context
;
1187 blk
= blk_by_name(device
);
1189 monitor_printf(mon
, "Device '%s' not found\n", device
);
1192 if (!blk_is_available(blk
)) {
1193 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1198 aio_context
= bdrv_get_aio_context(bs
);
1199 aio_context_acquire(aio_context
);
1201 ret
= bdrv_commit(bs
);
1203 aio_context_release(aio_context
);
1206 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1211 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1213 TransactionActionList list
;
1215 list
.value
= action
;
1217 qmp_transaction(&list
, false, NULL
, errp
);
1220 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1221 bool has_node_name
, const char *node_name
,
1222 const char *snapshot_file
,
1223 bool has_snapshot_node_name
,
1224 const char *snapshot_node_name
,
1225 bool has_format
, const char *format
,
1226 bool has_mode
, NewImageMode mode
, Error
**errp
)
1228 BlockdevSnapshotSync snapshot
= {
1229 .has_device
= has_device
,
1230 .device
= (char *) device
,
1231 .has_node_name
= has_node_name
,
1232 .node_name
= (char *) node_name
,
1233 .snapshot_file
= (char *) snapshot_file
,
1234 .has_snapshot_node_name
= has_snapshot_node_name
,
1235 .snapshot_node_name
= (char *) snapshot_node_name
,
1236 .has_format
= has_format
,
1237 .format
= (char *) format
,
1238 .has_mode
= has_mode
,
1241 TransactionAction action
= {
1242 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1243 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1245 blockdev_do_action(&action
, errp
);
1248 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1251 BlockdevSnapshot snapshot_data
= {
1252 .node
= (char *) node
,
1253 .overlay
= (char *) overlay
1255 TransactionAction action
= {
1256 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1257 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1259 blockdev_do_action(&action
, errp
);
1262 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1266 BlockdevSnapshotInternal snapshot
= {
1267 .device
= (char *) device
,
1268 .name
= (char *) name
1270 TransactionAction action
= {
1271 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1272 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1274 blockdev_do_action(&action
, errp
);
1277 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1284 BlockDriverState
*bs
;
1286 AioContext
*aio_context
;
1287 QEMUSnapshotInfo sn
;
1288 Error
*local_err
= NULL
;
1289 SnapshotInfo
*info
= NULL
;
1292 blk
= blk_by_name(device
);
1294 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1295 "Device '%s' not found", device
);
1299 aio_context
= blk_get_aio_context(blk
);
1300 aio_context_acquire(aio_context
);
1311 error_setg(errp
, "Name or id must be provided");
1312 goto out_aio_context
;
1315 if (!blk_is_available(blk
)) {
1316 error_setg(errp
, "Device '%s' has no medium", device
);
1317 goto out_aio_context
;
1321 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1322 goto out_aio_context
;
1325 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1327 error_propagate(errp
, local_err
);
1328 goto out_aio_context
;
1332 "Snapshot with id '%s' and name '%s' does not exist on "
1334 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1335 goto out_aio_context
;
1338 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1340 error_propagate(errp
, local_err
);
1341 goto out_aio_context
;
1344 aio_context_release(aio_context
);
1346 info
= g_new0(SnapshotInfo
, 1);
1347 info
->id
= g_strdup(sn
.id_str
);
1348 info
->name
= g_strdup(sn
.name
);
1349 info
->date_nsec
= sn
.date_nsec
;
1350 info
->date_sec
= sn
.date_sec
;
1351 info
->vm_state_size
= sn
.vm_state_size
;
1352 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1353 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1358 aio_context_release(aio_context
);
1363 * block_dirty_bitmap_lookup:
1364 * Return a dirty bitmap (if present), after validating
1365 * the node reference and bitmap names.
1367 * @node: The name of the BDS node to search for bitmaps
1368 * @name: The name of the bitmap to search for
1369 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1370 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1371 * @errp: Output pointer for error information. Can be NULL.
1373 * @return: A bitmap object on success, or NULL on failure.
1375 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1377 BlockDriverState
**pbs
,
1381 BlockDriverState
*bs
;
1382 BdrvDirtyBitmap
*bitmap
;
1383 AioContext
*aio_context
;
1386 error_setg(errp
, "Node cannot be NULL");
1390 error_setg(errp
, "Bitmap name cannot be NULL");
1393 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1395 error_setg(errp
, "Node '%s' not found", node
);
1399 aio_context
= bdrv_get_aio_context(bs
);
1400 aio_context_acquire(aio_context
);
1402 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1404 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1412 *paio
= aio_context
;
1414 aio_context_release(aio_context
);
1420 aio_context_release(aio_context
);
1424 /* New and old BlockDriverState structs for atomic group operations */
1426 typedef struct BlkActionState BlkActionState
;
1430 * Table of operations that define an Action.
1432 * @instance_size: Size of state struct, in bytes.
1433 * @prepare: Prepare the work, must NOT be NULL.
1434 * @commit: Commit the changes, can be NULL.
1435 * @abort: Abort the changes on fail, can be NULL.
1436 * @clean: Clean up resources after all transaction actions have called
1437 * commit() or abort(). Can be NULL.
1439 * Only prepare() may fail. In a single transaction, only one of commit() or
1440 * abort() will be called. clean() will always be called if it is present.
1442 typedef struct BlkActionOps
{
1443 size_t instance_size
;
1444 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1445 void (*commit
)(BlkActionState
*common
);
1446 void (*abort
)(BlkActionState
*common
);
1447 void (*clean
)(BlkActionState
*common
);
1452 * Describes one Action's state within a Transaction.
1454 * @action: QAPI-defined enum identifying which Action to perform.
1455 * @ops: Table of ActionOps this Action can perform.
1456 * @block_job_txn: Transaction which this action belongs to.
1457 * @entry: List membership for all Actions in this Transaction.
1459 * This structure must be arranged as first member in a subclassed type,
1460 * assuming that the compiler will also arrange it to the same offsets as the
1463 struct BlkActionState
{
1464 TransactionAction
*action
;
1465 const BlkActionOps
*ops
;
1466 BlockJobTxn
*block_job_txn
;
1467 TransactionProperties
*txn_props
;
1468 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1471 /* internal snapshot private data */
1472 typedef struct InternalSnapshotState
{
1473 BlkActionState common
;
1474 BlockDriverState
*bs
;
1475 AioContext
*aio_context
;
1476 QEMUSnapshotInfo sn
;
1478 } InternalSnapshotState
;
1481 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1483 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1485 "Action '%s' does not support Transaction property "
1486 "completion-mode = %s",
1487 TransactionActionKind_lookup
[s
->action
->type
],
1488 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1494 static void internal_snapshot_prepare(BlkActionState
*common
,
1497 Error
*local_err
= NULL
;
1501 BlockDriverState
*bs
;
1502 QEMUSnapshotInfo old_sn
, *sn
;
1505 BlockdevSnapshotInternal
*internal
;
1506 InternalSnapshotState
*state
;
1509 g_assert(common
->action
->type
==
1510 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1511 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1512 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1514 /* 1. parse input */
1515 device
= internal
->device
;
1516 name
= internal
->name
;
1518 /* 2. check for validation */
1519 if (action_check_completion_mode(common
, errp
) < 0) {
1523 blk
= blk_by_name(device
);
1525 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1526 "Device '%s' not found", device
);
1530 /* AioContext is released in .clean() */
1531 state
->aio_context
= blk_get_aio_context(blk
);
1532 aio_context_acquire(state
->aio_context
);
1534 if (!blk_is_available(blk
)) {
1535 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1541 bdrv_drained_begin(bs
);
1543 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1547 if (bdrv_is_read_only(bs
)) {
1548 error_setg(errp
, "Device '%s' is read only", device
);
1552 if (!bdrv_can_snapshot(bs
)) {
1553 error_setg(errp
, "Block format '%s' used by device '%s' "
1554 "does not support internal snapshots",
1555 bs
->drv
->format_name
, device
);
1559 if (!strlen(name
)) {
1560 error_setg(errp
, "Name is empty");
1564 /* check whether a snapshot with name exist */
1565 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1568 error_propagate(errp
, local_err
);
1572 "Snapshot with name '%s' already exists on device '%s'",
1577 /* 3. take the snapshot */
1579 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1580 qemu_gettimeofday(&tv
);
1581 sn
->date_sec
= tv
.tv_sec
;
1582 sn
->date_nsec
= tv
.tv_usec
* 1000;
1583 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1585 ret1
= bdrv_snapshot_create(bs
, sn
);
1587 error_setg_errno(errp
, -ret1
,
1588 "Failed to create snapshot '%s' on device '%s'",
1593 /* 4. succeed, mark a snapshot is created */
1594 state
->created
= true;
1597 static void internal_snapshot_abort(BlkActionState
*common
)
1599 InternalSnapshotState
*state
=
1600 DO_UPCAST(InternalSnapshotState
, common
, common
);
1601 BlockDriverState
*bs
= state
->bs
;
1602 QEMUSnapshotInfo
*sn
= &state
->sn
;
1603 Error
*local_error
= NULL
;
1605 if (!state
->created
) {
1609 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1610 error_reportf_err(local_error
,
1611 "Failed to delete snapshot with id '%s' and "
1612 "name '%s' on device '%s' in abort: ",
1613 sn
->id_str
, sn
->name
,
1614 bdrv_get_device_name(bs
));
1618 static void internal_snapshot_clean(BlkActionState
*common
)
1620 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1623 if (state
->aio_context
) {
1625 bdrv_drained_end(state
->bs
);
1627 aio_context_release(state
->aio_context
);
1631 /* external snapshot private data */
1632 typedef struct ExternalSnapshotState
{
1633 BlkActionState common
;
1634 BlockDriverState
*old_bs
;
1635 BlockDriverState
*new_bs
;
1636 AioContext
*aio_context
;
1637 } ExternalSnapshotState
;
1639 static void external_snapshot_prepare(BlkActionState
*common
,
1643 QDict
*options
= NULL
;
1644 Error
*local_err
= NULL
;
1645 /* Device and node name of the image to generate the snapshot from */
1647 const char *node_name
;
1648 /* Reference to the new image (for 'blockdev-snapshot') */
1649 const char *snapshot_ref
;
1650 /* File name of the new image (for 'blockdev-snapshot-sync') */
1651 const char *new_image_file
;
1652 ExternalSnapshotState
*state
=
1653 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1654 TransactionAction
*action
= common
->action
;
1656 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1657 * purpose but a different set of parameters */
1658 switch (action
->type
) {
1659 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1661 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1663 node_name
= s
->node
;
1664 new_image_file
= NULL
;
1665 snapshot_ref
= s
->overlay
;
1668 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1670 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1671 device
= s
->has_device
? s
->device
: NULL
;
1672 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1673 new_image_file
= s
->snapshot_file
;
1674 snapshot_ref
= NULL
;
1678 g_assert_not_reached();
1681 /* start processing */
1682 if (action_check_completion_mode(common
, errp
) < 0) {
1686 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1687 if (!state
->old_bs
) {
1691 /* Acquire AioContext now so any threads operating on old_bs stop */
1692 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1693 aio_context_acquire(state
->aio_context
);
1694 bdrv_drained_begin(state
->old_bs
);
1696 if (!bdrv_is_inserted(state
->old_bs
)) {
1697 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1701 if (bdrv_op_is_blocked(state
->old_bs
,
1702 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1706 if (!bdrv_is_read_only(state
->old_bs
)) {
1707 if (bdrv_flush(state
->old_bs
)) {
1708 error_setg(errp
, QERR_IO_ERROR
);
1713 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1714 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1718 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1719 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1720 const char *format
= s
->has_format
? s
->format
: "qcow2";
1721 enum NewImageMode mode
;
1722 const char *snapshot_node_name
=
1723 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1725 if (node_name
&& !snapshot_node_name
) {
1726 error_setg(errp
, "New snapshot node name missing");
1730 if (snapshot_node_name
&&
1731 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1732 error_setg(errp
, "New snapshot node name already in use");
1736 flags
= state
->old_bs
->open_flags
;
1737 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
| BDRV_O_COPY_ON_READ
);
1739 /* create new image w/backing file */
1740 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1741 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1742 int64_t size
= bdrv_getlength(state
->old_bs
);
1744 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1747 bdrv_img_create(new_image_file
, format
,
1748 state
->old_bs
->filename
,
1749 state
->old_bs
->drv
->format_name
,
1750 NULL
, size
, flags
, &local_err
, false);
1752 error_propagate(errp
, local_err
);
1757 options
= qdict_new();
1758 if (s
->has_snapshot_node_name
) {
1759 qdict_put(options
, "node-name",
1760 qstring_from_str(snapshot_node_name
));
1762 qdict_put(options
, "driver", qstring_from_str(format
));
1764 flags
|= BDRV_O_NO_BACKING
;
1767 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1769 /* We will manually add the backing_hd field to the bs later */
1770 if (!state
->new_bs
) {
1774 if (bdrv_has_blk(state
->new_bs
)) {
1775 error_setg(errp
, "The snapshot is already in use by %s",
1776 bdrv_get_parent_name(state
->new_bs
));
1780 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1785 if (state
->new_bs
->backing
!= NULL
) {
1786 error_setg(errp
, "The snapshot already has a backing image");
1790 if (!state
->new_bs
->drv
->supports_backing
) {
1791 error_setg(errp
, "The snapshot does not support backing images");
1795 static void external_snapshot_commit(BlkActionState
*common
)
1797 ExternalSnapshotState
*state
=
1798 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1800 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1802 /* This removes our old bs and adds the new bs */
1803 bdrv_append(state
->new_bs
, state
->old_bs
);
1804 /* We don't need (or want) to use the transactional
1805 * bdrv_reopen_multiple() across all the entries at once, because we
1806 * don't want to abort all of them if one of them fails the reopen */
1807 if (!state
->old_bs
->copy_on_read
) {
1808 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1813 static void external_snapshot_abort(BlkActionState
*common
)
1815 ExternalSnapshotState
*state
=
1816 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1817 if (state
->new_bs
) {
1818 bdrv_unref(state
->new_bs
);
1822 static void external_snapshot_clean(BlkActionState
*common
)
1824 ExternalSnapshotState
*state
=
1825 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1826 if (state
->aio_context
) {
1827 bdrv_drained_end(state
->old_bs
);
1828 aio_context_release(state
->aio_context
);
1832 typedef struct DriveBackupState
{
1833 BlkActionState common
;
1834 BlockDriverState
*bs
;
1835 AioContext
*aio_context
;
1839 static void do_drive_backup(const char *job_id
, const char *device
,
1840 const char *target
, bool has_format
,
1841 const char *format
, enum MirrorSyncMode sync
,
1842 bool has_mode
, enum NewImageMode mode
,
1843 bool has_speed
, int64_t speed
,
1844 bool has_bitmap
, const char *bitmap
,
1845 bool has_on_source_error
,
1846 BlockdevOnError on_source_error
,
1847 bool has_on_target_error
,
1848 BlockdevOnError on_target_error
,
1849 BlockJobTxn
*txn
, Error
**errp
);
1851 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1853 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1855 DriveBackup
*backup
;
1856 Error
*local_err
= NULL
;
1858 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1859 backup
= common
->action
->u
.drive_backup
.data
;
1861 blk
= blk_by_name(backup
->device
);
1863 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1864 "Device '%s' not found", backup
->device
);
1868 if (!blk_is_available(blk
)) {
1869 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1873 /* AioContext is released in .clean() */
1874 state
->aio_context
= blk_get_aio_context(blk
);
1875 aio_context_acquire(state
->aio_context
);
1876 bdrv_drained_begin(blk_bs(blk
));
1877 state
->bs
= blk_bs(blk
);
1879 do_drive_backup(backup
->has_job_id
? backup
->job_id
: NULL
,
1880 backup
->device
, backup
->target
,
1881 backup
->has_format
, backup
->format
,
1883 backup
->has_mode
, backup
->mode
,
1884 backup
->has_speed
, backup
->speed
,
1885 backup
->has_bitmap
, backup
->bitmap
,
1886 backup
->has_on_source_error
, backup
->on_source_error
,
1887 backup
->has_on_target_error
, backup
->on_target_error
,
1888 common
->block_job_txn
, &local_err
);
1890 error_propagate(errp
, local_err
);
1894 state
->job
= state
->bs
->job
;
1897 static void drive_backup_abort(BlkActionState
*common
)
1899 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1900 BlockDriverState
*bs
= state
->bs
;
1902 /* Only cancel if it's the job we started */
1903 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1904 block_job_cancel_sync(bs
->job
);
1908 static void drive_backup_clean(BlkActionState
*common
)
1910 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1912 if (state
->aio_context
) {
1913 bdrv_drained_end(state
->bs
);
1914 aio_context_release(state
->aio_context
);
1918 typedef struct BlockdevBackupState
{
1919 BlkActionState common
;
1920 BlockDriverState
*bs
;
1922 AioContext
*aio_context
;
1923 } BlockdevBackupState
;
1925 static void do_blockdev_backup(const char *job_id
, const char *device
,
1926 const char *target
, enum MirrorSyncMode sync
,
1927 bool has_speed
, int64_t speed
,
1928 bool has_on_source_error
,
1929 BlockdevOnError on_source_error
,
1930 bool has_on_target_error
,
1931 BlockdevOnError on_target_error
,
1932 BlockJobTxn
*txn
, Error
**errp
);
1934 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1936 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1937 BlockdevBackup
*backup
;
1938 BlockBackend
*blk
, *target
;
1939 Error
*local_err
= NULL
;
1941 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1942 backup
= common
->action
->u
.blockdev_backup
.data
;
1944 blk
= blk_by_name(backup
->device
);
1946 error_setg(errp
, "Device '%s' not found", backup
->device
);
1950 if (!blk_is_available(blk
)) {
1951 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1955 target
= blk_by_name(backup
->target
);
1957 error_setg(errp
, "Device '%s' not found", backup
->target
);
1961 /* AioContext is released in .clean() */
1962 state
->aio_context
= blk_get_aio_context(blk
);
1963 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1964 state
->aio_context
= NULL
;
1965 error_setg(errp
, "Backup between two IO threads is not implemented");
1968 aio_context_acquire(state
->aio_context
);
1969 state
->bs
= blk_bs(blk
);
1970 bdrv_drained_begin(state
->bs
);
1972 do_blockdev_backup(backup
->has_job_id
? backup
->job_id
: NULL
,
1973 backup
->device
, backup
->target
, backup
->sync
,
1974 backup
->has_speed
, backup
->speed
,
1975 backup
->has_on_source_error
, backup
->on_source_error
,
1976 backup
->has_on_target_error
, backup
->on_target_error
,
1977 common
->block_job_txn
, &local_err
);
1979 error_propagate(errp
, local_err
);
1983 state
->job
= state
->bs
->job
;
1986 static void blockdev_backup_abort(BlkActionState
*common
)
1988 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1989 BlockDriverState
*bs
= state
->bs
;
1991 /* Only cancel if it's the job we started */
1992 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1993 block_job_cancel_sync(bs
->job
);
1997 static void blockdev_backup_clean(BlkActionState
*common
)
1999 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2001 if (state
->aio_context
) {
2002 bdrv_drained_end(state
->bs
);
2003 aio_context_release(state
->aio_context
);
2007 typedef struct BlockDirtyBitmapState
{
2008 BlkActionState common
;
2009 BdrvDirtyBitmap
*bitmap
;
2010 BlockDriverState
*bs
;
2011 AioContext
*aio_context
;
2014 } BlockDirtyBitmapState
;
2016 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2019 Error
*local_err
= NULL
;
2020 BlockDirtyBitmapAdd
*action
;
2021 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2024 if (action_check_completion_mode(common
, errp
) < 0) {
2028 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2029 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2030 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2031 action
->has_granularity
, action
->granularity
,
2035 state
->prepared
= true;
2037 error_propagate(errp
, local_err
);
2041 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2043 BlockDirtyBitmapAdd
*action
;
2044 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2047 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2048 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2049 * then the node reference and bitmap name must have been valid.
2051 if (state
->prepared
) {
2052 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2056 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2059 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2061 BlockDirtyBitmap
*action
;
2063 if (action_check_completion_mode(common
, errp
) < 0) {
2067 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2068 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2071 &state
->aio_context
,
2073 if (!state
->bitmap
) {
2077 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2078 error_setg(errp
, "Cannot modify a frozen bitmap");
2080 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2081 error_setg(errp
, "Cannot clear a disabled bitmap");
2085 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2086 /* AioContext is released in .clean() */
2089 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2091 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2094 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2097 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2099 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2102 hbitmap_free(state
->backup
);
2105 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2107 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2110 if (state
->aio_context
) {
2111 aio_context_release(state
->aio_context
);
2115 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2117 error_setg(errp
, "Transaction aborted using Abort action");
2120 static void abort_commit(BlkActionState
*common
)
2122 g_assert_not_reached(); /* this action never succeeds */
2125 static const BlkActionOps actions
[] = {
2126 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2127 .instance_size
= sizeof(ExternalSnapshotState
),
2128 .prepare
= external_snapshot_prepare
,
2129 .commit
= external_snapshot_commit
,
2130 .abort
= external_snapshot_abort
,
2131 .clean
= external_snapshot_clean
,
2133 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2134 .instance_size
= sizeof(ExternalSnapshotState
),
2135 .prepare
= external_snapshot_prepare
,
2136 .commit
= external_snapshot_commit
,
2137 .abort
= external_snapshot_abort
,
2138 .clean
= external_snapshot_clean
,
2140 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2141 .instance_size
= sizeof(DriveBackupState
),
2142 .prepare
= drive_backup_prepare
,
2143 .abort
= drive_backup_abort
,
2144 .clean
= drive_backup_clean
,
2146 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2147 .instance_size
= sizeof(BlockdevBackupState
),
2148 .prepare
= blockdev_backup_prepare
,
2149 .abort
= blockdev_backup_abort
,
2150 .clean
= blockdev_backup_clean
,
2152 [TRANSACTION_ACTION_KIND_ABORT
] = {
2153 .instance_size
= sizeof(BlkActionState
),
2154 .prepare
= abort_prepare
,
2155 .commit
= abort_commit
,
2157 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2158 .instance_size
= sizeof(InternalSnapshotState
),
2159 .prepare
= internal_snapshot_prepare
,
2160 .abort
= internal_snapshot_abort
,
2161 .clean
= internal_snapshot_clean
,
2163 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2164 .instance_size
= sizeof(BlockDirtyBitmapState
),
2165 .prepare
= block_dirty_bitmap_add_prepare
,
2166 .abort
= block_dirty_bitmap_add_abort
,
2168 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2169 .instance_size
= sizeof(BlockDirtyBitmapState
),
2170 .prepare
= block_dirty_bitmap_clear_prepare
,
2171 .commit
= block_dirty_bitmap_clear_commit
,
2172 .abort
= block_dirty_bitmap_clear_abort
,
2173 .clean
= block_dirty_bitmap_clear_clean
,
2178 * Allocate a TransactionProperties structure if necessary, and fill
2179 * that structure with desired defaults if they are unset.
2181 static TransactionProperties
*get_transaction_properties(
2182 TransactionProperties
*props
)
2185 props
= g_new0(TransactionProperties
, 1);
2188 if (!props
->has_completion_mode
) {
2189 props
->has_completion_mode
= true;
2190 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2197 * 'Atomic' group operations. The operations are performed as a set, and if
2198 * any fail then we roll back all operations in the group.
2200 void qmp_transaction(TransactionActionList
*dev_list
,
2202 struct TransactionProperties
*props
,
2205 TransactionActionList
*dev_entry
= dev_list
;
2206 BlockJobTxn
*block_job_txn
= NULL
;
2207 BlkActionState
*state
, *next
;
2208 Error
*local_err
= NULL
;
2210 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2211 QSIMPLEQ_INIT(&snap_bdrv_states
);
2213 /* Does this transaction get canceled as a group on failure?
2214 * If not, we don't really need to make a BlockJobTxn.
2216 props
= get_transaction_properties(props
);
2217 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2218 block_job_txn
= block_job_txn_new();
2221 /* drain all i/o before any operations */
2224 /* We don't do anything in this loop that commits us to the operations */
2225 while (NULL
!= dev_entry
) {
2226 TransactionAction
*dev_info
= NULL
;
2227 const BlkActionOps
*ops
;
2229 dev_info
= dev_entry
->value
;
2230 dev_entry
= dev_entry
->next
;
2232 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2234 ops
= &actions
[dev_info
->type
];
2235 assert(ops
->instance_size
> 0);
2237 state
= g_malloc0(ops
->instance_size
);
2239 state
->action
= dev_info
;
2240 state
->block_job_txn
= block_job_txn
;
2241 state
->txn_props
= props
;
2242 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2244 state
->ops
->prepare(state
, &local_err
);
2246 error_propagate(errp
, local_err
);
2247 goto delete_and_fail
;
2251 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2252 if (state
->ops
->commit
) {
2253 state
->ops
->commit(state
);
2261 /* failure, and it is all-or-none; roll back all operations */
2262 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2263 if (state
->ops
->abort
) {
2264 state
->ops
->abort(state
);
2268 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2269 if (state
->ops
->clean
) {
2270 state
->ops
->clean(state
);
2275 qapi_free_TransactionProperties(props
);
2277 block_job_txn_unref(block_job_txn
);
2280 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2282 Error
*local_err
= NULL
;
2289 rc
= do_open_tray(device
, force
, &local_err
);
2290 if (rc
&& rc
!= -ENOSYS
) {
2291 error_propagate(errp
, local_err
);
2294 error_free(local_err
);
2296 qmp_x_blockdev_remove_medium(device
, errp
);
2299 void qmp_block_passwd(bool has_device
, const char *device
,
2300 bool has_node_name
, const char *node_name
,
2301 const char *password
, Error
**errp
)
2303 Error
*local_err
= NULL
;
2304 BlockDriverState
*bs
;
2305 AioContext
*aio_context
;
2307 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2308 has_node_name
? node_name
: NULL
,
2311 error_propagate(errp
, local_err
);
2315 aio_context
= bdrv_get_aio_context(bs
);
2316 aio_context_acquire(aio_context
);
2318 bdrv_add_key(bs
, password
, errp
);
2320 aio_context_release(aio_context
);
2324 * Attempt to open the tray of @device.
2325 * If @force, ignore its tray lock.
2326 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2327 * On error, store an error through @errp and return -errno.
2328 * If @device does not exist, return -ENODEV.
2329 * If it has no removable media, return -ENOTSUP.
2330 * If it has no tray, return -ENOSYS.
2331 * If the guest was asked to open the tray, return -EINPROGRESS.
2334 static int do_open_tray(const char *device
, bool force
, Error
**errp
)
2339 blk
= blk_by_name(device
);
2341 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2342 "Device '%s' not found", device
);
2346 if (!blk_dev_has_removable_media(blk
)) {
2347 error_setg(errp
, "Device '%s' is not removable", device
);
2351 if (!blk_dev_has_tray(blk
)) {
2352 error_setg(errp
, "Device '%s' does not have a tray", device
);
2356 if (blk_dev_is_tray_open(blk
)) {
2360 locked
= blk_dev_is_medium_locked(blk
);
2362 blk_dev_eject_request(blk
, force
);
2365 if (!locked
|| force
) {
2366 blk_dev_change_media_cb(blk
, false);
2369 if (locked
&& !force
) {
2370 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2371 "wait for tray to open and try again", device
);
2372 return -EINPROGRESS
;
2378 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2381 Error
*local_err
= NULL
;
2387 rc
= do_open_tray(device
, force
, &local_err
);
2388 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2389 error_propagate(errp
, local_err
);
2392 error_free(local_err
);
2395 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2399 blk
= blk_by_name(device
);
2401 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2402 "Device '%s' not found", device
);
2406 if (!blk_dev_has_removable_media(blk
)) {
2407 error_setg(errp
, "Device '%s' is not removable", device
);
2411 if (!blk_dev_has_tray(blk
)) {
2412 /* Ignore this command on tray-less devices */
2416 if (!blk_dev_is_tray_open(blk
)) {
2420 blk_dev_change_media_cb(blk
, true);
2423 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2426 BlockDriverState
*bs
;
2427 AioContext
*aio_context
;
2430 blk
= blk_by_name(device
);
2432 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2433 "Device '%s' not found", device
);
2437 /* For BBs without a device, we can exchange the BDS tree at will */
2438 has_device
= blk_get_attached_dev(blk
);
2440 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2441 error_setg(errp
, "Device '%s' is not removable", device
);
2445 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2446 error_setg(errp
, "Tray of device '%s' is not open", device
);
2455 aio_context
= bdrv_get_aio_context(bs
);
2456 aio_context_acquire(aio_context
);
2458 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2464 if (!blk_dev_has_tray(blk
)) {
2465 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2466 * called at all); therefore, the medium needs to be ejected here.
2467 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2468 * value passed here (i.e. false). */
2469 blk_dev_change_media_cb(blk
, false);
2473 aio_context_release(aio_context
);
2476 static void qmp_blockdev_insert_anon_medium(const char *device
,
2477 BlockDriverState
*bs
, Error
**errp
)
2482 blk
= blk_by_name(device
);
2484 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2485 "Device '%s' not found", device
);
2489 /* For BBs without a device, we can exchange the BDS tree at will */
2490 has_device
= blk_get_attached_dev(blk
);
2492 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2493 error_setg(errp
, "Device '%s' is not removable", device
);
2497 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2498 error_setg(errp
, "Tray of device '%s' is not open", device
);
2503 error_setg(errp
, "There already is a medium in device '%s'", device
);
2507 blk_insert_bs(blk
, bs
);
2509 if (!blk_dev_has_tray(blk
)) {
2510 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2511 * called at all); therefore, the medium needs to be pushed into the
2513 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2514 * value passed here (i.e. true). */
2515 blk_dev_change_media_cb(blk
, true);
2519 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2522 BlockDriverState
*bs
;
2524 bs
= bdrv_find_node(node_name
);
2526 error_setg(errp
, "Node '%s' not found", node_name
);
2530 if (bdrv_has_blk(bs
)) {
2531 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2532 bdrv_get_parent_name(bs
));
2536 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2539 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2540 bool has_format
, const char *format
,
2542 BlockdevChangeReadOnlyMode read_only
,
2546 BlockDriverState
*medium_bs
= NULL
;
2549 QDict
*options
= NULL
;
2552 blk
= blk_by_name(device
);
2554 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2555 "Device '%s' not found", device
);
2560 blk_update_root_state(blk
);
2563 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2564 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2567 if (!has_read_only
) {
2568 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2571 switch (read_only
) {
2572 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2575 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2576 bdrv_flags
&= ~BDRV_O_RDWR
;
2579 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2580 bdrv_flags
|= BDRV_O_RDWR
;
2588 options
= qdict_new();
2589 qdict_put(options
, "driver", qstring_from_str(format
));
2592 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2597 bdrv_add_key(medium_bs
, NULL
, &err
);
2599 error_propagate(errp
, err
);
2603 rc
= do_open_tray(device
, false, &err
);
2604 if (rc
&& rc
!= -ENOSYS
) {
2605 error_propagate(errp
, err
);
2611 qmp_x_blockdev_remove_medium(device
, &err
);
2613 error_propagate(errp
, err
);
2617 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2619 error_propagate(errp
, err
);
2623 blk_apply_root_state(blk
, medium_bs
);
2625 qmp_blockdev_close_tray(device
, errp
);
2628 /* If the medium has been inserted, the device has its own reference, so
2629 * ours must be relinquished; and if it has not been inserted successfully,
2630 * the reference must be relinquished anyway */
2631 bdrv_unref(medium_bs
);
2634 /* throttling disk I/O limits */
2635 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2642 bool has_bps_rd_max
,
2644 bool has_bps_wr_max
,
2648 bool has_iops_rd_max
,
2649 int64_t iops_rd_max
,
2650 bool has_iops_wr_max
,
2651 int64_t iops_wr_max
,
2652 bool has_bps_max_length
,
2653 int64_t bps_max_length
,
2654 bool has_bps_rd_max_length
,
2655 int64_t bps_rd_max_length
,
2656 bool has_bps_wr_max_length
,
2657 int64_t bps_wr_max_length
,
2658 bool has_iops_max_length
,
2659 int64_t iops_max_length
,
2660 bool has_iops_rd_max_length
,
2661 int64_t iops_rd_max_length
,
2662 bool has_iops_wr_max_length
,
2663 int64_t iops_wr_max_length
,
2667 const char *group
, Error
**errp
)
2670 BlockDriverState
*bs
;
2672 AioContext
*aio_context
;
2674 blk
= blk_by_name(device
);
2676 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2677 "Device '%s' not found", device
);
2681 aio_context
= blk_get_aio_context(blk
);
2682 aio_context_acquire(aio_context
);
2686 error_setg(errp
, "Device '%s' has no medium", device
);
2690 throttle_config_init(&cfg
);
2691 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2692 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2693 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2695 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2696 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2697 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2700 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2702 if (has_bps_rd_max
) {
2703 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2705 if (has_bps_wr_max
) {
2706 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2709 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2711 if (has_iops_rd_max
) {
2712 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2714 if (has_iops_wr_max
) {
2715 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2718 if (has_bps_max_length
) {
2719 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= bps_max_length
;
2721 if (has_bps_rd_max_length
) {
2722 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= bps_rd_max_length
;
2724 if (has_bps_wr_max_length
) {
2725 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= bps_wr_max_length
;
2727 if (has_iops_max_length
) {
2728 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= iops_max_length
;
2730 if (has_iops_rd_max_length
) {
2731 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= iops_rd_max_length
;
2733 if (has_iops_wr_max_length
) {
2734 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= iops_wr_max_length
;
2737 if (has_iops_size
) {
2738 cfg
.op_size
= iops_size
;
2741 if (!throttle_is_valid(&cfg
, errp
)) {
2745 if (throttle_enabled(&cfg
)) {
2746 /* Enable I/O limits if they're not enabled yet, otherwise
2747 * just update the throttling group. */
2748 if (!blk_get_public(blk
)->throttle_state
) {
2749 blk_io_limits_enable(blk
, has_group
? group
: device
);
2750 } else if (has_group
) {
2751 blk_io_limits_update_group(blk
, group
);
2753 /* Set the new throttling configuration */
2754 blk_set_io_limits(blk
, &cfg
);
2755 } else if (blk_get_public(blk
)->throttle_state
) {
2756 /* If all throttling settings are set to 0, disable I/O limits */
2757 blk_io_limits_disable(blk
);
2761 aio_context_release(aio_context
);
2764 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2765 bool has_granularity
, uint32_t granularity
,
2768 AioContext
*aio_context
;
2769 BlockDriverState
*bs
;
2771 if (!name
|| name
[0] == '\0') {
2772 error_setg(errp
, "Bitmap name cannot be empty");
2776 bs
= bdrv_lookup_bs(node
, node
, errp
);
2781 aio_context
= bdrv_get_aio_context(bs
);
2782 aio_context_acquire(aio_context
);
2784 if (has_granularity
) {
2785 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2786 error_setg(errp
, "Granularity must be power of 2 "
2787 "and at least 512");
2791 /* Default to cluster size, if available: */
2792 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2795 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2798 aio_context_release(aio_context
);
2801 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2804 AioContext
*aio_context
;
2805 BlockDriverState
*bs
;
2806 BdrvDirtyBitmap
*bitmap
;
2808 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2809 if (!bitmap
|| !bs
) {
2813 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2815 "Bitmap '%s' is currently frozen and cannot be removed",
2819 bdrv_dirty_bitmap_make_anon(bitmap
);
2820 bdrv_release_dirty_bitmap(bs
, bitmap
);
2823 aio_context_release(aio_context
);
2827 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2828 * immediately after a full backup operation.
2830 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2833 AioContext
*aio_context
;
2834 BdrvDirtyBitmap
*bitmap
;
2835 BlockDriverState
*bs
;
2837 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2838 if (!bitmap
|| !bs
) {
2842 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2844 "Bitmap '%s' is currently frozen and cannot be modified",
2847 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2849 "Bitmap '%s' is currently disabled and cannot be cleared",
2854 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2857 aio_context_release(aio_context
);
2860 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2862 const char *id
= qdict_get_str(qdict
, "id");
2864 BlockDriverState
*bs
;
2865 AioContext
*aio_context
;
2866 Error
*local_err
= NULL
;
2868 bs
= bdrv_find_node(id
);
2870 qmp_x_blockdev_del(false, NULL
, true, id
, &local_err
);
2872 error_report_err(local_err
);
2877 blk
= blk_by_name(id
);
2879 error_report("Device '%s' not found", id
);
2883 if (!blk_legacy_dinfo(blk
)) {
2884 error_report("Deleting device added with blockdev-add"
2885 " is not supported");
2889 aio_context
= blk_get_aio_context(blk
);
2890 aio_context_acquire(aio_context
);
2894 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2895 error_report_err(local_err
);
2896 aio_context_release(aio_context
);
2903 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2904 monitor_remove_blk(blk
);
2906 /* If this BlockBackend has a device attached to it, its refcount will be
2907 * decremented when the device is removed; otherwise we have to do so here.
2909 if (blk_get_attached_dev(blk
)) {
2910 /* Further I/O must not pause the guest */
2911 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2912 BLOCKDEV_ON_ERROR_REPORT
);
2917 aio_context_release(aio_context
);
2920 void qmp_block_resize(bool has_device
, const char *device
,
2921 bool has_node_name
, const char *node_name
,
2922 int64_t size
, Error
**errp
)
2924 Error
*local_err
= NULL
;
2925 BlockDriverState
*bs
;
2926 AioContext
*aio_context
;
2929 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2930 has_node_name
? node_name
: NULL
,
2933 error_propagate(errp
, local_err
);
2937 aio_context
= bdrv_get_aio_context(bs
);
2938 aio_context_acquire(aio_context
);
2940 if (!bdrv_is_first_non_filter(bs
)) {
2941 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2946 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2950 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2951 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2955 /* complete all in-flight operations before resizing the device */
2958 ret
= bdrv_truncate(bs
, size
);
2963 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2966 error_setg(errp
, QERR_UNSUPPORTED
);
2969 error_setg(errp
, "Device '%s' is read only", device
);
2972 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2975 error_setg_errno(errp
, -ret
, "Could not resize");
2980 aio_context_release(aio_context
);
2983 static void block_job_cb(void *opaque
, int ret
)
2985 /* Note that this function may be executed from another AioContext besides
2986 * the QEMU main loop. If you need to access anything that assumes the
2987 * QEMU global mutex, use a BH or introduce a mutex.
2990 BlockDriverState
*bs
= opaque
;
2991 const char *msg
= NULL
;
2993 trace_block_job_cb(bs
, bs
->job
, ret
);
2998 msg
= strerror(-ret
);
3001 if (block_job_is_cancelled(bs
->job
)) {
3002 block_job_event_cancelled(bs
->job
);
3004 block_job_event_completed(bs
->job
, msg
);
3008 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3009 bool has_base
, const char *base
,
3010 bool has_backing_file
, const char *backing_file
,
3011 bool has_speed
, int64_t speed
,
3012 bool has_on_error
, BlockdevOnError on_error
,
3016 BlockDriverState
*bs
;
3017 BlockDriverState
*base_bs
= NULL
;
3018 AioContext
*aio_context
;
3019 Error
*local_err
= NULL
;
3020 const char *base_name
= NULL
;
3022 if (!has_on_error
) {
3023 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3026 blk
= blk_by_name(device
);
3028 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3029 "Device '%s' not found", device
);
3033 aio_context
= blk_get_aio_context(blk
);
3034 aio_context_acquire(aio_context
);
3036 if (!blk_is_available(blk
)) {
3037 error_setg(errp
, "Device '%s' has no medium", device
);
3042 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3047 base_bs
= bdrv_find_backing_image(bs
, base
);
3048 if (base_bs
== NULL
) {
3049 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3052 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3056 /* if we are streaming the entire chain, the result will have no backing
3057 * file, and specifying one is therefore an error */
3058 if (base_bs
== NULL
&& has_backing_file
) {
3059 error_setg(errp
, "backing file specified, but streaming the "
3064 /* backing_file string overrides base bs filename */
3065 base_name
= has_backing_file
? backing_file
: base_name
;
3067 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3068 has_speed
? speed
: 0, on_error
, block_job_cb
, bs
, &local_err
);
3070 error_propagate(errp
, local_err
);
3074 trace_qmp_block_stream(bs
, bs
->job
);
3077 aio_context_release(aio_context
);
3080 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3081 bool has_base
, const char *base
,
3082 bool has_top
, const char *top
,
3083 bool has_backing_file
, const char *backing_file
,
3084 bool has_speed
, int64_t speed
,
3088 BlockDriverState
*bs
;
3089 BlockDriverState
*base_bs
, *top_bs
;
3090 AioContext
*aio_context
;
3091 Error
*local_err
= NULL
;
3092 /* This will be part of the QMP command, if/when the
3093 * BlockdevOnError change for blkmirror makes it in
3095 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3102 * libvirt relies on the DeviceNotFound error class in order to probe for
3103 * live commit feature versions; for this to work, we must make sure to
3104 * perform the device lookup before any generic errors that may occur in a
3105 * scenario in which all optional arguments are omitted. */
3106 blk
= blk_by_name(device
);
3108 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3109 "Device '%s' not found", device
);
3113 aio_context
= blk_get_aio_context(blk
);
3114 aio_context_acquire(aio_context
);
3116 if (!blk_is_available(blk
)) {
3117 error_setg(errp
, "Device '%s' has no medium", device
);
3122 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3126 /* default top_bs is the active layer */
3129 if (has_top
&& top
) {
3130 if (strcmp(bs
->filename
, top
) != 0) {
3131 top_bs
= bdrv_find_backing_image(bs
, top
);
3135 if (top_bs
== NULL
) {
3136 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3140 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3142 if (has_base
&& base
) {
3143 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3145 base_bs
= bdrv_find_base(top_bs
);
3148 if (base_bs
== NULL
) {
3149 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3153 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3155 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3159 /* Do not allow attempts to commit an image into itself */
3160 if (top_bs
== base_bs
) {
3161 error_setg(errp
, "cannot commit an image into itself");
3166 if (has_backing_file
) {
3167 error_setg(errp
, "'backing-file' specified,"
3168 " but 'top' is the active layer");
3171 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, speed
,
3172 on_error
, block_job_cb
, bs
, &local_err
);
3174 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3175 on_error
, block_job_cb
, bs
,
3176 has_backing_file
? backing_file
: NULL
, &local_err
);
3178 if (local_err
!= NULL
) {
3179 error_propagate(errp
, local_err
);
3184 aio_context_release(aio_context
);
3187 static void do_drive_backup(const char *job_id
, const char *device
,
3188 const char *target
, bool has_format
,
3189 const char *format
, enum MirrorSyncMode sync
,
3190 bool has_mode
, enum NewImageMode mode
,
3191 bool has_speed
, int64_t speed
,
3192 bool has_bitmap
, const char *bitmap
,
3193 bool has_on_source_error
,
3194 BlockdevOnError on_source_error
,
3195 bool has_on_target_error
,
3196 BlockdevOnError on_target_error
,
3197 BlockJobTxn
*txn
, Error
**errp
)
3200 BlockDriverState
*bs
;
3201 BlockDriverState
*target_bs
;
3202 BlockDriverState
*source
= NULL
;
3203 BdrvDirtyBitmap
*bmap
= NULL
;
3204 AioContext
*aio_context
;
3205 QDict
*options
= NULL
;
3206 Error
*local_err
= NULL
;
3213 if (!has_on_source_error
) {
3214 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3216 if (!has_on_target_error
) {
3217 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3220 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3223 blk
= blk_by_name(device
);
3225 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3226 "Device '%s' not found", device
);
3230 aio_context
= blk_get_aio_context(blk
);
3231 aio_context_acquire(aio_context
);
3233 /* Although backup_run has this check too, we need to use bs->drv below, so
3234 * do an early check redundantly. */
3235 if (!blk_is_available(blk
)) {
3236 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3242 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3245 /* Early check to avoid creating target */
3246 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3250 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3252 /* See if we have a backing HD we can use to create our new image
3254 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3255 source
= backing_bs(bs
);
3257 sync
= MIRROR_SYNC_MODE_FULL
;
3260 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3264 size
= bdrv_getlength(bs
);
3266 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3270 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3273 bdrv_img_create(target
, format
, source
->filename
,
3274 source
->drv
->format_name
, NULL
,
3275 size
, flags
, &local_err
, false);
3277 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3278 size
, flags
, &local_err
, false);
3283 error_propagate(errp
, local_err
);
3288 options
= qdict_new();
3289 qdict_put(options
, "driver", qstring_from_str(format
));
3292 target_bs
= bdrv_open(target
, NULL
, options
, flags
, errp
);
3297 bdrv_set_aio_context(target_bs
, aio_context
);
3300 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3302 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3303 bdrv_unref(target_bs
);
3308 backup_start(job_id
, bs
, target_bs
, speed
, sync
, bmap
,
3309 on_source_error
, on_target_error
,
3310 block_job_cb
, bs
, txn
, &local_err
);
3311 bdrv_unref(target_bs
);
3312 if (local_err
!= NULL
) {
3313 error_propagate(errp
, local_err
);
3318 aio_context_release(aio_context
);
3321 void qmp_drive_backup(bool has_job_id
, const char *job_id
,
3322 const char *device
, const char *target
,
3323 bool has_format
, const char *format
,
3324 enum MirrorSyncMode sync
,
3325 bool has_mode
, enum NewImageMode mode
,
3326 bool has_speed
, int64_t speed
,
3327 bool has_bitmap
, const char *bitmap
,
3328 bool has_on_source_error
, BlockdevOnError on_source_error
,
3329 bool has_on_target_error
, BlockdevOnError on_target_error
,
3332 return do_drive_backup(has_job_id
? job_id
: NULL
, device
, target
,
3333 has_format
, format
, sync
,
3334 has_mode
, mode
, has_speed
, speed
,
3336 has_on_source_error
, on_source_error
,
3337 has_on_target_error
, on_target_error
,
3341 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3343 return bdrv_named_nodes_list(errp
);
3346 void do_blockdev_backup(const char *job_id
, const char *device
,
3347 const char *target
, enum MirrorSyncMode sync
,
3348 bool has_speed
, int64_t speed
,
3349 bool has_on_source_error
,
3350 BlockdevOnError on_source_error
,
3351 bool has_on_target_error
,
3352 BlockdevOnError on_target_error
,
3353 BlockJobTxn
*txn
, Error
**errp
)
3356 BlockDriverState
*bs
;
3357 BlockDriverState
*target_bs
;
3358 Error
*local_err
= NULL
;
3359 AioContext
*aio_context
;
3364 if (!has_on_source_error
) {
3365 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3367 if (!has_on_target_error
) {
3368 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3371 blk
= blk_by_name(device
);
3373 error_setg(errp
, "Device '%s' not found", device
);
3377 aio_context
= blk_get_aio_context(blk
);
3378 aio_context_acquire(aio_context
);
3380 if (!blk_is_available(blk
)) {
3381 error_setg(errp
, "Device '%s' has no medium", device
);
3386 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3391 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3392 if (!bdrv_has_blk(target_bs
)) {
3393 /* The target BDS is not attached, we can safely move it to another
3395 bdrv_set_aio_context(target_bs
, aio_context
);
3397 error_setg(errp
, "Target is attached to a different thread from "
3402 backup_start(job_id
, bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3403 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3404 if (local_err
!= NULL
) {
3405 error_propagate(errp
, local_err
);
3408 aio_context_release(aio_context
);
3411 void qmp_blockdev_backup(bool has_job_id
, const char *job_id
,
3412 const char *device
, const char *target
,
3413 enum MirrorSyncMode sync
,
3414 bool has_speed
, int64_t speed
,
3415 bool has_on_source_error
,
3416 BlockdevOnError on_source_error
,
3417 bool has_on_target_error
,
3418 BlockdevOnError on_target_error
,
3421 do_blockdev_backup(has_job_id
? job_id
: NULL
, device
, target
,
3422 sync
, has_speed
, speed
,
3423 has_on_source_error
, on_source_error
,
3424 has_on_target_error
, on_target_error
,
3428 /* Parameter check and block job starting for drive mirroring.
3429 * Caller should hold @device and @target's aio context (must be the same).
3431 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3432 BlockDriverState
*target
,
3433 bool has_replaces
, const char *replaces
,
3434 enum MirrorSyncMode sync
,
3435 BlockMirrorBackingMode backing_mode
,
3436 bool has_speed
, int64_t speed
,
3437 bool has_granularity
, uint32_t granularity
,
3438 bool has_buf_size
, int64_t buf_size
,
3439 bool has_on_source_error
,
3440 BlockdevOnError on_source_error
,
3441 bool has_on_target_error
,
3442 BlockdevOnError on_target_error
,
3443 bool has_unmap
, bool unmap
,
3450 if (!has_on_source_error
) {
3451 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3453 if (!has_on_target_error
) {
3454 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3456 if (!has_granularity
) {
3459 if (!has_buf_size
) {
3466 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3467 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3468 "a value in range [512B, 64MB]");
3471 if (granularity
& (granularity
- 1)) {
3472 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3477 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3480 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3484 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3485 sync
= MIRROR_SYNC_MODE_FULL
;
3488 /* pass the node name to replace to mirror start since it's loose coupling
3489 * and will allow to check whether the node still exist at mirror completion
3491 mirror_start(job_id
, bs
, target
,
3492 has_replaces
? replaces
: NULL
,
3493 speed
, granularity
, buf_size
, sync
, backing_mode
,
3494 on_source_error
, on_target_error
, unmap
,
3495 block_job_cb
, bs
, errp
);
3498 void qmp_drive_mirror(bool has_job_id
, const char *job_id
, const char *device
,
3499 const char *target
, bool has_format
, const char *format
,
3500 bool has_node_name
, const char *node_name
,
3501 bool has_replaces
, const char *replaces
,
3502 enum MirrorSyncMode sync
,
3503 bool has_mode
, enum NewImageMode mode
,
3504 bool has_speed
, int64_t speed
,
3505 bool has_granularity
, uint32_t granularity
,
3506 bool has_buf_size
, int64_t buf_size
,
3507 bool has_on_source_error
, BlockdevOnError on_source_error
,
3508 bool has_on_target_error
, BlockdevOnError on_target_error
,
3509 bool has_unmap
, bool unmap
,
3512 BlockDriverState
*bs
;
3514 BlockDriverState
*source
, *target_bs
;
3515 AioContext
*aio_context
;
3516 BlockMirrorBackingMode backing_mode
;
3517 Error
*local_err
= NULL
;
3518 QDict
*options
= NULL
;
3522 blk
= blk_by_name(device
);
3524 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3525 "Device '%s' not found", device
);
3529 aio_context
= blk_get_aio_context(blk
);
3530 aio_context_acquire(aio_context
);
3532 if (!blk_is_available(blk
)) {
3533 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3538 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3542 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3545 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3546 source
= backing_bs(bs
);
3547 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3548 sync
= MIRROR_SYNC_MODE_FULL
;
3550 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3554 size
= bdrv_getlength(bs
);
3556 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3561 BlockDriverState
*to_replace_bs
;
3562 AioContext
*replace_aio_context
;
3563 int64_t replace_size
;
3565 if (!has_node_name
) {
3566 error_setg(errp
, "a node-name must be provided when replacing a"
3567 " named node of the graph");
3571 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3573 if (!to_replace_bs
) {
3574 error_propagate(errp
, local_err
);
3578 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3579 aio_context_acquire(replace_aio_context
);
3580 replace_size
= bdrv_getlength(to_replace_bs
);
3581 aio_context_release(replace_aio_context
);
3583 if (size
!= replace_size
) {
3584 error_setg(errp
, "cannot replace image with a mirror image of "
3590 if (mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3591 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3593 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3596 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3597 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3599 /* create new image w/o backing file */
3601 bdrv_img_create(target
, format
,
3602 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3605 case NEW_IMAGE_MODE_EXISTING
:
3607 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3608 /* create new image with backing file */
3609 bdrv_img_create(target
, format
,
3611 source
->drv
->format_name
,
3612 NULL
, size
, flags
, &local_err
, false);
3620 error_propagate(errp
, local_err
);
3624 options
= qdict_new();
3625 if (has_node_name
) {
3626 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3629 qdict_put(options
, "driver", qstring_from_str(format
));
3632 /* Mirroring takes care of copy-on-write using the source's backing
3635 target_bs
= bdrv_open(target
, NULL
, options
, flags
| BDRV_O_NO_BACKING
,
3641 bdrv_set_aio_context(target_bs
, aio_context
);
3643 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3644 has_replaces
, replaces
, sync
, backing_mode
,
3646 has_granularity
, granularity
,
3647 has_buf_size
, buf_size
,
3648 has_on_source_error
, on_source_error
,
3649 has_on_target_error
, on_target_error
,
3652 bdrv_unref(target_bs
);
3653 error_propagate(errp
, local_err
);
3655 aio_context_release(aio_context
);
3658 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3659 const char *device
, const char *target
,
3660 bool has_replaces
, const char *replaces
,
3661 MirrorSyncMode sync
,
3662 bool has_speed
, int64_t speed
,
3663 bool has_granularity
, uint32_t granularity
,
3664 bool has_buf_size
, int64_t buf_size
,
3665 bool has_on_source_error
,
3666 BlockdevOnError on_source_error
,
3667 bool has_on_target_error
,
3668 BlockdevOnError on_target_error
,
3671 BlockDriverState
*bs
;
3673 BlockDriverState
*target_bs
;
3674 AioContext
*aio_context
;
3675 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3676 Error
*local_err
= NULL
;
3678 blk
= blk_by_name(device
);
3680 error_setg(errp
, "Device '%s' not found", device
);
3686 error_setg(errp
, "Device '%s' has no media", device
);
3690 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3695 aio_context
= bdrv_get_aio_context(bs
);
3696 aio_context_acquire(aio_context
);
3698 bdrv_set_aio_context(target_bs
, aio_context
);
3700 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3701 has_replaces
, replaces
, sync
, backing_mode
,
3703 has_granularity
, granularity
,
3704 has_buf_size
, buf_size
,
3705 has_on_source_error
, on_source_error
,
3706 has_on_target_error
, on_target_error
,
3709 error_propagate(errp
, local_err
);
3711 aio_context_release(aio_context
);
3714 /* Get a block job using its ID and acquire its AioContext */
3715 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3722 *aio_context
= NULL
;
3724 job
= block_job_get(id
);
3727 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3728 "Block job '%s' not found", id
);
3732 *aio_context
= blk_get_aio_context(job
->blk
);
3733 aio_context_acquire(*aio_context
);
3738 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3740 AioContext
*aio_context
;
3741 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3747 block_job_set_speed(job
, speed
, errp
);
3748 aio_context_release(aio_context
);
3751 void qmp_block_job_cancel(const char *device
,
3752 bool has_force
, bool force
, Error
**errp
)
3754 AioContext
*aio_context
;
3755 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3765 if (job
->user_paused
&& !force
) {
3766 error_setg(errp
, "The block job for device '%s' is currently paused",
3771 trace_qmp_block_job_cancel(job
);
3772 block_job_cancel(job
);
3774 aio_context_release(aio_context
);
3777 void qmp_block_job_pause(const char *device
, Error
**errp
)
3779 AioContext
*aio_context
;
3780 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3782 if (!job
|| job
->user_paused
) {
3786 job
->user_paused
= true;
3787 trace_qmp_block_job_pause(job
);
3788 block_job_pause(job
);
3789 aio_context_release(aio_context
);
3792 void qmp_block_job_resume(const char *device
, Error
**errp
)
3794 AioContext
*aio_context
;
3795 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3797 if (!job
|| !job
->user_paused
) {
3801 job
->user_paused
= false;
3802 trace_qmp_block_job_resume(job
);
3803 block_job_iostatus_reset(job
);
3804 block_job_resume(job
);
3805 aio_context_release(aio_context
);
3808 void qmp_block_job_complete(const char *device
, Error
**errp
)
3810 AioContext
*aio_context
;
3811 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3817 trace_qmp_block_job_complete(job
);
3818 block_job_complete(job
, errp
);
3819 aio_context_release(aio_context
);
3822 void qmp_change_backing_file(const char *device
,
3823 const char *image_node_name
,
3824 const char *backing_file
,
3828 BlockDriverState
*bs
= NULL
;
3829 AioContext
*aio_context
;
3830 BlockDriverState
*image_bs
= NULL
;
3831 Error
*local_err
= NULL
;
3836 blk
= blk_by_name(device
);
3838 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3839 "Device '%s' not found", device
);
3843 aio_context
= blk_get_aio_context(blk
);
3844 aio_context_acquire(aio_context
);
3846 if (!blk_is_available(blk
)) {
3847 error_setg(errp
, "Device '%s' has no medium", device
);
3852 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3854 error_propagate(errp
, local_err
);
3859 error_setg(errp
, "image file not found");
3863 if (bdrv_find_base(image_bs
) == image_bs
) {
3864 error_setg(errp
, "not allowing backing file change on an image "
3865 "without a backing file");
3869 /* even though we are not necessarily operating on bs, we need it to
3870 * determine if block ops are currently prohibited on the chain */
3871 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3875 /* final sanity check */
3876 if (!bdrv_chain_contains(bs
, image_bs
)) {
3877 error_setg(errp
, "'%s' and image file are not in the same chain",
3882 /* if not r/w, reopen to make r/w */
3883 open_flags
= image_bs
->open_flags
;
3884 ro
= bdrv_is_read_only(image_bs
);
3887 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3889 error_propagate(errp
, local_err
);
3894 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3895 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3898 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3900 /* don't exit here, so we can try to restore open flags if
3905 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3906 error_propagate(errp
, local_err
);
3910 aio_context_release(aio_context
);
3913 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3917 Error
*local_err
= NULL
;
3919 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3924 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3926 if (!qdict_get_try_str(qdict
, "node-name")) {
3928 error_report("'node-name' needs to be specified");
3932 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3934 error_report_err(local_err
);
3938 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3941 qemu_opts_del(opts
);
3944 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3946 BlockDriverState
*bs
;
3947 BlockBackend
*blk
= NULL
;
3949 Visitor
*v
= qmp_output_visitor_new(&obj
);
3951 Error
*local_err
= NULL
;
3953 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3954 * cache.direct=false instead of silently switching to aio=threads, except
3955 * when called from drive_new().
3957 * For now, simply forbidding the combination for all drivers will do. */
3958 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3959 bool direct
= options
->has_cache
&&
3960 options
->cache
->has_direct
&&
3961 options
->cache
->direct
;
3963 error_setg(errp
, "aio=native requires cache.direct=true");
3968 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3970 error_propagate(errp
, local_err
);
3974 visit_complete(v
, &obj
);
3975 qdict
= qobject_to_qdict(obj
);
3977 qdict_flatten(qdict
);
3979 if (options
->has_id
) {
3980 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3982 error_propagate(errp
, local_err
);
3988 if (!qdict_get_try_str(qdict
, "node-name")) {
3989 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3994 bs
= bds_tree_init(qdict
, errp
);
3999 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4002 if (bs
&& bdrv_key_required(bs
)) {
4004 monitor_remove_blk(blk
);
4007 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4010 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
4018 void qmp_x_blockdev_del(bool has_id
, const char *id
,
4019 bool has_node_name
, const char *node_name
, Error
**errp
)
4021 AioContext
*aio_context
;
4023 BlockDriverState
*bs
;
4025 if (has_id
&& has_node_name
) {
4026 error_setg(errp
, "Only one of id and node-name must be specified");
4028 } else if (!has_id
&& !has_node_name
) {
4029 error_setg(errp
, "No block device specified");
4034 /* blk_by_name() never returns a BB that is not owned by the monitor */
4035 blk
= blk_by_name(id
);
4037 error_setg(errp
, "Cannot find block backend %s", id
);
4040 if (blk_legacy_dinfo(blk
)) {
4041 error_setg(errp
, "Deleting block backend added with drive-add"
4042 " is not supported");
4045 if (blk_get_refcnt(blk
) > 1) {
4046 error_setg(errp
, "Block backend %s is in use", id
);
4050 aio_context
= blk_get_aio_context(blk
);
4053 bs
= bdrv_find_node(node_name
);
4055 error_setg(errp
, "Cannot find node %s", node_name
);
4058 if (bdrv_has_blk(bs
)) {
4059 error_setg(errp
, "Node %s is in use by %s",
4060 node_name
, bdrv_get_parent_name(bs
));
4063 aio_context
= bdrv_get_aio_context(bs
);
4066 aio_context_acquire(aio_context
);
4069 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4073 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
4074 error_setg(errp
, "Node %s is not owned by the monitor",
4079 if (bs
->refcnt
> 1) {
4080 error_setg(errp
, "Block device %s is in use",
4081 bdrv_get_device_or_node_name(bs
));
4087 monitor_remove_blk(blk
);
4090 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4095 aio_context_release(aio_context
);
4098 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4099 const char *child_name
)
4103 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4104 if (strcmp(child
->name
, child_name
) == 0) {
4112 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4113 const char *child
, bool has_node
,
4114 const char *node
, Error
**errp
)
4116 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4119 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4124 if (has_child
== has_node
) {
4126 error_setg(errp
, "The parameters child and node are in conflict");
4128 error_setg(errp
, "Either child or node must be specified");
4134 p_child
= bdrv_find_child(parent_bs
, child
);
4136 error_setg(errp
, "Node '%s' does not have child '%s'",
4140 bdrv_del_child(parent_bs
, p_child
, errp
);
4144 new_bs
= bdrv_find_node(node
);
4146 error_setg(errp
, "Node '%s' not found", node
);
4149 bdrv_add_child(parent_bs
, new_bs
, errp
);
4153 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4155 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4158 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4159 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4160 AioContext
*aio_context
= blk_get_aio_context(job
->blk
);
4162 aio_context_acquire(aio_context
);
4163 elem
->value
= block_job_query(job
);
4164 aio_context_release(aio_context
);
4167 p_next
= &elem
->next
;
4173 QemuOptsList qemu_common_drive_opts
= {
4175 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4179 .type
= QEMU_OPT_BOOL
,
4180 .help
= "enable/disable snapshot mode",
4183 .type
= QEMU_OPT_STRING
,
4184 .help
= "discard operation (ignore/off, unmap/on)",
4187 .type
= QEMU_OPT_STRING
,
4188 .help
= "host AIO implementation (threads, native)",
4190 .name
= BDRV_OPT_CACHE_WB
,
4191 .type
= QEMU_OPT_BOOL
,
4192 .help
= "Enable writeback mode",
4195 .type
= QEMU_OPT_STRING
,
4196 .help
= "disk format (raw, qcow2, ...)",
4199 .type
= QEMU_OPT_STRING
,
4200 .help
= "read error action",
4203 .type
= QEMU_OPT_STRING
,
4204 .help
= "write error action",
4206 .name
= "read-only",
4207 .type
= QEMU_OPT_BOOL
,
4208 .help
= "open drive file as read-only",
4210 .name
= "throttling.iops-total",
4211 .type
= QEMU_OPT_NUMBER
,
4212 .help
= "limit total I/O operations per second",
4214 .name
= "throttling.iops-read",
4215 .type
= QEMU_OPT_NUMBER
,
4216 .help
= "limit read operations per second",
4218 .name
= "throttling.iops-write",
4219 .type
= QEMU_OPT_NUMBER
,
4220 .help
= "limit write operations per second",
4222 .name
= "throttling.bps-total",
4223 .type
= QEMU_OPT_NUMBER
,
4224 .help
= "limit total bytes per second",
4226 .name
= "throttling.bps-read",
4227 .type
= QEMU_OPT_NUMBER
,
4228 .help
= "limit read bytes per second",
4230 .name
= "throttling.bps-write",
4231 .type
= QEMU_OPT_NUMBER
,
4232 .help
= "limit write bytes per second",
4234 .name
= "throttling.iops-total-max",
4235 .type
= QEMU_OPT_NUMBER
,
4236 .help
= "I/O operations burst",
4238 .name
= "throttling.iops-read-max",
4239 .type
= QEMU_OPT_NUMBER
,
4240 .help
= "I/O operations read burst",
4242 .name
= "throttling.iops-write-max",
4243 .type
= QEMU_OPT_NUMBER
,
4244 .help
= "I/O operations write burst",
4246 .name
= "throttling.bps-total-max",
4247 .type
= QEMU_OPT_NUMBER
,
4248 .help
= "total bytes burst",
4250 .name
= "throttling.bps-read-max",
4251 .type
= QEMU_OPT_NUMBER
,
4252 .help
= "total bytes read burst",
4254 .name
= "throttling.bps-write-max",
4255 .type
= QEMU_OPT_NUMBER
,
4256 .help
= "total bytes write burst",
4258 .name
= "throttling.iops-total-max-length",
4259 .type
= QEMU_OPT_NUMBER
,
4260 .help
= "length of the iops-total-max burst period, in seconds",
4262 .name
= "throttling.iops-read-max-length",
4263 .type
= QEMU_OPT_NUMBER
,
4264 .help
= "length of the iops-read-max burst period, in seconds",
4266 .name
= "throttling.iops-write-max-length",
4267 .type
= QEMU_OPT_NUMBER
,
4268 .help
= "length of the iops-write-max burst period, in seconds",
4270 .name
= "throttling.bps-total-max-length",
4271 .type
= QEMU_OPT_NUMBER
,
4272 .help
= "length of the bps-total-max burst period, in seconds",
4274 .name
= "throttling.bps-read-max-length",
4275 .type
= QEMU_OPT_NUMBER
,
4276 .help
= "length of the bps-read-max burst period, in seconds",
4278 .name
= "throttling.bps-write-max-length",
4279 .type
= QEMU_OPT_NUMBER
,
4280 .help
= "length of the bps-write-max burst period, in seconds",
4282 .name
= "throttling.iops-size",
4283 .type
= QEMU_OPT_NUMBER
,
4284 .help
= "when limiting by iops max size of an I/O in bytes",
4286 .name
= "throttling.group",
4287 .type
= QEMU_OPT_STRING
,
4288 .help
= "name of the block throttling group",
4290 .name
= "copy-on-read",
4291 .type
= QEMU_OPT_BOOL
,
4292 .help
= "copy read data from backing file into image file",
4294 .name
= "detect-zeroes",
4295 .type
= QEMU_OPT_STRING
,
4296 .help
= "try to optimize zero writes (off, on, unmap)",
4298 .name
= "stats-account-invalid",
4299 .type
= QEMU_OPT_BOOL
,
4300 .help
= "whether to account for invalid I/O operations "
4301 "in the statistics",
4303 .name
= "stats-account-failed",
4304 .type
= QEMU_OPT_BOOL
,
4305 .help
= "whether to account for failed I/O operations "
4306 "in the statistics",
4308 { /* end of list */ }
4312 static QemuOptsList qemu_root_bds_opts
= {
4314 .head
= QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts
.head
),
4318 .type
= QEMU_OPT_STRING
,
4319 .help
= "discard operation (ignore/off, unmap/on)",
4322 .type
= QEMU_OPT_STRING
,
4323 .help
= "host AIO implementation (threads, native)",
4325 .name
= "read-only",
4326 .type
= QEMU_OPT_BOOL
,
4327 .help
= "open drive file as read-only",
4329 .name
= "copy-on-read",
4330 .type
= QEMU_OPT_BOOL
,
4331 .help
= "copy read data from backing file into image file",
4333 .name
= "detect-zeroes",
4334 .type
= QEMU_OPT_STRING
,
4335 .help
= "try to optimize zero writes (off, on, unmap)",
4337 { /* end of list */ }
4341 QemuOptsList qemu_drive_opts
= {
4343 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4346 * no elements => accept any params
4347 * validation will happen later
4349 { /* end of list */ }