From 73ac451f3435910433900d7d01a1cac6ccb401e6 Mon Sep 17 00:00:00 2001 From: Kevin Wolf Date: Mon, 14 Mar 2016 15:46:03 +0100 Subject: block: Reject writethrough mode except at the root Writethrough mode is going to become a BlockBackend feature rather than a BDS one, so forbid it in places where we won't be able to support it when the code finally matches the envisioned design. We only allowed setting the cache mode of non-root nodes after the 2.5 release, so we're still free to make this change. The target of block jobs is now always opened in a writeback mode because it doesn't have a BlockBackend attached. This makes more sense anyway because block jobs know when to flush. If the graph is modified on job completion, the original cache mode moves to the new root, so for the guest device writethough always stays enabled if it was configured this way. Signed-off-by: Kevin Wolf Reviewed-by: Eric Blake --- blockdev.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'blockdev.c') diff --git a/blockdev.c b/blockdev.c index 91a21eb..d8e3e31 100644 --- a/blockdev.c +++ b/blockdev.c @@ -1773,6 +1773,12 @@ static void external_snapshot_prepare(BlkActionState *common, flags |= BDRV_O_NO_BACKING; } + /* There is no BB attached during bdrv_open(), so we can't set a + * writethrough mode. bdrv_append() will swap the WCE setting so that the + * backing file becomes unconditionally writeback (which is what backing + * files should always be) and the new overlay gets the original setting. */ + flags |= BDRV_O_CACHE_WB; + assert(state->new_bs == NULL); ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, flags, errp); @@ -2517,6 +2523,7 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, BlockBackend *blk; BlockDriverState *medium_bs = NULL; int bdrv_flags, ret; + bool writethrough; QDict *options = NULL; Error *err = NULL; @@ -2535,6 +2542,12 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_PROTOCOL); + /* Must open the image in writeback mode as long as no BlockBackend is + * attached. The right mode can be set as the final step after changing the + * medium. */ + writethrough = !(bdrv_flags & BDRV_O_CACHE_WB); + bdrv_flags |= BDRV_O_CACHE_WB; + if (!has_read_only) { read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; } @@ -2592,6 +2605,8 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, goto fail; } + bdrv_set_enable_write_cache(medium_bs, !writethrough); + qmp_blockdev_close_tray(device, errp); fail: @@ -3217,7 +3232,7 @@ static void do_drive_backup(const char *device, const char *target, goto out; } - flags = bs->open_flags | BDRV_O_RDWR; + flags = bs->open_flags | BDRV_O_CACHE_WB | BDRV_O_RDWR; /* See if we have a backing HD we can use to create our new image * on top of. */ @@ -3512,7 +3527,7 @@ void qmp_drive_mirror(const char *device, const char *target, format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; } - flags = bs->open_flags | BDRV_O_RDWR; + flags = bs->open_flags | BDRV_O_CACHE_WB | BDRV_O_RDWR; source = backing_bs(bs); if (!source && sync == MIRROR_SYNC_MODE_TOP) { sync = MIRROR_SYNC_MODE_FULL; -- cgit v1.1