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/blockdev.h"
34 #include "hw/block/block.h"
35 #include "block/blockjob.h"
36 #include "monitor/monitor.h"
37 #include "qapi/qmp/qerror.h"
38 #include "qemu/option.h"
39 #include "qemu/config-file.h"
40 #include "qapi/qmp/types.h"
41 #include "sysemu/sysemu.h"
42 #include "block/block_int.h"
43 #include "qmp-commands.h"
45 #include "sysemu/arch_init.h"
47 static QTAILQ_HEAD(drivelist
, DriveInfo
) drives
= QTAILQ_HEAD_INITIALIZER(drives
);
48 extern QemuOptsList qemu_common_drive_opts
;
49 extern QemuOptsList qemu_old_drive_opts
;
51 static const char *const if_name
[IF_COUNT
] = {
55 [IF_FLOPPY
] = "floppy",
56 [IF_PFLASH
] = "pflash",
59 [IF_VIRTIO
] = "virtio",
63 static const int if_max_devs
[IF_COUNT
] = {
65 * Do not change these numbers! They govern how drive option
66 * index maps to unit and bus. That mapping is ABI.
68 * All controllers used to imlement if=T drives need to support
69 * if_max_devs[T] units, for any T with if_max_devs[T] != 0.
70 * Otherwise, some index values map to "impossible" bus, unit
73 * For instance, if you change [IF_SCSI] to 255, -drive
74 * if=scsi,index=12 no longer means bus=1,unit=5, but
75 * bus=0,unit=12. With an lsi53c895a controller (7 units max),
76 * the drive can't be set up. Regression.
83 * We automatically delete the drive when a device using it gets
84 * unplugged. Questionable feature, but we can't just drop it.
85 * Device models call blockdev_mark_auto_del() to schedule the
86 * automatic deletion, and generic qdev code calls blockdev_auto_del()
87 * when deletion is actually safe.
89 void blockdev_mark_auto_del(BlockDriverState
*bs
)
91 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
94 block_job_cancel(bs
->job
);
101 void blockdev_auto_del(BlockDriverState
*bs
)
103 DriveInfo
*dinfo
= drive_get_by_blockdev(bs
);
105 if (dinfo
&& dinfo
->auto_del
) {
106 drive_put_ref(dinfo
);
110 static int drive_index_to_bus_id(BlockInterfaceType type
, int index
)
112 int max_devs
= if_max_devs
[type
];
113 return max_devs
? index
/ max_devs
: 0;
116 static int drive_index_to_unit_id(BlockInterfaceType type
, int index
)
118 int max_devs
= if_max_devs
[type
];
119 return max_devs
? index
% max_devs
: index
;
122 QemuOpts
*drive_def(const char *optstr
)
124 return qemu_opts_parse(qemu_find_opts("drive"), optstr
, 0);
127 QemuOpts
*drive_add(BlockInterfaceType type
, int index
, const char *file
,
133 opts
= drive_def(optstr
);
137 if (type
!= IF_DEFAULT
) {
138 qemu_opt_set(opts
, "if", if_name
[type
]);
141 snprintf(buf
, sizeof(buf
), "%d", index
);
142 qemu_opt_set(opts
, "index", buf
);
145 qemu_opt_set(opts
, "file", file
);
149 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
153 /* seek interface, bus and unit */
155 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
156 if (dinfo
->type
== type
&&
165 DriveInfo
*drive_get_by_index(BlockInterfaceType type
, int index
)
167 return drive_get(type
,
168 drive_index_to_bus_id(type
, index
),
169 drive_index_to_unit_id(type
, index
));
172 int drive_get_max_bus(BlockInterfaceType type
)
178 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
179 if(dinfo
->type
== type
&&
180 dinfo
->bus
> max_bus
)
181 max_bus
= dinfo
->bus
;
186 /* Get a block device. This should only be used for single-drive devices
187 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the
189 DriveInfo
*drive_get_next(BlockInterfaceType type
)
191 static int next_block_unit
[IF_COUNT
];
193 return drive_get(type
, 0, next_block_unit
[type
]++);
196 DriveInfo
*drive_get_by_blockdev(BlockDriverState
*bs
)
200 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
201 if (dinfo
->bdrv
== bs
) {
208 static void bdrv_format_print(void *opaque
, const char *name
)
210 error_printf(" %s", name
);
213 static void drive_uninit(DriveInfo
*dinfo
)
215 qemu_opts_del(dinfo
->opts
);
216 bdrv_delete(dinfo
->bdrv
);
218 QTAILQ_REMOVE(&drives
, dinfo
, next
);
219 g_free(dinfo
->serial
);
223 void drive_put_ref(DriveInfo
*dinfo
)
225 assert(dinfo
->refcount
);
226 if (--dinfo
->refcount
== 0) {
231 void drive_get_ref(DriveInfo
*dinfo
)
241 static void drive_put_ref_bh(void *opaque
)
243 DrivePutRefBH
*s
= opaque
;
245 drive_put_ref(s
->dinfo
);
246 qemu_bh_delete(s
->bh
);
251 * Release a drive reference in a BH
253 * It is not possible to use drive_put_ref() from a callback function when the
254 * callers still need the drive. In such cases we schedule a BH to release the
257 static void drive_put_ref_bh_schedule(DriveInfo
*dinfo
)
261 s
= g_new(DrivePutRefBH
, 1);
262 s
->bh
= qemu_bh_new(drive_put_ref_bh
, s
);
264 qemu_bh_schedule(s
->bh
);
267 static int parse_block_error_action(const char *buf
, bool is_read
)
269 if (!strcmp(buf
, "ignore")) {
270 return BLOCKDEV_ON_ERROR_IGNORE
;
271 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
272 return BLOCKDEV_ON_ERROR_ENOSPC
;
273 } else if (!strcmp(buf
, "stop")) {
274 return BLOCKDEV_ON_ERROR_STOP
;
275 } else if (!strcmp(buf
, "report")) {
276 return BLOCKDEV_ON_ERROR_REPORT
;
278 error_report("'%s' invalid %s error action",
279 buf
, is_read
? "read" : "write");
284 static bool do_check_io_limits(BlockIOLimit
*io_limits
, Error
**errp
)
291 bps_flag
= (io_limits
->bps
[BLOCK_IO_LIMIT_TOTAL
] != 0)
292 && ((io_limits
->bps
[BLOCK_IO_LIMIT_READ
] != 0)
293 || (io_limits
->bps
[BLOCK_IO_LIMIT_WRITE
] != 0));
294 iops_flag
= (io_limits
->iops
[BLOCK_IO_LIMIT_TOTAL
] != 0)
295 && ((io_limits
->iops
[BLOCK_IO_LIMIT_READ
] != 0)
296 || (io_limits
->iops
[BLOCK_IO_LIMIT_WRITE
] != 0));
297 if (bps_flag
|| iops_flag
) {
298 error_setg(errp
, "bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) "
299 "cannot be used at the same time");
303 if (io_limits
->bps
[BLOCK_IO_LIMIT_TOTAL
] < 0 ||
304 io_limits
->bps
[BLOCK_IO_LIMIT_WRITE
] < 0 ||
305 io_limits
->bps
[BLOCK_IO_LIMIT_READ
] < 0 ||
306 io_limits
->iops
[BLOCK_IO_LIMIT_TOTAL
] < 0 ||
307 io_limits
->iops
[BLOCK_IO_LIMIT_WRITE
] < 0 ||
308 io_limits
->iops
[BLOCK_IO_LIMIT_READ
] < 0) {
309 error_setg(errp
, "bps and iops values must be 0 or greater");
316 static DriveInfo
*blockdev_init(QemuOpts
*all_opts
,
317 BlockInterfaceType block_default_type
)
320 const char *file
= NULL
;
322 const char *mediastr
= "";
323 BlockInterfaceType type
;
324 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
326 int cyls
, heads
, secs
, translation
;
331 int on_read_error
, on_write_error
;
334 BlockIOLimit io_limits
;
342 bool has_driver_specific_opts
;
344 translation
= BIOS_ATA_TRANSLATION_AUTO
;
347 /* Check common options by copying from all_opts to opts, all other options
348 * are stored in bs_opts. */
349 id
= qemu_opts_id(all_opts
);
350 opts
= qemu_opts_create(&qemu_common_drive_opts
, id
, 1, &error
);
351 if (error_is_set(&error
)) {
352 qerror_report_err(error
);
357 bs_opts
= qdict_new();
358 qemu_opts_to_qdict(all_opts
, bs_opts
);
359 qemu_opts_absorb_qdict(opts
, bs_opts
, &error
);
360 if (error_is_set(&error
)) {
361 qerror_report_err(error
);
367 qdict_del(bs_opts
, "id");
370 has_driver_specific_opts
= !!qdict_size(bs_opts
);
372 /* extract parameters */
373 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
374 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
375 index
= qemu_opt_get_number(opts
, "index", -1);
377 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
378 heads
= qemu_opt_get_number(opts
, "heads", 0);
379 secs
= qemu_opt_get_number(opts
, "secs", 0);
381 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
382 ro
= qemu_opt_get_bool(opts
, "read-only", 0);
383 copy_on_read
= qemu_opt_get_bool(opts
, "copy-on-read", false);
385 file
= qemu_opt_get(opts
, "file");
386 serial
= qemu_opt_get(opts
, "serial");
388 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
389 for (type
= 0; type
< IF_COUNT
&& strcmp(buf
, if_name
[type
]); type
++)
391 if (type
== IF_COUNT
) {
392 error_report("unsupported bus type '%s'", buf
);
396 type
= block_default_type
;
399 max_devs
= if_max_devs
[type
];
401 if (cyls
|| heads
|| secs
) {
403 error_report("invalid physical cyls number");
407 error_report("invalid physical heads number");
411 error_report("invalid physical secs number");
416 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
418 error_report("'%s' trans must be used with cyls, heads and secs",
422 if (!strcmp(buf
, "none"))
423 translation
= BIOS_ATA_TRANSLATION_NONE
;
424 else if (!strcmp(buf
, "lba"))
425 translation
= BIOS_ATA_TRANSLATION_LBA
;
426 else if (!strcmp(buf
, "auto"))
427 translation
= BIOS_ATA_TRANSLATION_AUTO
;
429 error_report("'%s' invalid translation type", buf
);
434 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
435 if (!strcmp(buf
, "disk")) {
437 } else if (!strcmp(buf
, "cdrom")) {
438 if (cyls
|| secs
|| heads
) {
439 error_report("CHS can't be set with media=%s", buf
);
444 error_report("'%s' invalid media", buf
);
449 if ((buf
= qemu_opt_get(opts
, "discard")) != NULL
) {
450 if (bdrv_parse_discard_flags(buf
, &bdrv_flags
) != 0) {
451 error_report("invalid discard option");
457 if (qemu_opt_get_bool(opts
, "cache.writeback", true)) {
458 bdrv_flags
|= BDRV_O_CACHE_WB
;
460 if (qemu_opt_get_bool(opts
, "cache.direct", false)) {
461 bdrv_flags
|= BDRV_O_NOCACHE
;
463 if (qemu_opt_get_bool(opts
, "cache.no-flush", true)) {
464 bdrv_flags
|= BDRV_O_NO_FLUSH
;
467 #ifdef CONFIG_LINUX_AIO
468 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
469 if (!strcmp(buf
, "native")) {
470 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
471 } else if (!strcmp(buf
, "threads")) {
472 /* this is the default */
474 error_report("invalid aio option");
480 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
481 if (is_help_option(buf
)) {
482 error_printf("Supported formats:");
483 bdrv_iterate_format(bdrv_format_print
, NULL
);
488 qdict_put(bs_opts
, "driver", qstring_from_str(buf
));
491 /* disk I/O throttling */
492 io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
] =
493 qemu_opt_get_number(opts
, "throttling.bps-total", 0);
494 io_limits
.bps
[BLOCK_IO_LIMIT_READ
] =
495 qemu_opt_get_number(opts
, "throttling.bps-read", 0);
496 io_limits
.bps
[BLOCK_IO_LIMIT_WRITE
] =
497 qemu_opt_get_number(opts
, "throttling.bps-write", 0);
498 io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
] =
499 qemu_opt_get_number(opts
, "throttling.iops-total", 0);
500 io_limits
.iops
[BLOCK_IO_LIMIT_READ
] =
501 qemu_opt_get_number(opts
, "throttling.iops-read", 0);
502 io_limits
.iops
[BLOCK_IO_LIMIT_WRITE
] =
503 qemu_opt_get_number(opts
, "throttling.iops-write", 0);
505 if (!do_check_io_limits(&io_limits
, &error
)) {
506 error_report("%s", error_get_pretty(error
));
511 if (qemu_opt_get(opts
, "boot") != NULL
) {
512 fprintf(stderr
, "qemu-kvm: boot=on|off is deprecated and will be "
513 "ignored. Future versions will reject this parameter. Please "
514 "update your scripts.\n");
517 on_write_error
= BLOCKDEV_ON_ERROR_ENOSPC
;
518 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
519 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
520 error_report("werror is not supported by this bus type");
524 on_write_error
= parse_block_error_action(buf
, 0);
525 if (on_write_error
< 0) {
530 on_read_error
= BLOCKDEV_ON_ERROR_REPORT
;
531 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
532 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_SCSI
&& type
!= IF_NONE
) {
533 error_report("rerror is not supported by this bus type");
537 on_read_error
= parse_block_error_action(buf
, 1);
538 if (on_read_error
< 0) {
543 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
544 if (type
!= IF_VIRTIO
) {
545 error_report("addr is not supported by this bus type");
550 /* compute bus and unit according index */
553 if (bus_id
!= 0 || unit_id
!= -1) {
554 error_report("index cannot be used with bus and unit");
557 bus_id
= drive_index_to_bus_id(type
, index
);
558 unit_id
= drive_index_to_unit_id(type
, index
);
561 /* if user doesn't specify a unit_id,
562 * try to find the first free
567 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
569 if (max_devs
&& unit_id
>= max_devs
) {
578 if (max_devs
&& unit_id
>= max_devs
) {
579 error_report("unit %d too big (max is %d)",
580 unit_id
, max_devs
- 1);
585 * catch multiple definitions
588 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
589 error_report("drive with bus=%d, unit=%d (index=%d) exists",
590 bus_id
, unit_id
, index
);
596 dinfo
= g_malloc0(sizeof(*dinfo
));
597 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
598 dinfo
->id
= g_strdup(buf
);
600 /* no id supplied -> create one */
601 dinfo
->id
= g_malloc0(32);
602 if (type
== IF_IDE
|| type
== IF_SCSI
)
603 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
605 snprintf(dinfo
->id
, 32, "%s%i%s%i",
606 if_name
[type
], bus_id
, mediastr
, unit_id
);
608 snprintf(dinfo
->id
, 32, "%s%s%i",
609 if_name
[type
], mediastr
, unit_id
);
611 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
612 dinfo
->bdrv
->open_flags
= snapshot
? BDRV_O_SNAPSHOT
: 0;
613 dinfo
->bdrv
->read_only
= ro
;
614 dinfo
->devaddr
= devaddr
;
617 dinfo
->unit
= unit_id
;
619 dinfo
->heads
= heads
;
621 dinfo
->trans
= translation
;
622 dinfo
->opts
= all_opts
;
624 if (serial
!= NULL
) {
625 dinfo
->serial
= g_strdup(serial
);
627 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
629 bdrv_set_on_error(dinfo
->bdrv
, on_read_error
, on_write_error
);
631 /* disk I/O throttling */
632 bdrv_set_io_limits(dinfo
->bdrv
, &io_limits
);
639 dinfo
->media_cd
= media
== MEDIA_CDROM
;
648 /* add virtio block device */
650 devopts
= qemu_opts_create_nofail(qemu_find_opts("device"));
651 if (arch_type
== QEMU_ARCH_S390X
) {
652 qemu_opt_set(devopts
, "driver", "virtio-blk-s390");
654 qemu_opt_set(devopts
, "driver", "virtio-blk-pci");
656 qemu_opt_set(devopts
, "drive", dinfo
->id
);
658 qemu_opt_set(devopts
, "addr", devaddr
);
664 if (!file
|| !*file
) {
665 if (has_driver_specific_opts
) {
672 /* always use cache=unsafe with snapshot */
673 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
674 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
678 bdrv_flags
|= BDRV_O_COPY_ON_READ
;
681 if (runstate_check(RUN_STATE_INMIGRATE
)) {
682 bdrv_flags
|= BDRV_O_INCOMING
;
685 if (media
== MEDIA_CDROM
) {
686 /* CDROM is fine for any interface, don't check. */
688 } else if (ro
== 1) {
689 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&&
690 type
!= IF_NONE
&& type
!= IF_PFLASH
) {
691 error_report("read-only not supported by this bus type");
696 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
698 if (ro
&& copy_on_read
) {
699 error_report("warning: disabling copy_on_read on read-only drive");
703 ret
= bdrv_open(dinfo
->bdrv
, file
, bs_opts
, bdrv_flags
, NULL
);
706 if (ret
== -EMEDIUMTYPE
) {
707 error_report("could not open disk image %s: not in %s format",
708 file
?: dinfo
->id
, qdict_get_str(bs_opts
, "driver"));
710 error_report("could not open disk image %s: %s",
711 file
?: dinfo
->id
, strerror(-ret
));
716 if (bdrv_key_required(dinfo
->bdrv
))
727 bdrv_delete(dinfo
->bdrv
);
729 QTAILQ_REMOVE(&drives
, dinfo
, next
);
734 static void qemu_opt_rename(QemuOpts
*opts
, const char *from
, const char *to
)
738 value
= qemu_opt_get(opts
, from
);
740 qemu_opt_set(opts
, to
, value
);
741 qemu_opt_unset(opts
, from
);
745 DriveInfo
*drive_init(QemuOpts
*all_opts
, BlockInterfaceType block_default_type
)
750 * Check that only old options are used by copying into a QemuOpts with
751 * stricter checks. Going through a QDict seems to be the easiest way to
754 QemuOpts
* check_opts
;
756 Error
*local_err
= NULL
;
758 qdict
= qemu_opts_to_qdict(all_opts
, NULL
);
759 check_opts
= qemu_opts_from_qdict(&qemu_old_drive_opts
, qdict
, &local_err
);
762 if (error_is_set(&local_err
)) {
763 qerror_report_err(local_err
);
764 error_free(local_err
);
767 qemu_opts_del(check_opts
);
769 /* Change legacy command line options into QMP ones */
770 qemu_opt_rename(all_opts
, "iops", "throttling.iops-total");
771 qemu_opt_rename(all_opts
, "iops_rd", "throttling.iops-read");
772 qemu_opt_rename(all_opts
, "iops_wr", "throttling.iops-write");
774 qemu_opt_rename(all_opts
, "bps", "throttling.bps-total");
775 qemu_opt_rename(all_opts
, "bps_rd", "throttling.bps-read");
776 qemu_opt_rename(all_opts
, "bps_wr", "throttling.bps-write");
778 qemu_opt_rename(all_opts
, "readonly", "read-only");
780 value
= qemu_opt_get(all_opts
, "cache");
784 if (bdrv_parse_cache_flags(value
, &flags
) != 0) {
785 error_report("invalid cache option");
789 /* Specific options take precedence */
790 if (!qemu_opt_get(all_opts
, "cache.writeback")) {
791 qemu_opt_set_bool(all_opts
, "cache.writeback",
792 !!(flags
& BDRV_O_CACHE_WB
));
794 if (!qemu_opt_get(all_opts
, "cache.direct")) {
795 qemu_opt_set_bool(all_opts
, "cache.direct",
796 !!(flags
& BDRV_O_NOCACHE
));
798 if (!qemu_opt_get(all_opts
, "cache.no-flush")) {
799 qemu_opt_set_bool(all_opts
, "cache.no-flush",
800 !!(flags
& BDRV_O_NO_FLUSH
));
802 qemu_opt_unset(all_opts
, "cache");
805 return blockdev_init(all_opts
, block_default_type
);
808 void do_commit(Monitor
*mon
, const QDict
*qdict
)
810 const char *device
= qdict_get_str(qdict
, "device");
811 BlockDriverState
*bs
;
814 if (!strcmp(device
, "all")) {
815 ret
= bdrv_commit_all();
817 bs
= bdrv_find(device
);
819 monitor_printf(mon
, "Device '%s' not found\n", device
);
822 ret
= bdrv_commit(bs
);
825 monitor_printf(mon
, "'commit' error for '%s': %s\n", device
,
830 static void blockdev_do_action(int kind
, void *data
, Error
**errp
)
832 TransactionAction action
;
833 TransactionActionList list
;
837 list
.value
= &action
;
839 qmp_transaction(&list
, errp
);
842 void qmp_blockdev_snapshot_sync(const char *device
, const char *snapshot_file
,
843 bool has_format
, const char *format
,
844 bool has_mode
, enum NewImageMode mode
,
847 BlockdevSnapshot snapshot
= {
848 .device
= (char *) device
,
849 .snapshot_file
= (char *) snapshot_file
,
850 .has_format
= has_format
,
851 .format
= (char *) format
,
852 .has_mode
= has_mode
,
855 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
,
860 /* New and old BlockDriverState structs for group snapshots */
862 typedef struct BlkTransactionState BlkTransactionState
;
864 /* Only prepare() may fail. In a single transaction, only one of commit() or
865 abort() will be called, clean() will always be called if it present. */
866 typedef struct BdrvActionOps
{
867 /* Size of state struct, in bytes. */
868 size_t instance_size
;
869 /* Prepare the work, must NOT be NULL. */
870 void (*prepare
)(BlkTransactionState
*common
, Error
**errp
);
871 /* Commit the changes, can be NULL. */
872 void (*commit
)(BlkTransactionState
*common
);
873 /* Abort the changes on fail, can be NULL. */
874 void (*abort
)(BlkTransactionState
*common
);
875 /* Clean up resource in the end, can be NULL. */
876 void (*clean
)(BlkTransactionState
*common
);
880 * This structure must be arranged as first member in child type, assuming
881 * that compiler will also arrange it to the same address with parent instance.
882 * Later it will be used in free().
884 struct BlkTransactionState
{
885 TransactionAction
*action
;
886 const BdrvActionOps
*ops
;
887 QSIMPLEQ_ENTRY(BlkTransactionState
) entry
;
890 /* external snapshot private data */
891 typedef struct ExternalSnapshotState
{
892 BlkTransactionState common
;
893 BlockDriverState
*old_bs
;
894 BlockDriverState
*new_bs
;
895 } ExternalSnapshotState
;
897 static void external_snapshot_prepare(BlkTransactionState
*common
,
902 Error
*local_err
= NULL
;
904 const char *new_image_file
;
905 const char *format
= "qcow2";
906 enum NewImageMode mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
907 ExternalSnapshotState
*state
=
908 DO_UPCAST(ExternalSnapshotState
, common
, common
);
909 TransactionAction
*action
= common
->action
;
912 g_assert(action
->kind
== TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
);
914 device
= action
->blockdev_snapshot_sync
->device
;
915 new_image_file
= action
->blockdev_snapshot_sync
->snapshot_file
;
916 if (action
->blockdev_snapshot_sync
->has_format
) {
917 format
= action
->blockdev_snapshot_sync
->format
;
919 if (action
->blockdev_snapshot_sync
->has_mode
) {
920 mode
= action
->blockdev_snapshot_sync
->mode
;
923 /* start processing */
924 drv
= bdrv_find_format(format
);
926 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
930 state
->old_bs
= bdrv_find(device
);
931 if (!state
->old_bs
) {
932 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
936 if (!bdrv_is_inserted(state
->old_bs
)) {
937 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
941 if (bdrv_in_use(state
->old_bs
)) {
942 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
946 if (!bdrv_is_read_only(state
->old_bs
)) {
947 if (bdrv_flush(state
->old_bs
)) {
948 error_set(errp
, QERR_IO_ERROR
);
953 flags
= state
->old_bs
->open_flags
;
955 /* create new image w/backing file */
956 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
957 bdrv_img_create(new_image_file
, format
,
958 state
->old_bs
->filename
,
959 state
->old_bs
->drv
->format_name
,
960 NULL
, -1, flags
, &local_err
, false);
961 if (error_is_set(&local_err
)) {
962 error_propagate(errp
, local_err
);
967 /* We will manually add the backing_hd field to the bs later */
968 state
->new_bs
= bdrv_new("");
969 /* TODO Inherit bs->options or only take explicit options with an
970 * extended QMP command? */
971 ret
= bdrv_open(state
->new_bs
, new_image_file
, NULL
,
972 flags
| BDRV_O_NO_BACKING
, drv
);
974 error_setg_file_open(errp
, -ret
, new_image_file
);
978 static void external_snapshot_commit(BlkTransactionState
*common
)
980 ExternalSnapshotState
*state
=
981 DO_UPCAST(ExternalSnapshotState
, common
, common
);
983 /* This removes our old bs and adds the new bs */
984 bdrv_append(state
->new_bs
, state
->old_bs
);
985 /* We don't need (or want) to use the transactional
986 * bdrv_reopen_multiple() across all the entries at once, because we
987 * don't want to abort all of them if one of them fails the reopen */
988 bdrv_reopen(state
->new_bs
, state
->new_bs
->open_flags
& ~BDRV_O_RDWR
,
992 static void external_snapshot_abort(BlkTransactionState
*common
)
994 ExternalSnapshotState
*state
=
995 DO_UPCAST(ExternalSnapshotState
, common
, common
);
997 bdrv_delete(state
->new_bs
);
1001 typedef struct DriveBackupState
{
1002 BlkTransactionState common
;
1003 BlockDriverState
*bs
;
1007 static void drive_backup_prepare(BlkTransactionState
*common
, Error
**errp
)
1009 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1010 DriveBackup
*backup
;
1011 Error
*local_err
= NULL
;
1013 assert(common
->action
->kind
== TRANSACTION_ACTION_KIND_DRIVE_BACKUP
);
1014 backup
= common
->action
->drive_backup
;
1016 qmp_drive_backup(backup
->device
, backup
->target
,
1017 backup
->has_format
, backup
->format
,
1019 backup
->has_mode
, backup
->mode
,
1020 backup
->has_speed
, backup
->speed
,
1021 backup
->has_on_source_error
, backup
->on_source_error
,
1022 backup
->has_on_target_error
, backup
->on_target_error
,
1024 if (error_is_set(&local_err
)) {
1025 error_propagate(errp
, local_err
);
1031 state
->bs
= bdrv_find(backup
->device
);
1032 state
->job
= state
->bs
->job
;
1035 static void drive_backup_abort(BlkTransactionState
*common
)
1037 DriveBackupState
*state
= DO_UPCAST(DriveBackupState
, common
, common
);
1038 BlockDriverState
*bs
= state
->bs
;
1040 /* Only cancel if it's the job we started */
1041 if (bs
&& bs
->job
&& bs
->job
== state
->job
) {
1042 block_job_cancel_sync(bs
->job
);
1046 static void abort_prepare(BlkTransactionState
*common
, Error
**errp
)
1048 error_setg(errp
, "Transaction aborted using Abort action");
1051 static void abort_commit(BlkTransactionState
*common
)
1053 g_assert_not_reached(); /* this action never succeeds */
1056 static const BdrvActionOps actions
[] = {
1057 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC
] = {
1058 .instance_size
= sizeof(ExternalSnapshotState
),
1059 .prepare
= external_snapshot_prepare
,
1060 .commit
= external_snapshot_commit
,
1061 .abort
= external_snapshot_abort
,
1063 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP
] = {
1064 .instance_size
= sizeof(DriveBackupState
),
1065 .prepare
= drive_backup_prepare
,
1066 .abort
= drive_backup_abort
,
1068 [TRANSACTION_ACTION_KIND_ABORT
] = {
1069 .instance_size
= sizeof(BlkTransactionState
),
1070 .prepare
= abort_prepare
,
1071 .commit
= abort_commit
,
1076 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail
1077 * then we do not pivot any of the devices in the group, and abandon the
1080 void qmp_transaction(TransactionActionList
*dev_list
, Error
**errp
)
1082 TransactionActionList
*dev_entry
= dev_list
;
1083 BlkTransactionState
*state
, *next
;
1084 Error
*local_err
= NULL
;
1086 QSIMPLEQ_HEAD(snap_bdrv_states
, BlkTransactionState
) snap_bdrv_states
;
1087 QSIMPLEQ_INIT(&snap_bdrv_states
);
1089 /* drain all i/o before any snapshots */
1092 /* We don't do anything in this loop that commits us to the snapshot */
1093 while (NULL
!= dev_entry
) {
1094 TransactionAction
*dev_info
= NULL
;
1095 const BdrvActionOps
*ops
;
1097 dev_info
= dev_entry
->value
;
1098 dev_entry
= dev_entry
->next
;
1100 assert(dev_info
->kind
< ARRAY_SIZE(actions
));
1102 ops
= &actions
[dev_info
->kind
];
1103 state
= g_malloc0(ops
->instance_size
);
1105 state
->action
= dev_info
;
1106 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states
, state
, entry
);
1108 state
->ops
->prepare(state
, &local_err
);
1109 if (error_is_set(&local_err
)) {
1110 error_propagate(errp
, local_err
);
1111 goto delete_and_fail
;
1115 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1116 if (state
->ops
->commit
) {
1117 state
->ops
->commit(state
);
1126 * failure, and it is all-or-none; abandon each new bs, and keep using
1127 * the original bs for all images
1129 QSIMPLEQ_FOREACH(state
, &snap_bdrv_states
, entry
) {
1130 if (state
->ops
->abort
) {
1131 state
->ops
->abort(state
);
1135 QSIMPLEQ_FOREACH_SAFE(state
, &snap_bdrv_states
, entry
, next
) {
1136 if (state
->ops
->clean
) {
1137 state
->ops
->clean(state
);
1144 static void eject_device(BlockDriverState
*bs
, int force
, Error
**errp
)
1146 if (bdrv_in_use(bs
)) {
1147 error_set(errp
, QERR_DEVICE_IN_USE
, bdrv_get_device_name(bs
));
1150 if (!bdrv_dev_has_removable_media(bs
)) {
1151 error_set(errp
, QERR_DEVICE_NOT_REMOVABLE
, bdrv_get_device_name(bs
));
1155 if (bdrv_dev_is_medium_locked(bs
) && !bdrv_dev_is_tray_open(bs
)) {
1156 bdrv_dev_eject_request(bs
, force
);
1158 error_set(errp
, QERR_DEVICE_LOCKED
, bdrv_get_device_name(bs
));
1166 void qmp_eject(const char *device
, bool has_force
, bool force
, Error
**errp
)
1168 BlockDriverState
*bs
;
1170 bs
= bdrv_find(device
);
1172 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1176 eject_device(bs
, force
, errp
);
1179 void qmp_block_passwd(const char *device
, const char *password
, Error
**errp
)
1181 BlockDriverState
*bs
;
1184 bs
= bdrv_find(device
);
1186 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1190 err
= bdrv_set_key(bs
, password
);
1191 if (err
== -EINVAL
) {
1192 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1194 } else if (err
< 0) {
1195 error_set(errp
, QERR_INVALID_PASSWORD
);
1200 static void qmp_bdrv_open_encrypted(BlockDriverState
*bs
, const char *filename
,
1201 int bdrv_flags
, BlockDriver
*drv
,
1202 const char *password
, Error
**errp
)
1206 ret
= bdrv_open(bs
, filename
, NULL
, bdrv_flags
, drv
);
1208 error_setg_file_open(errp
, -ret
, filename
);
1212 if (bdrv_key_required(bs
)) {
1214 if (bdrv_set_key(bs
, password
) < 0) {
1215 error_set(errp
, QERR_INVALID_PASSWORD
);
1218 error_set(errp
, QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
1219 bdrv_get_encrypted_filename(bs
));
1221 } else if (password
) {
1222 error_set(errp
, QERR_DEVICE_NOT_ENCRYPTED
, bdrv_get_device_name(bs
));
1226 void qmp_change_blockdev(const char *device
, const char *filename
,
1227 bool has_format
, const char *format
, Error
**errp
)
1229 BlockDriverState
*bs
;
1230 BlockDriver
*drv
= NULL
;
1234 bs
= bdrv_find(device
);
1236 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1241 drv
= bdrv_find_whitelisted_format(format
, bs
->read_only
);
1243 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1248 eject_device(bs
, 0, &err
);
1249 if (error_is_set(&err
)) {
1250 error_propagate(errp
, err
);
1254 bdrv_flags
= bdrv_is_read_only(bs
) ? 0 : BDRV_O_RDWR
;
1255 bdrv_flags
|= bdrv_is_snapshot(bs
) ? BDRV_O_SNAPSHOT
: 0;
1257 qmp_bdrv_open_encrypted(bs
, filename
, bdrv_flags
, drv
, NULL
, errp
);
1260 /* throttling disk I/O limits */
1261 void qmp_block_set_io_throttle(const char *device
, int64_t bps
, int64_t bps_rd
,
1262 int64_t bps_wr
, int64_t iops
, int64_t iops_rd
,
1263 int64_t iops_wr
, Error
**errp
)
1265 BlockIOLimit io_limits
;
1266 BlockDriverState
*bs
;
1268 bs
= bdrv_find(device
);
1270 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1274 io_limits
.bps
[BLOCK_IO_LIMIT_TOTAL
] = bps
;
1275 io_limits
.bps
[BLOCK_IO_LIMIT_READ
] = bps_rd
;
1276 io_limits
.bps
[BLOCK_IO_LIMIT_WRITE
] = bps_wr
;
1277 io_limits
.iops
[BLOCK_IO_LIMIT_TOTAL
]= iops
;
1278 io_limits
.iops
[BLOCK_IO_LIMIT_READ
] = iops_rd
;
1279 io_limits
.iops
[BLOCK_IO_LIMIT_WRITE
]= iops_wr
;
1281 if (!do_check_io_limits(&io_limits
, errp
)) {
1285 bs
->io_limits
= io_limits
;
1287 if (!bs
->io_limits_enabled
&& bdrv_io_limits_enabled(bs
)) {
1288 bdrv_io_limits_enable(bs
);
1289 } else if (bs
->io_limits_enabled
&& !bdrv_io_limits_enabled(bs
)) {
1290 bdrv_io_limits_disable(bs
);
1292 if (bs
->block_timer
) {
1293 qemu_mod_timer(bs
->block_timer
, qemu_get_clock_ns(vm_clock
));
1298 int do_drive_del(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1300 const char *id
= qdict_get_str(qdict
, "id");
1301 BlockDriverState
*bs
;
1305 qerror_report(QERR_DEVICE_NOT_FOUND
, id
);
1308 if (bdrv_in_use(bs
)) {
1309 qerror_report(QERR_DEVICE_IN_USE
, id
);
1313 /* quiesce block driver; prevent further io */
1318 /* if we have a device attached to this BlockDriverState
1319 * then we need to make the drive anonymous until the device
1320 * can be removed. If this is a drive with no device backing
1321 * then we can just get rid of the block driver state right here.
1323 if (bdrv_get_attached_dev(bs
)) {
1326 /* Further I/O must not pause the guest */
1327 bdrv_set_on_error(bs
, BLOCKDEV_ON_ERROR_REPORT
,
1328 BLOCKDEV_ON_ERROR_REPORT
);
1330 drive_uninit(drive_get_by_blockdev(bs
));
1336 void qmp_block_resize(const char *device
, int64_t size
, Error
**errp
)
1338 BlockDriverState
*bs
;
1341 bs
= bdrv_find(device
);
1343 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1348 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "size", "a >0 size");
1352 /* complete all in-flight operations before resizing the device */
1355 ret
= bdrv_truncate(bs
, size
);
1360 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1363 error_set(errp
, QERR_UNSUPPORTED
);
1366 error_set(errp
, QERR_DEVICE_IS_READ_ONLY
, device
);
1369 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1372 error_setg_errno(errp
, -ret
, "Could not resize");
1377 static void block_job_cb(void *opaque
, int ret
)
1379 BlockDriverState
*bs
= opaque
;
1382 trace_block_job_cb(bs
, bs
->job
, ret
);
1385 obj
= qobject_from_block_job(bs
->job
);
1387 QDict
*dict
= qobject_to_qdict(obj
);
1388 qdict_put(dict
, "error", qstring_from_str(strerror(-ret
)));
1391 if (block_job_is_cancelled(bs
->job
)) {
1392 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED
, obj
);
1394 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED
, obj
);
1396 qobject_decref(obj
);
1398 drive_put_ref_bh_schedule(drive_get_by_blockdev(bs
));
1401 void qmp_block_stream(const char *device
, bool has_base
,
1402 const char *base
, bool has_speed
, int64_t speed
,
1403 bool has_on_error
, BlockdevOnError on_error
,
1406 BlockDriverState
*bs
;
1407 BlockDriverState
*base_bs
= NULL
;
1408 Error
*local_err
= NULL
;
1410 if (!has_on_error
) {
1411 on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1414 bs
= bdrv_find(device
);
1416 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1421 base_bs
= bdrv_find_backing_image(bs
, base
);
1422 if (base_bs
== NULL
) {
1423 error_set(errp
, QERR_BASE_NOT_FOUND
, base
);
1428 stream_start(bs
, base_bs
, base
, has_speed
? speed
: 0,
1429 on_error
, block_job_cb
, bs
, &local_err
);
1430 if (error_is_set(&local_err
)) {
1431 error_propagate(errp
, local_err
);
1435 /* Grab a reference so hotplug does not delete the BlockDriverState from
1438 drive_get_ref(drive_get_by_blockdev(bs
));
1440 trace_qmp_block_stream(bs
, bs
->job
);
1443 void qmp_block_commit(const char *device
,
1444 bool has_base
, const char *base
, const char *top
,
1445 bool has_speed
, int64_t speed
,
1448 BlockDriverState
*bs
;
1449 BlockDriverState
*base_bs
, *top_bs
;
1450 Error
*local_err
= NULL
;
1451 /* This will be part of the QMP command, if/when the
1452 * BlockdevOnError change for blkmirror makes it in
1454 BlockdevOnError on_error
= BLOCKDEV_ON_ERROR_REPORT
;
1456 /* drain all i/o before commits */
1459 bs
= bdrv_find(device
);
1461 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1465 /* default top_bs is the active layer */
1469 if (strcmp(bs
->filename
, top
) != 0) {
1470 top_bs
= bdrv_find_backing_image(bs
, top
);
1474 if (top_bs
== NULL
) {
1475 error_setg(errp
, "Top image file %s not found", top
? top
: "NULL");
1479 if (has_base
&& base
) {
1480 base_bs
= bdrv_find_backing_image(top_bs
, base
);
1482 base_bs
= bdrv_find_base(top_bs
);
1485 if (base_bs
== NULL
) {
1486 error_set(errp
, QERR_BASE_NOT_FOUND
, base
? base
: "NULL");
1490 commit_start(bs
, base_bs
, top_bs
, speed
, on_error
, block_job_cb
, bs
,
1492 if (local_err
!= NULL
) {
1493 error_propagate(errp
, local_err
);
1496 /* Grab a reference so hotplug does not delete the BlockDriverState from
1499 drive_get_ref(drive_get_by_blockdev(bs
));
1502 void qmp_drive_backup(const char *device
, const char *target
,
1503 bool has_format
, const char *format
,
1504 enum MirrorSyncMode sync
,
1505 bool has_mode
, enum NewImageMode mode
,
1506 bool has_speed
, int64_t speed
,
1507 bool has_on_source_error
, BlockdevOnError on_source_error
,
1508 bool has_on_target_error
, BlockdevOnError on_target_error
,
1511 BlockDriverState
*bs
;
1512 BlockDriverState
*target_bs
;
1513 BlockDriverState
*source
= NULL
;
1514 BlockDriver
*drv
= NULL
;
1515 Error
*local_err
= NULL
;
1523 if (!has_on_source_error
) {
1524 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1526 if (!has_on_target_error
) {
1527 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1530 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1533 bs
= bdrv_find(device
);
1535 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1539 if (!bdrv_is_inserted(bs
)) {
1540 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1545 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1548 drv
= bdrv_find_format(format
);
1550 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1555 if (bdrv_in_use(bs
)) {
1556 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1560 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1562 /* See if we have a backing HD we can use to create our new image
1564 if (sync
== MIRROR_SYNC_MODE_TOP
) {
1565 source
= bs
->backing_hd
;
1567 sync
= MIRROR_SYNC_MODE_FULL
;
1570 if (sync
== MIRROR_SYNC_MODE_NONE
) {
1574 size
= bdrv_getlength(bs
);
1576 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1580 if (mode
!= NEW_IMAGE_MODE_EXISTING
) {
1581 assert(format
&& drv
);
1583 bdrv_img_create(target
, format
, source
->filename
,
1584 source
->drv
->format_name
, NULL
,
1585 size
, flags
, &local_err
, false);
1587 bdrv_img_create(target
, format
, NULL
, NULL
, NULL
,
1588 size
, flags
, &local_err
, false);
1592 if (error_is_set(&local_err
)) {
1593 error_propagate(errp
, local_err
);
1597 target_bs
= bdrv_new("");
1598 ret
= bdrv_open(target_bs
, target
, NULL
, flags
, drv
);
1600 bdrv_delete(target_bs
);
1601 error_setg_file_open(errp
, -ret
, target
);
1605 backup_start(bs
, target_bs
, speed
, sync
, on_source_error
, on_target_error
,
1606 block_job_cb
, bs
, &local_err
);
1607 if (local_err
!= NULL
) {
1608 bdrv_delete(target_bs
);
1609 error_propagate(errp
, local_err
);
1613 /* Grab a reference so hotplug does not delete the BlockDriverState from
1616 drive_get_ref(drive_get_by_blockdev(bs
));
1619 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20)
1621 void qmp_drive_mirror(const char *device
, const char *target
,
1622 bool has_format
, const char *format
,
1623 enum MirrorSyncMode sync
,
1624 bool has_mode
, enum NewImageMode mode
,
1625 bool has_speed
, int64_t speed
,
1626 bool has_granularity
, uint32_t granularity
,
1627 bool has_buf_size
, int64_t buf_size
,
1628 bool has_on_source_error
, BlockdevOnError on_source_error
,
1629 bool has_on_target_error
, BlockdevOnError on_target_error
,
1632 BlockDriverState
*bs
;
1633 BlockDriverState
*source
, *target_bs
;
1634 BlockDriver
*drv
= NULL
;
1635 Error
*local_err
= NULL
;
1643 if (!has_on_source_error
) {
1644 on_source_error
= BLOCKDEV_ON_ERROR_REPORT
;
1646 if (!has_on_target_error
) {
1647 on_target_error
= BLOCKDEV_ON_ERROR_REPORT
;
1650 mode
= NEW_IMAGE_MODE_ABSOLUTE_PATHS
;
1652 if (!has_granularity
) {
1655 if (!has_buf_size
) {
1656 buf_size
= DEFAULT_MIRROR_BUF_SIZE
;
1659 if (granularity
!= 0 && (granularity
< 512 || granularity
> 1048576 * 64)) {
1660 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1663 if (granularity
& (granularity
- 1)) {
1664 error_set(errp
, QERR_INVALID_PARAMETER
, device
);
1668 bs
= bdrv_find(device
);
1670 error_set(errp
, QERR_DEVICE_NOT_FOUND
, device
);
1674 if (!bdrv_is_inserted(bs
)) {
1675 error_set(errp
, QERR_DEVICE_HAS_NO_MEDIUM
, device
);
1680 format
= mode
== NEW_IMAGE_MODE_EXISTING
? NULL
: bs
->drv
->format_name
;
1683 drv
= bdrv_find_format(format
);
1685 error_set(errp
, QERR_INVALID_BLOCK_FORMAT
, format
);
1690 if (bdrv_in_use(bs
)) {
1691 error_set(errp
, QERR_DEVICE_IN_USE
, device
);
1695 flags
= bs
->open_flags
| BDRV_O_RDWR
;
1696 source
= bs
->backing_hd
;
1697 if (!source
&& sync
== MIRROR_SYNC_MODE_TOP
) {
1698 sync
= MIRROR_SYNC_MODE_FULL
;
1701 size
= bdrv_getlength(bs
);
1703 error_setg_errno(errp
, -size
, "bdrv_getlength failed");
1707 if (sync
== MIRROR_SYNC_MODE_FULL
&& mode
!= NEW_IMAGE_MODE_EXISTING
) {
1708 /* create new image w/o backing file */
1709 assert(format
&& drv
);
1710 bdrv_img_create(target
, format
,
1711 NULL
, NULL
, NULL
, size
, flags
, &local_err
, false);
1714 case NEW_IMAGE_MODE_EXISTING
:
1717 case NEW_IMAGE_MODE_ABSOLUTE_PATHS
:
1718 /* create new image with backing file */
1719 bdrv_img_create(target
, format
,
1721 source
->drv
->format_name
,
1722 NULL
, size
, flags
, &local_err
, false);
1729 if (error_is_set(&local_err
)) {
1730 error_propagate(errp
, local_err
);
1734 /* Mirroring takes care of copy-on-write using the source's backing
1737 target_bs
= bdrv_new("");
1738 ret
= bdrv_open(target_bs
, target
, NULL
, flags
| BDRV_O_NO_BACKING
, drv
);
1740 bdrv_delete(target_bs
);
1741 error_setg_file_open(errp
, -ret
, target
);
1745 mirror_start(bs
, target_bs
, speed
, granularity
, buf_size
, sync
,
1746 on_source_error
, on_target_error
,
1747 block_job_cb
, bs
, &local_err
);
1748 if (local_err
!= NULL
) {
1749 bdrv_delete(target_bs
);
1750 error_propagate(errp
, local_err
);
1754 /* Grab a reference so hotplug does not delete the BlockDriverState from
1757 drive_get_ref(drive_get_by_blockdev(bs
));
1760 static BlockJob
*find_block_job(const char *device
)
1762 BlockDriverState
*bs
;
1764 bs
= bdrv_find(device
);
1765 if (!bs
|| !bs
->job
) {
1771 void qmp_block_job_set_speed(const char *device
, int64_t speed
, Error
**errp
)
1773 BlockJob
*job
= find_block_job(device
);
1776 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1780 block_job_set_speed(job
, speed
, errp
);
1783 void qmp_block_job_cancel(const char *device
,
1784 bool has_force
, bool force
, Error
**errp
)
1786 BlockJob
*job
= find_block_job(device
);
1793 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1796 if (job
->paused
&& !force
) {
1797 error_set(errp
, QERR_BLOCK_JOB_PAUSED
, device
);
1801 trace_qmp_block_job_cancel(job
);
1802 block_job_cancel(job
);
1805 void qmp_block_job_pause(const char *device
, Error
**errp
)
1807 BlockJob
*job
= find_block_job(device
);
1810 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1814 trace_qmp_block_job_pause(job
);
1815 block_job_pause(job
);
1818 void qmp_block_job_resume(const char *device
, Error
**errp
)
1820 BlockJob
*job
= find_block_job(device
);
1823 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1827 trace_qmp_block_job_resume(job
);
1828 block_job_resume(job
);
1831 void qmp_block_job_complete(const char *device
, Error
**errp
)
1833 BlockJob
*job
= find_block_job(device
);
1836 error_set(errp
, QERR_BLOCK_JOB_NOT_ACTIVE
, device
);
1840 trace_qmp_block_job_complete(job
);
1841 block_job_complete(job
, errp
);
1844 static void do_qmp_query_block_jobs_one(void *opaque
, BlockDriverState
*bs
)
1846 BlockJobInfoList
**prev
= opaque
;
1847 BlockJob
*job
= bs
->job
;
1850 BlockJobInfoList
*elem
= g_new0(BlockJobInfoList
, 1);
1851 elem
->value
= block_job_query(bs
->job
);
1852 (*prev
)->next
= elem
;
1857 BlockJobInfoList
*qmp_query_block_jobs(Error
**errp
)
1859 /* Dummy is a fake list element for holding the head pointer */
1860 BlockJobInfoList dummy
= {};
1861 BlockJobInfoList
*prev
= &dummy
;
1862 bdrv_iterate(do_qmp_query_block_jobs_one
, &prev
);
1866 QemuOptsList qemu_common_drive_opts
= {
1868 .head
= QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts
.head
),
1872 .type
= QEMU_OPT_NUMBER
,
1873 .help
= "bus number",
1876 .type
= QEMU_OPT_NUMBER
,
1877 .help
= "unit number (i.e. lun for scsi)",
1880 .type
= QEMU_OPT_STRING
,
1881 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1884 .type
= QEMU_OPT_NUMBER
,
1885 .help
= "index number",
1888 .type
= QEMU_OPT_NUMBER
,
1889 .help
= "number of cylinders (ide disk geometry)",
1892 .type
= QEMU_OPT_NUMBER
,
1893 .help
= "number of heads (ide disk geometry)",
1896 .type
= QEMU_OPT_NUMBER
,
1897 .help
= "number of sectors (ide disk geometry)",
1900 .type
= QEMU_OPT_STRING
,
1901 .help
= "chs translation (auto, lba. none)",
1904 .type
= QEMU_OPT_STRING
,
1905 .help
= "media type (disk, cdrom)",
1908 .type
= QEMU_OPT_BOOL
,
1909 .help
= "enable/disable snapshot mode",
1912 .type
= QEMU_OPT_STRING
,
1913 .help
= "disk image",
1916 .type
= QEMU_OPT_STRING
,
1917 .help
= "discard operation (ignore/off, unmap/on)",
1919 .name
= "cache.writeback",
1920 .type
= QEMU_OPT_BOOL
,
1921 .help
= "enables writeback mode for any caches",
1923 .name
= "cache.direct",
1924 .type
= QEMU_OPT_BOOL
,
1925 .help
= "enables use of O_DIRECT (bypass the host page cache)",
1927 .name
= "cache.no-flush",
1928 .type
= QEMU_OPT_BOOL
,
1929 .help
= "ignore any flush requests for the device",
1932 .type
= QEMU_OPT_STRING
,
1933 .help
= "host AIO implementation (threads, native)",
1936 .type
= QEMU_OPT_STRING
,
1937 .help
= "disk format (raw, qcow2, ...)",
1940 .type
= QEMU_OPT_STRING
,
1941 .help
= "disk serial number",
1944 .type
= QEMU_OPT_STRING
,
1945 .help
= "read error action",
1948 .type
= QEMU_OPT_STRING
,
1949 .help
= "write error action",
1952 .type
= QEMU_OPT_STRING
,
1953 .help
= "pci address (virtio only)",
1955 .name
= "read-only",
1956 .type
= QEMU_OPT_BOOL
,
1957 .help
= "open drive file as read-only",
1959 .name
= "throttling.iops-total",
1960 .type
= QEMU_OPT_NUMBER
,
1961 .help
= "limit total I/O operations per second",
1963 .name
= "throttling.iops-read",
1964 .type
= QEMU_OPT_NUMBER
,
1965 .help
= "limit read operations per second",
1967 .name
= "throttling.iops-write",
1968 .type
= QEMU_OPT_NUMBER
,
1969 .help
= "limit write operations per second",
1971 .name
= "throttling.bps-total",
1972 .type
= QEMU_OPT_NUMBER
,
1973 .help
= "limit total bytes per second",
1975 .name
= "throttling.bps-read",
1976 .type
= QEMU_OPT_NUMBER
,
1977 .help
= "limit read bytes per second",
1979 .name
= "throttling.bps-write",
1980 .type
= QEMU_OPT_NUMBER
,
1981 .help
= "limit write bytes per second",
1983 .name
= "copy-on-read",
1984 .type
= QEMU_OPT_BOOL
,
1985 .help
= "copy read data from backing file into image file",
1988 .type
= QEMU_OPT_BOOL
,
1989 .help
= "(deprecated, ignored)",
1991 { /* end of list */ }
1995 QemuOptsList qemu_old_drive_opts
= {
1997 .head
= QTAILQ_HEAD_INITIALIZER(qemu_old_drive_opts
.head
),
2001 .type
= QEMU_OPT_NUMBER
,
2002 .help
= "bus number",
2005 .type
= QEMU_OPT_NUMBER
,
2006 .help
= "unit number (i.e. lun for scsi)",
2009 .type
= QEMU_OPT_STRING
,
2010 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
2013 .type
= QEMU_OPT_NUMBER
,
2014 .help
= "index number",
2017 .type
= QEMU_OPT_NUMBER
,
2018 .help
= "number of cylinders (ide disk geometry)",
2021 .type
= QEMU_OPT_NUMBER
,
2022 .help
= "number of heads (ide disk geometry)",
2025 .type
= QEMU_OPT_NUMBER
,
2026 .help
= "number of sectors (ide disk geometry)",
2029 .type
= QEMU_OPT_STRING
,
2030 .help
= "chs translation (auto, lba. none)",
2033 .type
= QEMU_OPT_STRING
,
2034 .help
= "media type (disk, cdrom)",
2037 .type
= QEMU_OPT_BOOL
,
2038 .help
= "enable/disable snapshot mode",
2041 .type
= QEMU_OPT_STRING
,
2042 .help
= "disk image",
2045 .type
= QEMU_OPT_STRING
,
2046 .help
= "discard operation (ignore/off, unmap/on)",
2049 .type
= QEMU_OPT_STRING
,
2050 .help
= "host cache usage (none, writeback, writethrough, "
2051 "directsync, unsafe)",
2054 .type
= QEMU_OPT_STRING
,
2055 .help
= "host AIO implementation (threads, native)",
2058 .type
= QEMU_OPT_STRING
,
2059 .help
= "disk format (raw, qcow2, ...)",
2062 .type
= QEMU_OPT_STRING
,
2063 .help
= "disk serial number",
2066 .type
= QEMU_OPT_STRING
,
2067 .help
= "read error action",
2070 .type
= QEMU_OPT_STRING
,
2071 .help
= "write error action",
2074 .type
= QEMU_OPT_STRING
,
2075 .help
= "pci address (virtio only)",
2078 .type
= QEMU_OPT_BOOL
,
2079 .help
= "open drive file as read-only",
2082 .type
= QEMU_OPT_NUMBER
,
2083 .help
= "limit total I/O operations per second",
2086 .type
= QEMU_OPT_NUMBER
,
2087 .help
= "limit read operations per second",
2090 .type
= QEMU_OPT_NUMBER
,
2091 .help
= "limit write operations per second",
2094 .type
= QEMU_OPT_NUMBER
,
2095 .help
= "limit total bytes per second",
2098 .type
= QEMU_OPT_NUMBER
,
2099 .help
= "limit read bytes per second",
2102 .type
= QEMU_OPT_NUMBER
,
2103 .help
= "limit write bytes per second",
2105 .name
= "copy-on-read",
2106 .type
= QEMU_OPT_BOOL
,
2107 .help
= "copy read data from backing file into image file",
2110 .type
= QEMU_OPT_BOOL
,
2111 .help
= "(deprecated, ignored)",
2113 { /* end of list */ }
2117 QemuOptsList qemu_drive_opts
= {
2119 .head
= QTAILQ_HEAD_INITIALIZER(qemu_drive_opts
.head
),
2122 * no elements => accept any params
2123 * validation will happen later
2125 { /* end of list */ }