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

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