Blame SOURCES/kvm-block-Fix-permissions-after-bdrv_reopen.patch

4a2fec
From b87d11b7f13dd4725fe801b014d425fa7753b1d0 Mon Sep 17 00:00:00 2001
4a2fec
From: Kevin Wolf <kwolf@redhat.com>
4a2fec
Date: Mon, 4 Dec 2017 12:10:05 +0100
4a2fec
Subject: [PATCH 34/36] block: Fix permissions after bdrv_reopen()
4a2fec
4a2fec
RH-Author: Kevin Wolf <kwolf@redhat.com>
4a2fec
Message-id: <20171204121007.12964-7-kwolf@redhat.com>
4a2fec
Patchwork-id: 78112
4a2fec
O-Subject: [RHV-7.5 qemu-kvm-rhev PATCH v2 6/8] block: Fix permissions after bdrv_reopen()
4a2fec
Bugzilla: 1492178
4a2fec
RH-Acked-by: Fam Zheng <famz@redhat.com>
4a2fec
RH-Acked-by: Max Reitz <mreitz@redhat.com>
4a2fec
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
4a2fec
4a2fec
If we switch between read-only and read-write, the permissions that
4a2fec
image format drivers need on bs->file change, too. Make sure to update
4a2fec
the permissions during bdrv_reopen().
4a2fec
4a2fec
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
4a2fec
Reviewed-by: Eric Blake <eblake@redhat.com>
4a2fec
(cherry picked from commit 3045025991ebeec77ce89c8ec56e83858950bbb3)
4a2fec
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
4a2fec
---
4a2fec
 block.c               | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++
4a2fec
 include/block/block.h |  1 +
4a2fec
 2 files changed, 65 insertions(+)
4a2fec
4a2fec
diff --git a/block.c b/block.c
4a2fec
index 0a7e2c6..bc8b80b 100644
4a2fec
--- a/block.c
4a2fec
+++ b/block.c
4a2fec
@@ -2780,6 +2780,10 @@ static BlockReopenQueue *bdrv_reopen_queue_child(BlockReopenQueue *bs_queue,
4a2fec
     bs_entry->state.explicit_options = explicit_options;
4a2fec
     bs_entry->state.flags = flags;
4a2fec
 
4a2fec
+    /* This needs to be overwritten in bdrv_reopen_prepare() */
4a2fec
+    bs_entry->state.perm = UINT64_MAX;
4a2fec
+    bs_entry->state.shared_perm = 0;
4a2fec
+
4a2fec
     QLIST_FOREACH(child, &bs->children, next) {
4a2fec
         QDict *new_child_options;
4a2fec
         char *child_key_dot;
4a2fec
@@ -2886,6 +2890,52 @@ int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
4a2fec
     return ret;
4a2fec
 }
4a2fec
 
4a2fec
+static BlockReopenQueueEntry *find_parent_in_reopen_queue(BlockReopenQueue *q,
4a2fec
+                                                          BdrvChild *c)
4a2fec
+{
4a2fec
+    BlockReopenQueueEntry *entry;
4a2fec
+
4a2fec
+    QSIMPLEQ_FOREACH(entry, q, entry) {
4a2fec
+        BlockDriverState *bs = entry->state.bs;
4a2fec
+        BdrvChild *child;
4a2fec
+
4a2fec
+        QLIST_FOREACH(child, &bs->children, next) {
4a2fec
+            if (child == c) {
4a2fec
+                return entry;
4a2fec
+            }
4a2fec
+        }
4a2fec
+    }
4a2fec
+
4a2fec
+    return NULL;
4a2fec
+}
4a2fec
+
4a2fec
+static void bdrv_reopen_perm(BlockReopenQueue *q, BlockDriverState *bs,
4a2fec
+                             uint64_t *perm, uint64_t *shared)
4a2fec
+{
4a2fec
+    BdrvChild *c;
4a2fec
+    BlockReopenQueueEntry *parent;
4a2fec
+    uint64_t cumulative_perms = 0;
4a2fec
+    uint64_t cumulative_shared_perms = BLK_PERM_ALL;
4a2fec
+
4a2fec
+    QLIST_FOREACH(c, &bs->parents, next_parent) {
4a2fec
+        parent = find_parent_in_reopen_queue(q, c);
4a2fec
+        if (!parent) {
4a2fec
+            cumulative_perms |= c->perm;
4a2fec
+            cumulative_shared_perms &= c->shared_perm;
4a2fec
+        } else {
4a2fec
+            uint64_t nperm, nshared;
4a2fec
+
4a2fec
+            bdrv_child_perm(parent->state.bs, bs, c, c->role, q,
4a2fec
+                            parent->state.perm, parent->state.shared_perm,
4a2fec
+                            &nperm, &nshared);
4a2fec
+
4a2fec
+            cumulative_perms |= nperm;
4a2fec
+            cumulative_shared_perms &= nshared;
4a2fec
+        }
4a2fec
+    }
4a2fec
+    *perm = cumulative_perms;
4a2fec
+    *shared = cumulative_shared_perms;
4a2fec
+}
4a2fec
 
4a2fec
 /*
4a2fec
  * Prepares a BlockDriverState for reopen. All changes are staged in the
4a2fec
@@ -2951,6 +3001,9 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
4a2fec
         goto error;
4a2fec
     }
4a2fec
 
4a2fec
+    /* Calculate required permissions after reopening */
4a2fec
+    bdrv_reopen_perm(queue, reopen_state->bs,
4a2fec
+                     &reopen_state->perm, &reopen_state->shared_perm);
4a2fec
 
4a2fec
     ret = bdrv_flush(reopen_state->bs);
4a2fec
     if (ret) {
4a2fec
@@ -3006,6 +3059,12 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
4a2fec
         } while ((entry = qdict_next(reopen_state->options, entry)));
4a2fec
     }
4a2fec
 
4a2fec
+    ret = bdrv_check_perm(reopen_state->bs, queue, reopen_state->perm,
4a2fec
+                          reopen_state->shared_perm, NULL, errp);
4a2fec
+    if (ret < 0) {
4a2fec
+        goto error;
4a2fec
+    }
4a2fec
+
4a2fec
     ret = 0;
4a2fec
 
4a2fec
 error:
4a2fec
@@ -3046,6 +3105,9 @@ void bdrv_reopen_commit(BDRVReopenState *reopen_state)
4a2fec
 
4a2fec
     bdrv_refresh_limits(bs, NULL);
4a2fec
 
4a2fec
+    bdrv_set_perm(reopen_state->bs, reopen_state->perm,
4a2fec
+                  reopen_state->shared_perm);
4a2fec
+
4a2fec
     new_can_write =
4a2fec
         !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
4a2fec
     if (!old_can_write && new_can_write && drv->bdrv_reopen_bitmaps_rw) {
4a2fec
@@ -3079,6 +3141,8 @@ void bdrv_reopen_abort(BDRVReopenState *reopen_state)
4a2fec
     }
4a2fec
 
4a2fec
     QDECREF(reopen_state->explicit_options);
4a2fec
+
4a2fec
+    bdrv_abort_perm_update(reopen_state->bs);
4a2fec
 }
4a2fec
 
4a2fec
 
4a2fec
diff --git a/include/block/block.h b/include/block/block.h
4a2fec
index 4d0d2da..59a3077 100644
4a2fec
--- a/include/block/block.h
4a2fec
+++ b/include/block/block.h
4a2fec
@@ -166,6 +166,7 @@ typedef QSIMPLEQ_HEAD(BlockReopenQueue, BlockReopenQueueEntry) BlockReopenQueue;
4a2fec
 typedef struct BDRVReopenState {
4a2fec
     BlockDriverState *bs;
4a2fec
     int flags;
4a2fec
+    uint64_t perm, shared_perm;
4a2fec
     QDict *options;
4a2fec
     QDict *explicit_options;
4a2fec
     void *opaque;
4a2fec
-- 
4a2fec
1.8.3.1
4a2fec