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 const char *const if_name
[IF_COUNT
] = {
63 [IF_FLOPPY
] = "floppy",
64 [IF_PFLASH
] = "pflash",
67 [IF_VIRTIO
] = "virtio",
71 static int if_max_devs
[IF_COUNT
] = {
73 * Do not change these numbers! They govern how drive option
74 * index maps to unit and bus. That mapping is ABI.
76 * All controllers used to imlement if=T drives need to support
77 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
78 * Otherwise, some index values map to "impossible" bus, unit
81 * For instance, if you change [IF_SCSI] to 255, -drive
82 * if=scsi,index=12 no longer means bus=1,unit=5, but
83 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
84 * the drive can't be set up. Regression.
91 * Boards may call this to offer board-by-board overrides
92 * of the default, global values.
94 void override_max_devs(BlockInterfaceType type
, int max_devs
)
103 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
104 dinfo
= blk_legacy_dinfo(blk
);
105 if (dinfo
->type
== type
) {
106 fprintf(stderr
, "Cannot override units-per-bus property of"
107 " the %s interface, because a drive of that type has"
108 " already been added.\n", if_name
[type
]);
109 g_assert_not_reached();
113 if_max_devs
[type
] = max_devs
;
117 * We automatically delete the drive when a device using it gets
118 * unplugged. Questionable feature, but we can't just drop it.
119 * Device models call blockdev_mark_auto_del() to schedule the
120 * automatic deletion, and generic qdev code calls blockdev_auto_del()
121 * when deletion is actually safe.
123 void blockdev_mark_auto_del(BlockBackend
*blk
)
125 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
126 BlockDriverState
*bs
= blk_bs(blk
);
127 AioContext
*aio_context
;
134 aio_context
= bdrv_get_aio_context(bs
);
135 aio_context_acquire(aio_context
);
138 block_job_cancel(bs
->job
);
141 aio_context_release(aio_context
);
147 void blockdev_auto_del(BlockBackend
*blk
)
149 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
151 if (dinfo
&& dinfo
->auto_del
) {
152 monitor_remove_blk(blk
);
158 * Returns the current mapping of how many units per bus
159 * a particular interface can support.
161 * A positive integer indicates n units per bus.
162 * 0 implies the mapping has not been established.
163 * -1 indicates an invalid BlockInterfaceType was given.
165 int drive_get_max_devs(BlockInterfaceType type
)
167 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
168 return if_max_devs
[type
];
174 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
176 int max_devs
= if_max_devs
[type
];
177 return max_devs
? index
/ max_devs
: 0;
180 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
182 int max_devs
= if_max_devs
[type
];
183 return max_devs
? index
% max_devs
: index
;
186 QemuOpts
*drive_def(const char *optstr
)
188 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
191 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
196 opts
= drive_def(optstr
);
200 if (type
!= IF_DEFAULT
) {
201 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
204 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
207 qemu_opt_set(opts
, "file", file
, &error_abort
);
211 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
216 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
217 dinfo
= blk_legacy_dinfo(blk
);
218 if (dinfo
&& dinfo
->type
== type
219 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
227 bool drive_check_orphaned(void)
233 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
234 dinfo
= blk_legacy_dinfo(blk
);
235 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
236 /* Unless this is a default drive, this may be an oversight. */
237 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
238 dinfo
->type
!= IF_NONE
) {
239 fprintf(stderr
, "Warning: Orphaned drive without device: "
240 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
241 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
242 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
250 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
252 return drive_get(type
,
253 drive_index_to_bus_id(type
, index
),
254 drive_index_to_unit_id(type
, index
));
257 int drive_get_max_bus(BlockInterfaceType type
)
264 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
265 dinfo
= blk_legacy_dinfo(blk
);
266 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
267 max_bus
= dinfo
->bus
;
273 /* Get a block device. This should only be used for single-drive devices
274 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
276 DriveInfo
*drive_get_next(BlockInterfaceType type
)
278 static int next_block_unit
[IF_COUNT
];
280 return drive_get(type
, 0, next_block_unit
[type
]++);
283 static void bdrv_format_print(void *opaque
, const char *name
)
285 error_printf(" %s", name
);
290 BlockDriverState
*bs
;
293 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
295 if (!strcmp(buf
, "ignore")) {
296 return BLOCKDEV_ON_ERROR_IGNORE
;
297 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
298 return BLOCKDEV_ON_ERROR_ENOSPC
;
299 } else if (!strcmp(buf
, "stop")) {
300 return BLOCKDEV_ON_ERROR_STOP
;
301 } else if (!strcmp(buf
, "report")) {
302 return BLOCKDEV_ON_ERROR_REPORT
;
304 error_setg(errp
, "'%s' invalid %s error action",
305 buf
, is_read
? "read" : "write");
310 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
313 const QListEntry
*entry
;
314 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
315 switch (qobject_type(entry
->value
)) {
317 case QTYPE_QSTRING
: {
318 unsigned long long length
;
319 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
320 if (parse_uint_full(str
, &length
, 10) == 0 &&
321 length
> 0 && length
<= UINT_MAX
) {
322 block_acct_add_interval(stats
, (unsigned) length
);
324 error_setg(errp
, "Invalid interval length: %s", str
);
331 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
332 if (length
> 0 && length
<= UINT_MAX
) {
333 block_acct_add_interval(stats
, (unsigned) length
);
335 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
342 error_setg(errp
, "The specification of stats-intervals is invalid");
349 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
351 /* All parameters but @opts are optional and may be set to NULL. */
352 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
353 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
354 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
357 Error
*local_error
= NULL
;
361 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
362 *bdrv_flags
|= BDRV_O_RDWR
;
364 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
365 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
368 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
369 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
370 error_setg(errp
, "Invalid discard option");
375 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
376 if (!strcmp(aio
, "native")) {
377 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
378 } else if (!strcmp(aio
, "threads")) {
379 /* this is the default */
381 error_setg(errp
, "invalid aio option");
387 /* disk I/O throttling */
388 if (throttling_group
) {
389 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
393 throttle_config_init(throttle_cfg
);
394 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
395 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
396 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
397 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
398 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
399 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
400 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
401 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
402 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
403 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
404 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
405 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
407 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
408 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
409 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
410 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
411 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
412 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
413 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
414 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
415 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
416 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
417 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
418 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
420 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
421 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
422 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
423 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
424 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
425 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
426 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
427 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
428 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
429 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
430 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
431 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
433 throttle_cfg
->op_size
=
434 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
436 if (!throttle_is_valid(throttle_cfg
, errp
)) {
443 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
444 qemu_opt_get(opts
, "detect-zeroes"),
445 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
446 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
449 error_propagate(errp
, local_error
);
454 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
455 !(*bdrv_flags
& BDRV_O_UNMAP
))
457 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
458 "without setting discard operation to unmap");
464 /* Takes the ownership of bs_opts */
465 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
470 int on_read_error
, on_write_error
;
471 bool account_invalid
, account_failed
;
473 BlockDriverState
*bs
;
478 QDict
*interval_dict
= NULL
;
479 QList
*interval_list
= NULL
;
481 BlockdevDetectZeroesOptions detect_zeroes
=
482 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
483 const char *throttling_group
= NULL
;
485 /* Check common options by copying from bs_opts to opts, all other options
486 * stay in bs_opts for processing by bdrv_open(). */
487 id
= qdict_get_try_str(bs_opts
, "id");
488 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
490 error_propagate(errp
, error
);
494 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
496 error_propagate(errp
, error
);
501 qdict_del(bs_opts
, "id");
504 /* extract parameters */
505 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
507 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
508 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
510 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
511 qdict_array_split(interval_dict
, &interval_list
);
513 if (qdict_size(interval_dict
) != 0) {
514 error_setg(errp
, "Invalid option stats-intervals.%s",
515 qdict_first(interval_dict
)->key
);
519 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
520 &detect_zeroes
, &error
);
522 error_propagate(errp
, error
);
526 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
527 if (is_help_option(buf
)) {
528 error_printf("Supported formats:");
529 bdrv_iterate_format(bdrv_format_print
, NULL
);
534 if (qdict_haskey(bs_opts
, "driver")) {
535 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
538 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
541 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
542 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
543 on_write_error
= parse_block_error_action(buf
, 0, &error
);
545 error_propagate(errp
, error
);
550 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
551 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
552 on_read_error
= parse_block_error_action(buf
, 1, &error
);
554 error_propagate(errp
, error
);
560 bdrv_flags
|= BDRV_O_SNAPSHOT
;
564 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
565 BlockBackendRootState
*blk_rs
;
572 blk_rs
= blk_get_root_state(blk
);
573 blk_rs
->open_flags
= bdrv_flags
;
574 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
575 blk_rs
->detect_zeroes
= detect_zeroes
;
577 if (throttle_enabled(&cfg
)) {
578 if (!throttling_group
) {
579 throttling_group
= blk_name(blk
);
581 blk_rs
->throttle_group
= g_strdup(throttling_group
);
582 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
583 blk_rs
->throttle_state
->cfg
= cfg
;
588 if (file
&& !*file
) {
592 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
593 * with other callers) rather than what we want as the real defaults.
594 * Apply the defaults here instead. */
595 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
596 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
597 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
599 if (runstate_check(RUN_STATE_INMIGRATE
)) {
600 bdrv_flags
|= BDRV_O_INACTIVE
;
603 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
609 bs
->detect_zeroes
= detect_zeroes
;
611 /* disk I/O throttling */
612 if (throttle_enabled(&cfg
)) {
613 if (!throttling_group
) {
614 throttling_group
= blk_name(blk
);
616 bdrv_io_limits_enable(bs
, throttling_group
);
617 bdrv_set_io_limits(bs
, &cfg
);
620 if (bdrv_key_required(bs
)) {
624 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
626 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
633 blk_set_on_error(blk
, on_read_error
, on_write_error
);
635 if (!monitor_add_blk(blk
, qemu_opts_id(opts
), errp
)) {
643 QDECREF(interval_dict
);
644 QDECREF(interval_list
);
649 QDECREF(interval_dict
);
650 QDECREF(interval_list
);
656 static QemuOptsList qemu_root_bds_opts
;
658 /* Takes the ownership of bs_opts */
659 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
661 BlockDriverState
*bs
;
663 Error
*local_error
= NULL
;
664 BlockdevDetectZeroesOptions detect_zeroes
;
668 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
673 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
675 error_propagate(errp
, local_error
);
679 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
680 &detect_zeroes
, &local_error
);
682 error_propagate(errp
, local_error
);
686 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
687 * with other callers) rather than what we want as the real defaults.
688 * Apply the defaults here instead. */
689 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
690 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
691 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
693 if (runstate_check(RUN_STATE_INMIGRATE
)) {
694 bdrv_flags
|= BDRV_O_INACTIVE
;
698 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
700 goto fail_no_bs_opts
;
703 bs
->detect_zeroes
= detect_zeroes
;
715 void blockdev_close_all_bdrv_states(void)
717 BlockDriverState
*bs
, *next_bs
;
719 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
720 AioContext
*ctx
= bdrv_get_aio_context(bs
);
722 aio_context_acquire(ctx
);
724 aio_context_release(ctx
);
728 /* Iterates over the list of monitor-owned BlockDriverStates */
729 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
731 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
732 : QTAILQ_FIRST(&monitor_bdrv_states
);
735 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
740 value
= qemu_opt_get(opts
, from
);
742 if (qemu_opt_find(opts
, to
)) {
743 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
744 "same time", to
, from
);
749 /* rename all items in opts */
750 while ((value
= qemu_opt_get(opts
, from
))) {
751 qemu_opt_set(opts
, to
, value
, &error_abort
);
752 qemu_opt_unset(opts
, from
);
756 QemuOptsList qemu_legacy_drive_opts
= {
758 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
762 .type
= QEMU_OPT_NUMBER
,
763 .help
= "bus number",
766 .type
= QEMU_OPT_NUMBER
,
767 .help
= "unit number (i.e. lun for scsi)",
770 .type
= QEMU_OPT_NUMBER
,
771 .help
= "index number",
774 .type
= QEMU_OPT_STRING
,
775 .help
= "media type (disk, cdrom)",
778 .type
= QEMU_OPT_STRING
,
779 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
782 .type
= QEMU_OPT_NUMBER
,
783 .help
= "number of cylinders (ide disk geometry)",
786 .type
= QEMU_OPT_NUMBER
,
787 .help
= "number of heads (ide disk geometry)",
790 .type
= QEMU_OPT_NUMBER
,
791 .help
= "number of sectors (ide disk geometry)",
794 .type
= QEMU_OPT_STRING
,
795 .help
= "chs translation (auto, lba, none)",
798 .type
= QEMU_OPT_BOOL
,
799 .help
= "(deprecated, ignored)",
802 .type
= QEMU_OPT_STRING
,
803 .help
= "pci address (virtio only)",
806 .type
= QEMU_OPT_STRING
,
807 .help
= "disk serial number",
810 .type
= QEMU_OPT_STRING
,
814 /* Options that are passed on, but have special semantics with -drive */
817 .type
= QEMU_OPT_BOOL
,
818 .help
= "open drive file as read-only",
821 .type
= QEMU_OPT_STRING
,
822 .help
= "read error action",
825 .type
= QEMU_OPT_STRING
,
826 .help
= "write error action",
828 .name
= "copy-on-read",
829 .type
= QEMU_OPT_BOOL
,
830 .help
= "copy read data from backing file into image file",
833 { /* end of list */ }
837 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
841 DriveInfo
*dinfo
= NULL
;
843 QemuOpts
*legacy_opts
;
844 DriveMediaType media
= MEDIA_DISK
;
845 BlockInterfaceType type
;
846 int cyls
, heads
, secs
, translation
;
847 int max_devs
, bus_id
, unit_id
, index
;
849 const char *werror
, *rerror
;
850 bool read_only
= false;
853 const char *filename
;
854 Error
*local_err
= NULL
;
857 /* Change legacy command line options into QMP ones */
858 static const struct {
862 { "iops", "throttling.iops-total" },
863 { "iops_rd", "throttling.iops-read" },
864 { "iops_wr", "throttling.iops-write" },
866 { "bps", "throttling.bps-total" },
867 { "bps_rd", "throttling.bps-read" },
868 { "bps_wr", "throttling.bps-write" },
870 { "iops_max", "throttling.iops-total-max" },
871 { "iops_rd_max", "throttling.iops-read-max" },
872 { "iops_wr_max", "throttling.iops-write-max" },
874 { "bps_max", "throttling.bps-total-max" },
875 { "bps_rd_max", "throttling.bps-read-max" },
876 { "bps_wr_max", "throttling.bps-write-max" },
878 { "iops_size", "throttling.iops-size" },
880 { "group", "throttling.group" },
882 { "readonly", "read-only" },
885 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
886 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
889 error_report_err(local_err
);
894 value
= qemu_opt_get(all_opts
, "cache");
898 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
899 error_report("invalid cache option");
903 /* Specific options take precedence */
904 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
905 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
906 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
908 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
909 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
910 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
912 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
913 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
914 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
916 qemu_opt_unset(all_opts
, "cache");
919 /* Get a QDict for processing the options */
920 bs_opts
= qdict_new();
921 qemu_opts_to_qdict(all_opts
, bs_opts
);
923 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
925 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
927 error_report_err(local_err
);
931 /* Deprecated option boot=[on|off] */
932 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
933 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
934 "ignored. Future versions will reject this parameter. Please "
935 "update your scripts.\n");
939 value
= qemu_opt_get(legacy_opts
, "media");
941 if (!strcmp(value
, "disk")) {
943 } else if (!strcmp(value
, "cdrom")) {
947 error_report("'%s' invalid media", value
);
952 /* copy-on-read is disabled with a warning for read-only devices */
953 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
954 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
956 if (read_only
&& copy_on_read
) {
957 error_report("warning: disabling copy-on-read on read-only drive");
958 copy_on_read
= false;
961 qdict_put(bs_opts
, "read-only",
962 qstring_from_str(read_only
? "on" : "off"));
963 qdict_put(bs_opts
, "copy-on-read",
964 qstring_from_str(copy_on_read
? "on" :"off"));
966 /* Controller type */
967 value
= qemu_opt_get(legacy_opts
, "if");
970 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
973 if (type
== IF_COUNT
) {
974 error_report("unsupported bus type '%s'", value
);
978 type
= block_default_type
;
982 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
983 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
984 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
986 if (cyls
|| heads
|| secs
) {
988 error_report("invalid physical cyls number");
992 error_report("invalid physical heads number");
996 error_report("invalid physical secs number");
1001 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1002 value
= qemu_opt_get(legacy_opts
, "trans");
1003 if (value
!= NULL
) {
1005 error_report("'%s' trans must be used with cyls, heads and secs",
1009 if (!strcmp(value
, "none")) {
1010 translation
= BIOS_ATA_TRANSLATION_NONE
;
1011 } else if (!strcmp(value
, "lba")) {
1012 translation
= BIOS_ATA_TRANSLATION_LBA
;
1013 } else if (!strcmp(value
, "large")) {
1014 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1015 } else if (!strcmp(value
, "rechs")) {
1016 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1017 } else if (!strcmp(value
, "auto")) {
1018 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1020 error_report("'%s' invalid translation type", value
);
1025 if (media
== MEDIA_CDROM
) {
1026 if (cyls
|| secs
|| heads
) {
1027 error_report("CHS can't be set with media=cdrom");
1032 /* Device address specified by bus/unit or index.
1033 * If none was specified, try to find the first free one. */
1034 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1035 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1036 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1038 max_devs
= if_max_devs
[type
];
1041 if (bus_id
!= 0 || unit_id
!= -1) {
1042 error_report("index cannot be used with bus and unit");
1045 bus_id
= drive_index_to_bus_id(type
, index
);
1046 unit_id
= drive_index_to_unit_id(type
, index
);
1049 if (unit_id
== -1) {
1051 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1053 if (max_devs
&& unit_id
>= max_devs
) {
1054 unit_id
-= max_devs
;
1060 if (max_devs
&& unit_id
>= max_devs
) {
1061 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1065 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1066 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1067 bus_id
, unit_id
, index
);
1072 serial
= qemu_opt_get(legacy_opts
, "serial");
1074 /* no id supplied -> create one */
1075 if (qemu_opts_id(all_opts
) == NULL
) {
1077 const char *mediastr
= "";
1078 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1079 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1082 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1085 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1088 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1092 /* Add virtio block device */
1093 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1094 if (devaddr
&& type
!= IF_VIRTIO
) {
1095 error_report("addr is not supported by this bus type");
1099 if (type
== IF_VIRTIO
) {
1101 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1103 if (arch_type
== QEMU_ARCH_S390X
) {
1104 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1106 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1108 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1111 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1115 filename
= qemu_opt_get(legacy_opts
, "file");
1117 /* Check werror/rerror compatibility with if=... */
1118 werror
= qemu_opt_get(legacy_opts
, "werror");
1119 if (werror
!= NULL
) {
1120 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1122 error_report("werror is not supported by this bus type");
1125 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1128 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1129 if (rerror
!= NULL
) {
1130 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1132 error_report("rerror is not supported by this bus type");
1135 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1138 /* Actual block device init: Functionality shared with blockdev-add */
1139 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1143 error_report_err(local_err
);
1150 /* Create legacy DriveInfo */
1151 dinfo
= g_malloc0(sizeof(*dinfo
));
1152 dinfo
->opts
= all_opts
;
1155 dinfo
->heads
= heads
;
1157 dinfo
->trans
= translation
;
1160 dinfo
->bus
= bus_id
;
1161 dinfo
->unit
= unit_id
;
1162 dinfo
->devaddr
= devaddr
;
1163 dinfo
->serial
= g_strdup(serial
);
1165 blk_set_legacy_dinfo(blk
, dinfo
);
1172 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1179 qemu_opts_del(legacy_opts
);
1184 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1186 const char *device
= qdict_get_str(qdict
, "device");
1190 if (!strcmp(device
, "all")) {
1191 ret
= blk_commit_all();
1193 BlockDriverState
*bs
;
1194 AioContext
*aio_context
;
1196 blk
= blk_by_name(device
);
1198 monitor_printf(mon
, "Device '%s' not found\n", device
);
1201 if (!blk_is_available(blk
)) {
1202 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1207 aio_context
= bdrv_get_aio_context(bs
);
1208 aio_context_acquire(aio_context
);
1210 ret
= bdrv_commit(bs
);
1212 aio_context_release(aio_context
);
1215 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1220 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1222 TransactionActionList list
;
1224 list
.value
= action
;
1226 qmp_transaction(&list
, false, NULL
, errp
);
1229 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1230 bool has_node_name
, const char *node_name
,
1231 const char *snapshot_file
,
1232 bool has_snapshot_node_name
,
1233 const char *snapshot_node_name
,
1234 bool has_format
, const char *format
,
1235 bool has_mode
, NewImageMode mode
, Error
**errp
)
1237 BlockdevSnapshotSync snapshot
= {
1238 .has_device
= has_device
,
1239 .device
= (char *) device
,
1240 .has_node_name
= has_node_name
,
1241 .node_name
= (char *) node_name
,
1242 .snapshot_file
= (char *) snapshot_file
,
1243 .has_snapshot_node_name
= has_snapshot_node_name
,
1244 .snapshot_node_name
= (char *) snapshot_node_name
,
1245 .has_format
= has_format
,
1246 .format
= (char *) format
,
1247 .has_mode
= has_mode
,
1250 TransactionAction action
= {
1251 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1252 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1254 blockdev_do_action(&action
, errp
);
1257 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1260 BlockdevSnapshot snapshot_data
= {
1261 .node
= (char *) node
,
1262 .overlay
= (char *) overlay
1264 TransactionAction action
= {
1265 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1266 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1268 blockdev_do_action(&action
, errp
);
1271 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1275 BlockdevSnapshotInternal snapshot
= {
1276 .device
= (char *) device
,
1277 .name
= (char *) name
1279 TransactionAction action
= {
1280 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1281 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1283 blockdev_do_action(&action
, errp
);
1286 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1293 BlockDriverState
*bs
;
1295 AioContext
*aio_context
;
1296 QEMUSnapshotInfo sn
;
1297 Error
*local_err
= NULL
;
1298 SnapshotInfo
*info
= NULL
;
1301 blk
= blk_by_name(device
);
1303 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1304 "Device '%s' not found", device
);
1308 aio_context
= blk_get_aio_context(blk
);
1309 aio_context_acquire(aio_context
);
1320 error_setg(errp
, "Name or id must be provided");
1321 goto out_aio_context
;
1324 if (!blk_is_available(blk
)) {
1325 error_setg(errp
, "Device '%s' has no medium", device
);
1326 goto out_aio_context
;
1330 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1331 goto out_aio_context
;
1334 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1336 error_propagate(errp
, local_err
);
1337 goto out_aio_context
;
1341 "Snapshot with id '%s' and name '%s' does not exist on "
1343 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1344 goto out_aio_context
;
1347 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1349 error_propagate(errp
, local_err
);
1350 goto out_aio_context
;
1353 aio_context_release(aio_context
);
1355 info
= g_new0(SnapshotInfo
, 1);
1356 info
->id
= g_strdup(sn
.id_str
);
1357 info
->name
= g_strdup(sn
.name
);
1358 info
->date_nsec
= sn
.date_nsec
;
1359 info
->date_sec
= sn
.date_sec
;
1360 info
->vm_state_size
= sn
.vm_state_size
;
1361 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1362 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1367 aio_context_release(aio_context
);
1372 * block_dirty_bitmap_lookup:
1373 * Return a dirty bitmap (if present), after validating
1374 * the node reference and bitmap names.
1376 * @node: The name of the BDS node to search for bitmaps
1377 * @name: The name of the bitmap to search for
1378 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1379 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1380 * @errp: Output pointer for error information. Can be NULL.
1382 * @return: A bitmap object on success, or NULL on failure.
1384 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1386 BlockDriverState
**pbs
,
1390 BlockDriverState
*bs
;
1391 BdrvDirtyBitmap
*bitmap
;
1392 AioContext
*aio_context
;
1395 error_setg(errp
, "Node cannot be NULL");
1399 error_setg(errp
, "Bitmap name cannot be NULL");
1402 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1404 error_setg(errp
, "Node '%s' not found", node
);
1408 aio_context
= bdrv_get_aio_context(bs
);
1409 aio_context_acquire(aio_context
);
1411 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1413 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1421 *paio
= aio_context
;
1423 aio_context_release(aio_context
);
1429 aio_context_release(aio_context
);
1433 /* New and old BlockDriverState structs for atomic group operations */
1435 typedef struct BlkActionState BlkActionState
;
1439 * Table of operations that define an Action.
1441 * @instance_size: Size of state struct, in bytes.
1442 * @prepare: Prepare the work, must NOT be NULL.
1443 * @commit: Commit the changes, can be NULL.
1444 * @abort: Abort the changes on fail, can be NULL.
1445 * @clean: Clean up resources after all transaction actions have called
1446 * commit() or abort(). Can be NULL.
1448 * Only prepare() may fail. In a single transaction, only one of commit() or
1449 * abort() will be called. clean() will always be called if it is present.
1451 typedef struct BlkActionOps
{
1452 size_t instance_size
;
1453 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1454 void (*commit
)(BlkActionState
*common
);
1455 void (*abort
)(BlkActionState
*common
);
1456 void (*clean
)(BlkActionState
*common
);
1461 * Describes one Action's state within a Transaction.
1463 * @action: QAPI-defined enum identifying which Action to perform.
1464 * @ops: Table of ActionOps this Action can perform.
1465 * @block_job_txn: Transaction which this action belongs to.
1466 * @entry: List membership for all Actions in this Transaction.
1468 * This structure must be arranged as first member in a subclassed type,
1469 * assuming that the compiler will also arrange it to the same offsets as the
1472 struct BlkActionState
{
1473 TransactionAction
*action
;
1474 const BlkActionOps
*ops
;
1475 BlockJobTxn
*block_job_txn
;
1476 TransactionProperties
*txn_props
;
1477 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1480 /* internal snapshot private data */
1481 typedef struct InternalSnapshotState
{
1482 BlkActionState common
;
1483 BlockDriverState
*bs
;
1484 AioContext
*aio_context
;
1485 QEMUSnapshotInfo sn
;
1487 } InternalSnapshotState
;
1490 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1492 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1494 "Action '%s' does not support Transaction property "
1495 "completion-mode = %s",
1496 TransactionActionKind_lookup
[s
->action
->type
],
1497 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1503 static void internal_snapshot_prepare(BlkActionState
*common
,
1506 Error
*local_err
= NULL
;
1510 BlockDriverState
*bs
;
1511 QEMUSnapshotInfo old_sn
, *sn
;
1514 BlockdevSnapshotInternal
*internal
;
1515 InternalSnapshotState
*state
;
1518 g_assert(common
->action
->type
==
1519 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1520 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1521 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1523 /* 1. parse input */
1524 device
= internal
->device
;
1525 name
= internal
->name
;
1527 /* 2. check for validation */
1528 if (action_check_completion_mode(common
, errp
) < 0) {
1532 blk
= blk_by_name(device
);
1534 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1535 "Device '%s' not found", device
);
1539 /* AioContext is released in .clean() */
1540 state
->aio_context
= blk_get_aio_context(blk
);
1541 aio_context_acquire(state
->aio_context
);
1543 if (!blk_is_available(blk
)) {
1544 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1550 bdrv_drained_begin(bs
);
1552 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1556 if (bdrv_is_read_only(bs
)) {
1557 error_setg(errp
, "Device '%s' is read only", device
);
1561 if (!bdrv_can_snapshot(bs
)) {
1562 error_setg(errp
, "Block format '%s' used by device '%s' "
1563 "does not support internal snapshots",
1564 bs
->drv
->format_name
, device
);
1568 if (!strlen(name
)) {
1569 error_setg(errp
, "Name is empty");
1573 /* check whether a snapshot with name exist */
1574 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1577 error_propagate(errp
, local_err
);
1581 "Snapshot with name '%s' already exists on device '%s'",
1586 /* 3. take the snapshot */
1588 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1589 qemu_gettimeofday(&tv
);
1590 sn
->date_sec
= tv
.tv_sec
;
1591 sn
->date_nsec
= tv
.tv_usec
* 1000;
1592 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1594 ret1
= bdrv_snapshot_create(bs
, sn
);
1596 error_setg_errno(errp
, -ret1
,
1597 "Failed to create snapshot '%s' on device '%s'",
1602 /* 4. succeed, mark a snapshot is created */
1603 state
->created
= true;
1606 static void internal_snapshot_abort(BlkActionState
*common
)
1608 InternalSnapshotState
*state
=
1609 DO_UPCAST(InternalSnapshotState
, common
, common
);
1610 BlockDriverState
*bs
= state
->bs
;
1611 QEMUSnapshotInfo
*sn
= &state
->sn
;
1612 Error
*local_error
= NULL
;
1614 if (!state
->created
) {
1618 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1619 error_reportf_err(local_error
,
1620 "Failed to delete snapshot with id '%s' and "
1621 "name '%s' on device '%s' in abort: ",
1622 sn
->id_str
, sn
->name
,
1623 bdrv_get_device_name(bs
));
1627 static void internal_snapshot_clean(BlkActionState
*common
)
1629 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1632 if (state
->aio_context
) {
1634 bdrv_drained_end(state
->bs
);
1636 aio_context_release(state
->aio_context
);
1640 /* external snapshot private data */
1641 typedef struct ExternalSnapshotState
{
1642 BlkActionState common
;
1643 BlockDriverState
*old_bs
;
1644 BlockDriverState
*new_bs
;
1645 AioContext
*aio_context
;
1646 } ExternalSnapshotState
;
1648 static void external_snapshot_prepare(BlkActionState
*common
,
1652 QDict
*options
= NULL
;
1653 Error
*local_err
= NULL
;
1654 /* Device and node name of the image to generate the snapshot from */
1656 const char *node_name
;
1657 /* Reference to the new image (for 'blockdev-snapshot') */
1658 const char *snapshot_ref
;
1659 /* File name of the new image (for 'blockdev-snapshot-sync') */
1660 const char *new_image_file
;
1661 ExternalSnapshotState
*state
=
1662 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1663 TransactionAction
*action
= common
->action
;
1665 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1666 * purpose but a different set of parameters */
1667 switch (action
->type
) {
1668 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1670 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1672 node_name
= s
->node
;
1673 new_image_file
= NULL
;
1674 snapshot_ref
= s
->overlay
;
1677 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1679 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1680 device
= s
->has_device
? s
->device
: NULL
;
1681 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1682 new_image_file
= s
->snapshot_file
;
1683 snapshot_ref
= NULL
;
1687 g_assert_not_reached();
1690 /* start processing */
1691 if (action_check_completion_mode(common
, errp
) < 0) {
1695 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1696 if (!state
->old_bs
) {
1700 /* Acquire AioContext now so any threads operating on old_bs stop */
1701 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1702 aio_context_acquire(state
->aio_context
);
1703 bdrv_drained_begin(state
->old_bs
);
1705 if (!bdrv_is_inserted(state
->old_bs
)) {
1706 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1710 if (bdrv_op_is_blocked(state
->old_bs
,
1711 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1715 if (!bdrv_is_read_only(state
->old_bs
)) {
1716 if (bdrv_flush(state
->old_bs
)) {
1717 error_setg(errp
, QERR_IO_ERROR
);
1722 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1723 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1727 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1728 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1729 const char *format
= s
->has_format
? s
->format
: "qcow2";
1730 enum NewImageMode mode
;
1731 const char *snapshot_node_name
=
1732 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1734 if (node_name
&& !snapshot_node_name
) {
1735 error_setg(errp
, "New snapshot node name missing");
1739 if (snapshot_node_name
&&
1740 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1741 error_setg(errp
, "New snapshot node name already in use");
1745 flags
= state
->old_bs
->open_flags
;
1746 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
| BDRV_O_COPY_ON_READ
);
1748 /* create new image w/backing file */
1749 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1750 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1751 int64_t size
= bdrv_getlength(state
->old_bs
);
1753 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1756 bdrv_img_create(new_image_file
, format
,
1757 state
->old_bs
->filename
,
1758 state
->old_bs
->drv
->format_name
,
1759 NULL
, size
, flags
, &local_err
, false);
1761 error_propagate(errp
, local_err
);
1766 options
= qdict_new();
1767 if (s
->has_snapshot_node_name
) {
1768 qdict_put(options
, "node-name",
1769 qstring_from_str(snapshot_node_name
));
1771 qdict_put(options
, "driver", qstring_from_str(format
));
1773 flags
|= BDRV_O_NO_BACKING
;
1776 /* There is no BB attached during bdrv_open(), so we can't set a
1777 * writethrough mode. bdrv_append() will swap the WCE setting so that the
1778 * backing file becomes unconditionally writeback (which is what backing
1779 * files should always be) and the new overlay gets the original setting. */
1780 flags
|= BDRV_O_CACHE_WB
;
1782 assert(state
->new_bs
== NULL
);
1783 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1785 /* We will manually add the backing_hd field to the bs later */
1790 if (state
->new_bs
->blk
!= NULL
) {
1791 error_setg(errp
, "The snapshot is already in use by %s",
1792 blk_name(state
->new_bs
->blk
));
1796 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1801 if (state
->new_bs
->backing
!= NULL
) {
1802 error_setg(errp
, "The snapshot already has a backing image");
1806 if (!state
->new_bs
->drv
->supports_backing
) {
1807 error_setg(errp
, "The snapshot does not support backing images");
1811 static void external_snapshot_commit(BlkActionState
*common
)
1813 ExternalSnapshotState
*state
=
1814 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1816 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1818 /* This removes our old bs and adds the new bs */
1819 bdrv_append(state
->new_bs
, state
->old_bs
);
1820 /* We don't need (or want) to use the transactional
1821 * bdrv_reopen_multiple() across all the entries at once, because we
1822 * don't want to abort all of them if one of them fails the reopen */
1823 if (!state
->old_bs
->copy_on_read
) {
1824 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1829 static void external_snapshot_abort(BlkActionState
*common
)
1831 ExternalSnapshotState
*state
=
1832 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1833 if (state
->new_bs
) {
1834 bdrv_unref(state
->new_bs
);
1838 static void external_snapshot_clean(BlkActionState
*common
)
1840 ExternalSnapshotState
*state
=
1841 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1842 if (state
->aio_context
) {
1843 bdrv_drained_end(state
->old_bs
);
1844 aio_context_release(state
->aio_context
);
1848 typedef struct DriveBackupState
{
1849 BlkActionState common
;
1850 BlockDriverState
*bs
;
1851 AioContext
*aio_context
;
1855 static void do_drive_backup(const char *device
, const char *target
,
1856 bool has_format
, const char *format
,
1857 enum MirrorSyncMode sync
,
1858 bool has_mode
, enum NewImageMode mode
,
1859 bool has_speed
, int64_t speed
,
1860 bool has_bitmap
, const char *bitmap
,
1861 bool has_on_source_error
,
1862 BlockdevOnError on_source_error
,
1863 bool has_on_target_error
,
1864 BlockdevOnError on_target_error
,
1865 BlockJobTxn
*txn
, Error
**errp
);
1867 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1869 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1871 DriveBackup
*backup
;
1872 Error
*local_err
= NULL
;
1874 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1875 backup
= common
->action
->u
.drive_backup
.data
;
1877 blk
= blk_by_name(backup
->device
);
1879 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1880 "Device '%s' not found", backup
->device
);
1884 if (!blk_is_available(blk
)) {
1885 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1889 /* AioContext is released in .clean() */
1890 state
->aio_context
= blk_get_aio_context(blk
);
1891 aio_context_acquire(state
->aio_context
);
1892 bdrv_drained_begin(blk_bs(blk
));
1893 state
->bs
= blk_bs(blk
);
1895 do_drive_backup(backup
->device
, backup
->target
,
1896 backup
->has_format
, backup
->format
,
1898 backup
->has_mode
, backup
->mode
,
1899 backup
->has_speed
, backup
->speed
,
1900 backup
->has_bitmap
, backup
->bitmap
,
1901 backup
->has_on_source_error
, backup
->on_source_error
,
1902 backup
->has_on_target_error
, backup
->on_target_error
,
1903 common
->block_job_txn
, &local_err
);
1905 error_propagate(errp
, local_err
);
1909 state
->job
= state
->bs
->job
;
1912 static void drive_backup_abort(BlkActionState
*common
)
1914 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1915 BlockDriverState
*bs
= state
->bs
;
1917 /* Only cancel if it's the job we started */
1918 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1919 block_job_cancel_sync(bs
->job
);
1923 static void drive_backup_clean(BlkActionState
*common
)
1925 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1927 if (state
->aio_context
) {
1928 bdrv_drained_end(state
->bs
);
1929 aio_context_release(state
->aio_context
);
1933 typedef struct BlockdevBackupState
{
1934 BlkActionState common
;
1935 BlockDriverState
*bs
;
1937 AioContext
*aio_context
;
1938 } BlockdevBackupState
;
1940 static void do_blockdev_backup(const char *device
, const char *target
,
1941 enum MirrorSyncMode sync
,
1942 bool has_speed
, int64_t speed
,
1943 bool has_on_source_error
,
1944 BlockdevOnError on_source_error
,
1945 bool has_on_target_error
,
1946 BlockdevOnError on_target_error
,
1947 BlockJobTxn
*txn
, Error
**errp
);
1949 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1951 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1952 BlockdevBackup
*backup
;
1953 BlockBackend
*blk
, *target
;
1954 Error
*local_err
= NULL
;
1956 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1957 backup
= common
->action
->u
.blockdev_backup
.data
;
1959 blk
= blk_by_name(backup
->device
);
1961 error_setg(errp
, "Device '%s' not found", backup
->device
);
1965 if (!blk_is_available(blk
)) {
1966 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1970 target
= blk_by_name(backup
->target
);
1972 error_setg(errp
, "Device '%s' not found", backup
->target
);
1976 /* AioContext is released in .clean() */
1977 state
->aio_context
= blk_get_aio_context(blk
);
1978 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1979 state
->aio_context
= NULL
;
1980 error_setg(errp
, "Backup between two IO threads is not implemented");
1983 aio_context_acquire(state
->aio_context
);
1984 state
->bs
= blk_bs(blk
);
1985 bdrv_drained_begin(state
->bs
);
1987 do_blockdev_backup(backup
->device
, backup
->target
,
1989 backup
->has_speed
, backup
->speed
,
1990 backup
->has_on_source_error
, backup
->on_source_error
,
1991 backup
->has_on_target_error
, backup
->on_target_error
,
1992 common
->block_job_txn
, &local_err
);
1994 error_propagate(errp
, local_err
);
1998 state
->job
= state
->bs
->job
;
2001 static void blockdev_backup_abort(BlkActionState
*common
)
2003 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2004 BlockDriverState
*bs
= state
->bs
;
2006 /* Only cancel if it's the job we started */
2007 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
2008 block_job_cancel_sync(bs
->job
);
2012 static void blockdev_backup_clean(BlkActionState
*common
)
2014 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2016 if (state
->aio_context
) {
2017 bdrv_drained_end(state
->bs
);
2018 aio_context_release(state
->aio_context
);
2022 typedef struct BlockDirtyBitmapState
{
2023 BlkActionState common
;
2024 BdrvDirtyBitmap
*bitmap
;
2025 BlockDriverState
*bs
;
2026 AioContext
*aio_context
;
2029 } BlockDirtyBitmapState
;
2031 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2034 Error
*local_err
= NULL
;
2035 BlockDirtyBitmapAdd
*action
;
2036 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2039 if (action_check_completion_mode(common
, errp
) < 0) {
2043 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2044 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2045 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2046 action
->has_granularity
, action
->granularity
,
2050 state
->prepared
= true;
2052 error_propagate(errp
, local_err
);
2056 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2058 BlockDirtyBitmapAdd
*action
;
2059 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2062 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2063 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2064 * then the node reference and bitmap name must have been valid.
2066 if (state
->prepared
) {
2067 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2071 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2074 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2076 BlockDirtyBitmap
*action
;
2078 if (action_check_completion_mode(common
, errp
) < 0) {
2082 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2083 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2086 &state
->aio_context
,
2088 if (!state
->bitmap
) {
2092 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2093 error_setg(errp
, "Cannot modify a frozen bitmap");
2095 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2096 error_setg(errp
, "Cannot clear a disabled bitmap");
2100 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2101 /* AioContext is released in .clean() */
2104 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2106 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2109 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2112 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2114 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2117 hbitmap_free(state
->backup
);
2120 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2122 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2125 if (state
->aio_context
) {
2126 aio_context_release(state
->aio_context
);
2130 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2132 error_setg(errp
, "Transaction aborted using Abort action");
2135 static void abort_commit(BlkActionState
*common
)
2137 g_assert_not_reached(); /* this action never succeeds */
2140 static const BlkActionOps actions
[] = {
2141 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2142 .instance_size
= sizeof(ExternalSnapshotState
),
2143 .prepare
= external_snapshot_prepare
,
2144 .commit
= external_snapshot_commit
,
2145 .abort
= external_snapshot_abort
,
2146 .clean
= external_snapshot_clean
,
2148 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2149 .instance_size
= sizeof(ExternalSnapshotState
),
2150 .prepare
= external_snapshot_prepare
,
2151 .commit
= external_snapshot_commit
,
2152 .abort
= external_snapshot_abort
,
2153 .clean
= external_snapshot_clean
,
2155 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2156 .instance_size
= sizeof(DriveBackupState
),
2157 .prepare
= drive_backup_prepare
,
2158 .abort
= drive_backup_abort
,
2159 .clean
= drive_backup_clean
,
2161 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2162 .instance_size
= sizeof(BlockdevBackupState
),
2163 .prepare
= blockdev_backup_prepare
,
2164 .abort
= blockdev_backup_abort
,
2165 .clean
= blockdev_backup_clean
,
2167 [TRANSACTION_ACTION_KIND_ABORT
] = {
2168 .instance_size
= sizeof(BlkActionState
),
2169 .prepare
= abort_prepare
,
2170 .commit
= abort_commit
,
2172 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2173 .instance_size
= sizeof(InternalSnapshotState
),
2174 .prepare
= internal_snapshot_prepare
,
2175 .abort
= internal_snapshot_abort
,
2176 .clean
= internal_snapshot_clean
,
2178 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2179 .instance_size
= sizeof(BlockDirtyBitmapState
),
2180 .prepare
= block_dirty_bitmap_add_prepare
,
2181 .abort
= block_dirty_bitmap_add_abort
,
2183 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2184 .instance_size
= sizeof(BlockDirtyBitmapState
),
2185 .prepare
= block_dirty_bitmap_clear_prepare
,
2186 .commit
= block_dirty_bitmap_clear_commit
,
2187 .abort
= block_dirty_bitmap_clear_abort
,
2188 .clean
= block_dirty_bitmap_clear_clean
,
2193 * Allocate a TransactionProperties structure if necessary, and fill
2194 * that structure with desired defaults if they are unset.
2196 static TransactionProperties
*get_transaction_properties(
2197 TransactionProperties
*props
)
2200 props
= g_new0(TransactionProperties
, 1);
2203 if (!props
->has_completion_mode
) {
2204 props
->has_completion_mode
= true;
2205 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2212 * 'Atomic' group operations. The operations are performed as a set, and if
2213 * any fail then we roll back all operations in the group.
2215 void qmp_transaction(TransactionActionList
*dev_list
,
2217 struct TransactionProperties
*props
,
2220 TransactionActionList
*dev_entry
= dev_list
;
2221 BlockJobTxn
*block_job_txn
= NULL
;
2222 BlkActionState
*state
, *next
;
2223 Error
*local_err
= NULL
;
2225 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2226 QSIMPLEQ_INIT(&snap_bdrv_states
);
2228 /* Does this transaction get canceled as a group on failure?
2229 * If not, we don't really need to make a BlockJobTxn.
2231 props
= get_transaction_properties(props
);
2232 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2233 block_job_txn
= block_job_txn_new();
2236 /* drain all i/o before any operations */
2239 /* We don't do anything in this loop that commits us to the operations */
2240 while (NULL
!= dev_entry
) {
2241 TransactionAction
*dev_info
= NULL
;
2242 const BlkActionOps
*ops
;
2244 dev_info
= dev_entry
->value
;
2245 dev_entry
= dev_entry
->next
;
2247 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2249 ops
= &actions
[dev_info
->type
];
2250 assert(ops
->instance_size
> 0);
2252 state
= g_malloc0(ops
->instance_size
);
2254 state
->action
= dev_info
;
2255 state
->block_job_txn
= block_job_txn
;
2256 state
->txn_props
= props
;
2257 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2259 state
->ops
->prepare(state
, &local_err
);
2261 error_propagate(errp
, local_err
);
2262 goto delete_and_fail
;
2266 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2267 if (state
->ops
->commit
) {
2268 state
->ops
->commit(state
);
2276 /* failure, and it is all-or-none; roll back all operations */
2277 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2278 if (state
->ops
->abort
) {
2279 state
->ops
->abort(state
);
2283 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2284 if (state
->ops
->clean
) {
2285 state
->ops
->clean(state
);
2290 qapi_free_TransactionProperties(props
);
2292 block_job_txn_unref(block_job_txn
);
2295 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2297 Error
*local_err
= NULL
;
2299 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2301 error_propagate(errp
, local_err
);
2305 qmp_x_blockdev_remove_medium(device
, errp
);
2308 void qmp_block_passwd(bool has_device
, const char *device
,
2309 bool has_node_name
, const char *node_name
,
2310 const char *password
, Error
**errp
)
2312 Error
*local_err
= NULL
;
2313 BlockDriverState
*bs
;
2314 AioContext
*aio_context
;
2316 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2317 has_node_name
? node_name
: NULL
,
2320 error_propagate(errp
, local_err
);
2324 aio_context
= bdrv_get_aio_context(bs
);
2325 aio_context_acquire(aio_context
);
2327 bdrv_add_key(bs
, password
, errp
);
2329 aio_context_release(aio_context
);
2332 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2342 blk
= blk_by_name(device
);
2344 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2345 "Device '%s' not found", device
);
2349 if (!blk_dev_has_removable_media(blk
)) {
2350 error_setg(errp
, "Device '%s' is not removable", device
);
2354 if (!blk_dev_has_tray(blk
)) {
2355 /* Ignore this command on tray-less devices */
2359 if (blk_dev_is_tray_open(blk
)) {
2363 locked
= blk_dev_is_medium_locked(blk
);
2365 blk_dev_eject_request(blk
, force
);
2368 if (!locked
|| force
) {
2369 blk_dev_change_media_cb(blk
, false);
2373 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2377 blk
= blk_by_name(device
);
2379 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2380 "Device '%s' not found", device
);
2384 if (!blk_dev_has_removable_media(blk
)) {
2385 error_setg(errp
, "Device '%s' is not removable", device
);
2389 if (!blk_dev_has_tray(blk
)) {
2390 /* Ignore this command on tray-less devices */
2394 if (!blk_dev_is_tray_open(blk
)) {
2398 blk_dev_change_media_cb(blk
, true);
2401 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2404 BlockDriverState
*bs
;
2405 AioContext
*aio_context
;
2408 blk
= blk_by_name(device
);
2410 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2411 "Device '%s' not found", device
);
2415 /* For BBs without a device, we can exchange the BDS tree at will */
2416 has_device
= blk_get_attached_dev(blk
);
2418 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2419 error_setg(errp
, "Device '%s' is not removable", device
);
2423 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2424 error_setg(errp
, "Tray of device '%s' is not open", device
);
2433 aio_context
= bdrv_get_aio_context(bs
);
2434 aio_context_acquire(aio_context
);
2436 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2442 if (!blk_dev_has_tray(blk
)) {
2443 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2444 * called at all); therefore, the medium needs to be ejected here.
2445 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2446 * value passed here (i.e. false). */
2447 blk_dev_change_media_cb(blk
, false);
2451 aio_context_release(aio_context
);
2454 static void qmp_blockdev_insert_anon_medium(const char *device
,
2455 BlockDriverState
*bs
, Error
**errp
)
2460 blk
= blk_by_name(device
);
2462 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2463 "Device '%s' not found", device
);
2467 /* For BBs without a device, we can exchange the BDS tree at will */
2468 has_device
= blk_get_attached_dev(blk
);
2470 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2471 error_setg(errp
, "Device '%s' is not removable", device
);
2475 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2476 error_setg(errp
, "Tray of device '%s' is not open", device
);
2481 error_setg(errp
, "There already is a medium in device '%s'", device
);
2485 blk_insert_bs(blk
, bs
);
2487 if (!blk_dev_has_tray(blk
)) {
2488 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2489 * called at all); therefore, the medium needs to be pushed into the
2491 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2492 * value passed here (i.e. true). */
2493 blk_dev_change_media_cb(blk
, true);
2497 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2500 BlockDriverState
*bs
;
2502 bs
= bdrv_find_node(node_name
);
2504 error_setg(errp
, "Node '%s' not found", node_name
);
2509 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2514 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2517 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2518 bool has_format
, const char *format
,
2520 BlockdevChangeReadOnlyMode read_only
,
2524 BlockDriverState
*medium_bs
= NULL
;
2525 int bdrv_flags
, ret
;
2527 QDict
*options
= NULL
;
2530 blk
= blk_by_name(device
);
2532 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2533 "Device '%s' not found", device
);
2538 blk_update_root_state(blk
);
2541 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2542 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2545 /* Must open the image in writeback mode as long as no BlockBackend is
2546 * attached. The right mode can be set as the final step after changing the
2548 writethrough
= !(bdrv_flags
& BDRV_O_CACHE_WB
);
2549 bdrv_flags
|= BDRV_O_CACHE_WB
;
2551 if (!has_read_only
) {
2552 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2555 switch (read_only
) {
2556 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2559 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2560 bdrv_flags
&= ~BDRV_O_RDWR
;
2563 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2564 bdrv_flags
|= BDRV_O_RDWR
;
2572 options
= qdict_new();
2573 qdict_put(options
, "driver", qstring_from_str(format
));
2577 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2582 blk_apply_root_state(blk
, medium_bs
);
2584 bdrv_add_key(medium_bs
, NULL
, &err
);
2586 error_propagate(errp
, err
);
2590 qmp_blockdev_open_tray(device
, false, false, &err
);
2592 error_propagate(errp
, err
);
2596 qmp_x_blockdev_remove_medium(device
, &err
);
2598 error_propagate(errp
, err
);
2602 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2604 error_propagate(errp
, err
);
2608 bdrv_set_enable_write_cache(medium_bs
, !writethrough
);
2610 qmp_blockdev_close_tray(device
, errp
);
2613 /* If the medium has been inserted, the device has its own reference, so
2614 * ours must be relinquished; and if it has not been inserted successfully,
2615 * the reference must be relinquished anyway */
2616 bdrv_unref(medium_bs
);
2619 /* throttling disk I/O limits */
2620 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2627 bool has_bps_rd_max
,
2629 bool has_bps_wr_max
,
2633 bool has_iops_rd_max
,
2634 int64_t iops_rd_max
,
2635 bool has_iops_wr_max
,
2636 int64_t iops_wr_max
,
2637 bool has_bps_max_length
,
2638 int64_t bps_max_length
,
2639 bool has_bps_rd_max_length
,
2640 int64_t bps_rd_max_length
,
2641 bool has_bps_wr_max_length
,
2642 int64_t bps_wr_max_length
,
2643 bool has_iops_max_length
,
2644 int64_t iops_max_length
,
2645 bool has_iops_rd_max_length
,
2646 int64_t iops_rd_max_length
,
2647 bool has_iops_wr_max_length
,
2648 int64_t iops_wr_max_length
,
2652 const char *group
, Error
**errp
)
2655 BlockDriverState
*bs
;
2657 AioContext
*aio_context
;
2659 blk
= blk_by_name(device
);
2661 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2662 "Device '%s' not found", device
);
2666 aio_context
= blk_get_aio_context(blk
);
2667 aio_context_acquire(aio_context
);
2671 error_setg(errp
, "Device '%s' has no medium", device
);
2675 throttle_config_init(&cfg
);
2676 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2677 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2678 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2680 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2681 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2682 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2685 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2687 if (has_bps_rd_max
) {
2688 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2690 if (has_bps_wr_max
) {
2691 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2694 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2696 if (has_iops_rd_max
) {
2697 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2699 if (has_iops_wr_max
) {
2700 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2703 if (has_bps_max_length
) {
2704 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= bps_max_length
;
2706 if (has_bps_rd_max_length
) {
2707 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= bps_rd_max_length
;
2709 if (has_bps_wr_max_length
) {
2710 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= bps_wr_max_length
;
2712 if (has_iops_max_length
) {
2713 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= iops_max_length
;
2715 if (has_iops_rd_max_length
) {
2716 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= iops_rd_max_length
;
2718 if (has_iops_wr_max_length
) {
2719 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= iops_wr_max_length
;
2722 if (has_iops_size
) {
2723 cfg
.op_size
= iops_size
;
2726 if (!throttle_is_valid(&cfg
, errp
)) {
2730 if (throttle_enabled(&cfg
)) {
2731 /* Enable I/O limits if they're not enabled yet, otherwise
2732 * just update the throttling group. */
2733 if (!bs
->throttle_state
) {
2734 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2735 } else if (has_group
) {
2736 bdrv_io_limits_update_group(bs
, group
);
2738 /* Set the new throttling configuration */
2739 bdrv_set_io_limits(bs
, &cfg
);
2740 } else if (bs
->throttle_state
) {
2741 /* If all throttling settings are set to 0, disable I/O limits */
2742 bdrv_io_limits_disable(bs
);
2746 aio_context_release(aio_context
);
2749 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2750 bool has_granularity
, uint32_t granularity
,
2753 AioContext
*aio_context
;
2754 BlockDriverState
*bs
;
2756 if (!name
|| name
[0] == '\0') {
2757 error_setg(errp
, "Bitmap name cannot be empty");
2761 bs
= bdrv_lookup_bs(node
, node
, errp
);
2766 aio_context
= bdrv_get_aio_context(bs
);
2767 aio_context_acquire(aio_context
);
2769 if (has_granularity
) {
2770 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2771 error_setg(errp
, "Granularity must be power of 2 "
2772 "and at least 512");
2776 /* Default to cluster size, if available: */
2777 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2780 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2783 aio_context_release(aio_context
);
2786 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2789 AioContext
*aio_context
;
2790 BlockDriverState
*bs
;
2791 BdrvDirtyBitmap
*bitmap
;
2793 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2794 if (!bitmap
|| !bs
) {
2798 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2800 "Bitmap '%s' is currently frozen and cannot be removed",
2804 bdrv_dirty_bitmap_make_anon(bitmap
);
2805 bdrv_release_dirty_bitmap(bs
, bitmap
);
2808 aio_context_release(aio_context
);
2812 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2813 * immediately after a full backup operation.
2815 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2818 AioContext
*aio_context
;
2819 BdrvDirtyBitmap
*bitmap
;
2820 BlockDriverState
*bs
;
2822 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2823 if (!bitmap
|| !bs
) {
2827 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2829 "Bitmap '%s' is currently frozen and cannot be modified",
2832 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2834 "Bitmap '%s' is currently disabled and cannot be cleared",
2839 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2842 aio_context_release(aio_context
);
2845 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2847 const char *id
= qdict_get_str(qdict
, "id");
2849 BlockDriverState
*bs
;
2850 AioContext
*aio_context
;
2851 Error
*local_err
= NULL
;
2853 bs
= bdrv_find_node(id
);
2855 qmp_x_blockdev_del(false, NULL
, true, id
, &local_err
);
2857 error_report_err(local_err
);
2862 blk
= blk_by_name(id
);
2864 error_report("Device '%s' not found", id
);
2868 if (!blk_legacy_dinfo(blk
)) {
2869 error_report("Deleting device added with blockdev-add"
2870 " is not supported");
2874 aio_context
= blk_get_aio_context(blk
);
2875 aio_context_acquire(aio_context
);
2879 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2880 error_report_err(local_err
);
2881 aio_context_release(aio_context
);
2888 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2889 monitor_remove_blk(blk
);
2891 /* If this BlockBackend has a device attached to it, its refcount will be
2892 * decremented when the device is removed; otherwise we have to do so here.
2894 if (blk_get_attached_dev(blk
)) {
2895 /* Further I/O must not pause the guest */
2896 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2897 BLOCKDEV_ON_ERROR_REPORT
);
2902 aio_context_release(aio_context
);
2905 void qmp_block_resize(bool has_device
, const char *device
,
2906 bool has_node_name
, const char *node_name
,
2907 int64_t size
, Error
**errp
)
2909 Error
*local_err
= NULL
;
2910 BlockDriverState
*bs
;
2911 AioContext
*aio_context
;
2914 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2915 has_node_name
? node_name
: NULL
,
2918 error_propagate(errp
, local_err
);
2922 aio_context
= bdrv_get_aio_context(bs
);
2923 aio_context_acquire(aio_context
);
2925 if (!bdrv_is_first_non_filter(bs
)) {
2926 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2931 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2935 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2936 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2940 /* complete all in-flight operations before resizing the device */
2943 ret
= bdrv_truncate(bs
, size
);
2948 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2951 error_setg(errp
, QERR_UNSUPPORTED
);
2954 error_setg(errp
, "Device '%s' is read only", device
);
2957 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2960 error_setg_errno(errp
, -ret
, "Could not resize");
2965 aio_context_release(aio_context
);
2968 static void block_job_cb(void *opaque
, int ret
)
2970 /* Note that this function may be executed from another AioContext besides
2971 * the QEMU main loop. If you need to access anything that assumes the
2972 * QEMU global mutex, use a BH or introduce a mutex.
2975 BlockDriverState
*bs
= opaque
;
2976 const char *msg
= NULL
;
2978 trace_block_job_cb(bs
, bs
->job
, ret
);
2983 msg
= strerror(-ret
);
2986 if (block_job_is_cancelled(bs
->job
)) {
2987 block_job_event_cancelled(bs
->job
);
2989 block_job_event_completed(bs
->job
, msg
);
2993 void qmp_block_stream(const char *device
,
2994 bool has_base
, const char *base
,
2995 bool has_backing_file
, const char *backing_file
,
2996 bool has_speed
, int64_t speed
,
2997 bool has_on_error
, BlockdevOnError on_error
,
3001 BlockDriverState
*bs
;
3002 BlockDriverState
*base_bs
= NULL
;
3003 AioContext
*aio_context
;
3004 Error
*local_err
= NULL
;
3005 const char *base_name
= NULL
;
3007 if (!has_on_error
) {
3008 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3011 blk
= blk_by_name(device
);
3013 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3014 "Device '%s' not found", device
);
3018 aio_context
= blk_get_aio_context(blk
);
3019 aio_context_acquire(aio_context
);
3021 if (!blk_is_available(blk
)) {
3022 error_setg(errp
, "Device '%s' has no medium", device
);
3027 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3032 base_bs
= bdrv_find_backing_image(bs
, base
);
3033 if (base_bs
== NULL
) {
3034 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3037 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3041 /* if we are streaming the entire chain, the result will have no backing
3042 * file, and specifying one is therefore an error */
3043 if (base_bs
== NULL
&& has_backing_file
) {
3044 error_setg(errp
, "backing file specified, but streaming the "
3049 /* backing_file string overrides base bs filename */
3050 base_name
= has_backing_file
? backing_file
: base_name
;
3052 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
3053 on_error
, block_job_cb
, bs
, &local_err
);
3055 error_propagate(errp
, local_err
);
3059 trace_qmp_block_stream(bs
, bs
->job
);
3062 aio_context_release(aio_context
);
3065 void qmp_block_commit(const char *device
,
3066 bool has_base
, const char *base
,
3067 bool has_top
, const char *top
,
3068 bool has_backing_file
, const char *backing_file
,
3069 bool has_speed
, int64_t speed
,
3073 BlockDriverState
*bs
;
3074 BlockDriverState
*base_bs
, *top_bs
;
3075 AioContext
*aio_context
;
3076 Error
*local_err
= NULL
;
3077 /* This will be part of the QMP command, if/when the
3078 * BlockdevOnError change for blkmirror makes it in
3080 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3087 * libvirt relies on the DeviceNotFound error class in order to probe for
3088 * live commit feature versions; for this to work, we must make sure to
3089 * perform the device lookup before any generic errors that may occur in a
3090 * scenario in which all optional arguments are omitted. */
3091 blk
= blk_by_name(device
);
3093 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3094 "Device '%s' not found", device
);
3098 aio_context
= blk_get_aio_context(blk
);
3099 aio_context_acquire(aio_context
);
3101 if (!blk_is_available(blk
)) {
3102 error_setg(errp
, "Device '%s' has no medium", device
);
3107 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3111 /* default top_bs is the active layer */
3114 if (has_top
&& top
) {
3115 if (strcmp(bs
->filename
, top
) != 0) {
3116 top_bs
= bdrv_find_backing_image(bs
, top
);
3120 if (top_bs
== NULL
) {
3121 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3125 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3127 if (has_base
&& base
) {
3128 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3130 base_bs
= bdrv_find_base(top_bs
);
3133 if (base_bs
== NULL
) {
3134 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3138 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3140 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3144 /* Do not allow attempts to commit an image into itself */
3145 if (top_bs
== base_bs
) {
3146 error_setg(errp
, "cannot commit an image into itself");
3151 if (has_backing_file
) {
3152 error_setg(errp
, "'backing-file' specified,"
3153 " but 'top' is the active layer");
3156 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3159 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3160 has_backing_file
? backing_file
: NULL
, &local_err
);
3162 if (local_err
!= NULL
) {
3163 error_propagate(errp
, local_err
);
3168 aio_context_release(aio_context
);
3171 static void do_drive_backup(const char *device
, const char *target
,
3172 bool has_format
, const char *format
,
3173 enum MirrorSyncMode sync
,
3174 bool has_mode
, enum NewImageMode mode
,
3175 bool has_speed
, int64_t speed
,
3176 bool has_bitmap
, const char *bitmap
,
3177 bool has_on_source_error
,
3178 BlockdevOnError on_source_error
,
3179 bool has_on_target_error
,
3180 BlockdevOnError on_target_error
,
3181 BlockJobTxn
*txn
, Error
**errp
)
3184 BlockDriverState
*bs
;
3185 BlockDriverState
*target_bs
;
3186 BlockDriverState
*source
= NULL
;
3187 BdrvDirtyBitmap
*bmap
= NULL
;
3188 AioContext
*aio_context
;
3189 QDict
*options
= NULL
;
3190 Error
*local_err
= NULL
;
3198 if (!has_on_source_error
) {
3199 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3201 if (!has_on_target_error
) {
3202 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3205 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3208 blk
= blk_by_name(device
);
3210 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3211 "Device '%s' not found", device
);
3215 aio_context
= blk_get_aio_context(blk
);
3216 aio_context_acquire(aio_context
);
3218 /* Although backup_run has this check too, we need to use bs->drv below, so
3219 * do an early check redundantly. */
3220 if (!blk_is_available(blk
)) {
3221 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3227 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3230 /* Early check to avoid creating target */
3231 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3235 flags
= bs
->open_flags
| BDRV_O_CACHE_WB
| BDRV_O_RDWR
;
3237 /* See if we have a backing HD we can use to create our new image
3239 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3240 source
= backing_bs(bs
);
3242 sync
= MIRROR_SYNC_MODE_FULL
;
3245 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3249 size
= bdrv_getlength(bs
);
3251 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3255 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3258 bdrv_img_create(target
, format
, source
->filename
,
3259 source
->drv
->format_name
, NULL
,
3260 size
, flags
, &local_err
, false);
3262 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3263 size
, flags
, &local_err
, false);
3268 error_propagate(errp
, local_err
);
3273 options
= qdict_new();
3274 qdict_put(options
, "driver", qstring_from_str(format
));
3278 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3280 error_propagate(errp
, local_err
);
3284 bdrv_set_aio_context(target_bs
, aio_context
);
3287 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3289 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3290 bdrv_unref(target_bs
);
3295 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3296 on_source_error
, on_target_error
,
3297 block_job_cb
, bs
, txn
, &local_err
);
3298 if (local_err
!= NULL
) {
3299 bdrv_unref(target_bs
);
3300 error_propagate(errp
, local_err
);
3305 aio_context_release(aio_context
);
3308 void qmp_drive_backup(const char *device
, const char *target
,
3309 bool has_format
, const char *format
,
3310 enum MirrorSyncMode sync
,
3311 bool has_mode
, enum NewImageMode mode
,
3312 bool has_speed
, int64_t speed
,
3313 bool has_bitmap
, const char *bitmap
,
3314 bool has_on_source_error
, BlockdevOnError on_source_error
,
3315 bool has_on_target_error
, BlockdevOnError on_target_error
,
3318 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3319 has_mode
, mode
, has_speed
, speed
,
3321 has_on_source_error
, on_source_error
,
3322 has_on_target_error
, on_target_error
,
3326 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3328 return bdrv_named_nodes_list(errp
);
3331 void do_blockdev_backup(const char *device
, const char *target
,
3332 enum MirrorSyncMode sync
,
3333 bool has_speed
, int64_t speed
,
3334 bool has_on_source_error
,
3335 BlockdevOnError on_source_error
,
3336 bool has_on_target_error
,
3337 BlockdevOnError on_target_error
,
3338 BlockJobTxn
*txn
, Error
**errp
)
3340 BlockBackend
*blk
, *target_blk
;
3341 BlockDriverState
*bs
;
3342 BlockDriverState
*target_bs
;
3343 Error
*local_err
= NULL
;
3344 AioContext
*aio_context
;
3349 if (!has_on_source_error
) {
3350 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3352 if (!has_on_target_error
) {
3353 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3356 blk
= blk_by_name(device
);
3358 error_setg(errp
, "Device '%s' not found", device
);
3362 aio_context
= blk_get_aio_context(blk
);
3363 aio_context_acquire(aio_context
);
3365 if (!blk_is_available(blk
)) {
3366 error_setg(errp
, "Device '%s' has no medium", device
);
3371 target_blk
= blk_by_name(target
);
3373 error_setg(errp
, "Device '%s' not found", target
);
3377 if (!blk_is_available(target_blk
)) {
3378 error_setg(errp
, "Device '%s' has no medium", target
);
3381 target_bs
= blk_bs(target_blk
);
3383 bdrv_ref(target_bs
);
3384 bdrv_set_aio_context(target_bs
, aio_context
);
3385 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3386 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3387 if (local_err
!= NULL
) {
3388 bdrv_unref(target_bs
);
3389 error_propagate(errp
, local_err
);
3392 aio_context_release(aio_context
);
3395 void qmp_blockdev_backup(const char *device
, const char *target
,
3396 enum MirrorSyncMode sync
,
3397 bool has_speed
, int64_t speed
,
3398 bool has_on_source_error
,
3399 BlockdevOnError on_source_error
,
3400 bool has_on_target_error
,
3401 BlockdevOnError on_target_error
,
3404 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3405 has_on_source_error
, on_source_error
,
3406 has_on_target_error
, on_target_error
,
3410 /* Parameter check and block job starting for drive mirroring.
3411 * Caller should hold @device and @target's aio context (must be the same).
3413 static void blockdev_mirror_common(BlockDriverState
*bs
,
3414 BlockDriverState
*target
,
3415 bool has_replaces
, const char *replaces
,
3416 enum MirrorSyncMode sync
,
3417 bool has_speed
, int64_t speed
,
3418 bool has_granularity
, uint32_t granularity
,
3419 bool has_buf_size
, int64_t buf_size
,
3420 bool has_on_source_error
,
3421 BlockdevOnError on_source_error
,
3422 bool has_on_target_error
,
3423 BlockdevOnError on_target_error
,
3424 bool has_unmap
, bool unmap
,
3431 if (!has_on_source_error
) {
3432 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3434 if (!has_on_target_error
) {
3435 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3437 if (!has_granularity
) {
3440 if (!has_buf_size
) {
3447 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3448 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3449 "a value in range [512B, 64MB]");
3452 if (granularity
& (granularity
- 1)) {
3453 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3458 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3461 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3465 error_setg(errp
, "Cannot mirror to an attached block device");
3469 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3470 sync
= MIRROR_SYNC_MODE_FULL
;
3473 /* pass the node name to replace to mirror start since it's loose coupling
3474 * and will allow to check whether the node still exist at mirror completion
3476 mirror_start(bs
, target
,
3477 has_replaces
? replaces
: NULL
,
3478 speed
, granularity
, buf_size
, sync
,
3479 on_source_error
, on_target_error
, unmap
,
3480 block_job_cb
, bs
, errp
);
3483 void qmp_drive_mirror(const char *device
, const char *target
,
3484 bool has_format
, const char *format
,
3485 bool has_node_name
, const char *node_name
,
3486 bool has_replaces
, const char *replaces
,
3487 enum MirrorSyncMode sync
,
3488 bool has_mode
, enum NewImageMode mode
,
3489 bool has_speed
, int64_t speed
,
3490 bool has_granularity
, uint32_t granularity
,
3491 bool has_buf_size
, int64_t buf_size
,
3492 bool has_on_source_error
, BlockdevOnError on_source_error
,
3493 bool has_on_target_error
, BlockdevOnError on_target_error
,
3494 bool has_unmap
, bool unmap
,
3497 BlockDriverState
*bs
;
3499 BlockDriverState
*source
, *target_bs
;
3500 AioContext
*aio_context
;
3501 Error
*local_err
= NULL
;
3502 QDict
*options
= NULL
;
3507 blk
= blk_by_name(device
);
3509 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3510 "Device '%s' not found", device
);
3514 aio_context
= blk_get_aio_context(blk
);
3515 aio_context_acquire(aio_context
);
3517 if (!blk_is_available(blk
)) {
3518 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3523 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3527 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3530 flags
= bs
->open_flags
| BDRV_O_CACHE_WB
| BDRV_O_RDWR
;
3531 source
= backing_bs(bs
);
3532 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3533 sync
= MIRROR_SYNC_MODE_FULL
;
3535 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3539 size
= bdrv_getlength(bs
);
3541 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3546 BlockDriverState
*to_replace_bs
;
3547 AioContext
*replace_aio_context
;
3548 int64_t replace_size
;
3550 if (!has_node_name
) {
3551 error_setg(errp
, "a node-name must be provided when replacing a"
3552 " named node of the graph");
3556 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3558 if (!to_replace_bs
) {
3559 error_propagate(errp
, local_err
);
3563 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3564 aio_context_acquire(replace_aio_context
);
3565 replace_size
= bdrv_getlength(to_replace_bs
);
3566 aio_context_release(replace_aio_context
);
3568 if (size
!= replace_size
) {
3569 error_setg(errp
, "cannot replace image with a mirror image of "
3575 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3576 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3578 /* create new image w/o backing file */
3580 bdrv_img_create(target
, format
,
3581 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3584 case NEW_IMAGE_MODE_EXISTING
:
3586 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3587 /* create new image with backing file */
3588 bdrv_img_create(target
, format
,
3590 source
->drv
->format_name
,
3591 NULL
, size
, flags
, &local_err
, false);
3599 error_propagate(errp
, local_err
);
3603 options
= qdict_new();
3604 if (has_node_name
) {
3605 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3608 qdict_put(options
, "driver", qstring_from_str(format
));
3611 /* Mirroring takes care of copy-on-write using the source's backing
3615 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3616 flags
| BDRV_O_NO_BACKING
, &local_err
);
3618 error_propagate(errp
, local_err
);
3622 bdrv_set_aio_context(target_bs
, aio_context
);
3624 blockdev_mirror_common(bs
, target_bs
,
3625 has_replaces
, replaces
, sync
,
3627 has_granularity
, granularity
,
3628 has_buf_size
, buf_size
,
3629 has_on_source_error
, on_source_error
,
3630 has_on_target_error
, on_target_error
,
3634 error_propagate(errp
, local_err
);
3635 bdrv_unref(target_bs
);
3638 aio_context_release(aio_context
);
3641 void qmp_blockdev_mirror(const char *device
, const char *target
,
3642 bool has_replaces
, const char *replaces
,
3643 MirrorSyncMode sync
,
3644 bool has_speed
, int64_t speed
,
3645 bool has_granularity
, uint32_t granularity
,
3646 bool has_buf_size
, int64_t buf_size
,
3647 bool has_on_source_error
,
3648 BlockdevOnError on_source_error
,
3649 bool has_on_target_error
,
3650 BlockdevOnError on_target_error
,
3653 BlockDriverState
*bs
;
3655 BlockDriverState
*target_bs
;
3656 AioContext
*aio_context
;
3657 Error
*local_err
= NULL
;
3659 blk
= blk_by_name(device
);
3661 error_setg(errp
, "Device '%s' not found", device
);
3667 error_setg(errp
, "Device '%s' has no media", device
);
3671 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3676 aio_context
= bdrv_get_aio_context(bs
);
3677 aio_context_acquire(aio_context
);
3679 bdrv_ref(target_bs
);
3680 bdrv_set_aio_context(target_bs
, aio_context
);
3682 blockdev_mirror_common(bs
, target_bs
,
3683 has_replaces
, replaces
, sync
,
3685 has_granularity
, granularity
,
3686 has_buf_size
, buf_size
,
3687 has_on_source_error
, on_source_error
,
3688 has_on_target_error
, on_target_error
,
3692 error_propagate(errp
, local_err
);
3693 bdrv_unref(target_bs
);
3696 aio_context_release(aio_context
);
3699 /* Get the block job for a given device name and acquire its AioContext */
3700 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3704 BlockDriverState
*bs
;
3706 *aio_context
= NULL
;
3708 blk
= blk_by_name(device
);
3713 *aio_context
= blk_get_aio_context(blk
);
3714 aio_context_acquire(*aio_context
);
3716 if (!blk_is_available(blk
)) {
3728 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3729 "No active block job on device '%s'", device
);
3731 aio_context_release(*aio_context
);
3732 *aio_context
= NULL
;
3737 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3739 AioContext
*aio_context
;
3740 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3746 block_job_set_speed(job
, speed
, errp
);
3747 aio_context_release(aio_context
);
3750 void qmp_block_job_cancel(const char *device
,
3751 bool has_force
, bool force
, Error
**errp
)
3753 AioContext
*aio_context
;
3754 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3764 if (job
->user_paused
&& !force
) {
3765 error_setg(errp
, "The block job for device '%s' is currently paused",
3770 trace_qmp_block_job_cancel(job
);
3771 block_job_cancel(job
);
3773 aio_context_release(aio_context
);
3776 void qmp_block_job_pause(const char *device
, Error
**errp
)
3778 AioContext
*aio_context
;
3779 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3781 if (!job
|| job
->user_paused
) {
3785 job
->user_paused
= true;
3786 trace_qmp_block_job_pause(job
);
3787 block_job_pause(job
);
3788 aio_context_release(aio_context
);
3791 void qmp_block_job_resume(const char *device
, Error
**errp
)
3793 AioContext
*aio_context
;
3794 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3796 if (!job
|| !job
->user_paused
) {
3800 job
->user_paused
= false;
3801 trace_qmp_block_job_resume(job
);
3802 block_job_resume(job
);
3803 aio_context_release(aio_context
);
3806 void qmp_block_job_complete(const char *device
, Error
**errp
)
3808 AioContext
*aio_context
;
3809 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3815 trace_qmp_block_job_complete(job
);
3816 block_job_complete(job
, errp
);
3817 aio_context_release(aio_context
);
3820 void qmp_change_backing_file(const char *device
,
3821 const char *image_node_name
,
3822 const char *backing_file
,
3826 BlockDriverState
*bs
= NULL
;
3827 AioContext
*aio_context
;
3828 BlockDriverState
*image_bs
= NULL
;
3829 Error
*local_err
= NULL
;
3834 blk
= blk_by_name(device
);
3836 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3837 "Device '%s' not found", device
);
3841 aio_context
= blk_get_aio_context(blk
);
3842 aio_context_acquire(aio_context
);
3844 if (!blk_is_available(blk
)) {
3845 error_setg(errp
, "Device '%s' has no medium", device
);
3850 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3852 error_propagate(errp
, local_err
);
3857 error_setg(errp
, "image file not found");
3861 if (bdrv_find_base(image_bs
) == image_bs
) {
3862 error_setg(errp
, "not allowing backing file change on an image "
3863 "without a backing file");
3867 /* even though we are not necessarily operating on bs, we need it to
3868 * determine if block ops are currently prohibited on the chain */
3869 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3873 /* final sanity check */
3874 if (!bdrv_chain_contains(bs
, image_bs
)) {
3875 error_setg(errp
, "'%s' and image file are not in the same chain",
3880 /* if not r/w, reopen to make r/w */
3881 open_flags
= image_bs
->open_flags
;
3882 ro
= bdrv_is_read_only(image_bs
);
3885 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3887 error_propagate(errp
, local_err
);
3892 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3893 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3896 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3898 /* don't exit here, so we can try to restore open flags if
3903 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3905 error_propagate(errp
, local_err
); /* will preserve prior errp */
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 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3947 BlockDriverState
*bs
;
3948 BlockBackend
*blk
= NULL
;
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(qmp_output_get_visitor(ov
), NULL
, &options
,
3971 error_propagate(errp
, local_err
);
3975 obj
= qmp_output_get_qobject(ov
);
3976 qdict
= qobject_to_qdict(obj
);
3978 qdict_flatten(qdict
);
3980 if (options
->has_id
) {
3981 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3983 error_propagate(errp
, local_err
);
3989 if (!qdict_get_try_str(qdict
, "node-name")) {
3990 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3995 bs
= bds_tree_init(qdict
, errp
);
4000 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4003 if (bs
&& bdrv_key_required(bs
)) {
4005 monitor_remove_blk(blk
);
4008 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4011 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
4016 qmp_output_visitor_cleanup(ov
);
4019 void qmp_x_blockdev_del(bool has_id
, const char *id
,
4020 bool has_node_name
, const char *node_name
, Error
**errp
)
4022 AioContext
*aio_context
;
4024 BlockDriverState
*bs
;
4026 if (has_id
&& has_node_name
) {
4027 error_setg(errp
, "Only one of id and node-name must be specified");
4029 } else if (!has_id
&& !has_node_name
) {
4030 error_setg(errp
, "No block device specified");
4035 /* blk_by_name() never returns a BB that is not owned by the monitor */
4036 blk
= blk_by_name(id
);
4038 error_setg(errp
, "Cannot find block backend %s", id
);
4041 if (blk_get_refcnt(blk
) > 1) {
4042 error_setg(errp
, "Block backend %s is in use", id
);
4046 aio_context
= blk_get_aio_context(blk
);
4048 bs
= bdrv_find_node(node_name
);
4050 error_setg(errp
, "Cannot find node %s", node_name
);
4055 error_setg(errp
, "Node %s is in use by %s",
4056 node_name
, blk_name(blk
));
4059 aio_context
= bdrv_get_aio_context(bs
);
4062 aio_context_acquire(aio_context
);
4065 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4069 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
4070 error_setg(errp
, "Node %s is not owned by the monitor",
4075 if (bs
->refcnt
> 1) {
4076 error_setg(errp
, "Block device %s is in use",
4077 bdrv_get_device_or_node_name(bs
));
4083 monitor_remove_blk(blk
);
4086 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4091 aio_context_release(aio_context
);
4094 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4096 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4097 BlockDriverState
*bs
;
4099 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4100 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
4102 aio_context_acquire(aio_context
);
4105 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4106 elem
->value
= block_job_query(bs
->job
);
4108 p_next
= &elem
->next
;
4111 aio_context_release(aio_context
);
4117 QemuOptsList qemu_common_drive_opts
= {
4119 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4123 .type
= QEMU_OPT_BOOL
,
4124 .help
= "enable/disable snapshot mode",
4127 .type
= QEMU_OPT_STRING
,
4128 .help
= "discard operation (ignore/off, unmap/on)",
4131 .type
= QEMU_OPT_STRING
,
4132 .help
= "host AIO implementation (threads, native)",
4135 .type
= QEMU_OPT_STRING
,
4136 .help
= "disk format (raw, qcow2, ...)",
4139 .type
= QEMU_OPT_STRING
,
4140 .help
= "read error action",
4143 .type
= QEMU_OPT_STRING
,
4144 .help
= "write error action",
4146 .name
= "read-only",
4147 .type
= QEMU_OPT_BOOL
,
4148 .help
= "open drive file as read-only",
4150 .name
= "throttling.iops-total",
4151 .type
= QEMU_OPT_NUMBER
,
4152 .help
= "limit total I/O operations per second",
4154 .name
= "throttling.iops-read",
4155 .type
= QEMU_OPT_NUMBER
,
4156 .help
= "limit read operations per second",
4158 .name
= "throttling.iops-write",
4159 .type
= QEMU_OPT_NUMBER
,
4160 .help
= "limit write operations per second",
4162 .name
= "throttling.bps-total",
4163 .type
= QEMU_OPT_NUMBER
,
4164 .help
= "limit total bytes per second",
4166 .name
= "throttling.bps-read",
4167 .type
= QEMU_OPT_NUMBER
,
4168 .help
= "limit read bytes per second",
4170 .name
= "throttling.bps-write",
4171 .type
= QEMU_OPT_NUMBER
,
4172 .help
= "limit write bytes per second",
4174 .name
= "throttling.iops-total-max",
4175 .type
= QEMU_OPT_NUMBER
,
4176 .help
= "I/O operations burst",
4178 .name
= "throttling.iops-read-max",
4179 .type
= QEMU_OPT_NUMBER
,
4180 .help
= "I/O operations read burst",
4182 .name
= "throttling.iops-write-max",
4183 .type
= QEMU_OPT_NUMBER
,
4184 .help
= "I/O operations write burst",
4186 .name
= "throttling.bps-total-max",
4187 .type
= QEMU_OPT_NUMBER
,
4188 .help
= "total bytes burst",
4190 .name
= "throttling.bps-read-max",
4191 .type
= QEMU_OPT_NUMBER
,
4192 .help
= "total bytes read burst",
4194 .name
= "throttling.bps-write-max",
4195 .type
= QEMU_OPT_NUMBER
,
4196 .help
= "total bytes write burst",
4198 .name
= "throttling.iops-total-max-length",
4199 .type
= QEMU_OPT_NUMBER
,
4200 .help
= "length of the iops-total-max burst period, in seconds",
4202 .name
= "throttling.iops-read-max-length",
4203 .type
= QEMU_OPT_NUMBER
,
4204 .help
= "length of the iops-read-max burst period, in seconds",
4206 .name
= "throttling.iops-write-max-length",
4207 .type
= QEMU_OPT_NUMBER
,
4208 .help
= "length of the iops-write-max burst period, in seconds",
4210 .name
= "throttling.bps-total-max-length",
4211 .type
= QEMU_OPT_NUMBER
,
4212 .help
= "length of the bps-total-max burst period, in seconds",
4214 .name
= "throttling.bps-read-max-length",
4215 .type
= QEMU_OPT_NUMBER
,
4216 .help
= "length of the bps-read-max burst period, in seconds",
4218 .name
= "throttling.bps-write-max-length",
4219 .type
= QEMU_OPT_NUMBER
,
4220 .help
= "length of the bps-write-max burst period, in seconds",
4222 .name
= "throttling.iops-size",
4223 .type
= QEMU_OPT_NUMBER
,
4224 .help
= "when limiting by iops max size of an I/O in bytes",
4226 .name
= "throttling.group",
4227 .type
= QEMU_OPT_STRING
,
4228 .help
= "name of the block throttling group",
4230 .name
= "copy-on-read",
4231 .type
= QEMU_OPT_BOOL
,
4232 .help
= "copy read data from backing file into image file",
4234 .name
= "detect-zeroes",
4235 .type
= QEMU_OPT_STRING
,
4236 .help
= "try to optimize zero writes (off, on, unmap)",
4238 .name
= "stats-account-invalid",
4239 .type
= QEMU_OPT_BOOL
,
4240 .help
= "whether to account for invalid I/O operations "
4241 "in the statistics",
4243 .name
= "stats-account-failed",
4244 .type
= QEMU_OPT_BOOL
,
4245 .help
= "whether to account for failed I/O operations "
4246 "in the statistics",
4248 { /* end of list */ }
4252 static QemuOptsList qemu_root_bds_opts
= {
4254 .head
= QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts
.head
),
4258 .type
= QEMU_OPT_STRING
,
4259 .help
= "discard operation (ignore/off, unmap/on)",
4262 .type
= QEMU_OPT_STRING
,
4263 .help
= "host AIO implementation (threads, native)",
4265 .name
= "read-only",
4266 .type
= QEMU_OPT_BOOL
,
4267 .help
= "open drive file as read-only",
4269 .name
= "copy-on-read",
4270 .type
= QEMU_OPT_BOOL
,
4271 .help
= "copy read data from backing file into image file",
4273 .name
= "detect-zeroes",
4274 .type
= QEMU_OPT_STRING
,
4275 .help
= "try to optimize zero writes (off, on, unmap)",
4277 { /* end of list */ }
4281 QemuOptsList qemu_drive_opts
= {
4283 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4286 * no elements => accept any params
4287 * validation will happen later
4289 { /* end of list */ }