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/qobject-output-visitor.h"
47 #include "sysemu/sysemu.h"
48 #include "sysemu/iothread.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "block/trace.h"
52 #include "sysemu/arch_init.h"
53 #include "sysemu/qtest.h"
54 #include "qemu/cutils.h"
55 #include "qemu/help_option.h"
56 #include "qemu/throttle-options.h"
58 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
59 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
61 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
62 bool force
, Error
**errp
);
63 static void blockdev_remove_medium(bool has_device
, const char *device
,
64 bool has_id
, const char *id
, Error
**errp
);
65 static void blockdev_insert_medium(bool has_device
, const char *device
,
66 bool has_id
, const char *id
,
67 const char *node_name
, Error
**errp
);
69 static const char *const if_name
[IF_COUNT
] = {
73 [IF_FLOPPY
] = "floppy",
74 [IF_PFLASH
] = "pflash",
77 [IF_VIRTIO
] = "virtio",
81 static int if_max_devs
[IF_COUNT
] = {
83 * Do not change these numbers! They govern how drive option
84 * index maps to unit and bus. That mapping is ABI.
86 * All controllers used to implement if=T drives need to support
87 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
88 * Otherwise, some index values map to "impossible" bus, unit
91 * For instance, if you change [IF_SCSI] to 255, -drive
92 * if=scsi,index=12 no longer means bus=1,unit=5, but
93 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
94 * the drive can't be set up. Regression.
101 * Boards may call this to offer board-by-board overrides
102 * of the default, global values.
104 void override_max_devs(BlockInterfaceType type
, int max_devs
)
113 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
114 dinfo
= blk_legacy_dinfo(blk
);
115 if (dinfo
->type
== type
) {
116 fprintf(stderr
, "Cannot override units-per-bus property of"
117 " the %s interface, because a drive of that type has"
118 " already been added.\n", if_name
[type
]);
119 g_assert_not_reached();
123 if_max_devs
[type
] = max_devs
;
127 * We automatically delete the drive when a device using it gets
128 * unplugged. Questionable feature, but we can't just drop it.
129 * Device models call blockdev_mark_auto_del() to schedule the
130 * automatic deletion, and generic qdev code calls blockdev_auto_del()
131 * when deletion is actually safe.
133 void blockdev_mark_auto_del(BlockBackend
*blk
)
135 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
136 BlockDriverState
*bs
= blk_bs(blk
);
137 AioContext
*aio_context
;
144 aio_context
= bdrv_get_aio_context(bs
);
145 aio_context_acquire(aio_context
);
148 block_job_cancel(bs
->job
);
151 aio_context_release(aio_context
);
157 void blockdev_auto_del(BlockBackend
*blk
)
159 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
161 if (dinfo
&& dinfo
->auto_del
) {
162 monitor_remove_blk(blk
);
168 * Returns the current mapping of how many units per bus
169 * a particular interface can support.
171 * A positive integer indicates n units per bus.
172 * 0 implies the mapping has not been established.
173 * -1 indicates an invalid BlockInterfaceType was given.
175 int drive_get_max_devs(BlockInterfaceType type
)
177 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
178 return if_max_devs
[type
];
184 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
186 int max_devs
= if_max_devs
[type
];
187 return max_devs
? index
/ max_devs
: 0;
190 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
192 int max_devs
= if_max_devs
[type
];
193 return max_devs
? index
% max_devs
: index
;
196 QemuOpts
*drive_def(const char *optstr
)
198 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
201 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
206 opts
= drive_def(optstr
);
210 if (type
!= IF_DEFAULT
) {
211 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
214 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
217 qemu_opt_set(opts
, "file", file
, &error_abort
);
221 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
226 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
227 dinfo
= blk_legacy_dinfo(blk
);
228 if (dinfo
&& dinfo
->type
== type
229 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
237 void drive_check_orphaned(void)
242 bool orphans
= false;
244 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
245 dinfo
= blk_legacy_dinfo(blk
);
246 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
247 dinfo
->type
!= IF_NONE
) {
249 qemu_opts_loc_restore(dinfo
->opts
);
250 error_report("machine type does not support"
251 " if=%s,bus=%d,unit=%d",
252 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
263 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
265 return drive_get(type
,
266 drive_index_to_bus_id(type
, index
),
267 drive_index_to_unit_id(type
, index
));
270 int drive_get_max_bus(BlockInterfaceType type
)
277 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
278 dinfo
= blk_legacy_dinfo(blk
);
279 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
280 max_bus
= dinfo
->bus
;
286 /* Get a block device. This should only be used for single-drive devices
287 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
289 DriveInfo
*drive_get_next(BlockInterfaceType type
)
291 static int next_block_unit
[IF_COUNT
];
293 return drive_get(type
, 0, next_block_unit
[type
]++);
296 static void bdrv_format_print(void *opaque
, const char *name
)
298 error_printf(" %s", name
);
303 BlockDriverState
*bs
;
306 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
308 if (!strcmp(buf
, "ignore")) {
309 return BLOCKDEV_ON_ERROR_IGNORE
;
310 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
311 return BLOCKDEV_ON_ERROR_ENOSPC
;
312 } else if (!strcmp(buf
, "stop")) {
313 return BLOCKDEV_ON_ERROR_STOP
;
314 } else if (!strcmp(buf
, "report")) {
315 return BLOCKDEV_ON_ERROR_REPORT
;
317 error_setg(errp
, "'%s' invalid %s error action",
318 buf
, is_read
? "read" : "write");
323 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
326 const QListEntry
*entry
;
327 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
328 switch (qobject_type(entry
->value
)) {
330 case QTYPE_QSTRING
: {
331 unsigned long long length
;
332 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
333 if (parse_uint_full(str
, &length
, 10) == 0 &&
334 length
> 0 && length
<= UINT_MAX
) {
335 block_acct_add_interval(stats
, (unsigned) length
);
337 error_setg(errp
, "Invalid interval length: %s", str
);
344 int64_t length
= qnum_get_int(qobject_to_qnum(entry
->value
));
346 if (length
> 0 && length
<= UINT_MAX
) {
347 block_acct_add_interval(stats
, (unsigned) length
);
349 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
356 error_setg(errp
, "The specification of stats-intervals is invalid");
363 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
365 /* All parameters but @opts are optional and may be set to NULL. */
366 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
367 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
368 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
370 Error
*local_error
= NULL
;
374 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
375 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
378 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
379 if (!strcmp(aio
, "native")) {
380 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
381 } else if (!strcmp(aio
, "threads")) {
382 /* this is the default */
384 error_setg(errp
, "invalid aio option");
390 /* disk I/O throttling */
391 if (throttling_group
) {
392 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
396 throttle_config_init(throttle_cfg
);
397 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
398 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
399 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
400 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
401 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
402 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
403 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
404 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
405 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
406 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
407 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
408 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
410 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
411 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
412 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
413 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
414 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
415 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
416 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
417 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
419 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
420 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
421 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
424 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
425 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
426 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
427 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
428 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
429 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
430 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
431 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
432 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
433 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
434 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
436 throttle_cfg
->op_size
=
437 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
439 if (!throttle_is_valid(throttle_cfg
, errp
)) {
446 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
447 qemu_opt_get(opts
, "detect-zeroes"),
448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
451 error_propagate(errp
, local_error
);
457 /* Takes the ownership of bs_opts */
458 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
463 int on_read_error
, on_write_error
;
464 bool account_invalid
, account_failed
;
465 bool writethrough
, read_only
;
467 BlockDriverState
*bs
;
472 QDict
*interval_dict
= NULL
;
473 QList
*interval_list
= NULL
;
475 BlockdevDetectZeroesOptions detect_zeroes
=
476 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
477 const char *throttling_group
= NULL
;
479 /* Check common options by copying from bs_opts to opts, all other options
480 * stay in bs_opts for processing by bdrv_open(). */
481 id
= qdict_get_try_str(bs_opts
, "id");
482 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
484 error_propagate(errp
, error
);
488 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
490 error_propagate(errp
, error
);
495 qdict_del(bs_opts
, "id");
498 /* extract parameters */
499 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
501 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
502 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
504 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
506 id
= qemu_opts_id(opts
);
508 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
509 qdict_array_split(interval_dict
, &interval_list
);
511 if (qdict_size(interval_dict
) != 0) {
512 error_setg(errp
, "Invalid option stats-intervals.%s",
513 qdict_first(interval_dict
)->key
);
517 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
518 &detect_zeroes
, &error
);
520 error_propagate(errp
, error
);
524 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
525 if (is_help_option(buf
)) {
526 error_printf("Supported formats:");
527 bdrv_iterate_format(bdrv_format_print
, NULL
);
532 if (qdict_haskey(bs_opts
, "driver")) {
533 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
536 qdict_put_str(bs_opts
, "driver", buf
);
539 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
540 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
541 on_write_error
= parse_block_error_action(buf
, 0, &error
);
543 error_propagate(errp
, error
);
548 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
549 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
550 on_read_error
= parse_block_error_action(buf
, 1, &error
);
552 error_propagate(errp
, error
);
558 bdrv_flags
|= BDRV_O_SNAPSHOT
;
561 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
564 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
565 BlockBackendRootState
*blk_rs
;
567 blk
= blk_new(0, BLK_PERM_ALL
);
568 blk_rs
= blk_get_root_state(blk
);
569 blk_rs
->open_flags
= bdrv_flags
;
570 blk_rs
->read_only
= read_only
;
571 blk_rs
->detect_zeroes
= detect_zeroes
;
575 if (file
&& !*file
) {
579 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
580 * with other callers) rather than what we want as the real defaults.
581 * Apply the defaults here instead. */
582 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
583 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
584 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
585 read_only
? "on" : "off");
586 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
588 if (runstate_check(RUN_STATE_INMIGRATE
)) {
589 bdrv_flags
|= BDRV_O_INACTIVE
;
592 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
598 bs
->detect_zeroes
= detect_zeroes
;
600 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
602 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
609 /* disk I/O throttling */
610 if (throttle_enabled(&cfg
)) {
611 if (!throttling_group
) {
612 throttling_group
= id
;
614 blk_io_limits_enable(blk
, throttling_group
);
615 blk_set_io_limits(blk
, &cfg
);
618 blk_set_enable_write_cache(blk
, !writethrough
);
619 blk_set_on_error(blk
, on_read_error
, on_write_error
);
621 if (!monitor_add_blk(blk
, id
, errp
)) {
629 QDECREF(interval_dict
);
630 QDECREF(interval_list
);
635 QDECREF(interval_dict
);
636 QDECREF(interval_list
);
642 /* Takes the ownership of bs_opts */
643 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
647 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
648 * with other callers) rather than what we want as the real defaults.
649 * Apply the defaults here instead. */
650 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
651 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
652 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
654 if (runstate_check(RUN_STATE_INMIGRATE
)) {
655 bdrv_flags
|= BDRV_O_INACTIVE
;
658 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
661 void blockdev_close_all_bdrv_states(void)
663 BlockDriverState
*bs
, *next_bs
;
665 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
666 AioContext
*ctx
= bdrv_get_aio_context(bs
);
668 aio_context_acquire(ctx
);
670 aio_context_release(ctx
);
674 /* Iterates over the list of monitor-owned BlockDriverStates */
675 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
677 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
678 : QTAILQ_FIRST(&monitor_bdrv_states
);
681 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
686 value
= qemu_opt_get(opts
, from
);
688 if (qemu_opt_find(opts
, to
)) {
689 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
690 "same time", to
, from
);
695 /* rename all items in opts */
696 while ((value
= qemu_opt_get(opts
, from
))) {
697 qemu_opt_set(opts
, to
, value
, &error_abort
);
698 qemu_opt_unset(opts
, from
);
702 QemuOptsList qemu_legacy_drive_opts
= {
704 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
708 .type
= QEMU_OPT_NUMBER
,
709 .help
= "bus number",
712 .type
= QEMU_OPT_NUMBER
,
713 .help
= "unit number (i.e. lun for scsi)",
716 .type
= QEMU_OPT_NUMBER
,
717 .help
= "index number",
720 .type
= QEMU_OPT_STRING
,
721 .help
= "media type (disk, cdrom)",
724 .type
= QEMU_OPT_STRING
,
725 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
728 .type
= QEMU_OPT_NUMBER
,
729 .help
= "number of cylinders (ide disk geometry)",
732 .type
= QEMU_OPT_NUMBER
,
733 .help
= "number of heads (ide disk geometry)",
736 .type
= QEMU_OPT_NUMBER
,
737 .help
= "number of sectors (ide disk geometry)",
740 .type
= QEMU_OPT_STRING
,
741 .help
= "chs translation (auto, lba, none)",
744 .type
= QEMU_OPT_STRING
,
745 .help
= "pci address (virtio only)",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "disk serial number",
752 .type
= QEMU_OPT_STRING
,
756 /* Options that are passed on, but have special semantics with -drive */
758 .name
= BDRV_OPT_READ_ONLY
,
759 .type
= QEMU_OPT_BOOL
,
760 .help
= "open drive file as read-only",
763 .type
= QEMU_OPT_STRING
,
764 .help
= "read error action",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "write error action",
770 .name
= "copy-on-read",
771 .type
= QEMU_OPT_BOOL
,
772 .help
= "copy read data from backing file into image file",
775 { /* end of list */ }
779 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
783 DriveInfo
*dinfo
= NULL
;
785 QemuOpts
*legacy_opts
;
786 DriveMediaType media
= MEDIA_DISK
;
787 BlockInterfaceType type
;
788 int cyls
, heads
, secs
, translation
;
789 int max_devs
, bus_id
, unit_id
, index
;
791 const char *werror
, *rerror
;
792 bool read_only
= false;
795 const char *filename
;
796 Error
*local_err
= NULL
;
798 const char *deprecated
[] = {
799 "serial", "trans", "secs", "heads", "cyls", "addr"
802 /* Change legacy command line options into QMP ones */
803 static const struct {
807 { "iops", "throttling.iops-total" },
808 { "iops_rd", "throttling.iops-read" },
809 { "iops_wr", "throttling.iops-write" },
811 { "bps", "throttling.bps-total" },
812 { "bps_rd", "throttling.bps-read" },
813 { "bps_wr", "throttling.bps-write" },
815 { "iops_max", "throttling.iops-total-max" },
816 { "iops_rd_max", "throttling.iops-read-max" },
817 { "iops_wr_max", "throttling.iops-write-max" },
819 { "bps_max", "throttling.bps-total-max" },
820 { "bps_rd_max", "throttling.bps-read-max" },
821 { "bps_wr_max", "throttling.bps-write-max" },
823 { "iops_size", "throttling.iops-size" },
825 { "group", "throttling.group" },
827 { "readonly", BDRV_OPT_READ_ONLY
},
830 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
831 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
834 error_report_err(local_err
);
839 value
= qemu_opt_get(all_opts
, "cache");
844 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
845 error_report("invalid cache option");
849 /* Specific options take precedence */
850 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
851 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
852 !writethrough
, &error_abort
);
854 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
855 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
856 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
858 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
859 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
860 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
862 qemu_opt_unset(all_opts
, "cache");
865 /* Get a QDict for processing the options */
866 bs_opts
= qdict_new();
867 qemu_opts_to_qdict(all_opts
, bs_opts
);
869 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
871 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
873 error_report_err(local_err
);
877 /* Other deprecated options */
878 if (!qtest_enabled()) {
879 for (i
= 0; i
< ARRAY_SIZE(deprecated
); i
++) {
880 if (qemu_opt_get(legacy_opts
, deprecated
[i
]) != NULL
) {
881 error_report("'%s' is deprecated, please use the corresponding "
882 "option of '-device' instead", deprecated
[i
]);
888 value
= qemu_opt_get(legacy_opts
, "media");
890 if (!strcmp(value
, "disk")) {
892 } else if (!strcmp(value
, "cdrom")) {
896 error_report("'%s' invalid media", value
);
901 /* copy-on-read is disabled with a warning for read-only devices */
902 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
903 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
905 if (read_only
&& copy_on_read
) {
906 warn_report("disabling copy-on-read on read-only drive");
907 copy_on_read
= false;
910 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
911 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
913 /* Controller type */
914 value
= qemu_opt_get(legacy_opts
, "if");
917 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
920 if (type
== IF_COUNT
) {
921 error_report("unsupported bus type '%s'", value
);
925 type
= block_default_type
;
929 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
930 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
931 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
933 if (cyls
|| heads
|| secs
) {
935 error_report("invalid physical cyls number");
939 error_report("invalid physical heads number");
943 error_report("invalid physical secs number");
948 translation
= BIOS_ATA_TRANSLATION_AUTO
;
949 value
= qemu_opt_get(legacy_opts
, "trans");
952 error_report("'%s' trans must be used with cyls, heads and secs",
956 if (!strcmp(value
, "none")) {
957 translation
= BIOS_ATA_TRANSLATION_NONE
;
958 } else if (!strcmp(value
, "lba")) {
959 translation
= BIOS_ATA_TRANSLATION_LBA
;
960 } else if (!strcmp(value
, "large")) {
961 translation
= BIOS_ATA_TRANSLATION_LARGE
;
962 } else if (!strcmp(value
, "rechs")) {
963 translation
= BIOS_ATA_TRANSLATION_RECHS
;
964 } else if (!strcmp(value
, "auto")) {
965 translation
= BIOS_ATA_TRANSLATION_AUTO
;
967 error_report("'%s' invalid translation type", value
);
972 if (media
== MEDIA_CDROM
) {
973 if (cyls
|| secs
|| heads
) {
974 error_report("CHS can't be set with media=cdrom");
979 /* Device address specified by bus/unit or index.
980 * If none was specified, try to find the first free one. */
981 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
982 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
983 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
985 max_devs
= if_max_devs
[type
];
988 if (bus_id
!= 0 || unit_id
!= -1) {
989 error_report("index cannot be used with bus and unit");
992 bus_id
= drive_index_to_bus_id(type
, index
);
993 unit_id
= drive_index_to_unit_id(type
, index
);
998 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1000 if (max_devs
&& unit_id
>= max_devs
) {
1001 unit_id
-= max_devs
;
1007 if (max_devs
&& unit_id
>= max_devs
) {
1008 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1012 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1013 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1014 bus_id
, unit_id
, index
);
1019 serial
= qemu_opt_get(legacy_opts
, "serial");
1021 /* no id supplied -> create one */
1022 if (qemu_opts_id(all_opts
) == NULL
) {
1024 const char *mediastr
= "";
1025 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1026 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1029 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1032 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1035 qdict_put_str(bs_opts
, "id", new_id
);
1039 /* Add virtio block device */
1040 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1041 if (devaddr
&& type
!= IF_VIRTIO
) {
1042 error_report("addr is not supported by this bus type");
1046 if (type
== IF_VIRTIO
) {
1048 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1050 if (arch_type
== QEMU_ARCH_S390X
) {
1051 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1053 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1055 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1058 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1062 filename
= qemu_opt_get(legacy_opts
, "file");
1064 /* Check werror/rerror compatibility with if=... */
1065 werror
= qemu_opt_get(legacy_opts
, "werror");
1066 if (werror
!= NULL
) {
1067 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1069 error_report("werror is not supported by this bus type");
1072 qdict_put_str(bs_opts
, "werror", werror
);
1075 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1076 if (rerror
!= NULL
) {
1077 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1079 error_report("rerror is not supported by this bus type");
1082 qdict_put_str(bs_opts
, "rerror", rerror
);
1085 /* Actual block device init: Functionality shared with blockdev-add */
1086 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1090 error_report_err(local_err
);
1097 /* Create legacy DriveInfo */
1098 dinfo
= g_malloc0(sizeof(*dinfo
));
1099 dinfo
->opts
= all_opts
;
1102 dinfo
->heads
= heads
;
1104 dinfo
->trans
= translation
;
1107 dinfo
->bus
= bus_id
;
1108 dinfo
->unit
= unit_id
;
1109 dinfo
->devaddr
= devaddr
;
1110 dinfo
->serial
= g_strdup(serial
);
1112 blk_set_legacy_dinfo(blk
, dinfo
);
1119 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1126 qemu_opts_del(legacy_opts
);
1131 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1133 BlockDriverState
*bs
;
1135 bs
= bdrv_lookup_bs(name
, name
, errp
);
1140 if (!bdrv_is_root_node(bs
)) {
1141 error_setg(errp
, "Need a root block node");
1145 if (!bdrv_is_inserted(bs
)) {
1146 error_setg(errp
, "Device has no medium");
1153 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1158 if (!blk_name
== !qdev_id
) {
1159 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1164 blk
= blk_by_qdev_id(qdev_id
, errp
);
1166 blk
= blk_by_name(blk_name
);
1168 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1169 "Device '%s' not found", blk_name
);
1176 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1178 const char *device
= qdict_get_str(qdict
, "device");
1182 if (!strcmp(device
, "all")) {
1183 ret
= blk_commit_all();
1185 BlockDriverState
*bs
;
1186 AioContext
*aio_context
;
1188 blk
= blk_by_name(device
);
1190 monitor_printf(mon
, "Device '%s' not found\n", device
);
1193 if (!blk_is_available(blk
)) {
1194 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1199 aio_context
= bdrv_get_aio_context(bs
);
1200 aio_context_acquire(aio_context
);
1202 ret
= bdrv_commit(bs
);
1204 aio_context_release(aio_context
);
1207 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1212 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1214 TransactionActionList list
;
1216 list
.value
= action
;
1218 qmp_transaction(&list
, false, NULL
, errp
);
1221 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1222 bool has_node_name
, const char *node_name
,
1223 const char *snapshot_file
,
1224 bool has_snapshot_node_name
,
1225 const char *snapshot_node_name
,
1226 bool has_format
, const char *format
,
1227 bool has_mode
, NewImageMode mode
, Error
**errp
)
1229 BlockdevSnapshotSync snapshot
= {
1230 .has_device
= has_device
,
1231 .device
= (char *) device
,
1232 .has_node_name
= has_node_name
,
1233 .node_name
= (char *) node_name
,
1234 .snapshot_file
= (char *) snapshot_file
,
1235 .has_snapshot_node_name
= has_snapshot_node_name
,
1236 .snapshot_node_name
= (char *) snapshot_node_name
,
1237 .has_format
= has_format
,
1238 .format
= (char *) format
,
1239 .has_mode
= has_mode
,
1242 TransactionAction action
= {
1243 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1244 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1246 blockdev_do_action(&action
, errp
);
1249 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1252 BlockdevSnapshot snapshot_data
= {
1253 .node
= (char *) node
,
1254 .overlay
= (char *) overlay
1256 TransactionAction action
= {
1257 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1258 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1260 blockdev_do_action(&action
, errp
);
1263 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1267 BlockdevSnapshotInternal snapshot
= {
1268 .device
= (char *) device
,
1269 .name
= (char *) name
1271 TransactionAction action
= {
1272 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1273 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1275 blockdev_do_action(&action
, errp
);
1278 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1285 BlockDriverState
*bs
;
1286 AioContext
*aio_context
;
1287 QEMUSnapshotInfo sn
;
1288 Error
*local_err
= NULL
;
1289 SnapshotInfo
*info
= NULL
;
1292 bs
= qmp_get_root_bs(device
, errp
);
1296 aio_context
= bdrv_get_aio_context(bs
);
1297 aio_context_acquire(aio_context
);
1308 error_setg(errp
, "Name or id must be provided");
1309 goto out_aio_context
;
1312 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1313 goto out_aio_context
;
1316 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1318 error_propagate(errp
, local_err
);
1319 goto out_aio_context
;
1323 "Snapshot with id '%s' and name '%s' does not exist on "
1325 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1326 goto out_aio_context
;
1329 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1331 error_propagate(errp
, local_err
);
1332 goto out_aio_context
;
1335 aio_context_release(aio_context
);
1337 info
= g_new0(SnapshotInfo
, 1);
1338 info
->id
= g_strdup(sn
.id_str
);
1339 info
->name
= g_strdup(sn
.name
);
1340 info
->date_nsec
= sn
.date_nsec
;
1341 info
->date_sec
= sn
.date_sec
;
1342 info
->vm_state_size
= sn
.vm_state_size
;
1343 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1344 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1349 aio_context_release(aio_context
);
1354 * block_dirty_bitmap_lookup:
1355 * Return a dirty bitmap (if present), after validating
1356 * the node reference and bitmap names.
1358 * @node: The name of the BDS node to search for bitmaps
1359 * @name: The name of the bitmap to search for
1360 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1361 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1362 * @errp: Output pointer for error information. Can be NULL.
1364 * @return: A bitmap object on success, or NULL on failure.
1366 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1368 BlockDriverState
**pbs
,
1371 BlockDriverState
*bs
;
1372 BdrvDirtyBitmap
*bitmap
;
1375 error_setg(errp
, "Node cannot be NULL");
1379 error_setg(errp
, "Bitmap name cannot be NULL");
1382 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1384 error_setg(errp
, "Node '%s' not found", node
);
1388 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1390 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1401 /* New and old BlockDriverState structs for atomic group operations */
1403 typedef struct BlkActionState BlkActionState
;
1407 * Table of operations that define an Action.
1409 * @instance_size: Size of state struct, in bytes.
1410 * @prepare: Prepare the work, must NOT be NULL.
1411 * @commit: Commit the changes, can be NULL.
1412 * @abort: Abort the changes on fail, can be NULL.
1413 * @clean: Clean up resources after all transaction actions have called
1414 * commit() or abort(). Can be NULL.
1416 * Only prepare() may fail. In a single transaction, only one of commit() or
1417 * abort() will be called. clean() will always be called if it is present.
1419 typedef struct BlkActionOps
{
1420 size_t instance_size
;
1421 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1422 void (*commit
)(BlkActionState
*common
);
1423 void (*abort
)(BlkActionState
*common
);
1424 void (*clean
)(BlkActionState
*common
);
1429 * Describes one Action's state within a Transaction.
1431 * @action: QAPI-defined enum identifying which Action to perform.
1432 * @ops: Table of ActionOps this Action can perform.
1433 * @block_job_txn: Transaction which this action belongs to.
1434 * @entry: List membership for all Actions in this Transaction.
1436 * This structure must be arranged as first member in a subclassed type,
1437 * assuming that the compiler will also arrange it to the same offsets as the
1440 struct BlkActionState
{
1441 TransactionAction
*action
;
1442 const BlkActionOps
*ops
;
1443 BlockJobTxn
*block_job_txn
;
1444 TransactionProperties
*txn_props
;
1445 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1448 /* internal snapshot private data */
1449 typedef struct InternalSnapshotState
{
1450 BlkActionState common
;
1451 BlockDriverState
*bs
;
1452 QEMUSnapshotInfo sn
;
1454 } InternalSnapshotState
;
1457 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1459 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1461 "Action '%s' does not support Transaction property "
1462 "completion-mode = %s",
1463 TransactionActionKind_str(s
->action
->type
),
1464 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1470 static void internal_snapshot_prepare(BlkActionState
*common
,
1473 Error
*local_err
= NULL
;
1476 BlockDriverState
*bs
;
1477 QEMUSnapshotInfo old_sn
, *sn
;
1480 BlockdevSnapshotInternal
*internal
;
1481 InternalSnapshotState
*state
;
1482 AioContext
*aio_context
;
1485 g_assert(common
->action
->type
==
1486 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1487 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1488 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1490 /* 1. parse input */
1491 device
= internal
->device
;
1492 name
= internal
->name
;
1494 /* 2. check for validation */
1495 if (action_check_completion_mode(common
, errp
) < 0) {
1499 bs
= qmp_get_root_bs(device
, errp
);
1504 aio_context
= bdrv_get_aio_context(bs
);
1505 aio_context_acquire(aio_context
);
1509 /* Paired with .clean() */
1510 bdrv_drained_begin(bs
);
1512 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1516 if (bdrv_is_read_only(bs
)) {
1517 error_setg(errp
, "Device '%s' is read only", device
);
1521 if (!bdrv_can_snapshot(bs
)) {
1522 error_setg(errp
, "Block format '%s' used by device '%s' "
1523 "does not support internal snapshots",
1524 bs
->drv
->format_name
, device
);
1528 if (!strlen(name
)) {
1529 error_setg(errp
, "Name is empty");
1533 /* check whether a snapshot with name exist */
1534 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1537 error_propagate(errp
, local_err
);
1541 "Snapshot with name '%s' already exists on device '%s'",
1546 /* 3. take the snapshot */
1548 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1549 qemu_gettimeofday(&tv
);
1550 sn
->date_sec
= tv
.tv_sec
;
1551 sn
->date_nsec
= tv
.tv_usec
* 1000;
1552 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1554 ret1
= bdrv_snapshot_create(bs
, sn
);
1556 error_setg_errno(errp
, -ret1
,
1557 "Failed to create snapshot '%s' on device '%s'",
1562 /* 4. succeed, mark a snapshot is created */
1563 state
->created
= true;
1566 aio_context_release(aio_context
);
1569 static void internal_snapshot_abort(BlkActionState
*common
)
1571 InternalSnapshotState
*state
=
1572 DO_UPCAST(InternalSnapshotState
, common
, common
);
1573 BlockDriverState
*bs
= state
->bs
;
1574 QEMUSnapshotInfo
*sn
= &state
->sn
;
1575 AioContext
*aio_context
;
1576 Error
*local_error
= NULL
;
1578 if (!state
->created
) {
1582 aio_context
= bdrv_get_aio_context(state
->bs
);
1583 aio_context_acquire(aio_context
);
1585 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1586 error_reportf_err(local_error
,
1587 "Failed to delete snapshot with id '%s' and "
1588 "name '%s' on device '%s' in abort: ",
1589 sn
->id_str
, sn
->name
,
1590 bdrv_get_device_name(bs
));
1593 aio_context_release(aio_context
);
1596 static void internal_snapshot_clean(BlkActionState
*common
)
1598 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1600 AioContext
*aio_context
;
1606 aio_context
= bdrv_get_aio_context(state
->bs
);
1607 aio_context_acquire(aio_context
);
1609 bdrv_drained_end(state
->bs
);
1611 aio_context_release(aio_context
);
1614 /* external snapshot private data */
1615 typedef struct ExternalSnapshotState
{
1616 BlkActionState common
;
1617 BlockDriverState
*old_bs
;
1618 BlockDriverState
*new_bs
;
1619 bool overlay_appended
;
1620 } ExternalSnapshotState
;
1622 static void external_snapshot_prepare(BlkActionState
*common
,
1626 QDict
*options
= NULL
;
1627 Error
*local_err
= NULL
;
1628 /* Device and node name of the image to generate the snapshot from */
1630 const char *node_name
;
1631 /* Reference to the new image (for 'blockdev-snapshot') */
1632 const char *snapshot_ref
;
1633 /* File name of the new image (for 'blockdev-snapshot-sync') */
1634 const char *new_image_file
;
1635 ExternalSnapshotState
*state
=
1636 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1637 TransactionAction
*action
= common
->action
;
1638 AioContext
*aio_context
;
1640 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1641 * purpose but a different set of parameters */
1642 switch (action
->type
) {
1643 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1645 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1647 node_name
= s
->node
;
1648 new_image_file
= NULL
;
1649 snapshot_ref
= s
->overlay
;
1652 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1654 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1655 device
= s
->has_device
? s
->device
: NULL
;
1656 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1657 new_image_file
= s
->snapshot_file
;
1658 snapshot_ref
= NULL
;
1662 g_assert_not_reached();
1665 /* start processing */
1666 if (action_check_completion_mode(common
, errp
) < 0) {
1670 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1671 if (!state
->old_bs
) {
1675 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1676 aio_context_acquire(aio_context
);
1678 /* Paired with .clean() */
1679 bdrv_drained_begin(state
->old_bs
);
1681 if (!bdrv_is_inserted(state
->old_bs
)) {
1682 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1686 if (bdrv_op_is_blocked(state
->old_bs
,
1687 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1691 if (!bdrv_is_read_only(state
->old_bs
)) {
1692 if (bdrv_flush(state
->old_bs
)) {
1693 error_setg(errp
, QERR_IO_ERROR
);
1698 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1699 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1703 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1704 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1705 const char *format
= s
->has_format
? s
->format
: "qcow2";
1706 enum NewImageMode mode
;
1707 const char *snapshot_node_name
=
1708 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1710 if (node_name
&& !snapshot_node_name
) {
1711 error_setg(errp
, "New snapshot node name missing");
1715 if (snapshot_node_name
&&
1716 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1717 error_setg(errp
, "New snapshot node name already in use");
1721 flags
= state
->old_bs
->open_flags
;
1722 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1723 flags
|= BDRV_O_NO_BACKING
;
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
, false, &local_err
);
1738 error_propagate(errp
, local_err
);
1743 options
= qdict_new();
1744 if (s
->has_snapshot_node_name
) {
1745 qdict_put_str(options
, "node-name", snapshot_node_name
);
1747 qdict_put_str(options
, "driver", format
);
1750 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1752 /* We will manually add the backing_hd field to the bs later */
1753 if (!state
->new_bs
) {
1757 if (bdrv_has_blk(state
->new_bs
)) {
1758 error_setg(errp
, "The snapshot is already in use");
1762 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1767 if (state
->new_bs
->backing
!= NULL
) {
1768 error_setg(errp
, "The snapshot already has a backing image");
1772 if (!state
->new_bs
->drv
->supports_backing
) {
1773 error_setg(errp
, "The snapshot does not support backing images");
1777 bdrv_set_aio_context(state
->new_bs
, aio_context
);
1779 /* This removes our old bs and adds the new bs. This is an operation that
1780 * can fail, so we need to do it in .prepare; undoing it for abort is
1781 * always possible. */
1782 bdrv_ref(state
->new_bs
);
1783 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1785 error_propagate(errp
, local_err
);
1788 state
->overlay_appended
= true;
1791 aio_context_release(aio_context
);
1794 static void external_snapshot_commit(BlkActionState
*common
)
1796 ExternalSnapshotState
*state
=
1797 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1798 AioContext
*aio_context
;
1800 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1801 aio_context_acquire(aio_context
);
1803 /* We don't need (or want) to use the transactional
1804 * bdrv_reopen_multiple() across all the entries at once, because we
1805 * don't want to abort all of them if one of them fails the reopen */
1806 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1807 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1811 aio_context_release(aio_context
);
1814 static void external_snapshot_abort(BlkActionState
*common
)
1816 ExternalSnapshotState
*state
=
1817 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1818 if (state
->new_bs
) {
1819 if (state
->overlay_appended
) {
1820 AioContext
*aio_context
;
1822 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1823 aio_context_acquire(aio_context
);
1825 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1826 close state->old_bs; we need it */
1827 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1828 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1829 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1831 aio_context_release(aio_context
);
1836 static void external_snapshot_clean(BlkActionState
*common
)
1838 ExternalSnapshotState
*state
=
1839 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1840 AioContext
*aio_context
;
1842 if (!state
->old_bs
) {
1846 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1847 aio_context_acquire(aio_context
);
1849 bdrv_drained_end(state
->old_bs
);
1850 bdrv_unref(state
->new_bs
);
1852 aio_context_release(aio_context
);
1855 typedef struct DriveBackupState
{
1856 BlkActionState common
;
1857 BlockDriverState
*bs
;
1861 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
1864 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1866 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1867 BlockDriverState
*bs
;
1868 DriveBackup
*backup
;
1869 AioContext
*aio_context
;
1870 Error
*local_err
= NULL
;
1872 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1873 backup
= common
->action
->u
.drive_backup
.data
;
1875 bs
= qmp_get_root_bs(backup
->device
, errp
);
1880 aio_context
= bdrv_get_aio_context(bs
);
1881 aio_context_acquire(aio_context
);
1883 /* Paired with .clean() */
1884 bdrv_drained_begin(bs
);
1888 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1890 error_propagate(errp
, local_err
);
1895 aio_context_release(aio_context
);
1898 static void drive_backup_commit(BlkActionState
*common
)
1900 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1901 AioContext
*aio_context
;
1903 aio_context
= bdrv_get_aio_context(state
->bs
);
1904 aio_context_acquire(aio_context
);
1907 block_job_start(state
->job
);
1909 aio_context_release(aio_context
);
1912 static void drive_backup_abort(BlkActionState
*common
)
1914 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1917 AioContext
*aio_context
;
1919 aio_context
= bdrv_get_aio_context(state
->bs
);
1920 aio_context_acquire(aio_context
);
1922 block_job_cancel_sync(state
->job
);
1924 aio_context_release(aio_context
);
1928 static void drive_backup_clean(BlkActionState
*common
)
1930 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1931 AioContext
*aio_context
;
1937 aio_context
= bdrv_get_aio_context(state
->bs
);
1938 aio_context_acquire(aio_context
);
1940 bdrv_drained_end(state
->bs
);
1942 aio_context_release(aio_context
);
1945 typedef struct BlockdevBackupState
{
1946 BlkActionState common
;
1947 BlockDriverState
*bs
;
1949 } BlockdevBackupState
;
1951 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
1954 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1956 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1957 BlockdevBackup
*backup
;
1958 BlockDriverState
*bs
, *target
;
1959 AioContext
*aio_context
;
1960 Error
*local_err
= NULL
;
1962 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1963 backup
= common
->action
->u
.blockdev_backup
.data
;
1965 bs
= qmp_get_root_bs(backup
->device
, errp
);
1970 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1975 aio_context
= bdrv_get_aio_context(bs
);
1976 if (aio_context
!= bdrv_get_aio_context(target
)) {
1977 error_setg(errp
, "Backup between two IO threads is not implemented");
1980 aio_context_acquire(aio_context
);
1983 /* Paired with .clean() */
1984 bdrv_drained_begin(state
->bs
);
1986 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1988 error_propagate(errp
, local_err
);
1993 aio_context_release(aio_context
);
1996 static void blockdev_backup_commit(BlkActionState
*common
)
1998 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1999 AioContext
*aio_context
;
2001 aio_context
= bdrv_get_aio_context(state
->bs
);
2002 aio_context_acquire(aio_context
);
2005 block_job_start(state
->job
);
2007 aio_context_release(aio_context
);
2010 static void blockdev_backup_abort(BlkActionState
*common
)
2012 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2015 AioContext
*aio_context
;
2017 aio_context
= bdrv_get_aio_context(state
->bs
);
2018 aio_context_acquire(aio_context
);
2020 block_job_cancel_sync(state
->job
);
2022 aio_context_release(aio_context
);
2026 static void blockdev_backup_clean(BlkActionState
*common
)
2028 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2029 AioContext
*aio_context
;
2035 aio_context
= bdrv_get_aio_context(state
->bs
);
2036 aio_context_acquire(aio_context
);
2038 bdrv_drained_end(state
->bs
);
2040 aio_context_release(aio_context
);
2043 typedef struct BlockDirtyBitmapState
{
2044 BlkActionState common
;
2045 BdrvDirtyBitmap
*bitmap
;
2046 BlockDriverState
*bs
;
2049 } BlockDirtyBitmapState
;
2051 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2054 Error
*local_err
= NULL
;
2055 BlockDirtyBitmapAdd
*action
;
2056 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2059 if (action_check_completion_mode(common
, errp
) < 0) {
2063 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2064 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2065 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2066 action
->has_granularity
, action
->granularity
,
2067 action
->has_persistent
, action
->persistent
,
2068 action
->has_autoload
, action
->autoload
,
2072 state
->prepared
= true;
2074 error_propagate(errp
, local_err
);
2078 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2080 BlockDirtyBitmapAdd
*action
;
2081 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2084 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2085 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2086 * then the node reference and bitmap name must have been valid.
2088 if (state
->prepared
) {
2089 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2093 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2096 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2098 BlockDirtyBitmap
*action
;
2100 if (action_check_completion_mode(common
, errp
) < 0) {
2104 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2105 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2109 if (!state
->bitmap
) {
2113 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2114 error_setg(errp
, "Cannot modify a frozen bitmap");
2116 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2117 error_setg(errp
, "Cannot clear a disabled bitmap");
2119 } else if (bdrv_dirty_bitmap_readonly(state
->bitmap
)) {
2120 error_setg(errp
, "Cannot clear a readonly bitmap");
2124 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2127 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2129 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2132 if (state
->backup
) {
2133 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2137 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2139 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2142 hbitmap_free(state
->backup
);
2145 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2147 error_setg(errp
, "Transaction aborted using Abort action");
2150 static void abort_commit(BlkActionState
*common
)
2152 g_assert_not_reached(); /* this action never succeeds */
2155 static const BlkActionOps actions
[] = {
2156 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2157 .instance_size
= sizeof(ExternalSnapshotState
),
2158 .prepare
= external_snapshot_prepare
,
2159 .commit
= external_snapshot_commit
,
2160 .abort
= external_snapshot_abort
,
2161 .clean
= external_snapshot_clean
,
2163 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2164 .instance_size
= sizeof(ExternalSnapshotState
),
2165 .prepare
= external_snapshot_prepare
,
2166 .commit
= external_snapshot_commit
,
2167 .abort
= external_snapshot_abort
,
2168 .clean
= external_snapshot_clean
,
2170 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2171 .instance_size
= sizeof(DriveBackupState
),
2172 .prepare
= drive_backup_prepare
,
2173 .commit
= drive_backup_commit
,
2174 .abort
= drive_backup_abort
,
2175 .clean
= drive_backup_clean
,
2177 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2178 .instance_size
= sizeof(BlockdevBackupState
),
2179 .prepare
= blockdev_backup_prepare
,
2180 .commit
= blockdev_backup_commit
,
2181 .abort
= blockdev_backup_abort
,
2182 .clean
= blockdev_backup_clean
,
2184 [TRANSACTION_ACTION_KIND_ABORT
] = {
2185 .instance_size
= sizeof(BlkActionState
),
2186 .prepare
= abort_prepare
,
2187 .commit
= abort_commit
,
2189 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2190 .instance_size
= sizeof(InternalSnapshotState
),
2191 .prepare
= internal_snapshot_prepare
,
2192 .abort
= internal_snapshot_abort
,
2193 .clean
= internal_snapshot_clean
,
2195 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2196 .instance_size
= sizeof(BlockDirtyBitmapState
),
2197 .prepare
= block_dirty_bitmap_add_prepare
,
2198 .abort
= block_dirty_bitmap_add_abort
,
2200 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2201 .instance_size
= sizeof(BlockDirtyBitmapState
),
2202 .prepare
= block_dirty_bitmap_clear_prepare
,
2203 .commit
= block_dirty_bitmap_clear_commit
,
2204 .abort
= block_dirty_bitmap_clear_abort
,
2209 * Allocate a TransactionProperties structure if necessary, and fill
2210 * that structure with desired defaults if they are unset.
2212 static TransactionProperties
*get_transaction_properties(
2213 TransactionProperties
*props
)
2216 props
= g_new0(TransactionProperties
, 1);
2219 if (!props
->has_completion_mode
) {
2220 props
->has_completion_mode
= true;
2221 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2228 * 'Atomic' group operations. The operations are performed as a set, and if
2229 * any fail then we roll back all operations in the group.
2231 void qmp_transaction(TransactionActionList
*dev_list
,
2233 struct TransactionProperties
*props
,
2236 TransactionActionList
*dev_entry
= dev_list
;
2237 BlockJobTxn
*block_job_txn
= NULL
;
2238 BlkActionState
*state
, *next
;
2239 Error
*local_err
= NULL
;
2241 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2242 QSIMPLEQ_INIT(&snap_bdrv_states
);
2244 /* Does this transaction get canceled as a group on failure?
2245 * If not, we don't really need to make a BlockJobTxn.
2247 props
= get_transaction_properties(props
);
2248 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2249 block_job_txn
= block_job_txn_new();
2252 /* drain all i/o before any operations */
2255 /* We don't do anything in this loop that commits us to the operations */
2256 while (NULL
!= dev_entry
) {
2257 TransactionAction
*dev_info
= NULL
;
2258 const BlkActionOps
*ops
;
2260 dev_info
= dev_entry
->value
;
2261 dev_entry
= dev_entry
->next
;
2263 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2265 ops
= &actions
[dev_info
->type
];
2266 assert(ops
->instance_size
> 0);
2268 state
= g_malloc0(ops
->instance_size
);
2270 state
->action
= dev_info
;
2271 state
->block_job_txn
= block_job_txn
;
2272 state
->txn_props
= props
;
2273 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2275 state
->ops
->prepare(state
, &local_err
);
2277 error_propagate(errp
, local_err
);
2278 goto delete_and_fail
;
2282 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2283 if (state
->ops
->commit
) {
2284 state
->ops
->commit(state
);
2292 /* failure, and it is all-or-none; roll back all operations */
2293 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2294 if (state
->ops
->abort
) {
2295 state
->ops
->abort(state
);
2299 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2300 if (state
->ops
->clean
) {
2301 state
->ops
->clean(state
);
2306 qapi_free_TransactionProperties(props
);
2308 block_job_txn_unref(block_job_txn
);
2311 void qmp_eject(bool has_device
, const char *device
,
2312 bool has_id
, const char *id
,
2313 bool has_force
, bool force
, Error
**errp
)
2315 Error
*local_err
= NULL
;
2322 rc
= do_open_tray(has_device
? device
: NULL
,
2325 if (rc
&& rc
!= -ENOSYS
) {
2326 error_propagate(errp
, local_err
);
2329 error_free(local_err
);
2331 blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2334 void qmp_block_passwd(bool has_device
, const char *device
,
2335 bool has_node_name
, const char *node_name
,
2336 const char *password
, Error
**errp
)
2339 "Setting block passwords directly is no longer supported");
2343 * Attempt to open the tray of @device.
2344 * If @force, ignore its tray lock.
2345 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2346 * On error, store an error through @errp and return -errno.
2347 * If @device does not exist, return -ENODEV.
2348 * If it has no removable media, return -ENOTSUP.
2349 * If it has no tray, return -ENOSYS.
2350 * If the guest was asked to open the tray, return -EINPROGRESS.
2353 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2354 bool force
, Error
**errp
)
2357 const char *device
= qdev_id
?: blk_name
;
2360 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2365 if (!blk_dev_has_removable_media(blk
)) {
2366 error_setg(errp
, "Device '%s' is not removable", device
);
2370 if (!blk_dev_has_tray(blk
)) {
2371 error_setg(errp
, "Device '%s' does not have a tray", device
);
2375 if (blk_dev_is_tray_open(blk
)) {
2379 locked
= blk_dev_is_medium_locked(blk
);
2381 blk_dev_eject_request(blk
, force
);
2384 if (!locked
|| force
) {
2385 blk_dev_change_media_cb(blk
, false, &error_abort
);
2388 if (locked
&& !force
) {
2389 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2390 "wait for tray to open and try again", device
);
2391 return -EINPROGRESS
;
2397 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2398 bool has_id
, const char *id
,
2399 bool has_force
, bool force
,
2402 Error
*local_err
= NULL
;
2408 rc
= do_open_tray(has_device
? device
: NULL
,
2411 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2412 error_propagate(errp
, local_err
);
2415 error_free(local_err
);
2418 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2419 bool has_id
, const char *id
,
2423 Error
*local_err
= NULL
;
2425 device
= has_device
? device
: NULL
;
2426 id
= has_id
? id
: NULL
;
2428 blk
= qmp_get_blk(device
, id
, errp
);
2433 if (!blk_dev_has_removable_media(blk
)) {
2434 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2438 if (!blk_dev_has_tray(blk
)) {
2439 /* Ignore this command on tray-less devices */
2443 if (!blk_dev_is_tray_open(blk
)) {
2447 blk_dev_change_media_cb(blk
, true, &local_err
);
2449 error_propagate(errp
, local_err
);
2454 static void blockdev_remove_medium(bool has_device
, const char *device
,
2455 bool has_id
, const char *id
, Error
**errp
)
2458 BlockDriverState
*bs
;
2459 AioContext
*aio_context
;
2460 bool has_attached_device
;
2462 device
= has_device
? device
: NULL
;
2463 id
= has_id
? id
: NULL
;
2465 blk
= qmp_get_blk(device
, id
, errp
);
2470 /* For BBs without a device, we can exchange the BDS tree at will */
2471 has_attached_device
= blk_get_attached_dev(blk
);
2473 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2474 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2478 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2479 !blk_dev_is_tray_open(blk
))
2481 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2490 aio_context
= bdrv_get_aio_context(bs
);
2491 aio_context_acquire(aio_context
);
2493 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2499 if (!blk_dev_has_tray(blk
)) {
2500 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2501 * called at all); therefore, the medium needs to be ejected here.
2502 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2503 * value passed here (i.e. false). */
2504 blk_dev_change_media_cb(blk
, false, &error_abort
);
2508 aio_context_release(aio_context
);
2511 void qmp_blockdev_remove_medium(const char *id
, Error
**errp
)
2513 blockdev_remove_medium(false, NULL
, true, id
, errp
);
2516 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2517 BlockDriverState
*bs
, Error
**errp
)
2519 Error
*local_err
= NULL
;
2523 /* For BBs without a device, we can exchange the BDS tree at will */
2524 has_device
= blk_get_attached_dev(blk
);
2526 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2527 error_setg(errp
, "Device is not removable");
2531 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2532 error_setg(errp
, "Tray of the device is not open");
2537 error_setg(errp
, "There already is a medium in the device");
2541 ret
= blk_insert_bs(blk
, bs
, errp
);
2546 if (!blk_dev_has_tray(blk
)) {
2547 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2548 * called at all); therefore, the medium needs to be pushed into the
2550 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2551 * value passed here (i.e. true). */
2552 blk_dev_change_media_cb(blk
, true, &local_err
);
2554 error_propagate(errp
, local_err
);
2561 static void blockdev_insert_medium(bool has_device
, const char *device
,
2562 bool has_id
, const char *id
,
2563 const char *node_name
, Error
**errp
)
2566 BlockDriverState
*bs
;
2568 blk
= qmp_get_blk(has_device
? device
: NULL
,
2575 bs
= bdrv_find_node(node_name
);
2577 error_setg(errp
, "Node '%s' not found", node_name
);
2581 if (bdrv_has_blk(bs
)) {
2582 error_setg(errp
, "Node '%s' is already in use", node_name
);
2586 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2589 void qmp_blockdev_insert_medium(const char *id
, const char *node_name
,
2592 blockdev_insert_medium(false, NULL
, true, id
, node_name
, errp
);
2595 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2596 bool has_id
, const char *id
,
2597 const char *filename
,
2598 bool has_format
, const char *format
,
2600 BlockdevChangeReadOnlyMode read_only
,
2604 BlockDriverState
*medium_bs
= NULL
;
2608 QDict
*options
= NULL
;
2611 blk
= qmp_get_blk(has_device
? device
: NULL
,
2619 blk_update_root_state(blk
);
2622 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2623 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2626 if (!has_read_only
) {
2627 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2630 switch (read_only
) {
2631 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2634 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2635 bdrv_flags
&= ~BDRV_O_RDWR
;
2638 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2639 bdrv_flags
|= BDRV_O_RDWR
;
2646 options
= qdict_new();
2647 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2648 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2651 qdict_put_str(options
, "driver", format
);
2654 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2659 rc
= do_open_tray(has_device
? device
: NULL
,
2662 if (rc
&& rc
!= -ENOSYS
) {
2663 error_propagate(errp
, err
);
2669 blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2671 error_propagate(errp
, err
);
2675 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2677 error_propagate(errp
, err
);
2681 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2684 /* If the medium has been inserted, the device has its own reference, so
2685 * ours must be relinquished; and if it has not been inserted successfully,
2686 * the reference must be relinquished anyway */
2687 bdrv_unref(medium_bs
);
2690 /* throttling disk I/O limits */
2691 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2694 BlockDriverState
*bs
;
2696 AioContext
*aio_context
;
2698 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2699 arg
->has_id
? arg
->id
: NULL
,
2705 aio_context
= blk_get_aio_context(blk
);
2706 aio_context_acquire(aio_context
);
2710 error_setg(errp
, "Device has no medium");
2714 throttle_config_init(&cfg
);
2715 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2716 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2717 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2719 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2720 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2721 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2723 if (arg
->has_bps_max
) {
2724 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2726 if (arg
->has_bps_rd_max
) {
2727 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2729 if (arg
->has_bps_wr_max
) {
2730 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2732 if (arg
->has_iops_max
) {
2733 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2735 if (arg
->has_iops_rd_max
) {
2736 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2738 if (arg
->has_iops_wr_max
) {
2739 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2742 if (arg
->has_bps_max_length
) {
2743 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2745 if (arg
->has_bps_rd_max_length
) {
2746 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2748 if (arg
->has_bps_wr_max_length
) {
2749 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2751 if (arg
->has_iops_max_length
) {
2752 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2754 if (arg
->has_iops_rd_max_length
) {
2755 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2757 if (arg
->has_iops_wr_max_length
) {
2758 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2761 if (arg
->has_iops_size
) {
2762 cfg
.op_size
= arg
->iops_size
;
2765 if (!throttle_is_valid(&cfg
, errp
)) {
2769 if (throttle_enabled(&cfg
)) {
2770 /* Enable I/O limits if they're not enabled yet, otherwise
2771 * just update the throttling group. */
2772 if (!blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2773 blk_io_limits_enable(blk
,
2774 arg
->has_group
? arg
->group
:
2775 arg
->has_device
? arg
->device
:
2777 } else if (arg
->has_group
) {
2778 blk_io_limits_update_group(blk
, arg
->group
);
2780 /* Set the new throttling configuration */
2781 blk_set_io_limits(blk
, &cfg
);
2782 } else if (blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2783 /* If all throttling settings are set to 0, disable I/O limits */
2784 blk_io_limits_disable(blk
);
2788 aio_context_release(aio_context
);
2791 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2792 bool has_granularity
, uint32_t granularity
,
2793 bool has_persistent
, bool persistent
,
2794 bool has_autoload
, bool autoload
,
2797 BlockDriverState
*bs
;
2798 BdrvDirtyBitmap
*bitmap
;
2800 if (!name
|| name
[0] == '\0') {
2801 error_setg(errp
, "Bitmap name cannot be empty");
2805 bs
= bdrv_lookup_bs(node
, node
, errp
);
2810 if (has_granularity
) {
2811 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2812 error_setg(errp
, "Granularity must be power of 2 "
2813 "and at least 512");
2817 /* Default to cluster size, if available: */
2818 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2821 if (!has_persistent
) {
2824 if (!has_autoload
) {
2828 if (has_autoload
&& !persistent
) {
2829 error_setg(errp
, "Autoload flag must be used only for persistent "
2835 !bdrv_can_store_new_dirty_bitmap(bs
, name
, granularity
, errp
))
2840 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2841 if (bitmap
== NULL
) {
2845 bdrv_dirty_bitmap_set_persistance(bitmap
, persistent
);
2846 bdrv_dirty_bitmap_set_autoload(bitmap
, autoload
);
2849 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2852 BlockDriverState
*bs
;
2853 BdrvDirtyBitmap
*bitmap
;
2854 Error
*local_err
= NULL
;
2856 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2857 if (!bitmap
|| !bs
) {
2861 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2863 "Bitmap '%s' is currently frozen and cannot be removed",
2868 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
2869 bdrv_remove_persistent_dirty_bitmap(bs
, name
, &local_err
);
2870 if (local_err
!= NULL
) {
2871 error_propagate(errp
, local_err
);
2876 bdrv_dirty_bitmap_make_anon(bitmap
);
2877 bdrv_release_dirty_bitmap(bs
, bitmap
);
2881 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2882 * immediately after a full backup operation.
2884 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2887 BdrvDirtyBitmap
*bitmap
;
2888 BlockDriverState
*bs
;
2890 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2891 if (!bitmap
|| !bs
) {
2895 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2897 "Bitmap '%s' is currently frozen and cannot be modified",
2900 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2902 "Bitmap '%s' is currently disabled and cannot be cleared",
2905 } else if (bdrv_dirty_bitmap_readonly(bitmap
)) {
2906 error_setg(errp
, "Bitmap '%s' is readonly and cannot be cleared", name
);
2910 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2913 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2917 BdrvDirtyBitmap
*bitmap
;
2918 BlockDriverState
*bs
;
2919 BlockDirtyBitmapSha256
*ret
= NULL
;
2922 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2923 if (!bitmap
|| !bs
) {
2927 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2928 if (sha256
== NULL
) {
2932 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2933 ret
->sha256
= sha256
;
2938 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2940 const char *id
= qdict_get_str(qdict
, "id");
2942 BlockDriverState
*bs
;
2943 AioContext
*aio_context
;
2944 Error
*local_err
= NULL
;
2946 bs
= bdrv_find_node(id
);
2948 qmp_blockdev_del(id
, &local_err
);
2950 error_report_err(local_err
);
2955 blk
= blk_by_name(id
);
2957 error_report("Device '%s' not found", id
);
2961 if (!blk_legacy_dinfo(blk
)) {
2962 error_report("Deleting device added with blockdev-add"
2963 " is not supported");
2967 aio_context
= blk_get_aio_context(blk
);
2968 aio_context_acquire(aio_context
);
2972 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2973 error_report_err(local_err
);
2974 aio_context_release(aio_context
);
2981 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2982 monitor_remove_blk(blk
);
2984 /* If this BlockBackend has a device attached to it, its refcount will be
2985 * decremented when the device is removed; otherwise we have to do so here.
2987 if (blk_get_attached_dev(blk
)) {
2988 /* Further I/O must not pause the guest */
2989 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2990 BLOCKDEV_ON_ERROR_REPORT
);
2995 aio_context_release(aio_context
);
2998 void qmp_block_resize(bool has_device
, const char *device
,
2999 bool has_node_name
, const char *node_name
,
3000 int64_t size
, Error
**errp
)
3002 Error
*local_err
= NULL
;
3003 BlockBackend
*blk
= NULL
;
3004 BlockDriverState
*bs
;
3005 AioContext
*aio_context
;
3008 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
3009 has_node_name
? node_name
: NULL
,
3012 error_propagate(errp
, local_err
);
3016 aio_context
= bdrv_get_aio_context(bs
);
3017 aio_context_acquire(aio_context
);
3019 if (!bdrv_is_first_non_filter(bs
)) {
3020 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
3025 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
3029 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
3030 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
3034 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3035 ret
= blk_insert_bs(blk
, bs
, errp
);
3040 bdrv_drained_begin(bs
);
3041 ret
= blk_truncate(blk
, size
, PREALLOC_MODE_OFF
, errp
);
3042 bdrv_drained_end(bs
);
3046 aio_context_release(aio_context
);
3049 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3050 bool has_base
, const char *base
,
3051 bool has_base_node
, const char *base_node
,
3052 bool has_backing_file
, const char *backing_file
,
3053 bool has_speed
, int64_t speed
,
3054 bool has_on_error
, BlockdevOnError on_error
,
3057 BlockDriverState
*bs
, *iter
;
3058 BlockDriverState
*base_bs
= NULL
;
3059 AioContext
*aio_context
;
3060 Error
*local_err
= NULL
;
3061 const char *base_name
= NULL
;
3063 if (!has_on_error
) {
3064 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3067 bs
= bdrv_lookup_bs(device
, device
, errp
);
3072 aio_context
= bdrv_get_aio_context(bs
);
3073 aio_context_acquire(aio_context
);
3075 if (has_base
&& has_base_node
) {
3076 error_setg(errp
, "'base' and 'base-node' cannot be specified "
3077 "at the same time");
3082 base_bs
= bdrv_find_backing_image(bs
, base
);
3083 if (base_bs
== NULL
) {
3084 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3087 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3091 if (has_base_node
) {
3092 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
3096 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
3097 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
3101 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3102 base_name
= base_bs
->filename
;
3105 /* Check for op blockers in the whole chain between bs and base */
3106 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
3107 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3112 /* if we are streaming the entire chain, the result will have no backing
3113 * file, and specifying one is therefore an error */
3114 if (base_bs
== NULL
&& has_backing_file
) {
3115 error_setg(errp
, "backing file specified, but streaming the "
3120 /* backing_file string overrides base bs filename */
3121 base_name
= has_backing_file
? backing_file
: base_name
;
3123 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3124 has_speed
? speed
: 0, on_error
, &local_err
);
3126 error_propagate(errp
, local_err
);
3130 trace_qmp_block_stream(bs
, bs
->job
);
3133 aio_context_release(aio_context
);
3136 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3137 bool has_base
, const char *base
,
3138 bool has_top
, const char *top
,
3139 bool has_backing_file
, const char *backing_file
,
3140 bool has_speed
, int64_t speed
,
3141 bool has_filter_node_name
, const char *filter_node_name
,
3144 BlockDriverState
*bs
;
3145 BlockDriverState
*iter
;
3146 BlockDriverState
*base_bs
, *top_bs
;
3147 AioContext
*aio_context
;
3148 Error
*local_err
= NULL
;
3149 /* This will be part of the QMP command, if/when the
3150 * BlockdevOnError change for blkmirror makes it in
3152 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3157 if (!has_filter_node_name
) {
3158 filter_node_name
= NULL
;
3162 * libvirt relies on the DeviceNotFound error class in order to probe for
3163 * live commit feature versions; for this to work, we must make sure to
3164 * perform the device lookup before any generic errors that may occur in a
3165 * scenario in which all optional arguments are omitted. */
3166 bs
= qmp_get_root_bs(device
, &local_err
);
3168 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3170 error_free(local_err
);
3171 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3172 "Device '%s' not found", device
);
3174 error_propagate(errp
, local_err
);
3179 aio_context
= bdrv_get_aio_context(bs
);
3180 aio_context_acquire(aio_context
);
3182 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3186 /* default top_bs is the active layer */
3189 if (has_top
&& top
) {
3190 if (strcmp(bs
->filename
, top
) != 0) {
3191 top_bs
= bdrv_find_backing_image(bs
, top
);
3195 if (top_bs
== NULL
) {
3196 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3200 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3202 if (has_base
&& base
) {
3203 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3205 base_bs
= bdrv_find_base(top_bs
);
3208 if (base_bs
== NULL
) {
3209 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3213 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3215 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3216 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3221 /* Do not allow attempts to commit an image into itself */
3222 if (top_bs
== base_bs
) {
3223 error_setg(errp
, "cannot commit an image into itself");
3228 if (has_backing_file
) {
3229 error_setg(errp
, "'backing-file' specified,"
3230 " but 'top' is the active layer");
3233 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3234 BLOCK_JOB_DEFAULT
, speed
, on_error
,
3235 filter_node_name
, NULL
, NULL
, false, &local_err
);
3237 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3238 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3241 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3242 on_error
, has_backing_file
? backing_file
: NULL
,
3243 filter_node_name
, &local_err
);
3245 if (local_err
!= NULL
) {
3246 error_propagate(errp
, local_err
);
3251 aio_context_release(aio_context
);
3254 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
3257 BlockDriverState
*bs
;
3258 BlockDriverState
*target_bs
;
3259 BlockDriverState
*source
= NULL
;
3260 BlockJob
*job
= NULL
;
3261 BdrvDirtyBitmap
*bmap
= NULL
;
3262 AioContext
*aio_context
;
3263 QDict
*options
= NULL
;
3264 Error
*local_err
= NULL
;
3267 bool set_backing_hd
= false;
3269 if (!backup
->has_speed
) {
3272 if (!backup
->has_on_source_error
) {
3273 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3275 if (!backup
->has_on_target_error
) {
3276 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3278 if (!backup
->has_mode
) {
3279 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3281 if (!backup
->has_job_id
) {
3282 backup
->job_id
= NULL
;
3284 if (!backup
->has_compress
) {
3285 backup
->compress
= false;
3288 bs
= qmp_get_root_bs(backup
->device
, errp
);
3293 aio_context
= bdrv_get_aio_context(bs
);
3294 aio_context_acquire(aio_context
);
3296 if (!backup
->has_format
) {
3297 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3298 NULL
: (char*) bs
->drv
->format_name
;
3301 /* Early check to avoid creating target */
3302 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3306 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3308 /* See if we have a backing HD we can use to create our new image
3310 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3311 source
= backing_bs(bs
);
3313 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3316 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3318 flags
|= BDRV_O_NO_BACKING
;
3319 set_backing_hd
= true;
3322 size
= bdrv_getlength(bs
);
3324 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3328 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3329 assert(backup
->format
);
3331 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3332 source
->drv
->format_name
, NULL
,
3333 size
, flags
, false, &local_err
);
3335 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3336 size
, flags
, false, &local_err
);
3341 error_propagate(errp
, local_err
);
3345 if (backup
->format
) {
3347 options
= qdict_new();
3349 qdict_put_str(options
, "driver", backup
->format
);
3352 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3357 bdrv_set_aio_context(target_bs
, aio_context
);
3359 if (set_backing_hd
) {
3360 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3362 bdrv_unref(target_bs
);
3367 if (backup
->has_bitmap
) {
3368 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3370 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3371 bdrv_unref(target_bs
);
3376 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3377 backup
->sync
, bmap
, backup
->compress
,
3378 backup
->on_source_error
, backup
->on_target_error
,
3379 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3380 bdrv_unref(target_bs
);
3381 if (local_err
!= NULL
) {
3382 error_propagate(errp
, local_err
);
3387 aio_context_release(aio_context
);
3391 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3395 job
= do_drive_backup(arg
, NULL
, errp
);
3397 block_job_start(job
);
3401 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3403 return bdrv_named_nodes_list(errp
);
3406 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
3409 BlockDriverState
*bs
;
3410 BlockDriverState
*target_bs
;
3411 Error
*local_err
= NULL
;
3412 AioContext
*aio_context
;
3413 BlockJob
*job
= NULL
;
3415 if (!backup
->has_speed
) {
3418 if (!backup
->has_on_source_error
) {
3419 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3421 if (!backup
->has_on_target_error
) {
3422 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3424 if (!backup
->has_job_id
) {
3425 backup
->job_id
= NULL
;
3427 if (!backup
->has_compress
) {
3428 backup
->compress
= false;
3431 bs
= qmp_get_root_bs(backup
->device
, errp
);
3436 aio_context
= bdrv_get_aio_context(bs
);
3437 aio_context_acquire(aio_context
);
3439 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3444 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3445 if (!bdrv_has_blk(target_bs
)) {
3446 /* The target BDS is not attached, we can safely move it to another
3448 bdrv_set_aio_context(target_bs
, aio_context
);
3450 error_setg(errp
, "Target is attached to a different thread from "
3455 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3456 backup
->sync
, NULL
, backup
->compress
,
3457 backup
->on_source_error
, backup
->on_target_error
,
3458 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3459 if (local_err
!= NULL
) {
3460 error_propagate(errp
, local_err
);
3463 aio_context_release(aio_context
);
3467 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3470 job
= do_blockdev_backup(arg
, NULL
, errp
);
3472 block_job_start(job
);
3476 /* Parameter check and block job starting for drive mirroring.
3477 * Caller should hold @device and @target's aio context (must be the same).
3479 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3480 BlockDriverState
*target
,
3481 bool has_replaces
, const char *replaces
,
3482 enum MirrorSyncMode sync
,
3483 BlockMirrorBackingMode backing_mode
,
3484 bool has_speed
, int64_t speed
,
3485 bool has_granularity
, uint32_t granularity
,
3486 bool has_buf_size
, int64_t buf_size
,
3487 bool has_on_source_error
,
3488 BlockdevOnError on_source_error
,
3489 bool has_on_target_error
,
3490 BlockdevOnError on_target_error
,
3491 bool has_unmap
, bool unmap
,
3492 bool has_filter_node_name
,
3493 const char *filter_node_name
,
3500 if (!has_on_source_error
) {
3501 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3503 if (!has_on_target_error
) {
3504 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3506 if (!has_granularity
) {
3509 if (!has_buf_size
) {
3515 if (!has_filter_node_name
) {
3516 filter_node_name
= NULL
;
3519 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3520 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3521 "a value in range [512B, 64MB]");
3524 if (granularity
& (granularity
- 1)) {
3525 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3530 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3533 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3537 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3538 sync
= MIRROR_SYNC_MODE_FULL
;
3541 /* pass the node name to replace to mirror start since it's loose coupling
3542 * and will allow to check whether the node still exist at mirror completion
3544 mirror_start(job_id
, bs
, target
,
3545 has_replaces
? replaces
: NULL
,
3546 speed
, granularity
, buf_size
, sync
, backing_mode
,
3547 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3551 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3553 BlockDriverState
*bs
;
3554 BlockDriverState
*source
, *target_bs
;
3555 AioContext
*aio_context
;
3556 BlockMirrorBackingMode backing_mode
;
3557 Error
*local_err
= NULL
;
3558 QDict
*options
= NULL
;
3561 const char *format
= arg
->format
;
3563 bs
= qmp_get_root_bs(arg
->device
, errp
);
3568 aio_context
= bdrv_get_aio_context(bs
);
3569 aio_context_acquire(aio_context
);
3571 if (!arg
->has_mode
) {
3572 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3575 if (!arg
->has_format
) {
3576 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3577 ? NULL
: bs
->drv
->format_name
);
3580 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3581 source
= backing_bs(bs
);
3582 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3583 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3585 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3589 size
= bdrv_getlength(bs
);
3591 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3595 if (arg
->has_replaces
) {
3596 BlockDriverState
*to_replace_bs
;
3597 AioContext
*replace_aio_context
;
3598 int64_t replace_size
;
3600 if (!arg
->has_node_name
) {
3601 error_setg(errp
, "a node-name must be provided when replacing a"
3602 " named node of the graph");
3606 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3608 if (!to_replace_bs
) {
3609 error_propagate(errp
, local_err
);
3613 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3614 aio_context_acquire(replace_aio_context
);
3615 replace_size
= bdrv_getlength(to_replace_bs
);
3616 aio_context_release(replace_aio_context
);
3618 if (size
!= replace_size
) {
3619 error_setg(errp
, "cannot replace image with a mirror image of "
3625 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3626 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3628 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3631 /* Don't open backing image in create() */
3632 flags
|= BDRV_O_NO_BACKING
;
3634 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3635 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3637 /* create new image w/o backing file */
3639 bdrv_img_create(arg
->target
, format
,
3640 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3642 switch (arg
->mode
) {
3643 case NEW_IMAGE_MODE_EXISTING
:
3645 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3646 /* create new image with backing file */
3647 bdrv_img_create(arg
->target
, format
,
3649 source
->drv
->format_name
,
3650 NULL
, size
, flags
, false, &local_err
);
3658 error_propagate(errp
, local_err
);
3662 options
= qdict_new();
3663 if (arg
->has_node_name
) {
3664 qdict_put_str(options
, "node-name", arg
->node_name
);
3667 qdict_put_str(options
, "driver", format
);
3670 /* Mirroring takes care of copy-on-write using the source's backing
3673 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3678 bdrv_set_aio_context(target_bs
, aio_context
);
3680 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3681 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3682 backing_mode
, arg
->has_speed
, arg
->speed
,
3683 arg
->has_granularity
, arg
->granularity
,
3684 arg
->has_buf_size
, arg
->buf_size
,
3685 arg
->has_on_source_error
, arg
->on_source_error
,
3686 arg
->has_on_target_error
, arg
->on_target_error
,
3687 arg
->has_unmap
, arg
->unmap
,
3690 bdrv_unref(target_bs
);
3691 error_propagate(errp
, local_err
);
3693 aio_context_release(aio_context
);
3696 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3697 const char *device
, const char *target
,
3698 bool has_replaces
, const char *replaces
,
3699 MirrorSyncMode sync
,
3700 bool has_speed
, int64_t speed
,
3701 bool has_granularity
, uint32_t granularity
,
3702 bool has_buf_size
, int64_t buf_size
,
3703 bool has_on_source_error
,
3704 BlockdevOnError on_source_error
,
3705 bool has_on_target_error
,
3706 BlockdevOnError on_target_error
,
3707 bool has_filter_node_name
,
3708 const char *filter_node_name
,
3711 BlockDriverState
*bs
;
3712 BlockDriverState
*target_bs
;
3713 AioContext
*aio_context
;
3714 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3715 Error
*local_err
= NULL
;
3717 bs
= qmp_get_root_bs(device
, errp
);
3722 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3727 aio_context
= bdrv_get_aio_context(bs
);
3728 aio_context_acquire(aio_context
);
3730 bdrv_set_aio_context(target_bs
, aio_context
);
3732 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3733 has_replaces
, replaces
, sync
, backing_mode
,
3735 has_granularity
, granularity
,
3736 has_buf_size
, buf_size
,
3737 has_on_source_error
, on_source_error
,
3738 has_on_target_error
, on_target_error
,
3740 has_filter_node_name
, filter_node_name
,
3742 error_propagate(errp
, local_err
);
3744 aio_context_release(aio_context
);
3747 /* Get a block job using its ID and acquire its AioContext */
3748 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3755 *aio_context
= NULL
;
3757 job
= block_job_get(id
);
3760 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3761 "Block job '%s' not found", id
);
3765 *aio_context
= blk_get_aio_context(job
->blk
);
3766 aio_context_acquire(*aio_context
);
3771 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3773 AioContext
*aio_context
;
3774 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3780 block_job_set_speed(job
, speed
, errp
);
3781 aio_context_release(aio_context
);
3784 void qmp_block_job_cancel(const char *device
,
3785 bool has_force
, bool force
, Error
**errp
)
3787 AioContext
*aio_context
;
3788 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3798 if (block_job_user_paused(job
) && !force
) {
3799 error_setg(errp
, "The block job for device '%s' is currently paused",
3804 trace_qmp_block_job_cancel(job
);
3805 block_job_cancel(job
);
3807 aio_context_release(aio_context
);
3810 void qmp_block_job_pause(const char *device
, Error
**errp
)
3812 AioContext
*aio_context
;
3813 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3815 if (!job
|| block_job_user_paused(job
)) {
3819 trace_qmp_block_job_pause(job
);
3820 block_job_user_pause(job
);
3821 aio_context_release(aio_context
);
3824 void qmp_block_job_resume(const char *device
, Error
**errp
)
3826 AioContext
*aio_context
;
3827 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3829 if (!job
|| !block_job_user_paused(job
)) {
3833 trace_qmp_block_job_resume(job
);
3834 block_job_user_resume(job
);
3835 aio_context_release(aio_context
);
3838 void qmp_block_job_complete(const char *device
, Error
**errp
)
3840 AioContext
*aio_context
;
3841 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3847 trace_qmp_block_job_complete(job
);
3848 block_job_complete(job
, errp
);
3849 aio_context_release(aio_context
);
3852 void qmp_change_backing_file(const char *device
,
3853 const char *image_node_name
,
3854 const char *backing_file
,
3857 BlockDriverState
*bs
= NULL
;
3858 AioContext
*aio_context
;
3859 BlockDriverState
*image_bs
= NULL
;
3860 Error
*local_err
= NULL
;
3865 bs
= qmp_get_root_bs(device
, errp
);
3870 aio_context
= bdrv_get_aio_context(bs
);
3871 aio_context_acquire(aio_context
);
3873 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3875 error_propagate(errp
, local_err
);
3880 error_setg(errp
, "image file not found");
3884 if (bdrv_find_base(image_bs
) == image_bs
) {
3885 error_setg(errp
, "not allowing backing file change on an image "
3886 "without a backing file");
3890 /* even though we are not necessarily operating on bs, we need it to
3891 * determine if block ops are currently prohibited on the chain */
3892 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3896 /* final sanity check */
3897 if (!bdrv_chain_contains(bs
, image_bs
)) {
3898 error_setg(errp
, "'%s' and image file are not in the same chain",
3903 /* if not r/w, reopen to make r/w */
3904 open_flags
= image_bs
->open_flags
;
3905 ro
= bdrv_is_read_only(image_bs
);
3908 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3910 error_propagate(errp
, local_err
);
3915 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3916 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3919 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3921 /* don't exit here, so we can try to restore open flags if
3926 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3927 error_propagate(errp
, local_err
);
3931 aio_context_release(aio_context
);
3934 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3938 Error
*local_err
= NULL
;
3940 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3945 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3947 if (!qdict_get_try_str(qdict
, "node-name")) {
3949 error_report("'node-name' needs to be specified");
3953 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3955 error_report_err(local_err
);
3959 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3962 qemu_opts_del(opts
);
3965 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3967 BlockDriverState
*bs
;
3969 Visitor
*v
= qobject_output_visitor_new(&obj
);
3971 const QDictEntry
*ent
;
3972 Error
*local_err
= NULL
;
3974 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3976 error_propagate(errp
, local_err
);
3980 visit_complete(v
, &obj
);
3981 qdict
= qobject_to_qdict(obj
);
3983 qdict_flatten(qdict
);
3986 * Rewrite "backing": null to "backing": ""
3987 * TODO Rewrite "" to null instead, and perhaps not even here
3989 for (ent
= qdict_first(qdict
); ent
; ent
= qdict_next(qdict
, ent
)) {
3990 char *dot
= strrchr(ent
->key
, '.');
3992 if (!strcmp(dot
? dot
+ 1 : ent
->key
, "backing")
3993 && qobject_type(ent
->value
) == QTYPE_QNULL
) {
3994 qdict_put(qdict
, ent
->key
, qstring_new());
3998 if (!qdict_get_try_str(qdict
, "node-name")) {
3999 error_setg(errp
, "'node-name' must be specified for the root node");
4003 bs
= bds_tree_init(qdict
, errp
);
4008 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4014 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
4016 AioContext
*aio_context
;
4017 BlockDriverState
*bs
;
4019 bs
= bdrv_find_node(node_name
);
4021 error_setg(errp
, "Cannot find node %s", node_name
);
4024 if (bdrv_has_blk(bs
)) {
4025 error_setg(errp
, "Node %s is in use", node_name
);
4028 aio_context
= bdrv_get_aio_context(bs
);
4029 aio_context_acquire(aio_context
);
4031 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4035 if (!bs
->monitor_list
.tqe_prev
) {
4036 error_setg(errp
, "Node %s is not owned by the monitor",
4041 if (bs
->refcnt
> 1) {
4042 error_setg(errp
, "Block device %s is in use",
4043 bdrv_get_device_or_node_name(bs
));
4047 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4051 aio_context_release(aio_context
);
4054 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4055 const char *child_name
)
4059 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4060 if (strcmp(child
->name
, child_name
) == 0) {
4068 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4069 const char *child
, bool has_node
,
4070 const char *node
, Error
**errp
)
4072 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4075 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4080 if (has_child
== has_node
) {
4082 error_setg(errp
, "The parameters child and node are in conflict");
4084 error_setg(errp
, "Either child or node must be specified");
4090 p_child
= bdrv_find_child(parent_bs
, child
);
4092 error_setg(errp
, "Node '%s' does not have child '%s'",
4096 bdrv_del_child(parent_bs
, p_child
, errp
);
4100 new_bs
= bdrv_find_node(node
);
4102 error_setg(errp
, "Node '%s' not found", node
);
4105 bdrv_add_child(parent_bs
, new_bs
, errp
);
4109 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4111 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4114 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4115 BlockJobInfoList
*elem
;
4116 AioContext
*aio_context
;
4118 if (block_job_is_internal(job
)) {
4121 elem
= g_new0(BlockJobInfoList
, 1);
4122 aio_context
= blk_get_aio_context(job
->blk
);
4123 aio_context_acquire(aio_context
);
4124 elem
->value
= block_job_query(job
, errp
);
4125 aio_context_release(aio_context
);
4128 qapi_free_BlockJobInfoList(head
);
4132 p_next
= &elem
->next
;
4138 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
4139 bool has_force
, bool force
, Error
**errp
)
4141 AioContext
*old_context
;
4142 AioContext
*new_context
;
4143 BlockDriverState
*bs
;
4145 bs
= bdrv_find_node(node_name
);
4147 error_setg(errp
, "Cannot find node %s", node_name
);
4151 /* Protects against accidents. */
4152 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
4153 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
4154 "be in use (use force=true to override this check)",
4159 if (iothread
->type
== QTYPE_QSTRING
) {
4160 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
4162 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
4166 new_context
= iothread_get_aio_context(obj
);
4168 new_context
= qemu_get_aio_context();
4171 old_context
= bdrv_get_aio_context(bs
);
4172 aio_context_acquire(old_context
);
4174 bdrv_set_aio_context(bs
, new_context
);
4176 aio_context_release(old_context
);
4179 QemuOptsList qemu_common_drive_opts
= {
4181 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4185 .type
= QEMU_OPT_BOOL
,
4186 .help
= "enable/disable snapshot mode",
4189 .type
= QEMU_OPT_STRING
,
4190 .help
= "host AIO implementation (threads, native)",
4192 .name
= BDRV_OPT_CACHE_WB
,
4193 .type
= QEMU_OPT_BOOL
,
4194 .help
= "Enable writeback mode",
4197 .type
= QEMU_OPT_STRING
,
4198 .help
= "disk format (raw, qcow2, ...)",
4201 .type
= QEMU_OPT_STRING
,
4202 .help
= "read error action",
4205 .type
= QEMU_OPT_STRING
,
4206 .help
= "write error action",
4208 .name
= BDRV_OPT_READ_ONLY
,
4209 .type
= QEMU_OPT_BOOL
,
4210 .help
= "open drive file as read-only",
4216 .name
= "throttling.group",
4217 .type
= QEMU_OPT_STRING
,
4218 .help
= "name of the block throttling group",
4220 .name
= "copy-on-read",
4221 .type
= QEMU_OPT_BOOL
,
4222 .help
= "copy read data from backing file into image file",
4224 .name
= "detect-zeroes",
4225 .type
= QEMU_OPT_STRING
,
4226 .help
= "try to optimize zero writes (off, on, unmap)",
4228 .name
= "stats-account-invalid",
4229 .type
= QEMU_OPT_BOOL
,
4230 .help
= "whether to account for invalid I/O operations "
4231 "in the statistics",
4233 .name
= "stats-account-failed",
4234 .type
= QEMU_OPT_BOOL
,
4235 .help
= "whether to account for failed I/O operations "
4236 "in the statistics",
4238 { /* end of list */ }
4242 QemuOptsList qemu_drive_opts
= {
4244 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4247 * no elements => accept any params
4248 * validation will happen later
4250 { /* end of list */ }