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
) {
155 * Returns the current mapping of how many units per bus
156 * a particular interface can support.
158 * A positive integer indicates n units per bus.
159 * 0 implies the mapping has not been established.
160 * -1 indicates an invalid BlockInterfaceType was given.
162 int drive_get_max_devs(BlockInterfaceType type
)
164 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
165 return if_max_devs
[type
];
171 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
173 int max_devs
= if_max_devs
[type
];
174 return max_devs
? index
/ max_devs
: 0;
177 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
179 int max_devs
= if_max_devs
[type
];
180 return max_devs
? index
% max_devs
: index
;
183 QemuOpts
*drive_def(const char *optstr
)
185 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
188 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
193 opts
= drive_def(optstr
);
197 if (type
!= IF_DEFAULT
) {
198 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
201 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
204 qemu_opt_set(opts
, "file", file
, &error_abort
);
208 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
213 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
214 dinfo
= blk_legacy_dinfo(blk
);
215 if (dinfo
&& dinfo
->type
== type
216 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
224 bool drive_check_orphaned(void)
230 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
231 dinfo
= blk_legacy_dinfo(blk
);
232 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
233 /* Unless this is a default drive, this may be an oversight. */
234 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
235 dinfo
->type
!= IF_NONE
) {
236 fprintf(stderr
, "Warning: Orphaned drive without device: "
237 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
238 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
239 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
247 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
249 return drive_get(type
,
250 drive_index_to_bus_id(type
, index
),
251 drive_index_to_unit_id(type
, index
));
254 int drive_get_max_bus(BlockInterfaceType type
)
261 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
262 dinfo
= blk_legacy_dinfo(blk
);
263 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
264 max_bus
= dinfo
->bus
;
270 /* Get a block device. This should only be used for single-drive devices
271 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
273 DriveInfo
*drive_get_next(BlockInterfaceType type
)
275 static int next_block_unit
[IF_COUNT
];
277 return drive_get(type
, 0, next_block_unit
[type
]++);
280 static void bdrv_format_print(void *opaque
, const char *name
)
282 error_printf(" %s", name
);
287 BlockDriverState
*bs
;
290 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
292 if (!strcmp(buf
, "ignore")) {
293 return BLOCKDEV_ON_ERROR_IGNORE
;
294 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
295 return BLOCKDEV_ON_ERROR_ENOSPC
;
296 } else if (!strcmp(buf
, "stop")) {
297 return BLOCKDEV_ON_ERROR_STOP
;
298 } else if (!strcmp(buf
, "report")) {
299 return BLOCKDEV_ON_ERROR_REPORT
;
301 error_setg(errp
, "'%s' invalid %s error action",
302 buf
, is_read
? "read" : "write");
307 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
310 const QListEntry
*entry
;
311 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
312 switch (qobject_type(entry
->value
)) {
314 case QTYPE_QSTRING
: {
315 unsigned long long length
;
316 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
317 if (parse_uint_full(str
, &length
, 10) == 0 &&
318 length
> 0 && length
<= UINT_MAX
) {
319 block_acct_add_interval(stats
, (unsigned) length
);
321 error_setg(errp
, "Invalid interval length: %s", str
);
328 int64_t length
= qint_get_int(qobject_to_qint(entry
->value
));
329 if (length
> 0 && length
<= UINT_MAX
) {
330 block_acct_add_interval(stats
, (unsigned) length
);
332 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
339 error_setg(errp
, "The specification of stats-intervals is invalid");
346 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
348 /* All parameters but @opts are optional and may be set to NULL. */
349 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
350 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
351 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
354 Error
*local_error
= NULL
;
358 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
359 *bdrv_flags
|= BDRV_O_RDWR
;
361 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
362 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
365 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
366 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
367 error_setg(errp
, "Invalid discard option");
372 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
373 if (!strcmp(aio
, "native")) {
374 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
375 } else if (!strcmp(aio
, "threads")) {
376 /* this is the default */
378 error_setg(errp
, "invalid aio option");
384 /* disk I/O throttling */
385 if (throttling_group
) {
386 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
390 throttle_config_init(throttle_cfg
);
391 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
392 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
393 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
394 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
395 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
396 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
397 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
398 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
399 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
400 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
401 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
402 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
404 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
405 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
406 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
407 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
408 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
409 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
410 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
411 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
412 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
413 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
414 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
415 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
417 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
418 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
419 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
420 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
421 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
422 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
423 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
424 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
425 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
426 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
427 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
428 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
430 throttle_cfg
->op_size
=
431 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
433 if (!throttle_is_valid(throttle_cfg
, errp
)) {
440 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
441 qemu_opt_get(opts
, "detect-zeroes"),
442 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
443 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
446 error_propagate(errp
, local_error
);
451 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
452 !(*bdrv_flags
& BDRV_O_UNMAP
))
454 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
455 "without setting discard operation to unmap");
461 /* Takes the ownership of bs_opts */
462 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
467 int on_read_error
, on_write_error
;
468 bool account_invalid
, account_failed
;
470 BlockDriverState
*bs
;
475 QDict
*interval_dict
= NULL
;
476 QList
*interval_list
= NULL
;
478 BlockdevDetectZeroesOptions detect_zeroes
=
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
480 const char *throttling_group
= NULL
;
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id
= qdict_get_try_str(bs_opts
, "id");
485 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
487 error_propagate(errp
, error
);
491 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
493 error_propagate(errp
, error
);
498 qdict_del(bs_opts
, "id");
501 /* extract parameters */
502 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
504 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
505 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
507 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
508 qdict_array_split(interval_dict
, &interval_list
);
510 if (qdict_size(interval_dict
) != 0) {
511 error_setg(errp
, "Invalid option stats-intervals.%s",
512 qdict_first(interval_dict
)->key
);
516 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
517 &detect_zeroes
, &error
);
519 error_propagate(errp
, error
);
523 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
524 if (is_help_option(buf
)) {
525 error_printf("Supported formats:");
526 bdrv_iterate_format(bdrv_format_print
, NULL
);
531 if (qdict_haskey(bs_opts
, "driver")) {
532 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
535 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
538 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
539 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
540 on_write_error
= parse_block_error_action(buf
, 0, &error
);
542 error_propagate(errp
, error
);
547 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
548 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
549 on_read_error
= parse_block_error_action(buf
, 1, &error
);
551 error_propagate(errp
, error
);
557 bdrv_flags
|= BDRV_O_SNAPSHOT
;
561 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
562 BlockBackendRootState
*blk_rs
;
564 blk
= blk_new(qemu_opts_id(opts
), errp
);
569 blk_rs
= blk_get_root_state(blk
);
570 blk_rs
->open_flags
= bdrv_flags
;
571 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
572 blk_rs
->detect_zeroes
= detect_zeroes
;
574 if (throttle_enabled(&cfg
)) {
575 if (!throttling_group
) {
576 throttling_group
= blk_name(blk
);
578 blk_rs
->throttle_group
= g_strdup(throttling_group
);
579 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
580 blk_rs
->throttle_state
->cfg
= cfg
;
585 if (file
&& !*file
) {
589 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
590 * with other callers) rather than what we want as the real defaults.
591 * Apply the defaults here instead. */
592 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_WB
, "on");
593 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
594 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
596 if (runstate_check(RUN_STATE_INMIGRATE
)) {
597 bdrv_flags
|= BDRV_O_INACTIVE
;
600 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
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
);
635 QDECREF(interval_dict
);
636 QDECREF(interval_list
);
641 QDECREF(interval_dict
);
642 QDECREF(interval_list
);
648 static QemuOptsList qemu_root_bds_opts
;
650 /* Takes the ownership of bs_opts */
651 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
653 BlockDriverState
*bs
;
655 Error
*local_error
= NULL
;
656 BlockdevDetectZeroesOptions detect_zeroes
;
660 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
665 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
667 error_propagate(errp
, local_error
);
671 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
672 &detect_zeroes
, &local_error
);
674 error_propagate(errp
, local_error
);
678 if (runstate_check(RUN_STATE_INMIGRATE
)) {
679 bdrv_flags
|= BDRV_O_INACTIVE
;
683 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
685 goto fail_no_bs_opts
;
688 bs
->detect_zeroes
= detect_zeroes
;
700 void blockdev_close_all_bdrv_states(void)
702 BlockDriverState
*bs
, *next_bs
;
704 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
705 AioContext
*ctx
= bdrv_get_aio_context(bs
);
707 aio_context_acquire(ctx
);
709 aio_context_release(ctx
);
713 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
718 value
= qemu_opt_get(opts
, from
);
720 if (qemu_opt_find(opts
, to
)) {
721 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
722 "same time", to
, from
);
727 /* rename all items in opts */
728 while ((value
= qemu_opt_get(opts
, from
))) {
729 qemu_opt_set(opts
, to
, value
, &error_abort
);
730 qemu_opt_unset(opts
, from
);
734 QemuOptsList qemu_legacy_drive_opts
= {
736 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
740 .type
= QEMU_OPT_NUMBER
,
741 .help
= "bus number",
744 .type
= QEMU_OPT_NUMBER
,
745 .help
= "unit number (i.e. lun for scsi)",
748 .type
= QEMU_OPT_NUMBER
,
749 .help
= "index number",
752 .type
= QEMU_OPT_STRING
,
753 .help
= "media type (disk, cdrom)",
756 .type
= QEMU_OPT_STRING
,
757 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
760 .type
= QEMU_OPT_NUMBER
,
761 .help
= "number of cylinders (ide disk geometry)",
764 .type
= QEMU_OPT_NUMBER
,
765 .help
= "number of heads (ide disk geometry)",
768 .type
= QEMU_OPT_NUMBER
,
769 .help
= "number of sectors (ide disk geometry)",
772 .type
= QEMU_OPT_STRING
,
773 .help
= "chs translation (auto, lba, none)",
776 .type
= QEMU_OPT_BOOL
,
777 .help
= "(deprecated, ignored)",
780 .type
= QEMU_OPT_STRING
,
781 .help
= "pci address (virtio only)",
784 .type
= QEMU_OPT_STRING
,
785 .help
= "disk serial number",
788 .type
= QEMU_OPT_STRING
,
792 /* Options that are passed on, but have special semantics with -drive */
795 .type
= QEMU_OPT_BOOL
,
796 .help
= "open drive file as read-only",
799 .type
= QEMU_OPT_STRING
,
800 .help
= "read error action",
803 .type
= QEMU_OPT_STRING
,
804 .help
= "write error action",
806 .name
= "copy-on-read",
807 .type
= QEMU_OPT_BOOL
,
808 .help
= "copy read data from backing file into image file",
811 { /* end of list */ }
815 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
819 DriveInfo
*dinfo
= NULL
;
821 QemuOpts
*legacy_opts
;
822 DriveMediaType media
= MEDIA_DISK
;
823 BlockInterfaceType type
;
824 int cyls
, heads
, secs
, translation
;
825 int max_devs
, bus_id
, unit_id
, index
;
827 const char *werror
, *rerror
;
828 bool read_only
= false;
831 const char *filename
;
832 Error
*local_err
= NULL
;
835 /* Change legacy command line options into QMP ones */
836 static const struct {
840 { "iops", "throttling.iops-total" },
841 { "iops_rd", "throttling.iops-read" },
842 { "iops_wr", "throttling.iops-write" },
844 { "bps", "throttling.bps-total" },
845 { "bps_rd", "throttling.bps-read" },
846 { "bps_wr", "throttling.bps-write" },
848 { "iops_max", "throttling.iops-total-max" },
849 { "iops_rd_max", "throttling.iops-read-max" },
850 { "iops_wr_max", "throttling.iops-write-max" },
852 { "bps_max", "throttling.bps-total-max" },
853 { "bps_rd_max", "throttling.bps-read-max" },
854 { "bps_wr_max", "throttling.bps-write-max" },
856 { "iops_size", "throttling.iops-size" },
858 { "group", "throttling.group" },
860 { "readonly", "read-only" },
863 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
864 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
867 error_report_err(local_err
);
872 value
= qemu_opt_get(all_opts
, "cache");
876 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
877 error_report("invalid cache option");
881 /* Specific options take precedence */
882 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
883 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
884 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
886 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
887 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
888 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
890 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
891 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
892 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
894 qemu_opt_unset(all_opts
, "cache");
897 /* Get a QDict for processing the options */
898 bs_opts
= qdict_new();
899 qemu_opts_to_qdict(all_opts
, bs_opts
);
901 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
903 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
905 error_report_err(local_err
);
909 /* Deprecated option boot=[on|off] */
910 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
911 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
912 "ignored. Future versions will reject this parameter. Please "
913 "update your scripts.\n");
917 value
= qemu_opt_get(legacy_opts
, "media");
919 if (!strcmp(value
, "disk")) {
921 } else if (!strcmp(value
, "cdrom")) {
925 error_report("'%s' invalid media", value
);
930 /* copy-on-read is disabled with a warning for read-only devices */
931 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
932 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
934 if (read_only
&& copy_on_read
) {
935 error_report("warning: disabling copy-on-read on read-only drive");
936 copy_on_read
= false;
939 qdict_put(bs_opts
, "read-only",
940 qstring_from_str(read_only
? "on" : "off"));
941 qdict_put(bs_opts
, "copy-on-read",
942 qstring_from_str(copy_on_read
? "on" :"off"));
944 /* Controller type */
945 value
= qemu_opt_get(legacy_opts
, "if");
948 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
951 if (type
== IF_COUNT
) {
952 error_report("unsupported bus type '%s'", value
);
956 type
= block_default_type
;
960 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
961 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
962 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
964 if (cyls
|| heads
|| secs
) {
966 error_report("invalid physical cyls number");
970 error_report("invalid physical heads number");
974 error_report("invalid physical secs number");
979 translation
= BIOS_ATA_TRANSLATION_AUTO
;
980 value
= qemu_opt_get(legacy_opts
, "trans");
983 error_report("'%s' trans must be used with cyls, heads and secs",
987 if (!strcmp(value
, "none")) {
988 translation
= BIOS_ATA_TRANSLATION_NONE
;
989 } else if (!strcmp(value
, "lba")) {
990 translation
= BIOS_ATA_TRANSLATION_LBA
;
991 } else if (!strcmp(value
, "large")) {
992 translation
= BIOS_ATA_TRANSLATION_LARGE
;
993 } else if (!strcmp(value
, "rechs")) {
994 translation
= BIOS_ATA_TRANSLATION_RECHS
;
995 } else if (!strcmp(value
, "auto")) {
996 translation
= BIOS_ATA_TRANSLATION_AUTO
;
998 error_report("'%s' invalid translation type", value
);
1003 if (media
== MEDIA_CDROM
) {
1004 if (cyls
|| secs
|| heads
) {
1005 error_report("CHS can't be set with media=cdrom");
1010 /* Device address specified by bus/unit or index.
1011 * If none was specified, try to find the first free one. */
1012 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
1013 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
1014 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
1016 max_devs
= if_max_devs
[type
];
1019 if (bus_id
!= 0 || unit_id
!= -1) {
1020 error_report("index cannot be used with bus and unit");
1023 bus_id
= drive_index_to_bus_id(type
, index
);
1024 unit_id
= drive_index_to_unit_id(type
, index
);
1027 if (unit_id
== -1) {
1029 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1031 if (max_devs
&& unit_id
>= max_devs
) {
1032 unit_id
-= max_devs
;
1038 if (max_devs
&& unit_id
>= max_devs
) {
1039 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1043 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1044 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1045 bus_id
, unit_id
, index
);
1050 serial
= qemu_opt_get(legacy_opts
, "serial");
1052 /* no id supplied -> create one */
1053 if (qemu_opts_id(all_opts
) == NULL
) {
1055 const char *mediastr
= "";
1056 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1057 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1060 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1063 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1066 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1070 /* Add virtio block device */
1071 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1072 if (devaddr
&& type
!= IF_VIRTIO
) {
1073 error_report("addr is not supported by this bus type");
1077 if (type
== IF_VIRTIO
) {
1079 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1081 if (arch_type
== QEMU_ARCH_S390X
) {
1082 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1084 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1086 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1089 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1093 filename
= qemu_opt_get(legacy_opts
, "file");
1095 /* Check werror/rerror compatibility with if=... */
1096 werror
= qemu_opt_get(legacy_opts
, "werror");
1097 if (werror
!= NULL
) {
1098 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1100 error_report("werror is not supported by this bus type");
1103 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1106 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1107 if (rerror
!= NULL
) {
1108 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1110 error_report("rerror is not supported by this bus type");
1113 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1116 /* Actual block device init: Functionality shared with blockdev-add */
1117 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1121 error_report_err(local_err
);
1128 /* Create legacy DriveInfo */
1129 dinfo
= g_malloc0(sizeof(*dinfo
));
1130 dinfo
->opts
= all_opts
;
1133 dinfo
->heads
= heads
;
1135 dinfo
->trans
= translation
;
1138 dinfo
->bus
= bus_id
;
1139 dinfo
->unit
= unit_id
;
1140 dinfo
->devaddr
= devaddr
;
1141 dinfo
->serial
= g_strdup(serial
);
1143 blk_set_legacy_dinfo(blk
, dinfo
);
1150 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1157 qemu_opts_del(legacy_opts
);
1162 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1164 const char *device
= qdict_get_str(qdict
, "device");
1168 if (!strcmp(device
, "all")) {
1169 ret
= bdrv_commit_all();
1171 BlockDriverState
*bs
;
1172 AioContext
*aio_context
;
1174 blk
= blk_by_name(device
);
1176 monitor_printf(mon
, "Device '%s' not found\n", device
);
1179 if (!blk_is_available(blk
)) {
1180 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1185 aio_context
= bdrv_get_aio_context(bs
);
1186 aio_context_acquire(aio_context
);
1188 ret
= bdrv_commit(bs
);
1190 aio_context_release(aio_context
);
1193 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1198 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1200 TransactionActionList list
;
1202 list
.value
= action
;
1204 qmp_transaction(&list
, false, NULL
, errp
);
1207 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1208 bool has_node_name
, const char *node_name
,
1209 const char *snapshot_file
,
1210 bool has_snapshot_node_name
,
1211 const char *snapshot_node_name
,
1212 bool has_format
, const char *format
,
1213 bool has_mode
, NewImageMode mode
, Error
**errp
)
1215 BlockdevSnapshotSync snapshot
= {
1216 .has_device
= has_device
,
1217 .device
= (char *) device
,
1218 .has_node_name
= has_node_name
,
1219 .node_name
= (char *) node_name
,
1220 .snapshot_file
= (char *) snapshot_file
,
1221 .has_snapshot_node_name
= has_snapshot_node_name
,
1222 .snapshot_node_name
= (char *) snapshot_node_name
,
1223 .has_format
= has_format
,
1224 .format
= (char *) format
,
1225 .has_mode
= has_mode
,
1228 TransactionAction action
= {
1229 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1230 .u
.blockdev_snapshot_sync
= &snapshot
,
1232 blockdev_do_action(&action
, errp
);
1235 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1238 BlockdevSnapshot snapshot_data
= {
1239 .node
= (char *) node
,
1240 .overlay
= (char *) overlay
1242 TransactionAction action
= {
1243 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1244 .u
.blockdev_snapshot
= &snapshot_data
,
1246 blockdev_do_action(&action
, errp
);
1249 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1253 BlockdevSnapshotInternal snapshot
= {
1254 .device
= (char *) device
,
1255 .name
= (char *) name
1257 TransactionAction action
= {
1258 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1259 .u
.blockdev_snapshot_internal_sync
= &snapshot
,
1261 blockdev_do_action(&action
, errp
);
1264 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1271 BlockDriverState
*bs
;
1273 AioContext
*aio_context
;
1274 QEMUSnapshotInfo sn
;
1275 Error
*local_err
= NULL
;
1276 SnapshotInfo
*info
= NULL
;
1279 blk
= blk_by_name(device
);
1281 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1282 "Device '%s' not found", device
);
1286 aio_context
= blk_get_aio_context(blk
);
1287 aio_context_acquire(aio_context
);
1298 error_setg(errp
, "Name or id must be provided");
1299 goto out_aio_context
;
1302 if (!blk_is_available(blk
)) {
1303 error_setg(errp
, "Device '%s' has no medium", device
);
1304 goto out_aio_context
;
1308 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1309 goto out_aio_context
;
1312 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1314 error_propagate(errp
, local_err
);
1315 goto out_aio_context
;
1319 "Snapshot with id '%s' and name '%s' does not exist on "
1321 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1322 goto out_aio_context
;
1325 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1327 error_propagate(errp
, local_err
);
1328 goto out_aio_context
;
1331 aio_context_release(aio_context
);
1333 info
= g_new0(SnapshotInfo
, 1);
1334 info
->id
= g_strdup(sn
.id_str
);
1335 info
->name
= g_strdup(sn
.name
);
1336 info
->date_nsec
= sn
.date_nsec
;
1337 info
->date_sec
= sn
.date_sec
;
1338 info
->vm_state_size
= sn
.vm_state_size
;
1339 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1340 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1345 aio_context_release(aio_context
);
1350 * block_dirty_bitmap_lookup:
1351 * Return a dirty bitmap (if present), after validating
1352 * the node reference and bitmap names.
1354 * @node: The name of the BDS node to search for bitmaps
1355 * @name: The name of the bitmap to search for
1356 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1357 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1358 * @errp: Output pointer for error information. Can be NULL.
1360 * @return: A bitmap object on success, or NULL on failure.
1362 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1364 BlockDriverState
**pbs
,
1368 BlockDriverState
*bs
;
1369 BdrvDirtyBitmap
*bitmap
;
1370 AioContext
*aio_context
;
1373 error_setg(errp
, "Node cannot be NULL");
1377 error_setg(errp
, "Bitmap name cannot be NULL");
1380 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1382 error_setg(errp
, "Node '%s' not found", node
);
1386 aio_context
= bdrv_get_aio_context(bs
);
1387 aio_context_acquire(aio_context
);
1389 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1391 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1399 *paio
= aio_context
;
1401 aio_context_release(aio_context
);
1407 aio_context_release(aio_context
);
1411 /* New and old BlockDriverState structs for atomic group operations */
1413 typedef struct BlkActionState BlkActionState
;
1417 * Table of operations that define an Action.
1419 * @instance_size: Size of state struct, in bytes.
1420 * @prepare: Prepare the work, must NOT be NULL.
1421 * @commit: Commit the changes, can be NULL.
1422 * @abort: Abort the changes on fail, can be NULL.
1423 * @clean: Clean up resources after all transaction actions have called
1424 * commit() or abort(). Can be NULL.
1426 * Only prepare() may fail. In a single transaction, only one of commit() or
1427 * abort() will be called. clean() will always be called if it is present.
1429 typedef struct BlkActionOps
{
1430 size_t instance_size
;
1431 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1432 void (*commit
)(BlkActionState
*common
);
1433 void (*abort
)(BlkActionState
*common
);
1434 void (*clean
)(BlkActionState
*common
);
1439 * Describes one Action's state within a Transaction.
1441 * @action: QAPI-defined enum identifying which Action to perform.
1442 * @ops: Table of ActionOps this Action can perform.
1443 * @block_job_txn: Transaction which this action belongs to.
1444 * @entry: List membership for all Actions in this Transaction.
1446 * This structure must be arranged as first member in a subclassed type,
1447 * assuming that the compiler will also arrange it to the same offsets as the
1450 struct BlkActionState
{
1451 TransactionAction
*action
;
1452 const BlkActionOps
*ops
;
1453 BlockJobTxn
*block_job_txn
;
1454 TransactionProperties
*txn_props
;
1455 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1458 /* internal snapshot private data */
1459 typedef struct InternalSnapshotState
{
1460 BlkActionState common
;
1461 BlockDriverState
*bs
;
1462 AioContext
*aio_context
;
1463 QEMUSnapshotInfo sn
;
1465 } InternalSnapshotState
;
1468 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1470 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1472 "Action '%s' does not support Transaction property "
1473 "completion-mode = %s",
1474 TransactionActionKind_lookup
[s
->action
->type
],
1475 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1481 static void internal_snapshot_prepare(BlkActionState
*common
,
1484 Error
*local_err
= NULL
;
1488 BlockDriverState
*bs
;
1489 QEMUSnapshotInfo old_sn
, *sn
;
1492 BlockdevSnapshotInternal
*internal
;
1493 InternalSnapshotState
*state
;
1496 g_assert(common
->action
->type
==
1497 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1498 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1499 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1501 /* 1. parse input */
1502 device
= internal
->device
;
1503 name
= internal
->name
;
1505 /* 2. check for validation */
1506 if (action_check_completion_mode(common
, errp
) < 0) {
1510 blk
= blk_by_name(device
);
1512 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1513 "Device '%s' not found", device
);
1517 /* AioContext is released in .clean() */
1518 state
->aio_context
= blk_get_aio_context(blk
);
1519 aio_context_acquire(state
->aio_context
);
1521 if (!blk_is_available(blk
)) {
1522 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1528 bdrv_drained_begin(bs
);
1530 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1534 if (bdrv_is_read_only(bs
)) {
1535 error_setg(errp
, "Device '%s' is read only", device
);
1539 if (!bdrv_can_snapshot(bs
)) {
1540 error_setg(errp
, "Block format '%s' used by device '%s' "
1541 "does not support internal snapshots",
1542 bs
->drv
->format_name
, device
);
1546 if (!strlen(name
)) {
1547 error_setg(errp
, "Name is empty");
1551 /* check whether a snapshot with name exist */
1552 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1555 error_propagate(errp
, local_err
);
1559 "Snapshot with name '%s' already exists on device '%s'",
1564 /* 3. take the snapshot */
1566 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1567 qemu_gettimeofday(&tv
);
1568 sn
->date_sec
= tv
.tv_sec
;
1569 sn
->date_nsec
= tv
.tv_usec
* 1000;
1570 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1572 ret1
= bdrv_snapshot_create(bs
, sn
);
1574 error_setg_errno(errp
, -ret1
,
1575 "Failed to create snapshot '%s' on device '%s'",
1580 /* 4. succeed, mark a snapshot is created */
1581 state
->created
= true;
1584 static void internal_snapshot_abort(BlkActionState
*common
)
1586 InternalSnapshotState
*state
=
1587 DO_UPCAST(InternalSnapshotState
, common
, common
);
1588 BlockDriverState
*bs
= state
->bs
;
1589 QEMUSnapshotInfo
*sn
= &state
->sn
;
1590 Error
*local_error
= NULL
;
1592 if (!state
->created
) {
1596 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1597 error_reportf_err(local_error
,
1598 "Failed to delete snapshot with id '%s' and "
1599 "name '%s' on device '%s' in abort: ",
1600 sn
->id_str
, sn
->name
,
1601 bdrv_get_device_name(bs
));
1605 static void internal_snapshot_clean(BlkActionState
*common
)
1607 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1610 if (state
->aio_context
) {
1612 bdrv_drained_end(state
->bs
);
1614 aio_context_release(state
->aio_context
);
1618 /* external snapshot private data */
1619 typedef struct ExternalSnapshotState
{
1620 BlkActionState common
;
1621 BlockDriverState
*old_bs
;
1622 BlockDriverState
*new_bs
;
1623 AioContext
*aio_context
;
1624 } ExternalSnapshotState
;
1626 static void external_snapshot_prepare(BlkActionState
*common
,
1630 QDict
*options
= NULL
;
1631 Error
*local_err
= NULL
;
1632 /* Device and node name of the image to generate the snapshot from */
1634 const char *node_name
;
1635 /* Reference to the new image (for 'blockdev-snapshot') */
1636 const char *snapshot_ref
;
1637 /* File name of the new image (for 'blockdev-snapshot-sync') */
1638 const char *new_image_file
;
1639 ExternalSnapshotState
*state
=
1640 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1641 TransactionAction
*action
= common
->action
;
1643 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1644 * purpose but a different set of parameters */
1645 switch (action
->type
) {
1646 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1648 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1650 node_name
= s
->node
;
1651 new_image_file
= NULL
;
1652 snapshot_ref
= s
->overlay
;
1655 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1657 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1658 device
= s
->has_device
? s
->device
: NULL
;
1659 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1660 new_image_file
= s
->snapshot_file
;
1661 snapshot_ref
= NULL
;
1665 g_assert_not_reached();
1668 /* start processing */
1669 if (action_check_completion_mode(common
, errp
) < 0) {
1673 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1674 if (!state
->old_bs
) {
1678 /* Acquire AioContext now so any threads operating on old_bs stop */
1679 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1680 aio_context_acquire(state
->aio_context
);
1681 bdrv_drained_begin(state
->old_bs
);
1683 if (!bdrv_is_inserted(state
->old_bs
)) {
1684 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1688 if (bdrv_op_is_blocked(state
->old_bs
,
1689 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1693 if (!bdrv_is_read_only(state
->old_bs
)) {
1694 if (bdrv_flush(state
->old_bs
)) {
1695 error_setg(errp
, QERR_IO_ERROR
);
1700 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1701 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1705 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1706 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1707 const char *format
= s
->has_format
? s
->format
: "qcow2";
1708 enum NewImageMode mode
;
1709 const char *snapshot_node_name
=
1710 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1712 if (node_name
&& !snapshot_node_name
) {
1713 error_setg(errp
, "New snapshot node name missing");
1717 if (snapshot_node_name
&&
1718 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1719 error_setg(errp
, "New snapshot node name already in use");
1723 flags
= state
->old_bs
->open_flags
;
1725 /* create new image w/backing file */
1726 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1727 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1728 int64_t size
= bdrv_getlength(state
->old_bs
);
1730 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1733 bdrv_img_create(new_image_file
, format
,
1734 state
->old_bs
->filename
,
1735 state
->old_bs
->drv
->format_name
,
1736 NULL
, size
, flags
, &local_err
, false);
1738 error_propagate(errp
, local_err
);
1743 options
= qdict_new();
1744 if (s
->has_snapshot_node_name
) {
1745 qdict_put(options
, "node-name",
1746 qstring_from_str(snapshot_node_name
));
1748 qdict_put(options
, "driver", qstring_from_str(format
));
1750 flags
|= BDRV_O_NO_BACKING
;
1753 assert(state
->new_bs
== NULL
);
1754 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1756 /* We will manually add the backing_hd field to the bs later */
1761 if (state
->new_bs
->blk
!= NULL
) {
1762 error_setg(errp
, "The snapshot is already in use by %s",
1763 blk_name(state
->new_bs
->blk
));
1767 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1772 if (state
->new_bs
->backing
!= NULL
) {
1773 error_setg(errp
, "The snapshot already has a backing image");
1777 if (!state
->new_bs
->drv
->supports_backing
) {
1778 error_setg(errp
, "The snapshot does not support backing images");
1782 static void external_snapshot_commit(BlkActionState
*common
)
1784 ExternalSnapshotState
*state
=
1785 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1787 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1789 /* This removes our old bs and adds the new bs */
1790 bdrv_append(state
->new_bs
, state
->old_bs
);
1791 /* We don't need (or want) to use the transactional
1792 * bdrv_reopen_multiple() across all the entries at once, because we
1793 * don't want to abort all of them if one of them fails the reopen */
1794 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1798 static void external_snapshot_abort(BlkActionState
*common
)
1800 ExternalSnapshotState
*state
=
1801 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1802 if (state
->new_bs
) {
1803 bdrv_unref(state
->new_bs
);
1807 static void external_snapshot_clean(BlkActionState
*common
)
1809 ExternalSnapshotState
*state
=
1810 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1811 if (state
->aio_context
) {
1812 bdrv_drained_end(state
->old_bs
);
1813 aio_context_release(state
->aio_context
);
1817 typedef struct DriveBackupState
{
1818 BlkActionState common
;
1819 BlockDriverState
*bs
;
1820 AioContext
*aio_context
;
1824 static void do_drive_backup(const char *device
, const char *target
,
1825 bool has_format
, const char *format
,
1826 enum MirrorSyncMode sync
,
1827 bool has_mode
, enum NewImageMode mode
,
1828 bool has_speed
, int64_t speed
,
1829 bool has_bitmap
, const char *bitmap
,
1830 bool has_on_source_error
,
1831 BlockdevOnError on_source_error
,
1832 bool has_on_target_error
,
1833 BlockdevOnError on_target_error
,
1834 BlockJobTxn
*txn
, Error
**errp
);
1836 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1838 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1840 DriveBackup
*backup
;
1841 Error
*local_err
= NULL
;
1843 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1844 backup
= common
->action
->u
.drive_backup
;
1846 blk
= blk_by_name(backup
->device
);
1848 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1849 "Device '%s' not found", backup
->device
);
1853 if (!blk_is_available(blk
)) {
1854 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1858 /* AioContext is released in .clean() */
1859 state
->aio_context
= blk_get_aio_context(blk
);
1860 aio_context_acquire(state
->aio_context
);
1861 bdrv_drained_begin(blk_bs(blk
));
1862 state
->bs
= blk_bs(blk
);
1864 do_drive_backup(backup
->device
, backup
->target
,
1865 backup
->has_format
, backup
->format
,
1867 backup
->has_mode
, backup
->mode
,
1868 backup
->has_speed
, backup
->speed
,
1869 backup
->has_bitmap
, backup
->bitmap
,
1870 backup
->has_on_source_error
, backup
->on_source_error
,
1871 backup
->has_on_target_error
, backup
->on_target_error
,
1872 common
->block_job_txn
, &local_err
);
1874 error_propagate(errp
, local_err
);
1878 state
->job
= state
->bs
->job
;
1881 static void drive_backup_abort(BlkActionState
*common
)
1883 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1884 BlockDriverState
*bs
= state
->bs
;
1886 /* Only cancel if it's the job we started */
1887 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1888 block_job_cancel_sync(bs
->job
);
1892 static void drive_backup_clean(BlkActionState
*common
)
1894 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1896 if (state
->aio_context
) {
1897 bdrv_drained_end(state
->bs
);
1898 aio_context_release(state
->aio_context
);
1902 typedef struct BlockdevBackupState
{
1903 BlkActionState common
;
1904 BlockDriverState
*bs
;
1906 AioContext
*aio_context
;
1907 } BlockdevBackupState
;
1909 static void do_blockdev_backup(const char *device
, const char *target
,
1910 enum MirrorSyncMode sync
,
1911 bool has_speed
, int64_t speed
,
1912 bool has_on_source_error
,
1913 BlockdevOnError on_source_error
,
1914 bool has_on_target_error
,
1915 BlockdevOnError on_target_error
,
1916 BlockJobTxn
*txn
, Error
**errp
);
1918 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1920 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1921 BlockdevBackup
*backup
;
1922 BlockBackend
*blk
, *target
;
1923 Error
*local_err
= NULL
;
1925 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1926 backup
= common
->action
->u
.blockdev_backup
;
1928 blk
= blk_by_name(backup
->device
);
1930 error_setg(errp
, "Device '%s' not found", backup
->device
);
1934 if (!blk_is_available(blk
)) {
1935 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1939 target
= blk_by_name(backup
->target
);
1941 error_setg(errp
, "Device '%s' not found", backup
->target
);
1945 /* AioContext is released in .clean() */
1946 state
->aio_context
= blk_get_aio_context(blk
);
1947 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1948 state
->aio_context
= NULL
;
1949 error_setg(errp
, "Backup between two IO threads is not implemented");
1952 aio_context_acquire(state
->aio_context
);
1953 state
->bs
= blk_bs(blk
);
1954 bdrv_drained_begin(state
->bs
);
1956 do_blockdev_backup(backup
->device
, backup
->target
,
1958 backup
->has_speed
, backup
->speed
,
1959 backup
->has_on_source_error
, backup
->on_source_error
,
1960 backup
->has_on_target_error
, backup
->on_target_error
,
1961 common
->block_job_txn
, &local_err
);
1963 error_propagate(errp
, local_err
);
1967 state
->job
= state
->bs
->job
;
1970 static void blockdev_backup_abort(BlkActionState
*common
)
1972 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1973 BlockDriverState
*bs
= state
->bs
;
1975 /* Only cancel if it's the job we started */
1976 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1977 block_job_cancel_sync(bs
->job
);
1981 static void blockdev_backup_clean(BlkActionState
*common
)
1983 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1985 if (state
->aio_context
) {
1986 bdrv_drained_end(state
->bs
);
1987 aio_context_release(state
->aio_context
);
1991 typedef struct BlockDirtyBitmapState
{
1992 BlkActionState common
;
1993 BdrvDirtyBitmap
*bitmap
;
1994 BlockDriverState
*bs
;
1995 AioContext
*aio_context
;
1998 } BlockDirtyBitmapState
;
2000 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2003 Error
*local_err
= NULL
;
2004 BlockDirtyBitmapAdd
*action
;
2005 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2008 if (action_check_completion_mode(common
, errp
) < 0) {
2012 action
= common
->action
->u
.block_dirty_bitmap_add
;
2013 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2014 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2015 action
->has_granularity
, action
->granularity
,
2019 state
->prepared
= true;
2021 error_propagate(errp
, local_err
);
2025 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2027 BlockDirtyBitmapAdd
*action
;
2028 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2031 action
= common
->action
->u
.block_dirty_bitmap_add
;
2032 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2033 * then the node reference and bitmap name must have been valid.
2035 if (state
->prepared
) {
2036 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2040 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2043 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2045 BlockDirtyBitmap
*action
;
2047 if (action_check_completion_mode(common
, errp
) < 0) {
2051 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2052 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2055 &state
->aio_context
,
2057 if (!state
->bitmap
) {
2061 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2062 error_setg(errp
, "Cannot modify a frozen bitmap");
2064 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2065 error_setg(errp
, "Cannot clear a disabled bitmap");
2069 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2070 /* AioContext is released in .clean() */
2073 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2075 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2078 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2081 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2083 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2086 hbitmap_free(state
->backup
);
2089 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2091 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2094 if (state
->aio_context
) {
2095 aio_context_release(state
->aio_context
);
2099 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2101 error_setg(errp
, "Transaction aborted using Abort action");
2104 static void abort_commit(BlkActionState
*common
)
2106 g_assert_not_reached(); /* this action never succeeds */
2109 static const BlkActionOps actions
[] = {
2110 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2111 .instance_size
= sizeof(ExternalSnapshotState
),
2112 .prepare
= external_snapshot_prepare
,
2113 .commit
= external_snapshot_commit
,
2114 .abort
= external_snapshot_abort
,
2115 .clean
= external_snapshot_clean
,
2117 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2118 .instance_size
= sizeof(ExternalSnapshotState
),
2119 .prepare
= external_snapshot_prepare
,
2120 .commit
= external_snapshot_commit
,
2121 .abort
= external_snapshot_abort
,
2122 .clean
= external_snapshot_clean
,
2124 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2125 .instance_size
= sizeof(DriveBackupState
),
2126 .prepare
= drive_backup_prepare
,
2127 .abort
= drive_backup_abort
,
2128 .clean
= drive_backup_clean
,
2130 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2131 .instance_size
= sizeof(BlockdevBackupState
),
2132 .prepare
= blockdev_backup_prepare
,
2133 .abort
= blockdev_backup_abort
,
2134 .clean
= blockdev_backup_clean
,
2136 [TRANSACTION_ACTION_KIND_ABORT
] = {
2137 .instance_size
= sizeof(BlkActionState
),
2138 .prepare
= abort_prepare
,
2139 .commit
= abort_commit
,
2141 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2142 .instance_size
= sizeof(InternalSnapshotState
),
2143 .prepare
= internal_snapshot_prepare
,
2144 .abort
= internal_snapshot_abort
,
2145 .clean
= internal_snapshot_clean
,
2147 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2148 .instance_size
= sizeof(BlockDirtyBitmapState
),
2149 .prepare
= block_dirty_bitmap_add_prepare
,
2150 .abort
= block_dirty_bitmap_add_abort
,
2152 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2153 .instance_size
= sizeof(BlockDirtyBitmapState
),
2154 .prepare
= block_dirty_bitmap_clear_prepare
,
2155 .commit
= block_dirty_bitmap_clear_commit
,
2156 .abort
= block_dirty_bitmap_clear_abort
,
2157 .clean
= block_dirty_bitmap_clear_clean
,
2162 * Allocate a TransactionProperties structure if necessary, and fill
2163 * that structure with desired defaults if they are unset.
2165 static TransactionProperties
*get_transaction_properties(
2166 TransactionProperties
*props
)
2169 props
= g_new0(TransactionProperties
, 1);
2172 if (!props
->has_completion_mode
) {
2173 props
->has_completion_mode
= true;
2174 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2181 * 'Atomic' group operations. The operations are performed as a set, and if
2182 * any fail then we roll back all operations in the group.
2184 void qmp_transaction(TransactionActionList
*dev_list
,
2186 struct TransactionProperties
*props
,
2189 TransactionActionList
*dev_entry
= dev_list
;
2190 BlockJobTxn
*block_job_txn
= NULL
;
2191 BlkActionState
*state
, *next
;
2192 Error
*local_err
= NULL
;
2194 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2195 QSIMPLEQ_INIT(&snap_bdrv_states
);
2197 /* Does this transaction get canceled as a group on failure?
2198 * If not, we don't really need to make a BlockJobTxn.
2200 props
= get_transaction_properties(props
);
2201 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2202 block_job_txn
= block_job_txn_new();
2205 /* drain all i/o before any operations */
2208 /* We don't do anything in this loop that commits us to the operations */
2209 while (NULL
!= dev_entry
) {
2210 TransactionAction
*dev_info
= NULL
;
2211 const BlkActionOps
*ops
;
2213 dev_info
= dev_entry
->value
;
2214 dev_entry
= dev_entry
->next
;
2216 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2218 ops
= &actions
[dev_info
->type
];
2219 assert(ops
->instance_size
> 0);
2221 state
= g_malloc0(ops
->instance_size
);
2223 state
->action
= dev_info
;
2224 state
->block_job_txn
= block_job_txn
;
2225 state
->txn_props
= props
;
2226 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2228 state
->ops
->prepare(state
, &local_err
);
2230 error_propagate(errp
, local_err
);
2231 goto delete_and_fail
;
2235 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2236 if (state
->ops
->commit
) {
2237 state
->ops
->commit(state
);
2245 /* failure, and it is all-or-none; roll back all operations */
2246 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2247 if (state
->ops
->abort
) {
2248 state
->ops
->abort(state
);
2252 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2253 if (state
->ops
->clean
) {
2254 state
->ops
->clean(state
);
2259 qapi_free_TransactionProperties(props
);
2261 block_job_txn_unref(block_job_txn
);
2264 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2266 Error
*local_err
= NULL
;
2268 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2270 error_propagate(errp
, local_err
);
2274 qmp_x_blockdev_remove_medium(device
, errp
);
2277 void qmp_block_passwd(bool has_device
, const char *device
,
2278 bool has_node_name
, const char *node_name
,
2279 const char *password
, Error
**errp
)
2281 Error
*local_err
= NULL
;
2282 BlockDriverState
*bs
;
2283 AioContext
*aio_context
;
2285 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2286 has_node_name
? node_name
: NULL
,
2289 error_propagate(errp
, local_err
);
2293 aio_context
= bdrv_get_aio_context(bs
);
2294 aio_context_acquire(aio_context
);
2296 bdrv_add_key(bs
, password
, errp
);
2298 aio_context_release(aio_context
);
2301 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2311 blk
= blk_by_name(device
);
2313 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2314 "Device '%s' not found", device
);
2318 if (!blk_dev_has_removable_media(blk
)) {
2319 error_setg(errp
, "Device '%s' is not removable", device
);
2323 if (!blk_dev_has_tray(blk
)) {
2324 /* Ignore this command on tray-less devices */
2328 if (blk_dev_is_tray_open(blk
)) {
2332 locked
= blk_dev_is_medium_locked(blk
);
2334 blk_dev_eject_request(blk
, force
);
2337 if (!locked
|| force
) {
2338 blk_dev_change_media_cb(blk
, false);
2342 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2346 blk
= blk_by_name(device
);
2348 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2349 "Device '%s' not found", device
);
2353 if (!blk_dev_has_removable_media(blk
)) {
2354 error_setg(errp
, "Device '%s' is not removable", device
);
2358 if (!blk_dev_has_tray(blk
)) {
2359 /* Ignore this command on tray-less devices */
2363 if (!blk_dev_is_tray_open(blk
)) {
2367 blk_dev_change_media_cb(blk
, true);
2370 void qmp_x_blockdev_remove_medium(const char *device
, Error
**errp
)
2373 BlockDriverState
*bs
;
2374 AioContext
*aio_context
;
2377 blk
= blk_by_name(device
);
2379 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2380 "Device '%s' not found", device
);
2384 /* For BBs without a device, we can exchange the BDS tree at will */
2385 has_device
= blk_get_attached_dev(blk
);
2387 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2388 error_setg(errp
, "Device '%s' is not removable", device
);
2392 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2393 error_setg(errp
, "Tray of device '%s' is not open", device
);
2402 aio_context
= bdrv_get_aio_context(bs
);
2403 aio_context_acquire(aio_context
);
2405 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2409 /* This follows the convention established by bdrv_make_anon() */
2410 if (bs
->device_list
.tqe_prev
) {
2411 bdrv_device_remove(bs
);
2416 if (!blk_dev_has_tray(blk
)) {
2417 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2418 * called at all); therefore, the medium needs to be ejected here.
2419 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2420 * value passed here (i.e. false). */
2421 blk_dev_change_media_cb(blk
, false);
2425 aio_context_release(aio_context
);
2428 static void qmp_blockdev_insert_anon_medium(const char *device
,
2429 BlockDriverState
*bs
, Error
**errp
)
2434 blk
= blk_by_name(device
);
2436 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2437 "Device '%s' not found", device
);
2441 /* For BBs without a device, we can exchange the BDS tree at will */
2442 has_device
= blk_get_attached_dev(blk
);
2444 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2445 error_setg(errp
, "Device '%s' is not removable", device
);
2449 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2450 error_setg(errp
, "Tray of device '%s' is not open", device
);
2455 error_setg(errp
, "There already is a medium in device '%s'", device
);
2459 blk_insert_bs(blk
, bs
);
2461 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2463 if (!blk_dev_has_tray(blk
)) {
2464 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2465 * called at all); therefore, the medium needs to be pushed into the
2467 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2468 * value passed here (i.e. true). */
2469 blk_dev_change_media_cb(blk
, true);
2473 void qmp_x_blockdev_insert_medium(const char *device
, const char *node_name
,
2476 BlockDriverState
*bs
;
2478 bs
= bdrv_find_node(node_name
);
2480 error_setg(errp
, "Node '%s' not found", node_name
);
2485 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2490 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2493 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2494 bool has_format
, const char *format
,
2496 BlockdevChangeReadOnlyMode read_only
,
2500 BlockDriverState
*medium_bs
= NULL
;
2501 int bdrv_flags
, ret
;
2502 QDict
*options
= NULL
;
2505 blk
= blk_by_name(device
);
2507 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2508 "Device '%s' not found", device
);
2513 blk_update_root_state(blk
);
2516 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2517 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2520 if (!has_read_only
) {
2521 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2524 switch (read_only
) {
2525 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2528 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2529 bdrv_flags
&= ~BDRV_O_RDWR
;
2532 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2533 bdrv_flags
|= BDRV_O_RDWR
;
2541 options
= qdict_new();
2542 qdict_put(options
, "driver", qstring_from_str(format
));
2546 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2551 blk_apply_root_state(blk
, medium_bs
);
2553 bdrv_add_key(medium_bs
, NULL
, &err
);
2555 error_propagate(errp
, err
);
2559 qmp_blockdev_open_tray(device
, false, false, &err
);
2561 error_propagate(errp
, err
);
2565 qmp_x_blockdev_remove_medium(device
, &err
);
2567 error_propagate(errp
, err
);
2571 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2573 error_propagate(errp
, err
);
2577 qmp_blockdev_close_tray(device
, errp
);
2580 /* If the medium has been inserted, the device has its own reference, so
2581 * ours must be relinquished; and if it has not been inserted successfully,
2582 * the reference must be relinquished anyway */
2583 bdrv_unref(medium_bs
);
2586 /* throttling disk I/O limits */
2587 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2594 bool has_bps_rd_max
,
2596 bool has_bps_wr_max
,
2600 bool has_iops_rd_max
,
2601 int64_t iops_rd_max
,
2602 bool has_iops_wr_max
,
2603 int64_t iops_wr_max
,
2604 bool has_bps_max_length
,
2605 int64_t bps_max_length
,
2606 bool has_bps_rd_max_length
,
2607 int64_t bps_rd_max_length
,
2608 bool has_bps_wr_max_length
,
2609 int64_t bps_wr_max_length
,
2610 bool has_iops_max_length
,
2611 int64_t iops_max_length
,
2612 bool has_iops_rd_max_length
,
2613 int64_t iops_rd_max_length
,
2614 bool has_iops_wr_max_length
,
2615 int64_t iops_wr_max_length
,
2619 const char *group
, Error
**errp
)
2622 BlockDriverState
*bs
;
2624 AioContext
*aio_context
;
2626 blk
= blk_by_name(device
);
2628 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2629 "Device '%s' not found", device
);
2633 aio_context
= blk_get_aio_context(blk
);
2634 aio_context_acquire(aio_context
);
2638 error_setg(errp
, "Device '%s' has no medium", device
);
2642 throttle_config_init(&cfg
);
2643 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2644 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2645 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2647 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2648 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2649 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2652 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2654 if (has_bps_rd_max
) {
2655 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2657 if (has_bps_wr_max
) {
2658 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2661 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2663 if (has_iops_rd_max
) {
2664 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2666 if (has_iops_wr_max
) {
2667 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2670 if (has_bps_max_length
) {
2671 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= bps_max_length
;
2673 if (has_bps_rd_max_length
) {
2674 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= bps_rd_max_length
;
2676 if (has_bps_wr_max_length
) {
2677 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= bps_wr_max_length
;
2679 if (has_iops_max_length
) {
2680 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= iops_max_length
;
2682 if (has_iops_rd_max_length
) {
2683 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= iops_rd_max_length
;
2685 if (has_iops_wr_max_length
) {
2686 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= iops_wr_max_length
;
2689 if (has_iops_size
) {
2690 cfg
.op_size
= iops_size
;
2693 if (!throttle_is_valid(&cfg
, errp
)) {
2697 if (throttle_enabled(&cfg
)) {
2698 /* Enable I/O limits if they're not enabled yet, otherwise
2699 * just update the throttling group. */
2700 if (!bs
->throttle_state
) {
2701 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2702 } else if (has_group
) {
2703 bdrv_io_limits_update_group(bs
, group
);
2705 /* Set the new throttling configuration */
2706 bdrv_set_io_limits(bs
, &cfg
);
2707 } else if (bs
->throttle_state
) {
2708 /* If all throttling settings are set to 0, disable I/O limits */
2709 bdrv_io_limits_disable(bs
);
2713 aio_context_release(aio_context
);
2716 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2717 bool has_granularity
, uint32_t granularity
,
2720 AioContext
*aio_context
;
2721 BlockDriverState
*bs
;
2723 if (!name
|| name
[0] == '\0') {
2724 error_setg(errp
, "Bitmap name cannot be empty");
2728 bs
= bdrv_lookup_bs(node
, node
, errp
);
2733 aio_context
= bdrv_get_aio_context(bs
);
2734 aio_context_acquire(aio_context
);
2736 if (has_granularity
) {
2737 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2738 error_setg(errp
, "Granularity must be power of 2 "
2739 "and at least 512");
2743 /* Default to cluster size, if available: */
2744 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2747 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2750 aio_context_release(aio_context
);
2753 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2756 AioContext
*aio_context
;
2757 BlockDriverState
*bs
;
2758 BdrvDirtyBitmap
*bitmap
;
2760 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2761 if (!bitmap
|| !bs
) {
2765 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2767 "Bitmap '%s' is currently frozen and cannot be removed",
2771 bdrv_dirty_bitmap_make_anon(bitmap
);
2772 bdrv_release_dirty_bitmap(bs
, bitmap
);
2775 aio_context_release(aio_context
);
2779 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2780 * immediately after a full backup operation.
2782 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2785 AioContext
*aio_context
;
2786 BdrvDirtyBitmap
*bitmap
;
2787 BlockDriverState
*bs
;
2789 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2790 if (!bitmap
|| !bs
) {
2794 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2796 "Bitmap '%s' is currently frozen and cannot be modified",
2799 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2801 "Bitmap '%s' is currently disabled and cannot be cleared",
2806 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2809 aio_context_release(aio_context
);
2812 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2814 const char *id
= qdict_get_str(qdict
, "id");
2816 BlockDriverState
*bs
;
2817 AioContext
*aio_context
;
2818 Error
*local_err
= NULL
;
2820 blk
= blk_by_name(id
);
2822 error_report("Device '%s' not found", id
);
2826 if (!blk_legacy_dinfo(blk
)) {
2827 error_report("Deleting device added with blockdev-add"
2828 " is not supported");
2832 aio_context
= blk_get_aio_context(blk
);
2833 aio_context_acquire(aio_context
);
2837 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2838 error_report_err(local_err
);
2839 aio_context_release(aio_context
);
2846 /* if we have a device attached to this BlockDriverState
2847 * then we need to make the drive anonymous until the device
2848 * can be removed. If this is a drive with no device backing
2849 * then we can just get rid of the block driver state right here.
2851 if (blk_get_attached_dev(blk
)) {
2852 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2853 /* Further I/O must not pause the guest */
2854 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2855 BLOCKDEV_ON_ERROR_REPORT
);
2860 aio_context_release(aio_context
);
2863 void qmp_block_resize(bool has_device
, const char *device
,
2864 bool has_node_name
, const char *node_name
,
2865 int64_t size
, Error
**errp
)
2867 Error
*local_err
= NULL
;
2868 BlockDriverState
*bs
;
2869 AioContext
*aio_context
;
2872 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2873 has_node_name
? node_name
: NULL
,
2876 error_propagate(errp
, local_err
);
2880 aio_context
= bdrv_get_aio_context(bs
);
2881 aio_context_acquire(aio_context
);
2883 if (!bdrv_is_first_non_filter(bs
)) {
2884 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2889 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2893 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2894 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2898 /* complete all in-flight operations before resizing the device */
2901 ret
= bdrv_truncate(bs
, size
);
2906 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2909 error_setg(errp
, QERR_UNSUPPORTED
);
2912 error_setg(errp
, "Device '%s' is read only", device
);
2915 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2918 error_setg_errno(errp
, -ret
, "Could not resize");
2923 aio_context_release(aio_context
);
2926 static void block_job_cb(void *opaque
, int ret
)
2928 /* Note that this function may be executed from another AioContext besides
2929 * the QEMU main loop. If you need to access anything that assumes the
2930 * QEMU global mutex, use a BH or introduce a mutex.
2933 BlockDriverState
*bs
= opaque
;
2934 const char *msg
= NULL
;
2936 trace_block_job_cb(bs
, bs
->job
, ret
);
2941 msg
= strerror(-ret
);
2944 if (block_job_is_cancelled(bs
->job
)) {
2945 block_job_event_cancelled(bs
->job
);
2947 block_job_event_completed(bs
->job
, msg
);
2951 void qmp_block_stream(const char *device
,
2952 bool has_base
, const char *base
,
2953 bool has_backing_file
, const char *backing_file
,
2954 bool has_speed
, int64_t speed
,
2955 bool has_on_error
, BlockdevOnError on_error
,
2959 BlockDriverState
*bs
;
2960 BlockDriverState
*base_bs
= NULL
;
2961 AioContext
*aio_context
;
2962 Error
*local_err
= NULL
;
2963 const char *base_name
= NULL
;
2965 if (!has_on_error
) {
2966 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2969 blk
= blk_by_name(device
);
2971 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2972 "Device '%s' not found", device
);
2976 aio_context
= blk_get_aio_context(blk
);
2977 aio_context_acquire(aio_context
);
2979 if (!blk_is_available(blk
)) {
2980 error_setg(errp
, "Device '%s' has no medium", device
);
2985 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2990 base_bs
= bdrv_find_backing_image(bs
, base
);
2991 if (base_bs
== NULL
) {
2992 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2995 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2999 /* if we are streaming the entire chain, the result will have no backing
3000 * file, and specifying one is therefore an error */
3001 if (base_bs
== NULL
&& has_backing_file
) {
3002 error_setg(errp
, "backing file specified, but streaming the "
3007 /* backing_file string overrides base bs filename */
3008 base_name
= has_backing_file
? backing_file
: base_name
;
3010 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
3011 on_error
, block_job_cb
, bs
, &local_err
);
3013 error_propagate(errp
, local_err
);
3017 trace_qmp_block_stream(bs
, bs
->job
);
3020 aio_context_release(aio_context
);
3023 void qmp_block_commit(const char *device
,
3024 bool has_base
, const char *base
,
3025 bool has_top
, const char *top
,
3026 bool has_backing_file
, const char *backing_file
,
3027 bool has_speed
, int64_t speed
,
3031 BlockDriverState
*bs
;
3032 BlockDriverState
*base_bs
, *top_bs
;
3033 AioContext
*aio_context
;
3034 Error
*local_err
= NULL
;
3035 /* This will be part of the QMP command, if/when the
3036 * BlockdevOnError change for blkmirror makes it in
3038 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3045 * libvirt relies on the DeviceNotFound error class in order to probe for
3046 * live commit feature versions; for this to work, we must make sure to
3047 * perform the device lookup before any generic errors that may occur in a
3048 * scenario in which all optional arguments are omitted. */
3049 blk
= blk_by_name(device
);
3051 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3052 "Device '%s' not found", device
);
3056 aio_context
= blk_get_aio_context(blk
);
3057 aio_context_acquire(aio_context
);
3059 if (!blk_is_available(blk
)) {
3060 error_setg(errp
, "Device '%s' has no medium", device
);
3065 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3069 /* default top_bs is the active layer */
3072 if (has_top
&& top
) {
3073 if (strcmp(bs
->filename
, top
) != 0) {
3074 top_bs
= bdrv_find_backing_image(bs
, top
);
3078 if (top_bs
== NULL
) {
3079 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3083 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3085 if (has_base
&& base
) {
3086 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3088 base_bs
= bdrv_find_base(top_bs
);
3091 if (base_bs
== NULL
) {
3092 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3096 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3098 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3102 /* Do not allow attempts to commit an image into itself */
3103 if (top_bs
== base_bs
) {
3104 error_setg(errp
, "cannot commit an image into itself");
3109 if (has_backing_file
) {
3110 error_setg(errp
, "'backing-file' specified,"
3111 " but 'top' is the active layer");
3114 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3117 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3118 has_backing_file
? backing_file
: NULL
, &local_err
);
3120 if (local_err
!= NULL
) {
3121 error_propagate(errp
, local_err
);
3126 aio_context_release(aio_context
);
3129 static void do_drive_backup(const char *device
, const char *target
,
3130 bool has_format
, const char *format
,
3131 enum MirrorSyncMode sync
,
3132 bool has_mode
, enum NewImageMode mode
,
3133 bool has_speed
, int64_t speed
,
3134 bool has_bitmap
, const char *bitmap
,
3135 bool has_on_source_error
,
3136 BlockdevOnError on_source_error
,
3137 bool has_on_target_error
,
3138 BlockdevOnError on_target_error
,
3139 BlockJobTxn
*txn
, Error
**errp
)
3142 BlockDriverState
*bs
;
3143 BlockDriverState
*target_bs
;
3144 BlockDriverState
*source
= NULL
;
3145 BdrvDirtyBitmap
*bmap
= NULL
;
3146 AioContext
*aio_context
;
3147 QDict
*options
= NULL
;
3148 Error
*local_err
= NULL
;
3156 if (!has_on_source_error
) {
3157 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3159 if (!has_on_target_error
) {
3160 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3163 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3166 blk
= blk_by_name(device
);
3168 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3169 "Device '%s' not found", device
);
3173 aio_context
= blk_get_aio_context(blk
);
3174 aio_context_acquire(aio_context
);
3176 /* Although backup_run has this check too, we need to use bs->drv below, so
3177 * do an early check redundantly. */
3178 if (!blk_is_available(blk
)) {
3179 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3185 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3188 /* Early check to avoid creating target */
3189 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3193 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3195 /* See if we have a backing HD we can use to create our new image
3197 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3198 source
= backing_bs(bs
);
3200 sync
= MIRROR_SYNC_MODE_FULL
;
3203 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3207 size
= bdrv_getlength(bs
);
3209 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3213 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3216 bdrv_img_create(target
, format
, source
->filename
,
3217 source
->drv
->format_name
, NULL
,
3218 size
, flags
, &local_err
, false);
3220 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3221 size
, flags
, &local_err
, false);
3226 error_propagate(errp
, local_err
);
3231 options
= qdict_new();
3232 qdict_put(options
, "driver", qstring_from_str(format
));
3236 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3238 error_propagate(errp
, local_err
);
3242 bdrv_set_aio_context(target_bs
, aio_context
);
3245 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3247 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3248 bdrv_unref(target_bs
);
3253 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3254 on_source_error
, on_target_error
,
3255 block_job_cb
, bs
, txn
, &local_err
);
3256 if (local_err
!= NULL
) {
3257 bdrv_unref(target_bs
);
3258 error_propagate(errp
, local_err
);
3263 aio_context_release(aio_context
);
3266 void qmp_drive_backup(const char *device
, const char *target
,
3267 bool has_format
, const char *format
,
3268 enum MirrorSyncMode sync
,
3269 bool has_mode
, enum NewImageMode mode
,
3270 bool has_speed
, int64_t speed
,
3271 bool has_bitmap
, const char *bitmap
,
3272 bool has_on_source_error
, BlockdevOnError on_source_error
,
3273 bool has_on_target_error
, BlockdevOnError on_target_error
,
3276 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3277 has_mode
, mode
, has_speed
, speed
,
3279 has_on_source_error
, on_source_error
,
3280 has_on_target_error
, on_target_error
,
3284 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3286 return bdrv_named_nodes_list(errp
);
3289 void do_blockdev_backup(const char *device
, const char *target
,
3290 enum MirrorSyncMode sync
,
3291 bool has_speed
, int64_t speed
,
3292 bool has_on_source_error
,
3293 BlockdevOnError on_source_error
,
3294 bool has_on_target_error
,
3295 BlockdevOnError on_target_error
,
3296 BlockJobTxn
*txn
, Error
**errp
)
3298 BlockBackend
*blk
, *target_blk
;
3299 BlockDriverState
*bs
;
3300 BlockDriverState
*target_bs
;
3301 Error
*local_err
= NULL
;
3302 AioContext
*aio_context
;
3307 if (!has_on_source_error
) {
3308 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3310 if (!has_on_target_error
) {
3311 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3314 blk
= blk_by_name(device
);
3316 error_setg(errp
, "Device '%s' not found", device
);
3320 aio_context
= blk_get_aio_context(blk
);
3321 aio_context_acquire(aio_context
);
3323 if (!blk_is_available(blk
)) {
3324 error_setg(errp
, "Device '%s' has no medium", device
);
3329 target_blk
= blk_by_name(target
);
3331 error_setg(errp
, "Device '%s' not found", target
);
3335 if (!blk_is_available(target_blk
)) {
3336 error_setg(errp
, "Device '%s' has no medium", target
);
3339 target_bs
= blk_bs(target_blk
);
3341 bdrv_ref(target_bs
);
3342 bdrv_set_aio_context(target_bs
, aio_context
);
3343 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3344 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3345 if (local_err
!= NULL
) {
3346 bdrv_unref(target_bs
);
3347 error_propagate(errp
, local_err
);
3350 aio_context_release(aio_context
);
3353 void qmp_blockdev_backup(const char *device
, const char *target
,
3354 enum MirrorSyncMode sync
,
3355 bool has_speed
, int64_t speed
,
3356 bool has_on_source_error
,
3357 BlockdevOnError on_source_error
,
3358 bool has_on_target_error
,
3359 BlockdevOnError on_target_error
,
3362 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3363 has_on_source_error
, on_source_error
,
3364 has_on_target_error
, on_target_error
,
3368 /* Parameter check and block job starting for drive mirroring.
3369 * Caller should hold @device and @target's aio context (must be the same).
3371 static void blockdev_mirror_common(BlockDriverState
*bs
,
3372 BlockDriverState
*target
,
3373 bool has_replaces
, const char *replaces
,
3374 enum MirrorSyncMode sync
,
3375 bool has_speed
, int64_t speed
,
3376 bool has_granularity
, uint32_t granularity
,
3377 bool has_buf_size
, int64_t buf_size
,
3378 bool has_on_source_error
,
3379 BlockdevOnError on_source_error
,
3380 bool has_on_target_error
,
3381 BlockdevOnError on_target_error
,
3382 bool has_unmap
, bool unmap
,
3389 if (!has_on_source_error
) {
3390 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3392 if (!has_on_target_error
) {
3393 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3395 if (!has_granularity
) {
3398 if (!has_buf_size
) {
3405 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3406 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3407 "a value in range [512B, 64MB]");
3410 if (granularity
& (granularity
- 1)) {
3411 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3416 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3419 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3423 error_setg(errp
, "Cannot mirror to an attached block device");
3427 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3428 sync
= MIRROR_SYNC_MODE_FULL
;
3431 /* pass the node name to replace to mirror start since it's loose coupling
3432 * and will allow to check whether the node still exist at mirror completion
3434 mirror_start(bs
, target
,
3435 has_replaces
? replaces
: NULL
,
3436 speed
, granularity
, buf_size
, sync
,
3437 on_source_error
, on_target_error
, unmap
,
3438 block_job_cb
, bs
, errp
);
3441 void qmp_drive_mirror(const char *device
, const char *target
,
3442 bool has_format
, const char *format
,
3443 bool has_node_name
, const char *node_name
,
3444 bool has_replaces
, const char *replaces
,
3445 enum MirrorSyncMode sync
,
3446 bool has_mode
, enum NewImageMode mode
,
3447 bool has_speed
, int64_t speed
,
3448 bool has_granularity
, uint32_t granularity
,
3449 bool has_buf_size
, int64_t buf_size
,
3450 bool has_on_source_error
, BlockdevOnError on_source_error
,
3451 bool has_on_target_error
, BlockdevOnError on_target_error
,
3452 bool has_unmap
, bool unmap
,
3455 BlockDriverState
*bs
;
3457 BlockDriverState
*source
, *target_bs
;
3458 AioContext
*aio_context
;
3459 Error
*local_err
= NULL
;
3460 QDict
*options
= NULL
;
3465 blk
= blk_by_name(device
);
3467 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3468 "Device '%s' not found", device
);
3472 aio_context
= blk_get_aio_context(blk
);
3473 aio_context_acquire(aio_context
);
3475 if (!blk_is_available(blk
)) {
3476 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3481 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3485 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3488 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3489 source
= backing_bs(bs
);
3490 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3491 sync
= MIRROR_SYNC_MODE_FULL
;
3493 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3497 size
= bdrv_getlength(bs
);
3499 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3504 BlockDriverState
*to_replace_bs
;
3505 AioContext
*replace_aio_context
;
3506 int64_t replace_size
;
3508 if (!has_node_name
) {
3509 error_setg(errp
, "a node-name must be provided when replacing a"
3510 " named node of the graph");
3514 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3516 if (!to_replace_bs
) {
3517 error_propagate(errp
, local_err
);
3521 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3522 aio_context_acquire(replace_aio_context
);
3523 replace_size
= bdrv_getlength(to_replace_bs
);
3524 aio_context_release(replace_aio_context
);
3526 if (size
!= replace_size
) {
3527 error_setg(errp
, "cannot replace image with a mirror image of "
3533 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3534 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3536 /* create new image w/o backing file */
3538 bdrv_img_create(target
, format
,
3539 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3542 case NEW_IMAGE_MODE_EXISTING
:
3544 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3545 /* create new image with backing file */
3546 bdrv_img_create(target
, format
,
3548 source
->drv
->format_name
,
3549 NULL
, size
, flags
, &local_err
, false);
3557 error_propagate(errp
, local_err
);
3561 options
= qdict_new();
3562 if (has_node_name
) {
3563 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3566 qdict_put(options
, "driver", qstring_from_str(format
));
3569 /* Mirroring takes care of copy-on-write using the source's backing
3573 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3574 flags
| BDRV_O_NO_BACKING
, &local_err
);
3576 error_propagate(errp
, local_err
);
3580 bdrv_set_aio_context(target_bs
, aio_context
);
3582 blockdev_mirror_common(bs
, target_bs
,
3583 has_replaces
, replaces
, sync
,
3585 has_granularity
, granularity
,
3586 has_buf_size
, buf_size
,
3587 has_on_source_error
, on_source_error
,
3588 has_on_target_error
, on_target_error
,
3592 error_propagate(errp
, local_err
);
3593 bdrv_unref(target_bs
);
3596 aio_context_release(aio_context
);
3599 void qmp_blockdev_mirror(const char *device
, const char *target
,
3600 bool has_replaces
, const char *replaces
,
3601 MirrorSyncMode sync
,
3602 bool has_speed
, int64_t speed
,
3603 bool has_granularity
, uint32_t granularity
,
3604 bool has_buf_size
, int64_t buf_size
,
3605 bool has_on_source_error
,
3606 BlockdevOnError on_source_error
,
3607 bool has_on_target_error
,
3608 BlockdevOnError on_target_error
,
3611 BlockDriverState
*bs
;
3613 BlockDriverState
*target_bs
;
3614 AioContext
*aio_context
;
3615 Error
*local_err
= NULL
;
3617 blk
= blk_by_name(device
);
3619 error_setg(errp
, "Device '%s' not found", device
);
3625 error_setg(errp
, "Device '%s' has no media", device
);
3629 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3634 aio_context
= bdrv_get_aio_context(bs
);
3635 aio_context_acquire(aio_context
);
3637 bdrv_ref(target_bs
);
3638 bdrv_set_aio_context(target_bs
, aio_context
);
3640 blockdev_mirror_common(bs
, target_bs
,
3641 has_replaces
, replaces
, sync
,
3643 has_granularity
, granularity
,
3644 has_buf_size
, buf_size
,
3645 has_on_source_error
, on_source_error
,
3646 has_on_target_error
, on_target_error
,
3650 error_propagate(errp
, local_err
);
3651 bdrv_unref(target_bs
);
3654 aio_context_release(aio_context
);
3657 /* Get the block job for a given device name and acquire its AioContext */
3658 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3662 BlockDriverState
*bs
;
3664 *aio_context
= NULL
;
3666 blk
= blk_by_name(device
);
3671 *aio_context
= blk_get_aio_context(blk
);
3672 aio_context_acquire(*aio_context
);
3674 if (!blk_is_available(blk
)) {
3686 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3687 "No active block job on device '%s'", device
);
3689 aio_context_release(*aio_context
);
3690 *aio_context
= NULL
;
3695 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3697 AioContext
*aio_context
;
3698 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3704 block_job_set_speed(job
, speed
, errp
);
3705 aio_context_release(aio_context
);
3708 void qmp_block_job_cancel(const char *device
,
3709 bool has_force
, bool force
, Error
**errp
)
3711 AioContext
*aio_context
;
3712 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3722 if (job
->user_paused
&& !force
) {
3723 error_setg(errp
, "The block job for device '%s' is currently paused",
3728 trace_qmp_block_job_cancel(job
);
3729 block_job_cancel(job
);
3731 aio_context_release(aio_context
);
3734 void qmp_block_job_pause(const char *device
, Error
**errp
)
3736 AioContext
*aio_context
;
3737 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3739 if (!job
|| job
->user_paused
) {
3743 job
->user_paused
= true;
3744 trace_qmp_block_job_pause(job
);
3745 block_job_pause(job
);
3746 aio_context_release(aio_context
);
3749 void qmp_block_job_resume(const char *device
, Error
**errp
)
3751 AioContext
*aio_context
;
3752 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3754 if (!job
|| !job
->user_paused
) {
3758 job
->user_paused
= false;
3759 trace_qmp_block_job_resume(job
);
3760 block_job_resume(job
);
3761 aio_context_release(aio_context
);
3764 void qmp_block_job_complete(const char *device
, Error
**errp
)
3766 AioContext
*aio_context
;
3767 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3773 trace_qmp_block_job_complete(job
);
3774 block_job_complete(job
, errp
);
3775 aio_context_release(aio_context
);
3778 void qmp_change_backing_file(const char *device
,
3779 const char *image_node_name
,
3780 const char *backing_file
,
3784 BlockDriverState
*bs
= NULL
;
3785 AioContext
*aio_context
;
3786 BlockDriverState
*image_bs
= NULL
;
3787 Error
*local_err
= NULL
;
3792 blk
= blk_by_name(device
);
3794 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3795 "Device '%s' not found", device
);
3799 aio_context
= blk_get_aio_context(blk
);
3800 aio_context_acquire(aio_context
);
3802 if (!blk_is_available(blk
)) {
3803 error_setg(errp
, "Device '%s' has no medium", device
);
3808 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3810 error_propagate(errp
, local_err
);
3815 error_setg(errp
, "image file not found");
3819 if (bdrv_find_base(image_bs
) == image_bs
) {
3820 error_setg(errp
, "not allowing backing file change on an image "
3821 "without a backing file");
3825 /* even though we are not necessarily operating on bs, we need it to
3826 * determine if block ops are currently prohibited on the chain */
3827 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3831 /* final sanity check */
3832 if (!bdrv_chain_contains(bs
, image_bs
)) {
3833 error_setg(errp
, "'%s' and image file are not in the same chain",
3838 /* if not r/w, reopen to make r/w */
3839 open_flags
= image_bs
->open_flags
;
3840 ro
= bdrv_is_read_only(image_bs
);
3843 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3845 error_propagate(errp
, local_err
);
3850 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3851 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3854 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3856 /* don't exit here, so we can try to restore open flags if
3861 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3863 error_propagate(errp
, local_err
); /* will preserve prior errp */
3868 aio_context_release(aio_context
);
3871 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3873 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3874 BlockDriverState
*bs
;
3875 BlockBackend
*blk
= NULL
;
3878 Error
*local_err
= NULL
;
3880 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3881 * cache.direct=false instead of silently switching to aio=threads, except
3882 * when called from drive_new().
3884 * For now, simply forbidding the combination for all drivers will do. */
3885 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3886 bool direct
= options
->has_cache
&&
3887 options
->cache
->has_direct
&&
3888 options
->cache
->direct
;
3890 error_setg(errp
, "aio=native requires cache.direct=true");
3895 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
), NULL
, &options
,
3898 error_propagate(errp
, local_err
);
3902 obj
= qmp_output_get_qobject(ov
);
3903 qdict
= qobject_to_qdict(obj
);
3905 qdict_flatten(qdict
);
3907 if (options
->has_id
) {
3908 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3910 error_propagate(errp
, local_err
);
3916 if (!qdict_get_try_str(qdict
, "node-name")) {
3917 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3922 bs
= bds_tree_init(qdict
, errp
);
3927 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3930 if (bs
&& bdrv_key_required(bs
)) {
3934 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3937 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3942 qmp_output_visitor_cleanup(ov
);
3945 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3946 bool has_node_name
, const char *node_name
, Error
**errp
)
3948 AioContext
*aio_context
;
3950 BlockDriverState
*bs
;
3952 if (has_id
&& has_node_name
) {
3953 error_setg(errp
, "Only one of id and node-name must be specified");
3955 } else if (!has_id
&& !has_node_name
) {
3956 error_setg(errp
, "No block device specified");
3961 blk
= blk_by_name(id
);
3963 error_setg(errp
, "Cannot find block backend %s", id
);
3966 if (blk_get_refcnt(blk
) > 1) {
3967 error_setg(errp
, "Block backend %s is in use", id
);
3971 aio_context
= blk_get_aio_context(blk
);
3973 bs
= bdrv_find_node(node_name
);
3975 error_setg(errp
, "Cannot find node %s", node_name
);
3980 error_setg(errp
, "Node %s is in use by %s",
3981 node_name
, blk_name(blk
));
3984 aio_context
= bdrv_get_aio_context(bs
);
3987 aio_context_acquire(aio_context
);
3990 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3994 if (!blk
&& !bs
->monitor_list
.tqe_prev
) {
3995 error_setg(errp
, "Node %s is not owned by the monitor",
4000 if (bs
->refcnt
> 1) {
4001 error_setg(errp
, "Block device %s is in use",
4002 bdrv_get_device_or_node_name(bs
));
4010 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4015 aio_context_release(aio_context
);
4018 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4020 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4021 BlockDriverState
*bs
;
4023 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4024 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
4026 aio_context_acquire(aio_context
);
4029 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
4030 elem
->value
= block_job_query(bs
->job
);
4032 p_next
= &elem
->next
;
4035 aio_context_release(aio_context
);
4041 QemuOptsList qemu_common_drive_opts
= {
4043 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4047 .type
= QEMU_OPT_BOOL
,
4048 .help
= "enable/disable snapshot mode",
4051 .type
= QEMU_OPT_STRING
,
4052 .help
= "discard operation (ignore/off, unmap/on)",
4055 .type
= QEMU_OPT_STRING
,
4056 .help
= "host AIO implementation (threads, native)",
4059 .type
= QEMU_OPT_STRING
,
4060 .help
= "disk format (raw, qcow2, ...)",
4063 .type
= QEMU_OPT_STRING
,
4064 .help
= "read error action",
4067 .type
= QEMU_OPT_STRING
,
4068 .help
= "write error action",
4070 .name
= "read-only",
4071 .type
= QEMU_OPT_BOOL
,
4072 .help
= "open drive file as read-only",
4074 .name
= "throttling.iops-total",
4075 .type
= QEMU_OPT_NUMBER
,
4076 .help
= "limit total I/O operations per second",
4078 .name
= "throttling.iops-read",
4079 .type
= QEMU_OPT_NUMBER
,
4080 .help
= "limit read operations per second",
4082 .name
= "throttling.iops-write",
4083 .type
= QEMU_OPT_NUMBER
,
4084 .help
= "limit write operations per second",
4086 .name
= "throttling.bps-total",
4087 .type
= QEMU_OPT_NUMBER
,
4088 .help
= "limit total bytes per second",
4090 .name
= "throttling.bps-read",
4091 .type
= QEMU_OPT_NUMBER
,
4092 .help
= "limit read bytes per second",
4094 .name
= "throttling.bps-write",
4095 .type
= QEMU_OPT_NUMBER
,
4096 .help
= "limit write bytes per second",
4098 .name
= "throttling.iops-total-max",
4099 .type
= QEMU_OPT_NUMBER
,
4100 .help
= "I/O operations burst",
4102 .name
= "throttling.iops-read-max",
4103 .type
= QEMU_OPT_NUMBER
,
4104 .help
= "I/O operations read burst",
4106 .name
= "throttling.iops-write-max",
4107 .type
= QEMU_OPT_NUMBER
,
4108 .help
= "I/O operations write burst",
4110 .name
= "throttling.bps-total-max",
4111 .type
= QEMU_OPT_NUMBER
,
4112 .help
= "total bytes burst",
4114 .name
= "throttling.bps-read-max",
4115 .type
= QEMU_OPT_NUMBER
,
4116 .help
= "total bytes read burst",
4118 .name
= "throttling.bps-write-max",
4119 .type
= QEMU_OPT_NUMBER
,
4120 .help
= "total bytes write burst",
4122 .name
= "throttling.iops-total-max-length",
4123 .type
= QEMU_OPT_NUMBER
,
4124 .help
= "length of the iops-total-max burst period, in seconds",
4126 .name
= "throttling.iops-read-max-length",
4127 .type
= QEMU_OPT_NUMBER
,
4128 .help
= "length of the iops-read-max burst period, in seconds",
4130 .name
= "throttling.iops-write-max-length",
4131 .type
= QEMU_OPT_NUMBER
,
4132 .help
= "length of the iops-write-max burst period, in seconds",
4134 .name
= "throttling.bps-total-max-length",
4135 .type
= QEMU_OPT_NUMBER
,
4136 .help
= "length of the bps-total-max burst period, in seconds",
4138 .name
= "throttling.bps-read-max-length",
4139 .type
= QEMU_OPT_NUMBER
,
4140 .help
= "length of the bps-read-max burst period, in seconds",
4142 .name
= "throttling.bps-write-max-length",
4143 .type
= QEMU_OPT_NUMBER
,
4144 .help
= "length of the bps-write-max burst period, in seconds",
4146 .name
= "throttling.iops-size",
4147 .type
= QEMU_OPT_NUMBER
,
4148 .help
= "when limiting by iops max size of an I/O in bytes",
4150 .name
= "throttling.group",
4151 .type
= QEMU_OPT_STRING
,
4152 .help
= "name of the block throttling group",
4154 .name
= "copy-on-read",
4155 .type
= QEMU_OPT_BOOL
,
4156 .help
= "copy read data from backing file into image file",
4158 .name
= "detect-zeroes",
4159 .type
= QEMU_OPT_STRING
,
4160 .help
= "try to optimize zero writes (off, on, unmap)",
4162 .name
= "stats-account-invalid",
4163 .type
= QEMU_OPT_BOOL
,
4164 .help
= "whether to account for invalid I/O operations "
4165 "in the statistics",
4167 .name
= "stats-account-failed",
4168 .type
= QEMU_OPT_BOOL
,
4169 .help
= "whether to account for failed I/O operations "
4170 "in the statistics",
4172 { /* end of list */ }
4176 static QemuOptsList qemu_root_bds_opts
= {
4178 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4182 .type
= QEMU_OPT_STRING
,
4183 .help
= "discard operation (ignore/off, unmap/on)",
4186 .type
= QEMU_OPT_STRING
,
4187 .help
= "host AIO implementation (threads, native)",
4189 .name
= "read-only",
4190 .type
= QEMU_OPT_BOOL
,
4191 .help
= "open drive file as read-only",
4193 .name
= "copy-on-read",
4194 .type
= QEMU_OPT_BOOL
,
4195 .help
= "copy read data from backing file into image file",
4197 .name
= "detect-zeroes",
4198 .type
= QEMU_OPT_STRING
,
4199 .help
= "try to optimize zero writes (off, on, unmap)",
4201 { /* end of list */ }
4205 QemuOptsList qemu_drive_opts
= {
4207 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4210 * no elements => accept any params
4211 * validation will happen later
4213 { /* end of list */ }