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/qapi-commands-block.h"
44 #include "qapi/qapi-commands-transaction.h"
45 #include "qapi/qapi-visit-block-core.h"
46 #include "qapi/qmp/qdict.h"
47 #include "qapi/qmp/qnum.h"
48 #include "qapi/qmp/qstring.h"
49 #include "qapi/error.h"
50 #include "qapi/qmp/qerror.h"
51 #include "qapi/qmp/qlist.h"
52 #include "qapi/qobject-output-visitor.h"
53 #include "sysemu/sysemu.h"
54 #include "sysemu/iothread.h"
55 #include "block/block_int.h"
56 #include "block/trace.h"
57 #include "sysemu/arch_init.h"
58 #include "sysemu/qtest.h"
59 #include "qemu/cutils.h"
60 #include "qemu/help_option.h"
61 #include "qemu/throttle-options.h"
63 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
64 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
66 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
67 bool force
, Error
**errp
);
68 static void blockdev_remove_medium(bool has_device
, const char *device
,
69 bool has_id
, const char *id
, Error
**errp
);
70 static void blockdev_insert_medium(bool has_device
, const char *device
,
71 bool has_id
, const char *id
,
72 const char *node_name
, Error
**errp
);
74 static const char *const if_name
[IF_COUNT
] = {
78 [IF_FLOPPY
] = "floppy",
79 [IF_PFLASH
] = "pflash",
82 [IF_VIRTIO
] = "virtio",
86 static int if_max_devs
[IF_COUNT
] = {
88 * Do not change these numbers! They govern how drive option
89 * index maps to unit and bus. That mapping is ABI.
91 * All controllers used to implement if=T drives need to support
92 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
93 * Otherwise, some index values map to "impossible" bus, unit
96 * For instance, if you change [IF_SCSI] to 255, -drive
97 * if=scsi,index=12 no longer means bus=1,unit=5, but
98 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
99 * the drive can't be set up. Regression.
106 * Boards may call this to offer board-by-board overrides
107 * of the default, global values.
109 void override_max_devs(BlockInterfaceType type
, int max_devs
)
118 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
119 dinfo
= blk_legacy_dinfo(blk
);
120 if (dinfo
->type
== type
) {
121 fprintf(stderr
, "Cannot override units-per-bus property of"
122 " the %s interface, because a drive of that type has"
123 " already been added.\n", if_name
[type
]);
124 g_assert_not_reached();
128 if_max_devs
[type
] = max_devs
;
132 * We automatically delete the drive when a device using it gets
133 * unplugged. Questionable feature, but we can't just drop it.
134 * Device models call blockdev_mark_auto_del() to schedule the
135 * automatic deletion, and generic qdev code calls blockdev_auto_del()
136 * when deletion is actually safe.
138 void blockdev_mark_auto_del(BlockBackend
*blk
)
140 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
141 BlockDriverState
*bs
= blk_bs(blk
);
142 AioContext
*aio_context
;
149 aio_context
= bdrv_get_aio_context(bs
);
150 aio_context_acquire(aio_context
);
153 block_job_cancel(bs
->job
);
156 aio_context_release(aio_context
);
162 void blockdev_auto_del(BlockBackend
*blk
)
164 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
166 if (dinfo
&& dinfo
->auto_del
) {
167 monitor_remove_blk(blk
);
173 * Returns the current mapping of how many units per bus
174 * a particular interface can support.
176 * A positive integer indicates n units per bus.
177 * 0 implies the mapping has not been established.
178 * -1 indicates an invalid BlockInterfaceType was given.
180 int drive_get_max_devs(BlockInterfaceType type
)
182 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
183 return if_max_devs
[type
];
189 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
191 int max_devs
= if_max_devs
[type
];
192 return max_devs
? index
/ max_devs
: 0;
195 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
197 int max_devs
= if_max_devs
[type
];
198 return max_devs
? index
% max_devs
: index
;
201 QemuOpts
*drive_def(const char *optstr
)
203 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
206 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
211 opts
= drive_def(optstr
);
215 if (type
!= IF_DEFAULT
) {
216 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
219 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
222 qemu_opt_set(opts
, "file", file
, &error_abort
);
226 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
231 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
232 dinfo
= blk_legacy_dinfo(blk
);
233 if (dinfo
&& dinfo
->type
== type
234 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
242 void drive_check_orphaned(void)
247 bool orphans
= false;
249 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
250 dinfo
= blk_legacy_dinfo(blk
);
251 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
252 dinfo
->type
!= IF_NONE
) {
254 qemu_opts_loc_restore(dinfo
->opts
);
255 error_report("machine type does not support"
256 " if=%s,bus=%d,unit=%d",
257 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
268 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
270 return drive_get(type
,
271 drive_index_to_bus_id(type
, index
),
272 drive_index_to_unit_id(type
, index
));
275 int drive_get_max_bus(BlockInterfaceType type
)
282 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
283 dinfo
= blk_legacy_dinfo(blk
);
284 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
285 max_bus
= dinfo
->bus
;
291 /* Get a block device. This should only be used for single-drive devices
292 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
294 DriveInfo
*drive_get_next(BlockInterfaceType type
)
296 static int next_block_unit
[IF_COUNT
];
298 return drive_get(type
, 0, next_block_unit
[type
]++);
301 static void bdrv_format_print(void *opaque
, const char *name
)
303 error_printf(" %s", name
);
308 BlockDriverState
*bs
;
311 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
313 if (!strcmp(buf
, "ignore")) {
314 return BLOCKDEV_ON_ERROR_IGNORE
;
315 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
316 return BLOCKDEV_ON_ERROR_ENOSPC
;
317 } else if (!strcmp(buf
, "stop")) {
318 return BLOCKDEV_ON_ERROR_STOP
;
319 } else if (!strcmp(buf
, "report")) {
320 return BLOCKDEV_ON_ERROR_REPORT
;
322 error_setg(errp
, "'%s' invalid %s error action",
323 buf
, is_read
? "read" : "write");
328 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
331 const QListEntry
*entry
;
332 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
333 switch (qobject_type(entry
->value
)) {
335 case QTYPE_QSTRING
: {
336 unsigned long long length
;
337 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
338 if (parse_uint_full(str
, &length
, 10) == 0 &&
339 length
> 0 && length
<= UINT_MAX
) {
340 block_acct_add_interval(stats
, (unsigned) length
);
342 error_setg(errp
, "Invalid interval length: %s", str
);
349 int64_t length
= qnum_get_int(qobject_to_qnum(entry
->value
));
351 if (length
> 0 && length
<= UINT_MAX
) {
352 block_acct_add_interval(stats
, (unsigned) length
);
354 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
361 error_setg(errp
, "The specification of stats-intervals is invalid");
368 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
370 /* All parameters but @opts are optional and may be set to NULL. */
371 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
372 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
373 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
375 Error
*local_error
= NULL
;
379 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
380 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
383 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
384 if (!strcmp(aio
, "native")) {
385 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
386 } else if (!strcmp(aio
, "threads")) {
387 /* this is the default */
389 error_setg(errp
, "invalid aio option");
395 /* disk I/O throttling */
396 if (throttling_group
) {
397 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
401 throttle_config_init(throttle_cfg
);
402 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
403 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
404 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
405 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
406 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
407 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
408 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
409 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
410 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
411 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
412 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
413 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
415 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
416 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
417 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
418 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
419 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
420 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
421 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
422 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
424 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
425 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
426 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
428 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
429 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
430 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
431 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
432 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
433 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
434 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
435 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
436 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
437 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
438 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
439 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
441 throttle_cfg
->op_size
=
442 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
444 if (!throttle_is_valid(throttle_cfg
, errp
)) {
451 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup
,
452 qemu_opt_get(opts
, "detect-zeroes"),
453 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
456 error_propagate(errp
, local_error
);
462 /* Takes the ownership of bs_opts */
463 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
468 int on_read_error
, on_write_error
;
469 bool account_invalid
, account_failed
;
470 bool writethrough
, read_only
;
472 BlockDriverState
*bs
;
477 QDict
*interval_dict
= NULL
;
478 QList
*interval_list
= NULL
;
480 BlockdevDetectZeroesOptions detect_zeroes
=
481 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
482 const char *throttling_group
= NULL
;
484 /* Check common options by copying from bs_opts to opts, all other options
485 * stay in bs_opts for processing by bdrv_open(). */
486 id
= qdict_get_try_str(bs_opts
, "id");
487 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
489 error_propagate(errp
, error
);
493 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
495 error_propagate(errp
, error
);
500 qdict_del(bs_opts
, "id");
503 /* extract parameters */
504 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
506 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
507 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
509 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
511 id
= qemu_opts_id(opts
);
513 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
514 qdict_array_split(interval_dict
, &interval_list
);
516 if (qdict_size(interval_dict
) != 0) {
517 error_setg(errp
, "Invalid option stats-intervals.%s",
518 qdict_first(interval_dict
)->key
);
522 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
523 &detect_zeroes
, &error
);
525 error_propagate(errp
, error
);
529 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
530 if (is_help_option(buf
)) {
531 error_printf("Supported formats:");
532 bdrv_iterate_format(bdrv_format_print
, NULL
);
537 if (qdict_haskey(bs_opts
, "driver")) {
538 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
541 qdict_put_str(bs_opts
, "driver", buf
);
544 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
545 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
546 on_write_error
= parse_block_error_action(buf
, 0, &error
);
548 error_propagate(errp
, error
);
553 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
554 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
555 on_read_error
= parse_block_error_action(buf
, 1, &error
);
557 error_propagate(errp
, error
);
563 bdrv_flags
|= BDRV_O_SNAPSHOT
;
566 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
569 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
570 BlockBackendRootState
*blk_rs
;
572 blk
= blk_new(0, BLK_PERM_ALL
);
573 blk_rs
= blk_get_root_state(blk
);
574 blk_rs
->open_flags
= bdrv_flags
;
575 blk_rs
->read_only
= read_only
;
576 blk_rs
->detect_zeroes
= detect_zeroes
;
580 if (file
&& !*file
) {
584 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
585 * with other callers) rather than what we want as the real defaults.
586 * Apply the defaults here instead. */
587 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
588 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
589 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
590 read_only
? "on" : "off");
591 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
593 if (runstate_check(RUN_STATE_INMIGRATE
)) {
594 bdrv_flags
|= BDRV_O_INACTIVE
;
597 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
603 bs
->detect_zeroes
= detect_zeroes
;
605 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
607 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
614 /* disk I/O throttling */
615 if (throttle_enabled(&cfg
)) {
616 if (!throttling_group
) {
617 throttling_group
= id
;
619 blk_io_limits_enable(blk
, throttling_group
);
620 blk_set_io_limits(blk
, &cfg
);
623 blk_set_enable_write_cache(blk
, !writethrough
);
624 blk_set_on_error(blk
, on_read_error
, on_write_error
);
626 if (!monitor_add_blk(blk
, id
, errp
)) {
634 QDECREF(interval_dict
);
635 QDECREF(interval_list
);
640 QDECREF(interval_dict
);
641 QDECREF(interval_list
);
647 /* Takes the ownership of bs_opts */
648 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
652 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
653 * with other callers) rather than what we want as the real defaults.
654 * Apply the defaults here instead. */
655 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
656 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
657 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
659 if (runstate_check(RUN_STATE_INMIGRATE
)) {
660 bdrv_flags
|= BDRV_O_INACTIVE
;
663 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
666 void blockdev_close_all_bdrv_states(void)
668 BlockDriverState
*bs
, *next_bs
;
670 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
671 AioContext
*ctx
= bdrv_get_aio_context(bs
);
673 aio_context_acquire(ctx
);
675 aio_context_release(ctx
);
679 /* Iterates over the list of monitor-owned BlockDriverStates */
680 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
682 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
683 : QTAILQ_FIRST(&monitor_bdrv_states
);
686 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
691 value
= qemu_opt_get(opts
, from
);
693 if (qemu_opt_find(opts
, to
)) {
694 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
695 "same time", to
, from
);
700 /* rename all items in opts */
701 while ((value
= qemu_opt_get(opts
, from
))) {
702 qemu_opt_set(opts
, to
, value
, &error_abort
);
703 qemu_opt_unset(opts
, from
);
707 QemuOptsList qemu_legacy_drive_opts
= {
709 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
713 .type
= QEMU_OPT_NUMBER
,
714 .help
= "bus number",
717 .type
= QEMU_OPT_NUMBER
,
718 .help
= "unit number (i.e. lun for scsi)",
721 .type
= QEMU_OPT_NUMBER
,
722 .help
= "index number",
725 .type
= QEMU_OPT_STRING
,
726 .help
= "media type (disk, cdrom)",
729 .type
= QEMU_OPT_STRING
,
730 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
733 .type
= QEMU_OPT_NUMBER
,
734 .help
= "number of cylinders (ide disk geometry)",
737 .type
= QEMU_OPT_NUMBER
,
738 .help
= "number of heads (ide disk geometry)",
741 .type
= QEMU_OPT_NUMBER
,
742 .help
= "number of sectors (ide disk geometry)",
745 .type
= QEMU_OPT_STRING
,
746 .help
= "chs translation (auto, lba, none)",
749 .type
= QEMU_OPT_STRING
,
750 .help
= "pci address (virtio only)",
753 .type
= QEMU_OPT_STRING
,
754 .help
= "disk serial number",
757 .type
= QEMU_OPT_STRING
,
761 /* Options that are passed on, but have special semantics with -drive */
763 .name
= BDRV_OPT_READ_ONLY
,
764 .type
= QEMU_OPT_BOOL
,
765 .help
= "open drive file as read-only",
768 .type
= QEMU_OPT_STRING
,
769 .help
= "read error action",
772 .type
= QEMU_OPT_STRING
,
773 .help
= "write error action",
775 .name
= "copy-on-read",
776 .type
= QEMU_OPT_BOOL
,
777 .help
= "copy read data from backing file into image file",
780 { /* end of list */ }
784 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
788 DriveInfo
*dinfo
= NULL
;
790 QemuOpts
*legacy_opts
;
791 DriveMediaType media
= MEDIA_DISK
;
792 BlockInterfaceType type
;
793 int cyls
, heads
, secs
, translation
;
794 int max_devs
, bus_id
, unit_id
, index
;
796 const char *werror
, *rerror
;
797 bool read_only
= false;
800 const char *filename
;
801 Error
*local_err
= NULL
;
803 const char *deprecated
[] = {
804 "serial", "trans", "secs", "heads", "cyls", "addr"
807 /* Change legacy command line options into QMP ones */
808 static const struct {
812 { "iops", "throttling.iops-total" },
813 { "iops_rd", "throttling.iops-read" },
814 { "iops_wr", "throttling.iops-write" },
816 { "bps", "throttling.bps-total" },
817 { "bps_rd", "throttling.bps-read" },
818 { "bps_wr", "throttling.bps-write" },
820 { "iops_max", "throttling.iops-total-max" },
821 { "iops_rd_max", "throttling.iops-read-max" },
822 { "iops_wr_max", "throttling.iops-write-max" },
824 { "bps_max", "throttling.bps-total-max" },
825 { "bps_rd_max", "throttling.bps-read-max" },
826 { "bps_wr_max", "throttling.bps-write-max" },
828 { "iops_size", "throttling.iops-size" },
830 { "group", "throttling.group" },
832 { "readonly", BDRV_OPT_READ_ONLY
},
835 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
836 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
839 error_report_err(local_err
);
844 value
= qemu_opt_get(all_opts
, "cache");
849 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
850 error_report("invalid cache option");
854 /* Specific options take precedence */
855 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
856 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
857 !writethrough
, &error_abort
);
859 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
860 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
861 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
863 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
864 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
865 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
867 qemu_opt_unset(all_opts
, "cache");
870 /* Get a QDict for processing the options */
871 bs_opts
= qdict_new();
872 qemu_opts_to_qdict(all_opts
, bs_opts
);
874 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
876 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
878 error_report_err(local_err
);
882 /* Other deprecated options */
883 if (!qtest_enabled()) {
884 for (i
= 0; i
< ARRAY_SIZE(deprecated
); i
++) {
885 if (qemu_opt_get(legacy_opts
, deprecated
[i
]) != NULL
) {
886 error_report("'%s' is deprecated, please use the corresponding "
887 "option of '-device' instead", deprecated
[i
]);
893 value
= qemu_opt_get(legacy_opts
, "media");
895 if (!strcmp(value
, "disk")) {
897 } else if (!strcmp(value
, "cdrom")) {
901 error_report("'%s' invalid media", value
);
906 /* copy-on-read is disabled with a warning for read-only devices */
907 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
908 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
910 if (read_only
&& copy_on_read
) {
911 warn_report("disabling copy-on-read on read-only drive");
912 copy_on_read
= false;
915 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
916 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
918 /* Controller type */
919 value
= qemu_opt_get(legacy_opts
, "if");
922 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
925 if (type
== IF_COUNT
) {
926 error_report("unsupported bus type '%s'", value
);
930 type
= block_default_type
;
934 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
935 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
936 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
938 if (cyls
|| heads
|| secs
) {
940 error_report("invalid physical cyls number");
944 error_report("invalid physical heads number");
948 error_report("invalid physical secs number");
953 translation
= BIOS_ATA_TRANSLATION_AUTO
;
954 value
= qemu_opt_get(legacy_opts
, "trans");
957 error_report("'%s' trans must be used with cyls, heads and secs",
961 if (!strcmp(value
, "none")) {
962 translation
= BIOS_ATA_TRANSLATION_NONE
;
963 } else if (!strcmp(value
, "lba")) {
964 translation
= BIOS_ATA_TRANSLATION_LBA
;
965 } else if (!strcmp(value
, "large")) {
966 translation
= BIOS_ATA_TRANSLATION_LARGE
;
967 } else if (!strcmp(value
, "rechs")) {
968 translation
= BIOS_ATA_TRANSLATION_RECHS
;
969 } else if (!strcmp(value
, "auto")) {
970 translation
= BIOS_ATA_TRANSLATION_AUTO
;
972 error_report("'%s' invalid translation type", value
);
977 if (media
== MEDIA_CDROM
) {
978 if (cyls
|| secs
|| heads
) {
979 error_report("CHS can't be set with media=cdrom");
984 /* Device address specified by bus/unit or index.
985 * If none was specified, try to find the first free one. */
986 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
987 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
988 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
990 max_devs
= if_max_devs
[type
];
993 if (bus_id
!= 0 || unit_id
!= -1) {
994 error_report("index cannot be used with bus and unit");
997 bus_id
= drive_index_to_bus_id(type
, index
);
998 unit_id
= drive_index_to_unit_id(type
, index
);
1001 if (unit_id
== -1) {
1003 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1005 if (max_devs
&& unit_id
>= max_devs
) {
1006 unit_id
-= max_devs
;
1012 if (max_devs
&& unit_id
>= max_devs
) {
1013 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1017 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1018 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1019 bus_id
, unit_id
, index
);
1024 serial
= qemu_opt_get(legacy_opts
, "serial");
1026 /* no id supplied -> create one */
1027 if (qemu_opts_id(all_opts
) == NULL
) {
1029 const char *mediastr
= "";
1030 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1031 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1034 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1037 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1040 qdict_put_str(bs_opts
, "id", new_id
);
1044 /* Add virtio block device */
1045 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1046 if (devaddr
&& type
!= IF_VIRTIO
) {
1047 error_report("addr is not supported by this bus type");
1051 if (type
== IF_VIRTIO
) {
1053 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1055 if (arch_type
== QEMU_ARCH_S390X
) {
1056 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1058 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1060 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1063 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1067 filename
= qemu_opt_get(legacy_opts
, "file");
1069 /* Check werror/rerror compatibility with if=... */
1070 werror
= qemu_opt_get(legacy_opts
, "werror");
1071 if (werror
!= NULL
) {
1072 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1074 error_report("werror is not supported by this bus type");
1077 qdict_put_str(bs_opts
, "werror", werror
);
1080 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1081 if (rerror
!= NULL
) {
1082 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1084 error_report("rerror is not supported by this bus type");
1087 qdict_put_str(bs_opts
, "rerror", rerror
);
1090 /* Actual block device init: Functionality shared with blockdev-add */
1091 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1095 error_report_err(local_err
);
1102 /* Create legacy DriveInfo */
1103 dinfo
= g_malloc0(sizeof(*dinfo
));
1104 dinfo
->opts
= all_opts
;
1107 dinfo
->heads
= heads
;
1109 dinfo
->trans
= translation
;
1112 dinfo
->bus
= bus_id
;
1113 dinfo
->unit
= unit_id
;
1114 dinfo
->devaddr
= devaddr
;
1115 dinfo
->serial
= g_strdup(serial
);
1117 blk_set_legacy_dinfo(blk
, dinfo
);
1124 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1131 qemu_opts_del(legacy_opts
);
1136 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1138 BlockDriverState
*bs
;
1140 bs
= bdrv_lookup_bs(name
, name
, errp
);
1145 if (!bdrv_is_root_node(bs
)) {
1146 error_setg(errp
, "Need a root block node");
1150 if (!bdrv_is_inserted(bs
)) {
1151 error_setg(errp
, "Device has no medium");
1158 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1163 if (!blk_name
== !qdev_id
) {
1164 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1169 blk
= blk_by_qdev_id(qdev_id
, errp
);
1171 blk
= blk_by_name(blk_name
);
1173 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1174 "Device '%s' not found", blk_name
);
1181 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1183 const char *device
= qdict_get_str(qdict
, "device");
1187 if (!strcmp(device
, "all")) {
1188 ret
= blk_commit_all();
1190 BlockDriverState
*bs
;
1191 AioContext
*aio_context
;
1193 blk
= blk_by_name(device
);
1195 monitor_printf(mon
, "Device '%s' not found\n", device
);
1198 if (!blk_is_available(blk
)) {
1199 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1204 aio_context
= bdrv_get_aio_context(bs
);
1205 aio_context_acquire(aio_context
);
1207 ret
= bdrv_commit(bs
);
1209 aio_context_release(aio_context
);
1212 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1217 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1219 TransactionActionList list
;
1221 list
.value
= action
;
1223 qmp_transaction(&list
, false, NULL
, errp
);
1226 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1227 bool has_node_name
, const char *node_name
,
1228 const char *snapshot_file
,
1229 bool has_snapshot_node_name
,
1230 const char *snapshot_node_name
,
1231 bool has_format
, const char *format
,
1232 bool has_mode
, NewImageMode mode
, Error
**errp
)
1234 BlockdevSnapshotSync snapshot
= {
1235 .has_device
= has_device
,
1236 .device
= (char *) device
,
1237 .has_node_name
= has_node_name
,
1238 .node_name
= (char *) node_name
,
1239 .snapshot_file
= (char *) snapshot_file
,
1240 .has_snapshot_node_name
= has_snapshot_node_name
,
1241 .snapshot_node_name
= (char *) snapshot_node_name
,
1242 .has_format
= has_format
,
1243 .format
= (char *) format
,
1244 .has_mode
= has_mode
,
1247 TransactionAction action
= {
1248 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1249 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1251 blockdev_do_action(&action
, errp
);
1254 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1257 BlockdevSnapshot snapshot_data
= {
1258 .node
= (char *) node
,
1259 .overlay
= (char *) overlay
1261 TransactionAction action
= {
1262 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1263 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1265 blockdev_do_action(&action
, errp
);
1268 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1272 BlockdevSnapshotInternal snapshot
= {
1273 .device
= (char *) device
,
1274 .name
= (char *) name
1276 TransactionAction action
= {
1277 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1278 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1280 blockdev_do_action(&action
, errp
);
1283 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1290 BlockDriverState
*bs
;
1291 AioContext
*aio_context
;
1292 QEMUSnapshotInfo sn
;
1293 Error
*local_err
= NULL
;
1294 SnapshotInfo
*info
= NULL
;
1297 bs
= qmp_get_root_bs(device
, errp
);
1301 aio_context
= bdrv_get_aio_context(bs
);
1302 aio_context_acquire(aio_context
);
1313 error_setg(errp
, "Name or id must be provided");
1314 goto out_aio_context
;
1317 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1318 goto out_aio_context
;
1321 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1323 error_propagate(errp
, local_err
);
1324 goto out_aio_context
;
1328 "Snapshot with id '%s' and name '%s' does not exist on "
1330 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1331 goto out_aio_context
;
1334 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1336 error_propagate(errp
, local_err
);
1337 goto out_aio_context
;
1340 aio_context_release(aio_context
);
1342 info
= g_new0(SnapshotInfo
, 1);
1343 info
->id
= g_strdup(sn
.id_str
);
1344 info
->name
= g_strdup(sn
.name
);
1345 info
->date_nsec
= sn
.date_nsec
;
1346 info
->date_sec
= sn
.date_sec
;
1347 info
->vm_state_size
= sn
.vm_state_size
;
1348 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1349 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1354 aio_context_release(aio_context
);
1359 * block_dirty_bitmap_lookup:
1360 * Return a dirty bitmap (if present), after validating
1361 * the node reference and bitmap names.
1363 * @node: The name of the BDS node to search for bitmaps
1364 * @name: The name of the bitmap to search for
1365 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1366 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1367 * @errp: Output pointer for error information. Can be NULL.
1369 * @return: A bitmap object on success, or NULL on failure.
1371 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1373 BlockDriverState
**pbs
,
1376 BlockDriverState
*bs
;
1377 BdrvDirtyBitmap
*bitmap
;
1380 error_setg(errp
, "Node cannot be NULL");
1384 error_setg(errp
, "Bitmap name cannot be NULL");
1387 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1389 error_setg(errp
, "Node '%s' not found", node
);
1393 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1395 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1406 /* New and old BlockDriverState structs for atomic group operations */
1408 typedef struct BlkActionState BlkActionState
;
1412 * Table of operations that define an Action.
1414 * @instance_size: Size of state struct, in bytes.
1415 * @prepare: Prepare the work, must NOT be NULL.
1416 * @commit: Commit the changes, can be NULL.
1417 * @abort: Abort the changes on fail, can be NULL.
1418 * @clean: Clean up resources after all transaction actions have called
1419 * commit() or abort(). Can be NULL.
1421 * Only prepare() may fail. In a single transaction, only one of commit() or
1422 * abort() will be called. clean() will always be called if it is present.
1424 typedef struct BlkActionOps
{
1425 size_t instance_size
;
1426 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1427 void (*commit
)(BlkActionState
*common
);
1428 void (*abort
)(BlkActionState
*common
);
1429 void (*clean
)(BlkActionState
*common
);
1434 * Describes one Action's state within a Transaction.
1436 * @action: QAPI-defined enum identifying which Action to perform.
1437 * @ops: Table of ActionOps this Action can perform.
1438 * @block_job_txn: Transaction which this action belongs to.
1439 * @entry: List membership for all Actions in this Transaction.
1441 * This structure must be arranged as first member in a subclassed type,
1442 * assuming that the compiler will also arrange it to the same offsets as the
1445 struct BlkActionState
{
1446 TransactionAction
*action
;
1447 const BlkActionOps
*ops
;
1448 BlockJobTxn
*block_job_txn
;
1449 TransactionProperties
*txn_props
;
1450 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1453 /* internal snapshot private data */
1454 typedef struct InternalSnapshotState
{
1455 BlkActionState common
;
1456 BlockDriverState
*bs
;
1457 QEMUSnapshotInfo sn
;
1459 } InternalSnapshotState
;
1462 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1464 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1466 "Action '%s' does not support Transaction property "
1467 "completion-mode = %s",
1468 TransactionActionKind_str(s
->action
->type
),
1469 ActionCompletionMode_str(s
->txn_props
->completion_mode
));
1475 static void internal_snapshot_prepare(BlkActionState
*common
,
1478 Error
*local_err
= NULL
;
1481 BlockDriverState
*bs
;
1482 QEMUSnapshotInfo old_sn
, *sn
;
1485 BlockdevSnapshotInternal
*internal
;
1486 InternalSnapshotState
*state
;
1487 AioContext
*aio_context
;
1490 g_assert(common
->action
->type
==
1491 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1492 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1493 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1495 /* 1. parse input */
1496 device
= internal
->device
;
1497 name
= internal
->name
;
1499 /* 2. check for validation */
1500 if (action_check_completion_mode(common
, errp
) < 0) {
1504 bs
= qmp_get_root_bs(device
, errp
);
1509 aio_context
= bdrv_get_aio_context(bs
);
1510 aio_context_acquire(aio_context
);
1514 /* Paired with .clean() */
1515 bdrv_drained_begin(bs
);
1517 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1521 if (bdrv_is_read_only(bs
)) {
1522 error_setg(errp
, "Device '%s' is read only", device
);
1526 if (!bdrv_can_snapshot(bs
)) {
1527 error_setg(errp
, "Block format '%s' used by device '%s' "
1528 "does not support internal snapshots",
1529 bs
->drv
->format_name
, device
);
1533 if (!strlen(name
)) {
1534 error_setg(errp
, "Name is empty");
1538 /* check whether a snapshot with name exist */
1539 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1542 error_propagate(errp
, local_err
);
1546 "Snapshot with name '%s' already exists on device '%s'",
1551 /* 3. take the snapshot */
1553 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1554 qemu_gettimeofday(&tv
);
1555 sn
->date_sec
= tv
.tv_sec
;
1556 sn
->date_nsec
= tv
.tv_usec
* 1000;
1557 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1559 ret1
= bdrv_snapshot_create(bs
, sn
);
1561 error_setg_errno(errp
, -ret1
,
1562 "Failed to create snapshot '%s' on device '%s'",
1567 /* 4. succeed, mark a snapshot is created */
1568 state
->created
= true;
1571 aio_context_release(aio_context
);
1574 static void internal_snapshot_abort(BlkActionState
*common
)
1576 InternalSnapshotState
*state
=
1577 DO_UPCAST(InternalSnapshotState
, common
, common
);
1578 BlockDriverState
*bs
= state
->bs
;
1579 QEMUSnapshotInfo
*sn
= &state
->sn
;
1580 AioContext
*aio_context
;
1581 Error
*local_error
= NULL
;
1583 if (!state
->created
) {
1587 aio_context
= bdrv_get_aio_context(state
->bs
);
1588 aio_context_acquire(aio_context
);
1590 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1591 error_reportf_err(local_error
,
1592 "Failed to delete snapshot with id '%s' and "
1593 "name '%s' on device '%s' in abort: ",
1594 sn
->id_str
, sn
->name
,
1595 bdrv_get_device_name(bs
));
1598 aio_context_release(aio_context
);
1601 static void internal_snapshot_clean(BlkActionState
*common
)
1603 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1605 AioContext
*aio_context
;
1611 aio_context
= bdrv_get_aio_context(state
->bs
);
1612 aio_context_acquire(aio_context
);
1614 bdrv_drained_end(state
->bs
);
1616 aio_context_release(aio_context
);
1619 /* external snapshot private data */
1620 typedef struct ExternalSnapshotState
{
1621 BlkActionState common
;
1622 BlockDriverState
*old_bs
;
1623 BlockDriverState
*new_bs
;
1624 bool overlay_appended
;
1625 } ExternalSnapshotState
;
1627 static void external_snapshot_prepare(BlkActionState
*common
,
1631 QDict
*options
= NULL
;
1632 Error
*local_err
= NULL
;
1633 /* Device and node name of the image to generate the snapshot from */
1635 const char *node_name
;
1636 /* Reference to the new image (for 'blockdev-snapshot') */
1637 const char *snapshot_ref
;
1638 /* File name of the new image (for 'blockdev-snapshot-sync') */
1639 const char *new_image_file
;
1640 ExternalSnapshotState
*state
=
1641 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1642 TransactionAction
*action
= common
->action
;
1643 AioContext
*aio_context
;
1645 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1646 * purpose but a different set of parameters */
1647 switch (action
->type
) {
1648 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1650 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1652 node_name
= s
->node
;
1653 new_image_file
= NULL
;
1654 snapshot_ref
= s
->overlay
;
1657 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1659 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1660 device
= s
->has_device
? s
->device
: NULL
;
1661 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1662 new_image_file
= s
->snapshot_file
;
1663 snapshot_ref
= NULL
;
1667 g_assert_not_reached();
1670 /* start processing */
1671 if (action_check_completion_mode(common
, errp
) < 0) {
1675 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1676 if (!state
->old_bs
) {
1680 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1681 aio_context_acquire(aio_context
);
1683 /* Paired with .clean() */
1684 bdrv_drained_begin(state
->old_bs
);
1686 if (!bdrv_is_inserted(state
->old_bs
)) {
1687 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1691 if (bdrv_op_is_blocked(state
->old_bs
,
1692 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1696 if (!bdrv_is_read_only(state
->old_bs
)) {
1697 if (bdrv_flush(state
->old_bs
)) {
1698 error_setg(errp
, QERR_IO_ERROR
);
1703 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1704 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1708 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1709 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1710 const char *format
= s
->has_format
? s
->format
: "qcow2";
1711 enum NewImageMode mode
;
1712 const char *snapshot_node_name
=
1713 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1715 if (node_name
&& !snapshot_node_name
) {
1716 error_setg(errp
, "New snapshot node name missing");
1720 if (snapshot_node_name
&&
1721 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1722 error_setg(errp
, "New snapshot node name already in use");
1726 flags
= state
->old_bs
->open_flags
;
1727 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_COPY_ON_READ
);
1728 flags
|= BDRV_O_NO_BACKING
;
1730 /* create new image w/backing file */
1731 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1732 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1733 int64_t size
= bdrv_getlength(state
->old_bs
);
1735 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1738 bdrv_img_create(new_image_file
, format
,
1739 state
->old_bs
->filename
,
1740 state
->old_bs
->drv
->format_name
,
1741 NULL
, size
, flags
, false, &local_err
);
1743 error_propagate(errp
, local_err
);
1748 options
= qdict_new();
1749 if (s
->has_snapshot_node_name
) {
1750 qdict_put_str(options
, "node-name", snapshot_node_name
);
1752 qdict_put_str(options
, "driver", format
);
1755 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1757 /* We will manually add the backing_hd field to the bs later */
1758 if (!state
->new_bs
) {
1762 if (bdrv_has_blk(state
->new_bs
)) {
1763 error_setg(errp
, "The snapshot is already in use");
1767 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1772 if (state
->new_bs
->backing
!= NULL
) {
1773 error_setg(errp
, "The snapshot already has a backing image");
1777 if (!state
->new_bs
->drv
->supports_backing
) {
1778 error_setg(errp
, "The snapshot does not support backing images");
1782 bdrv_set_aio_context(state
->new_bs
, aio_context
);
1784 /* This removes our old bs and adds the new bs. This is an operation that
1785 * can fail, so we need to do it in .prepare; undoing it for abort is
1786 * always possible. */
1787 bdrv_ref(state
->new_bs
);
1788 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1790 error_propagate(errp
, local_err
);
1793 state
->overlay_appended
= true;
1796 aio_context_release(aio_context
);
1799 static void external_snapshot_commit(BlkActionState
*common
)
1801 ExternalSnapshotState
*state
=
1802 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1803 AioContext
*aio_context
;
1805 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1806 aio_context_acquire(aio_context
);
1808 /* We don't need (or want) to use the transactional
1809 * bdrv_reopen_multiple() across all the entries at once, because we
1810 * don't want to abort all of them if one of them fails the reopen */
1811 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1812 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1816 aio_context_release(aio_context
);
1819 static void external_snapshot_abort(BlkActionState
*common
)
1821 ExternalSnapshotState
*state
=
1822 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1823 if (state
->new_bs
) {
1824 if (state
->overlay_appended
) {
1825 AioContext
*aio_context
;
1827 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1828 aio_context_acquire(aio_context
);
1830 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1831 close state->old_bs; we need it */
1832 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1833 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1834 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1836 aio_context_release(aio_context
);
1841 static void external_snapshot_clean(BlkActionState
*common
)
1843 ExternalSnapshotState
*state
=
1844 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1845 AioContext
*aio_context
;
1847 if (!state
->old_bs
) {
1851 aio_context
= bdrv_get_aio_context(state
->old_bs
);
1852 aio_context_acquire(aio_context
);
1854 bdrv_drained_end(state
->old_bs
);
1855 bdrv_unref(state
->new_bs
);
1857 aio_context_release(aio_context
);
1860 typedef struct DriveBackupState
{
1861 BlkActionState common
;
1862 BlockDriverState
*bs
;
1866 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
1869 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1871 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1872 BlockDriverState
*bs
;
1873 DriveBackup
*backup
;
1874 AioContext
*aio_context
;
1875 Error
*local_err
= NULL
;
1877 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1878 backup
= common
->action
->u
.drive_backup
.data
;
1880 bs
= qmp_get_root_bs(backup
->device
, errp
);
1885 aio_context
= bdrv_get_aio_context(bs
);
1886 aio_context_acquire(aio_context
);
1888 /* Paired with .clean() */
1889 bdrv_drained_begin(bs
);
1893 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1895 error_propagate(errp
, local_err
);
1900 aio_context_release(aio_context
);
1903 static void drive_backup_commit(BlkActionState
*common
)
1905 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1906 AioContext
*aio_context
;
1908 aio_context
= bdrv_get_aio_context(state
->bs
);
1909 aio_context_acquire(aio_context
);
1912 block_job_start(state
->job
);
1914 aio_context_release(aio_context
);
1917 static void drive_backup_abort(BlkActionState
*common
)
1919 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1922 AioContext
*aio_context
;
1924 aio_context
= bdrv_get_aio_context(state
->bs
);
1925 aio_context_acquire(aio_context
);
1927 block_job_cancel_sync(state
->job
);
1929 aio_context_release(aio_context
);
1933 static void drive_backup_clean(BlkActionState
*common
)
1935 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1936 AioContext
*aio_context
;
1942 aio_context
= bdrv_get_aio_context(state
->bs
);
1943 aio_context_acquire(aio_context
);
1945 bdrv_drained_end(state
->bs
);
1947 aio_context_release(aio_context
);
1950 typedef struct BlockdevBackupState
{
1951 BlkActionState common
;
1952 BlockDriverState
*bs
;
1954 } BlockdevBackupState
;
1956 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
1959 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1961 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1962 BlockdevBackup
*backup
;
1963 BlockDriverState
*bs
, *target
;
1964 AioContext
*aio_context
;
1965 Error
*local_err
= NULL
;
1967 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1968 backup
= common
->action
->u
.blockdev_backup
.data
;
1970 bs
= qmp_get_root_bs(backup
->device
, errp
);
1975 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1980 aio_context
= bdrv_get_aio_context(bs
);
1981 if (aio_context
!= bdrv_get_aio_context(target
)) {
1982 error_setg(errp
, "Backup between two IO threads is not implemented");
1985 aio_context_acquire(aio_context
);
1988 /* Paired with .clean() */
1989 bdrv_drained_begin(state
->bs
);
1991 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1993 error_propagate(errp
, local_err
);
1998 aio_context_release(aio_context
);
2001 static void blockdev_backup_commit(BlkActionState
*common
)
2003 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2004 AioContext
*aio_context
;
2006 aio_context
= bdrv_get_aio_context(state
->bs
);
2007 aio_context_acquire(aio_context
);
2010 block_job_start(state
->job
);
2012 aio_context_release(aio_context
);
2015 static void blockdev_backup_abort(BlkActionState
*common
)
2017 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2020 AioContext
*aio_context
;
2022 aio_context
= bdrv_get_aio_context(state
->bs
);
2023 aio_context_acquire(aio_context
);
2025 block_job_cancel_sync(state
->job
);
2027 aio_context_release(aio_context
);
2031 static void blockdev_backup_clean(BlkActionState
*common
)
2033 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
2034 AioContext
*aio_context
;
2040 aio_context
= bdrv_get_aio_context(state
->bs
);
2041 aio_context_acquire(aio_context
);
2043 bdrv_drained_end(state
->bs
);
2045 aio_context_release(aio_context
);
2048 typedef struct BlockDirtyBitmapState
{
2049 BlkActionState common
;
2050 BdrvDirtyBitmap
*bitmap
;
2051 BlockDriverState
*bs
;
2054 } BlockDirtyBitmapState
;
2056 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
2059 Error
*local_err
= NULL
;
2060 BlockDirtyBitmapAdd
*action
;
2061 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2064 if (action_check_completion_mode(common
, errp
) < 0) {
2068 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2069 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
2070 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
2071 action
->has_granularity
, action
->granularity
,
2072 action
->has_persistent
, action
->persistent
,
2073 action
->has_autoload
, action
->autoload
,
2077 state
->prepared
= true;
2079 error_propagate(errp
, local_err
);
2083 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2085 BlockDirtyBitmapAdd
*action
;
2086 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2089 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2090 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2091 * then the node reference and bitmap name must have been valid.
2093 if (state
->prepared
) {
2094 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2098 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2101 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2103 BlockDirtyBitmap
*action
;
2105 if (action_check_completion_mode(common
, errp
) < 0) {
2109 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2110 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2114 if (!state
->bitmap
) {
2118 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2119 error_setg(errp
, "Cannot modify a frozen bitmap");
2121 } else if (bdrv_dirty_bitmap_qmp_locked(state
->bitmap
)) {
2122 error_setg(errp
, "Cannot modify a locked bitmap");
2124 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2125 error_setg(errp
, "Cannot clear a disabled bitmap");
2127 } else if (bdrv_dirty_bitmap_readonly(state
->bitmap
)) {
2128 error_setg(errp
, "Cannot clear a readonly bitmap");
2132 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2135 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2137 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2140 if (state
->backup
) {
2141 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2145 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2147 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2150 hbitmap_free(state
->backup
);
2153 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2155 error_setg(errp
, "Transaction aborted using Abort action");
2158 static void abort_commit(BlkActionState
*common
)
2160 g_assert_not_reached(); /* this action never succeeds */
2163 static const BlkActionOps actions
[] = {
2164 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2165 .instance_size
= sizeof(ExternalSnapshotState
),
2166 .prepare
= external_snapshot_prepare
,
2167 .commit
= external_snapshot_commit
,
2168 .abort
= external_snapshot_abort
,
2169 .clean
= external_snapshot_clean
,
2171 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2172 .instance_size
= sizeof(ExternalSnapshotState
),
2173 .prepare
= external_snapshot_prepare
,
2174 .commit
= external_snapshot_commit
,
2175 .abort
= external_snapshot_abort
,
2176 .clean
= external_snapshot_clean
,
2178 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2179 .instance_size
= sizeof(DriveBackupState
),
2180 .prepare
= drive_backup_prepare
,
2181 .commit
= drive_backup_commit
,
2182 .abort
= drive_backup_abort
,
2183 .clean
= drive_backup_clean
,
2185 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2186 .instance_size
= sizeof(BlockdevBackupState
),
2187 .prepare
= blockdev_backup_prepare
,
2188 .commit
= blockdev_backup_commit
,
2189 .abort
= blockdev_backup_abort
,
2190 .clean
= blockdev_backup_clean
,
2192 [TRANSACTION_ACTION_KIND_ABORT
] = {
2193 .instance_size
= sizeof(BlkActionState
),
2194 .prepare
= abort_prepare
,
2195 .commit
= abort_commit
,
2197 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2198 .instance_size
= sizeof(InternalSnapshotState
),
2199 .prepare
= internal_snapshot_prepare
,
2200 .abort
= internal_snapshot_abort
,
2201 .clean
= internal_snapshot_clean
,
2203 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2204 .instance_size
= sizeof(BlockDirtyBitmapState
),
2205 .prepare
= block_dirty_bitmap_add_prepare
,
2206 .abort
= block_dirty_bitmap_add_abort
,
2208 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2209 .instance_size
= sizeof(BlockDirtyBitmapState
),
2210 .prepare
= block_dirty_bitmap_clear_prepare
,
2211 .commit
= block_dirty_bitmap_clear_commit
,
2212 .abort
= block_dirty_bitmap_clear_abort
,
2217 * Allocate a TransactionProperties structure if necessary, and fill
2218 * that structure with desired defaults if they are unset.
2220 static TransactionProperties
*get_transaction_properties(
2221 TransactionProperties
*props
)
2224 props
= g_new0(TransactionProperties
, 1);
2227 if (!props
->has_completion_mode
) {
2228 props
->has_completion_mode
= true;
2229 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2236 * 'Atomic' group operations. The operations are performed as a set, and if
2237 * any fail then we roll back all operations in the group.
2239 void qmp_transaction(TransactionActionList
*dev_list
,
2241 struct TransactionProperties
*props
,
2244 TransactionActionList
*dev_entry
= dev_list
;
2245 BlockJobTxn
*block_job_txn
= NULL
;
2246 BlkActionState
*state
, *next
;
2247 Error
*local_err
= NULL
;
2249 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2250 QSIMPLEQ_INIT(&snap_bdrv_states
);
2252 /* Does this transaction get canceled as a group on failure?
2253 * If not, we don't really need to make a BlockJobTxn.
2255 props
= get_transaction_properties(props
);
2256 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2257 block_job_txn
= block_job_txn_new();
2260 /* drain all i/o before any operations */
2263 /* We don't do anything in this loop that commits us to the operations */
2264 while (NULL
!= dev_entry
) {
2265 TransactionAction
*dev_info
= NULL
;
2266 const BlkActionOps
*ops
;
2268 dev_info
= dev_entry
->value
;
2269 dev_entry
= dev_entry
->next
;
2271 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2273 ops
= &actions
[dev_info
->type
];
2274 assert(ops
->instance_size
> 0);
2276 state
= g_malloc0(ops
->instance_size
);
2278 state
->action
= dev_info
;
2279 state
->block_job_txn
= block_job_txn
;
2280 state
->txn_props
= props
;
2281 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2283 state
->ops
->prepare(state
, &local_err
);
2285 error_propagate(errp
, local_err
);
2286 goto delete_and_fail
;
2290 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2291 if (state
->ops
->commit
) {
2292 state
->ops
->commit(state
);
2300 /* failure, and it is all-or-none; roll back all operations */
2301 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2302 if (state
->ops
->abort
) {
2303 state
->ops
->abort(state
);
2307 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2308 if (state
->ops
->clean
) {
2309 state
->ops
->clean(state
);
2314 qapi_free_TransactionProperties(props
);
2316 block_job_txn_unref(block_job_txn
);
2319 void qmp_eject(bool has_device
, const char *device
,
2320 bool has_id
, const char *id
,
2321 bool has_force
, bool force
, Error
**errp
)
2323 Error
*local_err
= NULL
;
2330 rc
= do_open_tray(has_device
? device
: NULL
,
2333 if (rc
&& rc
!= -ENOSYS
) {
2334 error_propagate(errp
, local_err
);
2337 error_free(local_err
);
2339 blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2342 void qmp_block_passwd(bool has_device
, const char *device
,
2343 bool has_node_name
, const char *node_name
,
2344 const char *password
, Error
**errp
)
2347 "Setting block passwords directly is no longer supported");
2351 * Attempt to open the tray of @device.
2352 * If @force, ignore its tray lock.
2353 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2354 * On error, store an error through @errp and return -errno.
2355 * If @device does not exist, return -ENODEV.
2356 * If it has no removable media, return -ENOTSUP.
2357 * If it has no tray, return -ENOSYS.
2358 * If the guest was asked to open the tray, return -EINPROGRESS.
2361 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2362 bool force
, Error
**errp
)
2365 const char *device
= qdev_id
?: blk_name
;
2368 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2373 if (!blk_dev_has_removable_media(blk
)) {
2374 error_setg(errp
, "Device '%s' is not removable", device
);
2378 if (!blk_dev_has_tray(blk
)) {
2379 error_setg(errp
, "Device '%s' does not have a tray", device
);
2383 if (blk_dev_is_tray_open(blk
)) {
2387 locked
= blk_dev_is_medium_locked(blk
);
2389 blk_dev_eject_request(blk
, force
);
2392 if (!locked
|| force
) {
2393 blk_dev_change_media_cb(blk
, false, &error_abort
);
2396 if (locked
&& !force
) {
2397 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2398 "wait for tray to open and try again", device
);
2399 return -EINPROGRESS
;
2405 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2406 bool has_id
, const char *id
,
2407 bool has_force
, bool force
,
2410 Error
*local_err
= NULL
;
2416 rc
= do_open_tray(has_device
? device
: NULL
,
2419 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2420 error_propagate(errp
, local_err
);
2423 error_free(local_err
);
2426 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2427 bool has_id
, const char *id
,
2431 Error
*local_err
= NULL
;
2433 device
= has_device
? device
: NULL
;
2434 id
= has_id
? id
: NULL
;
2436 blk
= qmp_get_blk(device
, id
, errp
);
2441 if (!blk_dev_has_removable_media(blk
)) {
2442 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2446 if (!blk_dev_has_tray(blk
)) {
2447 /* Ignore this command on tray-less devices */
2451 if (!blk_dev_is_tray_open(blk
)) {
2455 blk_dev_change_media_cb(blk
, true, &local_err
);
2457 error_propagate(errp
, local_err
);
2462 static void blockdev_remove_medium(bool has_device
, const char *device
,
2463 bool has_id
, const char *id
, Error
**errp
)
2466 BlockDriverState
*bs
;
2467 AioContext
*aio_context
;
2468 bool has_attached_device
;
2470 device
= has_device
? device
: NULL
;
2471 id
= has_id
? id
: NULL
;
2473 blk
= qmp_get_blk(device
, id
, errp
);
2478 /* For BBs without a device, we can exchange the BDS tree at will */
2479 has_attached_device
= blk_get_attached_dev(blk
);
2481 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2482 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2486 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2487 !blk_dev_is_tray_open(blk
))
2489 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2498 aio_context
= bdrv_get_aio_context(bs
);
2499 aio_context_acquire(aio_context
);
2501 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2507 if (!blk_dev_has_tray(blk
)) {
2508 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2509 * called at all); therefore, the medium needs to be ejected here.
2510 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2511 * value passed here (i.e. false). */
2512 blk_dev_change_media_cb(blk
, false, &error_abort
);
2516 aio_context_release(aio_context
);
2519 void qmp_blockdev_remove_medium(const char *id
, Error
**errp
)
2521 blockdev_remove_medium(false, NULL
, true, id
, errp
);
2524 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2525 BlockDriverState
*bs
, Error
**errp
)
2527 Error
*local_err
= NULL
;
2531 /* For BBs without a device, we can exchange the BDS tree at will */
2532 has_device
= blk_get_attached_dev(blk
);
2534 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2535 error_setg(errp
, "Device is not removable");
2539 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2540 error_setg(errp
, "Tray of the device is not open");
2545 error_setg(errp
, "There already is a medium in the device");
2549 ret
= blk_insert_bs(blk
, bs
, errp
);
2554 if (!blk_dev_has_tray(blk
)) {
2555 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2556 * called at all); therefore, the medium needs to be pushed into the
2558 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2559 * value passed here (i.e. true). */
2560 blk_dev_change_media_cb(blk
, true, &local_err
);
2562 error_propagate(errp
, local_err
);
2569 static void blockdev_insert_medium(bool has_device
, const char *device
,
2570 bool has_id
, const char *id
,
2571 const char *node_name
, Error
**errp
)
2574 BlockDriverState
*bs
;
2576 blk
= qmp_get_blk(has_device
? device
: NULL
,
2583 bs
= bdrv_find_node(node_name
);
2585 error_setg(errp
, "Node '%s' not found", node_name
);
2589 if (bdrv_has_blk(bs
)) {
2590 error_setg(errp
, "Node '%s' is already in use", node_name
);
2594 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2597 void qmp_blockdev_insert_medium(const char *id
, const char *node_name
,
2600 blockdev_insert_medium(false, NULL
, true, id
, node_name
, errp
);
2603 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2604 bool has_id
, const char *id
,
2605 const char *filename
,
2606 bool has_format
, const char *format
,
2608 BlockdevChangeReadOnlyMode read_only
,
2612 BlockDriverState
*medium_bs
= NULL
;
2616 QDict
*options
= NULL
;
2619 blk
= qmp_get_blk(has_device
? device
: NULL
,
2627 blk_update_root_state(blk
);
2630 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2631 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2634 if (!has_read_only
) {
2635 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2638 switch (read_only
) {
2639 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2642 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2643 bdrv_flags
&= ~BDRV_O_RDWR
;
2646 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2647 bdrv_flags
|= BDRV_O_RDWR
;
2654 options
= qdict_new();
2655 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2656 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2659 qdict_put_str(options
, "driver", format
);
2662 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2667 rc
= do_open_tray(has_device
? device
: NULL
,
2670 if (rc
&& rc
!= -ENOSYS
) {
2671 error_propagate(errp
, err
);
2677 blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2679 error_propagate(errp
, err
);
2683 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2685 error_propagate(errp
, err
);
2689 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2692 /* If the medium has been inserted, the device has its own reference, so
2693 * ours must be relinquished; and if it has not been inserted successfully,
2694 * the reference must be relinquished anyway */
2695 bdrv_unref(medium_bs
);
2698 /* throttling disk I/O limits */
2699 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2702 BlockDriverState
*bs
;
2704 AioContext
*aio_context
;
2706 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2707 arg
->has_id
? arg
->id
: NULL
,
2713 aio_context
= blk_get_aio_context(blk
);
2714 aio_context_acquire(aio_context
);
2718 error_setg(errp
, "Device has no medium");
2722 throttle_config_init(&cfg
);
2723 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2724 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2725 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2727 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2728 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2729 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2731 if (arg
->has_bps_max
) {
2732 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2734 if (arg
->has_bps_rd_max
) {
2735 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2737 if (arg
->has_bps_wr_max
) {
2738 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2740 if (arg
->has_iops_max
) {
2741 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2743 if (arg
->has_iops_rd_max
) {
2744 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2746 if (arg
->has_iops_wr_max
) {
2747 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2750 if (arg
->has_bps_max_length
) {
2751 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2753 if (arg
->has_bps_rd_max_length
) {
2754 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2756 if (arg
->has_bps_wr_max_length
) {
2757 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2759 if (arg
->has_iops_max_length
) {
2760 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2762 if (arg
->has_iops_rd_max_length
) {
2763 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2765 if (arg
->has_iops_wr_max_length
) {
2766 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2769 if (arg
->has_iops_size
) {
2770 cfg
.op_size
= arg
->iops_size
;
2773 if (!throttle_is_valid(&cfg
, errp
)) {
2777 if (throttle_enabled(&cfg
)) {
2778 /* Enable I/O limits if they're not enabled yet, otherwise
2779 * just update the throttling group. */
2780 if (!blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2781 blk_io_limits_enable(blk
,
2782 arg
->has_group
? arg
->group
:
2783 arg
->has_device
? arg
->device
:
2785 } else if (arg
->has_group
) {
2786 blk_io_limits_update_group(blk
, arg
->group
);
2788 /* Set the new throttling configuration */
2789 blk_set_io_limits(blk
, &cfg
);
2790 } else if (blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
2791 /* If all throttling settings are set to 0, disable I/O limits */
2792 blk_io_limits_disable(blk
);
2796 aio_context_release(aio_context
);
2799 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2800 bool has_granularity
, uint32_t granularity
,
2801 bool has_persistent
, bool persistent
,
2802 bool has_autoload
, bool autoload
,
2805 BlockDriverState
*bs
;
2806 BdrvDirtyBitmap
*bitmap
;
2808 if (!name
|| name
[0] == '\0') {
2809 error_setg(errp
, "Bitmap name cannot be empty");
2813 bs
= bdrv_lookup_bs(node
, node
, errp
);
2818 if (has_granularity
) {
2819 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2820 error_setg(errp
, "Granularity must be power of 2 "
2821 "and at least 512");
2825 /* Default to cluster size, if available: */
2826 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2829 if (!has_persistent
) {
2834 warn_report("Autoload option is deprecated and its value is ignored");
2838 !bdrv_can_store_new_dirty_bitmap(bs
, name
, granularity
, errp
))
2843 bitmap
= bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2844 if (bitmap
== NULL
) {
2848 bdrv_dirty_bitmap_set_persistance(bitmap
, persistent
);
2851 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2854 BlockDriverState
*bs
;
2855 BdrvDirtyBitmap
*bitmap
;
2856 Error
*local_err
= NULL
;
2858 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2859 if (!bitmap
|| !bs
) {
2863 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2865 "Bitmap '%s' is currently frozen and cannot be removed",
2868 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2870 "Bitmap '%s' is currently locked and cannot be removed",
2875 if (bdrv_dirty_bitmap_get_persistance(bitmap
)) {
2876 bdrv_remove_persistent_dirty_bitmap(bs
, name
, &local_err
);
2877 if (local_err
!= NULL
) {
2878 error_propagate(errp
, local_err
);
2883 bdrv_dirty_bitmap_make_anon(bitmap
);
2884 bdrv_release_dirty_bitmap(bs
, bitmap
);
2888 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2889 * immediately after a full backup operation.
2891 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2894 BdrvDirtyBitmap
*bitmap
;
2895 BlockDriverState
*bs
;
2897 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2898 if (!bitmap
|| !bs
) {
2902 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2904 "Bitmap '%s' is currently frozen and cannot be modified",
2907 } else if (bdrv_dirty_bitmap_qmp_locked(bitmap
)) {
2909 "Bitmap '%s' is currently locked and cannot be modified",
2912 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2914 "Bitmap '%s' is currently disabled and cannot be cleared",
2917 } else if (bdrv_dirty_bitmap_readonly(bitmap
)) {
2918 error_setg(errp
, "Bitmap '%s' is readonly and cannot be cleared", name
);
2922 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2925 BlockDirtyBitmapSha256
*qmp_x_debug_block_dirty_bitmap_sha256(const char *node
,
2929 BdrvDirtyBitmap
*bitmap
;
2930 BlockDriverState
*bs
;
2931 BlockDirtyBitmapSha256
*ret
= NULL
;
2934 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2935 if (!bitmap
|| !bs
) {
2939 sha256
= bdrv_dirty_bitmap_sha256(bitmap
, errp
);
2940 if (sha256
== NULL
) {
2944 ret
= g_new(BlockDirtyBitmapSha256
, 1);
2945 ret
->sha256
= sha256
;
2950 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2952 const char *id
= qdict_get_str(qdict
, "id");
2954 BlockDriverState
*bs
;
2955 AioContext
*aio_context
;
2956 Error
*local_err
= NULL
;
2958 bs
= bdrv_find_node(id
);
2960 qmp_blockdev_del(id
, &local_err
);
2962 error_report_err(local_err
);
2967 blk
= blk_by_name(id
);
2969 error_report("Device '%s' not found", id
);
2973 if (!blk_legacy_dinfo(blk
)) {
2974 error_report("Deleting device added with blockdev-add"
2975 " is not supported");
2979 aio_context
= blk_get_aio_context(blk
);
2980 aio_context_acquire(aio_context
);
2984 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2985 error_report_err(local_err
);
2986 aio_context_release(aio_context
);
2993 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2994 monitor_remove_blk(blk
);
2996 /* If this BlockBackend has a device attached to it, its refcount will be
2997 * decremented when the device is removed; otherwise we have to do so here.
2999 if (blk_get_attached_dev(blk
)) {
3000 /* Further I/O must not pause the guest */
3001 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
3002 BLOCKDEV_ON_ERROR_REPORT
);
3007 aio_context_release(aio_context
);
3010 void qmp_block_resize(bool has_device
, const char *device
,
3011 bool has_node_name
, const char *node_name
,
3012 int64_t size
, Error
**errp
)
3014 Error
*local_err
= NULL
;
3015 BlockBackend
*blk
= NULL
;
3016 BlockDriverState
*bs
;
3017 AioContext
*aio_context
;
3020 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
3021 has_node_name
? node_name
: NULL
,
3024 error_propagate(errp
, local_err
);
3028 aio_context
= bdrv_get_aio_context(bs
);
3029 aio_context_acquire(aio_context
);
3031 if (!bdrv_is_first_non_filter(bs
)) {
3032 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
3037 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
3041 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
3042 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
3046 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
3047 ret
= blk_insert_bs(blk
, bs
, errp
);
3052 bdrv_drained_begin(bs
);
3053 ret
= blk_truncate(blk
, size
, PREALLOC_MODE_OFF
, errp
);
3054 bdrv_drained_end(bs
);
3058 aio_context_release(aio_context
);
3061 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
3062 bool has_base
, const char *base
,
3063 bool has_base_node
, const char *base_node
,
3064 bool has_backing_file
, const char *backing_file
,
3065 bool has_speed
, int64_t speed
,
3066 bool has_on_error
, BlockdevOnError on_error
,
3069 BlockDriverState
*bs
, *iter
;
3070 BlockDriverState
*base_bs
= NULL
;
3071 AioContext
*aio_context
;
3072 Error
*local_err
= NULL
;
3073 const char *base_name
= NULL
;
3075 if (!has_on_error
) {
3076 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3079 bs
= bdrv_lookup_bs(device
, device
, errp
);
3084 aio_context
= bdrv_get_aio_context(bs
);
3085 aio_context_acquire(aio_context
);
3087 if (has_base
&& has_base_node
) {
3088 error_setg(errp
, "'base' and 'base-node' cannot be specified "
3089 "at the same time");
3094 base_bs
= bdrv_find_backing_image(bs
, base
);
3095 if (base_bs
== NULL
) {
3096 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
3099 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3103 if (has_base_node
) {
3104 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
3108 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
3109 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
3113 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3114 base_name
= base_bs
->filename
;
3117 /* Check for op blockers in the whole chain between bs and base */
3118 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
3119 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
3124 /* if we are streaming the entire chain, the result will have no backing
3125 * file, and specifying one is therefore an error */
3126 if (base_bs
== NULL
&& has_backing_file
) {
3127 error_setg(errp
, "backing file specified, but streaming the "
3132 /* backing_file string overrides base bs filename */
3133 base_name
= has_backing_file
? backing_file
: base_name
;
3135 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
3136 has_speed
? speed
: 0, on_error
, &local_err
);
3138 error_propagate(errp
, local_err
);
3142 trace_qmp_block_stream(bs
, bs
->job
);
3145 aio_context_release(aio_context
);
3148 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3149 bool has_base
, const char *base
,
3150 bool has_top
, const char *top
,
3151 bool has_backing_file
, const char *backing_file
,
3152 bool has_speed
, int64_t speed
,
3153 bool has_filter_node_name
, const char *filter_node_name
,
3156 BlockDriverState
*bs
;
3157 BlockDriverState
*iter
;
3158 BlockDriverState
*base_bs
, *top_bs
;
3159 AioContext
*aio_context
;
3160 Error
*local_err
= NULL
;
3161 /* This will be part of the QMP command, if/when the
3162 * BlockdevOnError change for blkmirror makes it in
3164 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3169 if (!has_filter_node_name
) {
3170 filter_node_name
= NULL
;
3174 * libvirt relies on the DeviceNotFound error class in order to probe for
3175 * live commit feature versions; for this to work, we must make sure to
3176 * perform the device lookup before any generic errors that may occur in a
3177 * scenario in which all optional arguments are omitted. */
3178 bs
= qmp_get_root_bs(device
, &local_err
);
3180 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3182 error_free(local_err
);
3183 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3184 "Device '%s' not found", device
);
3186 error_propagate(errp
, local_err
);
3191 aio_context
= bdrv_get_aio_context(bs
);
3192 aio_context_acquire(aio_context
);
3194 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3198 /* default top_bs is the active layer */
3201 if (has_top
&& top
) {
3202 if (strcmp(bs
->filename
, top
) != 0) {
3203 top_bs
= bdrv_find_backing_image(bs
, top
);
3207 if (top_bs
== NULL
) {
3208 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3212 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3214 if (has_base
&& base
) {
3215 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3217 base_bs
= bdrv_find_base(top_bs
);
3220 if (base_bs
== NULL
) {
3221 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3225 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3227 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3228 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3233 /* Do not allow attempts to commit an image into itself */
3234 if (top_bs
== base_bs
) {
3235 error_setg(errp
, "cannot commit an image into itself");
3240 if (has_backing_file
) {
3241 error_setg(errp
, "'backing-file' specified,"
3242 " but 'top' is the active layer");
3245 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3246 BLOCK_JOB_DEFAULT
, speed
, on_error
,
3247 filter_node_name
, NULL
, NULL
, false, &local_err
);
3249 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3250 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3253 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3254 on_error
, has_backing_file
? backing_file
: NULL
,
3255 filter_node_name
, &local_err
);
3257 if (local_err
!= NULL
) {
3258 error_propagate(errp
, local_err
);
3263 aio_context_release(aio_context
);
3266 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
3269 BlockDriverState
*bs
;
3270 BlockDriverState
*target_bs
;
3271 BlockDriverState
*source
= NULL
;
3272 BlockJob
*job
= NULL
;
3273 BdrvDirtyBitmap
*bmap
= NULL
;
3274 AioContext
*aio_context
;
3275 QDict
*options
= NULL
;
3276 Error
*local_err
= NULL
;
3279 bool set_backing_hd
= false;
3281 if (!backup
->has_speed
) {
3284 if (!backup
->has_on_source_error
) {
3285 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3287 if (!backup
->has_on_target_error
) {
3288 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3290 if (!backup
->has_mode
) {
3291 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3293 if (!backup
->has_job_id
) {
3294 backup
->job_id
= NULL
;
3296 if (!backup
->has_compress
) {
3297 backup
->compress
= false;
3300 bs
= qmp_get_root_bs(backup
->device
, errp
);
3305 aio_context
= bdrv_get_aio_context(bs
);
3306 aio_context_acquire(aio_context
);
3308 if (!backup
->has_format
) {
3309 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3310 NULL
: (char*) bs
->drv
->format_name
;
3313 /* Early check to avoid creating target */
3314 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3318 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3320 /* See if we have a backing HD we can use to create our new image
3322 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3323 source
= backing_bs(bs
);
3325 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3328 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3330 flags
|= BDRV_O_NO_BACKING
;
3331 set_backing_hd
= true;
3334 size
= bdrv_getlength(bs
);
3336 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3340 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3341 assert(backup
->format
);
3343 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3344 source
->drv
->format_name
, NULL
,
3345 size
, flags
, false, &local_err
);
3347 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3348 size
, flags
, false, &local_err
);
3353 error_propagate(errp
, local_err
);
3357 if (backup
->format
) {
3359 options
= qdict_new();
3361 qdict_put_str(options
, "driver", backup
->format
);
3364 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3369 bdrv_set_aio_context(target_bs
, aio_context
);
3371 if (set_backing_hd
) {
3372 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3374 bdrv_unref(target_bs
);
3379 if (backup
->has_bitmap
) {
3380 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3382 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3383 bdrv_unref(target_bs
);
3386 if (bdrv_dirty_bitmap_qmp_locked(bmap
)) {
3388 "Bitmap '%s' is currently locked and cannot be used for "
3389 "backup", backup
->bitmap
);
3394 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3395 backup
->sync
, bmap
, backup
->compress
,
3396 backup
->on_source_error
, backup
->on_target_error
,
3397 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3398 bdrv_unref(target_bs
);
3399 if (local_err
!= NULL
) {
3400 error_propagate(errp
, local_err
);
3405 aio_context_release(aio_context
);
3409 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3413 job
= do_drive_backup(arg
, NULL
, errp
);
3415 block_job_start(job
);
3419 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3421 return bdrv_named_nodes_list(errp
);
3424 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
3427 BlockDriverState
*bs
;
3428 BlockDriverState
*target_bs
;
3429 Error
*local_err
= NULL
;
3430 AioContext
*aio_context
;
3431 BlockJob
*job
= NULL
;
3433 if (!backup
->has_speed
) {
3436 if (!backup
->has_on_source_error
) {
3437 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3439 if (!backup
->has_on_target_error
) {
3440 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3442 if (!backup
->has_job_id
) {
3443 backup
->job_id
= NULL
;
3445 if (!backup
->has_compress
) {
3446 backup
->compress
= false;
3449 bs
= qmp_get_root_bs(backup
->device
, errp
);
3454 aio_context
= bdrv_get_aio_context(bs
);
3455 aio_context_acquire(aio_context
);
3457 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3462 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3463 if (!bdrv_has_blk(target_bs
)) {
3464 /* The target BDS is not attached, we can safely move it to another
3466 bdrv_set_aio_context(target_bs
, aio_context
);
3468 error_setg(errp
, "Target is attached to a different thread from "
3473 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3474 backup
->sync
, NULL
, backup
->compress
,
3475 backup
->on_source_error
, backup
->on_target_error
,
3476 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3477 if (local_err
!= NULL
) {
3478 error_propagate(errp
, local_err
);
3481 aio_context_release(aio_context
);
3485 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3488 job
= do_blockdev_backup(arg
, NULL
, errp
);
3490 block_job_start(job
);
3494 /* Parameter check and block job starting for drive mirroring.
3495 * Caller should hold @device and @target's aio context (must be the same).
3497 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3498 BlockDriverState
*target
,
3499 bool has_replaces
, const char *replaces
,
3500 enum MirrorSyncMode sync
,
3501 BlockMirrorBackingMode backing_mode
,
3502 bool has_speed
, int64_t speed
,
3503 bool has_granularity
, uint32_t granularity
,
3504 bool has_buf_size
, int64_t buf_size
,
3505 bool has_on_source_error
,
3506 BlockdevOnError on_source_error
,
3507 bool has_on_target_error
,
3508 BlockdevOnError on_target_error
,
3509 bool has_unmap
, bool unmap
,
3510 bool has_filter_node_name
,
3511 const char *filter_node_name
,
3518 if (!has_on_source_error
) {
3519 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3521 if (!has_on_target_error
) {
3522 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3524 if (!has_granularity
) {
3527 if (!has_buf_size
) {
3533 if (!has_filter_node_name
) {
3534 filter_node_name
= NULL
;
3537 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3538 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3539 "a value in range [512B, 64MB]");
3542 if (granularity
& (granularity
- 1)) {
3543 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3548 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3551 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3555 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3556 sync
= MIRROR_SYNC_MODE_FULL
;
3559 /* pass the node name to replace to mirror start since it's loose coupling
3560 * and will allow to check whether the node still exist at mirror completion
3562 mirror_start(job_id
, bs
, target
,
3563 has_replaces
? replaces
: NULL
,
3564 speed
, granularity
, buf_size
, sync
, backing_mode
,
3565 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3569 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3571 BlockDriverState
*bs
;
3572 BlockDriverState
*source
, *target_bs
;
3573 AioContext
*aio_context
;
3574 BlockMirrorBackingMode backing_mode
;
3575 Error
*local_err
= NULL
;
3576 QDict
*options
= NULL
;
3579 const char *format
= arg
->format
;
3581 bs
= qmp_get_root_bs(arg
->device
, errp
);
3586 /* Early check to avoid creating target */
3587 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3591 aio_context
= bdrv_get_aio_context(bs
);
3592 aio_context_acquire(aio_context
);
3594 if (!arg
->has_mode
) {
3595 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3598 if (!arg
->has_format
) {
3599 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3600 ? NULL
: bs
->drv
->format_name
);
3603 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3604 source
= backing_bs(bs
);
3605 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3606 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3608 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3612 size
= bdrv_getlength(bs
);
3614 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3618 if (arg
->has_replaces
) {
3619 BlockDriverState
*to_replace_bs
;
3620 AioContext
*replace_aio_context
;
3621 int64_t replace_size
;
3623 if (!arg
->has_node_name
) {
3624 error_setg(errp
, "a node-name must be provided when replacing a"
3625 " named node of the graph");
3629 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3631 if (!to_replace_bs
) {
3632 error_propagate(errp
, local_err
);
3636 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3637 aio_context_acquire(replace_aio_context
);
3638 replace_size
= bdrv_getlength(to_replace_bs
);
3639 aio_context_release(replace_aio_context
);
3641 if (size
!= replace_size
) {
3642 error_setg(errp
, "cannot replace image with a mirror image of "
3648 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3649 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3651 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3654 /* Don't open backing image in create() */
3655 flags
|= BDRV_O_NO_BACKING
;
3657 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3658 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3660 /* create new image w/o backing file */
3662 bdrv_img_create(arg
->target
, format
,
3663 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3665 switch (arg
->mode
) {
3666 case NEW_IMAGE_MODE_EXISTING
:
3668 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3669 /* create new image with backing file */
3670 bdrv_img_create(arg
->target
, format
,
3672 source
->drv
->format_name
,
3673 NULL
, size
, flags
, false, &local_err
);
3681 error_propagate(errp
, local_err
);
3685 options
= qdict_new();
3686 if (arg
->has_node_name
) {
3687 qdict_put_str(options
, "node-name", arg
->node_name
);
3690 qdict_put_str(options
, "driver", format
);
3693 /* Mirroring takes care of copy-on-write using the source's backing
3696 target_bs
= bdrv_open(arg
->target
, NULL
, options
, flags
, errp
);
3701 bdrv_set_aio_context(target_bs
, aio_context
);
3703 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3704 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3705 backing_mode
, arg
->has_speed
, arg
->speed
,
3706 arg
->has_granularity
, arg
->granularity
,
3707 arg
->has_buf_size
, arg
->buf_size
,
3708 arg
->has_on_source_error
, arg
->on_source_error
,
3709 arg
->has_on_target_error
, arg
->on_target_error
,
3710 arg
->has_unmap
, arg
->unmap
,
3713 bdrv_unref(target_bs
);
3714 error_propagate(errp
, local_err
);
3716 aio_context_release(aio_context
);
3719 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3720 const char *device
, const char *target
,
3721 bool has_replaces
, const char *replaces
,
3722 MirrorSyncMode sync
,
3723 bool has_speed
, int64_t speed
,
3724 bool has_granularity
, uint32_t granularity
,
3725 bool has_buf_size
, int64_t buf_size
,
3726 bool has_on_source_error
,
3727 BlockdevOnError on_source_error
,
3728 bool has_on_target_error
,
3729 BlockdevOnError on_target_error
,
3730 bool has_filter_node_name
,
3731 const char *filter_node_name
,
3734 BlockDriverState
*bs
;
3735 BlockDriverState
*target_bs
;
3736 AioContext
*aio_context
;
3737 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3738 Error
*local_err
= NULL
;
3740 bs
= qmp_get_root_bs(device
, errp
);
3745 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3750 aio_context
= bdrv_get_aio_context(bs
);
3751 aio_context_acquire(aio_context
);
3753 bdrv_set_aio_context(target_bs
, aio_context
);
3755 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3756 has_replaces
, replaces
, sync
, backing_mode
,
3758 has_granularity
, granularity
,
3759 has_buf_size
, buf_size
,
3760 has_on_source_error
, on_source_error
,
3761 has_on_target_error
, on_target_error
,
3763 has_filter_node_name
, filter_node_name
,
3765 error_propagate(errp
, local_err
);
3767 aio_context_release(aio_context
);
3770 /* Get a block job using its ID and acquire its AioContext */
3771 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3778 *aio_context
= NULL
;
3780 job
= block_job_get(id
);
3783 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3784 "Block job '%s' not found", id
);
3788 *aio_context
= blk_get_aio_context(job
->blk
);
3789 aio_context_acquire(*aio_context
);
3794 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3796 AioContext
*aio_context
;
3797 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3803 block_job_set_speed(job
, speed
, errp
);
3804 aio_context_release(aio_context
);
3807 void qmp_block_job_cancel(const char *device
,
3808 bool has_force
, bool force
, Error
**errp
)
3810 AioContext
*aio_context
;
3811 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3821 if (block_job_user_paused(job
) && !force
) {
3822 error_setg(errp
, "The block job for device '%s' is currently paused",
3827 trace_qmp_block_job_cancel(job
);
3828 block_job_cancel(job
);
3830 aio_context_release(aio_context
);
3833 void qmp_block_job_pause(const char *device
, Error
**errp
)
3835 AioContext
*aio_context
;
3836 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3838 if (!job
|| block_job_user_paused(job
)) {
3842 trace_qmp_block_job_pause(job
);
3843 block_job_user_pause(job
);
3844 aio_context_release(aio_context
);
3847 void qmp_block_job_resume(const char *device
, Error
**errp
)
3849 AioContext
*aio_context
;
3850 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3852 if (!job
|| !block_job_user_paused(job
)) {
3856 trace_qmp_block_job_resume(job
);
3857 block_job_user_resume(job
);
3858 aio_context_release(aio_context
);
3861 void qmp_block_job_complete(const char *device
, Error
**errp
)
3863 AioContext
*aio_context
;
3864 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3870 trace_qmp_block_job_complete(job
);
3871 block_job_complete(job
, errp
);
3872 aio_context_release(aio_context
);
3875 void qmp_change_backing_file(const char *device
,
3876 const char *image_node_name
,
3877 const char *backing_file
,
3880 BlockDriverState
*bs
= NULL
;
3881 AioContext
*aio_context
;
3882 BlockDriverState
*image_bs
= NULL
;
3883 Error
*local_err
= NULL
;
3888 bs
= qmp_get_root_bs(device
, errp
);
3893 aio_context
= bdrv_get_aio_context(bs
);
3894 aio_context_acquire(aio_context
);
3896 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3898 error_propagate(errp
, local_err
);
3903 error_setg(errp
, "image file not found");
3907 if (bdrv_find_base(image_bs
) == image_bs
) {
3908 error_setg(errp
, "not allowing backing file change on an image "
3909 "without a backing file");
3913 /* even though we are not necessarily operating on bs, we need it to
3914 * determine if block ops are currently prohibited on the chain */
3915 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3919 /* final sanity check */
3920 if (!bdrv_chain_contains(bs
, image_bs
)) {
3921 error_setg(errp
, "'%s' and image file are not in the same chain",
3926 /* if not r/w, reopen to make r/w */
3927 open_flags
= image_bs
->open_flags
;
3928 ro
= bdrv_is_read_only(image_bs
);
3931 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3933 error_propagate(errp
, local_err
);
3938 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3939 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3942 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3944 /* don't exit here, so we can try to restore open flags if
3949 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3950 error_propagate(errp
, local_err
);
3954 aio_context_release(aio_context
);
3957 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3961 Error
*local_err
= NULL
;
3963 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3968 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3970 if (!qdict_get_try_str(qdict
, "node-name")) {
3972 error_report("'node-name' needs to be specified");
3976 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3978 error_report_err(local_err
);
3982 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3985 qemu_opts_del(opts
);
3988 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3990 BlockDriverState
*bs
;
3992 Visitor
*v
= qobject_output_visitor_new(&obj
);
3994 const QDictEntry
*ent
;
3995 Error
*local_err
= NULL
;
3997 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3999 error_propagate(errp
, local_err
);
4003 visit_complete(v
, &obj
);
4004 qdict
= qobject_to_qdict(obj
);
4006 qdict_flatten(qdict
);
4009 * Rewrite "backing": null to "backing": ""
4010 * TODO Rewrite "" to null instead, and perhaps not even here
4012 for (ent
= qdict_first(qdict
); ent
; ent
= qdict_next(qdict
, ent
)) {
4013 char *dot
= strrchr(ent
->key
, '.');
4015 if (!strcmp(dot
? dot
+ 1 : ent
->key
, "backing")
4016 && qobject_type(ent
->value
) == QTYPE_QNULL
) {
4017 qdict_put(qdict
, ent
->key
, qstring_new());
4021 if (!qdict_get_try_str(qdict
, "node-name")) {
4022 error_setg(errp
, "'node-name' must be specified for the root node");
4026 bs
= bds_tree_init(qdict
, errp
);
4031 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
4037 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
4039 AioContext
*aio_context
;
4040 BlockDriverState
*bs
;
4042 bs
= bdrv_find_node(node_name
);
4044 error_setg(errp
, "Cannot find node %s", node_name
);
4047 if (bdrv_has_blk(bs
)) {
4048 error_setg(errp
, "Node %s is in use", node_name
);
4051 aio_context
= bdrv_get_aio_context(bs
);
4052 aio_context_acquire(aio_context
);
4054 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
4058 if (!bs
->monitor_list
.tqe_prev
) {
4059 error_setg(errp
, "Node %s is not owned by the monitor",
4064 if (bs
->refcnt
> 1) {
4065 error_setg(errp
, "Block device %s is in use",
4066 bdrv_get_device_or_node_name(bs
));
4070 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
4074 aio_context_release(aio_context
);
4077 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
4078 const char *child_name
)
4082 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
4083 if (strcmp(child
->name
, child_name
) == 0) {
4091 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
4092 const char *child
, bool has_node
,
4093 const char *node
, Error
**errp
)
4095 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
4098 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
4103 if (has_child
== has_node
) {
4105 error_setg(errp
, "The parameters child and node are in conflict");
4107 error_setg(errp
, "Either child or node must be specified");
4113 p_child
= bdrv_find_child(parent_bs
, child
);
4115 error_setg(errp
, "Node '%s' does not have child '%s'",
4119 bdrv_del_child(parent_bs
, p_child
, errp
);
4123 new_bs
= bdrv_find_node(node
);
4125 error_setg(errp
, "Node '%s' not found", node
);
4128 bdrv_add_child(parent_bs
, new_bs
, errp
);
4132 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
4134 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
4137 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
4138 BlockJobInfoList
*elem
;
4139 AioContext
*aio_context
;
4141 if (block_job_is_internal(job
)) {
4144 elem
= g_new0(BlockJobInfoList
, 1);
4145 aio_context
= blk_get_aio_context(job
->blk
);
4146 aio_context_acquire(aio_context
);
4147 elem
->value
= block_job_query(job
, errp
);
4148 aio_context_release(aio_context
);
4151 qapi_free_BlockJobInfoList(head
);
4155 p_next
= &elem
->next
;
4161 void qmp_x_blockdev_set_iothread(const char *node_name
, StrOrNull
*iothread
,
4162 bool has_force
, bool force
, Error
**errp
)
4164 AioContext
*old_context
;
4165 AioContext
*new_context
;
4166 BlockDriverState
*bs
;
4168 bs
= bdrv_find_node(node_name
);
4170 error_setg(errp
, "Cannot find node %s", node_name
);
4174 /* Protects against accidents. */
4175 if (!(has_force
&& force
) && bdrv_has_blk(bs
)) {
4176 error_setg(errp
, "Node %s is associated with a BlockBackend and could "
4177 "be in use (use force=true to override this check)",
4182 if (iothread
->type
== QTYPE_QSTRING
) {
4183 IOThread
*obj
= iothread_by_id(iothread
->u
.s
);
4185 error_setg(errp
, "Cannot find iothread %s", iothread
->u
.s
);
4189 new_context
= iothread_get_aio_context(obj
);
4191 new_context
= qemu_get_aio_context();
4194 old_context
= bdrv_get_aio_context(bs
);
4195 aio_context_acquire(old_context
);
4197 bdrv_set_aio_context(bs
, new_context
);
4199 aio_context_release(old_context
);
4202 QemuOptsList qemu_common_drive_opts
= {
4204 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4208 .type
= QEMU_OPT_BOOL
,
4209 .help
= "enable/disable snapshot mode",
4212 .type
= QEMU_OPT_STRING
,
4213 .help
= "host AIO implementation (threads, native)",
4215 .name
= BDRV_OPT_CACHE_WB
,
4216 .type
= QEMU_OPT_BOOL
,
4217 .help
= "Enable writeback mode",
4220 .type
= QEMU_OPT_STRING
,
4221 .help
= "disk format (raw, qcow2, ...)",
4224 .type
= QEMU_OPT_STRING
,
4225 .help
= "read error action",
4228 .type
= QEMU_OPT_STRING
,
4229 .help
= "write error action",
4231 .name
= BDRV_OPT_READ_ONLY
,
4232 .type
= QEMU_OPT_BOOL
,
4233 .help
= "open drive file as read-only",
4239 .name
= "throttling.group",
4240 .type
= QEMU_OPT_STRING
,
4241 .help
= "name of the block throttling group",
4243 .name
= "copy-on-read",
4244 .type
= QEMU_OPT_BOOL
,
4245 .help
= "copy read data from backing file into image file",
4247 .name
= "detect-zeroes",
4248 .type
= QEMU_OPT_STRING
,
4249 .help
= "try to optimize zero writes (off, on, unmap)",
4251 .name
= "stats-account-invalid",
4252 .type
= QEMU_OPT_BOOL
,
4253 .help
= "whether to account for invalid I/O operations "
4254 "in the statistics",
4256 .name
= "stats-account-failed",
4257 .type
= QEMU_OPT_BOOL
,
4258 .help
= "whether to account for failed I/O operations "
4259 "in the statistics",
4261 { /* end of list */ }
4265 QemuOptsList qemu_drive_opts
= {
4267 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4270 * no elements => accept any params
4271 * validation will happen later
4273 { /* end of list */ }