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 int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
288 if (!strcmp(buf
, "ignore")) {
289 return BLOCKDEV_ON_ERROR_IGNORE
;
290 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
291 return BLOCKDEV_ON_ERROR_ENOSPC
;
292 } else if (!strcmp(buf
, "stop")) {
293 return BLOCKDEV_ON_ERROR_STOP
;
294 } else if (!strcmp(buf
, "report")) {
295 return BLOCKDEV_ON_ERROR_REPORT
;
297 error_setg(errp
, "'%s' invalid %s error action",
298 buf
, is_read
? "read" : "write");
303 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
305 if (throttle_conflicting(cfg
)) {
306 error_setg(errp
, "bps/iops/max total values and read/write values"
307 " cannot be used at the same time");
311 if (!throttle_is_valid(cfg
)) {
312 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
316 if (throttle_max_is_missing_limit(cfg
)) {
317 error_setg(errp
, "bps_max/iops_max require corresponding"
325 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
327 /* All parameters but @opts are optional and may be set to NULL. */
328 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
329 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
330 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
333 Error
*local_error
= NULL
;
337 if (!qemu_opt_get_bool(opts
, "read-only", false)) {
338 *bdrv_flags
|= BDRV_O_RDWR
;
340 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
341 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
344 if ((discard
= qemu_opt_get(opts
, "discard")) != NULL
) {
345 if (bdrv_parse_discard_flags(discard
, bdrv_flags
) != 0) {
346 error_setg(errp
, "Invalid discard option");
351 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true)) {
352 *bdrv_flags
|= BDRV_O_CACHE_WB
;
354 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_DIRECT
, false)) {
355 *bdrv_flags
|= BDRV_O_NOCACHE
;
357 if (qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_NO_FLUSH
, false)) {
358 *bdrv_flags
|= BDRV_O_NO_FLUSH
;
361 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
362 if (!strcmp(aio
, "native")) {
363 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
364 } else if (!strcmp(aio
, "threads")) {
365 /* this is the default */
367 error_setg(errp
, "invalid aio option");
373 /* disk I/O throttling */
374 if (throttling_group
) {
375 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
379 memset(throttle_cfg
, 0, sizeof(*throttle_cfg
));
380 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
381 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
382 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
383 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
384 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
385 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
386 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
387 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
388 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
389 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
390 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
391 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
393 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
394 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
395 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
396 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
397 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
398 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
399 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
400 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
401 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
402 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
403 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
404 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
406 throttle_cfg
->op_size
=
407 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
409 if (!check_throttle_config(throttle_cfg
, errp
)) {
416 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
417 qemu_opt_get(opts
, "detect-zeroes"),
418 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
419 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
422 error_propagate(errp
, local_error
);
427 *detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
428 !(*bdrv_flags
& BDRV_O_UNMAP
))
430 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
431 "without setting discard operation to unmap");
437 /* Takes the ownership of bs_opts */
438 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
443 int on_read_error
, on_write_error
;
444 bool account_invalid
, account_failed
;
445 const char *stats_intervals
;
447 BlockDriverState
*bs
;
453 bool has_driver_specific_opts
;
454 BlockdevDetectZeroesOptions detect_zeroes
=
455 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
456 const char *throttling_group
= NULL
;
458 /* Check common options by copying from bs_opts to opts, all other options
459 * stay in bs_opts for processing by bdrv_open(). */
460 id
= qdict_get_try_str(bs_opts
, "id");
461 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
463 error_propagate(errp
, error
);
467 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
469 error_propagate(errp
, error
);
474 qdict_del(bs_opts
, "id");
477 has_driver_specific_opts
= !!qdict_size(bs_opts
);
479 /* extract parameters */
480 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
482 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
483 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
485 stats_intervals
= qemu_opt_get(opts
, "stats-intervals");
487 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
488 &detect_zeroes
, &error
);
490 error_propagate(errp
, error
);
494 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
495 if (is_help_option(buf
)) {
496 error_printf("Supported formats:");
497 bdrv_iterate_format(bdrv_format_print
, NULL
);
502 if (qdict_haskey(bs_opts
, "driver")) {
503 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
506 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
509 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
510 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
511 on_write_error
= parse_block_error_action(buf
, 0, &error
);
513 error_propagate(errp
, error
);
518 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
519 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
520 on_read_error
= parse_block_error_action(buf
, 1, &error
);
522 error_propagate(errp
, error
);
528 /* always use cache=unsafe with snapshot */
529 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
530 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
534 if ((!file
|| !*file
) && !has_driver_specific_opts
) {
535 BlockBackendRootState
*blk_rs
;
537 blk
= blk_new(qemu_opts_id(opts
), errp
);
542 blk_rs
= blk_get_root_state(blk
);
543 blk_rs
->open_flags
= bdrv_flags
;
544 blk_rs
->read_only
= !(bdrv_flags
& BDRV_O_RDWR
);
545 blk_rs
->detect_zeroes
= detect_zeroes
;
547 if (throttle_enabled(&cfg
)) {
548 if (!throttling_group
) {
549 throttling_group
= blk_name(blk
);
551 blk_rs
->throttle_group
= g_strdup(throttling_group
);
552 blk_rs
->throttle_state
= throttle_group_incref(throttling_group
);
553 blk_rs
->throttle_state
->cfg
= cfg
;
558 if (file
&& !*file
) {
562 blk
= blk_new_open(qemu_opts_id(opts
), file
, NULL
, bs_opts
, bdrv_flags
,
569 bs
->detect_zeroes
= detect_zeroes
;
571 /* disk I/O throttling */
572 if (throttle_enabled(&cfg
)) {
573 if (!throttling_group
) {
574 throttling_group
= blk_name(blk
);
576 bdrv_io_limits_enable(bs
, throttling_group
);
577 bdrv_set_io_limits(bs
, &cfg
);
580 if (bdrv_key_required(bs
)) {
584 block_acct_init(blk_get_stats(blk
), account_invalid
, account_failed
);
586 if (stats_intervals
) {
587 char **intervals
= g_strsplit(stats_intervals
, ":", 0);
590 if (*stats_intervals
== '\0') {
591 error_setg(&error
, "stats-intervals can't have an empty value");
594 for (i
= 0; !error
&& intervals
[i
] != NULL
; i
++) {
595 unsigned long long val
;
596 if (parse_uint_full(intervals
[i
], &val
, 10) == 0 &&
597 val
> 0 && val
<= UINT_MAX
) {
598 block_acct_add_interval(blk_get_stats(blk
), val
);
600 error_setg(&error
, "Invalid interval length: '%s'",
605 g_strfreev(intervals
);
608 error_propagate(errp
, error
);
616 blk_set_on_error(blk
, on_read_error
, on_write_error
);
629 static QemuOptsList qemu_root_bds_opts
;
631 /* Takes the ownership of bs_opts */
632 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
634 BlockDriverState
*bs
;
636 Error
*local_error
= NULL
;
637 BlockdevDetectZeroesOptions detect_zeroes
;
641 opts
= qemu_opts_create(&qemu_root_bds_opts
, NULL
, 1, errp
);
646 qemu_opts_absorb_qdict(opts
, bs_opts
, &local_error
);
648 error_propagate(errp
, local_error
);
652 extract_common_blockdev_options(opts
, &bdrv_flags
, NULL
, NULL
,
653 &detect_zeroes
, &local_error
);
655 error_propagate(errp
, local_error
);
660 ret
= bdrv_open(&bs
, NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
662 goto fail_no_bs_opts
;
665 bs
->detect_zeroes
= detect_zeroes
;
677 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
682 value
= qemu_opt_get(opts
, from
);
684 if (qemu_opt_find(opts
, to
)) {
685 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
686 "same time", to
, from
);
691 /* rename all items in opts */
692 while ((value
= qemu_opt_get(opts
, from
))) {
693 qemu_opt_set(opts
, to
, value
, &error_abort
);
694 qemu_opt_unset(opts
, from
);
698 QemuOptsList qemu_legacy_drive_opts
= {
700 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
704 .type
= QEMU_OPT_NUMBER
,
705 .help
= "bus number",
708 .type
= QEMU_OPT_NUMBER
,
709 .help
= "unit number (i.e. lun for scsi)",
712 .type
= QEMU_OPT_NUMBER
,
713 .help
= "index number",
716 .type
= QEMU_OPT_STRING
,
717 .help
= "media type (disk, cdrom)",
720 .type
= QEMU_OPT_STRING
,
721 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
724 .type
= QEMU_OPT_NUMBER
,
725 .help
= "number of cylinders (ide disk geometry)",
728 .type
= QEMU_OPT_NUMBER
,
729 .help
= "number of heads (ide disk geometry)",
732 .type
= QEMU_OPT_NUMBER
,
733 .help
= "number of sectors (ide disk geometry)",
736 .type
= QEMU_OPT_STRING
,
737 .help
= "chs translation (auto, lba, none)",
740 .type
= QEMU_OPT_BOOL
,
741 .help
= "(deprecated, ignored)",
744 .type
= QEMU_OPT_STRING
,
745 .help
= "pci address (virtio only)",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "disk serial number",
752 .type
= QEMU_OPT_STRING
,
756 /* Options that are passed on, but have special semantics with -drive */
759 .type
= QEMU_OPT_BOOL
,
760 .help
= "open drive file as read-only",
763 .type
= QEMU_OPT_STRING
,
764 .help
= "read error action",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "write error action",
770 .name
= "copy-on-read",
771 .type
= QEMU_OPT_BOOL
,
772 .help
= "copy read data from backing file into image file",
775 { /* end of list */ }
779 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
783 DriveInfo
*dinfo
= NULL
;
785 QemuOpts
*legacy_opts
;
786 DriveMediaType media
= MEDIA_DISK
;
787 BlockInterfaceType type
;
788 int cyls
, heads
, secs
, translation
;
789 int max_devs
, bus_id
, unit_id
, index
;
791 const char *werror
, *rerror
;
792 bool read_only
= false;
795 const char *filename
;
796 Error
*local_err
= NULL
;
799 /* Change legacy command line options into QMP ones */
800 static const struct {
804 { "iops", "throttling.iops-total" },
805 { "iops_rd", "throttling.iops-read" },
806 { "iops_wr", "throttling.iops-write" },
808 { "bps", "throttling.bps-total" },
809 { "bps_rd", "throttling.bps-read" },
810 { "bps_wr", "throttling.bps-write" },
812 { "iops_max", "throttling.iops-total-max" },
813 { "iops_rd_max", "throttling.iops-read-max" },
814 { "iops_wr_max", "throttling.iops-write-max" },
816 { "bps_max", "throttling.bps-total-max" },
817 { "bps_rd_max", "throttling.bps-read-max" },
818 { "bps_wr_max", "throttling.bps-write-max" },
820 { "iops_size", "throttling.iops-size" },
822 { "group", "throttling.group" },
824 { "readonly", "read-only" },
827 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
828 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
831 error_report_err(local_err
);
836 value
= qemu_opt_get(all_opts
, "cache");
840 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
841 error_report("invalid cache option");
845 /* Specific options take precedence */
846 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
847 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
848 !!(flags
& BDRV_O_CACHE_WB
), &error_abort
);
850 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
851 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
852 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
854 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
855 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
856 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
858 qemu_opt_unset(all_opts
, "cache");
861 /* Get a QDict for processing the options */
862 bs_opts
= qdict_new();
863 qemu_opts_to_qdict(all_opts
, bs_opts
);
865 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
867 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
869 error_report_err(local_err
);
873 /* Deprecated option boot=[on|off] */
874 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
875 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
876 "ignored. Future versions will reject this parameter. Please "
877 "update your scripts.\n");
881 value
= qemu_opt_get(legacy_opts
, "media");
883 if (!strcmp(value
, "disk")) {
885 } else if (!strcmp(value
, "cdrom")) {
889 error_report("'%s' invalid media", value
);
894 /* copy-on-read is disabled with a warning for read-only devices */
895 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
896 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
898 if (read_only
&& copy_on_read
) {
899 error_report("warning: disabling copy-on-read on read-only drive");
900 copy_on_read
= false;
903 qdict_put(bs_opts
, "read-only",
904 qstring_from_str(read_only
? "on" : "off"));
905 qdict_put(bs_opts
, "copy-on-read",
906 qstring_from_str(copy_on_read
? "on" :"off"));
908 /* Controller type */
909 value
= qemu_opt_get(legacy_opts
, "if");
912 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
915 if (type
== IF_COUNT
) {
916 error_report("unsupported bus type '%s'", value
);
920 type
= block_default_type
;
924 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
925 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
926 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
928 if (cyls
|| heads
|| secs
) {
930 error_report("invalid physical cyls number");
934 error_report("invalid physical heads number");
938 error_report("invalid physical secs number");
943 translation
= BIOS_ATA_TRANSLATION_AUTO
;
944 value
= qemu_opt_get(legacy_opts
, "trans");
947 error_report("'%s' trans must be used with cyls, heads and secs",
951 if (!strcmp(value
, "none")) {
952 translation
= BIOS_ATA_TRANSLATION_NONE
;
953 } else if (!strcmp(value
, "lba")) {
954 translation
= BIOS_ATA_TRANSLATION_LBA
;
955 } else if (!strcmp(value
, "large")) {
956 translation
= BIOS_ATA_TRANSLATION_LARGE
;
957 } else if (!strcmp(value
, "rechs")) {
958 translation
= BIOS_ATA_TRANSLATION_RECHS
;
959 } else if (!strcmp(value
, "auto")) {
960 translation
= BIOS_ATA_TRANSLATION_AUTO
;
962 error_report("'%s' invalid translation type", value
);
967 if (media
== MEDIA_CDROM
) {
968 if (cyls
|| secs
|| heads
) {
969 error_report("CHS can't be set with media=cdrom");
974 /* Device address specified by bus/unit or index.
975 * If none was specified, try to find the first free one. */
976 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
977 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
978 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
980 max_devs
= if_max_devs
[type
];
983 if (bus_id
!= 0 || unit_id
!= -1) {
984 error_report("index cannot be used with bus and unit");
987 bus_id
= drive_index_to_bus_id(type
, index
);
988 unit_id
= drive_index_to_unit_id(type
, index
);
993 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
995 if (max_devs
&& unit_id
>= max_devs
) {
1002 if (max_devs
&& unit_id
>= max_devs
) {
1003 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1007 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1008 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1009 bus_id
, unit_id
, index
);
1014 serial
= qemu_opt_get(legacy_opts
, "serial");
1016 /* no id supplied -> create one */
1017 if (qemu_opts_id(all_opts
) == NULL
) {
1019 const char *mediastr
= "";
1020 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1021 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1024 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1027 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1030 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
1034 /* Add virtio block device */
1035 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1036 if (devaddr
&& type
!= IF_VIRTIO
) {
1037 error_report("addr is not supported by this bus type");
1041 if (type
== IF_VIRTIO
) {
1043 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1045 if (arch_type
== QEMU_ARCH_S390X
) {
1046 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1048 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1050 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1053 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1057 filename
= qemu_opt_get(legacy_opts
, "file");
1059 /* Check werror/rerror compatibility with if=... */
1060 werror
= qemu_opt_get(legacy_opts
, "werror");
1061 if (werror
!= NULL
) {
1062 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1064 error_report("werror is not supported by this bus type");
1067 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
1070 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1071 if (rerror
!= NULL
) {
1072 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1074 error_report("rerror is not supported by this bus type");
1077 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
1080 /* Actual block device init: Functionality shared with blockdev-add */
1081 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1085 error_report_err(local_err
);
1092 /* Create legacy DriveInfo */
1093 dinfo
= g_malloc0(sizeof(*dinfo
));
1094 dinfo
->opts
= all_opts
;
1097 dinfo
->heads
= heads
;
1099 dinfo
->trans
= translation
;
1102 dinfo
->bus
= bus_id
;
1103 dinfo
->unit
= unit_id
;
1104 dinfo
->devaddr
= devaddr
;
1105 dinfo
->serial
= g_strdup(serial
);
1107 blk_set_legacy_dinfo(blk
, dinfo
);
1114 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1121 qemu_opts_del(legacy_opts
);
1126 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1128 const char *device
= qdict_get_str(qdict
, "device");
1132 if (!strcmp(device
, "all")) {
1133 ret
= bdrv_commit_all();
1135 BlockDriverState
*bs
;
1136 AioContext
*aio_context
;
1138 blk
= blk_by_name(device
);
1140 monitor_printf(mon
, "Device '%s' not found\n", device
);
1143 if (!blk_is_available(blk
)) {
1144 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1149 aio_context
= bdrv_get_aio_context(bs
);
1150 aio_context_acquire(aio_context
);
1152 ret
= bdrv_commit(bs
);
1154 aio_context_release(aio_context
);
1157 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1162 static void blockdev_do_action(TransactionActionKind type
, void *data
,
1165 TransactionAction action
;
1166 TransactionActionList list
;
1169 action
.u
.data
= data
;
1170 list
.value
= &action
;
1172 qmp_transaction(&list
, false, NULL
, errp
);
1175 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1176 bool has_node_name
, const char *node_name
,
1177 const char *snapshot_file
,
1178 bool has_snapshot_node_name
,
1179 const char *snapshot_node_name
,
1180 bool has_format
, const char *format
,
1181 bool has_mode
, NewImageMode mode
, Error
**errp
)
1183 BlockdevSnapshotSync snapshot
= {
1184 .has_device
= has_device
,
1185 .device
= (char *) device
,
1186 .has_node_name
= has_node_name
,
1187 .node_name
= (char *) node_name
,
1188 .snapshot_file
= (char *) snapshot_file
,
1189 .has_snapshot_node_name
= has_snapshot_node_name
,
1190 .snapshot_node_name
= (char *) snapshot_node_name
,
1191 .has_format
= has_format
,
1192 .format
= (char *) format
,
1193 .has_mode
= has_mode
,
1196 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1200 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1203 BlockdevSnapshot snapshot_data
= {
1204 .node
= (char *) node
,
1205 .overlay
= (char *) overlay
1208 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1209 &snapshot_data
, errp
);
1212 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1216 BlockdevSnapshotInternal snapshot
= {
1217 .device
= (char *) device
,
1218 .name
= (char *) name
1221 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1225 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1232 BlockDriverState
*bs
;
1234 AioContext
*aio_context
;
1235 QEMUSnapshotInfo sn
;
1236 Error
*local_err
= NULL
;
1237 SnapshotInfo
*info
= NULL
;
1240 blk
= blk_by_name(device
);
1242 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1243 "Device '%s' not found", device
);
1247 aio_context
= blk_get_aio_context(blk
);
1248 aio_context_acquire(aio_context
);
1259 error_setg(errp
, "Name or id must be provided");
1260 goto out_aio_context
;
1263 if (!blk_is_available(blk
)) {
1264 error_setg(errp
, "Device '%s' has no medium", device
);
1265 goto out_aio_context
;
1269 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1270 goto out_aio_context
;
1273 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1275 error_propagate(errp
, local_err
);
1276 goto out_aio_context
;
1280 "Snapshot with id '%s' and name '%s' does not exist on "
1282 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1283 goto out_aio_context
;
1286 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1288 error_propagate(errp
, local_err
);
1289 goto out_aio_context
;
1292 aio_context_release(aio_context
);
1294 info
= g_new0(SnapshotInfo
, 1);
1295 info
->id
= g_strdup(sn
.id_str
);
1296 info
->name
= g_strdup(sn
.name
);
1297 info
->date_nsec
= sn
.date_nsec
;
1298 info
->date_sec
= sn
.date_sec
;
1299 info
->vm_state_size
= sn
.vm_state_size
;
1300 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1301 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1306 aio_context_release(aio_context
);
1311 * block_dirty_bitmap_lookup:
1312 * Return a dirty bitmap (if present), after validating
1313 * the node reference and bitmap names.
1315 * @node: The name of the BDS node to search for bitmaps
1316 * @name: The name of the bitmap to search for
1317 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1318 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1319 * @errp: Output pointer for error information. Can be NULL.
1321 * @return: A bitmap object on success, or NULL on failure.
1323 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1325 BlockDriverState
**pbs
,
1329 BlockDriverState
*bs
;
1330 BdrvDirtyBitmap
*bitmap
;
1331 AioContext
*aio_context
;
1334 error_setg(errp
, "Node cannot be NULL");
1338 error_setg(errp
, "Bitmap name cannot be NULL");
1341 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1343 error_setg(errp
, "Node '%s' not found", node
);
1347 aio_context
= bdrv_get_aio_context(bs
);
1348 aio_context_acquire(aio_context
);
1350 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1352 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1360 *paio
= aio_context
;
1362 aio_context_release(aio_context
);
1368 aio_context_release(aio_context
);
1372 /* New and old BlockDriverState structs for atomic group operations */
1374 typedef struct BlkActionState BlkActionState
;
1378 * Table of operations that define an Action.
1380 * @instance_size: Size of state struct, in bytes.
1381 * @prepare: Prepare the work, must NOT be NULL.
1382 * @commit: Commit the changes, can be NULL.
1383 * @abort: Abort the changes on fail, can be NULL.
1384 * @clean: Clean up resources after all transaction actions have called
1385 * commit() or abort(). Can be NULL.
1387 * Only prepare() may fail. In a single transaction, only one of commit() or
1388 * abort() will be called. clean() will always be called if it is present.
1390 typedef struct BlkActionOps
{
1391 size_t instance_size
;
1392 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1393 void (*commit
)(BlkActionState
*common
);
1394 void (*abort
)(BlkActionState
*common
);
1395 void (*clean
)(BlkActionState
*common
);
1400 * Describes one Action's state within a Transaction.
1402 * @action: QAPI-defined enum identifying which Action to perform.
1403 * @ops: Table of ActionOps this Action can perform.
1404 * @block_job_txn: Transaction which this action belongs to.
1405 * @entry: List membership for all Actions in this Transaction.
1407 * This structure must be arranged as first member in a subclassed type,
1408 * assuming that the compiler will also arrange it to the same offsets as the
1411 struct BlkActionState
{
1412 TransactionAction
*action
;
1413 const BlkActionOps
*ops
;
1414 BlockJobTxn
*block_job_txn
;
1415 TransactionProperties
*txn_props
;
1416 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1419 /* internal snapshot private data */
1420 typedef struct InternalSnapshotState
{
1421 BlkActionState common
;
1422 BlockDriverState
*bs
;
1423 AioContext
*aio_context
;
1424 QEMUSnapshotInfo sn
;
1426 } InternalSnapshotState
;
1429 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1431 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1433 "Action '%s' does not support Transaction property "
1434 "completion-mode = %s",
1435 TransactionActionKind_lookup
[s
->action
->type
],
1436 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1442 static void internal_snapshot_prepare(BlkActionState
*common
,
1445 Error
*local_err
= NULL
;
1449 BlockDriverState
*bs
;
1450 QEMUSnapshotInfo old_sn
, *sn
;
1453 BlockdevSnapshotInternal
*internal
;
1454 InternalSnapshotState
*state
;
1457 g_assert(common
->action
->type
==
1458 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1459 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
;
1460 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1462 /* 1. parse input */
1463 device
= internal
->device
;
1464 name
= internal
->name
;
1466 /* 2. check for validation */
1467 if (action_check_completion_mode(common
, errp
) < 0) {
1471 blk
= blk_by_name(device
);
1473 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1474 "Device '%s' not found", device
);
1478 /* AioContext is released in .clean() */
1479 state
->aio_context
= blk_get_aio_context(blk
);
1480 aio_context_acquire(state
->aio_context
);
1482 if (!blk_is_available(blk
)) {
1483 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1489 bdrv_drained_begin(bs
);
1491 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1495 if (bdrv_is_read_only(bs
)) {
1496 error_setg(errp
, "Device '%s' is read only", device
);
1500 if (!bdrv_can_snapshot(bs
)) {
1501 error_setg(errp
, "Block format '%s' used by device '%s' "
1502 "does not support internal snapshots",
1503 bs
->drv
->format_name
, device
);
1507 if (!strlen(name
)) {
1508 error_setg(errp
, "Name is empty");
1512 /* check whether a snapshot with name exist */
1513 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1516 error_propagate(errp
, local_err
);
1520 "Snapshot with name '%s' already exists on device '%s'",
1525 /* 3. take the snapshot */
1527 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1528 qemu_gettimeofday(&tv
);
1529 sn
->date_sec
= tv
.tv_sec
;
1530 sn
->date_nsec
= tv
.tv_usec
* 1000;
1531 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1533 ret1
= bdrv_snapshot_create(bs
, sn
);
1535 error_setg_errno(errp
, -ret1
,
1536 "Failed to create snapshot '%s' on device '%s'",
1541 /* 4. succeed, mark a snapshot is created */
1542 state
->created
= true;
1545 static void internal_snapshot_abort(BlkActionState
*common
)
1547 InternalSnapshotState
*state
=
1548 DO_UPCAST(InternalSnapshotState
, common
, common
);
1549 BlockDriverState
*bs
= state
->bs
;
1550 QEMUSnapshotInfo
*sn
= &state
->sn
;
1551 Error
*local_error
= NULL
;
1553 if (!state
->created
) {
1557 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1558 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1559 "device '%s' in abort: %s",
1562 bdrv_get_device_name(bs
),
1563 error_get_pretty(local_error
));
1564 error_free(local_error
);
1568 static void internal_snapshot_clean(BlkActionState
*common
)
1570 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1573 if (state
->aio_context
) {
1575 bdrv_drained_end(state
->bs
);
1577 aio_context_release(state
->aio_context
);
1581 /* external snapshot private data */
1582 typedef struct ExternalSnapshotState
{
1583 BlkActionState common
;
1584 BlockDriverState
*old_bs
;
1585 BlockDriverState
*new_bs
;
1586 AioContext
*aio_context
;
1587 } ExternalSnapshotState
;
1589 static void external_snapshot_prepare(BlkActionState
*common
,
1593 QDict
*options
= NULL
;
1594 Error
*local_err
= NULL
;
1595 /* Device and node name of the image to generate the snapshot from */
1597 const char *node_name
;
1598 /* Reference to the new image (for 'blockdev-snapshot') */
1599 const char *snapshot_ref
;
1600 /* File name of the new image (for 'blockdev-snapshot-sync') */
1601 const char *new_image_file
;
1602 ExternalSnapshotState
*state
=
1603 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1604 TransactionAction
*action
= common
->action
;
1606 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1607 * purpose but a different set of parameters */
1608 switch (action
->type
) {
1609 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1611 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
;
1613 node_name
= s
->node
;
1614 new_image_file
= NULL
;
1615 snapshot_ref
= s
->overlay
;
1618 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1620 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1621 device
= s
->has_device
? s
->device
: NULL
;
1622 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1623 new_image_file
= s
->snapshot_file
;
1624 snapshot_ref
= NULL
;
1628 g_assert_not_reached();
1631 /* start processing */
1632 if (action_check_completion_mode(common
, errp
) < 0) {
1636 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1637 if (!state
->old_bs
) {
1641 /* Acquire AioContext now so any threads operating on old_bs stop */
1642 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1643 aio_context_acquire(state
->aio_context
);
1644 bdrv_drained_begin(state
->old_bs
);
1646 if (!bdrv_is_inserted(state
->old_bs
)) {
1647 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1651 if (bdrv_op_is_blocked(state
->old_bs
,
1652 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1656 if (!bdrv_is_read_only(state
->old_bs
)) {
1657 if (bdrv_flush(state
->old_bs
)) {
1658 error_setg(errp
, QERR_IO_ERROR
);
1663 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1664 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1668 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1669 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
;
1670 const char *format
= s
->has_format
? s
->format
: "qcow2";
1671 enum NewImageMode mode
;
1672 const char *snapshot_node_name
=
1673 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1675 if (node_name
&& !snapshot_node_name
) {
1676 error_setg(errp
, "New snapshot node name missing");
1680 if (snapshot_node_name
&&
1681 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1682 error_setg(errp
, "New snapshot node name already in use");
1686 flags
= state
->old_bs
->open_flags
;
1688 /* create new image w/backing file */
1689 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1690 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1691 bdrv_img_create(new_image_file
, format
,
1692 state
->old_bs
->filename
,
1693 state
->old_bs
->drv
->format_name
,
1694 NULL
, -1, flags
, &local_err
, false);
1696 error_propagate(errp
, local_err
);
1701 options
= qdict_new();
1702 if (s
->has_snapshot_node_name
) {
1703 qdict_put(options
, "node-name",
1704 qstring_from_str(snapshot_node_name
));
1706 qdict_put(options
, "driver", qstring_from_str(format
));
1708 flags
|= BDRV_O_NO_BACKING
;
1711 assert(state
->new_bs
== NULL
);
1712 ret
= bdrv_open(&state
->new_bs
, new_image_file
, snapshot_ref
, options
,
1714 /* We will manually add the backing_hd field to the bs later */
1719 if (state
->new_bs
->blk
!= NULL
) {
1720 error_setg(errp
, "The snapshot is already in use by %s",
1721 blk_name(state
->new_bs
->blk
));
1725 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1730 if (state
->new_bs
->backing
!= NULL
) {
1731 error_setg(errp
, "The snapshot already has a backing image");
1735 if (!state
->new_bs
->drv
->supports_backing
) {
1736 error_setg(errp
, "The snapshot does not support backing images");
1740 static void external_snapshot_commit(BlkActionState
*common
)
1742 ExternalSnapshotState
*state
=
1743 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1745 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1747 /* This removes our old bs and adds the new bs */
1748 bdrv_append(state
->new_bs
, state
->old_bs
);
1749 /* We don't need (or want) to use the transactional
1750 * bdrv_reopen_multiple() across all the entries at once, because we
1751 * don't want to abort all of them if one of them fails the reopen */
1752 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1756 static void external_snapshot_abort(BlkActionState
*common
)
1758 ExternalSnapshotState
*state
=
1759 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1760 if (state
->new_bs
) {
1761 bdrv_unref(state
->new_bs
);
1765 static void external_snapshot_clean(BlkActionState
*common
)
1767 ExternalSnapshotState
*state
=
1768 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1769 if (state
->aio_context
) {
1770 bdrv_drained_end(state
->old_bs
);
1771 aio_context_release(state
->aio_context
);
1775 typedef struct DriveBackupState
{
1776 BlkActionState common
;
1777 BlockDriverState
*bs
;
1778 AioContext
*aio_context
;
1782 static void do_drive_backup(const char *device
, const char *target
,
1783 bool has_format
, const char *format
,
1784 enum MirrorSyncMode sync
,
1785 bool has_mode
, enum NewImageMode mode
,
1786 bool has_speed
, int64_t speed
,
1787 bool has_bitmap
, const char *bitmap
,
1788 bool has_on_source_error
,
1789 BlockdevOnError on_source_error
,
1790 bool has_on_target_error
,
1791 BlockdevOnError on_target_error
,
1792 BlockJobTxn
*txn
, Error
**errp
);
1794 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1796 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1798 DriveBackup
*backup
;
1799 Error
*local_err
= NULL
;
1801 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1802 backup
= common
->action
->u
.drive_backup
;
1804 blk
= blk_by_name(backup
->device
);
1806 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1807 "Device '%s' not found", backup
->device
);
1811 if (!blk_is_available(blk
)) {
1812 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1816 /* AioContext is released in .clean() */
1817 state
->aio_context
= blk_get_aio_context(blk
);
1818 aio_context_acquire(state
->aio_context
);
1819 bdrv_drained_begin(blk_bs(blk
));
1820 state
->bs
= blk_bs(blk
);
1822 do_drive_backup(backup
->device
, backup
->target
,
1823 backup
->has_format
, backup
->format
,
1825 backup
->has_mode
, backup
->mode
,
1826 backup
->has_speed
, backup
->speed
,
1827 backup
->has_bitmap
, backup
->bitmap
,
1828 backup
->has_on_source_error
, backup
->on_source_error
,
1829 backup
->has_on_target_error
, backup
->on_target_error
,
1830 common
->block_job_txn
, &local_err
);
1832 error_propagate(errp
, local_err
);
1836 state
->job
= state
->bs
->job
;
1839 static void drive_backup_abort(BlkActionState
*common
)
1841 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1842 BlockDriverState
*bs
= state
->bs
;
1844 /* Only cancel if it's the job we started */
1845 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1846 block_job_cancel_sync(bs
->job
);
1850 static void drive_backup_clean(BlkActionState
*common
)
1852 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1854 if (state
->aio_context
) {
1855 bdrv_drained_end(state
->bs
);
1856 aio_context_release(state
->aio_context
);
1860 typedef struct BlockdevBackupState
{
1861 BlkActionState common
;
1862 BlockDriverState
*bs
;
1864 AioContext
*aio_context
;
1865 } BlockdevBackupState
;
1867 static void do_blockdev_backup(const char *device
, const char *target
,
1868 enum MirrorSyncMode sync
,
1869 bool has_speed
, int64_t speed
,
1870 bool has_on_source_error
,
1871 BlockdevOnError on_source_error
,
1872 bool has_on_target_error
,
1873 BlockdevOnError on_target_error
,
1874 BlockJobTxn
*txn
, Error
**errp
);
1876 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1878 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1879 BlockdevBackup
*backup
;
1880 BlockBackend
*blk
, *target
;
1881 Error
*local_err
= NULL
;
1883 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1884 backup
= common
->action
->u
.blockdev_backup
;
1886 blk
= blk_by_name(backup
->device
);
1888 error_setg(errp
, "Device '%s' not found", backup
->device
);
1892 if (!blk_is_available(blk
)) {
1893 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, backup
->device
);
1897 target
= blk_by_name(backup
->target
);
1899 error_setg(errp
, "Device '%s' not found", backup
->target
);
1903 /* AioContext is released in .clean() */
1904 state
->aio_context
= blk_get_aio_context(blk
);
1905 if (state
->aio_context
!= blk_get_aio_context(target
)) {
1906 state
->aio_context
= NULL
;
1907 error_setg(errp
, "Backup between two IO threads is not implemented");
1910 aio_context_acquire(state
->aio_context
);
1911 state
->bs
= blk_bs(blk
);
1912 bdrv_drained_begin(state
->bs
);
1914 do_blockdev_backup(backup
->device
, backup
->target
,
1916 backup
->has_speed
, backup
->speed
,
1917 backup
->has_on_source_error
, backup
->on_source_error
,
1918 backup
->has_on_target_error
, backup
->on_target_error
,
1919 common
->block_job_txn
, &local_err
);
1921 error_propagate(errp
, local_err
);
1925 state
->job
= state
->bs
->job
;
1928 static void blockdev_backup_abort(BlkActionState
*common
)
1930 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1931 BlockDriverState
*bs
= state
->bs
;
1933 /* Only cancel if it's the job we started */
1934 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1935 block_job_cancel_sync(bs
->job
);
1939 static void blockdev_backup_clean(BlkActionState
*common
)
1941 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1943 if (state
->aio_context
) {
1944 bdrv_drained_end(state
->bs
);
1945 aio_context_release(state
->aio_context
);
1949 typedef struct BlockDirtyBitmapState
{
1950 BlkActionState common
;
1951 BdrvDirtyBitmap
*bitmap
;
1952 BlockDriverState
*bs
;
1953 AioContext
*aio_context
;
1956 } BlockDirtyBitmapState
;
1958 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1961 Error
*local_err
= NULL
;
1962 BlockDirtyBitmapAdd
*action
;
1963 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1966 if (action_check_completion_mode(common
, errp
) < 0) {
1970 action
= common
->action
->u
.block_dirty_bitmap_add
;
1971 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1972 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1973 action
->has_granularity
, action
->granularity
,
1977 state
->prepared
= true;
1979 error_propagate(errp
, local_err
);
1983 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
1985 BlockDirtyBitmapAdd
*action
;
1986 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1989 action
= common
->action
->u
.block_dirty_bitmap_add
;
1990 /* Should not be able to fail: IF the bitmap was added via .prepare(),
1991 * then the node reference and bitmap name must have been valid.
1993 if (state
->prepared
) {
1994 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
1998 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2001 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2003 BlockDirtyBitmap
*action
;
2005 if (action_check_completion_mode(common
, errp
) < 0) {
2009 action
= common
->action
->u
.block_dirty_bitmap_clear
;
2010 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2013 &state
->aio_context
,
2015 if (!state
->bitmap
) {
2019 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2020 error_setg(errp
, "Cannot modify a frozen bitmap");
2022 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2023 error_setg(errp
, "Cannot clear a disabled bitmap");
2027 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2028 /* AioContext is released in .clean() */
2031 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2033 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2036 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2039 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2041 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2044 hbitmap_free(state
->backup
);
2047 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2049 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2052 if (state
->aio_context
) {
2053 aio_context_release(state
->aio_context
);
2057 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2059 error_setg(errp
, "Transaction aborted using Abort action");
2062 static void abort_commit(BlkActionState
*common
)
2064 g_assert_not_reached(); /* this action never succeeds */
2067 static const BlkActionOps actions
[] = {
2068 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2069 .instance_size
= sizeof(ExternalSnapshotState
),
2070 .prepare
= external_snapshot_prepare
,
2071 .commit
= external_snapshot_commit
,
2072 .abort
= external_snapshot_abort
,
2074 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2075 .instance_size
= sizeof(ExternalSnapshotState
),
2076 .prepare
= external_snapshot_prepare
,
2077 .commit
= external_snapshot_commit
,
2078 .abort
= external_snapshot_abort
,
2079 .clean
= external_snapshot_clean
,
2081 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2082 .instance_size
= sizeof(DriveBackupState
),
2083 .prepare
= drive_backup_prepare
,
2084 .abort
= drive_backup_abort
,
2085 .clean
= drive_backup_clean
,
2087 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2088 .instance_size
= sizeof(BlockdevBackupState
),
2089 .prepare
= blockdev_backup_prepare
,
2090 .abort
= blockdev_backup_abort
,
2091 .clean
= blockdev_backup_clean
,
2093 [TRANSACTION_ACTION_KIND_ABORT
] = {
2094 .instance_size
= sizeof(BlkActionState
),
2095 .prepare
= abort_prepare
,
2096 .commit
= abort_commit
,
2098 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2099 .instance_size
= sizeof(InternalSnapshotState
),
2100 .prepare
= internal_snapshot_prepare
,
2101 .abort
= internal_snapshot_abort
,
2102 .clean
= internal_snapshot_clean
,
2104 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2105 .instance_size
= sizeof(BlockDirtyBitmapState
),
2106 .prepare
= block_dirty_bitmap_add_prepare
,
2107 .abort
= block_dirty_bitmap_add_abort
,
2109 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2110 .instance_size
= sizeof(BlockDirtyBitmapState
),
2111 .prepare
= block_dirty_bitmap_clear_prepare
,
2112 .commit
= block_dirty_bitmap_clear_commit
,
2113 .abort
= block_dirty_bitmap_clear_abort
,
2114 .clean
= block_dirty_bitmap_clear_clean
,
2119 * Allocate a TransactionProperties structure if necessary, and fill
2120 * that structure with desired defaults if they are unset.
2122 static TransactionProperties
*get_transaction_properties(
2123 TransactionProperties
*props
)
2126 props
= g_new0(TransactionProperties
, 1);
2129 if (!props
->has_completion_mode
) {
2130 props
->has_completion_mode
= true;
2131 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2138 * 'Atomic' group operations. The operations are performed as a set, and if
2139 * any fail then we roll back all operations in the group.
2141 void qmp_transaction(TransactionActionList
*dev_list
,
2143 struct TransactionProperties
*props
,
2146 TransactionActionList
*dev_entry
= dev_list
;
2147 BlockJobTxn
*block_job_txn
= NULL
;
2148 BlkActionState
*state
, *next
;
2149 Error
*local_err
= NULL
;
2151 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2152 QSIMPLEQ_INIT(&snap_bdrv_states
);
2154 /* Does this transaction get canceled as a group on failure?
2155 * If not, we don't really need to make a BlockJobTxn.
2157 props
= get_transaction_properties(props
);
2158 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2159 block_job_txn
= block_job_txn_new();
2162 /* drain all i/o before any operations */
2165 /* We don't do anything in this loop that commits us to the operations */
2166 while (NULL
!= dev_entry
) {
2167 TransactionAction
*dev_info
= NULL
;
2168 const BlkActionOps
*ops
;
2170 dev_info
= dev_entry
->value
;
2171 dev_entry
= dev_entry
->next
;
2173 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2175 ops
= &actions
[dev_info
->type
];
2176 assert(ops
->instance_size
> 0);
2178 state
= g_malloc0(ops
->instance_size
);
2180 state
->action
= dev_info
;
2181 state
->block_job_txn
= block_job_txn
;
2182 state
->txn_props
= props
;
2183 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2185 state
->ops
->prepare(state
, &local_err
);
2187 error_propagate(errp
, local_err
);
2188 goto delete_and_fail
;
2192 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2193 if (state
->ops
->commit
) {
2194 state
->ops
->commit(state
);
2202 /* failure, and it is all-or-none; roll back all operations */
2203 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2204 if (state
->ops
->abort
) {
2205 state
->ops
->abort(state
);
2209 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2210 if (state
->ops
->clean
) {
2211 state
->ops
->clean(state
);
2216 qapi_free_TransactionProperties(props
);
2218 block_job_txn_unref(block_job_txn
);
2221 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
2223 Error
*local_err
= NULL
;
2225 qmp_blockdev_open_tray(device
, has_force
, force
, &local_err
);
2227 error_propagate(errp
, local_err
);
2231 qmp_blockdev_remove_medium(device
, errp
);
2234 void qmp_block_passwd(bool has_device
, const char *device
,
2235 bool has_node_name
, const char *node_name
,
2236 const char *password
, Error
**errp
)
2238 Error
*local_err
= NULL
;
2239 BlockDriverState
*bs
;
2240 AioContext
*aio_context
;
2242 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2243 has_node_name
? node_name
: NULL
,
2246 error_propagate(errp
, local_err
);
2250 aio_context
= bdrv_get_aio_context(bs
);
2251 aio_context_acquire(aio_context
);
2253 bdrv_add_key(bs
, password
, errp
);
2255 aio_context_release(aio_context
);
2258 void qmp_blockdev_open_tray(const char *device
, bool has_force
, bool force
,
2268 blk
= blk_by_name(device
);
2270 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2271 "Device '%s' not found", device
);
2275 if (!blk_dev_has_removable_media(blk
)) {
2276 error_setg(errp
, "Device '%s' is not removable", device
);
2280 if (blk_dev_is_tray_open(blk
)) {
2284 locked
= blk_dev_is_medium_locked(blk
);
2286 blk_dev_eject_request(blk
, force
);
2289 if (!locked
|| force
) {
2290 blk_dev_change_media_cb(blk
, false);
2294 void qmp_blockdev_close_tray(const char *device
, Error
**errp
)
2298 blk
= blk_by_name(device
);
2300 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2301 "Device '%s' not found", device
);
2305 if (!blk_dev_has_removable_media(blk
)) {
2306 error_setg(errp
, "Device '%s' is not removable", device
);
2310 if (!blk_dev_is_tray_open(blk
)) {
2314 blk_dev_change_media_cb(blk
, true);
2317 void qmp_blockdev_remove_medium(const char *device
, Error
**errp
)
2320 BlockDriverState
*bs
;
2321 AioContext
*aio_context
;
2324 blk
= blk_by_name(device
);
2326 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2327 "Device '%s' not found", device
);
2331 /* For BBs without a device, we can exchange the BDS tree at will */
2332 has_device
= blk_get_attached_dev(blk
);
2334 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2335 error_setg(errp
, "Device '%s' is not removable", device
);
2339 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2340 error_setg(errp
, "Tray of device '%s' is not open", device
);
2349 aio_context
= bdrv_get_aio_context(bs
);
2350 aio_context_acquire(aio_context
);
2352 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2356 /* This follows the convention established by bdrv_make_anon() */
2357 if (bs
->device_list
.tqe_prev
) {
2358 QTAILQ_REMOVE(&bdrv_states
, bs
, device_list
);
2359 bs
->device_list
.tqe_prev
= NULL
;
2365 aio_context_release(aio_context
);
2368 static void qmp_blockdev_insert_anon_medium(const char *device
,
2369 BlockDriverState
*bs
, Error
**errp
)
2374 blk
= blk_by_name(device
);
2376 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2377 "Device '%s' not found", device
);
2381 /* For BBs without a device, we can exchange the BDS tree at will */
2382 has_device
= blk_get_attached_dev(blk
);
2384 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2385 error_setg(errp
, "Device '%s' is not removable", device
);
2389 if (has_device
&& !blk_dev_is_tray_open(blk
)) {
2390 error_setg(errp
, "Tray of device '%s' is not open", device
);
2395 error_setg(errp
, "There already is a medium in device '%s'", device
);
2399 blk_insert_bs(blk
, bs
);
2401 QTAILQ_INSERT_TAIL(&bdrv_states
, bs
, device_list
);
2404 void qmp_blockdev_insert_medium(const char *device
, const char *node_name
,
2407 BlockDriverState
*bs
;
2409 bs
= bdrv_find_node(node_name
);
2411 error_setg(errp
, "Node '%s' not found", node_name
);
2416 error_setg(errp
, "Node '%s' is already in use by '%s'", node_name
,
2421 qmp_blockdev_insert_anon_medium(device
, bs
, errp
);
2424 void qmp_blockdev_change_medium(const char *device
, const char *filename
,
2425 bool has_format
, const char *format
,
2427 BlockdevChangeReadOnlyMode read_only
,
2431 BlockDriverState
*medium_bs
= NULL
;
2432 int bdrv_flags
, ret
;
2433 QDict
*options
= NULL
;
2436 blk
= blk_by_name(device
);
2438 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2439 "Device '%s' not found", device
);
2444 blk_update_root_state(blk
);
2447 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2449 if (!has_read_only
) {
2450 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2453 switch (read_only
) {
2454 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2457 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2458 bdrv_flags
&= ~BDRV_O_RDWR
;
2461 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2462 bdrv_flags
|= BDRV_O_RDWR
;
2470 options
= qdict_new();
2471 qdict_put(options
, "driver", qstring_from_str(format
));
2475 ret
= bdrv_open(&medium_bs
, filename
, NULL
, options
, bdrv_flags
, errp
);
2480 blk_apply_root_state(blk
, medium_bs
);
2482 bdrv_add_key(medium_bs
, NULL
, &err
);
2484 error_propagate(errp
, err
);
2488 qmp_blockdev_open_tray(device
, false, false, &err
);
2490 error_propagate(errp
, err
);
2494 qmp_blockdev_remove_medium(device
, &err
);
2496 error_propagate(errp
, err
);
2500 qmp_blockdev_insert_anon_medium(device
, medium_bs
, &err
);
2502 error_propagate(errp
, err
);
2506 qmp_blockdev_close_tray(device
, errp
);
2509 /* If the medium has been inserted, the device has its own reference, so
2510 * ours must be relinquished; and if it has not been inserted successfully,
2511 * the reference must be relinquished anyway */
2512 bdrv_unref(medium_bs
);
2515 /* throttling disk I/O limits */
2516 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
2523 bool has_bps_rd_max
,
2525 bool has_bps_wr_max
,
2529 bool has_iops_rd_max
,
2530 int64_t iops_rd_max
,
2531 bool has_iops_wr_max
,
2532 int64_t iops_wr_max
,
2536 const char *group
, Error
**errp
)
2539 BlockDriverState
*bs
;
2541 AioContext
*aio_context
;
2543 blk
= blk_by_name(device
);
2545 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2546 "Device '%s' not found", device
);
2550 aio_context
= blk_get_aio_context(blk
);
2551 aio_context_acquire(aio_context
);
2555 error_setg(errp
, "Device '%s' has no medium", device
);
2559 memset(&cfg
, 0, sizeof(cfg
));
2560 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
2561 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
2562 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
2564 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
2565 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
2566 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
2569 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
2571 if (has_bps_rd_max
) {
2572 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
2574 if (has_bps_wr_max
) {
2575 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
2578 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
2580 if (has_iops_rd_max
) {
2581 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
2583 if (has_iops_wr_max
) {
2584 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
2587 if (has_iops_size
) {
2588 cfg
.op_size
= iops_size
;
2591 if (!check_throttle_config(&cfg
, errp
)) {
2595 if (throttle_enabled(&cfg
)) {
2596 /* Enable I/O limits if they're not enabled yet, otherwise
2597 * just update the throttling group. */
2598 if (!bs
->throttle_state
) {
2599 bdrv_io_limits_enable(bs
, has_group
? group
: device
);
2600 } else if (has_group
) {
2601 bdrv_io_limits_update_group(bs
, group
);
2603 /* Set the new throttling configuration */
2604 bdrv_set_io_limits(bs
, &cfg
);
2605 } else if (bs
->throttle_state
) {
2606 /* If all throttling settings are set to 0, disable I/O limits */
2607 bdrv_io_limits_disable(bs
);
2611 aio_context_release(aio_context
);
2614 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2615 bool has_granularity
, uint32_t granularity
,
2618 AioContext
*aio_context
;
2619 BlockDriverState
*bs
;
2621 if (!name
|| name
[0] == '\0') {
2622 error_setg(errp
, "Bitmap name cannot be empty");
2626 bs
= bdrv_lookup_bs(node
, node
, errp
);
2631 aio_context
= bdrv_get_aio_context(bs
);
2632 aio_context_acquire(aio_context
);
2634 if (has_granularity
) {
2635 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2636 error_setg(errp
, "Granularity must be power of 2 "
2637 "and at least 512");
2641 /* Default to cluster size, if available: */
2642 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2645 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2648 aio_context_release(aio_context
);
2651 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2654 AioContext
*aio_context
;
2655 BlockDriverState
*bs
;
2656 BdrvDirtyBitmap
*bitmap
;
2658 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2659 if (!bitmap
|| !bs
) {
2663 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2665 "Bitmap '%s' is currently frozen and cannot be removed",
2669 bdrv_dirty_bitmap_make_anon(bitmap
);
2670 bdrv_release_dirty_bitmap(bs
, bitmap
);
2673 aio_context_release(aio_context
);
2677 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2678 * immediately after a full backup operation.
2680 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2683 AioContext
*aio_context
;
2684 BdrvDirtyBitmap
*bitmap
;
2685 BlockDriverState
*bs
;
2687 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, &aio_context
, errp
);
2688 if (!bitmap
|| !bs
) {
2692 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2694 "Bitmap '%s' is currently frozen and cannot be modified",
2697 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2699 "Bitmap '%s' is currently disabled and cannot be cleared",
2704 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2707 aio_context_release(aio_context
);
2710 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2712 const char *id
= qdict_get_str(qdict
, "id");
2714 BlockDriverState
*bs
;
2715 AioContext
*aio_context
;
2716 Error
*local_err
= NULL
;
2718 blk
= blk_by_name(id
);
2720 error_report("Device '%s' not found", id
);
2724 if (!blk_legacy_dinfo(blk
)) {
2725 error_report("Deleting device added with blockdev-add"
2726 " is not supported");
2730 aio_context
= blk_get_aio_context(blk
);
2731 aio_context_acquire(aio_context
);
2735 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2736 error_report_err(local_err
);
2737 aio_context_release(aio_context
);
2744 /* if we have a device attached to this BlockDriverState
2745 * then we need to make the drive anonymous until the device
2746 * can be removed. If this is a drive with no device backing
2747 * then we can just get rid of the block driver state right here.
2749 if (blk_get_attached_dev(blk
)) {
2750 blk_hide_on_behalf_of_hmp_drive_del(blk
);
2751 /* Further I/O must not pause the guest */
2752 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2753 BLOCKDEV_ON_ERROR_REPORT
);
2758 aio_context_release(aio_context
);
2761 void qmp_block_resize(bool has_device
, const char *device
,
2762 bool has_node_name
, const char *node_name
,
2763 int64_t size
, Error
**errp
)
2765 Error
*local_err
= NULL
;
2766 BlockDriverState
*bs
;
2767 AioContext
*aio_context
;
2770 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2771 has_node_name
? node_name
: NULL
,
2774 error_propagate(errp
, local_err
);
2778 aio_context
= bdrv_get_aio_context(bs
);
2779 aio_context_acquire(aio_context
);
2781 if (!bdrv_is_first_non_filter(bs
)) {
2782 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2787 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2791 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2792 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2796 /* complete all in-flight operations before resizing the device */
2799 ret
= bdrv_truncate(bs
, size
);
2804 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2807 error_setg(errp
, QERR_UNSUPPORTED
);
2810 error_setg(errp
, "Device '%s' is read only", device
);
2813 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2816 error_setg_errno(errp
, -ret
, "Could not resize");
2821 aio_context_release(aio_context
);
2824 static void block_job_cb(void *opaque
, int ret
)
2826 /* Note that this function may be executed from another AioContext besides
2827 * the QEMU main loop. If you need to access anything that assumes the
2828 * QEMU global mutex, use a BH or introduce a mutex.
2831 BlockDriverState
*bs
= opaque
;
2832 const char *msg
= NULL
;
2834 trace_block_job_cb(bs
, bs
->job
, ret
);
2839 msg
= strerror(-ret
);
2842 if (block_job_is_cancelled(bs
->job
)) {
2843 block_job_event_cancelled(bs
->job
);
2845 block_job_event_completed(bs
->job
, msg
);
2849 void qmp_block_stream(const char *device
,
2850 bool has_base
, const char *base
,
2851 bool has_backing_file
, const char *backing_file
,
2852 bool has_speed
, int64_t speed
,
2853 bool has_on_error
, BlockdevOnError on_error
,
2857 BlockDriverState
*bs
;
2858 BlockDriverState
*base_bs
= NULL
;
2859 AioContext
*aio_context
;
2860 Error
*local_err
= NULL
;
2861 const char *base_name
= NULL
;
2863 if (!has_on_error
) {
2864 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2867 blk
= blk_by_name(device
);
2869 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2870 "Device '%s' not found", device
);
2874 aio_context
= blk_get_aio_context(blk
);
2875 aio_context_acquire(aio_context
);
2877 if (!blk_is_available(blk
)) {
2878 error_setg(errp
, "Device '%s' has no medium", device
);
2883 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2888 base_bs
= bdrv_find_backing_image(bs
, base
);
2889 if (base_bs
== NULL
) {
2890 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2893 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2897 /* if we are streaming the entire chain, the result will have no backing
2898 * file, and specifying one is therefore an error */
2899 if (base_bs
== NULL
&& has_backing_file
) {
2900 error_setg(errp
, "backing file specified, but streaming the "
2905 /* backing_file string overrides base bs filename */
2906 base_name
= has_backing_file
? backing_file
: base_name
;
2908 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2909 on_error
, block_job_cb
, bs
, &local_err
);
2911 error_propagate(errp
, local_err
);
2915 trace_qmp_block_stream(bs
, bs
->job
);
2918 aio_context_release(aio_context
);
2921 void qmp_block_commit(const char *device
,
2922 bool has_base
, const char *base
,
2923 bool has_top
, const char *top
,
2924 bool has_backing_file
, const char *backing_file
,
2925 bool has_speed
, int64_t speed
,
2929 BlockDriverState
*bs
;
2930 BlockDriverState
*base_bs
, *top_bs
;
2931 AioContext
*aio_context
;
2932 Error
*local_err
= NULL
;
2933 /* This will be part of the QMP command, if/when the
2934 * BlockdevOnError change for blkmirror makes it in
2936 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2943 * libvirt relies on the DeviceNotFound error class in order to probe for
2944 * live commit feature versions; for this to work, we must make sure to
2945 * perform the device lookup before any generic errors that may occur in a
2946 * scenario in which all optional arguments are omitted. */
2947 blk
= blk_by_name(device
);
2949 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
2950 "Device '%s' not found", device
);
2954 aio_context
= blk_get_aio_context(blk
);
2955 aio_context_acquire(aio_context
);
2957 if (!blk_is_available(blk
)) {
2958 error_setg(errp
, "Device '%s' has no medium", device
);
2963 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2967 /* default top_bs is the active layer */
2970 if (has_top
&& top
) {
2971 if (strcmp(bs
->filename
, top
) != 0) {
2972 top_bs
= bdrv_find_backing_image(bs
, top
);
2976 if (top_bs
== NULL
) {
2977 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2981 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2983 if (has_base
&& base
) {
2984 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2986 base_bs
= bdrv_find_base(top_bs
);
2989 if (base_bs
== NULL
) {
2990 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2994 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2996 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3000 /* Do not allow attempts to commit an image into itself */
3001 if (top_bs
== base_bs
) {
3002 error_setg(errp
, "cannot commit an image into itself");
3007 if (has_backing_file
) {
3008 error_setg(errp
, "'backing-file' specified,"
3009 " but 'top' is the active layer");
3012 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
3015 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
3016 has_backing_file
? backing_file
: NULL
, &local_err
);
3018 if (local_err
!= NULL
) {
3019 error_propagate(errp
, local_err
);
3024 aio_context_release(aio_context
);
3027 static void do_drive_backup(const char *device
, const char *target
,
3028 bool has_format
, const char *format
,
3029 enum MirrorSyncMode sync
,
3030 bool has_mode
, enum NewImageMode mode
,
3031 bool has_speed
, int64_t speed
,
3032 bool has_bitmap
, const char *bitmap
,
3033 bool has_on_source_error
,
3034 BlockdevOnError on_source_error
,
3035 bool has_on_target_error
,
3036 BlockdevOnError on_target_error
,
3037 BlockJobTxn
*txn
, Error
**errp
)
3040 BlockDriverState
*bs
;
3041 BlockDriverState
*target_bs
;
3042 BlockDriverState
*source
= NULL
;
3043 BdrvDirtyBitmap
*bmap
= NULL
;
3044 AioContext
*aio_context
;
3045 QDict
*options
= NULL
;
3046 Error
*local_err
= NULL
;
3054 if (!has_on_source_error
) {
3055 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3057 if (!has_on_target_error
) {
3058 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3061 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3064 blk
= blk_by_name(device
);
3066 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3067 "Device '%s' not found", device
);
3071 aio_context
= blk_get_aio_context(blk
);
3072 aio_context_acquire(aio_context
);
3074 /* Although backup_run has this check too, we need to use bs->drv below, so
3075 * do an early check redundantly. */
3076 if (!blk_is_available(blk
)) {
3077 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3083 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3086 /* Early check to avoid creating target */
3087 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3091 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3093 /* See if we have a backing HD we can use to create our new image
3095 if (sync
== MIRROR_SYNC_MODE_TOP
) {
3096 source
= backing_bs(bs
);
3098 sync
= MIRROR_SYNC_MODE_FULL
;
3101 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3105 size
= bdrv_getlength(bs
);
3107 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3111 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
3114 bdrv_img_create(target
, format
, source
->filename
,
3115 source
->drv
->format_name
, NULL
,
3116 size
, flags
, &local_err
, false);
3118 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
3119 size
, flags
, &local_err
, false);
3124 error_propagate(errp
, local_err
);
3129 options
= qdict_new();
3130 qdict_put(options
, "driver", qstring_from_str(format
));
3134 ret
= bdrv_open(&target_bs
, target
, NULL
, options
, flags
, &local_err
);
3136 error_propagate(errp
, local_err
);
3140 bdrv_set_aio_context(target_bs
, aio_context
);
3143 bmap
= bdrv_find_dirty_bitmap(bs
, bitmap
);
3145 error_setg(errp
, "Bitmap '%s' could not be found", bitmap
);
3150 backup_start(bs
, target_bs
, speed
, sync
, bmap
,
3151 on_source_error
, on_target_error
,
3152 block_job_cb
, bs
, txn
, &local_err
);
3153 if (local_err
!= NULL
) {
3154 bdrv_unref(target_bs
);
3155 error_propagate(errp
, local_err
);
3160 aio_context_release(aio_context
);
3163 void qmp_drive_backup(const char *device
, const char *target
,
3164 bool has_format
, const char *format
,
3165 enum MirrorSyncMode sync
,
3166 bool has_mode
, enum NewImageMode mode
,
3167 bool has_speed
, int64_t speed
,
3168 bool has_bitmap
, const char *bitmap
,
3169 bool has_on_source_error
, BlockdevOnError on_source_error
,
3170 bool has_on_target_error
, BlockdevOnError on_target_error
,
3173 return do_drive_backup(device
, target
, has_format
, format
, sync
,
3174 has_mode
, mode
, has_speed
, speed
,
3176 has_on_source_error
, on_source_error
,
3177 has_on_target_error
, on_target_error
,
3181 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3183 return bdrv_named_nodes_list(errp
);
3186 void do_blockdev_backup(const char *device
, const char *target
,
3187 enum MirrorSyncMode sync
,
3188 bool has_speed
, int64_t speed
,
3189 bool has_on_source_error
,
3190 BlockdevOnError on_source_error
,
3191 bool has_on_target_error
,
3192 BlockdevOnError on_target_error
,
3193 BlockJobTxn
*txn
, Error
**errp
)
3195 BlockBackend
*blk
, *target_blk
;
3196 BlockDriverState
*bs
;
3197 BlockDriverState
*target_bs
;
3198 Error
*local_err
= NULL
;
3199 AioContext
*aio_context
;
3204 if (!has_on_source_error
) {
3205 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3207 if (!has_on_target_error
) {
3208 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3211 blk
= blk_by_name(device
);
3213 error_setg(errp
, "Device '%s' not found", device
);
3217 aio_context
= blk_get_aio_context(blk
);
3218 aio_context_acquire(aio_context
);
3220 if (!blk_is_available(blk
)) {
3221 error_setg(errp
, "Device '%s' has no medium", device
);
3226 target_blk
= blk_by_name(target
);
3228 error_setg(errp
, "Device '%s' not found", target
);
3232 if (!blk_is_available(target_blk
)) {
3233 error_setg(errp
, "Device '%s' has no medium", target
);
3236 target_bs
= blk_bs(target_blk
);
3238 bdrv_ref(target_bs
);
3239 bdrv_set_aio_context(target_bs
, aio_context
);
3240 backup_start(bs
, target_bs
, speed
, sync
, NULL
, on_source_error
,
3241 on_target_error
, block_job_cb
, bs
, txn
, &local_err
);
3242 if (local_err
!= NULL
) {
3243 bdrv_unref(target_bs
);
3244 error_propagate(errp
, local_err
);
3247 aio_context_release(aio_context
);
3250 void qmp_blockdev_backup(const char *device
, const char *target
,
3251 enum MirrorSyncMode sync
,
3252 bool has_speed
, int64_t speed
,
3253 bool has_on_source_error
,
3254 BlockdevOnError on_source_error
,
3255 bool has_on_target_error
,
3256 BlockdevOnError on_target_error
,
3259 do_blockdev_backup(device
, target
, sync
, has_speed
, speed
,
3260 has_on_source_error
, on_source_error
,
3261 has_on_target_error
, on_target_error
,
3265 void qmp_drive_mirror(const char *device
, const char *target
,
3266 bool has_format
, const char *format
,
3267 bool has_node_name
, const char *node_name
,
3268 bool has_replaces
, const char *replaces
,
3269 enum MirrorSyncMode sync
,
3270 bool has_mode
, enum NewImageMode mode
,
3271 bool has_speed
, int64_t speed
,
3272 bool has_granularity
, uint32_t granularity
,
3273 bool has_buf_size
, int64_t buf_size
,
3274 bool has_on_source_error
, BlockdevOnError on_source_error
,
3275 bool has_on_target_error
, BlockdevOnError on_target_error
,
3276 bool has_unmap
, bool unmap
,
3280 BlockDriverState
*bs
;
3281 BlockDriverState
*source
, *target_bs
;
3282 AioContext
*aio_context
;
3283 Error
*local_err
= NULL
;
3292 if (!has_on_source_error
) {
3293 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3295 if (!has_on_target_error
) {
3296 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3299 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3301 if (!has_granularity
) {
3304 if (!has_buf_size
) {
3311 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3312 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3313 "a value in range [512B, 64MB]");
3316 if (granularity
& (granularity
- 1)) {
3317 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3322 blk
= blk_by_name(device
);
3324 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3325 "Device '%s' not found", device
);
3329 aio_context
= blk_get_aio_context(blk
);
3330 aio_context_acquire(aio_context
);
3332 if (!blk_is_available(blk
)) {
3333 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
3339 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
3342 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
3346 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3347 source
= backing_bs(bs
);
3348 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3349 sync
= MIRROR_SYNC_MODE_FULL
;
3351 if (sync
== MIRROR_SYNC_MODE_NONE
) {
3355 size
= bdrv_getlength(bs
);
3357 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3362 BlockDriverState
*to_replace_bs
;
3363 AioContext
*replace_aio_context
;
3364 int64_t replace_size
;
3366 if (!has_node_name
) {
3367 error_setg(errp
, "a node-name must be provided when replacing a"
3368 " named node of the graph");
3372 to_replace_bs
= check_to_replace_node(bs
, replaces
, &local_err
);
3374 if (!to_replace_bs
) {
3375 error_propagate(errp
, local_err
);
3379 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3380 aio_context_acquire(replace_aio_context
);
3381 replace_size
= bdrv_getlength(to_replace_bs
);
3382 aio_context_release(replace_aio_context
);
3384 if (size
!= replace_size
) {
3385 error_setg(errp
, "cannot replace image with a mirror image of "
3391 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3392 && mode
!= NEW_IMAGE_MODE_EXISTING
)
3394 /* create new image w/o backing file */
3396 bdrv_img_create(target
, format
,
3397 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
3400 case NEW_IMAGE_MODE_EXISTING
:
3402 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3403 /* create new image with backing file */
3404 bdrv_img_create(target
, format
,
3406 source
->drv
->format_name
,
3407 NULL
, size
, flags
, &local_err
, false);
3415 error_propagate(errp
, local_err
);
3419 options
= qdict_new();
3420 if (has_node_name
) {
3421 qdict_put(options
, "node-name", qstring_from_str(node_name
));
3424 qdict_put(options
, "driver", qstring_from_str(format
));
3427 /* Mirroring takes care of copy-on-write using the source's backing
3431 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
3432 flags
| BDRV_O_NO_BACKING
, &local_err
);
3434 error_propagate(errp
, local_err
);
3438 bdrv_set_aio_context(target_bs
, aio_context
);
3440 /* pass the node name to replace to mirror start since it's loose coupling
3441 * and will allow to check whether the node still exist at mirror completion
3443 mirror_start(bs
, target_bs
,
3444 has_replaces
? replaces
: NULL
,
3445 speed
, granularity
, buf_size
, sync
,
3446 on_source_error
, on_target_error
,
3448 block_job_cb
, bs
, &local_err
);
3449 if (local_err
!= NULL
) {
3450 bdrv_unref(target_bs
);
3451 error_propagate(errp
, local_err
);
3456 aio_context_release(aio_context
);
3459 /* Get the block job for a given device name and acquire its AioContext */
3460 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
3464 BlockDriverState
*bs
;
3466 *aio_context
= NULL
;
3468 blk
= blk_by_name(device
);
3473 *aio_context
= blk_get_aio_context(blk
);
3474 aio_context_acquire(*aio_context
);
3476 if (!blk_is_available(blk
)) {
3488 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3489 "No active block job on device '%s'", device
);
3491 aio_context_release(*aio_context
);
3492 *aio_context
= NULL
;
3497 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3499 AioContext
*aio_context
;
3500 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3506 block_job_set_speed(job
, speed
, errp
);
3507 aio_context_release(aio_context
);
3510 void qmp_block_job_cancel(const char *device
,
3511 bool has_force
, bool force
, Error
**errp
)
3513 AioContext
*aio_context
;
3514 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3524 if (job
->user_paused
&& !force
) {
3525 error_setg(errp
, "The block job for device '%s' is currently paused",
3530 trace_qmp_block_job_cancel(job
);
3531 block_job_cancel(job
);
3533 aio_context_release(aio_context
);
3536 void qmp_block_job_pause(const char *device
, Error
**errp
)
3538 AioContext
*aio_context
;
3539 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3541 if (!job
|| job
->user_paused
) {
3545 job
->user_paused
= true;
3546 trace_qmp_block_job_pause(job
);
3547 block_job_pause(job
);
3548 aio_context_release(aio_context
);
3551 void qmp_block_job_resume(const char *device
, Error
**errp
)
3553 AioContext
*aio_context
;
3554 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3556 if (!job
|| !job
->user_paused
) {
3560 job
->user_paused
= false;
3561 trace_qmp_block_job_resume(job
);
3562 block_job_resume(job
);
3563 aio_context_release(aio_context
);
3566 void qmp_block_job_complete(const char *device
, Error
**errp
)
3568 AioContext
*aio_context
;
3569 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3575 trace_qmp_block_job_complete(job
);
3576 block_job_complete(job
, errp
);
3577 aio_context_release(aio_context
);
3580 void qmp_change_backing_file(const char *device
,
3581 const char *image_node_name
,
3582 const char *backing_file
,
3586 BlockDriverState
*bs
= NULL
;
3587 AioContext
*aio_context
;
3588 BlockDriverState
*image_bs
= NULL
;
3589 Error
*local_err
= NULL
;
3594 blk
= blk_by_name(device
);
3596 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3597 "Device '%s' not found", device
);
3601 aio_context
= blk_get_aio_context(blk
);
3602 aio_context_acquire(aio_context
);
3604 if (!blk_is_available(blk
)) {
3605 error_setg(errp
, "Device '%s' has no medium", device
);
3610 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3612 error_propagate(errp
, local_err
);
3617 error_setg(errp
, "image file not found");
3621 if (bdrv_find_base(image_bs
) == image_bs
) {
3622 error_setg(errp
, "not allowing backing file change on an image "
3623 "without a backing file");
3627 /* even though we are not necessarily operating on bs, we need it to
3628 * determine if block ops are currently prohibited on the chain */
3629 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3633 /* final sanity check */
3634 if (!bdrv_chain_contains(bs
, image_bs
)) {
3635 error_setg(errp
, "'%s' and image file are not in the same chain",
3640 /* if not r/w, reopen to make r/w */
3641 open_flags
= image_bs
->open_flags
;
3642 ro
= bdrv_is_read_only(image_bs
);
3645 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3647 error_propagate(errp
, local_err
);
3652 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3653 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3656 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3658 /* don't exit here, so we can try to restore open flags if
3663 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3665 error_propagate(errp
, local_err
); /* will preserve prior errp */
3670 aio_context_release(aio_context
);
3673 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3675 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
3676 BlockDriverState
*bs
;
3677 BlockBackend
*blk
= NULL
;
3680 Error
*local_err
= NULL
;
3682 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
3683 * cache.direct=false instead of silently switching to aio=threads, except
3684 * when called from drive_new().
3686 * For now, simply forbidding the combination for all drivers will do. */
3687 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
3688 bool direct
= options
->has_cache
&&
3689 options
->cache
->has_direct
&&
3690 options
->cache
->direct
;
3692 error_setg(errp
, "aio=native requires cache.direct=true");
3697 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
3698 &options
, NULL
, &local_err
);
3700 error_propagate(errp
, local_err
);
3704 obj
= qmp_output_get_qobject(ov
);
3705 qdict
= qobject_to_qdict(obj
);
3707 qdict_flatten(qdict
);
3709 if (options
->has_id
) {
3710 blk
= blockdev_init(NULL
, qdict
, &local_err
);
3712 error_propagate(errp
, local_err
);
3718 if (!qdict_get_try_str(qdict
, "node-name")) {
3719 error_setg(errp
, "'id' and/or 'node-name' need to be specified for "
3724 bs
= bds_tree_init(qdict
, errp
);
3730 if (bs
&& bdrv_key_required(bs
)) {
3736 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3741 qmp_output_visitor_cleanup(ov
);
3744 void qmp_x_blockdev_del(bool has_id
, const char *id
,
3745 bool has_node_name
, const char *node_name
, Error
**errp
)
3747 AioContext
*aio_context
;
3749 BlockDriverState
*bs
;
3751 if (has_id
&& has_node_name
) {
3752 error_setg(errp
, "Only one of id and node-name must be specified");
3754 } else if (!has_id
&& !has_node_name
) {
3755 error_setg(errp
, "No block device specified");
3760 blk
= blk_by_name(id
);
3762 error_setg(errp
, "Cannot find block backend %s", id
);
3765 if (blk_get_refcnt(blk
) > 1) {
3766 error_setg(errp
, "Block backend %s is in use", id
);
3770 aio_context
= blk_get_aio_context(blk
);
3772 bs
= bdrv_find_node(node_name
);
3774 error_setg(errp
, "Cannot find node %s", node_name
);
3779 error_setg(errp
, "Node %s is in use by %s",
3780 node_name
, blk_name(blk
));
3783 aio_context
= bdrv_get_aio_context(bs
);
3786 aio_context_acquire(aio_context
);
3789 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3793 if (bs
->refcnt
> 1 || !QLIST_EMPTY(&bs
->parents
)) {
3794 error_setg(errp
, "Block device %s is in use",
3795 bdrv_get_device_or_node_name(bs
));
3807 aio_context_release(aio_context
);
3810 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3812 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3813 BlockDriverState
*bs
;
3815 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3816 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
3818 aio_context_acquire(aio_context
);
3821 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
3822 elem
->value
= block_job_query(bs
->job
);
3824 p_next
= &elem
->next
;
3827 aio_context_release(aio_context
);
3833 QemuOptsList qemu_common_drive_opts
= {
3835 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3839 .type
= QEMU_OPT_BOOL
,
3840 .help
= "enable/disable snapshot mode",
3843 .type
= QEMU_OPT_STRING
,
3844 .help
= "discard operation (ignore/off, unmap/on)",
3846 .name
= BDRV_OPT_CACHE_WB
,
3847 .type
= QEMU_OPT_BOOL
,
3848 .help
= "enables writeback mode for any caches",
3850 .name
= BDRV_OPT_CACHE_DIRECT
,
3851 .type
= QEMU_OPT_BOOL
,
3852 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3854 .name
= BDRV_OPT_CACHE_NO_FLUSH
,
3855 .type
= QEMU_OPT_BOOL
,
3856 .help
= "ignore any flush requests for the device",
3859 .type
= QEMU_OPT_STRING
,
3860 .help
= "host AIO implementation (threads, native)",
3863 .type
= QEMU_OPT_STRING
,
3864 .help
= "disk format (raw, qcow2, ...)",
3867 .type
= QEMU_OPT_STRING
,
3868 .help
= "read error action",
3871 .type
= QEMU_OPT_STRING
,
3872 .help
= "write error action",
3874 .name
= "read-only",
3875 .type
= QEMU_OPT_BOOL
,
3876 .help
= "open drive file as read-only",
3878 .name
= "throttling.iops-total",
3879 .type
= QEMU_OPT_NUMBER
,
3880 .help
= "limit total I/O operations per second",
3882 .name
= "throttling.iops-read",
3883 .type
= QEMU_OPT_NUMBER
,
3884 .help
= "limit read operations per second",
3886 .name
= "throttling.iops-write",
3887 .type
= QEMU_OPT_NUMBER
,
3888 .help
= "limit write operations per second",
3890 .name
= "throttling.bps-total",
3891 .type
= QEMU_OPT_NUMBER
,
3892 .help
= "limit total bytes per second",
3894 .name
= "throttling.bps-read",
3895 .type
= QEMU_OPT_NUMBER
,
3896 .help
= "limit read bytes per second",
3898 .name
= "throttling.bps-write",
3899 .type
= QEMU_OPT_NUMBER
,
3900 .help
= "limit write bytes per second",
3902 .name
= "throttling.iops-total-max",
3903 .type
= QEMU_OPT_NUMBER
,
3904 .help
= "I/O operations burst",
3906 .name
= "throttling.iops-read-max",
3907 .type
= QEMU_OPT_NUMBER
,
3908 .help
= "I/O operations read burst",
3910 .name
= "throttling.iops-write-max",
3911 .type
= QEMU_OPT_NUMBER
,
3912 .help
= "I/O operations write burst",
3914 .name
= "throttling.bps-total-max",
3915 .type
= QEMU_OPT_NUMBER
,
3916 .help
= "total bytes burst",
3918 .name
= "throttling.bps-read-max",
3919 .type
= QEMU_OPT_NUMBER
,
3920 .help
= "total bytes read burst",
3922 .name
= "throttling.bps-write-max",
3923 .type
= QEMU_OPT_NUMBER
,
3924 .help
= "total bytes write burst",
3926 .name
= "throttling.iops-size",
3927 .type
= QEMU_OPT_NUMBER
,
3928 .help
= "when limiting by iops max size of an I/O in bytes",
3930 .name
= "throttling.group",
3931 .type
= QEMU_OPT_STRING
,
3932 .help
= "name of the block throttling group",
3934 .name
= "copy-on-read",
3935 .type
= QEMU_OPT_BOOL
,
3936 .help
= "copy read data from backing file into image file",
3938 .name
= "detect-zeroes",
3939 .type
= QEMU_OPT_STRING
,
3940 .help
= "try to optimize zero writes (off, on, unmap)",
3942 .name
= "stats-account-invalid",
3943 .type
= QEMU_OPT_BOOL
,
3944 .help
= "whether to account for invalid I/O operations "
3945 "in the statistics",
3947 .name
= "stats-account-failed",
3948 .type
= QEMU_OPT_BOOL
,
3949 .help
= "whether to account for failed I/O operations "
3950 "in the statistics",
3952 .name
= "stats-intervals",
3953 .type
= QEMU_OPT_STRING
,
3954 .help
= "colon-separated list of intervals "
3955 "for collecting I/O statistics, in seconds",
3957 { /* end of list */ }
3961 static QemuOptsList qemu_root_bds_opts
= {
3963 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
3967 .type
= QEMU_OPT_STRING
,
3968 .help
= "discard operation (ignore/off, unmap/on)",
3970 .name
= "cache.writeback",
3971 .type
= QEMU_OPT_BOOL
,
3972 .help
= "enables writeback mode for any caches",
3974 .name
= "cache.direct",
3975 .type
= QEMU_OPT_BOOL
,
3976 .help
= "enables use of O_DIRECT (bypass the host page cache)",
3978 .name
= "cache.no-flush",
3979 .type
= QEMU_OPT_BOOL
,
3980 .help
= "ignore any flush requests for the device",
3983 .type
= QEMU_OPT_STRING
,
3984 .help
= "host AIO implementation (threads, native)",
3986 .name
= "read-only",
3987 .type
= QEMU_OPT_BOOL
,
3988 .help
= "open drive file as read-only",
3990 .name
= "copy-on-read",
3991 .type
= QEMU_OPT_BOOL
,
3992 .help
= "copy read data from backing file into image file",
3994 .name
= "detect-zeroes",
3995 .type
= QEMU_OPT_STRING
,
3996 .help
= "try to optimize zero writes (off, on, unmap)",
3998 { /* end of list */ }
4002 QemuOptsList qemu_drive_opts
= {
4004 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4007 * no elements => accept any params
4008 * validation will happen later
4010 { /* end of list */ }