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

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