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 "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "block/throttle-groups.h"
38 #include "monitor/monitor.h"
39 #include "qemu/error-report.h"
40 #include "qemu/option.h"
41 #include "qemu/config-file.h"
42 #include "qapi/qmp/types.h"
43 #include "qapi-visit.h"
44 #include "qapi/qmp/qerror.h"
45 #include "qapi/qmp-output-visitor.h"
46 #include "qapi/util.h"
47 #include "sysemu/sysemu.h"
48 #include "block/block_int.h"
49 #include "qmp-commands.h"
51 #include "sysemu/arch_init.h"
53 static const char *const if_name
[IF_COUNT
] = {
57 [IF_FLOPPY
] = "floppy",
58 [IF_PFLASH
] = "pflash",
61 [IF_VIRTIO
] = "virtio",
65 static int if_max_devs
[IF_COUNT
] = {
67 * Do not change these numbers! They govern how drive option
68 * index maps to unit and bus. That mapping is ABI.
70 * All controllers used to imlement if=T drives need to support
71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
72 * Otherwise, some index values map to "impossible" bus, unit
75 * For instance, if you change [IF_SCSI] to 255, -drive
76 * if=scsi,index=12 no longer means bus=1,unit=5, but
77 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
78 * the drive can't be set up. Regression.
85 * Boards may call this to offer board-by-board overrides
86 * of the default, global values.
88 void override_max_devs(BlockInterfaceType type
, int max_devs
)
97 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
98 dinfo
= blk_legacy_dinfo(blk
);
99 if (dinfo
->type
== type
) {
100 fprintf(stderr
, "Cannot override units-per-bus property of"
101 " the %s interface, because a drive of that type has"
102 " already been added.\n", if_name
[type
]);
103 g_assert_not_reached();
107 if_max_devs
[type
] = max_devs
;
111 * We automatically delete the drive when a device using it gets
112 * unplugged. Questionable feature, but we can't just drop it.
113 * Device models call blockdev_mark_auto_del() to schedule the
114 * automatic deletion, and generic qdev code calls blockdev_auto_del()
115 * when deletion is actually safe.
117 void blockdev_mark_auto_del(BlockBackend
*blk
)
119 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
120 BlockDriverState
*bs
= blk_bs(blk
);
121 AioContext
*aio_context
;
128 aio_context
= bdrv_get_aio_context(bs
);
129 aio_context_acquire(aio_context
);
132 block_job_cancel(bs
->job
);
135 aio_context_release(aio_context
);
141 void blockdev_auto_del(BlockBackend
*blk
)
143 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
145 if (dinfo
&& dinfo
->auto_del
) {
151 * Returns the current mapping of how many units per bus
152 * a particular interface can support.
154 * A positive integer indicates n units per bus.
155 * 0 implies the mapping has not been established.
156 * -1 indicates an invalid BlockInterfaceType was given.
158 int drive_get_max_devs(BlockInterfaceType type
)
160 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
161 return if_max_devs
[type
];
167 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
169 int max_devs
= if_max_devs
[type
];
170 return max_devs
? index
/ max_devs
: 0;
173 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
175 int max_devs
= if_max_devs
[type
];
176 return max_devs
? index
% max_devs
: index
;
179 QemuOpts
*drive_def(const char *optstr
)
181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
184 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
189 opts
= drive_def(optstr
);
193 if (type
!= IF_DEFAULT
) {
194 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
197 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
200 qemu_opt_set(opts
, "file", file
, &error_abort
);
204 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
209 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
210 dinfo
= blk_legacy_dinfo(blk
);
211 if (dinfo
&& dinfo
->type
== type
212 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
220 bool drive_check_orphaned(void)
226 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
227 dinfo
= blk_legacy_dinfo(blk
);
228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
229 /* Unless this is a default drive, this may be an oversight. */
230 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
231 dinfo
->type
!= IF_NONE
) {
232 fprintf(stderr
, "Warning: Orphaned drive without device: "
233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
234 blk_name(blk
), blk_bs(blk
) ? blk_bs(blk
)->filename
: "",
235 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
243 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
245 return drive_get(type
,
246 drive_index_to_bus_id(type
, index
),
247 drive_index_to_unit_id(type
, index
));
250 int drive_get_max_bus(BlockInterfaceType type
)
257 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
258 dinfo
= blk_legacy_dinfo(blk
);
259 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
260 max_bus
= dinfo
->bus
;
266 /* Get a block device. This should only be used for single-drive devices
267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
269 DriveInfo
*drive_get_next(BlockInterfaceType type
)
271 static int next_block_unit
[IF_COUNT
];
273 return drive_get(type
, 0, next_block_unit
[type
]++);
276 static void bdrv_format_print(void *opaque
, const char *name
)
278 error_printf(" %s", name
);
283 BlockDriverState
*bs
;
286 static void bdrv_put_ref_bh(void *opaque
)
288 BDRVPutRefBH
*s
= opaque
;
291 qemu_bh_delete(s
->bh
);
296 * Release a BDS reference in a BH
298 * It is not safe to use bdrv_unref() from a callback function when the callers
299 * still need the BlockDriverState. In such cases we schedule a BH to release
302 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
306 s
= g_new(BDRVPutRefBH
, 1);
307 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
309 qemu_bh_schedule(s
->bh
);
312 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
314 if (!strcmp(buf
, "ignore")) {
315 return BLOCKDEV_ON_ERROR_IGNORE
;
316 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
317 return BLOCKDEV_ON_ERROR_ENOSPC
;
318 } else if (!strcmp(buf
, "stop")) {
319 return BLOCKDEV_ON_ERROR_STOP
;
320 } else if (!strcmp(buf
, "report")) {
321 return BLOCKDEV_ON_ERROR_REPORT
;
323 error_setg(errp
, "'%s' invalid %s error action",
324 buf
, is_read
? "read" : "write");
329 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
331 if (throttle_conflicting(cfg
)) {
332 error_setg(errp
, "bps/iops/max total values and read/write values"
333 " cannot be used at the same time");
337 if (!throttle_is_valid(cfg
)) {
338 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
342 if (throttle_max_is_missing_limit(cfg
)) {
343 error_setg(errp
, "bps_max/iops_max require corresponding"
351 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
353 /* All parameters but @opts are optional and may be set to NULL. */
354 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
355 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
356 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
359 Error
*local_error
= NULL
;
363 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
364 *bdrv_flags
|= BDRV_O_RDWR
;
366 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
367 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
370 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
371 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
372 error_setg(errp
, "Invalid discard option");
377 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
378 *bdrv_flags
|= BDRV_O_CACHE_WB
;
380 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
381 *bdrv_flags
|= BDRV_O_NOCACHE
;
383 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
384 *bdrv_flags
|= BDRV_O_NO_FLUSH
;
387 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
388 if (!strcmp(aio
, "native")) {
389 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
390 } else if (!strcmp(aio
, "threads")) {
391 /* this is the default */
393 error_setg(errp
, "invalid aio option");
399 /* disk I/O throttling */
400 if (throttling_group
) {
401 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
405 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
406 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
407 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
408 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
409 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
410 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
411 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
412 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
413 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
414 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
415 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
416 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
417 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
419 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
420 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
421 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
422 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
423 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
424 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
425 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
426 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
427 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
428 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
429 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
430 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
432 throttle_cfg
->op_size
=
433 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
435 if (!check_throttle_config(throttle_cfg
, errp
)) {
442 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
443 qemu_opt_get(opts
, "detect-zeroes"),
444 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
445 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
448 error_propagate(errp
, local_error
);
453 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
454 !(*bdrv_flags
& BDRV_O_UNMAP
))
456 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
457 "without setting discard operation to unmap");
463 /* Takes the ownership of bs_opts */
464 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
469 int on_read_error
, on_write_error
;
471 BlockDriverState
*bs
;
477 bool has_driver_specific_opts
;
478 BlockdevDetectZeroesOptions detect_zeroes
=
479 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
480 const char *throttling_group
= NULL
;
482 /* Check common options by copying from bs_opts to opts, all other options
483 * stay in bs_opts for processing by bdrv_open(). */
484 id
= qdict_get_try_str(bs_opts
, "id");
485 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
487 error_propagate(errp
, error
);
491 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
493 error_propagate(errp
, error
);
498 qdict_del(bs_opts
, "id");
501 has_driver_specific_opts
= !!qdict_size(bs_opts
);
503 /* extract parameters */
504 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
506 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
507 &detect_zeroes
, &error
);
509 error_propagate(errp
, error
);
513 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
514 if (is_help_option(buf
)) {
515 error_printf("Supported formats:");
516 bdrv_iterate_format(bdrv_format_print
, NULL
);
521 if (qdict_haskey(bs_opts
, "driver")) {
522 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
525 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
528 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
529 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
530 on_write_error
= parse_block_error_action(buf
, 0, &error
);
532 error_propagate(errp
, error
);
537 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
538 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
539 on_read_error
= parse_block_error_action(buf
, 1, &error
);
541 error_propagate(errp
, error
);
547 /* always use cache=unsafe with snapshot */
548 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
549 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
553 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
554 BlockBackendRootState
*blk_rs
;
556 blk
= blk_new(qemu_opts_id(opts
), errp
);
561 blk_rs
= blk_get_root_state(blk
);
562 blk_rs
->open_flags
= bdrv_flags
;
563 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
564 blk_rs
->detect_zeroes
= detect_zeroes
;
566 if (throttle_enabled(&cfg
)) {
567 if (!throttling_group
) {
568 throttling_group
= blk_name(blk
);
570 blk_rs
->throttle_group
= g_strdup(throttling_group
);
571 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
572 blk_rs
->throttle_state
->cfg
= cfg
;
577 if (file
&& !*file
) {
581 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
588 bs
->detect_zeroes
= detect_zeroes
;
590 /* disk I/O throttling */
591 if (throttle_enabled(&cfg
)) {
592 if (!throttling_group
) {
593 throttling_group
= blk_name(blk
);
595 bdrv_io_limits_enable(bs
, throttling_group
);
596 bdrv_set_io_limits(bs
, &cfg
);
599 if (bdrv_key_required(bs
)) {
604 blk_set_on_error(blk
, on_read_error
, on_write_error
);
617 static QemuOptsList qemu_root_bds_opts
;
619 /* Takes the ownership of bs_opts */
620 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
622 BlockDriverState
*bs
;
624 Error
*local_error
= NULL
;
625 BlockdevDetectZeroesOptions detect_zeroes
;
629 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
634 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
636 error_propagate(errp
, local_error
);
640 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
641 &detect_zeroes
, &local_error
);
643 error_propagate(errp
, local_error
);
648 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
650 goto fail_no_bs_opts
;
653 bs
->detect_zeroes
= detect_zeroes
;
665 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
670 value
= qemu_opt_get(opts
, from
);
672 if (qemu_opt_find(opts
, to
)) {
673 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
674 "same time", to
, from
);
679 /* rename all items in opts */
680 while ((value
= qemu_opt_get(opts
, from
))) {
681 qemu_opt_set(opts
, to
, value
, &error_abort
);
682 qemu_opt_unset(opts
, from
);
686 QemuOptsList qemu_legacy_drive_opts
= {
688 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
692 .type
= QEMU_OPT_NUMBER
,
693 .help
= "bus number",
696 .type
= QEMU_OPT_NUMBER
,
697 .help
= "unit number (i.e. lun for scsi)",
700 .type
= QEMU_OPT_NUMBER
,
701 .help
= "index number",
704 .type
= QEMU_OPT_STRING
,
705 .help
= "media type (disk, cdrom)",
708 .type
= QEMU_OPT_STRING
,
709 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
712 .type
= QEMU_OPT_NUMBER
,
713 .help
= "number of cylinders (ide disk geometry)",
716 .type
= QEMU_OPT_NUMBER
,
717 .help
= "number of heads (ide disk geometry)",
720 .type
= QEMU_OPT_NUMBER
,
721 .help
= "number of sectors (ide disk geometry)",
724 .type
= QEMU_OPT_STRING
,
725 .help
= "chs translation (auto, lba, none)",
728 .type
= QEMU_OPT_BOOL
,
729 .help
= "(deprecated, ignored)",
732 .type
= QEMU_OPT_STRING
,
733 .help
= "pci address (virtio only)",
736 .type
= QEMU_OPT_STRING
,
737 .help
= "disk serial number",
740 .type
= QEMU_OPT_STRING
,
744 /* Options that are passed on, but have special semantics with -drive */
747 .type
= QEMU_OPT_BOOL
,
748 .help
= "open drive file as read-only",
751 .type
= QEMU_OPT_STRING
,
752 .help
= "read error action",
755 .type
= QEMU_OPT_STRING
,
756 .help
= "write error action",
758 .name
= "copy-on-read",
759 .type
= QEMU_OPT_BOOL
,
760 .help
= "copy read data from backing file into image file",
763 { /* end of list */ }
767 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
771 DriveInfo
*dinfo
= NULL
;
773 QemuOpts
*legacy_opts
;
774 DriveMediaType media
= MEDIA_DISK
;
775 BlockInterfaceType type
;
776 int cyls
, heads
, secs
, translation
;
777 int max_devs
, bus_id
, unit_id
, index
;
779 const char *werror
, *rerror
;
780 bool read_only
= false;
783 const char *filename
;
784 Error
*local_err
= NULL
;
787 /* Change legacy command line options into QMP ones */
788 static const struct {
792 { "iops", "throttling.iops-total" },
793 { "iops_rd", "throttling.iops-read" },
794 { "iops_wr", "throttling.iops-write" },
796 { "bps", "throttling.bps-total" },
797 { "bps_rd", "throttling.bps-read" },
798 { "bps_wr", "throttling.bps-write" },
800 { "iops_max", "throttling.iops-total-max" },
801 { "iops_rd_max", "throttling.iops-read-max" },
802 { "iops_wr_max", "throttling.iops-write-max" },
804 { "bps_max", "throttling.bps-total-max" },
805 { "bps_rd_max", "throttling.bps-read-max" },
806 { "bps_wr_max", "throttling.bps-write-max" },
808 { "iops_size", "throttling.iops-size" },
810 { "group", "throttling.group" },
812 { "readonly", "read-only" },
815 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
816 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
819 error_report_err(local_err
);
824 value
= qemu_opt_get(all_opts
, "cache");
828 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
829 error_report("invalid cache option");
833 /* Specific options take precedence */
834 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
835 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
836 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
838 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
839 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
840 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
842 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
843 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
844 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
846 qemu_opt_unset(all_opts
, "cache");
849 /* Get a QDict for processing the options */
850 bs_opts
= qdict_new();
851 qemu_opts_to_qdict(all_opts
, bs_opts
);
853 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
855 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
857 error_report_err(local_err
);
861 /* Deprecated option boot=[on|off] */
862 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
863 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
864 "ignored. Future versions will reject this parameter. Please "
865 "update your scripts.\n");
869 value
= qemu_opt_get(legacy_opts
, "media");
871 if (!strcmp(value
, "disk")) {
873 } else if (!strcmp(value
, "cdrom")) {
877 error_report("'%s' invalid media", value
);
882 /* copy-on-read is disabled with a warning for read-only devices */
883 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
884 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
886 if (read_only
&& copy_on_read
) {
887 error_report("warning: disabling copy-on-read on read-only drive");
888 copy_on_read
= false;
891 qdict_put(bs_opts
, "read-only",
892 qstring_from_str(read_only
? "on" : "off"));
893 qdict_put(bs_opts
, "copy-on-read",
894 qstring_from_str(copy_on_read
? "on" :"off"));
896 /* Controller type */
897 value
= qemu_opt_get(legacy_opts
, "if");
900 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
903 if (type
== IF_COUNT
) {
904 error_report("unsupported bus type '%s'", value
);
908 type
= block_default_type
;
912 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
913 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
914 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
916 if (cyls
|| heads
|| secs
) {
918 error_report("invalid physical cyls number");
922 error_report("invalid physical heads number");
926 error_report("invalid physical secs number");
931 translation
= BIOS_ATA_TRANSLATION_AUTO
;
932 value
= qemu_opt_get(legacy_opts
, "trans");
935 error_report("'%s' trans must be used with cyls, heads and secs",
939 if (!strcmp(value
, "none")) {
940 translation
= BIOS_ATA_TRANSLATION_NONE
;
941 } else if (!strcmp(value
, "lba")) {
942 translation
= BIOS_ATA_TRANSLATION_LBA
;
943 } else if (!strcmp(value
, "large")) {
944 translation
= BIOS_ATA_TRANSLATION_LARGE
;
945 } else if (!strcmp(value
, "rechs")) {
946 translation
= BIOS_ATA_TRANSLATION_RECHS
;
947 } else if (!strcmp(value
, "auto")) {
948 translation
= BIOS_ATA_TRANSLATION_AUTO
;
950 error_report("'%s' invalid translation type", value
);
955 if (media
== MEDIA_CDROM
) {
956 if (cyls
|| secs
|| heads
) {
957 error_report("CHS can't be set with media=cdrom");
962 /* Device address specified by bus/unit or index.
963 * If none was specified, try to find the first free one. */
964 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
965 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
966 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
968 max_devs
= if_max_devs
[type
];
971 if (bus_id
!= 0 || unit_id
!= -1) {
972 error_report("index cannot be used with bus and unit");
975 bus_id
= drive_index_to_bus_id(type
, index
);
976 unit_id
= drive_index_to_unit_id(type
, index
);
981 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
983 if (max_devs
&& unit_id
>= max_devs
) {
990 if (max_devs
&& unit_id
>= max_devs
) {
991 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
995 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
996 error_report("drive with bus=%d, unit=%d (index=%d) exists",
997 bus_id
, unit_id
, index
);
1002 serial
= qemu_opt_get(legacy_opts
, "serial");
1004 /* no id supplied -> create one */
1005 if (qemu_opts_id(all_opts
) == NULL
) {
1007 const char *mediastr
= "";
1008 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1009 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1012 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1015 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1018 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1022 /* Add virtio block device */
1023 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1024 if (devaddr
&& type
!= IF_VIRTIO
) {
1025 error_report("addr is not supported by this bus type");
1029 if (type
== IF_VIRTIO
) {
1031 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1033 if (arch_type
== QEMU_ARCH_S390X
) {
1034 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1036 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1038 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1041 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1045 filename
= qemu_opt_get(legacy_opts
, "file");
1047 /* Check werror/rerror compatibility with if=... */
1048 werror
= qemu_opt_get(legacy_opts
, "werror");
1049 if (werror
!= NULL
) {
1050 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1052 error_report("werror is not supported by this bus type");
1055 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1058 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1059 if (rerror
!= NULL
) {
1060 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1062 error_report("rerror is not supported by this bus type");
1065 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1068 /* Actual block device init: Functionality shared with blockdev-add */
1069 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1073 error_report_err(local_err
);
1080 /* Create legacy DriveInfo */
1081 dinfo
= g_malloc0(sizeof(*dinfo
));
1082 dinfo
->opts
= all_opts
;
1085 dinfo
->heads
= heads
;
1087 dinfo
->trans
= translation
;
1090 dinfo
->bus
= bus_id
;
1091 dinfo
->unit
= unit_id
;
1092 dinfo
->devaddr
= devaddr
;
1093 dinfo
->serial
= g_strdup(serial
);
1095 blk_set_legacy_dinfo(blk
, dinfo
);
1102 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1109 qemu_opts_del(legacy_opts
);
1114 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1116 const char *device
= qdict_get_str(qdict
, "device");
1120 if (!strcmp(device
, "all")) {
1121 ret
= bdrv_commit_all();
1123 BlockDriverState
*bs
;
1124 AioContext
*aio_context
;
1126 blk
= blk_by_name(device
);
1128 monitor_printf(mon
, "Device '%s' not found\n", device
);
1131 if (!blk_is_available(blk
)) {
1132 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1137 aio_context
= bdrv_get_aio_context(bs
);
1138 aio_context_acquire(aio_context
);
1140 ret
= bdrv_commit(bs
);
1142 aio_context_release(aio_context
);
1145 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1150 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1153 TransactionAction action
;
1154 TransactionActionList list
;
1157 action
.u
.data
= data
;
1158 list
.value
= &action
;
1160 qmp_transaction(&list
, errp
);
1163 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1164 bool has_node_name
, const char *node_name
,
1165 const char *snapshot_file
,
1166 bool has_snapshot_node_name
,
1167 const char *snapshot_node_name
,
1168 bool has_format
, const char *format
,
1169 bool has_mode
, NewImageMode mode
, Error
**errp
)
1171 BlockdevSnapshotSync snapshot
= {
1172 .has_device
= has_device
,
1173 .device
= (char *) device
,
1174 .has_node_name
= has_node_name
,
1175 .node_name
= (char *) node_name
,
1176 .snapshot_file
= (char *) snapshot_file
,
1177 .has_snapshot_node_name
= has_snapshot_node_name
,
1178 .snapshot_node_name
= (char *) snapshot_node_name
,
1179 .has_format
= has_format
,
1180 .format
= (char *) format
,
1181 .has_mode
= has_mode
,
1184 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1188 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1191 BlockdevSnapshot snapshot_data
= {
1192 .node
= (char *) node
,
1193 .overlay
= (char *) overlay
1196 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1197 &snapshot_data
, errp
);
1200 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1204 BlockdevSnapshotInternal snapshot
= {
1205 .device
= (char *) device
,
1206 .name
= (char *) name
1209 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1213 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1220 BlockDriverState
*bs
;
1222 AioContext
*aio_context
;
1223 QEMUSnapshotInfo sn
;
1224 Error
*local_err
= NULL
;
1225 SnapshotInfo
*info
= NULL
;
1228 blk
= blk_by_name(device
);
1230 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1231 "Device '%s' not found", device
);
1235 aio_context
= blk_get_aio_context(blk
);
1236 aio_context_acquire(aio_context
);
1247 error_setg(errp
, "Name or id must be provided");
1248 goto out_aio_context
;
1251 if (!blk_is_available(blk
)) {
1252 error_setg(errp
, "Device '%s' has no medium", device
);
1253 goto out_aio_context
;
1257 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1258 goto out_aio_context
;
1261 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1263 error_propagate(errp
, local_err
);
1264 goto out_aio_context
;
1268 "Snapshot with id '%s' and name '%s' does not exist on "
1270 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1271 goto out_aio_context
;
1274 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1276 error_propagate(errp
, local_err
);
1277 goto out_aio_context
;
1280 aio_context_release(aio_context
);
1282 info
= g_new0(SnapshotInfo
, 1);
1283 info
->id
= g_strdup(sn
.id_str
);
1284 info
->name
= g_strdup(sn
.name
);
1285 info
->date_nsec
= sn
.date_nsec
;
1286 info
->date_sec
= sn
.date_sec
;
1287 info
->vm_state_size
= sn
.vm_state_size
;
1288 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1289 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1294 aio_context_release(aio_context
);
1299 * block_dirty_bitmap_lookup:
1300 * Return a dirty bitmap (if present), after validating
1301 * the node reference and bitmap names.
1303 * @node: The name of the BDS node to search for bitmaps
1304 * @name: The name of the bitmap to search for
1305 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1306 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1307 * @errp: Output pointer for error information. Can be NULL.
1309 * @return: A bitmap object on success, or NULL on failure.
1311 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1313 BlockDriverState
**pbs
,
1317 BlockDriverState
*bs
;
1318 BdrvDirtyBitmap
*bitmap
;
1319 AioContext
*aio_context
;
1322 error_setg(errp
, "Node cannot be NULL");
1326 error_setg(errp
, "Bitmap name cannot be NULL");
1329 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1331 error_setg(errp
, "Node '%s' not found", node
);
1335 aio_context
= bdrv_get_aio_context(bs
);
1336 aio_context_acquire(aio_context
);
1338 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1340 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1348 *paio
= aio_context
;
1350 aio_context_release(aio_context
);
1356 aio_context_release(aio_context
);
1360 /* New and old BlockDriverState structs for atomic group operations */
1362 typedef struct BlkTransactionState BlkTransactionState
;
1364 /* Only prepare() may fail. In a single transaction, only one of commit() or
1365 abort() will be called, clean() will always be called if it present. */
1366 typedef struct BdrvActionOps
{
1367 /* Size of state struct, in bytes. */
1368 size_t instance_size
;
1369 /* Prepare the work, must NOT be NULL. */
1370 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1371 /* Commit the changes, can be NULL. */
1372 void (*commit
)(BlkTransactionState
*common
);
1373 /* Abort the changes on fail, can be NULL. */
1374 void (*abort
)(BlkTransactionState
*common
);
1375 /* Clean up resource in the end, can be NULL. */
1376 void (*clean
)(BlkTransactionState
*common
);
1380 * This structure must be arranged as first member in child type, assuming
1381 * that compiler will also arrange it to the same address with parent instance.
1382 * Later it will be used in free().
1384 struct BlkTransactionState
{
1385 TransactionAction
*action
;
1386 const BdrvActionOps
*ops
;
1387 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1390 /* internal snapshot private data */
1391 typedef struct InternalSnapshotState
{
1392 BlkTransactionState common
;
1393 BlockDriverState
*bs
;
1394 AioContext
*aio_context
;
1395 QEMUSnapshotInfo sn
;
1397 } InternalSnapshotState
;
1399 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1402 Error
*local_err
= NULL
;
1406 BlockDriverState
*bs
;
1407 QEMUSnapshotInfo old_sn
, *sn
;
1410 BlockdevSnapshotInternal
*internal
;
1411 InternalSnapshotState
*state
;
1414 g_assert(common
->action
->type
==
1415 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1416 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1417 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1419 /* 1. parse input */
1420 device
= internal
->device
;
1421 name
= internal
->name
;
1423 /* 2. check for validation */
1424 blk
= blk_by_name(device
);
1426 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1427 "Device '%s' not found", device
);
1431 /* AioContext is released in .clean() */
1432 state
->aio_context
= blk_get_aio_context(blk
);
1433 aio_context_acquire(state
->aio_context
);
1435 if (!blk_is_available(blk
)) {
1436 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1442 bdrv_drained_begin(bs
);
1444 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1448 if (bdrv_is_read_only(bs
)) {
1449 error_setg(errp
, "Device '%s' is read only", device
);
1453 if (!bdrv_can_snapshot(bs
)) {
1454 error_setg(errp
, "Block format '%s' used by device '%s' "
1455 "does not support internal snapshots",
1456 bs
->drv
->format_name
, device
);
1460 if (!strlen(name
)) {
1461 error_setg(errp
, "Name is empty");
1465 /* check whether a snapshot with name exist */
1466 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1469 error_propagate(errp
, local_err
);
1473 "Snapshot with name '%s' already exists on device '%s'",
1478 /* 3. take the snapshot */
1480 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1481 qemu_gettimeofday(&tv
);
1482 sn
->date_sec
= tv
.tv_sec
;
1483 sn
->date_nsec
= tv
.tv_usec
* 1000;
1484 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1486 ret1
= bdrv_snapshot_create(bs
, sn
);
1488 error_setg_errno(errp
, -ret1
,
1489 "Failed to create snapshot '%s' on device '%s'",
1494 /* 4. succeed, mark a snapshot is created */
1495 state
->created
= true;
1498 static void internal_snapshot_abort(BlkTransactionState
*common
)
1500 InternalSnapshotState
*state
=
1501 DO_UPCAST(InternalSnapshotState
, common
, common
);
1502 BlockDriverState
*bs
= state
->bs
;
1503 QEMUSnapshotInfo
*sn
= &state
->sn
;
1504 Error
*local_error
= NULL
;
1506 if (!state
->created
) {
1510 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1511 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1512 "device '%s' in abort: %s",
1515 bdrv_get_device_name(bs
),
1516 error_get_pretty(local_error
));
1517 error_free(local_error
);
1521 static void internal_snapshot_clean(BlkTransactionState
*common
)
1523 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1526 if (state
->aio_context
) {
1528 bdrv_drained_end(state
->bs
);
1530 aio_context_release(state
->aio_context
);
1534 /* external snapshot private data */
1535 typedef struct ExternalSnapshotState
{
1536 BlkTransactionState common
;
1537 BlockDriverState
*old_bs
;
1538 BlockDriverState
*new_bs
;
1539 AioContext
*aio_context
;
1540 } ExternalSnapshotState
;
1542 static void external_snapshot_prepare(BlkTransactionState
*common
,
1546 QDict
*options
= NULL
;
1547 Error
*local_err
= NULL
;
1548 /* Device and node name of the image to generate the snapshot from */
1550 const char *node_name
;
1551 /* Reference to the new image (for 'blockdev-snapshot') */
1552 const char *snapshot_ref
;
1553 /* File name of the new image (for 'blockdev-snapshot-sync') */
1554 const char *new_image_file
;
1555 ExternalSnapshotState
*state
=
1556 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1557 TransactionAction
*action
= common
->action
;
1559 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1560 * purpose but a different set of parameters */
1561 switch (action
->type
) {
1562 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1564 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1566 node_name
= s
->node
;
1567 new_image_file
= NULL
;
1568 snapshot_ref
= s
->overlay
;
1571 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1573 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1574 device
= s
->has_device
? s
->device
: NULL
;
1575 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1576 new_image_file
= s
->snapshot_file
;
1577 snapshot_ref
= NULL
;
1581 g_assert_not_reached();
1584 /* start processing */
1585 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1586 if (!state
->old_bs
) {
1590 /* Acquire AioContext now so any threads operating on old_bs stop */
1591 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1592 aio_context_acquire(state
->aio_context
);
1593 bdrv_drained_begin(state
->old_bs
);
1595 if (!bdrv_is_inserted(state
->old_bs
)) {
1596 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1600 if (bdrv_op_is_blocked(state
->old_bs
,
1601 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1605 if (!bdrv_is_read_only(state
->old_bs
)) {
1606 if (bdrv_flush(state
->old_bs
)) {
1607 error_setg(errp
, QERR_IO_ERROR
);
1612 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1613 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1617 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1618 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1619 const char *format
= s
->has_format
? s
->format
: "qcow2";
1620 enum NewImageMode mode
;
1621 const char *snapshot_node_name
=
1622 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1624 if (node_name
&& !snapshot_node_name
) {
1625 error_setg(errp
, "New snapshot node name missing");
1629 if (snapshot_node_name
&&
1630 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1631 error_setg(errp
, "New snapshot node name already in use");
1635 flags
= state
->old_bs
->open_flags
;
1637 /* create new image w/backing file */
1638 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1639 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1640 bdrv_img_create(new_image_file
, format
,
1641 state
->old_bs
->filename
,
1642 state
->old_bs
->drv
->format_name
,
1643 NULL
, -1, flags
, &local_err
, false);
1645 error_propagate(errp
, local_err
);
1650 options
= qdict_new();
1651 if (s
->has_snapshot_node_name
) {
1652 qdict_put(options
, "node-name",
1653 qstring_from_str(snapshot_node_name
));
1655 qdict_put(options
, "driver", qstring_from_str(format
));
1657 flags
|= BDRV_O_NO_BACKING
;
1660 assert(state
->new_bs
== NULL
);
1661 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1663 /* We will manually add the backing_hd field to the bs later */
1668 if (state
->new_bs
->blk
!= NULL
) {
1669 error_setg(errp
, "The snapshot is already in use by %s",
1670 blk_name(state
->new_bs
->blk
));
1674 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1679 if (state
->new_bs
->backing
!= NULL
) {
1680 error_setg(errp
, "The snapshot already has a backing image");
1684 static void external_snapshot_commit(BlkTransactionState
*common
)
1686 ExternalSnapshotState
*state
=
1687 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1689 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1691 /* This removes our old bs and adds the new bs */
1692 bdrv_append(state
->new_bs
, state
->old_bs
);
1693 /* We don't need (or want) to use the transactional
1694 * bdrv_reopen_multiple() across all the entries at once, because we
1695 * don't want to abort all of them if one of them fails the reopen */
1696 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1700 static void external_snapshot_abort(BlkTransactionState
*common
)
1702 ExternalSnapshotState
*state
=
1703 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1704 if (state
->new_bs
) {
1705 bdrv_unref(state
->new_bs
);
1709 static void external_snapshot_clean(BlkTransactionState
*common
)
1711 ExternalSnapshotState
*state
=
1712 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1713 if (state
->aio_context
) {
1714 bdrv_drained_end(state
->old_bs
);
1715 aio_context_release(state
->aio_context
);
1719 typedef struct DriveBackupState
{
1720 BlkTransactionState common
;
1721 BlockDriverState
*bs
;
1722 AioContext
*aio_context
;
1726 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1728 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1730 DriveBackup
*backup
;
1731 Error
*local_err
= NULL
;
1733 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1734 backup
= common
->action
->u
.drive_backup
;
1736 blk
= blk_by_name(backup
->device
);
1738 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1739 "Device '%s' not found", backup
->device
);
1743 if (!blk_is_available(blk
)) {
1744 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1748 /* AioContext is released in .clean() */
1749 state
->aio_context
= blk_get_aio_context(blk
);
1750 aio_context_acquire(state
->aio_context
);
1751 bdrv_drained_begin(blk_bs(blk
));
1752 state
->bs
= blk_bs(blk
);
1754 qmp_drive_backup(backup
->device
, backup
->target
,
1755 backup
->has_format
, backup
->format
,
1757 backup
->has_mode
, backup
->mode
,
1758 backup
->has_speed
, backup
->speed
,
1759 backup
->has_bitmap
, backup
->bitmap
,
1760 backup
->has_on_source_error
, backup
->on_source_error
,
1761 backup
->has_on_target_error
, backup
->on_target_error
,
1764 error_propagate(errp
, local_err
);
1768 state
->job
= state
->bs
->job
;
1771 static void drive_backup_abort(BlkTransactionState
*common
)
1773 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1774 BlockDriverState
*bs
= state
->bs
;
1776 /* Only cancel if it's the job we started */
1777 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1778 block_job_cancel_sync(bs
->job
);
1782 static void drive_backup_clean(BlkTransactionState
*common
)
1784 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1786 if (state
->aio_context
) {
1787 bdrv_drained_end(state
->bs
);
1788 aio_context_release(state
->aio_context
);
1792 typedef struct BlockdevBackupState
{
1793 BlkTransactionState common
;
1794 BlockDriverState
*bs
;
1796 AioContext
*aio_context
;
1797 } BlockdevBackupState
;
1799 static void blockdev_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1801 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1802 BlockdevBackup
*backup
;
1803 BlockBackend
*blk
, *target
;
1804 Error
*local_err
= NULL
;
1806 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1807 backup
= common
->action
->u
.blockdev_backup
;
1809 blk
= blk_by_name(backup
->device
);
1811 error_setg(errp
, "Device '%s' not found", backup
->device
);
1815 if (!blk_is_available(blk
)) {
1816 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1820 target
= blk_by_name(backup
->target
);
1822 error_setg(errp
, "Device '%s' not found", backup
->target
);
1826 /* AioContext is released in .clean() */
1827 state
->aio_context
= blk_get_aio_context(blk
);
1828 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1829 state
->aio_context
= NULL
;
1830 error_setg(errp
, "Backup between two IO threads is not implemented");
1833 aio_context_acquire(state
->aio_context
);
1834 state
->bs
= blk_bs(blk
);
1835 bdrv_drained_begin(state
->bs
);
1837 qmp_blockdev_backup(backup
->device
, backup
->target
,
1839 backup
->has_speed
, backup
->speed
,
1840 backup
->has_on_source_error
, backup
->on_source_error
,
1841 backup
->has_on_target_error
, backup
->on_target_error
,
1844 error_propagate(errp
, local_err
);
1848 state
->job
= state
->bs
->job
;
1851 static void blockdev_backup_abort(BlkTransactionState
*common
)
1853 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1854 BlockDriverState
*bs
= state
->bs
;
1856 /* Only cancel if it's the job we started */
1857 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1858 block_job_cancel_sync(bs
->job
);
1862 static void blockdev_backup_clean(BlkTransactionState
*common
)
1864 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1866 if (state
->aio_context
) {
1867 bdrv_drained_end(state
->bs
);
1868 aio_context_release(state
->aio_context
);
1872 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1874 error_setg(errp
, "Transaction aborted using Abort action");
1877 static void abort_commit(BlkTransactionState
*common
)
1879 g_assert_not_reached(); /* this action never succeeds */
1882 static const BdrvActionOps actions
[] = {
1883 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
1884 .instance_size
= sizeof(ExternalSnapshotState
),
1885 .prepare
= external_snapshot_prepare
,
1886 .commit
= external_snapshot_commit
,
1887 .abort
= external_snapshot_abort
,
1889 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1890 .instance_size
= sizeof(ExternalSnapshotState
),
1891 .prepare
= external_snapshot_prepare
,
1892 .commit
= external_snapshot_commit
,
1893 .abort
= external_snapshot_abort
,
1894 .clean
= external_snapshot_clean
,
1896 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1897 .instance_size
= sizeof(DriveBackupState
),
1898 .prepare
= drive_backup_prepare
,
1899 .abort
= drive_backup_abort
,
1900 .clean
= drive_backup_clean
,
1902 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
1903 .instance_size
= sizeof(BlockdevBackupState
),
1904 .prepare
= blockdev_backup_prepare
,
1905 .abort
= blockdev_backup_abort
,
1906 .clean
= blockdev_backup_clean
,
1908 [TRANSACTION_ACTION_KIND_ABORT
] = {
1909 .instance_size
= sizeof(BlkTransactionState
),
1910 .prepare
= abort_prepare
,
1911 .commit
= abort_commit
,
1913 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1914 .instance_size
= sizeof(InternalSnapshotState
),
1915 .prepare
= internal_snapshot_prepare
,
1916 .abort
= internal_snapshot_abort
,
1917 .clean
= internal_snapshot_clean
,
1922 * 'Atomic' group operations. The operations are performed as a set, and if
1923 * any fail then we roll back all operations in the group.
1925 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1927 TransactionActionList
*dev_entry
= dev_list
;
1928 BlkTransactionState
*state
, *next
;
1929 Error
*local_err
= NULL
;
1931 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1932 QSIMPLEQ_INIT(&snap_bdrv_states
);
1934 /* drain all i/o before any operations */
1937 /* We don't do anything in this loop that commits us to the operations */
1938 while (NULL
!= dev_entry
) {
1939 TransactionAction
*dev_info
= NULL
;
1940 const BdrvActionOps
*ops
;
1942 dev_info
= dev_entry
->value
;
1943 dev_entry
= dev_entry
->next
;
1945 assert(dev_info
->type
< ARRAY_SIZE(actions
));
1947 ops
= &actions
[dev_info
->type
];
1948 assert(ops
->instance_size
> 0);
1950 state
= g_malloc0(ops
->instance_size
);
1952 state
->action
= dev_info
;
1953 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1955 state
->ops
->prepare(state
, &local_err
);
1957 error_propagate(errp
, local_err
);
1958 goto delete_and_fail
;
1962 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1963 if (state
->ops
->commit
) {
1964 state
->ops
->commit(state
);
1972 /* failure, and it is all-or-none; roll back all operations */
1973 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1974 if (state
->ops
->abort
) {
1975 state
->ops
->abort(state
);
1979 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1980 if (state
->ops
->clean
) {
1981 state
->ops
->clean(state
);
1987 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1989 Error
*local_err
= NULL
;
1991 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
1993 error_propagate(errp
, local_err
);
1997 qmp_blockdev_remove_medium(device
, errp
);
2000 void qmp_block_passwd(bool has_device
, const char *device
,
2001 bool has_node_name
, const char *node_name
,
2002 const char *password
, Error
**errp
)
2004 Error
*local_err
= NULL
;
2005 BlockDriverState
*bs
;
2006 AioContext
*aio_context
;
2008 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2009 has_node_name
? node_name
: NULL
,
2012 error_propagate(errp
, local_err
);
2016 aio_context
= bdrv_get_aio_context(bs
);
2017 aio_context_acquire(aio_context
);
2019 bdrv_add_key(bs
, password
, errp
);
2021 aio_context_release(aio_context
);
2024 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2034 blk
= blk_by_name(device
);
2036 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2037 "Device '%s' not found", device
);
2041 if (!blk_dev_has_removable_media(blk
)) {
2042 error_setg(errp
, "Device '%s' is not removable", device
);
2046 if (blk_dev_is_tray_open(blk
)) {
2050 locked
= blk_dev_is_medium_locked(blk
);
2052 blk_dev_eject_request(blk
, force
);
2055 if (!locked
|| force
) {
2056 blk_dev_change_media_cb(blk
, false);
2060 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2064 blk
= blk_by_name(device
);
2066 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2067 "Device '%s' not found", device
);
2071 if (!blk_dev_has_removable_media(blk
)) {
2072 error_setg(errp
, "Device '%s' is not removable", device
);
2076 if (!blk_dev_is_tray_open(blk
)) {
2080 blk_dev_change_media_cb(blk
, true);
2083 void qmp_blockdev_remove_medium(const char *device
, Error
**errp
)
2086 BlockDriverState
*bs
;
2087 AioContext
*aio_context
;
2090 blk
= blk_by_name(device
);
2092 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2093 "Device '%s' not found", device
);
2097 /* For BBs without a device, we can exchange the BDS tree at will */
2098 has_device
= blk_get_attached_dev(blk
);
2100 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2101 error_setg(errp
, "Device '%s' is not removable", device
);
2105 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2106 error_setg(errp
, "Tray of device '%s' is not open", device
);
2115 aio_context
= bdrv_get_aio_context(bs
);
2116 aio_context_acquire(aio_context
);
2118 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2122 /* This follows the convention established by bdrv_make_anon() */
2123 if (bs
->device_list
.tqe_prev
) {
2124 QTAILQ_REMOVE(&bdrv_states
, bs
, device_list
);
2125 bs
->device_list
.tqe_prev
= NULL
;
2131 aio_context_release(aio_context
);
2134 static void qmp_blockdev_insert_anon_medium(const char *device
,
2135 BlockDriverState
*bs
, Error
**errp
)
2140 blk
= blk_by_name(device
);
2142 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2143 "Device '%s' not found", device
);
2147 /* For BBs without a device, we can exchange the BDS tree at will */
2148 has_device
= blk_get_attached_dev(blk
);
2150 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2151 error_setg(errp
, "Device '%s' is not removable", device
);
2155 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2156 error_setg(errp
, "Tray of device '%s' is not open", device
);
2161 error_setg(errp
, "There already is a medium in device '%s'", device
);
2165 blk_insert_bs(blk
, bs
);
2167 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2170 void qmp_blockdev_insert_medium(const char *device
, const char *node_name
,
2173 BlockDriverState
*bs
;
2175 bs
= bdrv_find_node(node_name
);
2177 error_setg(errp
, "Node '%s' not found", node_name
);
2182 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2187 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2190 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2191 bool has_format
, const char *format
,
2193 BlockdevChangeReadOnlyMode read_only
,
2197 BlockDriverState
*medium_bs
= NULL
;
2198 int bdrv_flags
, ret
;
2199 QDict
*options
= NULL
;
2202 blk
= blk_by_name(device
);
2204 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2205 "Device '%s' not found", device
);
2210 blk_update_root_state(blk
);
2213 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2215 if (!has_read_only
) {
2216 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2219 switch (read_only
) {
2220 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2223 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2224 bdrv_flags
&= ~BDRV_O_RDWR
;
2227 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2228 bdrv_flags
|= BDRV_O_RDWR
;
2236 options
= qdict_new();
2237 qdict_put(options
, "driver", qstring_from_str(format
));
2241 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2246 blk_apply_root_state(blk
, medium_bs
);
2248 bdrv_add_key(medium_bs
, NULL
, &err
);
2250 error_propagate(errp
, err
);
2254 qmp_blockdev_open_tray(device
, false, false, &err
);
2256 error_propagate(errp
, err
);
2260 qmp_blockdev_remove_medium(device
, &err
);
2262 error_propagate(errp
, err
);
2266 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2268 error_propagate(errp
, err
);
2272 qmp_blockdev_close_tray(device
, errp
);
2275 /* If the medium has been inserted, the device has its own reference, so
2276 * ours must be relinquished; and if it has not been inserted successfully,
2277 * the reference must be relinquished anyway */
2278 bdrv_unref(medium_bs
);
2281 /* throttling disk I/O limits */
2282 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2289 bool has_bps_rd_max
,
2291 bool has_bps_wr_max
,
2295 bool has_iops_rd_max
,
2296 int64_t iops_rd_max
,
2297 bool has_iops_wr_max
,
2298 int64_t iops_wr_max
,
2302 const char *group
, Error
**errp
)
2305 BlockDriverState
*bs
;
2307 AioContext
*aio_context
;
2309 blk
= blk_by_name(device
);
2311 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2312 "Device '%s' not found", device
);
2316 aio_context
= blk_get_aio_context(blk
);
2317 aio_context_acquire(aio_context
);
2321 error_setg(errp
, "Device '%s' has no medium", device
);
2325 memset(&cfg
, 0, sizeof(cfg
));
2326 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2327 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2328 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2330 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2331 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2332 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2335 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2337 if (has_bps_rd_max
) {
2338 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2340 if (has_bps_wr_max
) {
2341 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2344 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2346 if (has_iops_rd_max
) {
2347 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2349 if (has_iops_wr_max
) {
2350 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2353 if (has_iops_size
) {
2354 cfg
.op_size
= iops_size
;
2357 if (!check_throttle_config(&cfg
, errp
)) {
2361 if (throttle_enabled(&cfg
)) {
2362 /* Enable I/O limits if they're not enabled yet, otherwise
2363 * just update the throttling group. */
2364 if (!bs
->io_limits_enabled
) {
2365 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2366 } else if (has_group
) {
2367 bdrv_io_limits_update_group(bs
, group
);
2369 /* Set the new throttling configuration */
2370 bdrv_set_io_limits(bs
, &cfg
);
2371 } else if (bs
->io_limits_enabled
) {
2372 /* If all throttling settings are set to 0, disable I/O limits */
2373 bdrv_io_limits_disable(bs
);
2377 aio_context_release(aio_context
);
2380 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2381 bool has_granularity
, uint32_t granularity
,
2384 AioContext
*aio_context
;
2385 BlockDriverState
*bs
;
2387 if (!name
|| name
[0] == '\0') {
2388 error_setg(errp
, "Bitmap name cannot be empty");
2392 bs
= bdrv_lookup_bs(node
, node
, errp
);
2397 aio_context
= bdrv_get_aio_context(bs
);
2398 aio_context_acquire(aio_context
);
2400 if (has_granularity
) {
2401 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2402 error_setg(errp
, "Granularity must be power of 2 "
2403 "and at least 512");
2407 /* Default to cluster size, if available: */
2408 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2411 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2414 aio_context_release(aio_context
);
2417 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2420 AioContext
*aio_context
;
2421 BlockDriverState
*bs
;
2422 BdrvDirtyBitmap
*bitmap
;
2424 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2425 if (!bitmap
|| !bs
) {
2429 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2431 "Bitmap '%s' is currently frozen and cannot be removed",
2435 bdrv_dirty_bitmap_make_anon(bitmap
);
2436 bdrv_release_dirty_bitmap(bs
, bitmap
);
2439 aio_context_release(aio_context
);
2443 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2444 * immediately after a full backup operation.
2446 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2449 AioContext
*aio_context
;
2450 BdrvDirtyBitmap
*bitmap
;
2451 BlockDriverState
*bs
;
2453 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2454 if (!bitmap
|| !bs
) {
2458 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2460 "Bitmap '%s' is currently frozen and cannot be modified",
2463 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2465 "Bitmap '%s' is currently disabled and cannot be cleared",
2470 bdrv_clear_dirty_bitmap(bitmap
);
2473 aio_context_release(aio_context
);
2476 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2478 const char *id
= qdict_get_str(qdict
, "id");
2480 BlockDriverState
*bs
;
2481 AioContext
*aio_context
;
2482 Error
*local_err
= NULL
;
2484 blk
= blk_by_name(id
);
2486 error_report("Device '%s' not found", id
);
2490 if (!blk_legacy_dinfo(blk
)) {
2491 error_report("Deleting device added with blockdev-add"
2492 " is not supported");
2496 aio_context
= blk_get_aio_context(blk
);
2497 aio_context_acquire(aio_context
);
2501 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2502 error_report_err(local_err
);
2503 aio_context_release(aio_context
);
2510 /* if we have a device attached to this BlockDriverState
2511 * then we need to make the drive anonymous until the device
2512 * can be removed. If this is a drive with no device backing
2513 * then we can just get rid of the block driver state right here.
2515 if (blk_get_attached_dev(blk
)) {
2516 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2517 /* Further I/O must not pause the guest */
2518 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2519 BLOCKDEV_ON_ERROR_REPORT
);
2524 aio_context_release(aio_context
);
2527 void qmp_block_resize(bool has_device
, const char *device
,
2528 bool has_node_name
, const char *node_name
,
2529 int64_t size
, Error
**errp
)
2531 Error
*local_err
= NULL
;
2532 BlockDriverState
*bs
;
2533 AioContext
*aio_context
;
2536 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2537 has_node_name
? node_name
: NULL
,
2540 error_propagate(errp
, local_err
);
2544 aio_context
= bdrv_get_aio_context(bs
);
2545 aio_context_acquire(aio_context
);
2547 if (!bdrv_is_first_non_filter(bs
)) {
2548 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2553 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2557 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2558 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2562 /* complete all in-flight operations before resizing the device */
2565 ret
= bdrv_truncate(bs
, size
);
2570 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2573 error_setg(errp
, QERR_UNSUPPORTED
);
2576 error_setg(errp
, "Device '%s' is read only", device
);
2579 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2582 error_setg_errno(errp
, -ret
, "Could not resize");
2587 aio_context_release(aio_context
);
2590 static void block_job_cb(void *opaque
, int ret
)
2592 /* Note that this function may be executed from another AioContext besides
2593 * the QEMU main loop. If you need to access anything that assumes the
2594 * QEMU global mutex, use a BH or introduce a mutex.
2597 BlockDriverState
*bs
= opaque
;
2598 const char *msg
= NULL
;
2600 trace_block_job_cb(bs
, bs
->job
, ret
);
2605 msg
= strerror(-ret
);
2608 if (block_job_is_cancelled(bs
->job
)) {
2609 block_job_event_cancelled(bs
->job
);
2611 block_job_event_completed(bs
->job
, msg
);
2614 bdrv_put_ref_bh_schedule(bs
);
2617 void qmp_block_stream(const char *device
,
2618 bool has_base
, const char *base
,
2619 bool has_backing_file
, const char *backing_file
,
2620 bool has_speed
, int64_t speed
,
2621 bool has_on_error
, BlockdevOnError on_error
,
2625 BlockDriverState
*bs
;
2626 BlockDriverState
*base_bs
= NULL
;
2627 AioContext
*aio_context
;
2628 Error
*local_err
= NULL
;
2629 const char *base_name
= NULL
;
2631 if (!has_on_error
) {
2632 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2635 blk
= blk_by_name(device
);
2637 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2638 "Device '%s' not found", device
);
2642 aio_context
= blk_get_aio_context(blk
);
2643 aio_context_acquire(aio_context
);
2645 if (!blk_is_available(blk
)) {
2646 error_setg(errp
, "Device '%s' has no medium", device
);
2651 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2656 base_bs
= bdrv_find_backing_image(bs
, base
);
2657 if (base_bs
== NULL
) {
2658 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2661 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2665 /* if we are streaming the entire chain, the result will have no backing
2666 * file, and specifying one is therefore an error */
2667 if (base_bs
== NULL
&& has_backing_file
) {
2668 error_setg(errp
, "backing file specified, but streaming the "
2673 /* backing_file string overrides base bs filename */
2674 base_name
= has_backing_file
? backing_file
: base_name
;
2676 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2677 on_error
, block_job_cb
, bs
, &local_err
);
2679 error_propagate(errp
, local_err
);
2683 trace_qmp_block_stream(bs
, bs
->job
);
2686 aio_context_release(aio_context
);
2689 void qmp_block_commit(const char *device
,
2690 bool has_base
, const char *base
,
2691 bool has_top
, const char *top
,
2692 bool has_backing_file
, const char *backing_file
,
2693 bool has_speed
, int64_t speed
,
2697 BlockDriverState
*bs
;
2698 BlockDriverState
*base_bs
, *top_bs
;
2699 AioContext
*aio_context
;
2700 Error
*local_err
= NULL
;
2701 /* This will be part of the QMP command, if/when the
2702 * BlockdevOnError change for blkmirror makes it in
2704 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2711 * libvirt relies on the DeviceNotFound error class in order to probe for
2712 * live commit feature versions; for this to work, we must make sure to
2713 * perform the device lookup before any generic errors that may occur in a
2714 * scenario in which all optional arguments are omitted. */
2715 blk
= blk_by_name(device
);
2717 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2718 "Device '%s' not found", device
);
2722 aio_context
= blk_get_aio_context(blk
);
2723 aio_context_acquire(aio_context
);
2725 if (!blk_is_available(blk
)) {
2726 error_setg(errp
, "Device '%s' has no medium", device
);
2731 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2735 /* default top_bs is the active layer */
2738 if (has_top
&& top
) {
2739 if (strcmp(bs
->filename
, top
) != 0) {
2740 top_bs
= bdrv_find_backing_image(bs
, top
);
2744 if (top_bs
== NULL
) {
2745 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2749 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2751 if (has_base
&& base
) {
2752 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2754 base_bs
= bdrv_find_base(top_bs
);
2757 if (base_bs
== NULL
) {
2758 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2762 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2764 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2768 /* Do not allow attempts to commit an image into itself */
2769 if (top_bs
== base_bs
) {
2770 error_setg(errp
, "cannot commit an image into itself");
2775 if (has_backing_file
) {
2776 error_setg(errp
, "'backing-file' specified,"
2777 " but 'top' is the active layer");
2780 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2783 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2784 has_backing_file
? backing_file
: NULL
, &local_err
);
2786 if (local_err
!= NULL
) {
2787 error_propagate(errp
, local_err
);
2792 aio_context_release(aio_context
);
2795 void qmp_drive_backup(const char *device
, const char *target
,
2796 bool has_format
, const char *format
,
2797 enum MirrorSyncMode sync
,
2798 bool has_mode
, enum NewImageMode mode
,
2799 bool has_speed
, int64_t speed
,
2800 bool has_bitmap
, const char *bitmap
,
2801 bool has_on_source_error
, BlockdevOnError on_source_error
,
2802 bool has_on_target_error
, BlockdevOnError on_target_error
,
2806 BlockDriverState
*bs
;
2807 BlockDriverState
*target_bs
;
2808 BlockDriverState
*source
= NULL
;
2809 BdrvDirtyBitmap
*bmap
= NULL
;
2810 AioContext
*aio_context
;
2811 QDict
*options
= NULL
;
2812 Error
*local_err
= NULL
;
2820 if (!has_on_source_error
) {
2821 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2823 if (!has_on_target_error
) {
2824 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2827 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2830 blk
= blk_by_name(device
);
2832 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2833 "Device '%s' not found", device
);
2837 aio_context
= blk_get_aio_context(blk
);
2838 aio_context_acquire(aio_context
);
2840 /* Although backup_run has this check too, we need to use bs->drv below, so
2841 * do an early check redundantly. */
2842 if (!blk_is_available(blk
)) {
2843 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2849 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2852 /* Early check to avoid creating target */
2853 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2857 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2859 /* See if we have a backing HD we can use to create our new image
2861 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2862 source
= backing_bs(bs
);
2864 sync
= MIRROR_SYNC_MODE_FULL
;
2867 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2871 size
= bdrv_getlength(bs
);
2873 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2877 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2880 bdrv_img_create(target
, format
, source
->filename
,
2881 source
->drv
->format_name
, NULL
,
2882 size
, flags
, &local_err
, false);
2884 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2885 size
, flags
, &local_err
, false);
2890 error_propagate(errp
, local_err
);
2895 options
= qdict_new();
2896 qdict_put(options
, "driver", qstring_from_str(format
));
2900 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
2902 error_propagate(errp
, local_err
);
2906 bdrv_set_aio_context(target_bs
, aio_context
);
2909 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
2911 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
2916 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
2917 on_source_error
, on_target_error
,
2918 block_job_cb
, bs
, &local_err
);
2919 if (local_err
!= NULL
) {
2920 bdrv_unref(target_bs
);
2921 error_propagate(errp
, local_err
);
2926 aio_context_release(aio_context
);
2929 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2931 return bdrv_named_nodes_list(errp
);
2934 void qmp_blockdev_backup(const char *device
, const char *target
,
2935 enum MirrorSyncMode sync
,
2936 bool has_speed
, int64_t speed
,
2937 bool has_on_source_error
,
2938 BlockdevOnError on_source_error
,
2939 bool has_on_target_error
,
2940 BlockdevOnError on_target_error
,
2943 BlockBackend
*blk
, *target_blk
;
2944 BlockDriverState
*bs
;
2945 BlockDriverState
*target_bs
;
2946 Error
*local_err
= NULL
;
2947 AioContext
*aio_context
;
2952 if (!has_on_source_error
) {
2953 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2955 if (!has_on_target_error
) {
2956 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2959 blk
= blk_by_name(device
);
2961 error_setg(errp
, "Device '%s' not found", device
);
2965 aio_context
= blk_get_aio_context(blk
);
2966 aio_context_acquire(aio_context
);
2968 if (!blk_is_available(blk
)) {
2969 error_setg(errp
, "Device '%s' has no medium", device
);
2974 target_blk
= blk_by_name(target
);
2976 error_setg(errp
, "Device '%s' not found", target
);
2980 if (!blk_is_available(target_blk
)) {
2981 error_setg(errp
, "Device '%s' has no medium", target
);
2984 target_bs
= blk_bs(target_blk
);
2986 bdrv_ref(target_bs
);
2987 bdrv_set_aio_context(target_bs
, aio_context
);
2988 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
2989 on_target_error
, block_job_cb
, bs
, &local_err
);
2990 if (local_err
!= NULL
) {
2991 bdrv_unref(target_bs
);
2992 error_propagate(errp
, local_err
);
2995 aio_context_release(aio_context
);
2998 void qmp_drive_mirror(const char *device
, const char *target
,
2999 bool has_format
, const char *format
,
3000 bool has_node_name
, const char *node_name
,
3001 bool has_replaces
, const char *replaces
,
3002 enum MirrorSyncMode sync
,
3003 bool has_mode
, enum NewImageMode mode
,
3004 bool has_speed
, int64_t speed
,
3005 bool has_granularity
, uint32_t granularity
,
3006 bool has_buf_size
, int64_t buf_size
,
3007 bool has_on_source_error
, BlockdevOnError on_source_error
,
3008 bool has_on_target_error
, BlockdevOnError on_target_error
,
3009 bool has_unmap
, bool unmap
,
3013 BlockDriverState
*bs
;
3014 BlockDriverState
*source
, *target_bs
;
3015 AioContext
*aio_context
;
3016 Error
*local_err
= NULL
;
3025 if (!has_on_source_error
) {
3026 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3028 if (!has_on_target_error
) {
3029 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3032 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3034 if (!has_granularity
) {
3037 if (!has_buf_size
) {
3044 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3045 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3046 "a value in range [512B, 64MB]");
3049 if (granularity
& (granularity
- 1)) {
3050 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3055 blk
= blk_by_name(device
);
3057 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3058 "Device '%s' not found", device
);
3062 aio_context
= blk_get_aio_context(blk
);
3063 aio_context_acquire(aio_context
);
3065 if (!blk_is_available(blk
)) {
3066 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3072 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3075 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
3079 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3080 source
= backing_bs(bs
);
3081 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3082 sync
= MIRROR_SYNC_MODE_FULL
;
3084 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3088 size
= bdrv_getlength(bs
);
3090 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3095 BlockDriverState
*to_replace_bs
;
3096 AioContext
*replace_aio_context
;
3097 int64_t replace_size
;
3099 if (!has_node_name
) {
3100 error_setg(errp
, "a node-name must be provided when replacing a"
3101 " named node of the graph");
3105 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3107 if (!to_replace_bs
) {
3108 error_propagate(errp
, local_err
);
3112 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3113 aio_context_acquire(replace_aio_context
);
3114 replace_size
= bdrv_getlength(to_replace_bs
);
3115 aio_context_release(replace_aio_context
);
3117 if (size
!= replace_size
) {
3118 error_setg(errp
, "cannot replace image with a mirror image of "
3124 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3125 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3127 /* create new image w/o backing file */
3129 bdrv_img_create(target
, format
,
3130 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3133 case NEW_IMAGE_MODE_EXISTING
:
3135 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3136 /* create new image with backing file */
3137 bdrv_img_create(target
, format
,
3139 source
->drv
->format_name
,
3140 NULL
, size
, flags
, &local_err
, false);
3148 error_propagate(errp
, local_err
);
3152 options
= qdict_new();
3153 if (has_node_name
) {
3154 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3157 qdict_put(options
, "driver", qstring_from_str(format
));
3160 /* Mirroring takes care of copy-on-write using the source's backing
3164 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3165 flags
| BDRV_O_NO_BACKING
, &local_err
);
3167 error_propagate(errp
, local_err
);
3171 bdrv_set_aio_context(target_bs
, aio_context
);
3173 /* pass the node name to replace to mirror start since it's loose coupling
3174 * and will allow to check whether the node still exist at mirror completion
3176 mirror_start(bs
, target_bs
,
3177 has_replaces
? replaces
: NULL
,
3178 speed
, granularity
, buf_size
, sync
,
3179 on_source_error
, on_target_error
,
3181 block_job_cb
, bs
, &local_err
);
3182 if (local_err
!= NULL
) {
3183 bdrv_unref(target_bs
);
3184 error_propagate(errp
, local_err
);
3189 aio_context_release(aio_context
);
3192 /* Get the block job for a given device name and acquire its AioContext */
3193 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3197 BlockDriverState
*bs
;
3199 *aio_context
= NULL
;
3201 blk
= blk_by_name(device
);
3206 *aio_context
= blk_get_aio_context(blk
);
3207 aio_context_acquire(*aio_context
);
3209 if (!blk_is_available(blk
)) {
3221 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3222 "No active block job on device '%s'", device
);
3224 aio_context_release(*aio_context
);
3225 *aio_context
= NULL
;
3230 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3232 AioContext
*aio_context
;
3233 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3239 block_job_set_speed(job
, speed
, errp
);
3240 aio_context_release(aio_context
);
3243 void qmp_block_job_cancel(const char *device
,
3244 bool has_force
, bool force
, Error
**errp
)
3246 AioContext
*aio_context
;
3247 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3257 if (job
->user_paused
&& !force
) {
3258 error_setg(errp
, "The block job for device '%s' is currently paused",
3263 trace_qmp_block_job_cancel(job
);
3264 block_job_cancel(job
);
3266 aio_context_release(aio_context
);
3269 void qmp_block_job_pause(const char *device
, Error
**errp
)
3271 AioContext
*aio_context
;
3272 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3274 if (!job
|| job
->user_paused
) {
3278 job
->user_paused
= true;
3279 trace_qmp_block_job_pause(job
);
3280 block_job_pause(job
);
3281 aio_context_release(aio_context
);
3284 void qmp_block_job_resume(const char *device
, Error
**errp
)
3286 AioContext
*aio_context
;
3287 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3289 if (!job
|| !job
->user_paused
) {
3293 job
->user_paused
= false;
3294 trace_qmp_block_job_resume(job
);
3295 block_job_resume(job
);
3296 aio_context_release(aio_context
);
3299 void qmp_block_job_complete(const char *device
, Error
**errp
)
3301 AioContext
*aio_context
;
3302 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3308 trace_qmp_block_job_complete(job
);
3309 block_job_complete(job
, errp
);
3310 aio_context_release(aio_context
);
3313 void qmp_change_backing_file(const char *device
,
3314 const char *image_node_name
,
3315 const char *backing_file
,
3319 BlockDriverState
*bs
= NULL
;
3320 AioContext
*aio_context
;
3321 BlockDriverState
*image_bs
= NULL
;
3322 Error
*local_err
= NULL
;
3327 blk
= blk_by_name(device
);
3329 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3330 "Device '%s' not found", device
);
3334 aio_context
= blk_get_aio_context(blk
);
3335 aio_context_acquire(aio_context
);
3337 if (!blk_is_available(blk
)) {
3338 error_setg(errp
, "Device '%s' has no medium", device
);
3343 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3345 error_propagate(errp
, local_err
);
3350 error_setg(errp
, "image file not found");
3354 if (bdrv_find_base(image_bs
) == image_bs
) {
3355 error_setg(errp
, "not allowing backing file change on an image "
3356 "without a backing file");
3360 /* even though we are not necessarily operating on bs, we need it to
3361 * determine if block ops are currently prohibited on the chain */
3362 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3366 /* final sanity check */
3367 if (!bdrv_chain_contains(bs
, image_bs
)) {
3368 error_setg(errp
, "'%s' and image file are not in the same chain",
3373 /* if not r/w, reopen to make r/w */
3374 open_flags
= image_bs
->open_flags
;
3375 ro
= bdrv_is_read_only(image_bs
);
3378 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3380 error_propagate(errp
, local_err
);
3385 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3386 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3389 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3391 /* don't exit here, so we can try to restore open flags if
3396 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3398 error_propagate(errp
, local_err
); /* will preserve prior errp */
3403 aio_context_release(aio_context
);
3406 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3408 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3409 BlockDriverState
*bs
;
3410 BlockBackend
*blk
= NULL
;
3413 Error
*local_err
= NULL
;
3415 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3416 * cache.direct=false instead of silently switching to aio=threads, except
3417 * when called from drive_new().
3419 * For now, simply forbidding the combination for all drivers will do. */
3420 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3421 bool direct
= options
->has_cache
&&
3422 options
->cache
->has_direct
&&
3423 options
->cache
->direct
;
3425 error_setg(errp
, "aio=native requires cache.direct=true");
3430 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3431 &options
, NULL
, &local_err
);
3433 error_propagate(errp
, local_err
);
3437 obj
= qmp_output_get_qobject(ov
);
3438 qdict
= qobject_to_qdict(obj
);
3440 qdict_flatten(qdict
);
3442 if (options
->has_id
) {
3443 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3445 error_propagate(errp
, local_err
);
3451 if (!qdict_get_try_str(qdict
, "node-name")) {
3452 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3457 bs
= bds_tree_init(qdict
, errp
);
3463 if (bs
&& bdrv_key_required(bs
)) {
3469 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3474 qmp_output_visitor_cleanup(ov
);
3477 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3479 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3480 BlockDriverState
*bs
;
3482 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3483 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3485 aio_context_acquire(aio_context
);
3488 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3489 elem
->value
= block_job_query(bs
->job
);
3491 p_next
= &elem
->next
;
3494 aio_context_release(aio_context
);
3500 QemuOptsList qemu_common_drive_opts
= {
3502 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3506 .type
= QEMU_OPT_BOOL
,
3507 .help
= "enable/disable snapshot mode",
3510 .type
= QEMU_OPT_STRING
,
3511 .help
= "discard operation (ignore/off, unmap/on)",
3513 .name
= BDRV_OPT_CACHE_WB
,
3514 .type
= QEMU_OPT_BOOL
,
3515 .help
= "enables writeback mode for any caches",
3517 .name
= BDRV_OPT_CACHE_DIRECT
,
3518 .type
= QEMU_OPT_BOOL
,
3519 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3521 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3522 .type
= QEMU_OPT_BOOL
,
3523 .help
= "ignore any flush requests for the device",
3526 .type
= QEMU_OPT_STRING
,
3527 .help
= "host AIO implementation (threads, native)",
3530 .type
= QEMU_OPT_STRING
,
3531 .help
= "disk format (raw, qcow2, ...)",
3534 .type
= QEMU_OPT_STRING
,
3535 .help
= "read error action",
3538 .type
= QEMU_OPT_STRING
,
3539 .help
= "write error action",
3541 .name
= "read-only",
3542 .type
= QEMU_OPT_BOOL
,
3543 .help
= "open drive file as read-only",
3545 .name
= "throttling.iops-total",
3546 .type
= QEMU_OPT_NUMBER
,
3547 .help
= "limit total I/O operations per second",
3549 .name
= "throttling.iops-read",
3550 .type
= QEMU_OPT_NUMBER
,
3551 .help
= "limit read operations per second",
3553 .name
= "throttling.iops-write",
3554 .type
= QEMU_OPT_NUMBER
,
3555 .help
= "limit write operations per second",
3557 .name
= "throttling.bps-total",
3558 .type
= QEMU_OPT_NUMBER
,
3559 .help
= "limit total bytes per second",
3561 .name
= "throttling.bps-read",
3562 .type
= QEMU_OPT_NUMBER
,
3563 .help
= "limit read bytes per second",
3565 .name
= "throttling.bps-write",
3566 .type
= QEMU_OPT_NUMBER
,
3567 .help
= "limit write bytes per second",
3569 .name
= "throttling.iops-total-max",
3570 .type
= QEMU_OPT_NUMBER
,
3571 .help
= "I/O operations burst",
3573 .name
= "throttling.iops-read-max",
3574 .type
= QEMU_OPT_NUMBER
,
3575 .help
= "I/O operations read burst",
3577 .name
= "throttling.iops-write-max",
3578 .type
= QEMU_OPT_NUMBER
,
3579 .help
= "I/O operations write burst",
3581 .name
= "throttling.bps-total-max",
3582 .type
= QEMU_OPT_NUMBER
,
3583 .help
= "total bytes burst",
3585 .name
= "throttling.bps-read-max",
3586 .type
= QEMU_OPT_NUMBER
,
3587 .help
= "total bytes read burst",
3589 .name
= "throttling.bps-write-max",
3590 .type
= QEMU_OPT_NUMBER
,
3591 .help
= "total bytes write burst",
3593 .name
= "throttling.iops-size",
3594 .type
= QEMU_OPT_NUMBER
,
3595 .help
= "when limiting by iops max size of an I/O in bytes",
3597 .name
= "throttling.group",
3598 .type
= QEMU_OPT_STRING
,
3599 .help
= "name of the block throttling group",
3601 .name
= "copy-on-read",
3602 .type
= QEMU_OPT_BOOL
,
3603 .help
= "copy read data from backing file into image file",
3605 .name
= "detect-zeroes",
3606 .type
= QEMU_OPT_STRING
,
3607 .help
= "try to optimize zero writes (off, on, unmap)",
3609 { /* end of list */ }
3613 static QemuOptsList qemu_root_bds_opts
= {
3615 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3619 .type
= QEMU_OPT_STRING
,
3620 .help
= "discard operation (ignore/off, unmap/on)",
3622 .name
= "cache.writeback",
3623 .type
= QEMU_OPT_BOOL
,
3624 .help
= "enables writeback mode for any caches",
3626 .name
= "cache.direct",
3627 .type
= QEMU_OPT_BOOL
,
3628 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3630 .name
= "cache.no-flush",
3631 .type
= QEMU_OPT_BOOL
,
3632 .help
= "ignore any flush requests for the device",
3635 .type
= QEMU_OPT_STRING
,
3636 .help
= "host AIO implementation (threads, native)",
3638 .name
= "read-only",
3639 .type
= QEMU_OPT_BOOL
,
3640 .help
= "open drive file as read-only",
3642 .name
= "copy-on-read",
3643 .type
= QEMU_OPT_BOOL
,
3644 .help
= "copy read data from backing file into image file",
3646 .name
= "detect-zeroes",
3647 .type
= QEMU_OPT_STRING
,
3648 .help
= "try to optimize zero writes (off, on, unmap)",
3650 { /* end of list */ }
3654 QemuOptsList qemu_drive_opts
= {
3656 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3659 * no elements => accept any params
3660 * validation will happen later
3662 { /* end of list */ }