2 * QEMU host block devices
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This work is licensed under the terms of the GNU GPL, version 2 or
7 * later. See the COPYING file in the top-level directory.
9 * This file incorporates work covered by the following copyright and
12 * Copyright (c) 2003-2008 Fabrice Bellard
14 * Permission is hereby granted, free of charge, to any person obtaining a copy
15 * of this software and associated documentation files (the "Software"), to deal
16 * in the Software without restriction, including without limitation the rights
17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 * copies of the Software, and to permit persons to whom the Software is
19 * furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #include "sysemu/block-backend.h"
34 #include "sysemu/blockdev.h"
35 #include "hw/block/block.h"
36 #include "block/blockjob.h"
37 #include "monitor/monitor.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "qapi-visit.h"
42 #include "qapi/qmp-output-visitor.h"
43 #include "qapi/util.h"
44 #include "sysemu/sysemu.h"
45 #include "block/block_int.h"
46 #include "qmp-commands.h"
48 #include "sysemu/arch_init.h"
50 static const char *const if_name
[IF_COUNT
] = {
54 [IF_FLOPPY
] = "floppy",
55 [IF_PFLASH
] = "pflash",
58 [IF_VIRTIO
] = "virtio",
62 static int if_max_devs
[IF_COUNT
] = {
64 * Do not change these numbers! They govern how drive option
65 * index maps to unit and bus. That mapping is ABI.
67 * All controllers used to imlement if=T drives need to support
68 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
69 * Otherwise, some index values map to "impossible" bus, unit
72 * For instance, if you change [IF_SCSI] to 255, -drive
73 * if=scsi,index=12 no longer means bus=1,unit=5, but
74 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
75 * the drive can't be set up. Regression.
82 * Boards may call this to offer board-by-board overrides
83 * of the default, global values.
85 void override_max_devs(BlockInterfaceType type
, int max_devs
)
94 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
95 dinfo
= blk_legacy_dinfo(blk
);
96 if (dinfo
->type
== type
) {
97 fprintf(stderr
, "Cannot override units-per-bus property of"
98 " the %s interface, because a drive of that type has"
99 " already been added.\n", if_name
[type
]);
100 g_assert_not_reached();
104 if_max_devs
[type
] = max_devs
;
108 * We automatically delete the drive when a device using it gets
109 * unplugged. Questionable feature, but we can't just drop it.
110 * Device models call blockdev_mark_auto_del() to schedule the
111 * automatic deletion, and generic qdev code calls blockdev_auto_del()
112 * when deletion is actually safe.
114 void blockdev_mark_auto_del(BlockBackend
*blk
)
116 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
117 BlockDriverState
*bs
= blk_bs(blk
);
118 AioContext
*aio_context
;
124 aio_context
= bdrv_get_aio_context(bs
);
125 aio_context_acquire(aio_context
);
128 block_job_cancel(bs
->job
);
131 aio_context_release(aio_context
);
136 void blockdev_auto_del(BlockBackend
*blk
)
138 DriveInfo
*dinfo
= blk_legacy_dinfo(blk
);
140 if (dinfo
&& dinfo
->auto_del
) {
146 * Returns the current mapping of how many units per bus
147 * a particular interface can support.
149 * A positive integer indicates n units per bus.
150 * 0 implies the mapping has not been established.
151 * -1 indicates an invalid BlockInterfaceType was given.
153 int drive_get_max_devs(BlockInterfaceType type
)
155 if (type
>= IF_IDE
&& type
< IF_COUNT
) {
156 return if_max_devs
[type
];
162 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
164 int max_devs
= if_max_devs
[type
];
165 return max_devs
? index
/ max_devs
: 0;
168 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
170 int max_devs
= if_max_devs
[type
];
171 return max_devs
? index
% max_devs
: index
;
174 QemuOpts
*drive_def(const char *optstr
)
176 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
179 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
185 opts
= drive_def(optstr
);
189 if (type
!= IF_DEFAULT
) {
190 qemu_opt_set(opts
, "if", if_name
[type
]);
193 snprintf(buf
, sizeof(buf
), "%d", index
);
194 qemu_opt_set(opts
, "index", buf
);
197 qemu_opt_set(opts
, "file", file
);
201 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
206 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
207 dinfo
= blk_legacy_dinfo(blk
);
208 if (dinfo
&& dinfo
->type
== type
209 && dinfo
->bus
== bus
&& dinfo
->unit
== unit
) {
217 bool drive_check_orphaned(void)
223 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
224 dinfo
= blk_legacy_dinfo(blk
);
225 /* If dinfo->bdrv->dev is NULL, it has no device attached. */
226 /* Unless this is a default drive, this may be an oversight. */
227 if (!blk_get_attached_dev(blk
) && !dinfo
->is_default
&&
228 dinfo
->type
!= IF_NONE
) {
229 fprintf(stderr
, "Warning: Orphaned drive without device: "
230 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n",
231 blk_name(blk
), blk_bs(blk
)->filename
, if_name
[dinfo
->type
],
232 dinfo
->bus
, dinfo
->unit
);
240 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
242 return drive_get(type
,
243 drive_index_to_bus_id(type
, index
),
244 drive_index_to_unit_id(type
, index
));
247 int drive_get_max_bus(BlockInterfaceType type
)
254 for (blk
= blk_next(NULL
); blk
; blk
= blk_next(blk
)) {
255 dinfo
= blk_legacy_dinfo(blk
);
256 if (dinfo
&& dinfo
->type
== type
&& dinfo
->bus
> max_bus
) {
257 max_bus
= dinfo
->bus
;
263 /* Get a block device. This should only be used for single-drive devices
264 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
266 DriveInfo
*drive_get_next(BlockInterfaceType type
)
268 static int next_block_unit
[IF_COUNT
];
270 return drive_get(type
, 0, next_block_unit
[type
]++);
273 static void bdrv_format_print(void *opaque
, const char *name
)
275 error_printf(" %s", name
);
280 BlockDriverState
*bs
;
283 static void bdrv_put_ref_bh(void *opaque
)
285 BDRVPutRefBH
*s
= opaque
;
288 qemu_bh_delete(s
->bh
);
293 * Release a BDS reference in a BH
295 * It is not safe to use bdrv_unref() from a callback function when the callers
296 * still need the BlockDriverState. In such cases we schedule a BH to release
299 static void bdrv_put_ref_bh_schedule(BlockDriverState
*bs
)
303 s
= g_new(BDRVPutRefBH
, 1);
304 s
->bh
= qemu_bh_new(bdrv_put_ref_bh
, s
);
306 qemu_bh_schedule(s
->bh
);
309 static int parse_block_error_action(const char *buf
, bool is_read
, Error
**errp
)
311 if (!strcmp(buf
, "ignore")) {
312 return BLOCKDEV_ON_ERROR_IGNORE
;
313 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
314 return BLOCKDEV_ON_ERROR_ENOSPC
;
315 } else if (!strcmp(buf
, "stop")) {
316 return BLOCKDEV_ON_ERROR_STOP
;
317 } else if (!strcmp(buf
, "report")) {
318 return BLOCKDEV_ON_ERROR_REPORT
;
320 error_setg(errp
, "'%s' invalid %s error action",
321 buf
, is_read
? "read" : "write");
326 static bool check_throttle_config(ThrottleConfig
*cfg
, Error
**errp
)
328 if (throttle_conflicting(cfg
)) {
329 error_setg(errp
, "bps/iops/max total values and read/write values"
330 " cannot be used at the same time");
334 if (!throttle_is_valid(cfg
)) {
335 error_setg(errp
, "bps/iops/maxs values must be 0 or greater");
342 typedef enum { MEDIA_DISK
, MEDIA_CDROM
} DriveMediaType
;
344 /* Takes the ownership of bs_opts */
345 static BlockBackend
*blockdev_init(const char *file
, QDict
*bs_opts
,
351 int on_read_error
, on_write_error
;
353 BlockDriverState
*bs
;
361 bool has_driver_specific_opts
;
362 BlockdevDetectZeroesOptions detect_zeroes
;
363 BlockDriver
*drv
= NULL
;
365 /* Check common options by copying from bs_opts to opts, all other options
366 * stay in bs_opts for processing by bdrv_open(). */
367 id
= qdict_get_try_str(bs_opts
, "id");
368 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
370 error_propagate(errp
, error
);
374 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
376 error_propagate(errp
, error
);
381 qdict_del(bs_opts
, "id");
384 has_driver_specific_opts
= !!qdict_size(bs_opts
);
386 /* extract parameters */
387 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
388 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
389 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
391 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
392 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
393 error_setg(errp
, "invalid discard option");
398 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
399 bdrv_flags
|= BDRV_O_CACHE_WB
;
401 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
402 bdrv_flags
|= BDRV_O_NOCACHE
;
404 if (qemu_opt_get_bool(opts
, "cache.no-flush", false)) {
405 bdrv_flags
|= BDRV_O_NO_FLUSH
;
408 #ifdef CONFIG_LINUX_AIO
409 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
410 if (!strcmp(buf
, "native")) {
411 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
412 } else if (!strcmp(buf
, "threads")) {
413 /* this is the default */
415 error_setg(errp
, "invalid aio option");
421 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
422 if (is_help_option(buf
)) {
423 error_printf("Supported formats:");
424 bdrv_iterate_format(bdrv_format_print
, NULL
);
429 drv
= bdrv_find_format(buf
);
431 error_setg(errp
, "'%s' invalid format", buf
);
436 /* disk I/O throttling */
437 memset(&cfg
, 0, sizeof(cfg
));
438 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
=
439 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
440 cfg
.buckets
[THROTTLE_BPS_READ
].avg
=
441 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
442 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
=
443 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
444 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
=
445 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
446 cfg
.buckets
[THROTTLE_OPS_READ
].avg
=
447 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
448 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
=
449 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
451 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
=
452 qemu_opt_get_number(opts
, "throttling.bps-total-max", 0);
453 cfg
.buckets
[THROTTLE_BPS_READ
].max
=
454 qemu_opt_get_number(opts
, "throttling.bps-read-max", 0);
455 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
=
456 qemu_opt_get_number(opts
, "throttling.bps-write-max", 0);
457 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
=
458 qemu_opt_get_number(opts
, "throttling.iops-total-max", 0);
459 cfg
.buckets
[THROTTLE_OPS_READ
].max
=
460 qemu_opt_get_number(opts
, "throttling.iops-read-max", 0);
461 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
=
462 qemu_opt_get_number(opts
, "throttling.iops-write-max", 0);
464 cfg
.op_size
= qemu_opt_get_number(opts
, "throttling.iops-size", 0);
466 if (!check_throttle_config(&cfg
, &error
)) {
467 error_propagate(errp
, error
);
471 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
472 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
473 on_write_error
= parse_block_error_action(buf
, 0, &error
);
475 error_propagate(errp
, error
);
480 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
481 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
482 on_read_error
= parse_block_error_action(buf
, 1, &error
);
484 error_propagate(errp
, error
);
490 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup
,
491 qemu_opt_get(opts
, "detect-zeroes"),
492 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX
,
493 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF
,
496 error_propagate(errp
, error
);
500 if (detect_zeroes
== BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP
&&
501 !(bdrv_flags
& BDRV_O_UNMAP
)) {
502 error_setg(errp
, "setting detect-zeroes to unmap is not allowed "
503 "without setting discard operation to unmap");
508 blk
= blk_new_with_bs(qemu_opts_id(opts
), errp
);
513 bs
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
515 bs
->detect_zeroes
= detect_zeroes
;
517 bdrv_set_on_error(bs
, on_read_error
, on_write_error
);
519 /* disk I/O throttling */
520 if (throttle_enabled(&cfg
)) {
521 bdrv_io_limits_enable(bs
);
522 bdrv_set_io_limits(bs
, &cfg
);
525 if (!file
|| !*file
) {
526 if (has_driver_specific_opts
) {
535 /* always use cache=unsafe with snapshot */
536 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
537 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
541 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
544 if (runstate_check(RUN_STATE_INMIGRATE
)) {
545 bdrv_flags
|= BDRV_O_INCOMING
;
548 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
551 ret
= bdrv_open(&bs
, file
, NULL
, bs_opts
, bdrv_flags
, drv
, &error
);
552 assert(bs
== blk_bs(blk
));
555 error_setg(errp
, "could not open disk image %s: %s",
556 file
?: blk_name(blk
), error_get_pretty(error
));
561 if (bdrv_key_required(bs
)) {
579 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
,
584 value
= qemu_opt_get(opts
, from
);
586 if (qemu_opt_find(opts
, to
)) {
587 error_setg(errp
, "'%s' and its alias '%s' can't be used at the "
588 "same time", to
, from
);
593 /* rename all items in opts */
594 while ((value
= qemu_opt_get(opts
, from
))) {
595 qemu_opt_set(opts
, to
, value
);
596 qemu_opt_unset(opts
, from
);
600 QemuOptsList qemu_legacy_drive_opts
= {
602 .head
= QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts
.head
),
606 .type
= QEMU_OPT_NUMBER
,
607 .help
= "bus number",
610 .type
= QEMU_OPT_NUMBER
,
611 .help
= "unit number (i.e. lun for scsi)",
614 .type
= QEMU_OPT_NUMBER
,
615 .help
= "index number",
618 .type
= QEMU_OPT_STRING
,
619 .help
= "media type (disk, cdrom)",
622 .type
= QEMU_OPT_STRING
,
623 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
626 .type
= QEMU_OPT_NUMBER
,
627 .help
= "number of cylinders (ide disk geometry)",
630 .type
= QEMU_OPT_NUMBER
,
631 .help
= "number of heads (ide disk geometry)",
634 .type
= QEMU_OPT_NUMBER
,
635 .help
= "number of sectors (ide disk geometry)",
638 .type
= QEMU_OPT_STRING
,
639 .help
= "chs translation (auto, lba, none)",
642 .type
= QEMU_OPT_BOOL
,
643 .help
= "(deprecated, ignored)",
646 .type
= QEMU_OPT_STRING
,
647 .help
= "pci address (virtio only)",
650 .type
= QEMU_OPT_STRING
,
651 .help
= "disk serial number",
654 .type
= QEMU_OPT_STRING
,
658 /* Options that are passed on, but have special semantics with -drive */
661 .type
= QEMU_OPT_BOOL
,
662 .help
= "open drive file as read-only",
665 .type
= QEMU_OPT_STRING
,
666 .help
= "read error action",
669 .type
= QEMU_OPT_STRING
,
670 .help
= "write error action",
672 .name
= "copy-on-read",
673 .type
= QEMU_OPT_BOOL
,
674 .help
= "copy read data from backing file into image file",
677 { /* end of list */ }
681 DriveInfo
*drive_new(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
685 DriveInfo
*dinfo
= NULL
;
687 QemuOpts
*legacy_opts
;
688 DriveMediaType media
= MEDIA_DISK
;
689 BlockInterfaceType type
;
690 int cyls
, heads
, secs
, translation
;
691 int max_devs
, bus_id
, unit_id
, index
;
693 const char *werror
, *rerror
;
694 bool read_only
= false;
697 const char *filename
;
698 Error
*local_err
= NULL
;
701 /* Change legacy command line options into QMP ones */
702 static const struct {
706 { "iops", "throttling.iops-total" },
707 { "iops_rd", "throttling.iops-read" },
708 { "iops_wr", "throttling.iops-write" },
710 { "bps", "throttling.bps-total" },
711 { "bps_rd", "throttling.bps-read" },
712 { "bps_wr", "throttling.bps-write" },
714 { "iops_max", "throttling.iops-total-max" },
715 { "iops_rd_max", "throttling.iops-read-max" },
716 { "iops_wr_max", "throttling.iops-write-max" },
718 { "bps_max", "throttling.bps-total-max" },
719 { "bps_rd_max", "throttling.bps-read-max" },
720 { "bps_wr_max", "throttling.bps-write-max" },
722 { "iops_size", "throttling.iops-size" },
724 { "readonly", "read-only" },
727 for (i
= 0; i
< ARRAY_SIZE(opt_renames
); i
++) {
728 qemu_opt_rename(all_opts
, opt_renames
[i
].from
, opt_renames
[i
].to
,
731 error_report_err(local_err
);
736 value
= qemu_opt_get(all_opts
, "cache");
740 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
741 error_report("invalid cache option");
745 /* Specific options take precedence */
746 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
747 qemu_opt_set_bool(all_opts
, "cache.writeback",
748 !!(flags
& BDRV_O_CACHE_WB
));
750 if (!qemu_opt_get(all_opts
, "cache.direct")) {
751 qemu_opt_set_bool(all_opts
, "cache.direct",
752 !!(flags
& BDRV_O_NOCACHE
));
754 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
755 qemu_opt_set_bool(all_opts
, "cache.no-flush",
756 !!(flags
& BDRV_O_NO_FLUSH
));
758 qemu_opt_unset(all_opts
, "cache");
761 /* Get a QDict for processing the options */
762 bs_opts
= qdict_new();
763 qemu_opts_to_qdict(all_opts
, bs_opts
);
765 legacy_opts
= qemu_opts_create(&qemu_legacy_drive_opts
, NULL
, 0,
767 qemu_opts_absorb_qdict(legacy_opts
, bs_opts
, &local_err
);
769 error_report_err(local_err
);
773 /* Deprecated option boot=[on|off] */
774 if (qemu_opt_get(legacy_opts
, "boot") != NULL
) {
775 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
776 "ignored. Future versions will reject this parameter. Please "
777 "update your scripts.\n");
781 value
= qemu_opt_get(legacy_opts
, "media");
783 if (!strcmp(value
, "disk")) {
785 } else if (!strcmp(value
, "cdrom")) {
789 error_report("'%s' invalid media", value
);
794 /* copy-on-read is disabled with a warning for read-only devices */
795 read_only
|= qemu_opt_get_bool(legacy_opts
, "read-only", false);
796 copy_on_read
= qemu_opt_get_bool(legacy_opts
, "copy-on-read", false);
798 if (read_only
&& copy_on_read
) {
799 error_report("warning: disabling copy-on-read on read-only drive");
800 copy_on_read
= false;
803 qdict_put(bs_opts
, "read-only",
804 qstring_from_str(read_only
? "on" : "off"));
805 qdict_put(bs_opts
, "copy-on-read",
806 qstring_from_str(copy_on_read
? "on" :"off"));
808 /* Controller type */
809 value
= qemu_opt_get(legacy_opts
, "if");
812 type
< IF_COUNT
&& strcmp(value
, if_name
[type
]);
815 if (type
== IF_COUNT
) {
816 error_report("unsupported bus type '%s'", value
);
820 type
= block_default_type
;
824 cyls
= qemu_opt_get_number(legacy_opts
, "cyls", 0);
825 heads
= qemu_opt_get_number(legacy_opts
, "heads", 0);
826 secs
= qemu_opt_get_number(legacy_opts
, "secs", 0);
828 if (cyls
|| heads
|| secs
) {
830 error_report("invalid physical cyls number");
834 error_report("invalid physical heads number");
838 error_report("invalid physical secs number");
843 translation
= BIOS_ATA_TRANSLATION_AUTO
;
844 value
= qemu_opt_get(legacy_opts
, "trans");
847 error_report("'%s' trans must be used with cyls, heads and secs",
851 if (!strcmp(value
, "none")) {
852 translation
= BIOS_ATA_TRANSLATION_NONE
;
853 } else if (!strcmp(value
, "lba")) {
854 translation
= BIOS_ATA_TRANSLATION_LBA
;
855 } else if (!strcmp(value
, "large")) {
856 translation
= BIOS_ATA_TRANSLATION_LARGE
;
857 } else if (!strcmp(value
, "rechs")) {
858 translation
= BIOS_ATA_TRANSLATION_RECHS
;
859 } else if (!strcmp(value
, "auto")) {
860 translation
= BIOS_ATA_TRANSLATION_AUTO
;
862 error_report("'%s' invalid translation type", value
);
867 if (media
== MEDIA_CDROM
) {
868 if (cyls
|| secs
|| heads
) {
869 error_report("CHS can't be set with media=cdrom");
874 /* Device address specified by bus/unit or index.
875 * If none was specified, try to find the first free one. */
876 bus_id
= qemu_opt_get_number(legacy_opts
, "bus", 0);
877 unit_id
= qemu_opt_get_number(legacy_opts
, "unit", -1);
878 index
= qemu_opt_get_number(legacy_opts
, "index", -1);
880 max_devs
= if_max_devs
[type
];
883 if (bus_id
!= 0 || unit_id
!= -1) {
884 error_report("index cannot be used with bus and unit");
887 bus_id
= drive_index_to_bus_id(type
, index
);
888 unit_id
= drive_index_to_unit_id(type
, index
);
893 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
895 if (max_devs
&& unit_id
>= max_devs
) {
902 if (max_devs
&& unit_id
>= max_devs
) {
903 error_report("unit %d too big (max is %d)", unit_id
, max_devs
- 1);
907 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
908 error_report("drive with bus=%d, unit=%d (index=%d) exists",
909 bus_id
, unit_id
, index
);
914 serial
= qemu_opt_get(legacy_opts
, "serial");
916 /* no id supplied -> create one */
917 if (qemu_opts_id(all_opts
) == NULL
) {
919 const char *mediastr
= "";
920 if (type
== IF_IDE
|| type
== IF_SCSI
) {
921 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
924 new_id
= g_strdup_printf("%s%i%s%i", if_name
[type
], bus_id
,
927 new_id
= g_strdup_printf("%s%s%i", if_name
[type
],
930 qdict_put(bs_opts
, "id", qstring_from_str(new_id
));
934 /* Add virtio block device */
935 devaddr
= qemu_opt_get(legacy_opts
, "addr");
936 if (devaddr
&& type
!= IF_VIRTIO
) {
937 error_report("addr is not supported by this bus type");
941 if (type
== IF_VIRTIO
) {
943 devopts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
945 if (arch_type
== QEMU_ARCH_S390X
) {
946 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
948 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
950 qemu_opt_set(devopts
, "drive", qdict_get_str(bs_opts
, "id"));
952 qemu_opt_set(devopts
, "addr", devaddr
);
956 filename
= qemu_opt_get(legacy_opts
, "file");
958 /* Check werror/rerror compatibility with if=... */
959 werror
= qemu_opt_get(legacy_opts
, "werror");
960 if (werror
!= NULL
) {
961 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&&
963 error_report("werror is not supported by this bus type");
966 qdict_put(bs_opts
, "werror", qstring_from_str(werror
));
969 rerror
= qemu_opt_get(legacy_opts
, "rerror");
970 if (rerror
!= NULL
) {
971 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&&
973 error_report("rerror is not supported by this bus type");
976 qdict_put(bs_opts
, "rerror", qstring_from_str(rerror
));
979 /* Actual block device init: Functionality shared with blockdev-add */
980 blk
= blockdev_init(filename
, bs_opts
, &local_err
);
984 error_report_err(local_err
);
991 /* Create legacy DriveInfo */
992 dinfo
= g_malloc0(sizeof(*dinfo
));
993 dinfo
->opts
= all_opts
;
996 dinfo
->heads
= heads
;
998 dinfo
->trans
= translation
;
1001 dinfo
->bus
= bus_id
;
1002 dinfo
->unit
= unit_id
;
1003 dinfo
->devaddr
= devaddr
;
1004 dinfo
->serial
= g_strdup(serial
);
1006 blk_set_legacy_dinfo(blk
, dinfo
);
1013 dinfo
->media_cd
= media
== MEDIA_CDROM
;
1020 qemu_opts_del(legacy_opts
);
1025 void do_commit(Monitor
*mon
, const QDict
*qdict
)
1027 const char *device
= qdict_get_str(qdict
, "device");
1028 BlockDriverState
*bs
;
1031 if (!strcmp(device
, "all")) {
1032 ret
= bdrv_commit_all();
1034 bs
= bdrv_find(device
);
1036 monitor_printf(mon
, "Device '%s' not found\n", device
);
1039 ret
= bdrv_commit(bs
);
1042 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
1047 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
1049 TransactionAction action
;
1050 TransactionActionList list
;
1054 list
.value
= &action
;
1056 qmp_transaction(&list
, errp
);
1059 void qmp_blockdev_snapshot_sync(bool has_device
, const char *device
,
1060 bool has_node_name
, const char *node_name
,
1061 const char *snapshot_file
,
1062 bool has_snapshot_node_name
,
1063 const char *snapshot_node_name
,
1064 bool has_format
, const char *format
,
1065 bool has_mode
, NewImageMode mode
, Error
**errp
)
1067 BlockdevSnapshot snapshot
= {
1068 .has_device
= has_device
,
1069 .device
= (char *) device
,
1070 .has_node_name
= has_node_name
,
1071 .node_name
= (char *) node_name
,
1072 .snapshot_file
= (char *) snapshot_file
,
1073 .has_snapshot_node_name
= has_snapshot_node_name
,
1074 .snapshot_node_name
= (char *) snapshot_node_name
,
1075 .has_format
= has_format
,
1076 .format
= (char *) format
,
1077 .has_mode
= has_mode
,
1080 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
1084 void qmp_blockdev_snapshot_internal_sync(const char *device
,
1088 BlockdevSnapshotInternal snapshot
= {
1089 .device
= (char *) device
,
1090 .name
= (char *) name
1093 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
,
1097 SnapshotInfo
*qmp_blockdev_snapshot_delete_internal_sync(const char *device
,
1104 BlockDriverState
*bs
= bdrv_find(device
);
1105 AioContext
*aio_context
;
1106 QEMUSnapshotInfo sn
;
1107 Error
*local_err
= NULL
;
1108 SnapshotInfo
*info
= NULL
;
1112 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1125 error_setg(errp
, "Name or id must be provided");
1129 aio_context
= bdrv_get_aio_context(bs
);
1130 aio_context_acquire(aio_context
);
1132 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE
, errp
)) {
1133 goto out_aio_context
;
1136 ret
= bdrv_snapshot_find_by_id_and_name(bs
, id
, name
, &sn
, &local_err
);
1138 error_propagate(errp
, local_err
);
1139 goto out_aio_context
;
1143 "Snapshot with id '%s' and name '%s' does not exist on "
1145 STR_OR_NULL(id
), STR_OR_NULL(name
), device
);
1146 goto out_aio_context
;
1149 bdrv_snapshot_delete(bs
, id
, name
, &local_err
);
1151 error_propagate(errp
, local_err
);
1152 goto out_aio_context
;
1155 aio_context_release(aio_context
);
1157 info
= g_new0(SnapshotInfo
, 1);
1158 info
->id
= g_strdup(sn
.id_str
);
1159 info
->name
= g_strdup(sn
.name
);
1160 info
->date_nsec
= sn
.date_nsec
;
1161 info
->date_sec
= sn
.date_sec
;
1162 info
->vm_state_size
= sn
.vm_state_size
;
1163 info
->vm_clock_nsec
= sn
.vm_clock_nsec
% 1000000000;
1164 info
->vm_clock_sec
= sn
.vm_clock_nsec
/ 1000000000;
1169 aio_context_release(aio_context
);
1173 /* New and old BlockDriverState structs for atomic group operations */
1175 typedef struct BlkTransactionState BlkTransactionState
;
1177 /* Only prepare() may fail. In a single transaction, only one of commit() or
1178 abort() will be called, clean() will always be called if it present. */
1179 typedef struct BdrvActionOps
{
1180 /* Size of state struct, in bytes. */
1181 size_t instance_size
;
1182 /* Prepare the work, must NOT be NULL. */
1183 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
1184 /* Commit the changes, can be NULL. */
1185 void (*commit
)(BlkTransactionState
*common
);
1186 /* Abort the changes on fail, can be NULL. */
1187 void (*abort
)(BlkTransactionState
*common
);
1188 /* Clean up resource in the end, can be NULL. */
1189 void (*clean
)(BlkTransactionState
*common
);
1193 * This structure must be arranged as first member in child type, assuming
1194 * that compiler will also arrange it to the same address with parent instance.
1195 * Later it will be used in free().
1197 struct BlkTransactionState
{
1198 TransactionAction
*action
;
1199 const BdrvActionOps
*ops
;
1200 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
1203 /* internal snapshot private data */
1204 typedef struct InternalSnapshotState
{
1205 BlkTransactionState common
;
1206 BlockDriverState
*bs
;
1207 AioContext
*aio_context
;
1208 QEMUSnapshotInfo sn
;
1209 } InternalSnapshotState
;
1211 static void internal_snapshot_prepare(BlkTransactionState
*common
,
1214 Error
*local_err
= NULL
;
1217 BlockDriverState
*bs
;
1218 QEMUSnapshotInfo old_sn
, *sn
;
1221 BlockdevSnapshotInternal
*internal
;
1222 InternalSnapshotState
*state
;
1225 g_assert(common
->action
->kind
==
1226 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
);
1227 internal
= common
->action
->blockdev_snapshot_internal_sync
;
1228 state
= DO_UPCAST(InternalSnapshotState
, common
, common
);
1230 /* 1. parse input */
1231 device
= internal
->device
;
1232 name
= internal
->name
;
1234 /* 2. check for validation */
1235 bs
= bdrv_find(device
);
1237 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1241 /* AioContext is released in .clean() */
1242 state
->aio_context
= bdrv_get_aio_context(bs
);
1243 aio_context_acquire(state
->aio_context
);
1245 if (!bdrv_is_inserted(bs
)) {
1246 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1250 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT
, errp
)) {
1254 if (bdrv_is_read_only(bs
)) {
1255 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1259 if (!bdrv_can_snapshot(bs
)) {
1260 error_set(errp
, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
1261 bs
->drv
->format_name
, device
, "internal snapshot");
1265 if (!strlen(name
)) {
1266 error_setg(errp
, "Name is empty");
1270 /* check whether a snapshot with name exist */
1271 ret
= bdrv_snapshot_find_by_id_and_name(bs
, NULL
, name
, &old_sn
,
1274 error_propagate(errp
, local_err
);
1278 "Snapshot with name '%s' already exists on device '%s'",
1283 /* 3. take the snapshot */
1285 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1286 qemu_gettimeofday(&tv
);
1287 sn
->date_sec
= tv
.tv_sec
;
1288 sn
->date_nsec
= tv
.tv_usec
* 1000;
1289 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1291 ret1
= bdrv_snapshot_create(bs
, sn
);
1293 error_setg_errno(errp
, -ret1
,
1294 "Failed to create snapshot '%s' on device '%s'",
1299 /* 4. succeed, mark a snapshot is created */
1303 static void internal_snapshot_abort(BlkTransactionState
*common
)
1305 InternalSnapshotState
*state
=
1306 DO_UPCAST(InternalSnapshotState
, common
, common
);
1307 BlockDriverState
*bs
= state
->bs
;
1308 QEMUSnapshotInfo
*sn
= &state
->sn
;
1309 Error
*local_error
= NULL
;
1315 if (bdrv_snapshot_delete(bs
, sn
->id_str
, sn
->name
, &local_error
) < 0) {
1316 error_report("Failed to delete snapshot with id '%s' and name '%s' on "
1317 "device '%s' in abort: %s",
1320 bdrv_get_device_name(bs
),
1321 error_get_pretty(local_error
));
1322 error_free(local_error
);
1326 static void internal_snapshot_clean(BlkTransactionState
*common
)
1328 InternalSnapshotState
*state
= DO_UPCAST(InternalSnapshotState
,
1331 if (state
->aio_context
) {
1332 aio_context_release(state
->aio_context
);
1336 /* external snapshot private data */
1337 typedef struct ExternalSnapshotState
{
1338 BlkTransactionState common
;
1339 BlockDriverState
*old_bs
;
1340 BlockDriverState
*new_bs
;
1341 AioContext
*aio_context
;
1342 } ExternalSnapshotState
;
1344 static void external_snapshot_prepare(BlkTransactionState
*common
,
1349 QDict
*options
= NULL
;
1350 Error
*local_err
= NULL
;
1351 bool has_device
= false;
1353 bool has_node_name
= false;
1354 const char *node_name
;
1355 bool has_snapshot_node_name
= false;
1356 const char *snapshot_node_name
;
1357 const char *new_image_file
;
1358 const char *format
= "qcow2";
1359 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1360 ExternalSnapshotState
*state
=
1361 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1362 TransactionAction
*action
= common
->action
;
1364 /* get parameters */
1365 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
1367 has_device
= action
->blockdev_snapshot_sync
->has_device
;
1368 device
= action
->blockdev_snapshot_sync
->device
;
1369 has_node_name
= action
->blockdev_snapshot_sync
->has_node_name
;
1370 node_name
= action
->blockdev_snapshot_sync
->node_name
;
1371 has_snapshot_node_name
=
1372 action
->blockdev_snapshot_sync
->has_snapshot_node_name
;
1373 snapshot_node_name
= action
->blockdev_snapshot_sync
->snapshot_node_name
;
1375 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
1376 if (action
->blockdev_snapshot_sync
->has_format
) {
1377 format
= action
->blockdev_snapshot_sync
->format
;
1379 if (action
->blockdev_snapshot_sync
->has_mode
) {
1380 mode
= action
->blockdev_snapshot_sync
->mode
;
1383 /* start processing */
1384 drv
= bdrv_find_format(format
);
1386 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1390 state
->old_bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1391 has_node_name
? node_name
: NULL
,
1394 error_propagate(errp
, local_err
);
1398 if (has_node_name
&& !has_snapshot_node_name
) {
1399 error_setg(errp
, "New snapshot node name missing");
1403 if (has_snapshot_node_name
&& bdrv_find_node(snapshot_node_name
)) {
1404 error_setg(errp
, "New snapshot node name already existing");
1408 /* Acquire AioContext now so any threads operating on old_bs stop */
1409 state
->aio_context
= bdrv_get_aio_context(state
->old_bs
);
1410 aio_context_acquire(state
->aio_context
);
1412 if (!bdrv_is_inserted(state
->old_bs
)) {
1413 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1417 if (bdrv_op_is_blocked(state
->old_bs
,
1418 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT
, errp
)) {
1422 if (!bdrv_is_read_only(state
->old_bs
)) {
1423 if (bdrv_flush(state
->old_bs
)) {
1424 error_set(errp
, QERR_IO_ERROR
);
1429 if (!bdrv_is_first_non_filter(state
->old_bs
)) {
1430 error_set(errp
, QERR_FEATURE_DISABLED
, "snapshot");
1434 flags
= state
->old_bs
->open_flags
;
1436 /* create new image w/backing file */
1437 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1438 bdrv_img_create(new_image_file
, format
,
1439 state
->old_bs
->filename
,
1440 state
->old_bs
->drv
->format_name
,
1441 NULL
, -1, flags
, &local_err
, false);
1443 error_propagate(errp
, local_err
);
1448 if (has_snapshot_node_name
) {
1449 options
= qdict_new();
1450 qdict_put(options
, "node-name",
1451 qstring_from_str(snapshot_node_name
));
1454 /* TODO Inherit bs->options or only take explicit options with an
1455 * extended QMP command? */
1456 assert(state
->new_bs
== NULL
);
1457 ret
= bdrv_open(&state
->new_bs
, new_image_file
, NULL
, options
,
1458 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
1459 /* We will manually add the backing_hd field to the bs later */
1461 error_propagate(errp
, local_err
);
1465 static void external_snapshot_commit(BlkTransactionState
*common
)
1467 ExternalSnapshotState
*state
=
1468 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1470 bdrv_set_aio_context(state
->new_bs
, state
->aio_context
);
1472 /* This removes our old bs and adds the new bs */
1473 bdrv_append(state
->new_bs
, state
->old_bs
);
1474 /* We don't need (or want) to use the transactional
1475 * bdrv_reopen_multiple() across all the entries at once, because we
1476 * don't want to abort all of them if one of them fails the reopen */
1477 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
1480 aio_context_release(state
->aio_context
);
1483 static void external_snapshot_abort(BlkTransactionState
*common
)
1485 ExternalSnapshotState
*state
=
1486 DO_UPCAST(ExternalSnapshotState
, common
, common
);
1487 if (state
->new_bs
) {
1488 bdrv_unref(state
->new_bs
);
1490 if (state
->aio_context
) {
1491 aio_context_release(state
->aio_context
);
1495 typedef struct DriveBackupState
{
1496 BlkTransactionState common
;
1497 BlockDriverState
*bs
;
1498 AioContext
*aio_context
;
1502 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1504 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1505 BlockDriverState
*bs
;
1506 DriveBackup
*backup
;
1507 Error
*local_err
= NULL
;
1509 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1510 backup
= common
->action
->drive_backup
;
1512 bs
= bdrv_find(backup
->device
);
1514 error_set(errp
, QERR_DEVICE_NOT_FOUND
, backup
->device
);
1518 /* AioContext is released in .clean() */
1519 state
->aio_context
= bdrv_get_aio_context(bs
);
1520 aio_context_acquire(state
->aio_context
);
1522 qmp_drive_backup(backup
->device
, backup
->target
,
1523 backup
->has_format
, backup
->format
,
1525 backup
->has_mode
, backup
->mode
,
1526 backup
->has_speed
, backup
->speed
,
1527 backup
->has_on_source_error
, backup
->on_source_error
,
1528 backup
->has_on_target_error
, backup
->on_target_error
,
1531 error_propagate(errp
, local_err
);
1536 state
->job
= state
->bs
->job
;
1539 static void drive_backup_abort(BlkTransactionState
*common
)
1541 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1542 BlockDriverState
*bs
= state
->bs
;
1544 /* Only cancel if it's the job we started */
1545 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1546 block_job_cancel_sync(bs
->job
);
1550 static void drive_backup_clean(BlkTransactionState
*common
)
1552 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1554 if (state
->aio_context
) {
1555 aio_context_release(state
->aio_context
);
1559 typedef struct BlockdevBackupState
{
1560 BlkTransactionState common
;
1561 BlockDriverState
*bs
;
1563 AioContext
*aio_context
;
1564 } BlockdevBackupState
;
1566 static void blockdev_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1568 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1569 BlockdevBackup
*backup
;
1570 BlockDriverState
*bs
, *target
;
1571 Error
*local_err
= NULL
;
1573 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
);
1574 backup
= common
->action
->blockdev_backup
;
1576 bs
= bdrv_find(backup
->device
);
1578 error_set(errp
, QERR_DEVICE_NOT_FOUND
, backup
->device
);
1582 target
= bdrv_find(backup
->target
);
1584 error_set(errp
, QERR_DEVICE_NOT_FOUND
, backup
->target
);
1588 /* AioContext is released in .clean() */
1589 state
->aio_context
= bdrv_get_aio_context(bs
);
1590 if (state
->aio_context
!= bdrv_get_aio_context(target
)) {
1591 state
->aio_context
= NULL
;
1592 error_setg(errp
, "Backup between two IO threads is not implemented");
1595 aio_context_acquire(state
->aio_context
);
1597 qmp_blockdev_backup(backup
->device
, backup
->target
,
1599 backup
->has_speed
, backup
->speed
,
1600 backup
->has_on_source_error
, backup
->on_source_error
,
1601 backup
->has_on_target_error
, backup
->on_target_error
,
1604 error_propagate(errp
, local_err
);
1609 state
->job
= state
->bs
->job
;
1612 static void blockdev_backup_abort(BlkTransactionState
*common
)
1614 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1615 BlockDriverState
*bs
= state
->bs
;
1617 /* Only cancel if it's the job we started */
1618 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1619 block_job_cancel_sync(bs
->job
);
1623 static void blockdev_backup_clean(BlkTransactionState
*common
)
1625 BlockdevBackupState
*state
= DO_UPCAST(BlockdevBackupState
, common
, common
);
1627 if (state
->aio_context
) {
1628 aio_context_release(state
->aio_context
);
1632 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1634 error_setg(errp
, "Transaction aborted using Abort action");
1637 static void abort_commit(BlkTransactionState
*common
)
1639 g_assert_not_reached(); /* this action never succeeds */
1642 static const BdrvActionOps actions
[] = {
1643 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1644 .instance_size
= sizeof(ExternalSnapshotState
),
1645 .prepare
= external_snapshot_prepare
,
1646 .commit
= external_snapshot_commit
,
1647 .abort
= external_snapshot_abort
,
1649 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1650 .instance_size
= sizeof(DriveBackupState
),
1651 .prepare
= drive_backup_prepare
,
1652 .abort
= drive_backup_abort
,
1653 .clean
= drive_backup_clean
,
1655 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP
] = {
1656 .instance_size
= sizeof(BlockdevBackupState
),
1657 .prepare
= blockdev_backup_prepare
,
1658 .abort
= blockdev_backup_abort
,
1659 .clean
= blockdev_backup_clean
,
1661 [TRANSACTION_ACTION_KIND_ABORT
] = {
1662 .instance_size
= sizeof(BlkTransactionState
),
1663 .prepare
= abort_prepare
,
1664 .commit
= abort_commit
,
1666 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC
] = {
1667 .instance_size
= sizeof(InternalSnapshotState
),
1668 .prepare
= internal_snapshot_prepare
,
1669 .abort
= internal_snapshot_abort
,
1670 .clean
= internal_snapshot_clean
,
1675 * 'Atomic' group operations. The operations are performed as a set, and if
1676 * any fail then we roll back all operations in the group.
1678 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1680 TransactionActionList
*dev_entry
= dev_list
;
1681 BlkTransactionState
*state
, *next
;
1682 Error
*local_err
= NULL
;
1684 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1685 QSIMPLEQ_INIT(&snap_bdrv_states
);
1687 /* drain all i/o before any operations */
1690 /* We don't do anything in this loop that commits us to the operations */
1691 while (NULL
!= dev_entry
) {
1692 TransactionAction
*dev_info
= NULL
;
1693 const BdrvActionOps
*ops
;
1695 dev_info
= dev_entry
->value
;
1696 dev_entry
= dev_entry
->next
;
1698 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1700 ops
= &actions
[dev_info
->kind
];
1701 assert(ops
->instance_size
> 0);
1703 state
= g_malloc0(ops
->instance_size
);
1705 state
->action
= dev_info
;
1706 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1708 state
->ops
->prepare(state
, &local_err
);
1710 error_propagate(errp
, local_err
);
1711 goto delete_and_fail
;
1715 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1716 if (state
->ops
->commit
) {
1717 state
->ops
->commit(state
);
1725 /* failure, and it is all-or-none; roll back all operations */
1726 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1727 if (state
->ops
->abort
) {
1728 state
->ops
->abort(state
);
1732 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1733 if (state
->ops
->clean
) {
1734 state
->ops
->clean(state
);
1741 static void eject_device(BlockBackend
*blk
, int force
, Error
**errp
)
1743 BlockDriverState
*bs
= blk_bs(blk
);
1744 AioContext
*aio_context
;
1746 aio_context
= bdrv_get_aio_context(bs
);
1747 aio_context_acquire(aio_context
);
1749 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_EJECT
, errp
)) {
1752 if (!blk_dev_has_removable_media(blk
)) {
1753 error_setg(errp
, "Device '%s' is not removable",
1754 bdrv_get_device_name(bs
));
1758 if (blk_dev_is_medium_locked(blk
) && !blk_dev_is_tray_open(blk
)) {
1759 blk_dev_eject_request(blk
, force
);
1761 error_setg(errp
, "Device '%s' is locked",
1762 bdrv_get_device_name(bs
));
1770 aio_context_release(aio_context
);
1773 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1777 blk
= blk_by_name(device
);
1779 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1783 eject_device(blk
, force
, errp
);
1786 void qmp_block_passwd(bool has_device
, const char *device
,
1787 bool has_node_name
, const char *node_name
,
1788 const char *password
, Error
**errp
)
1790 Error
*local_err
= NULL
;
1791 BlockDriverState
*bs
;
1792 AioContext
*aio_context
;
1794 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
1795 has_node_name
? node_name
: NULL
,
1798 error_propagate(errp
, local_err
);
1802 aio_context
= bdrv_get_aio_context(bs
);
1803 aio_context_acquire(aio_context
);
1805 bdrv_add_key(bs
, password
, errp
);
1807 aio_context_release(aio_context
);
1810 /* Assumes AioContext is held */
1811 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1812 int bdrv_flags
, BlockDriver
*drv
,
1813 const char *password
, Error
**errp
)
1815 Error
*local_err
= NULL
;
1818 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, bdrv_flags
, drv
, &local_err
);
1820 error_propagate(errp
, local_err
);
1824 bdrv_add_key(bs
, password
, errp
);
1827 void qmp_change_blockdev(const char *device
, const char *filename
,
1828 const char *format
, Error
**errp
)
1831 BlockDriverState
*bs
;
1832 AioContext
*aio_context
;
1833 BlockDriver
*drv
= NULL
;
1837 blk
= blk_by_name(device
);
1839 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1844 aio_context
= bdrv_get_aio_context(bs
);
1845 aio_context_acquire(aio_context
);
1848 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1850 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1855 eject_device(blk
, 0, &err
);
1857 error_propagate(errp
, err
);
1861 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1862 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1864 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1867 aio_context_release(aio_context
);
1870 /* throttling disk I/O limits */
1871 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1878 bool has_bps_rd_max
,
1880 bool has_bps_wr_max
,
1884 bool has_iops_rd_max
,
1885 int64_t iops_rd_max
,
1886 bool has_iops_wr_max
,
1887 int64_t iops_wr_max
,
1889 int64_t iops_size
, Error
**errp
)
1892 BlockDriverState
*bs
;
1893 AioContext
*aio_context
;
1895 bs
= bdrv_find(device
);
1897 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1901 memset(&cfg
, 0, sizeof(cfg
));
1902 cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
= bps
;
1903 cfg
.buckets
[THROTTLE_BPS_READ
].avg
= bps_rd
;
1904 cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
= bps_wr
;
1906 cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
= iops
;
1907 cfg
.buckets
[THROTTLE_OPS_READ
].avg
= iops_rd
;
1908 cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
= iops_wr
;
1911 cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
= bps_max
;
1913 if (has_bps_rd_max
) {
1914 cfg
.buckets
[THROTTLE_BPS_READ
].max
= bps_rd_max
;
1916 if (has_bps_wr_max
) {
1917 cfg
.buckets
[THROTTLE_BPS_WRITE
].max
= bps_wr_max
;
1920 cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
= iops_max
;
1922 if (has_iops_rd_max
) {
1923 cfg
.buckets
[THROTTLE_OPS_READ
].max
= iops_rd_max
;
1925 if (has_iops_wr_max
) {
1926 cfg
.buckets
[THROTTLE_OPS_WRITE
].max
= iops_wr_max
;
1929 if (has_iops_size
) {
1930 cfg
.op_size
= iops_size
;
1933 if (!check_throttle_config(&cfg
, errp
)) {
1937 aio_context
= bdrv_get_aio_context(bs
);
1938 aio_context_acquire(aio_context
);
1940 if (!bs
->io_limits_enabled
&& throttle_enabled(&cfg
)) {
1941 bdrv_io_limits_enable(bs
);
1942 } else if (bs
->io_limits_enabled
&& !throttle_enabled(&cfg
)) {
1943 bdrv_io_limits_disable(bs
);
1946 if (bs
->io_limits_enabled
) {
1947 bdrv_set_io_limits(bs
, &cfg
);
1950 aio_context_release(aio_context
);
1953 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1955 const char *id
= qdict_get_str(qdict
, "id");
1957 BlockDriverState
*bs
;
1958 AioContext
*aio_context
;
1959 Error
*local_err
= NULL
;
1961 blk
= blk_by_name(id
);
1963 error_report("Device '%s' not found", id
);
1968 if (!blk_legacy_dinfo(blk
)) {
1969 error_report("Deleting device added with blockdev-add"
1970 " is not supported");
1974 aio_context
= bdrv_get_aio_context(bs
);
1975 aio_context_acquire(aio_context
);
1977 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_DRIVE_DEL
, &local_err
)) {
1978 error_report_err(local_err
);
1979 aio_context_release(aio_context
);
1983 /* quiesce block driver; prevent further io */
1988 /* if we have a device attached to this BlockDriverState
1989 * then we need to make the drive anonymous until the device
1990 * can be removed. If this is a drive with no device backing
1991 * then we can just get rid of the block driver state right here.
1993 if (blk_get_attached_dev(blk
)) {
1994 blk_hide_on_behalf_of_do_drive_del(blk
);
1995 /* Further I/O must not pause the guest */
1996 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1997 BLOCKDEV_ON_ERROR_REPORT
);
2002 aio_context_release(aio_context
);
2006 void qmp_block_resize(bool has_device
, const char *device
,
2007 bool has_node_name
, const char *node_name
,
2008 int64_t size
, Error
**errp
)
2010 Error
*local_err
= NULL
;
2011 BlockDriverState
*bs
;
2012 AioContext
*aio_context
;
2015 bs
= bdrv_lookup_bs(has_device
? device
: NULL
,
2016 has_node_name
? node_name
: NULL
,
2019 error_propagate(errp
, local_err
);
2023 aio_context
= bdrv_get_aio_context(bs
);
2024 aio_context_acquire(aio_context
);
2026 if (!bdrv_is_first_non_filter(bs
)) {
2027 error_set(errp
, QERR_FEATURE_DISABLED
, "resize");
2032 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
2036 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_RESIZE
, NULL
)) {
2037 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
2041 /* complete all in-flight operations before resizing the device */
2044 ret
= bdrv_truncate(bs
, size
);
2049 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2052 error_set(errp
, QERR_UNSUPPORTED
);
2055 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
2058 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
2061 error_setg_errno(errp
, -ret
, "Could not resize");
2066 aio_context_release(aio_context
);
2069 static void block_job_cb(void *opaque
, int ret
)
2071 /* Note that this function may be executed from another AioContext besides
2072 * the QEMU main loop. If you need to access anything that assumes the
2073 * QEMU global mutex, use a BH or introduce a mutex.
2076 BlockDriverState
*bs
= opaque
;
2077 const char *msg
= NULL
;
2079 trace_block_job_cb(bs
, bs
->job
, ret
);
2084 msg
= strerror(-ret
);
2087 if (block_job_is_cancelled(bs
->job
)) {
2088 block_job_event_cancelled(bs
->job
);
2090 block_job_event_completed(bs
->job
, msg
);
2093 bdrv_put_ref_bh_schedule(bs
);
2096 void qmp_block_stream(const char *device
,
2097 bool has_base
, const char *base
,
2098 bool has_backing_file
, const char *backing_file
,
2099 bool has_speed
, int64_t speed
,
2100 bool has_on_error
, BlockdevOnError on_error
,
2103 BlockDriverState
*bs
;
2104 BlockDriverState
*base_bs
= NULL
;
2105 AioContext
*aio_context
;
2106 Error
*local_err
= NULL
;
2107 const char *base_name
= NULL
;
2109 if (!has_on_error
) {
2110 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2113 bs
= bdrv_find(device
);
2115 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2119 aio_context
= bdrv_get_aio_context(bs
);
2120 aio_context_acquire(aio_context
);
2122 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_STREAM
, errp
)) {
2127 base_bs
= bdrv_find_backing_image(bs
, base
);
2128 if (base_bs
== NULL
) {
2129 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
2132 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2136 /* if we are streaming the entire chain, the result will have no backing
2137 * file, and specifying one is therefore an error */
2138 if (base_bs
== NULL
&& has_backing_file
) {
2139 error_setg(errp
, "backing file specified, but streaming the "
2144 /* backing_file string overrides base bs filename */
2145 base_name
= has_backing_file
? backing_file
: base_name
;
2147 stream_start(bs
, base_bs
, base_name
, has_speed
? speed
: 0,
2148 on_error
, block_job_cb
, bs
, &local_err
);
2150 error_propagate(errp
, local_err
);
2154 trace_qmp_block_stream(bs
, bs
->job
);
2157 aio_context_release(aio_context
);
2160 void qmp_block_commit(const char *device
,
2161 bool has_base
, const char *base
,
2162 bool has_top
, const char *top
,
2163 bool has_backing_file
, const char *backing_file
,
2164 bool has_speed
, int64_t speed
,
2167 BlockDriverState
*bs
;
2168 BlockDriverState
*base_bs
, *top_bs
;
2169 AioContext
*aio_context
;
2170 Error
*local_err
= NULL
;
2171 /* This will be part of the QMP command, if/when the
2172 * BlockdevOnError change for blkmirror makes it in
2174 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
2181 * libvirt relies on the DeviceNotFound error class in order to probe for
2182 * live commit feature versions; for this to work, we must make sure to
2183 * perform the device lookup before any generic errors that may occur in a
2184 * scenario in which all optional arguments are omitted. */
2185 bs
= bdrv_find(device
);
2187 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2191 aio_context
= bdrv_get_aio_context(bs
);
2192 aio_context_acquire(aio_context
);
2194 /* drain all i/o before commits */
2197 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_COMMIT_SOURCE
, errp
)) {
2201 /* default top_bs is the active layer */
2204 if (has_top
&& top
) {
2205 if (strcmp(bs
->filename
, top
) != 0) {
2206 top_bs
= bdrv_find_backing_image(bs
, top
);
2210 if (top_bs
== NULL
) {
2211 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
2215 assert(bdrv_get_aio_context(top_bs
) == aio_context
);
2217 if (has_base
&& base
) {
2218 base_bs
= bdrv_find_backing_image(top_bs
, base
);
2220 base_bs
= bdrv_find_base(top_bs
);
2223 if (base_bs
== NULL
) {
2224 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
2228 assert(bdrv_get_aio_context(base_bs
) == aio_context
);
2230 if (bdrv_op_is_blocked(base_bs
, BLOCK_OP_TYPE_COMMIT_TARGET
, errp
)) {
2234 /* Do not allow attempts to commit an image into itself */
2235 if (top_bs
== base_bs
) {
2236 error_setg(errp
, "cannot commit an image into itself");
2241 if (has_backing_file
) {
2242 error_setg(errp
, "'backing-file' specified,"
2243 " but 'top' is the active layer");
2246 commit_active_start(bs
, base_bs
, speed
, on_error
, block_job_cb
,
2249 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
2250 has_backing_file
? backing_file
: NULL
, &local_err
);
2252 if (local_err
!= NULL
) {
2253 error_propagate(errp
, local_err
);
2258 aio_context_release(aio_context
);
2261 void qmp_drive_backup(const char *device
, const char *target
,
2262 bool has_format
, const char *format
,
2263 enum MirrorSyncMode sync
,
2264 bool has_mode
, enum NewImageMode mode
,
2265 bool has_speed
, int64_t speed
,
2266 bool has_on_source_error
, BlockdevOnError on_source_error
,
2267 bool has_on_target_error
, BlockdevOnError on_target_error
,
2270 BlockDriverState
*bs
;
2271 BlockDriverState
*target_bs
;
2272 BlockDriverState
*source
= NULL
;
2273 AioContext
*aio_context
;
2274 BlockDriver
*drv
= NULL
;
2275 Error
*local_err
= NULL
;
2283 if (!has_on_source_error
) {
2284 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2286 if (!has_on_target_error
) {
2287 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2290 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2293 bs
= bdrv_find(device
);
2295 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2299 aio_context
= bdrv_get_aio_context(bs
);
2300 aio_context_acquire(aio_context
);
2302 /* Although backup_run has this check too, we need to use bs->drv below, so
2303 * do an early check redundantly. */
2304 if (!bdrv_is_inserted(bs
)) {
2305 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2310 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2313 drv
= bdrv_find_format(format
);
2315 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2320 /* Early check to avoid creating target */
2321 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_BACKUP_SOURCE
, errp
)) {
2325 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2327 /* See if we have a backing HD we can use to create our new image
2329 if (sync
== MIRROR_SYNC_MODE_TOP
) {
2330 source
= bs
->backing_hd
;
2332 sync
= MIRROR_SYNC_MODE_FULL
;
2335 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2339 size
= bdrv_getlength(bs
);
2341 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2345 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
2346 assert(format
&& drv
);
2348 bdrv_img_create(target
, format
, source
->filename
,
2349 source
->drv
->format_name
, NULL
,
2350 size
, flags
, &local_err
, false);
2352 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
2353 size
, flags
, &local_err
, false);
2358 error_propagate(errp
, local_err
);
2363 ret
= bdrv_open(&target_bs
, target
, NULL
, NULL
, flags
, drv
, &local_err
);
2365 error_propagate(errp
, local_err
);
2369 bdrv_set_aio_context(target_bs
, aio_context
);
2371 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
2372 block_job_cb
, bs
, &local_err
);
2373 if (local_err
!= NULL
) {
2374 bdrv_unref(target_bs
);
2375 error_propagate(errp
, local_err
);
2380 aio_context_release(aio_context
);
2383 BlockDeviceInfoList
*qmp_query_named_block_nodes(Error
**errp
)
2385 return bdrv_named_nodes_list();
2388 void qmp_blockdev_backup(const char *device
, const char *target
,
2389 enum MirrorSyncMode sync
,
2390 bool has_speed
, int64_t speed
,
2391 bool has_on_source_error
,
2392 BlockdevOnError on_source_error
,
2393 bool has_on_target_error
,
2394 BlockdevOnError on_target_error
,
2397 BlockDriverState
*bs
;
2398 BlockDriverState
*target_bs
;
2399 Error
*local_err
= NULL
;
2400 AioContext
*aio_context
;
2405 if (!has_on_source_error
) {
2406 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2408 if (!has_on_target_error
) {
2409 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2412 bs
= bdrv_find(device
);
2414 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2418 aio_context
= bdrv_get_aio_context(bs
);
2419 aio_context_acquire(aio_context
);
2421 target_bs
= bdrv_find(target
);
2423 error_set(errp
, QERR_DEVICE_NOT_FOUND
, target
);
2427 bdrv_ref(target_bs
);
2428 bdrv_set_aio_context(target_bs
, aio_context
);
2429 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
2430 block_job_cb
, bs
, &local_err
);
2431 if (local_err
!= NULL
) {
2432 bdrv_unref(target_bs
);
2433 error_propagate(errp
, local_err
);
2436 aio_context_release(aio_context
);
2439 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
2441 void qmp_drive_mirror(const char *device
, const char *target
,
2442 bool has_format
, const char *format
,
2443 bool has_node_name
, const char *node_name
,
2444 bool has_replaces
, const char *replaces
,
2445 enum MirrorSyncMode sync
,
2446 bool has_mode
, enum NewImageMode mode
,
2447 bool has_speed
, int64_t speed
,
2448 bool has_granularity
, uint32_t granularity
,
2449 bool has_buf_size
, int64_t buf_size
,
2450 bool has_on_source_error
, BlockdevOnError on_source_error
,
2451 bool has_on_target_error
, BlockdevOnError on_target_error
,
2454 BlockDriverState
*bs
;
2455 BlockDriverState
*source
, *target_bs
;
2456 AioContext
*aio_context
;
2457 BlockDriver
*drv
= NULL
;
2458 Error
*local_err
= NULL
;
2459 QDict
*options
= NULL
;
2467 if (!has_on_source_error
) {
2468 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
2470 if (!has_on_target_error
) {
2471 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
2474 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
2476 if (!has_granularity
) {
2479 if (!has_buf_size
) {
2480 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
2483 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
2484 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity",
2485 "a value in range [512B, 64MB]");
2488 if (granularity
& (granularity
- 1)) {
2489 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "granularity", "power of 2");
2493 bs
= bdrv_find(device
);
2495 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2499 aio_context
= bdrv_get_aio_context(bs
);
2500 aio_context_acquire(aio_context
);
2502 if (!bdrv_is_inserted(bs
)) {
2503 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
2508 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
2511 drv
= bdrv_find_format(format
);
2513 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
2518 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_MIRROR
, errp
)) {
2522 flags
= bs
->open_flags
| BDRV_O_RDWR
;
2523 source
= bs
->backing_hd
;
2524 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
2525 sync
= MIRROR_SYNC_MODE_FULL
;
2527 if (sync
== MIRROR_SYNC_MODE_NONE
) {
2531 size
= bdrv_getlength(bs
);
2533 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
2538 BlockDriverState
*to_replace_bs
;
2539 AioContext
*replace_aio_context
;
2540 int64_t replace_size
;
2542 if (!has_node_name
) {
2543 error_setg(errp
, "a node-name must be provided when replacing a"
2544 " named node of the graph");
2548 to_replace_bs
= check_to_replace_node(replaces
, &local_err
);
2550 if (!to_replace_bs
) {
2551 error_propagate(errp
, local_err
);
2555 replace_aio_context
= bdrv_get_aio_context(to_replace_bs
);
2556 aio_context_acquire(replace_aio_context
);
2557 replace_size
= bdrv_getlength(to_replace_bs
);
2558 aio_context_release(replace_aio_context
);
2560 if (size
!= replace_size
) {
2561 error_setg(errp
, "cannot replace image with a mirror image of "
2567 if ((sync
== MIRROR_SYNC_MODE_FULL
|| !source
)
2568 && mode
!= NEW_IMAGE_MODE_EXISTING
)
2570 /* create new image w/o backing file */
2571 assert(format
&& drv
);
2572 bdrv_img_create(target
, format
,
2573 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
2576 case NEW_IMAGE_MODE_EXISTING
:
2578 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
2579 /* create new image with backing file */
2580 bdrv_img_create(target
, format
,
2582 source
->drv
->format_name
,
2583 NULL
, size
, flags
, &local_err
, false);
2591 error_propagate(errp
, local_err
);
2595 if (has_node_name
) {
2596 options
= qdict_new();
2597 qdict_put(options
, "node-name", qstring_from_str(node_name
));
2600 /* Mirroring takes care of copy-on-write using the source's backing
2604 ret
= bdrv_open(&target_bs
, target
, NULL
, options
,
2605 flags
| BDRV_O_NO_BACKING
, drv
, &local_err
);
2607 error_propagate(errp
, local_err
);
2611 bdrv_set_aio_context(target_bs
, aio_context
);
2613 /* pass the node name to replace to mirror start since it's loose coupling
2614 * and will allow to check whether the node still exist at mirror completion
2616 mirror_start(bs
, target_bs
,
2617 has_replaces
? replaces
: NULL
,
2618 speed
, granularity
, buf_size
, sync
,
2619 on_source_error
, on_target_error
,
2620 block_job_cb
, bs
, &local_err
);
2621 if (local_err
!= NULL
) {
2622 bdrv_unref(target_bs
);
2623 error_propagate(errp
, local_err
);
2628 aio_context_release(aio_context
);
2631 /* Get the block job for a given device name and acquire its AioContext */
2632 static BlockJob
*find_block_job(const char *device
, AioContext
**aio_context
,
2635 BlockDriverState
*bs
;
2637 bs
= bdrv_find(device
);
2642 *aio_context
= bdrv_get_aio_context(bs
);
2643 aio_context_acquire(*aio_context
);
2646 aio_context_release(*aio_context
);
2653 error_set(errp
, ERROR_CLASS_DEVICE_NOT_ACTIVE
,
2654 "No active block job on device '%s'", device
);
2655 *aio_context
= NULL
;
2659 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
2661 AioContext
*aio_context
;
2662 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2668 block_job_set_speed(job
, speed
, errp
);
2669 aio_context_release(aio_context
);
2672 void qmp_block_job_cancel(const char *device
,
2673 bool has_force
, bool force
, Error
**errp
)
2675 AioContext
*aio_context
;
2676 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2686 if (job
->paused
&& !force
) {
2687 error_setg(errp
, "The block job for device '%s' is currently paused",
2692 trace_qmp_block_job_cancel(job
);
2693 block_job_cancel(job
);
2695 aio_context_release(aio_context
);
2698 void qmp_block_job_pause(const char *device
, Error
**errp
)
2700 AioContext
*aio_context
;
2701 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2707 trace_qmp_block_job_pause(job
);
2708 block_job_pause(job
);
2709 aio_context_release(aio_context
);
2712 void qmp_block_job_resume(const char *device
, Error
**errp
)
2714 AioContext
*aio_context
;
2715 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2721 trace_qmp_block_job_resume(job
);
2722 block_job_resume(job
);
2723 aio_context_release(aio_context
);
2726 void qmp_block_job_complete(const char *device
, Error
**errp
)
2728 AioContext
*aio_context
;
2729 BlockJob
*job
= find_block_job(device
, &aio_context
, errp
);
2735 trace_qmp_block_job_complete(job
);
2736 block_job_complete(job
, errp
);
2737 aio_context_release(aio_context
);
2740 void qmp_change_backing_file(const char *device
,
2741 const char *image_node_name
,
2742 const char *backing_file
,
2745 BlockDriverState
*bs
= NULL
;
2746 AioContext
*aio_context
;
2747 BlockDriverState
*image_bs
= NULL
;
2748 Error
*local_err
= NULL
;
2753 /* find the top layer BDS of the chain */
2754 bs
= bdrv_find(device
);
2756 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
2760 aio_context
= bdrv_get_aio_context(bs
);
2761 aio_context_acquire(aio_context
);
2763 image_bs
= bdrv_lookup_bs(NULL
, image_node_name
, &local_err
);
2765 error_propagate(errp
, local_err
);
2770 error_setg(errp
, "image file not found");
2774 if (bdrv_find_base(image_bs
) == image_bs
) {
2775 error_setg(errp
, "not allowing backing file change on an image "
2776 "without a backing file");
2780 /* even though we are not necessarily operating on bs, we need it to
2781 * determine if block ops are currently prohibited on the chain */
2782 if (bdrv_op_is_blocked(bs
, BLOCK_OP_TYPE_CHANGE
, errp
)) {
2786 /* final sanity check */
2787 if (!bdrv_chain_contains(bs
, image_bs
)) {
2788 error_setg(errp
, "'%s' and image file are not in the same chain",
2793 /* if not r/w, reopen to make r/w */
2794 open_flags
= image_bs
->open_flags
;
2795 ro
= bdrv_is_read_only(image_bs
);
2798 bdrv_reopen(image_bs
, open_flags
| BDRV_O_RDWR
, &local_err
);
2800 error_propagate(errp
, local_err
);
2805 ret
= bdrv_change_backing_file(image_bs
, backing_file
,
2806 image_bs
->drv
? image_bs
->drv
->format_name
: "");
2809 error_setg_errno(errp
, -ret
, "Could not change backing file to '%s'",
2811 /* don't exit here, so we can try to restore open flags if
2816 bdrv_reopen(image_bs
, open_flags
, &local_err
);
2818 error_propagate(errp
, local_err
); /* will preserve prior errp */
2823 aio_context_release(aio_context
);
2826 void qmp_blockdev_add(BlockdevOptions
*options
, Error
**errp
)
2828 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
2832 Error
*local_err
= NULL
;
2834 /* Require an ID in the top level */
2835 if (!options
->has_id
) {
2836 error_setg(errp
, "Block device needs an ID");
2840 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with
2841 * cache.direct=false instead of silently switching to aio=threads, except
2842 * when called from drive_new().
2844 * For now, simply forbidding the combination for all drivers will do. */
2845 if (options
->has_aio
&& options
->aio
== BLOCKDEV_AIO_OPTIONS_NATIVE
) {
2846 bool direct
= options
->has_cache
&&
2847 options
->cache
->has_direct
&&
2848 options
->cache
->direct
;
2850 error_setg(errp
, "aio=native requires cache.direct=true");
2855 visit_type_BlockdevOptions(qmp_output_get_visitor(ov
),
2856 &options
, NULL
, &local_err
);
2858 error_propagate(errp
, local_err
);
2862 obj
= qmp_output_get_qobject(ov
);
2863 qdict
= qobject_to_qdict(obj
);
2865 qdict_flatten(qdict
);
2867 blk
= blockdev_init(NULL
, qdict
, &local_err
);
2869 error_propagate(errp
, local_err
);
2873 if (bdrv_key_required(blk_bs(blk
))) {
2875 error_setg(errp
, "blockdev-add doesn't support encrypted devices");
2880 qmp_output_visitor_cleanup(ov
);
2883 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
2885 BlockJobInfoList
*head
= NULL
, **p_next
= &head
;
2886 BlockDriverState
*bs
;
2888 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
2889 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
2891 aio_context_acquire(aio_context
);
2894 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
2895 elem
->value
= block_job_query(bs
->job
);
2897 p_next
= &elem
->next
;
2900 aio_context_release(aio_context
);
2906 QemuOptsList qemu_common_drive_opts
= {
2908 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
2912 .type
= QEMU_OPT_BOOL
,
2913 .help
= "enable/disable snapshot mode",
2916 .type
= QEMU_OPT_STRING
,
2917 .help
= "discard operation (ignore/off, unmap/on)",
2919 .name
= "cache.writeback",
2920 .type
= QEMU_OPT_BOOL
,
2921 .help
= "enables writeback mode for any caches",
2923 .name
= "cache.direct",
2924 .type
= QEMU_OPT_BOOL
,
2925 .help
= "enables use of O_DIRECT (bypass the host page cache)",
2927 .name
= "cache.no-flush",
2928 .type
= QEMU_OPT_BOOL
,
2929 .help
= "ignore any flush requests for the device",
2932 .type
= QEMU_OPT_STRING
,
2933 .help
= "host AIO implementation (threads, native)",
2936 .type
= QEMU_OPT_STRING
,
2937 .help
= "disk format (raw, qcow2, ...)",
2940 .type
= QEMU_OPT_STRING
,
2941 .help
= "read error action",
2944 .type
= QEMU_OPT_STRING
,
2945 .help
= "write error action",
2947 .name
= "read-only",
2948 .type
= QEMU_OPT_BOOL
,
2949 .help
= "open drive file as read-only",
2951 .name
= "throttling.iops-total",
2952 .type
= QEMU_OPT_NUMBER
,
2953 .help
= "limit total I/O operations per second",
2955 .name
= "throttling.iops-read",
2956 .type
= QEMU_OPT_NUMBER
,
2957 .help
= "limit read operations per second",
2959 .name
= "throttling.iops-write",
2960 .type
= QEMU_OPT_NUMBER
,
2961 .help
= "limit write operations per second",
2963 .name
= "throttling.bps-total",
2964 .type
= QEMU_OPT_NUMBER
,
2965 .help
= "limit total bytes per second",
2967 .name
= "throttling.bps-read",
2968 .type
= QEMU_OPT_NUMBER
,
2969 .help
= "limit read bytes per second",
2971 .name
= "throttling.bps-write",
2972 .type
= QEMU_OPT_NUMBER
,
2973 .help
= "limit write bytes per second",
2975 .name
= "throttling.iops-total-max",
2976 .type
= QEMU_OPT_NUMBER
,
2977 .help
= "I/O operations burst",
2979 .name
= "throttling.iops-read-max",
2980 .type
= QEMU_OPT_NUMBER
,
2981 .help
= "I/O operations read burst",
2983 .name
= "throttling.iops-write-max",
2984 .type
= QEMU_OPT_NUMBER
,
2985 .help
= "I/O operations write burst",
2987 .name
= "throttling.bps-total-max",
2988 .type
= QEMU_OPT_NUMBER
,
2989 .help
= "total bytes burst",
2991 .name
= "throttling.bps-read-max",
2992 .type
= QEMU_OPT_NUMBER
,
2993 .help
= "total bytes read burst",
2995 .name
= "throttling.bps-write-max",
2996 .type
= QEMU_OPT_NUMBER
,
2997 .help
= "total bytes write burst",
2999 .name
= "throttling.iops-size",
3000 .type
= QEMU_OPT_NUMBER
,
3001 .help
= "when limiting by iops max size of an I/O in bytes",
3003 .name
= "copy-on-read",
3004 .type
= QEMU_OPT_BOOL
,
3005 .help
= "copy read data from backing file into image file",
3007 .name
= "detect-zeroes",
3008 .type
= QEMU_OPT_STRING
,
3009 .help
= "try to optimize zero writes (off, on, unmap)",
3011 { /* end of list */ }
3015 QemuOptsList qemu_drive_opts
= {
3017 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
3020 * no elements => accept any params
3021 * validation will happen later
3023 { /* end of list */ }