qemu-io: Use bdrv_parse_cache_mode() in reopen_f()
authorKevin Wolf <kwolf@redhat.com>
Fri, 18 Mar 2016 14:36:31 +0000 (15:36 +0100)
committerKevin Wolf <kwolf@redhat.com>
Wed, 30 Mar 2016 10:16:03 +0000 (12:16 +0200)
We must forbid changing the WCE flag in bdrv_reopen() in the same patch,
as otherwise the behaviour would change so that the flag takes
precedence over the explicitly specified option.

The correct value of the WCE flag depends on the BlockBackend user (e.g.
guest device) and isn't a decision that the QMP client makes, so this
change is what we want.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
block.c
qemu-io-cmds.c
tests/qemu-iotests/142
tests/qemu-iotests/142.out

diff --git a/block.c b/block.c
index ca7e7a0b4525382e366e6a3a2d8b403850217b4f..4e27c49a753f3c6540a1cb96c387c14888e695a9 100644 (file)
--- a/block.c
+++ b/block.c
@@ -2028,18 +2028,12 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
 
     update_flags_from_options(&reopen_state->flags, opts);
 
-    /* If a guest device is attached, it owns WCE */
-    if (reopen_state->bs->blk && blk_get_attached_dev(reopen_state->bs->blk)) {
-        bool old_wce = bdrv_enable_write_cache(reopen_state->bs);
-        bool new_wce = (reopen_state->flags & BDRV_O_CACHE_WB);
-        if (old_wce != new_wce) {
-            error_setg(errp, "Cannot change cache.writeback: Device attached");
-            ret = -EINVAL;
-            goto error;
-        }
-    }
-    if (!reopen_state->bs->blk && !(reopen_state->flags & BDRV_O_CACHE_WB)) {
-        error_setg(errp, "Cannot disable cache.writeback: No BlockBackend");
+    /* WCE is a BlockBackend level option, can't change it */
+    bool old_wce = bdrv_enable_write_cache(reopen_state->bs);
+    bool new_wce = (reopen_state->flags & BDRV_O_CACHE_WB);
+
+    if (old_wce != new_wce) {
+        error_setg(errp, "Cannot change cache.writeback");
         ret = -EINVAL;
         goto error;
     }
index 139f7ebcbbd55f08ea802756642f0fd9bb8e223b..932e367a176051d7128896cdef7871631e89ecac 100644 (file)
@@ -2106,6 +2106,7 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
     QDict *opts;
     int c;
     int flags = bs->open_flags;
+    bool writethrough = !blk_enable_write_cache(blk);
 
     BlockReopenQueue *brq;
     Error *local_err = NULL;
@@ -2113,7 +2114,7 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
     while ((c = getopt(argc, argv, "c:o:r")) != -1) {
         switch (c) {
         case 'c':
-            if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
+            if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
                 error_report("Invalid cache option: %s", optarg);
                 return 0;
             }
@@ -2138,14 +2139,25 @@ static int reopen_f(BlockBackend *blk, int argc, char **argv)
         return qemuio_command_usage(&reopen_cmd);
     }
 
+    if (writethrough != blk_enable_write_cache(blk) &&
+        blk_get_attached_dev(blk))
+    {
+        error_report("Cannot change cache.writeback: Device attached");
+        qemu_opts_reset(&reopen_opts);
+        return 0;
+    }
+
     qopts = qemu_opts_find(&reopen_opts, NULL);
     opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
     qemu_opts_reset(&reopen_opts);
 
+    flags |= blk_enable_write_cache(blk) ? BDRV_O_CACHE_WB : 0;
     brq = bdrv_reopen_queue(NULL, bs, opts, flags);
     bdrv_reopen_multiple(brq, &local_err);
     if (local_err) {
         error_report_err(local_err);
+    } else {
+        blk_set_enable_write_cache(blk, !writethrough);
     }
 
     return 0;
index 8bbbfde3a15876720f833926436478c4ba20a994..a03574790453abc59a91385f8905d30eff18660f 100755 (executable)
@@ -216,7 +216,7 @@ echo
 # BDS initialised with the json: pseudo-protocol, but still have it inherit
 # options from its parent node.
 
-hmp_cmds="qemu-io none0 \"reopen -o cache.writeback=off,cache.direct=on,cache.no-flush=on\"
+hmp_cmds="qemu-io none0 \"reopen -o cache.direct=on,cache.no-flush=on\"
 info block none0
 info block image
 info block blkdebug
index c9224909b5bf618938a2afd6f96cbb38ef9aed24..3d5ef5fe8d26ae0538817c147bc9f012976983d5 100644 (file)
@@ -414,7 +414,7 @@ cache.no-flush=on on backing-file
 
 --- Change cache mode in parent, child has explicit option in JSON ---
 
-    Cache mode:       writethrough, direct, ignore flushes
+    Cache mode:       writeback, direct, ignore flushes
     Cache mode:       writeback, direct, ignore flushes
     Cache mode:       writeback, direct, ignore flushes
     Cache mode:       writeback, ignore flushes