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"
54 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
55 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
57 static const char *const if_name
[IF_COUNT
] = {
61 [IF_FLOPPY
] = "floppy",
62 [IF_PFLASH
] = "pflash",
65 [IF_VIRTIO
] = "virtio",
69 static int if_max_devs
[IF_COUNT
] = {
71 * Do not change these numbers! They govern how drive option
72 * index maps to unit and bus. That mapping is ABI.
74 * All controllers used to imlement if=T drives need to support
75 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
76 * Otherwise, some index values map to "impossible" bus, unit
79 * For instance, if you change [IF_SCSI] to 255, -drive
80 * if=scsi,index=12 no longer means bus=1,unit=5, but
81 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
82 * the drive can't be set up. Regression.
89 * Boards may call this to offer board-by-board overrides
90 * of the default, global values.
92 void override_max_devs(BlockInterfaceType type
, int max_devs
)
101 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
102 dinfo
= blk_legacy_dinfo(blk
);
103 if (dinfo
->type
== type
) {
104 fprintf(stderr
, "Cannot override units-per-bus property of"
105 " the %s interface, because a drive of that type has"
106 " already been added.\n", if_name
[type
]);
107 g_assert_not_reached();
111 if_max_devs
[type
] = max_devs
;
115 * We automatically delete the drive when a device using it gets
116 * unplugged. Questionable feature, but we can't just drop it.
117 * Device models call blockdev_mark_auto_del() to schedule the
118 * automatic deletion, and generic qdev code calls blockdev_auto_del()
119 * when deletion is actually safe.
121 void blockdev_mark_auto_del(BlockBackend
*blk
)
123 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
124 BlockDriverState
*bs
= blk_bs(blk
);
125 AioContext
*aio_context
;
132 aio_context
= bdrv_get_aio_context(bs
);
133 aio_context_acquire(aio_context
);
136 block_job_cancel(bs
->job
);
139 aio_context_release(aio_context
);
145 void blockdev_auto_del(BlockBackend
*blk
)
147 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
149 if (dinfo
&& dinfo
->auto_del
) {
150 monitor_remove_blk(blk
);
156 * Returns the current mapping of how many units per bus
157 * a particular interface can support.
159 * A positive integer indicates n units per bus.
160 * 0 implies the mapping has not been established.
161 * -1 indicates an invalid BlockInterfaceType was given.
163 int drive_get_max_devs(BlockInterfaceType type
)
165 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
166 return if_max_devs
[type
];
172 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
174 int max_devs
= if_max_devs
[type
];
175 return max_devs
? index
/ max_devs
: 0;
178 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
180 int max_devs
= if_max_devs
[type
];
181 return max_devs
? index
% max_devs
: index
;
184 QemuOpts
*drive_def(const char *optstr
)
186 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
189 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
194 opts
= drive_def(optstr
);
198 if (type
!= IF_DEFAULT
) {
199 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
202 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
205 qemu_opt_set(opts
, "file", file
, &error_abort
);
209 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
214 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
215 dinfo
= blk_legacy_dinfo(blk
);
216 if (dinfo
&& dinfo
->type
== type
217 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
225 bool drive_check_orphaned(void)
231 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
232 dinfo
= blk_legacy_dinfo(blk
);
233 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
234 /* Unless this is a default drive, this may be an oversight. */
235 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
236 dinfo
->type
!= IF_NONE
) {
237 fprintf(stderr
, "Warning: Orphaned drive without device: "
238 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
239 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
240 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
248 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
250 return drive_get(type
,
251 drive_index_to_bus_id(type
, index
),
252 drive_index_to_unit_id(type
, index
));
255 int drive_get_max_bus(BlockInterfaceType type
)
262 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
263 dinfo
= blk_legacy_dinfo(blk
);
264 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
265 max_bus
= dinfo
->bus
;
271 /* Get a block device. This should only be used for single-drive devices
272 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
274 DriveInfo
*drive_get_next(BlockInterfaceType type
)
276 static int next_block_unit
[IF_COUNT
];
278 return drive_get(type
, 0, next_block_unit
[type
]++);
281 static void bdrv_format_print(void *opaque
, const char *name
)
283 error_printf(" %s", name
);
288 BlockDriverState
*bs
;
291 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
293 if (!strcmp(buf
, "ignore")) {
294 return BLOCKDEV_ON_ERROR_IGNORE
;
295 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
296 return BLOCKDEV_ON_ERROR_ENOSPC
;
297 } else if (!strcmp(buf
, "stop")) {
298 return BLOCKDEV_ON_ERROR_STOP
;
299 } else if (!strcmp(buf
, "report")) {
300 return BLOCKDEV_ON_ERROR_REPORT
;
302 error_setg(errp
, "'%s' invalid %s error action",
303 buf
, is_read
? "read" : "write");
308 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
311 const QListEntry
*entry
;
312 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
313 switch (qobject_type(entry
->value
)) {
315 case QTYPE_QSTRING
: {
316 unsigned long long length
;
317 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
318 if (parse_uint_full(str
, &length
, 10) == 0 &&
319 length
> 0 && length
<= UINT_MAX
) {
320 block_acct_add_interval(stats
, (unsigned) length
);
322 error_setg(errp
, "Invalid interval length: %s", str
);
329 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
330 if (length
> 0 && length
<= UINT_MAX
) {
331 block_acct_add_interval(stats
, (unsigned) length
);
333 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
340 error_setg(errp
, "The specification of stats-intervals is invalid");
347 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
349 /* All parameters but @opts are optional and may be set to NULL. */
350 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
351 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
352 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
355 Error
*local_error
= NULL
;
359 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
360 *bdrv_flags
|= BDRV_O_RDWR
;
362 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
363 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
366 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
367 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
368 error_setg(errp
, "Invalid discard option");
373 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
374 if (!strcmp(aio
, "native")) {
375 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
376 } else if (!strcmp(aio
, "threads")) {
377 /* this is the default */
379 error_setg(errp
, "invalid aio option");
385 /* disk I/O throttling */
386 if (throttling_group
) {
387 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
391 throttle_config_init(throttle_cfg
);
392 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
393 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
394 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
395 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
396 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
397 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
398 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
399 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
400 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
401 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
402 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
403 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
405 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
406 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
407 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
408 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
409 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
410 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
411 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
412 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
413 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
414 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
415 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
416 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
419 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
420 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
421 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
422 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
423 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
424 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
425 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
426 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
427 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
428 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
429 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
431 throttle_cfg
->op_size
=
432 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
434 if (!throttle_is_valid(throttle_cfg
, errp
)) {
441 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
442 qemu_opt_get(opts
, "detect-zeroes"),
443 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
444 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
447 error_propagate(errp
, local_error
);
452 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
453 !(*bdrv_flags
& BDRV_O_UNMAP
))
455 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
456 "without setting discard operation to unmap");
462 /* Takes the ownership of bs_opts */
463 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
468 int on_read_error
, on_write_error
;
469 bool account_invalid
, account_failed
;
471 BlockDriverState
*bs
;
476 QDict
*interval_dict
= NULL
;
477 QList
*interval_list
= NULL
;
479 BlockdevDetectZeroesOptions detect_zeroes
=
480 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
481 const char *throttling_group
= NULL
;
483 /* Check common options by copying from bs_opts to opts, all other options
484 * stay in bs_opts for processing by bdrv_open(). */
485 id
= qdict_get_try_str(bs_opts
, "id");
486 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
488 error_propagate(errp
, error
);
492 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
494 error_propagate(errp
, error
);
499 qdict_del(bs_opts
, "id");
502 /* extract parameters */
503 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
505 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
506 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
508 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
509 qdict_array_split(interval_dict
, &interval_list
);
511 if (qdict_size(interval_dict
) != 0) {
512 error_setg(errp
, "Invalid option stats-intervals.%s",
513 qdict_first(interval_dict
)->key
);
517 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
518 &detect_zeroes
, &error
);
520 error_propagate(errp
, error
);
524 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
525 if (is_help_option(buf
)) {
526 error_printf("Supported formats:");
527 bdrv_iterate_format(bdrv_format_print
, NULL
);
532 if (qdict_haskey(bs_opts
, "driver")) {
533 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
536 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
539 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
540 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
541 on_write_error
= parse_block_error_action(buf
, 0, &error
);
543 error_propagate(errp
, error
);
548 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
549 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
550 on_read_error
= parse_block_error_action(buf
, 1, &error
);
552 error_propagate(errp
, error
);
558 bdrv_flags
|= BDRV_O_SNAPSHOT
;
562 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
563 BlockBackendRootState
*blk_rs
;
570 blk_rs
= blk_get_root_state(blk
);
571 blk_rs
->open_flags
= bdrv_flags
;
572 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
573 blk_rs
->detect_zeroes
= detect_zeroes
;
575 if (throttle_enabled(&cfg
)) {
576 if (!throttling_group
) {
577 throttling_group
= blk_name(blk
);
579 blk_rs
->throttle_group
= g_strdup(throttling_group
);
580 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
581 blk_rs
->throttle_state
->cfg
= cfg
;
586 if (file
&& !*file
) {
590 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
591 * with other callers) rather than what we want as the real defaults.
592 * Apply the defaults here instead. */
593 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
594 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
595 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
597 if (runstate_check(RUN_STATE_INMIGRATE
)) {
598 bdrv_flags
|= BDRV_O_INACTIVE
;
601 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
607 bs
->detect_zeroes
= detect_zeroes
;
609 /* disk I/O throttling */
610 if (throttle_enabled(&cfg
)) {
611 if (!throttling_group
) {
612 throttling_group
= blk_name(blk
);
614 bdrv_io_limits_enable(bs
, throttling_group
);
615 bdrv_set_io_limits(bs
, &cfg
);
618 if (bdrv_key_required(bs
)) {
622 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
624 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
631 blk_set_on_error(blk
, on_read_error
, on_write_error
);
633 if (!monitor_add_blk(blk
, qemu_opts_id(opts
), errp
)) {
641 QDECREF(interval_dict
);
642 QDECREF(interval_list
);
647 QDECREF(interval_dict
);
648 QDECREF(interval_list
);
654 static QemuOptsList qemu_root_bds_opts
;
656 /* Takes the ownership of bs_opts */
657 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
659 BlockDriverState
*bs
;
661 Error
*local_error
= NULL
;
662 BlockdevDetectZeroesOptions detect_zeroes
;
666 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
671 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
673 error_propagate(errp
, local_error
);
677 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
678 &detect_zeroes
, &local_error
);
680 error_propagate(errp
, local_error
);
684 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
685 * with other callers) rather than what we want as the real defaults.
686 * Apply the defaults here instead. */
687 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
688 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
689 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
691 if (runstate_check(RUN_STATE_INMIGRATE
)) {
692 bdrv_flags
|= BDRV_O_INACTIVE
;
696 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
698 goto fail_no_bs_opts
;
701 bs
->detect_zeroes
= detect_zeroes
;
713 void blockdev_close_all_bdrv_states(void)
715 BlockDriverState
*bs
, *next_bs
;
717 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
718 AioContext
*ctx
= bdrv_get_aio_context(bs
);
720 aio_context_acquire(ctx
);
722 aio_context_release(ctx
);
726 /* Iterates over the list of monitor-owned BlockDriverStates */
727 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
729 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
730 : QTAILQ_FIRST(&monitor_bdrv_states
);
733 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
738 value
= qemu_opt_get(opts
, from
);
740 if (qemu_opt_find(opts
, to
)) {
741 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
742 "same time", to
, from
);
747 /* rename all items in opts */
748 while ((value
= qemu_opt_get(opts
, from
))) {
749 qemu_opt_set(opts
, to
, value
, &error_abort
);
750 qemu_opt_unset(opts
, from
);
754 QemuOptsList qemu_legacy_drive_opts
= {
756 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
760 .type
= QEMU_OPT_NUMBER
,
761 .help
= "bus number",
764 .type
= QEMU_OPT_NUMBER
,
765 .help
= "unit number (i.e. lun for scsi)",
768 .type
= QEMU_OPT_NUMBER
,
769 .help
= "index number",
772 .type
= QEMU_OPT_STRING
,
773 .help
= "media type (disk, cdrom)",
776 .type
= QEMU_OPT_STRING
,
777 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
780 .type
= QEMU_OPT_NUMBER
,
781 .help
= "number of cylinders (ide disk geometry)",
784 .type
= QEMU_OPT_NUMBER
,
785 .help
= "number of heads (ide disk geometry)",
788 .type
= QEMU_OPT_NUMBER
,
789 .help
= "number of sectors (ide disk geometry)",
792 .type
= QEMU_OPT_STRING
,
793 .help
= "chs translation (auto, lba, none)",
796 .type
= QEMU_OPT_BOOL
,
797 .help
= "(deprecated, ignored)",
800 .type
= QEMU_OPT_STRING
,
801 .help
= "pci address (virtio only)",
804 .type
= QEMU_OPT_STRING
,
805 .help
= "disk serial number",
808 .type
= QEMU_OPT_STRING
,
812 /* Options that are passed on, but have special semantics with -drive */
815 .type
= QEMU_OPT_BOOL
,
816 .help
= "open drive file as read-only",
819 .type
= QEMU_OPT_STRING
,
820 .help
= "read error action",
823 .type
= QEMU_OPT_STRING
,
824 .help
= "write error action",
826 .name
= "copy-on-read",
827 .type
= QEMU_OPT_BOOL
,
828 .help
= "copy read data from backing file into image file",
831 { /* end of list */ }
835 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
839 DriveInfo
*dinfo
= NULL
;
841 QemuOpts
*legacy_opts
;
842 DriveMediaType media
= MEDIA_DISK
;
843 BlockInterfaceType type
;
844 int cyls
, heads
, secs
, translation
;
845 int max_devs
, bus_id
, unit_id
, index
;
847 const char *werror
, *rerror
;
848 bool read_only
= false;
851 const char *filename
;
852 Error
*local_err
= NULL
;
855 /* Change legacy command line options into QMP ones */
856 static const struct {
860 { "iops", "throttling.iops-total" },
861 { "iops_rd", "throttling.iops-read" },
862 { "iops_wr", "throttling.iops-write" },
864 { "bps", "throttling.bps-total" },
865 { "bps_rd", "throttling.bps-read" },
866 { "bps_wr", "throttling.bps-write" },
868 { "iops_max", "throttling.iops-total-max" },
869 { "iops_rd_max", "throttling.iops-read-max" },
870 { "iops_wr_max", "throttling.iops-write-max" },
872 { "bps_max", "throttling.bps-total-max" },
873 { "bps_rd_max", "throttling.bps-read-max" },
874 { "bps_wr_max", "throttling.bps-write-max" },
876 { "iops_size", "throttling.iops-size" },
878 { "group", "throttling.group" },
880 { "readonly", "read-only" },
883 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
884 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
887 error_report_err(local_err
);
892 value
= qemu_opt_get(all_opts
, "cache");
896 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
897 error_report("invalid cache option");
901 /* Specific options take precedence */
902 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
903 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
904 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
906 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
907 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
908 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
910 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
911 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
912 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
914 qemu_opt_unset(all_opts
, "cache");
917 /* Get a QDict for processing the options */
918 bs_opts
= qdict_new();
919 qemu_opts_to_qdict(all_opts
, bs_opts
);
921 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
923 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
925 error_report_err(local_err
);
929 /* Deprecated option boot=[on|off] */
930 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
931 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
932 "ignored. Future versions will reject this parameter. Please "
933 "update your scripts.\n");
937 value
= qemu_opt_get(legacy_opts
, "media");
939 if (!strcmp(value
, "disk")) {
941 } else if (!strcmp(value
, "cdrom")) {
945 error_report("'%s' invalid media", value
);
950 /* copy-on-read is disabled with a warning for read-only devices */
951 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
952 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
954 if (read_only
&& copy_on_read
) {
955 error_report("warning: disabling copy-on-read on read-only drive");
956 copy_on_read
= false;
959 qdict_put(bs_opts
, "read-only",
960 qstring_from_str(read_only
? "on" : "off"));
961 qdict_put(bs_opts
, "copy-on-read",
962 qstring_from_str(copy_on_read
? "on" :"off"));
964 /* Controller type */
965 value
= qemu_opt_get(legacy_opts
, "if");
968 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
971 if (type
== IF_COUNT
) {
972 error_report("unsupported bus type '%s'", value
);
976 type
= block_default_type
;
980 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
981 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
982 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
984 if (cyls
|| heads
|| secs
) {
986 error_report("invalid physical cyls number");
990 error_report("invalid physical heads number");
994 error_report("invalid physical secs number");
999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1000 value
= qemu_opt_get(legacy_opts
, "trans");
1001 if (value
!= NULL
) {
1003 error_report("'%s' trans must be used with cyls, heads and secs",
1007 if (!strcmp(value
, "none")) {
1008 translation
= BIOS_ATA_TRANSLATION_NONE
;
1009 } else if (!strcmp(value
, "lba")) {
1010 translation
= BIOS_ATA_TRANSLATION_LBA
;
1011 } else if (!strcmp(value
, "large")) {
1012 translation
= BIOS_ATA_TRANSLATION_LARGE
;
1013 } else if (!strcmp(value
, "rechs")) {
1014 translation
= BIOS_ATA_TRANSLATION_RECHS
;
1015 } else if (!strcmp(value
, "auto")) {
1016 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1018 error_report("'%s' invalid translation type", value
);
1023 if (media
== MEDIA_CDROM
) {
1024 if (cyls
|| secs
|| heads
) {
1025 error_report("CHS can't be set with media=cdrom");
1030 /* Device address specified by bus/unit or index.
1031 * If none was specified, try to find the first free one. */
1032 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1033 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1034 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1036 max_devs
= if_max_devs
[type
];
1039 if (bus_id
!= 0 || unit_id
!= -1) {
1040 error_report("index cannot be used with bus and unit");
1043 bus_id
= drive_index_to_bus_id(type
, index
);
1044 unit_id
= drive_index_to_unit_id(type
, index
);
1047 if (unit_id
== -1) {
1049 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1051 if (max_devs
&& unit_id
>= max_devs
) {
1052 unit_id
-= max_devs
;
1058 if (max_devs
&& unit_id
>= max_devs
) {
1059 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1063 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1064 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1065 bus_id
, unit_id
, index
);
1070 serial
= qemu_opt_get(legacy_opts
, "serial");
1072 /* no id supplied -> create one */
1073 if (qemu_opts_id(all_opts
) == NULL
) {
1075 const char *mediastr
= "";
1076 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1077 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1080 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1083 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1086 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1090 /* Add virtio block device */
1091 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1092 if (devaddr
&& type
!= IF_VIRTIO
) {
1093 error_report("addr is not supported by this bus type");
1097 if (type
== IF_VIRTIO
) {
1099 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1101 if (arch_type
== QEMU_ARCH_S390X
) {
1102 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1104 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1106 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1109 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1113 filename
= qemu_opt_get(legacy_opts
, "file");
1115 /* Check werror/rerror compatibility with if=... */
1116 werror
= qemu_opt_get(legacy_opts
, "werror");
1117 if (werror
!= NULL
) {
1118 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1120 error_report("werror is not supported by this bus type");
1123 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1126 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1127 if (rerror
!= NULL
) {
1128 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1130 error_report("rerror is not supported by this bus type");
1133 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1136 /* Actual block device init: Functionality shared with blockdev-add */
1137 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1141 error_report_err(local_err
);
1148 /* Create legacy DriveInfo */
1149 dinfo
= g_malloc0(sizeof(*dinfo
));
1150 dinfo
->opts
= all_opts
;
1153 dinfo
->heads
= heads
;
1155 dinfo
->trans
= translation
;
1158 dinfo
->bus
= bus_id
;
1159 dinfo
->unit
= unit_id
;
1160 dinfo
->devaddr
= devaddr
;
1161 dinfo
->serial
= g_strdup(serial
);
1163 blk_set_legacy_dinfo(blk
, dinfo
);
1170 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1177 qemu_opts_del(legacy_opts
);
1182 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1184 const char *device
= qdict_get_str(qdict
, "device");
1188 if (!strcmp(device
, "all")) {
1189 ret
= blk_commit_all();
1191 BlockDriverState
*bs
;
1192 AioContext
*aio_context
;
1194 blk
= blk_by_name(device
);
1196 monitor_printf(mon
, "Device '%s' not found\n", device
);
1199 if (!blk_is_available(blk
)) {
1200 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1205 aio_context
= bdrv_get_aio_context(bs
);
1206 aio_context_acquire(aio_context
);
1208 ret
= bdrv_commit(bs
);
1210 aio_context_release(aio_context
);
1213 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1218 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1220 TransactionActionList list
;
1222 list
.value
= action
;
1224 qmp_transaction(&list
, false, NULL
, errp
);
1227 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1228 bool has_node_name
, const char *node_name
,
1229 const char *snapshot_file
,
1230 bool has_snapshot_node_name
,
1231 const char *snapshot_node_name
,
1232 bool has_format
, const char *format
,
1233 bool has_mode
, NewImageMode mode
, Error
**errp
)
1235 BlockdevSnapshotSync snapshot
= {
1236 .has_device
= has_device
,
1237 .device
= (char *) device
,
1238 .has_node_name
= has_node_name
,
1239 .node_name
= (char *) node_name
,
1240 .snapshot_file
= (char *) snapshot_file
,
1241 .has_snapshot_node_name
= has_snapshot_node_name
,
1242 .snapshot_node_name
= (char *) snapshot_node_name
,
1243 .has_format
= has_format
,
1244 .format
= (char *) format
,
1245 .has_mode
= has_mode
,
1248 TransactionAction action
= {
1249 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1250 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1252 blockdev_do_action(&action
, errp
);
1255 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1258 BlockdevSnapshot snapshot_data
= {
1259 .node
= (char *) node
,
1260 .overlay
= (char *) overlay
1262 TransactionAction action
= {
1263 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1264 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1266 blockdev_do_action(&action
, errp
);
1269 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1273 BlockdevSnapshotInternal snapshot
= {
1274 .device
= (char *) device
,
1275 .name
= (char *) name
1277 TransactionAction action
= {
1278 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1279 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1281 blockdev_do_action(&action
, errp
);
1284 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1291 BlockDriverState
*bs
;
1293 AioContext
*aio_context
;
1294 QEMUSnapshotInfo sn
;
1295 Error
*local_err
= NULL
;
1296 SnapshotInfo
*info
= NULL
;
1299 blk
= blk_by_name(device
);
1301 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1302 "Device '%s' not found", device
);
1306 aio_context
= blk_get_aio_context(blk
);
1307 aio_context_acquire(aio_context
);
1318 error_setg(errp
, "Name or id must be provided");
1319 goto out_aio_context
;
1322 if (!blk_is_available(blk
)) {
1323 error_setg(errp
, "Device '%s' has no medium", device
);
1324 goto out_aio_context
;
1328 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1329 goto out_aio_context
;
1332 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1334 error_propagate(errp
, local_err
);
1335 goto out_aio_context
;
1339 "Snapshot with id '%s' and name '%s' does not exist on "
1341 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1342 goto out_aio_context
;
1345 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1347 error_propagate(errp
, local_err
);
1348 goto out_aio_context
;
1351 aio_context_release(aio_context
);
1353 info
= g_new0(SnapshotInfo
, 1);
1354 info
->id
= g_strdup(sn
.id_str
);
1355 info
->name
= g_strdup(sn
.name
);
1356 info
->date_nsec
= sn
.date_nsec
;
1357 info
->date_sec
= sn
.date_sec
;
1358 info
->vm_state_size
= sn
.vm_state_size
;
1359 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1360 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1365 aio_context_release(aio_context
);
1370 * block_dirty_bitmap_lookup:
1371 * Return a dirty bitmap (if present), after validating
1372 * the node reference and bitmap names.
1374 * @node: The name of the BDS node to search for bitmaps
1375 * @name: The name of the bitmap to search for
1376 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1377 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1378 * @errp: Output pointer for error information. Can be NULL.
1380 * @return: A bitmap object on success, or NULL on failure.
1382 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1384 BlockDriverState
**pbs
,
1388 BlockDriverState
*bs
;
1389 BdrvDirtyBitmap
*bitmap
;
1390 AioContext
*aio_context
;
1393 error_setg(errp
, "Node cannot be NULL");
1397 error_setg(errp
, "Bitmap name cannot be NULL");
1400 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1402 error_setg(errp
, "Node '%s' not found", node
);
1406 aio_context
= bdrv_get_aio_context(bs
);
1407 aio_context_acquire(aio_context
);
1409 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1411 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1419 *paio
= aio_context
;
1421 aio_context_release(aio_context
);
1427 aio_context_release(aio_context
);
1431 /* New and old BlockDriverState structs for atomic group operations */
1433 typedef struct BlkActionState BlkActionState
;
1437 * Table of operations that define an Action.
1439 * @instance_size: Size of state struct, in bytes.
1440 * @prepare: Prepare the work, must NOT be NULL.
1441 * @commit: Commit the changes, can be NULL.
1442 * @abort: Abort the changes on fail, can be NULL.
1443 * @clean: Clean up resources after all transaction actions have called
1444 * commit() or abort(). Can be NULL.
1446 * Only prepare() may fail. In a single transaction, only one of commit() or
1447 * abort() will be called. clean() will always be called if it is present.
1449 typedef struct BlkActionOps
{
1450 size_t instance_size
;
1451 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1452 void (*commit
)(BlkActionState
*common
);
1453 void (*abort
)(BlkActionState
*common
);
1454 void (*clean
)(BlkActionState
*common
);
1459 * Describes one Action's state within a Transaction.
1461 * @action: QAPI-defined enum identifying which Action to perform.
1462 * @ops: Table of ActionOps this Action can perform.
1463 * @block_job_txn: Transaction which this action belongs to.
1464 * @entry: List membership for all Actions in this Transaction.
1466 * This structure must be arranged as first member in a subclassed type,
1467 * assuming that the compiler will also arrange it to the same offsets as the
1470 struct BlkActionState
{
1471 TransactionAction
*action
;
1472 const BlkActionOps
*ops
;
1473 BlockJobTxn
*block_job_txn
;
1474 TransactionProperties
*txn_props
;
1475 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1478 /* internal snapshot private data */
1479 typedef struct InternalSnapshotState
{
1480 BlkActionState common
;
1481 BlockDriverState
*bs
;
1482 AioContext
*aio_context
;
1483 QEMUSnapshotInfo sn
;
1485 } InternalSnapshotState
;
1488 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1490 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1492 "Action '%s' does not support Transaction property "
1493 "completion-mode = %s",
1494 TransactionActionKind_lookup
[s
->action
->type
],
1495 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1501 static void internal_snapshot_prepare(BlkActionState
*common
,
1504 Error
*local_err
= NULL
;
1508 BlockDriverState
*bs
;
1509 QEMUSnapshotInfo old_sn
, *sn
;
1512 BlockdevSnapshotInternal
*internal
;
1513 InternalSnapshotState
*state
;
1516 g_assert(common
->action
->type
==
1517 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1518 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1519 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1521 /* 1. parse input */
1522 device
= internal
->device
;
1523 name
= internal
->name
;
1525 /* 2. check for validation */
1526 if (action_check_completion_mode(common
, errp
) < 0) {
1530 blk
= blk_by_name(device
);
1532 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1533 "Device '%s' not found", device
);
1537 /* AioContext is released in .clean() */
1538 state
->aio_context
= blk_get_aio_context(blk
);
1539 aio_context_acquire(state
->aio_context
);
1541 if (!blk_is_available(blk
)) {
1542 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1548 bdrv_drained_begin(bs
);
1550 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1554 if (bdrv_is_read_only(bs
)) {
1555 error_setg(errp
, "Device '%s' is read only", device
);
1559 if (!bdrv_can_snapshot(bs
)) {
1560 error_setg(errp
, "Block format '%s' used by device '%s' "
1561 "does not support internal snapshots",
1562 bs
->drv
->format_name
, device
);
1566 if (!strlen(name
)) {
1567 error_setg(errp
, "Name is empty");
1571 /* check whether a snapshot with name exist */
1572 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1575 error_propagate(errp
, local_err
);
1579 "Snapshot with name '%s' already exists on device '%s'",
1584 /* 3. take the snapshot */
1586 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1587 qemu_gettimeofday(&tv
);
1588 sn
->date_sec
= tv
.tv_sec
;
1589 sn
->date_nsec
= tv
.tv_usec
* 1000;
1590 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1592 ret1
= bdrv_snapshot_create(bs
, sn
);
1594 error_setg_errno(errp
, -ret1
,
1595 "Failed to create snapshot '%s' on device '%s'",
1600 /* 4. succeed, mark a snapshot is created */
1601 state
->created
= true;
1604 static void internal_snapshot_abort(BlkActionState
*common
)
1606 InternalSnapshotState
*state
=
1607 DO_UPCAST(InternalSnapshotState
, common
, common
);
1608 BlockDriverState
*bs
= state
->bs
;
1609 QEMUSnapshotInfo
*sn
= &state
->sn
;
1610 Error
*local_error
= NULL
;
1612 if (!state
->created
) {
1616 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1617 error_reportf_err(local_error
,
1618 "Failed to delete snapshot with id '%s' and "
1619 "name '%s' on device '%s' in abort: ",
1620 sn
->id_str
, sn
->name
,
1621 bdrv_get_device_name(bs
));
1625 static void internal_snapshot_clean(BlkActionState
*common
)
1627 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1630 if (state
->aio_context
) {
1632 bdrv_drained_end(state
->bs
);
1634 aio_context_release(state
->aio_context
);
1638 /* external snapshot private data */
1639 typedef struct ExternalSnapshotState
{
1640 BlkActionState common
;
1641 BlockDriverState
*old_bs
;
1642 BlockDriverState
*new_bs
;
1643 AioContext
*aio_context
;
1644 } ExternalSnapshotState
;
1646 static void external_snapshot_prepare(BlkActionState
*common
,
1650 QDict
*options
= NULL
;
1651 Error
*local_err
= NULL
;
1652 /* Device and node name of the image to generate the snapshot from */
1654 const char *node_name
;
1655 /* Reference to the new image (for 'blockdev-snapshot') */
1656 const char *snapshot_ref
;
1657 /* File name of the new image (for 'blockdev-snapshot-sync') */
1658 const char *new_image_file
;
1659 ExternalSnapshotState
*state
=
1660 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1661 TransactionAction
*action
= common
->action
;
1663 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1664 * purpose but a different set of parameters */
1665 switch (action
->type
) {
1666 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1668 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1670 node_name
= s
->node
;
1671 new_image_file
= NULL
;
1672 snapshot_ref
= s
->overlay
;
1675 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1677 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1678 device
= s
->has_device
? s
->device
: NULL
;
1679 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1680 new_image_file
= s
->snapshot_file
;
1681 snapshot_ref
= NULL
;
1685 g_assert_not_reached();
1688 /* start processing */
1689 if (action_check_completion_mode(common
, errp
) < 0) {
1693 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1694 if (!state
->old_bs
) {
1698 /* Acquire AioContext now so any threads operating on old_bs stop */
1699 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1700 aio_context_acquire(state
->aio_context
);
1701 bdrv_drained_begin(state
->old_bs
);
1703 if (!bdrv_is_inserted(state
->old_bs
)) {
1704 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1708 if (bdrv_op_is_blocked(state
->old_bs
,
1709 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1713 if (!bdrv_is_read_only(state
->old_bs
)) {
1714 if (bdrv_flush(state
->old_bs
)) {
1715 error_setg(errp
, QERR_IO_ERROR
);
1720 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1721 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1725 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1726 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1727 const char *format
= s
->has_format
? s
->format
: "qcow2";
1728 enum NewImageMode mode
;
1729 const char *snapshot_node_name
=
1730 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1732 if (node_name
&& !snapshot_node_name
) {
1733 error_setg(errp
, "New snapshot node name missing");
1737 if (snapshot_node_name
&&
1738 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1739 error_setg(errp
, "New snapshot node name already in use");
1743 flags
= state
->old_bs
->open_flags
;
1745 /* create new image w/backing file */
1746 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1747 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1748 int64_t size
= bdrv_getlength(state
->old_bs
);
1750 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1753 bdrv_img_create(new_image_file
, format
,
1754 state
->old_bs
->filename
,
1755 state
->old_bs
->drv
->format_name
,
1756 NULL
, size
, flags
, &local_err
, false);
1758 error_propagate(errp
, local_err
);
1763 options
= qdict_new();
1764 if (s
->has_snapshot_node_name
) {
1765 qdict_put(options
, "node-name",
1766 qstring_from_str(snapshot_node_name
));
1768 qdict_put(options
, "driver", qstring_from_str(format
));
1770 flags
|= BDRV_O_NO_BACKING
;
1773 assert(state
->new_bs
== NULL
);
1774 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1776 /* We will manually add the backing_hd field to the bs later */
1781 if (state
->new_bs
->blk
!= NULL
) {
1782 error_setg(errp
, "The snapshot is already in use by %s",
1783 blk_name(state
->new_bs
->blk
));
1787 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1792 if (state
->new_bs
->backing
!= NULL
) {
1793 error_setg(errp
, "The snapshot already has a backing image");
1797 if (!state
->new_bs
->drv
->supports_backing
) {
1798 error_setg(errp
, "The snapshot does not support backing images");
1802 static void external_snapshot_commit(BlkActionState
*common
)
1804 ExternalSnapshotState
*state
=
1805 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1807 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1809 /* This removes our old bs and adds the new bs */
1810 bdrv_append(state
->new_bs
, state
->old_bs
);
1811 /* We don't need (or want) to use the transactional
1812 * bdrv_reopen_multiple() across all the entries at once, because we
1813 * don't want to abort all of them if one of them fails the reopen */
1814 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1818 static void external_snapshot_abort(BlkActionState
*common
)
1820 ExternalSnapshotState
*state
=
1821 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1822 if (state
->new_bs
) {
1823 bdrv_unref(state
->new_bs
);
1827 static void external_snapshot_clean(BlkActionState
*common
)
1829 ExternalSnapshotState
*state
=
1830 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1831 if (state
->aio_context
) {
1832 bdrv_drained_end(state
->old_bs
);
1833 aio_context_release(state
->aio_context
);
1837 typedef struct DriveBackupState
{
1838 BlkActionState common
;
1839 BlockDriverState
*bs
;
1840 AioContext
*aio_context
;
1844 static void do_drive_backup(const char *device
, const char *target
,
1845 bool has_format
, const char *format
,
1846 enum MirrorSyncMode sync
,
1847 bool has_mode
, enum NewImageMode mode
,
1848 bool has_speed
, int64_t speed
,
1849 bool has_bitmap
, const char *bitmap
,
1850 bool has_on_source_error
,
1851 BlockdevOnError on_source_error
,
1852 bool has_on_target_error
,
1853 BlockdevOnError on_target_error
,
1854 BlockJobTxn
*txn
, Error
**errp
);
1856 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1858 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1860 DriveBackup
*backup
;
1861 Error
*local_err
= NULL
;
1863 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1864 backup
= common
->action
->u
.drive_backup
.data
;
1866 blk
= blk_by_name(backup
->device
);
1868 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1869 "Device '%s' not found", backup
->device
);
1873 if (!blk_is_available(blk
)) {
1874 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1878 /* AioContext is released in .clean() */
1879 state
->aio_context
= blk_get_aio_context(blk
);
1880 aio_context_acquire(state
->aio_context
);
1881 bdrv_drained_begin(blk_bs(blk
));
1882 state
->bs
= blk_bs(blk
);
1884 do_drive_backup(backup
->device
, backup
->target
,
1885 backup
->has_format
, backup
->format
,
1887 backup
->has_mode
, backup
->mode
,
1888 backup
->has_speed
, backup
->speed
,
1889 backup
->has_bitmap
, backup
->bitmap
,
1890 backup
->has_on_source_error
, backup
->on_source_error
,
1891 backup
->has_on_target_error
, backup
->on_target_error
,
1892 common
->block_job_txn
, &local_err
);
1894 error_propagate(errp
, local_err
);
1898 state
->job
= state
->bs
->job
;
1901 static void drive_backup_abort(BlkActionState
*common
)
1903 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1904 BlockDriverState
*bs
= state
->bs
;
1906 /* Only cancel if it's the job we started */
1907 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1908 block_job_cancel_sync(bs
->job
);
1912 static void drive_backup_clean(BlkActionState
*common
)
1914 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1916 if (state
->aio_context
) {
1917 bdrv_drained_end(state
->bs
);
1918 aio_context_release(state
->aio_context
);
1922 typedef struct BlockdevBackupState
{
1923 BlkActionState common
;
1924 BlockDriverState
*bs
;
1926 AioContext
*aio_context
;
1927 } BlockdevBackupState
;
1929 static void do_blockdev_backup(const char *device
, const char *target
,
1930 enum MirrorSyncMode sync
,
1931 bool has_speed
, int64_t speed
,
1932 bool has_on_source_error
,
1933 BlockdevOnError on_source_error
,
1934 bool has_on_target_error
,
1935 BlockdevOnError on_target_error
,
1936 BlockJobTxn
*txn
, Error
**errp
);
1938 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1940 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1941 BlockdevBackup
*backup
;
1942 BlockBackend
*blk
, *target
;
1943 Error
*local_err
= NULL
;
1945 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1946 backup
= common
->action
->u
.blockdev_backup
.data
;
1948 blk
= blk_by_name(backup
->device
);
1950 error_setg(errp
, "Device '%s' not found", backup
->device
);
1954 if (!blk_is_available(blk
)) {
1955 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1959 target
= blk_by_name(backup
->target
);
1961 error_setg(errp
, "Device '%s' not found", backup
->target
);
1965 /* AioContext is released in .clean() */
1966 state
->aio_context
= blk_get_aio_context(blk
);
1967 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1968 state
->aio_context
= NULL
;
1969 error_setg(errp
, "Backup between two IO threads is not implemented");
1972 aio_context_acquire(state
->aio_context
);
1973 state
->bs
= blk_bs(blk
);
1974 bdrv_drained_begin(state
->bs
);
1976 do_blockdev_backup(backup
->device
, backup
->target
,
1978 backup
->has_speed
, backup
->speed
,
1979 backup
->has_on_source_error
, backup
->on_source_error
,
1980 backup
->has_on_target_error
, backup
->on_target_error
,
1981 common
->block_job_txn
, &local_err
);
1983 error_propagate(errp
, local_err
);
1987 state
->job
= state
->bs
->job
;
1990 static void blockdev_backup_abort(BlkActionState
*common
)
1992 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1993 BlockDriverState
*bs
= state
->bs
;
1995 /* Only cancel if it's the job we started */
1996 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1997 block_job_cancel_sync(bs
->job
);
2001 static void blockdev_backup_clean(BlkActionState
*common
)
2003 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2005 if (state
->aio_context
) {
2006 bdrv_drained_end(state
->bs
);
2007 aio_context_release(state
->aio_context
);
2011 typedef struct BlockDirtyBitmapState
{
2012 BlkActionState common
;
2013 BdrvDirtyBitmap
*bitmap
;
2014 BlockDriverState
*bs
;
2015 AioContext
*aio_context
;
2018 } BlockDirtyBitmapState
;
2020 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2023 Error
*local_err
= NULL
;
2024 BlockDirtyBitmapAdd
*action
;
2025 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2028 if (action_check_completion_mode(common
, errp
) < 0) {
2032 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2033 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2034 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2035 action
->has_granularity
, action
->granularity
,
2039 state
->prepared
= true;
2041 error_propagate(errp
, local_err
);
2045 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2047 BlockDirtyBitmapAdd
*action
;
2048 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2051 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2052 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2053 * then the node reference and bitmap name must have been valid.
2055 if (state
->prepared
) {
2056 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2060 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2063 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2065 BlockDirtyBitmap
*action
;
2067 if (action_check_completion_mode(common
, errp
) < 0) {
2071 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2072 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2075 &state
->aio_context
,
2077 if (!state
->bitmap
) {
2081 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2082 error_setg(errp
, "Cannot modify a frozen bitmap");
2084 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2085 error_setg(errp
, "Cannot clear a disabled bitmap");
2089 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2090 /* AioContext is released in .clean() */
2093 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2095 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2098 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2101 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2103 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2106 hbitmap_free(state
->backup
);
2109 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2111 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2114 if (state
->aio_context
) {
2115 aio_context_release(state
->aio_context
);
2119 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2121 error_setg(errp
, "Transaction aborted using Abort action");
2124 static void abort_commit(BlkActionState
*common
)
2126 g_assert_not_reached(); /* this action never succeeds */
2129 static const BlkActionOps actions
[] = {
2130 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2131 .instance_size
= sizeof(ExternalSnapshotState
),
2132 .prepare
= external_snapshot_prepare
,
2133 .commit
= external_snapshot_commit
,
2134 .abort
= external_snapshot_abort
,
2135 .clean
= external_snapshot_clean
,
2137 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2138 .instance_size
= sizeof(ExternalSnapshotState
),
2139 .prepare
= external_snapshot_prepare
,
2140 .commit
= external_snapshot_commit
,
2141 .abort
= external_snapshot_abort
,
2142 .clean
= external_snapshot_clean
,
2144 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2145 .instance_size
= sizeof(DriveBackupState
),
2146 .prepare
= drive_backup_prepare
,
2147 .abort
= drive_backup_abort
,
2148 .clean
= drive_backup_clean
,
2150 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2151 .instance_size
= sizeof(BlockdevBackupState
),
2152 .prepare
= blockdev_backup_prepare
,
2153 .abort
= blockdev_backup_abort
,
2154 .clean
= blockdev_backup_clean
,
2156 [TRANSACTION_ACTION_KIND_ABORT
] = {
2157 .instance_size
= sizeof(BlkActionState
),
2158 .prepare
= abort_prepare
,
2159 .commit
= abort_commit
,
2161 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2162 .instance_size
= sizeof(InternalSnapshotState
),
2163 .prepare
= internal_snapshot_prepare
,
2164 .abort
= internal_snapshot_abort
,
2165 .clean
= internal_snapshot_clean
,
2167 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2168 .instance_size
= sizeof(BlockDirtyBitmapState
),
2169 .prepare
= block_dirty_bitmap_add_prepare
,
2170 .abort
= block_dirty_bitmap_add_abort
,
2172 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2173 .instance_size
= sizeof(BlockDirtyBitmapState
),
2174 .prepare
= block_dirty_bitmap_clear_prepare
,
2175 .commit
= block_dirty_bitmap_clear_commit
,
2176 .abort
= block_dirty_bitmap_clear_abort
,
2177 .clean
= block_dirty_bitmap_clear_clean
,
2182 * Allocate a TransactionProperties structure if necessary, and fill
2183 * that structure with desired defaults if they are unset.
2185 static TransactionProperties
*get_transaction_properties(
2186 TransactionProperties
*props
)
2189 props
= g_new0(TransactionProperties
, 1);
2192 if (!props
->has_completion_mode
) {
2193 props
->has_completion_mode
= true;
2194 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2201 * 'Atomic' group operations. The operations are performed as a set, and if
2202 * any fail then we roll back all operations in the group.
2204 void qmp_transaction(TransactionActionList
*dev_list
,
2206 struct TransactionProperties
*props
,
2209 TransactionActionList
*dev_entry
= dev_list
;
2210 BlockJobTxn
*block_job_txn
= NULL
;
2211 BlkActionState
*state
, *next
;
2212 Error
*local_err
= NULL
;
2214 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2215 QSIMPLEQ_INIT(&snap_bdrv_states
);
2217 /* Does this transaction get canceled as a group on failure?
2218 * If not, we don't really need to make a BlockJobTxn.
2220 props
= get_transaction_properties(props
);
2221 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2222 block_job_txn
= block_job_txn_new();
2225 /* drain all i/o before any operations */
2228 /* We don't do anything in this loop that commits us to the operations */
2229 while (NULL
!= dev_entry
) {
2230 TransactionAction
*dev_info
= NULL
;
2231 const BlkActionOps
*ops
;
2233 dev_info
= dev_entry
->value
;
2234 dev_entry
= dev_entry
->next
;
2236 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2238 ops
= &actions
[dev_info
->type
];
2239 assert(ops
->instance_size
> 0);
2241 state
= g_malloc0(ops
->instance_size
);
2243 state
->action
= dev_info
;
2244 state
->block_job_txn
= block_job_txn
;
2245 state
->txn_props
= props
;
2246 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2248 state
->ops
->prepare(state
, &local_err
);
2250 error_propagate(errp
, local_err
);
2251 goto delete_and_fail
;
2255 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2256 if (state
->ops
->commit
) {
2257 state
->ops
->commit(state
);
2265 /* failure, and it is all-or-none; roll back all operations */
2266 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2267 if (state
->ops
->abort
) {
2268 state
->ops
->abort(state
);
2272 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2273 if (state
->ops
->clean
) {
2274 state
->ops
->clean(state
);
2279 qapi_free_TransactionProperties(props
);
2281 block_job_txn_unref(block_job_txn
);
2284 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2286 Error
*local_err
= NULL
;
2288 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2290 error_propagate(errp
, local_err
);
2294 qmp_x_blockdev_remove_medium(device
, errp
);
2297 void qmp_block_passwd(bool has_device
, const char *device
,
2298 bool has_node_name
, const char *node_name
,
2299 const char *password
, Error
**errp
)
2301 Error
*local_err
= NULL
;
2302 BlockDriverState
*bs
;
2303 AioContext
*aio_context
;
2305 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2306 has_node_name
? node_name
: NULL
,
2309 error_propagate(errp
, local_err
);
2313 aio_context
= bdrv_get_aio_context(bs
);
2314 aio_context_acquire(aio_context
);
2316 bdrv_add_key(bs
, password
, errp
);
2318 aio_context_release(aio_context
);
2321 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2331 blk
= blk_by_name(device
);
2333 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2334 "Device '%s' not found", device
);
2338 if (!blk_dev_has_removable_media(blk
)) {
2339 error_setg(errp
, "Device '%s' is not removable", device
);
2343 if (!blk_dev_has_tray(blk
)) {
2344 /* Ignore this command on tray-less devices */
2348 if (blk_dev_is_tray_open(blk
)) {
2352 locked
= blk_dev_is_medium_locked(blk
);
2354 blk_dev_eject_request(blk
, force
);
2357 if (!locked
|| force
) {
2358 blk_dev_change_media_cb(blk
, false);
2362 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2366 blk
= blk_by_name(device
);
2368 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2369 "Device '%s' not found", device
);
2373 if (!blk_dev_has_removable_media(blk
)) {
2374 error_setg(errp
, "Device '%s' is not removable", device
);
2378 if (!blk_dev_has_tray(blk
)) {
2379 /* Ignore this command on tray-less devices */
2383 if (!blk_dev_is_tray_open(blk
)) {
2387 blk_dev_change_media_cb(blk
, true);
2390 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2393 BlockDriverState
*bs
;
2394 AioContext
*aio_context
;
2397 blk
= blk_by_name(device
);
2399 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2400 "Device '%s' not found", device
);
2404 /* For BBs without a device, we can exchange the BDS tree at will */
2405 has_device
= blk_get_attached_dev(blk
);
2407 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2408 error_setg(errp
, "Device '%s' is not removable", device
);
2412 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2413 error_setg(errp
, "Tray of device '%s' is not open", device
);
2422 aio_context
= bdrv_get_aio_context(bs
);
2423 aio_context_acquire(aio_context
);
2425 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2431 if (!blk_dev_has_tray(blk
)) {
2432 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2433 * called at all); therefore, the medium needs to be ejected here.
2434 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2435 * value passed here (i.e. false). */
2436 blk_dev_change_media_cb(blk
, false);
2440 aio_context_release(aio_context
);
2443 static void qmp_blockdev_insert_anon_medium(const char *device
,
2444 BlockDriverState
*bs
, Error
**errp
)
2449 blk
= blk_by_name(device
);
2451 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2452 "Device '%s' not found", device
);
2456 /* For BBs without a device, we can exchange the BDS tree at will */
2457 has_device
= blk_get_attached_dev(blk
);
2459 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2460 error_setg(errp
, "Device '%s' is not removable", device
);
2464 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2465 error_setg(errp
, "Tray of device '%s' is not open", device
);
2470 error_setg(errp
, "There already is a medium in device '%s'", device
);
2474 blk_insert_bs(blk
, bs
);
2476 if (!blk_dev_has_tray(blk
)) {
2477 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2478 * called at all); therefore, the medium needs to be pushed into the
2480 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2481 * value passed here (i.e. true). */
2482 blk_dev_change_media_cb(blk
, true);
2486 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2489 BlockDriverState
*bs
;
2491 bs
= bdrv_find_node(node_name
);
2493 error_setg(errp
, "Node '%s' not found", node_name
);
2498 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2503 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2506 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2507 bool has_format
, const char *format
,
2509 BlockdevChangeReadOnlyMode read_only
,
2513 BlockDriverState
*medium_bs
= NULL
;
2514 int bdrv_flags
, ret
;
2515 QDict
*options
= NULL
;
2518 blk
= blk_by_name(device
);
2520 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2521 "Device '%s' not found", device
);
2526 blk_update_root_state(blk
);
2529 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2530 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2533 if (!has_read_only
) {
2534 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2537 switch (read_only
) {
2538 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2541 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2542 bdrv_flags
&= ~BDRV_O_RDWR
;
2545 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2546 bdrv_flags
|= BDRV_O_RDWR
;
2554 options
= qdict_new();
2555 qdict_put(options
, "driver", qstring_from_str(format
));
2559 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2564 blk_apply_root_state(blk
, medium_bs
);
2566 bdrv_add_key(medium_bs
, NULL
, &err
);
2568 error_propagate(errp
, err
);
2572 qmp_blockdev_open_tray(device
, false, false, &err
);
2574 error_propagate(errp
, err
);
2578 qmp_x_blockdev_remove_medium(device
, &err
);
2580 error_propagate(errp
, err
);
2584 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2586 error_propagate(errp
, err
);
2590 qmp_blockdev_close_tray(device
, errp
);
2593 /* If the medium has been inserted, the device has its own reference, so
2594 * ours must be relinquished; and if it has not been inserted successfully,
2595 * the reference must be relinquished anyway */
2596 bdrv_unref(medium_bs
);
2599 /* throttling disk I/O limits */
2600 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2607 bool has_bps_rd_max
,
2609 bool has_bps_wr_max
,
2613 bool has_iops_rd_max
,
2614 int64_t iops_rd_max
,
2615 bool has_iops_wr_max
,
2616 int64_t iops_wr_max
,
2617 bool has_bps_max_length
,
2618 int64_t bps_max_length
,
2619 bool has_bps_rd_max_length
,
2620 int64_t bps_rd_max_length
,
2621 bool has_bps_wr_max_length
,
2622 int64_t bps_wr_max_length
,
2623 bool has_iops_max_length
,
2624 int64_t iops_max_length
,
2625 bool has_iops_rd_max_length
,
2626 int64_t iops_rd_max_length
,
2627 bool has_iops_wr_max_length
,
2628 int64_t iops_wr_max_length
,
2632 const char *group
, Error
**errp
)
2635 BlockDriverState
*bs
;
2637 AioContext
*aio_context
;
2639 blk
= blk_by_name(device
);
2641 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2642 "Device '%s' not found", device
);
2646 aio_context
= blk_get_aio_context(blk
);
2647 aio_context_acquire(aio_context
);
2651 error_setg(errp
, "Device '%s' has no medium", device
);
2655 throttle_config_init(&cfg
);
2656 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2657 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2658 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2660 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2661 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2662 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2665 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2667 if (has_bps_rd_max
) {
2668 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2670 if (has_bps_wr_max
) {
2671 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2674 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2676 if (has_iops_rd_max
) {
2677 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2679 if (has_iops_wr_max
) {
2680 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2683 if (has_bps_max_length
) {
2684 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= bps_max_length
;
2686 if (has_bps_rd_max_length
) {
2687 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= bps_rd_max_length
;
2689 if (has_bps_wr_max_length
) {
2690 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= bps_wr_max_length
;
2692 if (has_iops_max_length
) {
2693 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= iops_max_length
;
2695 if (has_iops_rd_max_length
) {
2696 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= iops_rd_max_length
;
2698 if (has_iops_wr_max_length
) {
2699 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= iops_wr_max_length
;
2702 if (has_iops_size
) {
2703 cfg
.op_size
= iops_size
;
2706 if (!throttle_is_valid(&cfg
, errp
)) {
2710 if (throttle_enabled(&cfg
)) {
2711 /* Enable I/O limits if they're not enabled yet, otherwise
2712 * just update the throttling group. */
2713 if (!bs
->throttle_state
) {
2714 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2715 } else if (has_group
) {
2716 bdrv_io_limits_update_group(bs
, group
);
2718 /* Set the new throttling configuration */
2719 bdrv_set_io_limits(bs
, &cfg
);
2720 } else if (bs
->throttle_state
) {
2721 /* If all throttling settings are set to 0, disable I/O limits */
2722 bdrv_io_limits_disable(bs
);
2726 aio_context_release(aio_context
);
2729 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2730 bool has_granularity
, uint32_t granularity
,
2733 AioContext
*aio_context
;
2734 BlockDriverState
*bs
;
2736 if (!name
|| name
[0] == '\0') {
2737 error_setg(errp
, "Bitmap name cannot be empty");
2741 bs
= bdrv_lookup_bs(node
, node
, errp
);
2746 aio_context
= bdrv_get_aio_context(bs
);
2747 aio_context_acquire(aio_context
);
2749 if (has_granularity
) {
2750 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2751 error_setg(errp
, "Granularity must be power of 2 "
2752 "and at least 512");
2756 /* Default to cluster size, if available: */
2757 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2760 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2763 aio_context_release(aio_context
);
2766 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2769 AioContext
*aio_context
;
2770 BlockDriverState
*bs
;
2771 BdrvDirtyBitmap
*bitmap
;
2773 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2774 if (!bitmap
|| !bs
) {
2778 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2780 "Bitmap '%s' is currently frozen and cannot be removed",
2784 bdrv_dirty_bitmap_make_anon(bitmap
);
2785 bdrv_release_dirty_bitmap(bs
, bitmap
);
2788 aio_context_release(aio_context
);
2792 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2793 * immediately after a full backup operation.
2795 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2798 AioContext
*aio_context
;
2799 BdrvDirtyBitmap
*bitmap
;
2800 BlockDriverState
*bs
;
2802 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2803 if (!bitmap
|| !bs
) {
2807 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2809 "Bitmap '%s' is currently frozen and cannot be modified",
2812 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2814 "Bitmap '%s' is currently disabled and cannot be cleared",
2819 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2822 aio_context_release(aio_context
);
2825 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2827 const char *id
= qdict_get_str(qdict
, "id");
2829 BlockDriverState
*bs
;
2830 AioContext
*aio_context
;
2831 Error
*local_err
= NULL
;
2833 bs
= bdrv_find_node(id
);
2835 qmp_x_blockdev_del(false, NULL
, true, id
, &local_err
);
2837 error_report_err(local_err
);
2842 blk
= blk_by_name(id
);
2844 error_report("Device '%s' not found", id
);
2848 if (!blk_legacy_dinfo(blk
)) {
2849 error_report("Deleting device added with blockdev-add"
2850 " is not supported");
2854 aio_context
= blk_get_aio_context(blk
);
2855 aio_context_acquire(aio_context
);
2859 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2860 error_report_err(local_err
);
2861 aio_context_release(aio_context
);
2868 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2869 monitor_remove_blk(blk
);
2871 bdrv_make_anon(blk_bs(blk
));
2874 /* If this BlockBackend has a device attached to it, its refcount will be
2875 * decremented when the device is removed; otherwise we have to do so here.
2877 if (blk_get_attached_dev(blk
)) {
2878 /* Further I/O must not pause the guest */
2879 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2880 BLOCKDEV_ON_ERROR_REPORT
);
2885 aio_context_release(aio_context
);
2888 void qmp_block_resize(bool has_device
, const char *device
,
2889 bool has_node_name
, const char *node_name
,
2890 int64_t size
, Error
**errp
)
2892 Error
*local_err
= NULL
;
2893 BlockDriverState
*bs
;
2894 AioContext
*aio_context
;
2897 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2898 has_node_name
? node_name
: NULL
,
2901 error_propagate(errp
, local_err
);
2905 aio_context
= bdrv_get_aio_context(bs
);
2906 aio_context_acquire(aio_context
);
2908 if (!bdrv_is_first_non_filter(bs
)) {
2909 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2914 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2918 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2919 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2923 /* complete all in-flight operations before resizing the device */
2926 ret
= bdrv_truncate(bs
, size
);
2931 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2934 error_setg(errp
, QERR_UNSUPPORTED
);
2937 error_setg(errp
, "Device '%s' is read only", device
);
2940 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2943 error_setg_errno(errp
, -ret
, "Could not resize");
2948 aio_context_release(aio_context
);
2951 static void block_job_cb(void *opaque
, int ret
)
2953 /* Note that this function may be executed from another AioContext besides
2954 * the QEMU main loop. If you need to access anything that assumes the
2955 * QEMU global mutex, use a BH or introduce a mutex.
2958 BlockDriverState
*bs
= opaque
;
2959 const char *msg
= NULL
;
2961 trace_block_job_cb(bs
, bs
->job
, ret
);
2966 msg
= strerror(-ret
);
2969 if (block_job_is_cancelled(bs
->job
)) {
2970 block_job_event_cancelled(bs
->job
);
2972 block_job_event_completed(bs
->job
, msg
);
2976 void qmp_block_stream(const char *device
,
2977 bool has_base
, const char *base
,
2978 bool has_backing_file
, const char *backing_file
,
2979 bool has_speed
, int64_t speed
,
2980 bool has_on_error
, BlockdevOnError on_error
,
2984 BlockDriverState
*bs
;
2985 BlockDriverState
*base_bs
= NULL
;
2986 AioContext
*aio_context
;
2987 Error
*local_err
= NULL
;
2988 const char *base_name
= NULL
;
2990 if (!has_on_error
) {
2991 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2994 blk
= blk_by_name(device
);
2996 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2997 "Device '%s' not found", device
);
3001 aio_context
= blk_get_aio_context(blk
);
3002 aio_context_acquire(aio_context
);
3004 if (!blk_is_available(blk
)) {
3005 error_setg(errp
, "Device '%s' has no medium", device
);
3010 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3015 base_bs
= bdrv_find_backing_image(bs
, base
);
3016 if (base_bs
== NULL
) {
3017 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3020 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3024 /* if we are streaming the entire chain, the result will have no backing
3025 * file, and specifying one is therefore an error */
3026 if (base_bs
== NULL
&& has_backing_file
) {
3027 error_setg(errp
, "backing file specified, but streaming the "
3032 /* backing_file string overrides base bs filename */
3033 base_name
= has_backing_file
? backing_file
: base_name
;
3035 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
3036 on_error
, block_job_cb
, bs
, &local_err
);
3038 error_propagate(errp
, local_err
);
3042 trace_qmp_block_stream(bs
, bs
->job
);
3045 aio_context_release(aio_context
);
3048 void qmp_block_commit(const char *device
,
3049 bool has_base
, const char *base
,
3050 bool has_top
, const char *top
,
3051 bool has_backing_file
, const char *backing_file
,
3052 bool has_speed
, int64_t speed
,
3056 BlockDriverState
*bs
;
3057 BlockDriverState
*base_bs
, *top_bs
;
3058 AioContext
*aio_context
;
3059 Error
*local_err
= NULL
;
3060 /* This will be part of the QMP command, if/when the
3061 * BlockdevOnError change for blkmirror makes it in
3063 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3070 * libvirt relies on the DeviceNotFound error class in order to probe for
3071 * live commit feature versions; for this to work, we must make sure to
3072 * perform the device lookup before any generic errors that may occur in a
3073 * scenario in which all optional arguments are omitted. */
3074 blk
= blk_by_name(device
);
3076 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3077 "Device '%s' not found", device
);
3081 aio_context
= blk_get_aio_context(blk
);
3082 aio_context_acquire(aio_context
);
3084 if (!blk_is_available(blk
)) {
3085 error_setg(errp
, "Device '%s' has no medium", device
);
3090 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3094 /* default top_bs is the active layer */
3097 if (has_top
&& top
) {
3098 if (strcmp(bs
->filename
, top
) != 0) {
3099 top_bs
= bdrv_find_backing_image(bs
, top
);
3103 if (top_bs
== NULL
) {
3104 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3108 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3110 if (has_base
&& base
) {
3111 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3113 base_bs
= bdrv_find_base(top_bs
);
3116 if (base_bs
== NULL
) {
3117 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3121 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3123 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3127 /* Do not allow attempts to commit an image into itself */
3128 if (top_bs
== base_bs
) {
3129 error_setg(errp
, "cannot commit an image into itself");
3134 if (has_backing_file
) {
3135 error_setg(errp
, "'backing-file' specified,"
3136 " but 'top' is the active layer");
3139 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3142 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3143 has_backing_file
? backing_file
: NULL
, &local_err
);
3145 if (local_err
!= NULL
) {
3146 error_propagate(errp
, local_err
);
3151 aio_context_release(aio_context
);
3154 static void do_drive_backup(const char *device
, const char *target
,
3155 bool has_format
, const char *format
,
3156 enum MirrorSyncMode sync
,
3157 bool has_mode
, enum NewImageMode mode
,
3158 bool has_speed
, int64_t speed
,
3159 bool has_bitmap
, const char *bitmap
,
3160 bool has_on_source_error
,
3161 BlockdevOnError on_source_error
,
3162 bool has_on_target_error
,
3163 BlockdevOnError on_target_error
,
3164 BlockJobTxn
*txn
, Error
**errp
)
3167 BlockDriverState
*bs
;
3168 BlockDriverState
*target_bs
;
3169 BlockDriverState
*source
= NULL
;
3170 BdrvDirtyBitmap
*bmap
= NULL
;
3171 AioContext
*aio_context
;
3172 QDict
*options
= NULL
;
3173 Error
*local_err
= NULL
;
3181 if (!has_on_source_error
) {
3182 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3184 if (!has_on_target_error
) {
3185 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3188 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3191 blk
= blk_by_name(device
);
3193 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3194 "Device '%s' not found", device
);
3198 aio_context
= blk_get_aio_context(blk
);
3199 aio_context_acquire(aio_context
);
3201 /* Although backup_run has this check too, we need to use bs->drv below, so
3202 * do an early check redundantly. */
3203 if (!blk_is_available(blk
)) {
3204 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3210 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3213 /* Early check to avoid creating target */
3214 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3218 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3220 /* See if we have a backing HD we can use to create our new image
3222 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3223 source
= backing_bs(bs
);
3225 sync
= MIRROR_SYNC_MODE_FULL
;
3228 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3232 size
= bdrv_getlength(bs
);
3234 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3238 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3241 bdrv_img_create(target
, format
, source
->filename
,
3242 source
->drv
->format_name
, NULL
,
3243 size
, flags
, &local_err
, false);
3245 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3246 size
, flags
, &local_err
, false);
3251 error_propagate(errp
, local_err
);
3256 options
= qdict_new();
3257 qdict_put(options
, "driver", qstring_from_str(format
));
3261 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3263 error_propagate(errp
, local_err
);
3267 bdrv_set_aio_context(target_bs
, aio_context
);
3270 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3272 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3273 bdrv_unref(target_bs
);
3278 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3279 on_source_error
, on_target_error
,
3280 block_job_cb
, bs
, txn
, &local_err
);
3281 if (local_err
!= NULL
) {
3282 bdrv_unref(target_bs
);
3283 error_propagate(errp
, local_err
);
3288 aio_context_release(aio_context
);
3291 void qmp_drive_backup(const char *device
, const char *target
,
3292 bool has_format
, const char *format
,
3293 enum MirrorSyncMode sync
,
3294 bool has_mode
, enum NewImageMode mode
,
3295 bool has_speed
, int64_t speed
,
3296 bool has_bitmap
, const char *bitmap
,
3297 bool has_on_source_error
, BlockdevOnError on_source_error
,
3298 bool has_on_target_error
, BlockdevOnError on_target_error
,
3301 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3302 has_mode
, mode
, has_speed
, speed
,
3304 has_on_source_error
, on_source_error
,
3305 has_on_target_error
, on_target_error
,
3309 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3311 return bdrv_named_nodes_list(errp
);
3314 void do_blockdev_backup(const char *device
, const char *target
,
3315 enum MirrorSyncMode sync
,
3316 bool has_speed
, int64_t speed
,
3317 bool has_on_source_error
,
3318 BlockdevOnError on_source_error
,
3319 bool has_on_target_error
,
3320 BlockdevOnError on_target_error
,
3321 BlockJobTxn
*txn
, Error
**errp
)
3323 BlockBackend
*blk
, *target_blk
;
3324 BlockDriverState
*bs
;
3325 BlockDriverState
*target_bs
;
3326 Error
*local_err
= NULL
;
3327 AioContext
*aio_context
;
3332 if (!has_on_source_error
) {
3333 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3335 if (!has_on_target_error
) {
3336 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3339 blk
= blk_by_name(device
);
3341 error_setg(errp
, "Device '%s' not found", device
);
3345 aio_context
= blk_get_aio_context(blk
);
3346 aio_context_acquire(aio_context
);
3348 if (!blk_is_available(blk
)) {
3349 error_setg(errp
, "Device '%s' has no medium", device
);
3354 target_blk
= blk_by_name(target
);
3356 error_setg(errp
, "Device '%s' not found", target
);
3360 if (!blk_is_available(target_blk
)) {
3361 error_setg(errp
, "Device '%s' has no medium", target
);
3364 target_bs
= blk_bs(target_blk
);
3366 bdrv_ref(target_bs
);
3367 bdrv_set_aio_context(target_bs
, aio_context
);
3368 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3369 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3370 if (local_err
!= NULL
) {
3371 bdrv_unref(target_bs
);
3372 error_propagate(errp
, local_err
);
3375 aio_context_release(aio_context
);
3378 void qmp_blockdev_backup(const char *device
, const char *target
,
3379 enum MirrorSyncMode sync
,
3380 bool has_speed
, int64_t speed
,
3381 bool has_on_source_error
,
3382 BlockdevOnError on_source_error
,
3383 bool has_on_target_error
,
3384 BlockdevOnError on_target_error
,
3387 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3388 has_on_source_error
, on_source_error
,
3389 has_on_target_error
, on_target_error
,
3393 /* Parameter check and block job starting for drive mirroring.
3394 * Caller should hold @device and @target's aio context (must be the same).
3396 static void blockdev_mirror_common(BlockDriverState
*bs
,
3397 BlockDriverState
*target
,
3398 bool has_replaces
, const char *replaces
,
3399 enum MirrorSyncMode sync
,
3400 bool has_speed
, int64_t speed
,
3401 bool has_granularity
, uint32_t granularity
,
3402 bool has_buf_size
, int64_t buf_size
,
3403 bool has_on_source_error
,
3404 BlockdevOnError on_source_error
,
3405 bool has_on_target_error
,
3406 BlockdevOnError on_target_error
,
3407 bool has_unmap
, bool unmap
,
3414 if (!has_on_source_error
) {
3415 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3417 if (!has_on_target_error
) {
3418 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3420 if (!has_granularity
) {
3423 if (!has_buf_size
) {
3430 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3431 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3432 "a value in range [512B, 64MB]");
3435 if (granularity
& (granularity
- 1)) {
3436 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3441 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3444 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3448 error_setg(errp
, "Cannot mirror to an attached block device");
3452 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3453 sync
= MIRROR_SYNC_MODE_FULL
;
3456 /* pass the node name to replace to mirror start since it's loose coupling
3457 * and will allow to check whether the node still exist at mirror completion
3459 mirror_start(bs
, target
,
3460 has_replaces
? replaces
: NULL
,
3461 speed
, granularity
, buf_size
, sync
,
3462 on_source_error
, on_target_error
, unmap
,
3463 block_job_cb
, bs
, errp
);
3466 void qmp_drive_mirror(const char *device
, const char *target
,
3467 bool has_format
, const char *format
,
3468 bool has_node_name
, const char *node_name
,
3469 bool has_replaces
, const char *replaces
,
3470 enum MirrorSyncMode sync
,
3471 bool has_mode
, enum NewImageMode mode
,
3472 bool has_speed
, int64_t speed
,
3473 bool has_granularity
, uint32_t granularity
,
3474 bool has_buf_size
, int64_t buf_size
,
3475 bool has_on_source_error
, BlockdevOnError on_source_error
,
3476 bool has_on_target_error
, BlockdevOnError on_target_error
,
3477 bool has_unmap
, bool unmap
,
3480 BlockDriverState
*bs
;
3482 BlockDriverState
*source
, *target_bs
;
3483 AioContext
*aio_context
;
3484 Error
*local_err
= NULL
;
3485 QDict
*options
= NULL
;
3490 blk
= blk_by_name(device
);
3492 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3493 "Device '%s' not found", device
);
3497 aio_context
= blk_get_aio_context(blk
);
3498 aio_context_acquire(aio_context
);
3500 if (!blk_is_available(blk
)) {
3501 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3506 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3510 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3513 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3514 source
= backing_bs(bs
);
3515 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3516 sync
= MIRROR_SYNC_MODE_FULL
;
3518 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3522 size
= bdrv_getlength(bs
);
3524 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3529 BlockDriverState
*to_replace_bs
;
3530 AioContext
*replace_aio_context
;
3531 int64_t replace_size
;
3533 if (!has_node_name
) {
3534 error_setg(errp
, "a node-name must be provided when replacing a"
3535 " named node of the graph");
3539 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3541 if (!to_replace_bs
) {
3542 error_propagate(errp
, local_err
);
3546 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3547 aio_context_acquire(replace_aio_context
);
3548 replace_size
= bdrv_getlength(to_replace_bs
);
3549 aio_context_release(replace_aio_context
);
3551 if (size
!= replace_size
) {
3552 error_setg(errp
, "cannot replace image with a mirror image of "
3558 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3559 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3561 /* create new image w/o backing file */
3563 bdrv_img_create(target
, format
,
3564 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3567 case NEW_IMAGE_MODE_EXISTING
:
3569 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3570 /* create new image with backing file */
3571 bdrv_img_create(target
, format
,
3573 source
->drv
->format_name
,
3574 NULL
, size
, flags
, &local_err
, false);
3582 error_propagate(errp
, local_err
);
3586 options
= qdict_new();
3587 if (has_node_name
) {
3588 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3591 qdict_put(options
, "driver", qstring_from_str(format
));
3594 /* Mirroring takes care of copy-on-write using the source's backing
3598 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3599 flags
| BDRV_O_NO_BACKING
, &local_err
);
3601 error_propagate(errp
, local_err
);
3605 bdrv_set_aio_context(target_bs
, aio_context
);
3607 blockdev_mirror_common(bs
, target_bs
,
3608 has_replaces
, replaces
, sync
,
3610 has_granularity
, granularity
,
3611 has_buf_size
, buf_size
,
3612 has_on_source_error
, on_source_error
,
3613 has_on_target_error
, on_target_error
,
3617 error_propagate(errp
, local_err
);
3618 bdrv_unref(target_bs
);
3621 aio_context_release(aio_context
);
3624 void qmp_blockdev_mirror(const char *device
, const char *target
,
3625 bool has_replaces
, const char *replaces
,
3626 MirrorSyncMode sync
,
3627 bool has_speed
, int64_t speed
,
3628 bool has_granularity
, uint32_t granularity
,
3629 bool has_buf_size
, int64_t buf_size
,
3630 bool has_on_source_error
,
3631 BlockdevOnError on_source_error
,
3632 bool has_on_target_error
,
3633 BlockdevOnError on_target_error
,
3636 BlockDriverState
*bs
;
3638 BlockDriverState
*target_bs
;
3639 AioContext
*aio_context
;
3640 Error
*local_err
= NULL
;
3642 blk
= blk_by_name(device
);
3644 error_setg(errp
, "Device '%s' not found", device
);
3650 error_setg(errp
, "Device '%s' has no media", device
);
3654 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3659 aio_context
= bdrv_get_aio_context(bs
);
3660 aio_context_acquire(aio_context
);
3662 bdrv_ref(target_bs
);
3663 bdrv_set_aio_context(target_bs
, aio_context
);
3665 blockdev_mirror_common(bs
, target_bs
,
3666 has_replaces
, replaces
, sync
,
3668 has_granularity
, granularity
,
3669 has_buf_size
, buf_size
,
3670 has_on_source_error
, on_source_error
,
3671 has_on_target_error
, on_target_error
,
3675 error_propagate(errp
, local_err
);
3676 bdrv_unref(target_bs
);
3679 aio_context_release(aio_context
);
3682 /* Get the block job for a given device name and acquire its AioContext */
3683 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3687 BlockDriverState
*bs
;
3689 *aio_context
= NULL
;
3691 blk
= blk_by_name(device
);
3696 *aio_context
= blk_get_aio_context(blk
);
3697 aio_context_acquire(*aio_context
);
3699 if (!blk_is_available(blk
)) {
3711 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3712 "No active block job on device '%s'", device
);
3714 aio_context_release(*aio_context
);
3715 *aio_context
= NULL
;
3720 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3722 AioContext
*aio_context
;
3723 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3729 block_job_set_speed(job
, speed
, errp
);
3730 aio_context_release(aio_context
);
3733 void qmp_block_job_cancel(const char *device
,
3734 bool has_force
, bool force
, Error
**errp
)
3736 AioContext
*aio_context
;
3737 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3747 if (job
->user_paused
&& !force
) {
3748 error_setg(errp
, "The block job for device '%s' is currently paused",
3753 trace_qmp_block_job_cancel(job
);
3754 block_job_cancel(job
);
3756 aio_context_release(aio_context
);
3759 void qmp_block_job_pause(const char *device
, Error
**errp
)
3761 AioContext
*aio_context
;
3762 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3764 if (!job
|| job
->user_paused
) {
3768 job
->user_paused
= true;
3769 trace_qmp_block_job_pause(job
);
3770 block_job_pause(job
);
3771 aio_context_release(aio_context
);
3774 void qmp_block_job_resume(const char *device
, Error
**errp
)
3776 AioContext
*aio_context
;
3777 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3779 if (!job
|| !job
->user_paused
) {
3783 job
->user_paused
= false;
3784 trace_qmp_block_job_resume(job
);
3785 block_job_resume(job
);
3786 aio_context_release(aio_context
);
3789 void qmp_block_job_complete(const char *device
, Error
**errp
)
3791 AioContext
*aio_context
;
3792 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3798 trace_qmp_block_job_complete(job
);
3799 block_job_complete(job
, errp
);
3800 aio_context_release(aio_context
);
3803 void qmp_change_backing_file(const char *device
,
3804 const char *image_node_name
,
3805 const char *backing_file
,
3809 BlockDriverState
*bs
= NULL
;
3810 AioContext
*aio_context
;
3811 BlockDriverState
*image_bs
= NULL
;
3812 Error
*local_err
= NULL
;
3817 blk
= blk_by_name(device
);
3819 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3820 "Device '%s' not found", device
);
3824 aio_context
= blk_get_aio_context(blk
);
3825 aio_context_acquire(aio_context
);
3827 if (!blk_is_available(blk
)) {
3828 error_setg(errp
, "Device '%s' has no medium", device
);
3833 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3835 error_propagate(errp
, local_err
);
3840 error_setg(errp
, "image file not found");
3844 if (bdrv_find_base(image_bs
) == image_bs
) {
3845 error_setg(errp
, "not allowing backing file change on an image "
3846 "without a backing file");
3850 /* even though we are not necessarily operating on bs, we need it to
3851 * determine if block ops are currently prohibited on the chain */
3852 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3856 /* final sanity check */
3857 if (!bdrv_chain_contains(bs
, image_bs
)) {
3858 error_setg(errp
, "'%s' and image file are not in the same chain",
3863 /* if not r/w, reopen to make r/w */
3864 open_flags
= image_bs
->open_flags
;
3865 ro
= bdrv_is_read_only(image_bs
);
3868 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3870 error_propagate(errp
, local_err
);
3875 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3876 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3879 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3881 /* don't exit here, so we can try to restore open flags if
3886 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3888 error_propagate(errp
, local_err
); /* will preserve prior errp */
3893 aio_context_release(aio_context
);
3896 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3900 Error
*local_err
= NULL
;
3902 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3907 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3909 if (!qdict_get_try_str(qdict
, "node-name")) {
3911 error_report("'node-name' needs to be specified");
3915 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3917 error_report_err(local_err
);
3921 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3924 qemu_opts_del(opts
);
3927 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3929 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3930 BlockDriverState
*bs
;
3931 BlockBackend
*blk
= NULL
;
3934 Error
*local_err
= NULL
;
3936 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3937 * cache.direct=false instead of silently switching to aio=threads, except
3938 * when called from drive_new().
3940 * For now, simply forbidding the combination for all drivers will do. */
3941 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3942 bool direct
= options
->has_cache
&&
3943 options
->cache
->has_direct
&&
3944 options
->cache
->direct
;
3946 error_setg(errp
, "aio=native requires cache.direct=true");
3951 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
), NULL
, &options
,
3954 error_propagate(errp
, local_err
);
3958 obj
= qmp_output_get_qobject(ov
);
3959 qdict
= qobject_to_qdict(obj
);
3961 qdict_flatten(qdict
);
3963 if (options
->has_id
) {
3964 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3966 error_propagate(errp
, local_err
);
3972 if (!qdict_get_try_str(qdict
, "node-name")) {
3973 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3978 bs
= bds_tree_init(qdict
, errp
);
3983 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3986 if (bs
&& bdrv_key_required(bs
)) {
3988 monitor_remove_blk(blk
);
3991 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3994 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3999 qmp_output_visitor_cleanup(ov
);
4002 void qmp_x_blockdev_del(bool has_id
, const char *id
,
4003 bool has_node_name
, const char *node_name
, Error
**errp
)
4005 AioContext
*aio_context
;
4007 BlockDriverState
*bs
;
4009 if (has_id
&& has_node_name
) {
4010 error_setg(errp
, "Only one of id and node-name must be specified");
4012 } else if (!has_id
&& !has_node_name
) {
4013 error_setg(errp
, "No block device specified");
4018 /* blk_by_name() never returns a BB that is not owned by the monitor */
4019 blk
= blk_by_name(id
);
4021 error_setg(errp
, "Cannot find block backend %s", id
);
4024 if (blk_get_refcnt(blk
) > 1) {
4025 error_setg(errp
, "Block backend %s is in use", id
);
4029 aio_context
= blk_get_aio_context(blk
);
4031 bs
= bdrv_find_node(node_name
);
4033 error_setg(errp
, "Cannot find node %s", node_name
);
4038 error_setg(errp
, "Node %s is in use by %s",
4039 node_name
, blk_name(blk
));
4042 aio_context
= bdrv_get_aio_context(bs
);
4045 aio_context_acquire(aio_context
);
4048 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4052 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
4053 error_setg(errp
, "Node %s is not owned by the monitor",
4058 if (bs
->refcnt
> 1) {
4059 error_setg(errp
, "Block device %s is in use",
4060 bdrv_get_device_or_node_name(bs
));
4066 monitor_remove_blk(blk
);
4069 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4074 aio_context_release(aio_context
);
4077 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4079 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4080 BlockDriverState
*bs
;
4082 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4083 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
4085 aio_context_acquire(aio_context
);
4088 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4089 elem
->value
= block_job_query(bs
->job
);
4091 p_next
= &elem
->next
;
4094 aio_context_release(aio_context
);
4100 QemuOptsList qemu_common_drive_opts
= {
4102 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4106 .type
= QEMU_OPT_BOOL
,
4107 .help
= "enable/disable snapshot mode",
4110 .type
= QEMU_OPT_STRING
,
4111 .help
= "discard operation (ignore/off, unmap/on)",
4114 .type
= QEMU_OPT_STRING
,
4115 .help
= "host AIO implementation (threads, native)",
4118 .type
= QEMU_OPT_STRING
,
4119 .help
= "disk format (raw, qcow2, ...)",
4122 .type
= QEMU_OPT_STRING
,
4123 .help
= "read error action",
4126 .type
= QEMU_OPT_STRING
,
4127 .help
= "write error action",
4129 .name
= "read-only",
4130 .type
= QEMU_OPT_BOOL
,
4131 .help
= "open drive file as read-only",
4133 .name
= "throttling.iops-total",
4134 .type
= QEMU_OPT_NUMBER
,
4135 .help
= "limit total I/O operations per second",
4137 .name
= "throttling.iops-read",
4138 .type
= QEMU_OPT_NUMBER
,
4139 .help
= "limit read operations per second",
4141 .name
= "throttling.iops-write",
4142 .type
= QEMU_OPT_NUMBER
,
4143 .help
= "limit write operations per second",
4145 .name
= "throttling.bps-total",
4146 .type
= QEMU_OPT_NUMBER
,
4147 .help
= "limit total bytes per second",
4149 .name
= "throttling.bps-read",
4150 .type
= QEMU_OPT_NUMBER
,
4151 .help
= "limit read bytes per second",
4153 .name
= "throttling.bps-write",
4154 .type
= QEMU_OPT_NUMBER
,
4155 .help
= "limit write bytes per second",
4157 .name
= "throttling.iops-total-max",
4158 .type
= QEMU_OPT_NUMBER
,
4159 .help
= "I/O operations burst",
4161 .name
= "throttling.iops-read-max",
4162 .type
= QEMU_OPT_NUMBER
,
4163 .help
= "I/O operations read burst",
4165 .name
= "throttling.iops-write-max",
4166 .type
= QEMU_OPT_NUMBER
,
4167 .help
= "I/O operations write burst",
4169 .name
= "throttling.bps-total-max",
4170 .type
= QEMU_OPT_NUMBER
,
4171 .help
= "total bytes burst",
4173 .name
= "throttling.bps-read-max",
4174 .type
= QEMU_OPT_NUMBER
,
4175 .help
= "total bytes read burst",
4177 .name
= "throttling.bps-write-max",
4178 .type
= QEMU_OPT_NUMBER
,
4179 .help
= "total bytes write burst",
4181 .name
= "throttling.iops-total-max-length",
4182 .type
= QEMU_OPT_NUMBER
,
4183 .help
= "length of the iops-total-max burst period, in seconds",
4185 .name
= "throttling.iops-read-max-length",
4186 .type
= QEMU_OPT_NUMBER
,
4187 .help
= "length of the iops-read-max burst period, in seconds",
4189 .name
= "throttling.iops-write-max-length",
4190 .type
= QEMU_OPT_NUMBER
,
4191 .help
= "length of the iops-write-max burst period, in seconds",
4193 .name
= "throttling.bps-total-max-length",
4194 .type
= QEMU_OPT_NUMBER
,
4195 .help
= "length of the bps-total-max burst period, in seconds",
4197 .name
= "throttling.bps-read-max-length",
4198 .type
= QEMU_OPT_NUMBER
,
4199 .help
= "length of the bps-read-max burst period, in seconds",
4201 .name
= "throttling.bps-write-max-length",
4202 .type
= QEMU_OPT_NUMBER
,
4203 .help
= "length of the bps-write-max burst period, in seconds",
4205 .name
= "throttling.iops-size",
4206 .type
= QEMU_OPT_NUMBER
,
4207 .help
= "when limiting by iops max size of an I/O in bytes",
4209 .name
= "throttling.group",
4210 .type
= QEMU_OPT_STRING
,
4211 .help
= "name of the block throttling group",
4213 .name
= "copy-on-read",
4214 .type
= QEMU_OPT_BOOL
,
4215 .help
= "copy read data from backing file into image file",
4217 .name
= "detect-zeroes",
4218 .type
= QEMU_OPT_STRING
,
4219 .help
= "try to optimize zero writes (off, on, unmap)",
4221 .name
= "stats-account-invalid",
4222 .type
= QEMU_OPT_BOOL
,
4223 .help
= "whether to account for invalid I/O operations "
4224 "in the statistics",
4226 .name
= "stats-account-failed",
4227 .type
= QEMU_OPT_BOOL
,
4228 .help
= "whether to account for failed I/O operations "
4229 "in the statistics",
4231 { /* end of list */ }
4235 static QemuOptsList qemu_root_bds_opts
= {
4237 .head
= QTAILQ_HEAD_INITIALIZER(qemu_root_bds_opts
.head
),
4241 .type
= QEMU_OPT_STRING
,
4242 .help
= "discard operation (ignore/off, unmap/on)",
4245 .type
= QEMU_OPT_STRING
,
4246 .help
= "host AIO implementation (threads, native)",
4248 .name
= "read-only",
4249 .type
= QEMU_OPT_BOOL
,
4250 .help
= "open drive file as read-only",
4252 .name
= "copy-on-read",
4253 .type
= QEMU_OPT_BOOL
,
4254 .help
= "copy read data from backing file into image file",
4256 .name
= "detect-zeroes",
4257 .type
= QEMU_OPT_STRING
,
4258 .help
= "try to optimize zero writes (off, on, unmap)",
4260 { /* end of list */ }
4264 QemuOptsList qemu_drive_opts
= {
4266 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4269 * no elements => accept any params
4270 * validation will happen later
4272 { /* end of list */ }