yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-block-Require-auto-read-only-for-existing-fallbacks.patch

ae23c9
From 60065cb41272144762ceb38e9bcd1a203c29c064 Mon Sep 17 00:00:00 2001
ae23c9
From: Kevin Wolf <kwolf@redhat.com>
ae23c9
Date: Thu, 10 Jan 2019 12:44:34 +0000
ae23c9
Subject: [PATCH 04/14] block: Require auto-read-only for existing fallbacks
ae23c9
ae23c9
RH-Author: Kevin Wolf <kwolf@redhat.com>
ae23c9
Message-id: <20190110124442.30132-5-kwolf@redhat.com>
ae23c9
Patchwork-id: 83954
ae23c9
O-Subject: [RHEL-8.0 qemu-kvm PATCH 04/12] block: Require auto-read-only for existing fallbacks
ae23c9
Bugzilla: 1644996
ae23c9
RH-Acked-by: Max Reitz <mreitz@redhat.com>
ae23c9
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
RH-Acked-by: Eric Blake <eblake@redhat.com>
ae23c9
ae23c9
Some block drivers have traditionally changed their node to read-only
ae23c9
mode without asking the user. This behaviour has been marked deprecated
ae23c9
since 2.11, expecting users to provide an explicit read-only=on option.
ae23c9
ae23c9
Now that we have auto-read-only=on, enable these drivers to make use of
ae23c9
the option.
ae23c9
ae23c9
This is the only use of bdrv_set_read_only(), so we can make it a bit
ae23c9
more specific and turn it into a bdrv_apply_auto_read_only() that is
ae23c9
more convenient for drivers to use.
ae23c9
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Reviewed-by: Eric Blake <eblake@redhat.com>
ae23c9
(cherry picked from commit eaa2410f1ea864609090c0a5fda9e0ce9499da79)
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 block.c               | 42 +++++++++++++++++++++++++++---------------
ae23c9
 block/bochs.c         | 17 ++++++-----------
ae23c9
 block/cloop.c         | 16 +++++-----------
ae23c9
 block/dmg.c           | 16 +++++-----------
ae23c9
 block/rbd.c           | 15 ++++-----------
ae23c9
 block/vvfat.c         | 10 ++--------
ae23c9
 include/block/block.h |  3 ++-
ae23c9
 7 files changed, 51 insertions(+), 68 deletions(-)
ae23c9
ae23c9
diff --git a/block.c b/block.c
ae23c9
index f357975..268debe 100644
ae23c9
--- a/block.c
ae23c9
+++ b/block.c
ae23c9
@@ -266,29 +266,41 @@ int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
ae23c9
     return 0;
ae23c9
 }
ae23c9
 
ae23c9
-/* TODO Remove (deprecated since 2.11)
ae23c9
- * Block drivers are not supposed to automatically change bs->read_only.
ae23c9
- * Instead, they should just check whether they can provide what the user
ae23c9
- * explicitly requested and error out if read-write is requested, but they can
ae23c9
- * only provide read-only access. */
ae23c9
-int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp)
ae23c9
+/*
ae23c9
+ * Called by a driver that can only provide a read-only image.
ae23c9
+ *
ae23c9
+ * Returns 0 if the node is already read-only or it could switch the node to
ae23c9
+ * read-only because BDRV_O_AUTO_RDONLY is set.
ae23c9
+ *
ae23c9
+ * Returns -EACCES if the node is read-write and BDRV_O_AUTO_RDONLY is not set
ae23c9
+ * or bdrv_can_set_read_only() forbids making the node read-only. If @errmsg
ae23c9
+ * is not NULL, it is used as the error message for the Error object.
ae23c9
+ */
ae23c9
+int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
ae23c9
+                              Error **errp)
ae23c9
 {
ae23c9
     int ret = 0;
ae23c9
 
ae23c9
-    ret = bdrv_can_set_read_only(bs, read_only, false, errp);
ae23c9
-    if (ret < 0) {
ae23c9
-        return ret;
ae23c9
+    if (!(bs->open_flags & BDRV_O_RDWR)) {
ae23c9
+        return 0;
ae23c9
+    }
ae23c9
+    if (!(bs->open_flags & BDRV_O_AUTO_RDONLY)) {
ae23c9
+        goto fail;
ae23c9
     }
ae23c9
 
ae23c9
-    bs->read_only = read_only;
ae23c9
-
ae23c9
-    if (read_only) {
ae23c9
-        bs->open_flags &= ~BDRV_O_RDWR;
ae23c9
-    } else {
ae23c9
-        bs->open_flags |= BDRV_O_RDWR;
ae23c9
+    ret = bdrv_can_set_read_only(bs, true, false, NULL);
ae23c9
+    if (ret < 0) {
ae23c9
+        goto fail;
ae23c9
     }
ae23c9
 
ae23c9
+    bs->read_only = true;
ae23c9
+    bs->open_flags &= ~BDRV_O_RDWR;
ae23c9
+
ae23c9
     return 0;
ae23c9
+
ae23c9
+fail:
ae23c9
+    error_setg(errp, "%s", errmsg ?: "Image is read-only");
ae23c9
+    return -EACCES;
ae23c9
 }
ae23c9
 
ae23c9
 void bdrv_get_full_backing_filename_from_filename(const char *backed,
ae23c9
diff --git a/block/bochs.c b/block/bochs.c
ae23c9
index 50c6300..22e7d44 100644
ae23c9
--- a/block/bochs.c
ae23c9
+++ b/block/bochs.c
ae23c9
@@ -105,23 +105,18 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
ae23c9
     struct bochs_header bochs;
ae23c9
     int ret;
ae23c9
 
ae23c9
+    /* No write support yet */
ae23c9
+    ret = bdrv_apply_auto_read_only(bs, NULL, errp);
ae23c9
+    if (ret < 0) {
ae23c9
+        return ret;
ae23c9
+    }
ae23c9
+
ae23c9
     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
ae23c9
                                false, errp);
ae23c9
     if (!bs->file) {
ae23c9
         return -EINVAL;
ae23c9
     }
ae23c9
 
ae23c9
-    if (!bdrv_is_read_only(bs)) {
ae23c9
-        error_report("Opening bochs images without an explicit read-only=on "
ae23c9
-                     "option is deprecated. Future versions will refuse to "
ae23c9
-                     "open the image instead of automatically marking the "
ae23c9
-                     "image read-only.");
ae23c9
-        ret = bdrv_set_read_only(bs, true, errp); /* no write support yet */
ae23c9
-        if (ret < 0) {
ae23c9
-            return ret;
ae23c9
-        }
ae23c9
-    }
ae23c9
-
ae23c9
     ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));
ae23c9
     if (ret < 0) {
ae23c9
         return ret;
ae23c9
diff --git a/block/cloop.c b/block/cloop.c
ae23c9
index 2be6898..df2b85f 100644
ae23c9
--- a/block/cloop.c
ae23c9
+++ b/block/cloop.c
ae23c9
@@ -67,23 +67,17 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
ae23c9
     uint32_t offsets_size, max_compressed_block_size = 1, i;
ae23c9
     int ret;
ae23c9
 
ae23c9
+    ret = bdrv_apply_auto_read_only(bs, NULL, errp);
ae23c9
+    if (ret < 0) {
ae23c9
+        return ret;
ae23c9
+    }
ae23c9
+
ae23c9
     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
ae23c9
                                false, errp);
ae23c9
     if (!bs->file) {
ae23c9
         return -EINVAL;
ae23c9
     }
ae23c9
 
ae23c9
-    if (!bdrv_is_read_only(bs)) {
ae23c9
-        error_report("Opening cloop images without an explicit read-only=on "
ae23c9
-                     "option is deprecated. Future versions will refuse to "
ae23c9
-                     "open the image instead of automatically marking the "
ae23c9
-                     "image read-only.");
ae23c9
-        ret = bdrv_set_read_only(bs, true, errp);
ae23c9
-        if (ret < 0) {
ae23c9
-            return ret;
ae23c9
-        }
ae23c9
-    }
ae23c9
-
ae23c9
     /* read header */
ae23c9
     ret = bdrv_pread(bs->file, 128, &s->block_size, 4);
ae23c9
     if (ret < 0) {
ae23c9
diff --git a/block/dmg.c b/block/dmg.c
ae23c9
index c9b3c51..1d9283b 100644
ae23c9
--- a/block/dmg.c
ae23c9
+++ b/block/dmg.c
ae23c9
@@ -413,23 +413,17 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
ae23c9
     int64_t offset;
ae23c9
     int ret;
ae23c9
 
ae23c9
+    ret = bdrv_apply_auto_read_only(bs, NULL, errp);
ae23c9
+    if (ret < 0) {
ae23c9
+        return ret;
ae23c9
+    }
ae23c9
+
ae23c9
     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
ae23c9
                                false, errp);
ae23c9
     if (!bs->file) {
ae23c9
         return -EINVAL;
ae23c9
     }
ae23c9
 
ae23c9
-    if (!bdrv_is_read_only(bs)) {
ae23c9
-        error_report("Opening dmg images without an explicit read-only=on "
ae23c9
-                     "option is deprecated. Future versions will refuse to "
ae23c9
-                     "open the image instead of automatically marking the "
ae23c9
-                     "image read-only.");
ae23c9
-        ret = bdrv_set_read_only(bs, true, errp);
ae23c9
-        if (ret < 0) {
ae23c9
-            return ret;
ae23c9
-        }
ae23c9
-    }
ae23c9
-
ae23c9
     block_module_load_one("dmg-bz2");
ae23c9
 
ae23c9
     s->n_chunks = 0;
ae23c9
diff --git a/block/rbd.c b/block/rbd.c
ae23c9
index dc369d0..8d74a29 100644
ae23c9
--- a/block/rbd.c
ae23c9
+++ b/block/rbd.c
ae23c9
@@ -773,17 +773,10 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
ae23c9
     /* If we are using an rbd snapshot, we must be r/o, otherwise
ae23c9
      * leave as-is */
ae23c9
     if (s->snap != NULL) {
ae23c9
-        if (!bdrv_is_read_only(bs)) {
ae23c9
-            error_report("Opening rbd snapshots without an explicit "
ae23c9
-                         "read-only=on option is deprecated. Future versions "
ae23c9
-                         "will refuse to open the image instead of "
ae23c9
-                         "automatically marking the image read-only.");
ae23c9
-            r = bdrv_set_read_only(bs, true, &local_err);
ae23c9
-            if (r < 0) {
ae23c9
-                rbd_close(s->image);
ae23c9
-                error_propagate(errp, local_err);
ae23c9
-                goto failed_open;
ae23c9
-            }
ae23c9
+        r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
ae23c9
+        if (r < 0) {
ae23c9
+            rbd_close(s->image);
ae23c9
+            goto failed_open;
ae23c9
         }
ae23c9
     }
ae23c9
 
ae23c9
diff --git a/block/vvfat.c b/block/vvfat.c
ae23c9
index 3efce9e..a5a3fb9 100644
ae23c9
--- a/block/vvfat.c
ae23c9
+++ b/block/vvfat.c
ae23c9
@@ -1262,15 +1262,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
ae23c9
                        "Unable to set VVFAT to 'rw' when drive is read-only");
ae23c9
             goto fail;
ae23c9
         }
ae23c9
-    } else  if (!bdrv_is_read_only(bs)) {
ae23c9
-        error_report("Opening non-rw vvfat images without an explicit "
ae23c9
-                     "read-only=on option is deprecated. Future versions "
ae23c9
-                     "will refuse to open the image instead of "
ae23c9
-                     "automatically marking the image read-only.");
ae23c9
-        /* read only is the default for safety */
ae23c9
-        ret = bdrv_set_read_only(bs, true, &local_err);
ae23c9
+    } else {
ae23c9
+        ret = bdrv_apply_auto_read_only(bs, NULL, errp);
ae23c9
         if (ret < 0) {
ae23c9
-            error_propagate(errp, local_err);
ae23c9
             goto fail;
ae23c9
         }
ae23c9
     }
ae23c9
diff --git a/include/block/block.h b/include/block/block.h
ae23c9
index 6ee8b2a..36a702c 100644
ae23c9
--- a/include/block/block.h
ae23c9
+++ b/include/block/block.h
ae23c9
@@ -433,7 +433,8 @@ int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
ae23c9
 bool bdrv_is_read_only(BlockDriverState *bs);
ae23c9
 int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
ae23c9
                            bool ignore_allow_rdw, Error **errp);
ae23c9
-int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp);
ae23c9
+int bdrv_apply_auto_read_only(BlockDriverState *bs, const char *errmsg,
ae23c9
+                              Error **errp);
ae23c9
 bool bdrv_is_writable(BlockDriverState *bs);
ae23c9
 bool bdrv_is_sg(BlockDriverState *bs);
ae23c9
 bool bdrv_is_inserted(BlockDriverState *bs);
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9