Skip to content

Commit 0ebd24e

Browse files
committed
blockdev: Don't disable COR automatically with blockdev-add
If a read-only device is configured with copy-on-read=on, the old code only prints a warning and automatically disables copy on read. Make it a real error for blockdev-add. Signed-off-by: Kevin Wolf <[email protected]> Reviewed-by: Max Reitz <[email protected]> Reviewed-by: Eric Blake <[email protected]>
1 parent e34ef04 commit 0ebd24e

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

block.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,13 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
778778
}
779779

780780
assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
781-
if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {
782-
bdrv_enable_copy_on_read(bs);
781+
if (flags & BDRV_O_COPY_ON_READ) {
782+
if (!bs->read_only) {
783+
bdrv_enable_copy_on_read(bs);
784+
} else {
785+
error_setg(errp, "Can't use copy-on-read on read-only device");
786+
return -EINVAL;
787+
}
783788
}
784789

785790
if (filename != NULL) {

blockdev.c

+27-4
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,6 @@ static DriveInfo *blockdev_init(QDict *bs_opts,
510510

511511
bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
512512

513-
if (ro && copy_on_read) {
514-
error_report("warning: disabling copy_on_read on read-only drive");
515-
}
516-
517513
QINCREF(bs_opts);
518514
ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error);
519515

@@ -601,6 +597,18 @@ QemuOptsList qemu_legacy_drive_opts = {
601597
.type = QEMU_OPT_STRING,
602598
.help = "pci address (virtio only)",
603599
},
600+
601+
/* Options that are passed on, but have special semantics with -drive */
602+
{
603+
.name = "read-only",
604+
.type = QEMU_OPT_BOOL,
605+
.help = "open drive file as read-only",
606+
},{
607+
.name = "copy-on-read",
608+
.type = QEMU_OPT_BOOL,
609+
.help = "copy read data from backing file into image file",
610+
},
611+
604612
{ /* end of list */ }
605613
},
606614
};
@@ -616,6 +624,7 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
616624
int cyls, heads, secs, translation;
617625
int max_devs, bus_id, unit_id, index;
618626
const char *devaddr;
627+
bool read_only, copy_on_read;
619628
Error *local_err = NULL;
620629

621630
/* Change legacy command line options into QMP ones */
@@ -698,6 +707,20 @@ DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type)
698707
}
699708
}
700709

710+
/* copy-on-read is disabled with a warning for read-only devices */
711+
read_only = qemu_opt_get_bool(legacy_opts, "read-only", false);
712+
copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false);
713+
714+
if (read_only && copy_on_read) {
715+
error_report("warning: disabling copy-on-read on read-only drive");
716+
copy_on_read = false;
717+
}
718+
719+
qdict_put(bs_opts, "read-only",
720+
qstring_from_str(read_only ? "on" : "off"));
721+
qdict_put(bs_opts, "copy-on-read",
722+
qstring_from_str(copy_on_read ? "on" :"off"));
723+
701724
/* Controller type */
702725
value = qemu_opt_get(legacy_opts, "if");
703726
if (value) {

0 commit comments

Comments
 (0)