Blame SOURCES/kvm-file-posix-Make-auto-read-only-dynamic.patch

7711c0
From ee549d8b1c8cd482bb84d49e7535e174fd89b9ea Mon Sep 17 00:00:00 2001
7711c0
From: Kevin Wolf <kwolf@redhat.com>
7711c0
Date: Fri, 15 Mar 2019 18:10:10 +0100
7711c0
Subject: [PATCH 014/163] file-posix: Make auto-read-only dynamic
7711c0
7711c0
RH-Author: Kevin Wolf <kwolf@redhat.com>
7711c0
Message-id: <20190315181010.14964-15-kwolf@redhat.com>
7711c0
Patchwork-id: 84891
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 14/14] file-posix: Make auto-read-only dynamic
7711c0
Bugzilla: 1685989
7711c0
RH-Acked-by: John Snow <jsnow@redhat.com>
7711c0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
7711c0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
7711c0
Until now, with auto-read-only=on we tried to open the file read-write
7711c0
first and if that failed, read-only was tried. This is actually not good
7711c0
enough for libvirt, which gives QEMU SELinux permissions for read-write
7711c0
only as soon as it actually intends to write to the image. So we need to
7711c0
be able to switch between read-only and read-write at runtime.
7711c0
7711c0
This patch makes auto-read-only dynamic, i.e. the file is opened
7711c0
read-only as long as no user of the node has requested write
7711c0
permissions, but it is automatically reopened read-write as soon as the
7711c0
first writer is attached. Conversely, if the last writer goes away, the
7711c0
file is reopened read-only again.
7711c0
7711c0
bs->read_only is no longer set for auto-read-only=on files even if the
7711c0
file descriptor is opened read-only because it will be transparently
7711c0
upgraded as soon as a writer is attached. This changes the output of
7711c0
qemu-iotests 232.
7711c0
7711c0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7711c0
(cherry picked from commit 23dece19da41724349809873923e20a48b619cb7)
7711c0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 block/file-posix.c         | 36 +++++++++++++++++-------------------
7711c0
 tests/qemu-iotests/232.out | 12 ++++++------
7711c0
 2 files changed, 23 insertions(+), 25 deletions(-)
7711c0
7711c0
diff --git a/block/file-posix.c b/block/file-posix.c
7711c0
index f0f8eaf..0cf7261 100644
7711c0
--- a/block/file-posix.c
7711c0
+++ b/block/file-posix.c
7711c0
@@ -382,13 +382,21 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
7711c0
     }
7711c0
 }
7711c0
 
7711c0
-static void raw_parse_flags(int bdrv_flags, int *open_flags)
7711c0
+static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers)
7711c0
 {
7711c0
+    bool read_write = false;
7711c0
     assert(open_flags != NULL);
7711c0
 
7711c0
     *open_flags |= O_BINARY;
7711c0
     *open_flags &= ~O_ACCMODE;
7711c0
-    if (bdrv_flags & BDRV_O_RDWR) {
7711c0
+
7711c0
+    if (bdrv_flags & BDRV_O_AUTO_RDONLY) {
7711c0
+        read_write = has_writers;
7711c0
+    } else if (bdrv_flags & BDRV_O_RDWR) {
7711c0
+        read_write = true;
7711c0
+    }
7711c0
+
7711c0
+    if (read_write) {
7711c0
         *open_flags |= O_RDWR;
7711c0
     } else {
7711c0
         *open_flags |= O_RDONLY;
7711c0
@@ -516,24 +524,12 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
7711c0
     }
7711c0
 
7711c0
     s->open_flags = open_flags;
7711c0
-    raw_parse_flags(bdrv_flags, &s->open_flags);
7711c0
+    raw_parse_flags(bdrv_flags, &s->open_flags, false);
7711c0
 
7711c0
     s->fd = -1;
7711c0
     fd = qemu_open(filename, s->open_flags, 0644);
7711c0
     ret = fd < 0 ? -errno : 0;
7711c0
 
7711c0
-    if (ret == -EACCES || ret == -EROFS) {
7711c0
-        /* Try to degrade to read-only, but if it doesn't work, still use the
7711c0
-         * normal error message. */
7711c0
-        if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) {
7711c0
-            bdrv_flags &= ~BDRV_O_RDWR;
7711c0
-            raw_parse_flags(bdrv_flags, &s->open_flags);
7711c0
-            assert(!(s->open_flags & O_CREAT));
7711c0
-            fd = qemu_open(filename, s->open_flags);
7711c0
-            ret = fd < 0 ? -errno : 0;
7711c0
-        }
7711c0
-    }
7711c0
-
7711c0
     if (ret < 0) {
7711c0
         error_setg_errno(errp, -ret, "Could not open '%s'", filename);
7711c0
         if (ret == -EROFS) {
7711c0
@@ -838,12 +834,14 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
7711c0
 }
7711c0
 
7711c0
 static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
7711c0
-                                 int *open_flags, bool force_dup,
7711c0
+                                 int *open_flags, uint64_t perm, bool force_dup,
7711c0
                                  Error **errp)
7711c0
 {
7711c0
     BDRVRawState *s = bs->opaque;
7711c0
     int fd = -1;
7711c0
     int ret;
7711c0
+    bool has_writers = perm &
7711c0
+        (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED | BLK_PERM_RESIZE);
7711c0
     int fcntl_flags = O_APPEND | O_NONBLOCK;
7711c0
 #ifdef O_NOATIME
7711c0
     fcntl_flags |= O_NOATIME;
7711c0
@@ -854,7 +852,7 @@ static int raw_reconfigure_getfd(BlockDriverState *bs, int flags,
7711c0
         *open_flags |= O_NONBLOCK;
7711c0
     }
7711c0
 
7711c0
-    raw_parse_flags(flags, open_flags);
7711c0
+    raw_parse_flags(flags, open_flags, has_writers);
7711c0
 
7711c0
 #ifdef O_ASYNC
7711c0
     /* Not all operating systems have O_ASYNC, and those that don't
7711c0
@@ -916,7 +914,7 @@ static int raw_reopen_prepare(BDRVReopenState *state,
7711c0
     rs = state->opaque;
7711c0
 
7711c0
     rs->fd = raw_reconfigure_getfd(state->bs, state->flags, &rs->open_flags,
7711c0
-                                   true, &local_err);
7711c0
+                                   state->perm, true, &local_err);
7711c0
     if (local_err) {
7711c0
         error_propagate(errp, local_err);
7711c0
         ret = -1;
7711c0
@@ -2548,7 +2546,7 @@ static int raw_check_perm(BlockDriverState *bs, uint64_t perm, uint64_t shared,
7711c0
         s->perm_change_fd = rs->fd;
7711c0
     } else {
7711c0
         /* We may need a new fd if auto-read-only switches the mode */
7711c0
-        ret = raw_reconfigure_getfd(bs, bs->open_flags, &open_flags,
7711c0
+        ret = raw_reconfigure_getfd(bs, bs->open_flags, &open_flags, perm,
7711c0
                                     false, errp);
7711c0
         if (ret < 0) {
7711c0
             return ret;
7711c0
diff --git a/tests/qemu-iotests/232.out b/tests/qemu-iotests/232.out
7711c0
index dcb683a..3bd1a92 100644
7711c0
--- a/tests/qemu-iotests/232.out
7711c0
+++ b/tests/qemu-iotests/232.out
7711c0
@@ -22,12 +22,12 @@ NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
 NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
 
7711c0
 QEMU_PROG: -drive driver=file,file=TEST_DIR/t.IMGFMT,if=none,read-only=off,auto-read-only=off: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
-NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
-NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
+NODE_NAME: TEST_DIR/t.IMGFMT (file)
7711c0
+NODE_NAME: TEST_DIR/t.IMGFMT (file)
7711c0
 
7711c0
 QEMU_PROG: -drive driver=file,file=TEST_DIR/t.IMGFMT,if=none,auto-read-only=off: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
-NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
-NODE_NAME: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
+NODE_NAME: TEST_DIR/t.IMGFMT (file)
7711c0
+NODE_NAME: TEST_DIR/t.IMGFMT (file)
7711c0
 
7711c0
 === -blockdev with read-write image: read-only/auto-read-only combinations ===
7711c0
 
7711c0
@@ -50,10 +50,10 @@ node0: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
 node0: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
 
7711c0
 QEMU_PROG: -blockdev driver=file,filename=TEST_DIR/t.IMGFMT,node-name=node0,read-only=off,auto-read-only=off: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
-node0: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
+node0: TEST_DIR/t.IMGFMT (file)
7711c0
 QEMU_PROG: -blockdev driver=file,filename=TEST_DIR/t.IMGFMT,node-name=node0,read-only=off: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
 
7711c0
 QEMU_PROG: -blockdev driver=file,filename=TEST_DIR/t.IMGFMT,node-name=node0,auto-read-only=off: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
-node0: TEST_DIR/t.IMGFMT (file, read-only)
7711c0
+node0: TEST_DIR/t.IMGFMT (file)
7711c0
 QEMU_PROG: -blockdev driver=file,filename=TEST_DIR/t.IMGFMT,node-name=node0: Could not open 'TEST_DIR/t.IMGFMT': Permission denied
7711c0
 *** done
7711c0
-- 
7711c0
1.8.3.1
7711c0