[v0,1/2] block: check for read-only on copy-on-read setting

Message ID 20180613154711.12977-2-dplotnikov@virtuozzo.com
State New
Headers show
Series
  • enable/disable copy-on-read via qmp
Related show

Commit Message

Denis Plotnikov June 13, 2018, 3:47 p.m.
We should always check whether block device is in the
read-only state before enabling copy-on-read.
The patch embeds the check in the copy-on-read setter.

Signed-off-by: Denis Plotnikov <dplotnikov@virtuozzo.com>
---
 block.c               |  4 +---
 block/io.c            | 10 ++++++++--
 block/stream.c        |  5 +++--
 include/block/block.h |  2 +-
 4 files changed, 13 insertions(+), 8 deletions(-)

Patch

diff --git a/block.c b/block.c
index 50887087f3..e73fccd397 100644
--- a/block.c
+++ b/block.c
@@ -1370,9 +1370,7 @@  static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
     assert(atomic_read(&bs->copy_on_read) == 0);
 
     if (bs->open_flags & BDRV_O_COPY_ON_READ) {
-        if (!bs->read_only) {
-            bdrv_enable_copy_on_read(bs);
-        } else {
+        if (!bdrv_enable_copy_on_read(bs)) {
             error_setg(errp, "Can't use copy-on-read on read-only device");
             ret = -EINVAL;
             goto fail_opts;
diff --git a/block/io.c b/block/io.c
index b7beaeeb9f..6ec008dbdc 100644
--- a/block/io.c
+++ b/block/io.c
@@ -131,9 +131,15 @@  void bdrv_refresh_limits(BlockDriverState *bs, Error **errp)
  * use the feature without worrying about clobbering its previous state.
  * Copy-on-read stays enabled until all users have called to disable it.
  */
-void bdrv_enable_copy_on_read(BlockDriverState *bs)
+bool bdrv_enable_copy_on_read(BlockDriverState *bs)
 {
-    atomic_inc(&bs->copy_on_read);
+    if (bs->read_only) {
+        return false;
+    } else {
+        atomic_inc(&bs->copy_on_read);
+    }
+
+    return true;
 }
 
 void bdrv_disable_copy_on_read(BlockDriverState *bs)
diff --git a/block/stream.c b/block/stream.c
index 9264b68a1e..033e24f22c 100644
--- a/block/stream.c
+++ b/block/stream.c
@@ -130,8 +130,9 @@  static void coroutine_fn stream_run(void *opaque)
      * backing chain since the copy-on-read operation does not take base into
      * account.
      */
-    if (!base) {
-        bdrv_enable_copy_on_read(bs);
+    if (!base && !bdrv_enable_copy_on_read(bs)) {
+        ret = -EPERM;
+        goto out;
     }
 
     for ( ; offset < len; offset += n) {
diff --git a/include/block/block.h b/include/block/block.h
index e677080c4e..dddfd032f9 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -490,7 +490,7 @@  void *qemu_try_blockalign(BlockDriverState *bs, size_t size);
 void *qemu_try_blockalign0(BlockDriverState *bs, size_t size);
 bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov);
 
-void bdrv_enable_copy_on_read(BlockDriverState *bs);
+bool bdrv_enable_copy_on_read(BlockDriverState *bs);
 void bdrv_disable_copy_on_read(BlockDriverState *bs);
 
 void bdrv_ref(BlockDriverState *bs);