2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "qemu/osdep.h"
34 #include "sysemu/block-backend.h"
35 #include "sysemu/blockdev.h"
36 #include "hw/block/block.h"
37 #include "block/blockjob.h"
38 #include "block/throttle-groups.h"
39 #include "monitor/monitor.h"
40 #include "qemu/error-report.h"
41 #include "qemu/option.h"
42 #include "qemu/config-file.h"
43 #include "qapi/qmp/types.h"
44 #include "qapi-visit.h"
45 #include "qapi/qmp/qerror.h"
46 #include "qapi/qobject-output-visitor.h"
47 #include "qapi/util.h"
48 #include "sysemu/sysemu.h"
49 #include "block/block_int.h"
50 #include "qmp-commands.h"
51 #include "block/trace.h"
52 #include "sysemu/arch_init.h"
53 #include "sysemu/qtest.h"
54 #include "qemu/cutils.h"
55 #include "qemu/help_option.h"
56 #include "qemu/throttle-options.h"
58 static QTAILQ_HEAD(, BlockDriverState
) monitor_bdrv_states
=
59 QTAILQ_HEAD_INITIALIZER(monitor_bdrv_states
);
61 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
62 bool force
, Error
**errp
);
64 static const char *const if_name
[IF_COUNT
] = {
68 [IF_FLOPPY
] = "floppy",
69 [IF_PFLASH
] = "pflash",
72 [IF_VIRTIO
] = "virtio",
76 static int if_max_devs
[IF_COUNT
] = {
78 * Do not change these numbers! They govern how drive option
79 * index maps to unit and bus. That mapping is ABI.
81 * All controllers used to implement if=T drives need to support
82 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
83 * Otherwise, some index values map to "impossible" bus, unit
86 * For instance, if you change [IF_SCSI] to 255, -drive
87 * if=scsi,index=12 no longer means bus=1,unit=5, but
88 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
89 * the drive can't be set up. Regression.
96 * Boards may call this to offer board-by-board overrides
97 * of the default, global values.
99 void override_max_devs(BlockInterfaceType type
, int max_devs
)
108 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
109 dinfo
= blk_legacy_dinfo(blk
);
110 if (dinfo
->type
== type
) {
111 fprintf(stderr
, "Cannot override units-per-bus property of"
112 " the %s interface, because a drive of that type has"
113 " already been added.\n", if_name
[type
]);
114 g_assert_not_reached();
118 if_max_devs
[type
] = max_devs
;
122 * We automatically delete the drive when a device using it gets
123 * unplugged. Questionable feature, but we can't just drop it.
124 * Device models call blockdev_mark_auto_del() to schedule the
125 * automatic deletion, and generic qdev code calls blockdev_auto_del()
126 * when deletion is actually safe.
128 void blockdev_mark_auto_del(BlockBackend
*blk
)
130 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
131 BlockDriverState
*bs
= blk_bs(blk
);
132 AioContext
*aio_context
;
139 aio_context
= bdrv_get_aio_context(bs
);
140 aio_context_acquire(aio_context
);
143 block_job_cancel(bs
->job
);
146 aio_context_release(aio_context
);
152 void blockdev_auto_del(BlockBackend
*blk
)
154 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
156 if (dinfo
&& dinfo
->auto_del
) {
157 monitor_remove_blk(blk
);
163 * Returns the current mapping of how many units per bus
164 * a particular interface can support.
166 * A positive integer indicates n units per bus.
167 * 0 implies the mapping has not been established.
168 * -1 indicates an invalid BlockInterfaceType was given.
170 int drive_get_max_devs(BlockInterfaceType type
)
172 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
173 return if_max_devs
[type
];
179 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
181 int max_devs
= if_max_devs
[type
];
182 return max_devs
? index
/ max_devs
: 0;
185 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
187 int max_devs
= if_max_devs
[type
];
188 return max_devs
? index
% max_devs
: index
;
191 QemuOpts
*drive_def(const char *optstr
)
193 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr
, false);
196 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
201 opts
= drive_def(optstr
);
205 if (type
!= IF_DEFAULT
) {
206 qemu_opt_set(opts
, "if", if_name
[type
], &error_abort
);
209 qemu_opt_set_number(opts
, "index", index
, &error_abort
);
212 qemu_opt_set(opts
, "file", file
, &error_abort
);
216 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
221 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
222 dinfo
= blk_legacy_dinfo(blk
);
223 if (dinfo
&& dinfo
->type
== type
224 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
232 void drive_check_orphaned(void)
237 bool orphans
= false;
239 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
240 dinfo
= blk_legacy_dinfo(blk
);
241 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
242 dinfo
->type
!= IF_NONE
) {
244 qemu_opts_loc_restore(dinfo
->opts
);
245 error_report("machine type does not support"
246 " if=%s,bus=%d,unit=%d",
247 if_name
[dinfo
->type
], dinfo
->bus
, dinfo
->unit
);
258 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
260 return drive_get(type
,
261 drive_index_to_bus_id(type
, index
),
262 drive_index_to_unit_id(type
, index
));
265 int drive_get_max_bus(BlockInterfaceType type
)
272 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
273 dinfo
= blk_legacy_dinfo(blk
);
274 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
275 max_bus
= dinfo
->bus
;
281 /* Get a block device. This should only be used for single-drive devices
282 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
284 DriveInfo
*drive_get_next(BlockInterfaceType type
)
286 static int next_block_unit
[IF_COUNT
];
288 return drive_get(type
, 0, next_block_unit
[type
]++);
291 static void bdrv_format_print(void *opaque
, const char *name
)
293 error_printf(" %s", name
);
298 BlockDriverState
*bs
;
301 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
303 if (!strcmp(buf
, "ignore")) {
304 return BLOCKDEV_ON_ERROR_IGNORE
;
305 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
306 return BLOCKDEV_ON_ERROR_ENOSPC
;
307 } else if (!strcmp(buf
, "stop")) {
308 return BLOCKDEV_ON_ERROR_STOP
;
309 } else if (!strcmp(buf
, "report")) {
310 return BLOCKDEV_ON_ERROR_REPORT
;
312 error_setg(errp
, "'%s' invalid %s error action",
313 buf
, is_read
? "read" : "write");
318 static bool parse_stats_intervals(BlockAcctStats
*stats
, QList
*intervals
,
321 const QListEntry
*entry
;
322 for (entry
= qlist_first(intervals
); entry
; entry
= qlist_next(entry
)) {
323 switch (qobject_type(entry
->value
)) {
325 case QTYPE_QSTRING
: {
326 unsigned long long length
;
327 const char *str
= qstring_get_str(qobject_to_qstring(entry
->value
));
328 if (parse_uint_full(str
, &length
, 10) == 0 &&
329 length
> 0 && length
<= UINT_MAX
) {
330 block_acct_add_interval(stats
, (unsigned) length
);
332 error_setg(errp
, "Invalid interval length: %s", str
);
339 int64_t length
= qnum_get_int(qobject_to_qnum(entry
->value
));
341 if (length
> 0 && length
<= UINT_MAX
) {
342 block_acct_add_interval(stats
, (unsigned) length
);
344 error_setg(errp
, "Invalid interval length: %" PRId64
, length
);
351 error_setg(errp
, "The specification of stats-intervals is invalid");
358 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
360 /* All parameters but @opts are optional and may be set to NULL. */
361 static void extract_common_blockdev_options(QemuOpts
*opts
, int *bdrv_flags
,
362 const char **throttling_group
, ThrottleConfig
*throttle_cfg
,
363 BlockdevDetectZeroesOptions
*detect_zeroes
, Error
**errp
)
365 Error
*local_error
= NULL
;
369 if (qemu_opt_get_bool(opts
, "copy-on-read", false)) {
370 *bdrv_flags
|= BDRV_O_COPY_ON_READ
;
373 if ((aio
= qemu_opt_get(opts
, "aio")) != NULL
) {
374 if (!strcmp(aio
, "native")) {
375 *bdrv_flags
|= BDRV_O_NATIVE_AIO
;
376 } else if (!strcmp(aio
, "threads")) {
377 /* this is the default */
379 error_setg(errp
, "invalid aio option");
385 /* disk I/O throttling */
386 if (throttling_group
) {
387 *throttling_group
= qemu_opt_get(opts
, "throttling.group");
391 throttle_config_init(throttle_cfg
);
392 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].avg
=
393 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
394 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].avg
=
395 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
396 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].avg
=
397 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
398 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].avg
=
399 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
400 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].avg
=
401 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
402 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].avg
=
403 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
405 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].max
=
406 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
407 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].max
=
408 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
409 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].max
=
410 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
411 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].max
=
412 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
413 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].max
=
414 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
415 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].max
=
416 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
418 throttle_cfg
->buckets
[THROTTLE_BPS_TOTAL
].burst_length
=
419 qemu_opt_get_number(opts
, "throttling.bps-total-max-length", 1);
420 throttle_cfg
->buckets
[THROTTLE_BPS_READ
].burst_length
=
421 qemu_opt_get_number(opts
, "throttling.bps-read-max-length", 1);
422 throttle_cfg
->buckets
[THROTTLE_BPS_WRITE
].burst_length
=
423 qemu_opt_get_number(opts
, "throttling.bps-write-max-length", 1);
424 throttle_cfg
->buckets
[THROTTLE_OPS_TOTAL
].burst_length
=
425 qemu_opt_get_number(opts
, "throttling.iops-total-max-length", 1);
426 throttle_cfg
->buckets
[THROTTLE_OPS_READ
].burst_length
=
427 qemu_opt_get_number(opts
, "throttling.iops-read-max-length", 1);
428 throttle_cfg
->buckets
[THROTTLE_OPS_WRITE
].burst_length
=
429 qemu_opt_get_number(opts
, "throttling.iops-write-max-length", 1);
431 throttle_cfg
->op_size
=
432 qemu_opt_get_number(opts
, "throttling.iops-size", 0);
434 if (!throttle_is_valid(throttle_cfg
, errp
)) {
441 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
442 qemu_opt_get(opts
, "detect-zeroes"),
443 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX
,
444 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
447 error_propagate(errp
, local_error
);
453 /* Takes the ownership of bs_opts */
454 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
459 int on_read_error
, on_write_error
;
460 bool account_invalid
, account_failed
;
461 bool writethrough
, read_only
;
463 BlockDriverState
*bs
;
468 QDict
*interval_dict
= NULL
;
469 QList
*interval_list
= NULL
;
471 BlockdevDetectZeroesOptions detect_zeroes
=
472 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
;
473 const char *throttling_group
= NULL
;
475 /* Check common options by copying from bs_opts to opts, all other options
476 * stay in bs_opts for processing by bdrv_open(). */
477 id
= qdict_get_try_str(bs_opts
, "id");
478 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
480 error_propagate(errp
, error
);
484 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
486 error_propagate(errp
, error
);
491 qdict_del(bs_opts
, "id");
494 /* extract parameters */
495 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
497 account_invalid
= qemu_opt_get_bool(opts
, "stats-account-invalid", true);
498 account_failed
= qemu_opt_get_bool(opts
, "stats-account-failed", true);
500 writethrough
= !qemu_opt_get_bool(opts
, BDRV_OPT_CACHE_WB
, true);
502 id
= qemu_opts_id(opts
);
504 qdict_extract_subqdict(bs_opts
, &interval_dict
, "stats-intervals.");
505 qdict_array_split(interval_dict
, &interval_list
);
507 if (qdict_size(interval_dict
) != 0) {
508 error_setg(errp
, "Invalid option stats-intervals.%s",
509 qdict_first(interval_dict
)->key
);
513 extract_common_blockdev_options(opts
, &bdrv_flags
, &throttling_group
, &cfg
,
514 &detect_zeroes
, &error
);
516 error_propagate(errp
, error
);
520 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
521 if (is_help_option(buf
)) {
522 error_printf("Supported formats:");
523 bdrv_iterate_format(bdrv_format_print
, NULL
);
528 if (qdict_haskey(bs_opts
, "driver")) {
529 error_setg(errp
, "Cannot specify both 'driver' and 'format'");
532 qdict_put_str(bs_opts
, "driver", buf
);
535 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
536 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
537 on_write_error
= parse_block_error_action(buf
, 0, &error
);
539 error_propagate(errp
, error
);
544 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
545 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
546 on_read_error
= parse_block_error_action(buf
, 1, &error
);
548 error_propagate(errp
, error
);
554 bdrv_flags
|= BDRV_O_SNAPSHOT
;
557 read_only
= qemu_opt_get_bool(opts
, BDRV_OPT_READ_ONLY
, false);
560 if ((!file
|| !*file
) && !qdict_size(bs_opts
)) {
561 BlockBackendRootState
*blk_rs
;
563 blk
= blk_new(0, BLK_PERM_ALL
);
564 blk_rs
= blk_get_root_state(blk
);
565 blk_rs
->open_flags
= bdrv_flags
;
566 blk_rs
->read_only
= read_only
;
567 blk_rs
->detect_zeroes
= detect_zeroes
;
571 if (file
&& !*file
) {
575 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
576 * with other callers) rather than what we want as the real defaults.
577 * Apply the defaults here instead. */
578 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
579 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
580 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
,
581 read_only
? "on" : "off");
582 assert((bdrv_flags
& BDRV_O_CACHE_MASK
) == 0);
584 if (runstate_check(RUN_STATE_INMIGRATE
)) {
585 bdrv_flags
|= BDRV_O_INACTIVE
;
588 blk
= blk_new_open(file
, NULL
, bs_opts
, bdrv_flags
, errp
);
594 bs
->detect_zeroes
= detect_zeroes
;
596 if (bdrv_key_required(bs
)) {
600 block_acct_setup(blk_get_stats(blk
), account_invalid
, account_failed
);
602 if (!parse_stats_intervals(blk_get_stats(blk
), interval_list
, errp
)) {
609 /* disk I/O throttling */
610 if (throttle_enabled(&cfg
)) {
611 if (!throttling_group
) {
612 throttling_group
= id
;
614 blk_io_limits_enable(blk
, throttling_group
);
615 blk_set_io_limits(blk
, &cfg
);
618 blk_set_enable_write_cache(blk
, !writethrough
);
619 blk_set_on_error(blk
, on_read_error
, on_write_error
);
621 if (!monitor_add_blk(blk
, id
, errp
)) {
629 QDECREF(interval_dict
);
630 QDECREF(interval_list
);
635 QDECREF(interval_dict
);
636 QDECREF(interval_list
);
642 /* Takes the ownership of bs_opts */
643 static BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
)
647 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility
648 * with other callers) rather than what we want as the real defaults.
649 * Apply the defaults here instead. */
650 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_DIRECT
, "off");
651 qdict_set_default_str(bs_opts
, BDRV_OPT_CACHE_NO_FLUSH
, "off");
652 qdict_set_default_str(bs_opts
, BDRV_OPT_READ_ONLY
, "off");
654 if (runstate_check(RUN_STATE_INMIGRATE
)) {
655 bdrv_flags
|= BDRV_O_INACTIVE
;
658 return bdrv_open(NULL
, NULL
, bs_opts
, bdrv_flags
, errp
);
661 void blockdev_close_all_bdrv_states(void)
663 BlockDriverState
*bs
, *next_bs
;
665 QTAILQ_FOREACH_SAFE(bs
, &monitor_bdrv_states
, monitor_list
, next_bs
) {
666 AioContext
*ctx
= bdrv_get_aio_context(bs
);
668 aio_context_acquire(ctx
);
670 aio_context_release(ctx
);
674 /* Iterates over the list of monitor-owned BlockDriverStates */
675 BlockDriverState
*bdrv_next_monitor_owned(BlockDriverState
*bs
)
677 return bs
? QTAILQ_NEXT(bs
, monitor_list
)
678 : QTAILQ_FIRST(&monitor_bdrv_states
);
681 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
686 value
= qemu_opt_get(opts
, from
);
688 if (qemu_opt_find(opts
, to
)) {
689 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
690 "same time", to
, from
);
695 /* rename all items in opts */
696 while ((value
= qemu_opt_get(opts
, from
))) {
697 qemu_opt_set(opts
, to
, value
, &error_abort
);
698 qemu_opt_unset(opts
, from
);
702 QemuOptsList qemu_legacy_drive_opts
= {
704 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
708 .type
= QEMU_OPT_NUMBER
,
709 .help
= "bus number",
712 .type
= QEMU_OPT_NUMBER
,
713 .help
= "unit number (i.e. lun for scsi)",
716 .type
= QEMU_OPT_NUMBER
,
717 .help
= "index number",
720 .type
= QEMU_OPT_STRING
,
721 .help
= "media type (disk, cdrom)",
724 .type
= QEMU_OPT_STRING
,
725 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
728 .type
= QEMU_OPT_NUMBER
,
729 .help
= "number of cylinders (ide disk geometry)",
732 .type
= QEMU_OPT_NUMBER
,
733 .help
= "number of heads (ide disk geometry)",
736 .type
= QEMU_OPT_NUMBER
,
737 .help
= "number of sectors (ide disk geometry)",
740 .type
= QEMU_OPT_STRING
,
741 .help
= "chs translation (auto, lba, none)",
744 .type
= QEMU_OPT_BOOL
,
745 .help
= "(deprecated, ignored)",
748 .type
= QEMU_OPT_STRING
,
749 .help
= "pci address (virtio only)",
752 .type
= QEMU_OPT_STRING
,
753 .help
= "disk serial number",
756 .type
= QEMU_OPT_STRING
,
760 /* Options that are passed on, but have special semantics with -drive */
762 .name
= BDRV_OPT_READ_ONLY
,
763 .type
= QEMU_OPT_BOOL
,
764 .help
= "open drive file as read-only",
767 .type
= QEMU_OPT_STRING
,
768 .help
= "read error action",
771 .type
= QEMU_OPT_STRING
,
772 .help
= "write error action",
774 .name
= "copy-on-read",
775 .type
= QEMU_OPT_BOOL
,
776 .help
= "copy read data from backing file into image file",
779 { /* end of list */ }
783 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
787 DriveInfo
*dinfo
= NULL
;
789 QemuOpts
*legacy_opts
;
790 DriveMediaType media
= MEDIA_DISK
;
791 BlockInterfaceType type
;
792 int cyls
, heads
, secs
, translation
;
793 int max_devs
, bus_id
, unit_id
, index
;
795 const char *werror
, *rerror
;
796 bool read_only
= false;
799 const char *filename
;
800 Error
*local_err
= NULL
;
802 const char *deprecated
[] = {
803 "serial", "trans", "secs", "heads", "cyls", "addr"
806 /* Change legacy command line options into QMP ones */
807 static const struct {
811 { "iops", "throttling.iops-total" },
812 { "iops_rd", "throttling.iops-read" },
813 { "iops_wr", "throttling.iops-write" },
815 { "bps", "throttling.bps-total" },
816 { "bps_rd", "throttling.bps-read" },
817 { "bps_wr", "throttling.bps-write" },
819 { "iops_max", "throttling.iops-total-max" },
820 { "iops_rd_max", "throttling.iops-read-max" },
821 { "iops_wr_max", "throttling.iops-write-max" },
823 { "bps_max", "throttling.bps-total-max" },
824 { "bps_rd_max", "throttling.bps-read-max" },
825 { "bps_wr_max", "throttling.bps-write-max" },
827 { "iops_size", "throttling.iops-size" },
829 { "group", "throttling.group" },
831 { "readonly", BDRV_OPT_READ_ONLY
},
834 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
835 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
838 error_report_err(local_err
);
843 value
= qemu_opt_get(all_opts
, "cache");
848 if (bdrv_parse_cache_mode(value
, &flags
, &writethrough
) != 0) {
849 error_report("invalid cache option");
853 /* Specific options take precedence */
854 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_WB
)) {
855 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_WB
,
856 !writethrough
, &error_abort
);
858 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_DIRECT
)) {
859 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_DIRECT
,
860 !!(flags
& BDRV_O_NOCACHE
), &error_abort
);
862 if (!qemu_opt_get(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
)) {
863 qemu_opt_set_bool(all_opts
, BDRV_OPT_CACHE_NO_FLUSH
,
864 !!(flags
& BDRV_O_NO_FLUSH
), &error_abort
);
866 qemu_opt_unset(all_opts
, "cache");
869 /* Get a QDict for processing the options */
870 bs_opts
= qdict_new();
871 qemu_opts_to_qdict(all_opts
, bs_opts
);
873 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
875 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
877 error_report_err(local_err
);
881 /* Deprecated option boot=[on|off] */
882 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
883 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
884 "ignored. Future versions will reject this parameter. Please "
885 "update your scripts.\n");
888 /* Other deprecated options */
889 if (!qtest_enabled()) {
890 for (i
= 0; i
< ARRAY_SIZE(deprecated
); i
++) {
891 if (qemu_opt_get(legacy_opts
, deprecated
[i
]) != NULL
) {
892 error_report("'%s' is deprecated, please use the corresponding "
893 "option of '-device' instead", deprecated
[i
]);
899 value
= qemu_opt_get(legacy_opts
, "media");
901 if (!strcmp(value
, "disk")) {
903 } else if (!strcmp(value
, "cdrom")) {
907 error_report("'%s' invalid media", value
);
912 /* copy-on-read is disabled with a warning for read-only devices */
913 read_only
|= qemu_opt_get_bool(legacy_opts
, BDRV_OPT_READ_ONLY
, false);
914 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
916 if (read_only
&& copy_on_read
) {
917 error_report("warning: disabling copy-on-read on read-only drive");
918 copy_on_read
= false;
921 qdict_put_str(bs_opts
, BDRV_OPT_READ_ONLY
, read_only
? "on" : "off");
922 qdict_put_str(bs_opts
, "copy-on-read", copy_on_read
? "on" : "off");
924 /* Controller type */
925 value
= qemu_opt_get(legacy_opts
, "if");
928 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
931 if (type
== IF_COUNT
) {
932 error_report("unsupported bus type '%s'", value
);
936 type
= block_default_type
;
940 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
941 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
942 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
944 if (cyls
|| heads
|| secs
) {
946 error_report("invalid physical cyls number");
950 error_report("invalid physical heads number");
954 error_report("invalid physical secs number");
959 translation
= BIOS_ATA_TRANSLATION_AUTO
;
960 value
= qemu_opt_get(legacy_opts
, "trans");
963 error_report("'%s' trans must be used with cyls, heads and secs",
967 if (!strcmp(value
, "none")) {
968 translation
= BIOS_ATA_TRANSLATION_NONE
;
969 } else if (!strcmp(value
, "lba")) {
970 translation
= BIOS_ATA_TRANSLATION_LBA
;
971 } else if (!strcmp(value
, "large")) {
972 translation
= BIOS_ATA_TRANSLATION_LARGE
;
973 } else if (!strcmp(value
, "rechs")) {
974 translation
= BIOS_ATA_TRANSLATION_RECHS
;
975 } else if (!strcmp(value
, "auto")) {
976 translation
= BIOS_ATA_TRANSLATION_AUTO
;
978 error_report("'%s' invalid translation type", value
);
983 if (media
== MEDIA_CDROM
) {
984 if (cyls
|| secs
|| heads
) {
985 error_report("CHS can't be set with media=cdrom");
990 /* Device address specified by bus/unit or index.
991 * If none was specified, try to find the first free one. */
992 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
993 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
994 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
996 max_devs
= if_max_devs
[type
];
999 if (bus_id
!= 0 || unit_id
!= -1) {
1000 error_report("index cannot be used with bus and unit");
1003 bus_id
= drive_index_to_bus_id(type
, index
);
1004 unit_id
= drive_index_to_unit_id(type
, index
);
1007 if (unit_id
== -1) {
1009 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1011 if (max_devs
&& unit_id
>= max_devs
) {
1012 unit_id
-= max_devs
;
1018 if (max_devs
&& unit_id
>= max_devs
) {
1019 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
1023 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1024 error_report("drive with bus=%d, unit=%d (index=%d) exists",
1025 bus_id
, unit_id
, index
);
1030 serial
= qemu_opt_get(legacy_opts
, "serial");
1032 /* no id supplied -> create one */
1033 if (qemu_opts_id(all_opts
) == NULL
) {
1035 const char *mediastr
= "";
1036 if (type
== IF_IDE
|| type
== IF_SCSI
) {
1037 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1040 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
1043 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
1046 qdict_put_str(bs_opts
, "id", new_id
);
1050 /* Add virtio block device */
1051 devaddr
= qemu_opt_get(legacy_opts
, "addr");
1052 if (devaddr
&& type
!= IF_VIRTIO
) {
1053 error_report("addr is not supported by this bus type");
1057 if (type
== IF_VIRTIO
) {
1059 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
1061 if (arch_type
== QEMU_ARCH_S390X
) {
1062 qemu_opt_set(devopts
, "driver", "virtio-blk-ccw", &error_abort
);
1064 qemu_opt_set(devopts
, "driver", "virtio-blk-pci", &error_abort
);
1066 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"),
1069 qemu_opt_set(devopts
, "addr", devaddr
, &error_abort
);
1073 filename
= qemu_opt_get(legacy_opts
, "file");
1075 /* Check werror/rerror compatibility with if=... */
1076 werror
= qemu_opt_get(legacy_opts
, "werror");
1077 if (werror
!= NULL
) {
1078 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
1080 error_report("werror is not supported by this bus type");
1083 qdict_put_str(bs_opts
, "werror", werror
);
1086 rerror
= qemu_opt_get(legacy_opts
, "rerror");
1087 if (rerror
!= NULL
) {
1088 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
1090 error_report("rerror is not supported by this bus type");
1093 qdict_put_str(bs_opts
, "rerror", rerror
);
1096 /* Actual block device init: Functionality shared with blockdev-add */
1097 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
1101 error_report_err(local_err
);
1108 /* Create legacy DriveInfo */
1109 dinfo
= g_malloc0(sizeof(*dinfo
));
1110 dinfo
->opts
= all_opts
;
1113 dinfo
->heads
= heads
;
1115 dinfo
->trans
= translation
;
1118 dinfo
->bus
= bus_id
;
1119 dinfo
->unit
= unit_id
;
1120 dinfo
->devaddr
= devaddr
;
1121 dinfo
->serial
= g_strdup(serial
);
1123 blk_set_legacy_dinfo(blk
, dinfo
);
1130 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1137 qemu_opts_del(legacy_opts
);
1142 static BlockDriverState
*qmp_get_root_bs(const char *name
, Error
**errp
)
1144 BlockDriverState
*bs
;
1146 bs
= bdrv_lookup_bs(name
, name
, errp
);
1151 if (!bdrv_is_root_node(bs
)) {
1152 error_setg(errp
, "Need a root block node");
1156 if (!bdrv_is_inserted(bs
)) {
1157 error_setg(errp
, "Device has no medium");
1164 static BlockBackend
*qmp_get_blk(const char *blk_name
, const char *qdev_id
,
1169 if (!blk_name
== !qdev_id
) {
1170 error_setg(errp
, "Need exactly one of 'device' and 'id'");
1175 blk
= blk_by_qdev_id(qdev_id
, errp
);
1177 blk
= blk_by_name(blk_name
);
1179 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1180 "Device '%s' not found", blk_name
);
1187 void hmp_commit(Monitor
*mon
, const QDict
*qdict
)
1189 const char *device
= qdict_get_str(qdict
, "device");
1193 if (!strcmp(device
, "all")) {
1194 ret
= blk_commit_all();
1196 BlockDriverState
*bs
;
1197 AioContext
*aio_context
;
1199 blk
= blk_by_name(device
);
1201 monitor_printf(mon
, "Device '%s' not found\n", device
);
1204 if (!blk_is_available(blk
)) {
1205 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
1210 aio_context
= bdrv_get_aio_context(bs
);
1211 aio_context_acquire(aio_context
);
1213 ret
= bdrv_commit(bs
);
1215 aio_context_release(aio_context
);
1218 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1223 static void blockdev_do_action(TransactionAction
*action
, Error
**errp
)
1225 TransactionActionList list
;
1227 list
.value
= action
;
1229 qmp_transaction(&list
, false, NULL
, errp
);
1232 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1233 bool has_node_name
, const char *node_name
,
1234 const char *snapshot_file
,
1235 bool has_snapshot_node_name
,
1236 const char *snapshot_node_name
,
1237 bool has_format
, const char *format
,
1238 bool has_mode
, NewImageMode mode
, Error
**errp
)
1240 BlockdevSnapshotSync snapshot
= {
1241 .has_device
= has_device
,
1242 .device
= (char *) device
,
1243 .has_node_name
= has_node_name
,
1244 .node_name
= (char *) node_name
,
1245 .snapshot_file
= (char *) snapshot_file
,
1246 .has_snapshot_node_name
= has_snapshot_node_name
,
1247 .snapshot_node_name
= (char *) snapshot_node_name
,
1248 .has_format
= has_format
,
1249 .format
= (char *) format
,
1250 .has_mode
= has_mode
,
1253 TransactionAction action
= {
1254 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1255 .u
.blockdev_snapshot_sync
.data
= &snapshot
,
1257 blockdev_do_action(&action
, errp
);
1260 void qmp_blockdev_snapshot(const char *node
, const char *overlay
,
1263 BlockdevSnapshot snapshot_data
= {
1264 .node
= (char *) node
,
1265 .overlay
= (char *) overlay
1267 TransactionAction action
= {
1268 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
,
1269 .u
.blockdev_snapshot
.data
= &snapshot_data
,
1271 blockdev_do_action(&action
, errp
);
1274 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1278 BlockdevSnapshotInternal snapshot
= {
1279 .device
= (char *) device
,
1280 .name
= (char *) name
1282 TransactionAction action
= {
1283 .type
= TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1284 .u
.blockdev_snapshot_internal_sync
.data
= &snapshot
,
1286 blockdev_do_action(&action
, errp
);
1289 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1296 BlockDriverState
*bs
;
1297 AioContext
*aio_context
;
1298 QEMUSnapshotInfo sn
;
1299 Error
*local_err
= NULL
;
1300 SnapshotInfo
*info
= NULL
;
1303 bs
= qmp_get_root_bs(device
, errp
);
1307 aio_context
= bdrv_get_aio_context(bs
);
1308 aio_context_acquire(aio_context
);
1319 error_setg(errp
, "Name or id must be provided");
1320 goto out_aio_context
;
1323 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1324 goto out_aio_context
;
1327 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1329 error_propagate(errp
, local_err
);
1330 goto out_aio_context
;
1334 "Snapshot with id '%s' and name '%s' does not exist on "
1336 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1337 goto out_aio_context
;
1340 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1342 error_propagate(errp
, local_err
);
1343 goto out_aio_context
;
1346 aio_context_release(aio_context
);
1348 info
= g_new0(SnapshotInfo
, 1);
1349 info
->id
= g_strdup(sn
.id_str
);
1350 info
->name
= g_strdup(sn
.name
);
1351 info
->date_nsec
= sn
.date_nsec
;
1352 info
->date_sec
= sn
.date_sec
;
1353 info
->vm_state_size
= sn
.vm_state_size
;
1354 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1355 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1360 aio_context_release(aio_context
);
1365 * block_dirty_bitmap_lookup:
1366 * Return a dirty bitmap (if present), after validating
1367 * the node reference and bitmap names.
1369 * @node: The name of the BDS node to search for bitmaps
1370 * @name: The name of the bitmap to search for
1371 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL.
1372 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL.
1373 * @errp: Output pointer for error information. Can be NULL.
1375 * @return: A bitmap object on success, or NULL on failure.
1377 static BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1379 BlockDriverState
**pbs
,
1382 BlockDriverState
*bs
;
1383 BdrvDirtyBitmap
*bitmap
;
1386 error_setg(errp
, "Node cannot be NULL");
1390 error_setg(errp
, "Bitmap name cannot be NULL");
1393 bs
= bdrv_lookup_bs(node
, node
, NULL
);
1395 error_setg(errp
, "Node '%s' not found", node
);
1399 bitmap
= bdrv_find_dirty_bitmap(bs
, name
);
1401 error_setg(errp
, "Dirty bitmap '%s' not found", name
);
1412 /* New and old BlockDriverState structs for atomic group operations */
1414 typedef struct BlkActionState BlkActionState
;
1418 * Table of operations that define an Action.
1420 * @instance_size: Size of state struct, in bytes.
1421 * @prepare: Prepare the work, must NOT be NULL.
1422 * @commit: Commit the changes, can be NULL.
1423 * @abort: Abort the changes on fail, can be NULL.
1424 * @clean: Clean up resources after all transaction actions have called
1425 * commit() or abort(). Can be NULL.
1427 * Only prepare() may fail. In a single transaction, only one of commit() or
1428 * abort() will be called. clean() will always be called if it is present.
1430 typedef struct BlkActionOps
{
1431 size_t instance_size
;
1432 void (*prepare
)(BlkActionState
*common
, Error
**errp
);
1433 void (*commit
)(BlkActionState
*common
);
1434 void (*abort
)(BlkActionState
*common
);
1435 void (*clean
)(BlkActionState
*common
);
1440 * Describes one Action's state within a Transaction.
1442 * @action: QAPI-defined enum identifying which Action to perform.
1443 * @ops: Table of ActionOps this Action can perform.
1444 * @block_job_txn: Transaction which this action belongs to.
1445 * @entry: List membership for all Actions in this Transaction.
1447 * This structure must be arranged as first member in a subclassed type,
1448 * assuming that the compiler will also arrange it to the same offsets as the
1451 struct BlkActionState
{
1452 TransactionAction
*action
;
1453 const BlkActionOps
*ops
;
1454 BlockJobTxn
*block_job_txn
;
1455 TransactionProperties
*txn_props
;
1456 QSIMPLEQ_ENTRY(BlkActionState
) entry
;
1459 /* internal snapshot private data */
1460 typedef struct InternalSnapshotState
{
1461 BlkActionState common
;
1462 BlockDriverState
*bs
;
1463 AioContext
*aio_context
;
1464 QEMUSnapshotInfo sn
;
1466 } InternalSnapshotState
;
1469 static int action_check_completion_mode(BlkActionState
*s
, Error
**errp
)
1471 if (s
->txn_props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
1473 "Action '%s' does not support Transaction property "
1474 "completion-mode = %s",
1475 TransactionActionKind_lookup
[s
->action
->type
],
1476 ActionCompletionMode_lookup
[s
->txn_props
->completion_mode
]);
1482 static void internal_snapshot_prepare(BlkActionState
*common
,
1485 Error
*local_err
= NULL
;
1488 BlockDriverState
*bs
;
1489 QEMUSnapshotInfo old_sn
, *sn
;
1492 BlockdevSnapshotInternal
*internal
;
1493 InternalSnapshotState
*state
;
1496 g_assert(common
->action
->type
==
1497 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1498 internal
= common
->action
->u
.blockdev_snapshot_internal_sync
.data
;
1499 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1501 /* 1. parse input */
1502 device
= internal
->device
;
1503 name
= internal
->name
;
1505 /* 2. check for validation */
1506 if (action_check_completion_mode(common
, errp
) < 0) {
1510 bs
= qmp_get_root_bs(device
, errp
);
1515 /* AioContext is released in .clean() */
1516 state
->aio_context
= bdrv_get_aio_context(bs
);
1517 aio_context_acquire(state
->aio_context
);
1520 bdrv_drained_begin(bs
);
1522 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1526 if (bdrv_is_read_only(bs
)) {
1527 error_setg(errp
, "Device '%s' is read only", device
);
1531 if (!bdrv_can_snapshot(bs
)) {
1532 error_setg(errp
, "Block format '%s' used by device '%s' "
1533 "does not support internal snapshots",
1534 bs
->drv
->format_name
, device
);
1538 if (!strlen(name
)) {
1539 error_setg(errp
, "Name is empty");
1543 /* check whether a snapshot with name exist */
1544 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1547 error_propagate(errp
, local_err
);
1551 "Snapshot with name '%s' already exists on device '%s'",
1556 /* 3. take the snapshot */
1558 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1559 qemu_gettimeofday(&tv
);
1560 sn
->date_sec
= tv
.tv_sec
;
1561 sn
->date_nsec
= tv
.tv_usec
* 1000;
1562 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1564 ret1
= bdrv_snapshot_create(bs
, sn
);
1566 error_setg_errno(errp
, -ret1
,
1567 "Failed to create snapshot '%s' on device '%s'",
1572 /* 4. succeed, mark a snapshot is created */
1573 state
->created
= true;
1576 static void internal_snapshot_abort(BlkActionState
*common
)
1578 InternalSnapshotState
*state
=
1579 DO_UPCAST(InternalSnapshotState
, common
, common
);
1580 BlockDriverState
*bs
= state
->bs
;
1581 QEMUSnapshotInfo
*sn
= &state
->sn
;
1582 Error
*local_error
= NULL
;
1584 if (!state
->created
) {
1588 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1589 error_reportf_err(local_error
,
1590 "Failed to delete snapshot with id '%s' and "
1591 "name '%s' on device '%s' in abort: ",
1592 sn
->id_str
, sn
->name
,
1593 bdrv_get_device_name(bs
));
1597 static void internal_snapshot_clean(BlkActionState
*common
)
1599 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1602 if (state
->aio_context
) {
1604 bdrv_drained_end(state
->bs
);
1606 aio_context_release(state
->aio_context
);
1610 /* external snapshot private data */
1611 typedef struct ExternalSnapshotState
{
1612 BlkActionState common
;
1613 BlockDriverState
*old_bs
;
1614 BlockDriverState
*new_bs
;
1615 AioContext
*aio_context
;
1616 bool overlay_appended
;
1617 } ExternalSnapshotState
;
1619 static void external_snapshot_prepare(BlkActionState
*common
,
1623 QDict
*options
= NULL
;
1624 Error
*local_err
= NULL
;
1625 /* Device and node name of the image to generate the snapshot from */
1627 const char *node_name
;
1628 /* Reference to the new image (for 'blockdev-snapshot') */
1629 const char *snapshot_ref
;
1630 /* File name of the new image (for 'blockdev-snapshot-sync') */
1631 const char *new_image_file
;
1632 ExternalSnapshotState
*state
=
1633 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1634 TransactionAction
*action
= common
->action
;
1636 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar
1637 * purpose but a different set of parameters */
1638 switch (action
->type
) {
1639 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
:
1641 BlockdevSnapshot
*s
= action
->u
.blockdev_snapshot
.data
;
1643 node_name
= s
->node
;
1644 new_image_file
= NULL
;
1645 snapshot_ref
= s
->overlay
;
1648 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
:
1650 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1651 device
= s
->has_device
? s
->device
: NULL
;
1652 node_name
= s
->has_node_name
? s
->node_name
: NULL
;
1653 new_image_file
= s
->snapshot_file
;
1654 snapshot_ref
= NULL
;
1658 g_assert_not_reached();
1661 /* start processing */
1662 if (action_check_completion_mode(common
, errp
) < 0) {
1666 state
->old_bs
= bdrv_lookup_bs(device
, node_name
, errp
);
1667 if (!state
->old_bs
) {
1671 /* Acquire AioContext now so any threads operating on old_bs stop */
1672 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1673 aio_context_acquire(state
->aio_context
);
1674 bdrv_drained_begin(state
->old_bs
);
1676 if (!bdrv_is_inserted(state
->old_bs
)) {
1677 error_setg(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1681 if (bdrv_op_is_blocked(state
->old_bs
,
1682 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1686 if (!bdrv_is_read_only(state
->old_bs
)) {
1687 if (bdrv_flush(state
->old_bs
)) {
1688 error_setg(errp
, QERR_IO_ERROR
);
1693 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1694 error_setg(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1698 if (action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
) {
1699 BlockdevSnapshotSync
*s
= action
->u
.blockdev_snapshot_sync
.data
;
1700 const char *format
= s
->has_format
? s
->format
: "qcow2";
1701 enum NewImageMode mode
;
1702 const char *snapshot_node_name
=
1703 s
->has_snapshot_node_name
? s
->snapshot_node_name
: NULL
;
1705 if (node_name
&& !snapshot_node_name
) {
1706 error_setg(errp
, "New snapshot node name missing");
1710 if (snapshot_node_name
&&
1711 bdrv_lookup_bs(snapshot_node_name
, snapshot_node_name
, NULL
)) {
1712 error_setg(errp
, "New snapshot node name already in use");
1716 flags
= state
->old_bs
->open_flags
;
1717 flags
&= ~(BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
| BDRV_O_COPY_ON_READ
);
1719 /* create new image w/backing file */
1720 mode
= s
->has_mode
? s
->mode
: NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1721 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1722 int64_t size
= bdrv_getlength(state
->old_bs
);
1724 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1727 bdrv_img_create(new_image_file
, format
,
1728 state
->old_bs
->filename
,
1729 state
->old_bs
->drv
->format_name
,
1730 NULL
, size
, flags
, false, &local_err
);
1732 error_propagate(errp
, local_err
);
1737 options
= qdict_new();
1738 if (s
->has_snapshot_node_name
) {
1739 qdict_put_str(options
, "node-name", snapshot_node_name
);
1741 qdict_put_str(options
, "driver", format
);
1743 flags
|= BDRV_O_NO_BACKING
;
1746 state
->new_bs
= bdrv_open(new_image_file
, snapshot_ref
, options
, flags
,
1748 /* We will manually add the backing_hd field to the bs later */
1749 if (!state
->new_bs
) {
1753 if (bdrv_has_blk(state
->new_bs
)) {
1754 error_setg(errp
, "The snapshot is already in use");
1758 if (bdrv_op_is_blocked(state
->new_bs
, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
,
1763 if (state
->new_bs
->backing
!= NULL
) {
1764 error_setg(errp
, "The snapshot already has a backing image");
1768 if (!state
->new_bs
->drv
->supports_backing
) {
1769 error_setg(errp
, "The snapshot does not support backing images");
1773 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1775 /* This removes our old bs and adds the new bs. This is an operation that
1776 * can fail, so we need to do it in .prepare; undoing it for abort is
1777 * always possible. */
1778 bdrv_ref(state
->new_bs
);
1779 bdrv_append(state
->new_bs
, state
->old_bs
, &local_err
);
1781 error_propagate(errp
, local_err
);
1784 state
->overlay_appended
= true;
1787 static void external_snapshot_commit(BlkActionState
*common
)
1789 ExternalSnapshotState
*state
=
1790 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1792 /* We don't need (or want) to use the transactional
1793 * bdrv_reopen_multiple() across all the entries at once, because we
1794 * don't want to abort all of them if one of them fails the reopen */
1795 if (!atomic_read(&state
->old_bs
->copy_on_read
)) {
1796 bdrv_reopen(state
->old_bs
, state
->old_bs
->open_flags
& ~BDRV_O_RDWR
,
1801 static void external_snapshot_abort(BlkActionState
*common
)
1803 ExternalSnapshotState
*state
=
1804 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1805 if (state
->new_bs
) {
1806 if (state
->overlay_appended
) {
1807 bdrv_ref(state
->old_bs
); /* we can't let bdrv_set_backind_hd()
1808 close state->old_bs; we need it */
1809 bdrv_set_backing_hd(state
->new_bs
, NULL
, &error_abort
);
1810 bdrv_replace_node(state
->new_bs
, state
->old_bs
, &error_abort
);
1811 bdrv_unref(state
->old_bs
); /* bdrv_replace_node() ref'ed old_bs */
1816 static void external_snapshot_clean(BlkActionState
*common
)
1818 ExternalSnapshotState
*state
=
1819 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1820 if (state
->aio_context
) {
1821 bdrv_drained_end(state
->old_bs
);
1822 aio_context_release(state
->aio_context
);
1823 bdrv_unref(state
->new_bs
);
1827 typedef struct DriveBackupState
{
1828 BlkActionState common
;
1829 BlockDriverState
*bs
;
1830 AioContext
*aio_context
;
1834 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
1837 static void drive_backup_prepare(BlkActionState
*common
, Error
**errp
)
1839 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1840 BlockDriverState
*bs
;
1841 DriveBackup
*backup
;
1842 Error
*local_err
= NULL
;
1844 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1845 backup
= common
->action
->u
.drive_backup
.data
;
1847 bs
= qmp_get_root_bs(backup
->device
, errp
);
1852 /* AioContext is released in .clean() */
1853 state
->aio_context
= bdrv_get_aio_context(bs
);
1854 aio_context_acquire(state
->aio_context
);
1855 bdrv_drained_begin(bs
);
1858 state
->job
= do_drive_backup(backup
, common
->block_job_txn
, &local_err
);
1860 error_propagate(errp
, local_err
);
1865 static void drive_backup_commit(BlkActionState
*common
)
1867 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1869 block_job_start(state
->job
);
1872 static void drive_backup_abort(BlkActionState
*common
)
1874 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1877 block_job_cancel_sync(state
->job
);
1881 static void drive_backup_clean(BlkActionState
*common
)
1883 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1885 if (state
->aio_context
) {
1886 bdrv_drained_end(state
->bs
);
1887 aio_context_release(state
->aio_context
);
1891 typedef struct BlockdevBackupState
{
1892 BlkActionState common
;
1893 BlockDriverState
*bs
;
1895 AioContext
*aio_context
;
1896 } BlockdevBackupState
;
1898 static BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
1901 static void blockdev_backup_prepare(BlkActionState
*common
, Error
**errp
)
1903 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1904 BlockdevBackup
*backup
;
1905 BlockDriverState
*bs
, *target
;
1906 Error
*local_err
= NULL
;
1908 assert(common
->action
->type
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1909 backup
= common
->action
->u
.blockdev_backup
.data
;
1911 bs
= qmp_get_root_bs(backup
->device
, errp
);
1916 target
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
1921 /* AioContext is released in .clean() */
1922 state
->aio_context
= bdrv_get_aio_context(bs
);
1923 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1924 state
->aio_context
= NULL
;
1925 error_setg(errp
, "Backup between two IO threads is not implemented");
1928 aio_context_acquire(state
->aio_context
);
1930 bdrv_drained_begin(state
->bs
);
1932 state
->job
= do_blockdev_backup(backup
, common
->block_job_txn
, &local_err
);
1934 error_propagate(errp
, local_err
);
1939 static void blockdev_backup_commit(BlkActionState
*common
)
1941 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1943 block_job_start(state
->job
);
1946 static void blockdev_backup_abort(BlkActionState
*common
)
1948 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1951 block_job_cancel_sync(state
->job
);
1955 static void blockdev_backup_clean(BlkActionState
*common
)
1957 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1959 if (state
->aio_context
) {
1960 bdrv_drained_end(state
->bs
);
1961 aio_context_release(state
->aio_context
);
1965 typedef struct BlockDirtyBitmapState
{
1966 BlkActionState common
;
1967 BdrvDirtyBitmap
*bitmap
;
1968 BlockDriverState
*bs
;
1969 AioContext
*aio_context
;
1972 } BlockDirtyBitmapState
;
1974 static void block_dirty_bitmap_add_prepare(BlkActionState
*common
,
1977 Error
*local_err
= NULL
;
1978 BlockDirtyBitmapAdd
*action
;
1979 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
1982 if (action_check_completion_mode(common
, errp
) < 0) {
1986 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
1987 /* AIO context taken and released within qmp_block_dirty_bitmap_add */
1988 qmp_block_dirty_bitmap_add(action
->node
, action
->name
,
1989 action
->has_granularity
, action
->granularity
,
1993 state
->prepared
= true;
1995 error_propagate(errp
, local_err
);
1999 static void block_dirty_bitmap_add_abort(BlkActionState
*common
)
2001 BlockDirtyBitmapAdd
*action
;
2002 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2005 action
= common
->action
->u
.block_dirty_bitmap_add
.data
;
2006 /* Should not be able to fail: IF the bitmap was added via .prepare(),
2007 * then the node reference and bitmap name must have been valid.
2009 if (state
->prepared
) {
2010 qmp_block_dirty_bitmap_remove(action
->node
, action
->name
, &error_abort
);
2014 static void block_dirty_bitmap_clear_prepare(BlkActionState
*common
,
2017 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2019 BlockDirtyBitmap
*action
;
2021 if (action_check_completion_mode(common
, errp
) < 0) {
2025 action
= common
->action
->u
.block_dirty_bitmap_clear
.data
;
2026 state
->bitmap
= block_dirty_bitmap_lookup(action
->node
,
2030 if (!state
->bitmap
) {
2034 if (bdrv_dirty_bitmap_frozen(state
->bitmap
)) {
2035 error_setg(errp
, "Cannot modify a frozen bitmap");
2037 } else if (!bdrv_dirty_bitmap_enabled(state
->bitmap
)) {
2038 error_setg(errp
, "Cannot clear a disabled bitmap");
2042 bdrv_clear_dirty_bitmap(state
->bitmap
, &state
->backup
);
2043 /* AioContext is released in .clean() */
2046 static void block_dirty_bitmap_clear_abort(BlkActionState
*common
)
2048 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2051 if (state
->backup
) {
2052 bdrv_undo_clear_dirty_bitmap(state
->bitmap
, state
->backup
);
2056 static void block_dirty_bitmap_clear_commit(BlkActionState
*common
)
2058 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2061 hbitmap_free(state
->backup
);
2064 static void block_dirty_bitmap_clear_clean(BlkActionState
*common
)
2066 BlockDirtyBitmapState
*state
= DO_UPCAST(BlockDirtyBitmapState
,
2069 if (state
->aio_context
) {
2070 aio_context_release(state
->aio_context
);
2074 static void abort_prepare(BlkActionState
*common
, Error
**errp
)
2076 error_setg(errp
, "Transaction aborted using Abort action");
2079 static void abort_commit(BlkActionState
*common
)
2081 g_assert_not_reached(); /* this action never succeeds */
2084 static const BlkActionOps actions
[] = {
2085 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT
] = {
2086 .instance_size
= sizeof(ExternalSnapshotState
),
2087 .prepare
= external_snapshot_prepare
,
2088 .commit
= external_snapshot_commit
,
2089 .abort
= external_snapshot_abort
,
2090 .clean
= external_snapshot_clean
,
2092 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
2093 .instance_size
= sizeof(ExternalSnapshotState
),
2094 .prepare
= external_snapshot_prepare
,
2095 .commit
= external_snapshot_commit
,
2096 .abort
= external_snapshot_abort
,
2097 .clean
= external_snapshot_clean
,
2099 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
2100 .instance_size
= sizeof(DriveBackupState
),
2101 .prepare
= drive_backup_prepare
,
2102 .commit
= drive_backup_commit
,
2103 .abort
= drive_backup_abort
,
2104 .clean
= drive_backup_clean
,
2106 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
2107 .instance_size
= sizeof(BlockdevBackupState
),
2108 .prepare
= blockdev_backup_prepare
,
2109 .commit
= blockdev_backup_commit
,
2110 .abort
= blockdev_backup_abort
,
2111 .clean
= blockdev_backup_clean
,
2113 [TRANSACTION_ACTION_KIND_ABORT
] = {
2114 .instance_size
= sizeof(BlkActionState
),
2115 .prepare
= abort_prepare
,
2116 .commit
= abort_commit
,
2118 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
2119 .instance_size
= sizeof(InternalSnapshotState
),
2120 .prepare
= internal_snapshot_prepare
,
2121 .abort
= internal_snapshot_abort
,
2122 .clean
= internal_snapshot_clean
,
2124 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD
] = {
2125 .instance_size
= sizeof(BlockDirtyBitmapState
),
2126 .prepare
= block_dirty_bitmap_add_prepare
,
2127 .abort
= block_dirty_bitmap_add_abort
,
2129 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR
] = {
2130 .instance_size
= sizeof(BlockDirtyBitmapState
),
2131 .prepare
= block_dirty_bitmap_clear_prepare
,
2132 .commit
= block_dirty_bitmap_clear_commit
,
2133 .abort
= block_dirty_bitmap_clear_abort
,
2134 .clean
= block_dirty_bitmap_clear_clean
,
2139 * Allocate a TransactionProperties structure if necessary, and fill
2140 * that structure with desired defaults if they are unset.
2142 static TransactionProperties
*get_transaction_properties(
2143 TransactionProperties
*props
)
2146 props
= g_new0(TransactionProperties
, 1);
2149 if (!props
->has_completion_mode
) {
2150 props
->has_completion_mode
= true;
2151 props
->completion_mode
= ACTION_COMPLETION_MODE_INDIVIDUAL
;
2158 * 'Atomic' group operations. The operations are performed as a set, and if
2159 * any fail then we roll back all operations in the group.
2161 void qmp_transaction(TransactionActionList
*dev_list
,
2163 struct TransactionProperties
*props
,
2166 TransactionActionList
*dev_entry
= dev_list
;
2167 BlockJobTxn
*block_job_txn
= NULL
;
2168 BlkActionState
*state
, *next
;
2169 Error
*local_err
= NULL
;
2171 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkActionState
) snap_bdrv_states
;
2172 QSIMPLEQ_INIT(&snap_bdrv_states
);
2174 /* Does this transaction get canceled as a group on failure?
2175 * If not, we don't really need to make a BlockJobTxn.
2177 props
= get_transaction_properties(props
);
2178 if (props
->completion_mode
!= ACTION_COMPLETION_MODE_INDIVIDUAL
) {
2179 block_job_txn
= block_job_txn_new();
2182 /* drain all i/o before any operations */
2185 /* We don't do anything in this loop that commits us to the operations */
2186 while (NULL
!= dev_entry
) {
2187 TransactionAction
*dev_info
= NULL
;
2188 const BlkActionOps
*ops
;
2190 dev_info
= dev_entry
->value
;
2191 dev_entry
= dev_entry
->next
;
2193 assert(dev_info
->type
< ARRAY_SIZE(actions
));
2195 ops
= &actions
[dev_info
->type
];
2196 assert(ops
->instance_size
> 0);
2198 state
= g_malloc0(ops
->instance_size
);
2200 state
->action
= dev_info
;
2201 state
->block_job_txn
= block_job_txn
;
2202 state
->txn_props
= props
;
2203 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
2205 state
->ops
->prepare(state
, &local_err
);
2207 error_propagate(errp
, local_err
);
2208 goto delete_and_fail
;
2212 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2213 if (state
->ops
->commit
) {
2214 state
->ops
->commit(state
);
2222 /* failure, and it is all-or-none; roll back all operations */
2223 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
2224 if (state
->ops
->abort
) {
2225 state
->ops
->abort(state
);
2229 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
2230 if (state
->ops
->clean
) {
2231 state
->ops
->clean(state
);
2236 qapi_free_TransactionProperties(props
);
2238 block_job_txn_unref(block_job_txn
);
2241 void qmp_eject(bool has_device
, const char *device
,
2242 bool has_id
, const char *id
,
2243 bool has_force
, bool force
, Error
**errp
)
2245 Error
*local_err
= NULL
;
2252 rc
= do_open_tray(has_device
? device
: NULL
,
2255 if (rc
&& rc
!= -ENOSYS
) {
2256 error_propagate(errp
, local_err
);
2259 error_free(local_err
);
2261 qmp_x_blockdev_remove_medium(has_device
, device
, has_id
, id
, errp
);
2264 void qmp_block_passwd(bool has_device
, const char *device
,
2265 bool has_node_name
, const char *node_name
,
2266 const char *password
, Error
**errp
)
2268 Error
*local_err
= NULL
;
2269 BlockDriverState
*bs
;
2270 AioContext
*aio_context
;
2272 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2273 has_node_name
? node_name
: NULL
,
2276 error_propagate(errp
, local_err
);
2280 aio_context
= bdrv_get_aio_context(bs
);
2281 aio_context_acquire(aio_context
);
2283 bdrv_add_key(bs
, password
, errp
);
2285 aio_context_release(aio_context
);
2289 * Attempt to open the tray of @device.
2290 * If @force, ignore its tray lock.
2291 * Else, if the tray is locked, don't open it, but ask the guest to open it.
2292 * On error, store an error through @errp and return -errno.
2293 * If @device does not exist, return -ENODEV.
2294 * If it has no removable media, return -ENOTSUP.
2295 * If it has no tray, return -ENOSYS.
2296 * If the guest was asked to open the tray, return -EINPROGRESS.
2299 static int do_open_tray(const char *blk_name
, const char *qdev_id
,
2300 bool force
, Error
**errp
)
2303 const char *device
= qdev_id
?: blk_name
;
2306 blk
= qmp_get_blk(blk_name
, qdev_id
, errp
);
2311 if (!blk_dev_has_removable_media(blk
)) {
2312 error_setg(errp
, "Device '%s' is not removable", device
);
2316 if (!blk_dev_has_tray(blk
)) {
2317 error_setg(errp
, "Device '%s' does not have a tray", device
);
2321 if (blk_dev_is_tray_open(blk
)) {
2325 locked
= blk_dev_is_medium_locked(blk
);
2327 blk_dev_eject_request(blk
, force
);
2330 if (!locked
|| force
) {
2331 blk_dev_change_media_cb(blk
, false, &error_abort
);
2334 if (locked
&& !force
) {
2335 error_setg(errp
, "Device '%s' is locked and force was not specified, "
2336 "wait for tray to open and try again", device
);
2337 return -EINPROGRESS
;
2343 void qmp_blockdev_open_tray(bool has_device
, const char *device
,
2344 bool has_id
, const char *id
,
2345 bool has_force
, bool force
,
2348 Error
*local_err
= NULL
;
2354 rc
= do_open_tray(has_device
? device
: NULL
,
2357 if (rc
&& rc
!= -ENOSYS
&& rc
!= -EINPROGRESS
) {
2358 error_propagate(errp
, local_err
);
2361 error_free(local_err
);
2364 void qmp_blockdev_close_tray(bool has_device
, const char *device
,
2365 bool has_id
, const char *id
,
2369 Error
*local_err
= NULL
;
2371 device
= has_device
? device
: NULL
;
2372 id
= has_id
? id
: NULL
;
2374 blk
= qmp_get_blk(device
, id
, errp
);
2379 if (!blk_dev_has_removable_media(blk
)) {
2380 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2384 if (!blk_dev_has_tray(blk
)) {
2385 /* Ignore this command on tray-less devices */
2389 if (!blk_dev_is_tray_open(blk
)) {
2393 blk_dev_change_media_cb(blk
, true, &local_err
);
2395 error_propagate(errp
, local_err
);
2400 void qmp_x_blockdev_remove_medium(bool has_device
, const char *device
,
2401 bool has_id
, const char *id
, Error
**errp
)
2404 BlockDriverState
*bs
;
2405 AioContext
*aio_context
;
2406 bool has_attached_device
;
2408 device
= has_device
? device
: NULL
;
2409 id
= has_id
? id
: NULL
;
2411 blk
= qmp_get_blk(device
, id
, errp
);
2416 /* For BBs without a device, we can exchange the BDS tree at will */
2417 has_attached_device
= blk_get_attached_dev(blk
);
2419 if (has_attached_device
&& !blk_dev_has_removable_media(blk
)) {
2420 error_setg(errp
, "Device '%s' is not removable", device
?: id
);
2424 if (has_attached_device
&& blk_dev_has_tray(blk
) &&
2425 !blk_dev_is_tray_open(blk
))
2427 error_setg(errp
, "Tray of device '%s' is not open", device
?: id
);
2436 aio_context
= bdrv_get_aio_context(bs
);
2437 aio_context_acquire(aio_context
);
2439 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
2445 if (!blk_dev_has_tray(blk
)) {
2446 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be
2447 * called at all); therefore, the medium needs to be ejected here.
2448 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load
2449 * value passed here (i.e. false). */
2450 blk_dev_change_media_cb(blk
, false, &error_abort
);
2454 aio_context_release(aio_context
);
2457 static void qmp_blockdev_insert_anon_medium(BlockBackend
*blk
,
2458 BlockDriverState
*bs
, Error
**errp
)
2460 Error
*local_err
= NULL
;
2464 /* For BBs without a device, we can exchange the BDS tree at will */
2465 has_device
= blk_get_attached_dev(blk
);
2467 if (has_device
&& !blk_dev_has_removable_media(blk
)) {
2468 error_setg(errp
, "Device is not removable");
2472 if (has_device
&& blk_dev_has_tray(blk
) && !blk_dev_is_tray_open(blk
)) {
2473 error_setg(errp
, "Tray of the device is not open");
2478 error_setg(errp
, "There already is a medium in the device");
2482 ret
= blk_insert_bs(blk
, bs
, errp
);
2487 if (!blk_dev_has_tray(blk
)) {
2488 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be
2489 * called at all); therefore, the medium needs to be pushed into the
2491 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load
2492 * value passed here (i.e. true). */
2493 blk_dev_change_media_cb(blk
, true, &local_err
);
2495 error_propagate(errp
, local_err
);
2502 void qmp_x_blockdev_insert_medium(bool has_device
, const char *device
,
2503 bool has_id
, const char *id
,
2504 const char *node_name
, Error
**errp
)
2507 BlockDriverState
*bs
;
2509 blk
= qmp_get_blk(has_device
? device
: NULL
,
2516 bs
= bdrv_find_node(node_name
);
2518 error_setg(errp
, "Node '%s' not found", node_name
);
2522 if (bdrv_has_blk(bs
)) {
2523 error_setg(errp
, "Node '%s' is already in use", node_name
);
2527 qmp_blockdev_insert_anon_medium(blk
, bs
, errp
);
2530 void qmp_blockdev_change_medium(bool has_device
, const char *device
,
2531 bool has_id
, const char *id
,
2532 const char *filename
,
2533 bool has_format
, const char *format
,
2535 BlockdevChangeReadOnlyMode read_only
,
2539 BlockDriverState
*medium_bs
= NULL
;
2543 QDict
*options
= NULL
;
2546 blk
= qmp_get_blk(has_device
? device
: NULL
,
2554 blk_update_root_state(blk
);
2557 bdrv_flags
= blk_get_open_flags_from_root_state(blk
);
2558 bdrv_flags
&= ~(BDRV_O_TEMPORARY
| BDRV_O_SNAPSHOT
| BDRV_O_NO_BACKING
|
2561 if (!has_read_only
) {
2562 read_only
= BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
;
2565 switch (read_only
) {
2566 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN
:
2569 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY
:
2570 bdrv_flags
&= ~BDRV_O_RDWR
;
2573 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE
:
2574 bdrv_flags
|= BDRV_O_RDWR
;
2581 options
= qdict_new();
2582 detect_zeroes
= blk_get_detect_zeroes_from_root_state(blk
);
2583 qdict_put_str(options
, "detect-zeroes", detect_zeroes
? "on" : "off");
2586 qdict_put_str(options
, "driver", format
);
2589 medium_bs
= bdrv_open(filename
, NULL
, options
, bdrv_flags
, errp
);
2594 bdrv_add_key(medium_bs
, NULL
, &err
);
2596 error_propagate(errp
, err
);
2600 rc
= do_open_tray(has_device
? device
: NULL
,
2603 if (rc
&& rc
!= -ENOSYS
) {
2604 error_propagate(errp
, err
);
2610 qmp_x_blockdev_remove_medium(has_device
, device
, has_id
, id
, &err
);
2612 error_propagate(errp
, err
);
2616 qmp_blockdev_insert_anon_medium(blk
, medium_bs
, &err
);
2618 error_propagate(errp
, err
);
2622 qmp_blockdev_close_tray(has_device
, device
, has_id
, id
, errp
);
2625 /* If the medium has been inserted, the device has its own reference, so
2626 * ours must be relinquished; and if it has not been inserted successfully,
2627 * the reference must be relinquished anyway */
2628 bdrv_unref(medium_bs
);
2631 /* throttling disk I/O limits */
2632 void qmp_block_set_io_throttle(BlockIOThrottle
*arg
, Error
**errp
)
2635 BlockDriverState
*bs
;
2637 AioContext
*aio_context
;
2639 blk
= qmp_get_blk(arg
->has_device
? arg
->device
: NULL
,
2640 arg
->has_id
? arg
->id
: NULL
,
2646 aio_context
= blk_get_aio_context(blk
);
2647 aio_context_acquire(aio_context
);
2651 error_setg(errp
, "Device has no medium");
2655 throttle_config_init(&cfg
);
2656 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= arg
->bps
;
2657 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= arg
->bps_rd
;
2658 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= arg
->bps_wr
;
2660 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= arg
->iops
;
2661 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= arg
->iops_rd
;
2662 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= arg
->iops_wr
;
2664 if (arg
->has_bps_max
) {
2665 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= arg
->bps_max
;
2667 if (arg
->has_bps_rd_max
) {
2668 cfg
.buckets
[THROTTLE_BPS_READ
].max
= arg
->bps_rd_max
;
2670 if (arg
->has_bps_wr_max
) {
2671 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= arg
->bps_wr_max
;
2673 if (arg
->has_iops_max
) {
2674 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= arg
->iops_max
;
2676 if (arg
->has_iops_rd_max
) {
2677 cfg
.buckets
[THROTTLE_OPS_READ
].max
= arg
->iops_rd_max
;
2679 if (arg
->has_iops_wr_max
) {
2680 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= arg
->iops_wr_max
;
2683 if (arg
->has_bps_max_length
) {
2684 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
= arg
->bps_max_length
;
2686 if (arg
->has_bps_rd_max_length
) {
2687 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
= arg
->bps_rd_max_length
;
2689 if (arg
->has_bps_wr_max_length
) {
2690 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
= arg
->bps_wr_max_length
;
2692 if (arg
->has_iops_max_length
) {
2693 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
= arg
->iops_max_length
;
2695 if (arg
->has_iops_rd_max_length
) {
2696 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
= arg
->iops_rd_max_length
;
2698 if (arg
->has_iops_wr_max_length
) {
2699 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
= arg
->iops_wr_max_length
;
2702 if (arg
->has_iops_size
) {
2703 cfg
.op_size
= arg
->iops_size
;
2706 if (!throttle_is_valid(&cfg
, errp
)) {
2710 if (throttle_enabled(&cfg
)) {
2711 /* Enable I/O limits if they're not enabled yet, otherwise
2712 * just update the throttling group. */
2713 if (!blk_get_public(blk
)->throttle_state
) {
2714 blk_io_limits_enable(blk
,
2715 arg
->has_group
? arg
->group
:
2716 arg
->has_device
? arg
->device
:
2718 } else if (arg
->has_group
) {
2719 blk_io_limits_update_group(blk
, arg
->group
);
2721 /* Set the new throttling configuration */
2722 blk_set_io_limits(blk
, &cfg
);
2723 } else if (blk_get_public(blk
)->throttle_state
) {
2724 /* If all throttling settings are set to 0, disable I/O limits */
2725 blk_io_limits_disable(blk
);
2729 aio_context_release(aio_context
);
2732 void qmp_block_dirty_bitmap_add(const char *node
, const char *name
,
2733 bool has_granularity
, uint32_t granularity
,
2736 BlockDriverState
*bs
;
2738 if (!name
|| name
[0] == '\0') {
2739 error_setg(errp
, "Bitmap name cannot be empty");
2743 bs
= bdrv_lookup_bs(node
, node
, errp
);
2748 if (has_granularity
) {
2749 if (granularity
< 512 || !is_power_of_2(granularity
)) {
2750 error_setg(errp
, "Granularity must be power of 2 "
2751 "and at least 512");
2755 /* Default to cluster size, if available: */
2756 granularity
= bdrv_get_default_bitmap_granularity(bs
);
2759 bdrv_create_dirty_bitmap(bs
, granularity
, name
, errp
);
2762 void qmp_block_dirty_bitmap_remove(const char *node
, const char *name
,
2765 BlockDriverState
*bs
;
2766 BdrvDirtyBitmap
*bitmap
;
2768 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2769 if (!bitmap
|| !bs
) {
2773 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2775 "Bitmap '%s' is currently frozen and cannot be removed",
2779 bdrv_dirty_bitmap_make_anon(bitmap
);
2780 bdrv_release_dirty_bitmap(bs
, bitmap
);
2784 * Completely clear a bitmap, for the purposes of synchronizing a bitmap
2785 * immediately after a full backup operation.
2787 void qmp_block_dirty_bitmap_clear(const char *node
, const char *name
,
2790 BdrvDirtyBitmap
*bitmap
;
2791 BlockDriverState
*bs
;
2793 bitmap
= block_dirty_bitmap_lookup(node
, name
, &bs
, errp
);
2794 if (!bitmap
|| !bs
) {
2798 if (bdrv_dirty_bitmap_frozen(bitmap
)) {
2800 "Bitmap '%s' is currently frozen and cannot be modified",
2803 } else if (!bdrv_dirty_bitmap_enabled(bitmap
)) {
2805 "Bitmap '%s' is currently disabled and cannot be cleared",
2810 bdrv_clear_dirty_bitmap(bitmap
, NULL
);
2813 void hmp_drive_del(Monitor
*mon
, const QDict
*qdict
)
2815 const char *id
= qdict_get_str(qdict
, "id");
2817 BlockDriverState
*bs
;
2818 AioContext
*aio_context
;
2819 Error
*local_err
= NULL
;
2821 bs
= bdrv_find_node(id
);
2823 qmp_blockdev_del(id
, &local_err
);
2825 error_report_err(local_err
);
2830 blk
= blk_by_name(id
);
2832 error_report("Device '%s' not found", id
);
2836 if (!blk_legacy_dinfo(blk
)) {
2837 error_report("Deleting device added with blockdev-add"
2838 " is not supported");
2842 aio_context
= blk_get_aio_context(blk
);
2843 aio_context_acquire(aio_context
);
2847 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
2848 error_report_err(local_err
);
2849 aio_context_release(aio_context
);
2856 /* Make the BlockBackend and the attached BlockDriverState anonymous */
2857 monitor_remove_blk(blk
);
2859 /* If this BlockBackend has a device attached to it, its refcount will be
2860 * decremented when the device is removed; otherwise we have to do so here.
2862 if (blk_get_attached_dev(blk
)) {
2863 /* Further I/O must not pause the guest */
2864 blk_set_on_error(blk
, BLOCKDEV_ON_ERROR_REPORT
,
2865 BLOCKDEV_ON_ERROR_REPORT
);
2870 aio_context_release(aio_context
);
2873 void qmp_block_resize(bool has_device
, const char *device
,
2874 bool has_node_name
, const char *node_name
,
2875 int64_t size
, Error
**errp
)
2877 Error
*local_err
= NULL
;
2878 BlockBackend
*blk
= NULL
;
2879 BlockDriverState
*bs
;
2880 AioContext
*aio_context
;
2883 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2884 has_node_name
? node_name
: NULL
,
2887 error_propagate(errp
, local_err
);
2891 aio_context
= bdrv_get_aio_context(bs
);
2892 aio_context_acquire(aio_context
);
2894 if (!bdrv_is_first_non_filter(bs
)) {
2895 error_setg(errp
, QERR_FEATURE_DISABLED
, "resize");
2900 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2904 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2905 error_setg(errp
, QERR_DEVICE_IN_USE
, device
);
2909 blk
= blk_new(BLK_PERM_RESIZE
, BLK_PERM_ALL
);
2910 ret
= blk_insert_bs(blk
, bs
, errp
);
2915 bdrv_drained_begin(bs
);
2916 ret
= blk_truncate(blk
, size
, errp
);
2917 bdrv_drained_end(bs
);
2921 aio_context_release(aio_context
);
2924 void qmp_block_stream(bool has_job_id
, const char *job_id
, const char *device
,
2925 bool has_base
, const char *base
,
2926 bool has_base_node
, const char *base_node
,
2927 bool has_backing_file
, const char *backing_file
,
2928 bool has_speed
, int64_t speed
,
2929 bool has_on_error
, BlockdevOnError on_error
,
2932 BlockDriverState
*bs
, *iter
;
2933 BlockDriverState
*base_bs
= NULL
;
2934 AioContext
*aio_context
;
2935 Error
*local_err
= NULL
;
2936 const char *base_name
= NULL
;
2938 if (!has_on_error
) {
2939 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2942 bs
= bdrv_lookup_bs(device
, device
, errp
);
2947 aio_context
= bdrv_get_aio_context(bs
);
2948 aio_context_acquire(aio_context
);
2950 if (has_base
&& has_base_node
) {
2951 error_setg(errp
, "'base' and 'base-node' cannot be specified "
2952 "at the same time");
2957 base_bs
= bdrv_find_backing_image(bs
, base
);
2958 if (base_bs
== NULL
) {
2959 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
);
2962 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2966 if (has_base_node
) {
2967 base_bs
= bdrv_lookup_bs(NULL
, base_node
, errp
);
2971 if (bs
== base_bs
|| !bdrv_chain_contains(bs
, base_bs
)) {
2972 error_setg(errp
, "Node '%s' is not a backing image of '%s'",
2976 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2977 base_name
= base_bs
->filename
;
2980 /* Check for op blockers in the whole chain between bs and base */
2981 for (iter
= bs
; iter
&& iter
!= base_bs
; iter
= backing_bs(iter
)) {
2982 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2987 /* if we are streaming the entire chain, the result will have no backing
2988 * file, and specifying one is therefore an error */
2989 if (base_bs
== NULL
&& has_backing_file
) {
2990 error_setg(errp
, "backing file specified, but streaming the "
2995 /* backing_file string overrides base bs filename */
2996 base_name
= has_backing_file
? backing_file
: base_name
;
2998 stream_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, base_name
,
2999 has_speed
? speed
: 0, on_error
, &local_err
);
3001 error_propagate(errp
, local_err
);
3005 trace_qmp_block_stream(bs
, bs
->job
);
3008 aio_context_release(aio_context
);
3011 void qmp_block_commit(bool has_job_id
, const char *job_id
, const char *device
,
3012 bool has_base
, const char *base
,
3013 bool has_top
, const char *top
,
3014 bool has_backing_file
, const char *backing_file
,
3015 bool has_speed
, int64_t speed
,
3016 bool has_filter_node_name
, const char *filter_node_name
,
3019 BlockDriverState
*bs
;
3020 BlockDriverState
*iter
;
3021 BlockDriverState
*base_bs
, *top_bs
;
3022 AioContext
*aio_context
;
3023 Error
*local_err
= NULL
;
3024 /* This will be part of the QMP command, if/when the
3025 * BlockdevOnError change for blkmirror makes it in
3027 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
3032 if (!has_filter_node_name
) {
3033 filter_node_name
= NULL
;
3037 * libvirt relies on the DeviceNotFound error class in order to probe for
3038 * live commit feature versions; for this to work, we must make sure to
3039 * perform the device lookup before any generic errors that may occur in a
3040 * scenario in which all optional arguments are omitted. */
3041 bs
= qmp_get_root_bs(device
, &local_err
);
3043 bs
= bdrv_lookup_bs(device
, device
, NULL
);
3045 error_free(local_err
);
3046 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
3047 "Device '%s' not found", device
);
3049 error_propagate(errp
, local_err
);
3054 aio_context
= bdrv_get_aio_context(bs
);
3055 aio_context_acquire(aio_context
);
3057 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
3061 /* default top_bs is the active layer */
3064 if (has_top
&& top
) {
3065 if (strcmp(bs
->filename
, top
) != 0) {
3066 top_bs
= bdrv_find_backing_image(bs
, top
);
3070 if (top_bs
== NULL
) {
3071 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
3075 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
3077 if (has_base
&& base
) {
3078 base_bs
= bdrv_find_backing_image(top_bs
, base
);
3080 base_bs
= bdrv_find_base(top_bs
);
3083 if (base_bs
== NULL
) {
3084 error_setg(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
3088 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
3090 for (iter
= top_bs
; iter
!= backing_bs(base_bs
); iter
= backing_bs(iter
)) {
3091 if (bdrv_op_is_blocked(iter
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3096 /* Do not allow attempts to commit an image into itself */
3097 if (top_bs
== base_bs
) {
3098 error_setg(errp
, "cannot commit an image into itself");
3103 if (has_backing_file
) {
3104 error_setg(errp
, "'backing-file' specified,"
3105 " but 'top' is the active layer");
3108 commit_active_start(has_job_id
? job_id
: NULL
, bs
, base_bs
,
3109 BLOCK_JOB_DEFAULT
, speed
, on_error
,
3110 filter_node_name
, NULL
, NULL
, false, &local_err
);
3112 BlockDriverState
*overlay_bs
= bdrv_find_overlay(bs
, top_bs
);
3113 if (bdrv_op_is_blocked(overlay_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
3116 commit_start(has_job_id
? job_id
: NULL
, bs
, base_bs
, top_bs
, speed
,
3117 on_error
, has_backing_file
? backing_file
: NULL
,
3118 filter_node_name
, &local_err
);
3120 if (local_err
!= NULL
) {
3121 error_propagate(errp
, local_err
);
3126 aio_context_release(aio_context
);
3129 static BlockJob
*do_drive_backup(DriveBackup
*backup
, BlockJobTxn
*txn
,
3132 BlockDriverState
*bs
;
3133 BlockDriverState
*target_bs
;
3134 BlockDriverState
*source
= NULL
;
3135 BlockJob
*job
= NULL
;
3136 BdrvDirtyBitmap
*bmap
= NULL
;
3137 AioContext
*aio_context
;
3138 QDict
*options
= NULL
;
3139 Error
*local_err
= NULL
;
3142 bool set_backing_hd
= false;
3144 if (!backup
->has_speed
) {
3147 if (!backup
->has_on_source_error
) {
3148 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3150 if (!backup
->has_on_target_error
) {
3151 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3153 if (!backup
->has_mode
) {
3154 backup
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3156 if (!backup
->has_job_id
) {
3157 backup
->job_id
= NULL
;
3159 if (!backup
->has_compress
) {
3160 backup
->compress
= false;
3163 bs
= qmp_get_root_bs(backup
->device
, errp
);
3168 aio_context
= bdrv_get_aio_context(bs
);
3169 aio_context_acquire(aio_context
);
3171 if (!backup
->has_format
) {
3172 backup
->format
= backup
->mode
== NEW_IMAGE_MODE_EXISTING
?
3173 NULL
: (char*) bs
->drv
->format_name
;
3176 /* Early check to avoid creating target */
3177 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
3181 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3183 /* See if we have a backing HD we can use to create our new image
3185 if (backup
->sync
== MIRROR_SYNC_MODE_TOP
) {
3186 source
= backing_bs(bs
);
3188 backup
->sync
= MIRROR_SYNC_MODE_FULL
;
3191 if (backup
->sync
== MIRROR_SYNC_MODE_NONE
) {
3193 flags
|= BDRV_O_NO_BACKING
;
3194 set_backing_hd
= true;
3197 size
= bdrv_getlength(bs
);
3199 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3203 if (backup
->mode
!= NEW_IMAGE_MODE_EXISTING
) {
3204 assert(backup
->format
);
3206 bdrv_img_create(backup
->target
, backup
->format
, source
->filename
,
3207 source
->drv
->format_name
, NULL
,
3208 size
, flags
, false, &local_err
);
3210 bdrv_img_create(backup
->target
, backup
->format
, NULL
, NULL
, NULL
,
3211 size
, flags
, false, &local_err
);
3216 error_propagate(errp
, local_err
);
3220 if (backup
->format
) {
3222 options
= qdict_new();
3224 qdict_put_str(options
, "driver", backup
->format
);
3227 target_bs
= bdrv_open(backup
->target
, NULL
, options
, flags
, errp
);
3232 bdrv_set_aio_context(target_bs
, aio_context
);
3234 if (set_backing_hd
) {
3235 bdrv_set_backing_hd(target_bs
, source
, &local_err
);
3237 bdrv_unref(target_bs
);
3242 if (backup
->has_bitmap
) {
3243 bmap
= bdrv_find_dirty_bitmap(bs
, backup
->bitmap
);
3245 error_setg(errp
, "Bitmap '%s' could not be found", backup
->bitmap
);
3246 bdrv_unref(target_bs
);
3251 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3252 backup
->sync
, bmap
, backup
->compress
,
3253 backup
->on_source_error
, backup
->on_target_error
,
3254 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3255 bdrv_unref(target_bs
);
3256 if (local_err
!= NULL
) {
3257 error_propagate(errp
, local_err
);
3262 aio_context_release(aio_context
);
3266 void qmp_drive_backup(DriveBackup
*arg
, Error
**errp
)
3270 job
= do_drive_backup(arg
, NULL
, errp
);
3272 block_job_start(job
);
3276 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
3278 return bdrv_named_nodes_list(errp
);
3281 BlockJob
*do_blockdev_backup(BlockdevBackup
*backup
, BlockJobTxn
*txn
,
3284 BlockDriverState
*bs
;
3285 BlockDriverState
*target_bs
;
3286 Error
*local_err
= NULL
;
3287 AioContext
*aio_context
;
3288 BlockJob
*job
= NULL
;
3290 if (!backup
->has_speed
) {
3293 if (!backup
->has_on_source_error
) {
3294 backup
->on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3296 if (!backup
->has_on_target_error
) {
3297 backup
->on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3299 if (!backup
->has_job_id
) {
3300 backup
->job_id
= NULL
;
3302 if (!backup
->has_compress
) {
3303 backup
->compress
= false;
3306 bs
= qmp_get_root_bs(backup
->device
, errp
);
3311 aio_context
= bdrv_get_aio_context(bs
);
3312 aio_context_acquire(aio_context
);
3314 target_bs
= bdrv_lookup_bs(backup
->target
, backup
->target
, errp
);
3319 if (bdrv_get_aio_context(target_bs
) != aio_context
) {
3320 if (!bdrv_has_blk(target_bs
)) {
3321 /* The target BDS is not attached, we can safely move it to another
3323 bdrv_set_aio_context(target_bs
, aio_context
);
3325 error_setg(errp
, "Target is attached to a different thread from "
3330 job
= backup_job_create(backup
->job_id
, bs
, target_bs
, backup
->speed
,
3331 backup
->sync
, NULL
, backup
->compress
,
3332 backup
->on_source_error
, backup
->on_target_error
,
3333 BLOCK_JOB_DEFAULT
, NULL
, NULL
, txn
, &local_err
);
3334 if (local_err
!= NULL
) {
3335 error_propagate(errp
, local_err
);
3338 aio_context_release(aio_context
);
3342 void qmp_blockdev_backup(BlockdevBackup
*arg
, Error
**errp
)
3345 job
= do_blockdev_backup(arg
, NULL
, errp
);
3347 block_job_start(job
);
3351 /* Parameter check and block job starting for drive mirroring.
3352 * Caller should hold @device and @target's aio context (must be the same).
3354 static void blockdev_mirror_common(const char *job_id
, BlockDriverState
*bs
,
3355 BlockDriverState
*target
,
3356 bool has_replaces
, const char *replaces
,
3357 enum MirrorSyncMode sync
,
3358 BlockMirrorBackingMode backing_mode
,
3359 bool has_speed
, int64_t speed
,
3360 bool has_granularity
, uint32_t granularity
,
3361 bool has_buf_size
, int64_t buf_size
,
3362 bool has_on_source_error
,
3363 BlockdevOnError on_source_error
,
3364 bool has_on_target_error
,
3365 BlockdevOnError on_target_error
,
3366 bool has_unmap
, bool unmap
,
3367 bool has_filter_node_name
,
3368 const char *filter_node_name
,
3375 if (!has_on_source_error
) {
3376 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
3378 if (!has_on_target_error
) {
3379 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
3381 if (!has_granularity
) {
3384 if (!has_buf_size
) {
3390 if (!has_filter_node_name
) {
3391 filter_node_name
= NULL
;
3394 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
3395 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3396 "a value in range [512B, 64MB]");
3399 if (granularity
& (granularity
- 1)) {
3400 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
3405 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR_SOURCE
, errp
)) {
3408 if (bdrv_op_is_blocked(target
, BLOCK_OP_TYPE_MIRROR_TARGET
, errp
)) {
3412 if (!bs
->backing
&& sync
== MIRROR_SYNC_MODE_TOP
) {
3413 sync
= MIRROR_SYNC_MODE_FULL
;
3416 /* pass the node name to replace to mirror start since it's loose coupling
3417 * and will allow to check whether the node still exist at mirror completion
3419 mirror_start(job_id
, bs
, target
,
3420 has_replaces
? replaces
: NULL
,
3421 speed
, granularity
, buf_size
, sync
, backing_mode
,
3422 on_source_error
, on_target_error
, unmap
, filter_node_name
,
3426 void qmp_drive_mirror(DriveMirror
*arg
, Error
**errp
)
3428 BlockDriverState
*bs
;
3429 BlockDriverState
*source
, *target_bs
;
3430 AioContext
*aio_context
;
3431 BlockMirrorBackingMode backing_mode
;
3432 Error
*local_err
= NULL
;
3433 QDict
*options
= NULL
;
3436 const char *format
= arg
->format
;
3438 bs
= qmp_get_root_bs(arg
->device
, errp
);
3443 aio_context
= bdrv_get_aio_context(bs
);
3444 aio_context_acquire(aio_context
);
3446 if (!arg
->has_mode
) {
3447 arg
->mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
3450 if (!arg
->has_format
) {
3451 format
= (arg
->mode
== NEW_IMAGE_MODE_EXISTING
3452 ? NULL
: bs
->drv
->format_name
);
3455 flags
= bs
->open_flags
| BDRV_O_RDWR
;
3456 source
= backing_bs(bs
);
3457 if (!source
&& arg
->sync
== MIRROR_SYNC_MODE_TOP
) {
3458 arg
->sync
= MIRROR_SYNC_MODE_FULL
;
3460 if (arg
->sync
== MIRROR_SYNC_MODE_NONE
) {
3464 size
= bdrv_getlength(bs
);
3466 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
3470 if (arg
->has_replaces
) {
3471 BlockDriverState
*to_replace_bs
;
3472 AioContext
*replace_aio_context
;
3473 int64_t replace_size
;
3475 if (!arg
->has_node_name
) {
3476 error_setg(errp
, "a node-name must be provided when replacing a"
3477 " named node of the graph");
3481 to_replace_bs
= check_to_replace_node(bs
, arg
->replaces
, &local_err
);
3483 if (!to_replace_bs
) {
3484 error_propagate(errp
, local_err
);
3488 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
3489 aio_context_acquire(replace_aio_context
);
3490 replace_size
= bdrv_getlength(to_replace_bs
);
3491 aio_context_release(replace_aio_context
);
3493 if (size
!= replace_size
) {
3494 error_setg(errp
, "cannot replace image with a mirror image of "
3500 if (arg
->mode
== NEW_IMAGE_MODE_ABSOLUTE_PATHS
) {
3501 backing_mode
= MIRROR_SOURCE_BACKING_CHAIN
;
3503 backing_mode
= MIRROR_OPEN_BACKING_CHAIN
;
3506 if ((arg
->sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
3507 && arg
->mode
!= NEW_IMAGE_MODE_EXISTING
)
3509 /* create new image w/o backing file */
3511 bdrv_img_create(arg
->target
, format
,
3512 NULL
, NULL
, NULL
, size
, flags
, false, &local_err
);
3514 switch (arg
->mode
) {
3515 case NEW_IMAGE_MODE_EXISTING
:
3517 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
3518 /* create new image with backing file */
3519 bdrv_img_create(arg
->target
, format
,
3521 source
->drv
->format_name
,
3522 NULL
, size
, flags
, false, &local_err
);
3530 error_propagate(errp
, local_err
);
3534 options
= qdict_new();
3535 if (arg
->has_node_name
) {
3536 qdict_put_str(options
, "node-name", arg
->node_name
);
3539 qdict_put_str(options
, "driver", format
);
3542 /* Mirroring takes care of copy-on-write using the source's backing
3545 target_bs
= bdrv_open(arg
->target
, NULL
, options
,
3546 flags
| BDRV_O_NO_BACKING
, errp
);
3551 bdrv_set_aio_context(target_bs
, aio_context
);
3553 blockdev_mirror_common(arg
->has_job_id
? arg
->job_id
: NULL
, bs
, target_bs
,
3554 arg
->has_replaces
, arg
->replaces
, arg
->sync
,
3555 backing_mode
, arg
->has_speed
, arg
->speed
,
3556 arg
->has_granularity
, arg
->granularity
,
3557 arg
->has_buf_size
, arg
->buf_size
,
3558 arg
->has_on_source_error
, arg
->on_source_error
,
3559 arg
->has_on_target_error
, arg
->on_target_error
,
3560 arg
->has_unmap
, arg
->unmap
,
3563 bdrv_unref(target_bs
);
3564 error_propagate(errp
, local_err
);
3566 aio_context_release(aio_context
);
3569 void qmp_blockdev_mirror(bool has_job_id
, const char *job_id
,
3570 const char *device
, const char *target
,
3571 bool has_replaces
, const char *replaces
,
3572 MirrorSyncMode sync
,
3573 bool has_speed
, int64_t speed
,
3574 bool has_granularity
, uint32_t granularity
,
3575 bool has_buf_size
, int64_t buf_size
,
3576 bool has_on_source_error
,
3577 BlockdevOnError on_source_error
,
3578 bool has_on_target_error
,
3579 BlockdevOnError on_target_error
,
3580 bool has_filter_node_name
,
3581 const char *filter_node_name
,
3584 BlockDriverState
*bs
;
3585 BlockDriverState
*target_bs
;
3586 AioContext
*aio_context
;
3587 BlockMirrorBackingMode backing_mode
= MIRROR_LEAVE_BACKING_CHAIN
;
3588 Error
*local_err
= NULL
;
3590 bs
= qmp_get_root_bs(device
, errp
);
3595 target_bs
= bdrv_lookup_bs(target
, target
, errp
);
3600 aio_context
= bdrv_get_aio_context(bs
);
3601 aio_context_acquire(aio_context
);
3603 bdrv_set_aio_context(target_bs
, aio_context
);
3605 blockdev_mirror_common(has_job_id
? job_id
: NULL
, bs
, target_bs
,
3606 has_replaces
, replaces
, sync
, backing_mode
,
3608 has_granularity
, granularity
,
3609 has_buf_size
, buf_size
,
3610 has_on_source_error
, on_source_error
,
3611 has_on_target_error
, on_target_error
,
3613 has_filter_node_name
, filter_node_name
,
3615 error_propagate(errp
, local_err
);
3617 aio_context_release(aio_context
);
3620 /* Get a block job using its ID and acquire its AioContext */
3621 static BlockJob
*find_block_job(const char *id
, AioContext
**aio_context
,
3628 *aio_context
= NULL
;
3630 job
= block_job_get(id
);
3633 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
3634 "Block job '%s' not found", id
);
3638 *aio_context
= blk_get_aio_context(job
->blk
);
3639 aio_context_acquire(*aio_context
);
3644 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
3646 AioContext
*aio_context
;
3647 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3653 block_job_set_speed(job
, speed
, errp
);
3654 aio_context_release(aio_context
);
3657 void qmp_block_job_cancel(const char *device
,
3658 bool has_force
, bool force
, Error
**errp
)
3660 AioContext
*aio_context
;
3661 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3671 if (block_job_user_paused(job
) && !force
) {
3672 error_setg(errp
, "The block job for device '%s' is currently paused",
3677 trace_qmp_block_job_cancel(job
);
3678 block_job_cancel(job
);
3680 aio_context_release(aio_context
);
3683 void qmp_block_job_pause(const char *device
, Error
**errp
)
3685 AioContext
*aio_context
;
3686 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3688 if (!job
|| block_job_user_paused(job
)) {
3692 trace_qmp_block_job_pause(job
);
3693 block_job_user_pause(job
);
3694 aio_context_release(aio_context
);
3697 void qmp_block_job_resume(const char *device
, Error
**errp
)
3699 AioContext
*aio_context
;
3700 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3702 if (!job
|| !block_job_user_paused(job
)) {
3706 trace_qmp_block_job_resume(job
);
3707 block_job_user_resume(job
);
3708 aio_context_release(aio_context
);
3711 void qmp_block_job_complete(const char *device
, Error
**errp
)
3713 AioContext
*aio_context
;
3714 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
3720 trace_qmp_block_job_complete(job
);
3721 block_job_complete(job
, errp
);
3722 aio_context_release(aio_context
);
3725 void qmp_change_backing_file(const char *device
,
3726 const char *image_node_name
,
3727 const char *backing_file
,
3730 BlockDriverState
*bs
= NULL
;
3731 AioContext
*aio_context
;
3732 BlockDriverState
*image_bs
= NULL
;
3733 Error
*local_err
= NULL
;
3738 bs
= qmp_get_root_bs(device
, errp
);
3743 aio_context
= bdrv_get_aio_context(bs
);
3744 aio_context_acquire(aio_context
);
3746 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
3748 error_propagate(errp
, local_err
);
3753 error_setg(errp
, "image file not found");
3757 if (bdrv_find_base(image_bs
) == image_bs
) {
3758 error_setg(errp
, "not allowing backing file change on an image "
3759 "without a backing file");
3763 /* even though we are not necessarily operating on bs, we need it to
3764 * determine if block ops are currently prohibited on the chain */
3765 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
3769 /* final sanity check */
3770 if (!bdrv_chain_contains(bs
, image_bs
)) {
3771 error_setg(errp
, "'%s' and image file are not in the same chain",
3776 /* if not r/w, reopen to make r/w */
3777 open_flags
= image_bs
->open_flags
;
3778 ro
= bdrv_is_read_only(image_bs
);
3781 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
3783 error_propagate(errp
, local_err
);
3788 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
3789 image_bs
->drv
? image_bs
->drv
->format_name
: "");
3792 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
3794 /* don't exit here, so we can try to restore open flags if
3799 bdrv_reopen(image_bs
, open_flags
, &local_err
);
3800 error_propagate(errp
, local_err
);
3804 aio_context_release(aio_context
);
3807 void hmp_drive_add_node(Monitor
*mon
, const char *optstr
)
3811 Error
*local_err
= NULL
;
3813 opts
= qemu_opts_parse_noisily(&qemu_drive_opts
, optstr
, false);
3818 qdict
= qemu_opts_to_qdict(opts
, NULL
);
3820 if (!qdict_get_try_str(qdict
, "node-name")) {
3822 error_report("'node-name' needs to be specified");
3826 BlockDriverState
*bs
= bds_tree_init(qdict
, &local_err
);
3828 error_report_err(local_err
);
3832 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3835 qemu_opts_del(opts
);
3838 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
3840 BlockDriverState
*bs
;
3842 Visitor
*v
= qobject_output_visitor_new(&obj
);
3844 Error
*local_err
= NULL
;
3846 visit_type_BlockdevOptions(v
, NULL
, &options
, &local_err
);
3848 error_propagate(errp
, local_err
);
3852 visit_complete(v
, &obj
);
3853 qdict
= qobject_to_qdict(obj
);
3855 qdict_flatten(qdict
);
3857 if (!qdict_get_try_str(qdict
, "node-name")) {
3858 error_setg(errp
, "'node-name' must be specified for the root node");
3862 bs
= bds_tree_init(qdict
, errp
);
3867 QTAILQ_INSERT_TAIL(&monitor_bdrv_states
, bs
, monitor_list
);
3869 if (bs
&& bdrv_key_required(bs
)) {
3870 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3872 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
3880 void qmp_blockdev_del(const char *node_name
, Error
**errp
)
3882 AioContext
*aio_context
;
3883 BlockDriverState
*bs
;
3885 bs
= bdrv_find_node(node_name
);
3887 error_setg(errp
, "Cannot find node %s", node_name
);
3890 if (bdrv_has_blk(bs
)) {
3891 error_setg(errp
, "Node %s is in use", node_name
);
3894 aio_context
= bdrv_get_aio_context(bs
);
3895 aio_context_acquire(aio_context
);
3897 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, errp
)) {
3901 if (!bs
->monitor_list
.tqe_prev
) {
3902 error_setg(errp
, "Node %s is not owned by the monitor",
3907 if (bs
->refcnt
> 1) {
3908 error_setg(errp
, "Block device %s is in use",
3909 bdrv_get_device_or_node_name(bs
));
3913 QTAILQ_REMOVE(&monitor_bdrv_states
, bs
, monitor_list
);
3917 aio_context_release(aio_context
);
3920 static BdrvChild
*bdrv_find_child(BlockDriverState
*parent_bs
,
3921 const char *child_name
)
3925 QLIST_FOREACH(child
, &parent_bs
->children
, next
) {
3926 if (strcmp(child
->name
, child_name
) == 0) {
3934 void qmp_x_blockdev_change(const char *parent
, bool has_child
,
3935 const char *child
, bool has_node
,
3936 const char *node
, Error
**errp
)
3938 BlockDriverState
*parent_bs
, *new_bs
= NULL
;
3941 parent_bs
= bdrv_lookup_bs(parent
, parent
, errp
);
3946 if (has_child
== has_node
) {
3948 error_setg(errp
, "The parameters child and node are in conflict");
3950 error_setg(errp
, "Either child or node must be specified");
3956 p_child
= bdrv_find_child(parent_bs
, child
);
3958 error_setg(errp
, "Node '%s' does not have child '%s'",
3962 bdrv_del_child(parent_bs
, p_child
, errp
);
3966 new_bs
= bdrv_find_node(node
);
3968 error_setg(errp
, "Node '%s' not found", node
);
3971 bdrv_add_child(parent_bs
, new_bs
, errp
);
3975 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
3977 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
3980 for (job
= block_job_next(NULL
); job
; job
= block_job_next(job
)) {
3981 BlockJobInfoList
*elem
;
3982 AioContext
*aio_context
;
3984 if (block_job_is_internal(job
)) {
3987 elem
= g_new0(BlockJobInfoList
, 1);
3988 aio_context
= blk_get_aio_context(job
->blk
);
3989 aio_context_acquire(aio_context
);
3990 elem
->value
= block_job_query(job
, errp
);
3991 aio_context_release(aio_context
);
3994 qapi_free_BlockJobInfoList(head
);
3998 p_next
= &elem
->next
;
4004 QemuOptsList qemu_common_drive_opts
= {
4006 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
4010 .type
= QEMU_OPT_BOOL
,
4011 .help
= "enable/disable snapshot mode",
4014 .type
= QEMU_OPT_STRING
,
4015 .help
= "host AIO implementation (threads, native)",
4017 .name
= BDRV_OPT_CACHE_WB
,
4018 .type
= QEMU_OPT_BOOL
,
4019 .help
= "Enable writeback mode",
4022 .type
= QEMU_OPT_STRING
,
4023 .help
= "disk format (raw, qcow2, ...)",
4026 .type
= QEMU_OPT_STRING
,
4027 .help
= "read error action",
4030 .type
= QEMU_OPT_STRING
,
4031 .help
= "write error action",
4033 .name
= BDRV_OPT_READ_ONLY
,
4034 .type
= QEMU_OPT_BOOL
,
4035 .help
= "open drive file as read-only",
4041 .name
= "throttling.group",
4042 .type
= QEMU_OPT_STRING
,
4043 .help
= "name of the block throttling group",
4045 .name
= "copy-on-read",
4046 .type
= QEMU_OPT_BOOL
,
4047 .help
= "copy read data from backing file into image file",
4049 .name
= "detect-zeroes",
4050 .type
= QEMU_OPT_STRING
,
4051 .help
= "try to optimize zero writes (off, on, unmap)",
4053 .name
= "stats-account-invalid",
4054 .type
= QEMU_OPT_BOOL
,
4055 .help
= "whether to account for invalid I/O operations "
4056 "in the statistics",
4058 .name
= "stats-account-failed",
4059 .type
= QEMU_OPT_BOOL
,
4060 .help
= "whether to account for failed I/O operations "
4061 "in the statistics",
4063 { /* end of list */ }
4067 QemuOptsList qemu_drive_opts
= {
4069 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
4072 * no elements => accept any params
4073 * validation will happen later
4075 { /* end of list */ }