qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com, jcody@redhat.com,
	famz@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH v2 04/43] block: Involve block drivers in permission granting
Date: Mon, 27 Feb 2017 21:09:05 +0100	[thread overview]
Message-ID: <1488226184-9044-5-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1488226184-9044-1-git-send-email-kwolf@redhat.com>

In many cases, the required permissions of one node on its children
depend on what its parents require from it. For example, the raw format
or most filter drivers only need to request consistent reads if that's
something that one of their parents wants.

In order to achieve this, this patch introduces two new BlockDriver
callbacks. The first one lets drivers first check (recursively) whether
the requested permissions can be set; the second one actually sets the
new permission bitmask.

Also add helper functions that drivers can use in their implementation
of the callbacks to update their permissions on a specific child.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c                   | 177 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block_int.h |  61 ++++++++++++++++
 2 files changed, 238 insertions(+)

diff --git a/block.c b/block.c
index 9628c7a..803a688 100644
--- a/block.c
+++ b/block.c
@@ -1326,11 +1326,145 @@ static int bdrv_fill_options(QDict **options, const char *filename,
     return 0;
 }
 
+/*
+ * Check whether permissions on this node can be changed in a way that
+ * @cumulative_perms and @cumulative_shared_perms are the new cumulative
+ * permissions of all its parents. This involves checking whether all necessary
+ * permission changes to child nodes can be performed.
+ *
+ * A call to this function must always be followed by a call to bdrv_set_perm()
+ * or bdrv_abort_perm_update().
+ */
+static int bdrv_check_perm(BlockDriverState *bs, uint64_t cumulative_perms,
+                           uint64_t cumulative_shared_perms, Error **errp)
+{
+    BlockDriver *drv = bs->drv;
+    BdrvChild *c;
+    int ret;
+
+    if (!drv) {
+        error_setg(errp, "Block node is not opened");
+        return -EINVAL;
+    }
+
+    /* Write permissions never work with read-only images */
+    if ((cumulative_perms & (BLK_PERM_WRITE | BLK_PERM_WRITE_UNCHANGED)) &&
+        bdrv_is_read_only(bs))
+    {
+        error_setg(errp, "Block node is read-only");
+        return -EPERM;
+    }
+
+    /* Check this node */
+    if (drv->bdrv_check_perm) {
+        return drv->bdrv_check_perm(bs, cumulative_perms,
+                                    cumulative_shared_perms, errp);
+    }
+
+    /* Drivers may not have .bdrv_child_perm() */
+    if (!drv->bdrv_child_perm) {
+        return 0;
+    }
+
+    /* Check all children */
+    QLIST_FOREACH(c, &bs->children, next) {
+        uint64_t cur_perm, cur_shared;
+        drv->bdrv_child_perm(bs, c, c->role,
+                             cumulative_perms, cumulative_shared_perms,
+                             &cur_perm, &cur_shared);
+        ret = bdrv_child_check_perm(c, cur_perm, cur_shared, errp);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    return 0;
+}
+
+/*
+ * Notifies drivers that after a previous bdrv_check_perm() call, the
+ * permission update is not performed and any preparations made for it (e.g.
+ * taken file locks) need to be undone.
+ *
+ * This function recursively notifies all child nodes.
+ */
+static void bdrv_abort_perm_update(BlockDriverState *bs)
+{
+    BlockDriver *drv = bs->drv;
+    BdrvChild *c;
+
+    if (!drv) {
+        return;
+    }
+
+    if (drv->bdrv_abort_perm_update) {
+        drv->bdrv_abort_perm_update(bs);
+    }
+
+    QLIST_FOREACH(c, &bs->children, next) {
+        bdrv_child_abort_perm_update(c);
+    }
+}
+
+static void bdrv_set_perm(BlockDriverState *bs, uint64_t cumulative_perms,
+                          uint64_t cumulative_shared_perms)
+{
+    BlockDriver *drv = bs->drv;
+    BdrvChild *c;
+
+    if (!drv) {
+        return;
+    }
+
+    /* Update this node */
+    if (drv->bdrv_set_perm) {
+        drv->bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
+    }
+
+    /* Drivers may not have .bdrv_child_perm() */
+    if (!drv->bdrv_child_perm) {
+        return;
+    }
+
+    /* Update all children */
+    QLIST_FOREACH(c, &bs->children, next) {
+        uint64_t cur_perm, cur_shared;
+        drv->bdrv_child_perm(bs, c, c->role,
+                             cumulative_perms, cumulative_shared_perms,
+                             &cur_perm, &cur_shared);
+        bdrv_child_set_perm(c, cur_perm, cur_shared);
+    }
+}
+
+static void bdrv_update_perm(BlockDriverState *bs)
+{
+    BdrvChild *c;
+    uint64_t cumulative_perms = 0;
+    uint64_t cumulative_shared_perms = BLK_PERM_ALL;
+
+    QLIST_FOREACH(c, &bs->parents, next_parent) {
+        cumulative_perms |= c->perm;
+        cumulative_shared_perms &= c->shared_perm;
+    }
+
+    bdrv_set_perm(bs, cumulative_perms, cumulative_shared_perms);
+}
+
+/*
+ * Checks whether a new reference to @bs can be added if the new user requires
+ * @new_used_perm/@new_shared_perm as its permissions. If @ignore_child is set,
+ * this old reference is ignored in the calculations; this allows checking
+ * permission updates for an existing reference.
+ *
+ * Needs to be followed by a call to either bdrv_set_perm() or
+ * bdrv_abort_perm_update(). */
 static int bdrv_check_update_perm(BlockDriverState *bs, uint64_t new_used_perm,
                                   uint64_t new_shared_perm,
                                   BdrvChild *ignore_child, Error **errp)
 {
     BdrvChild *c;
+    uint64_t cumulative_perms = new_used_perm;
+    uint64_t cumulative_shared_perms = new_shared_perm;
 
     /* There is no reason why anyone couldn't tolerate write_unchanged */
     assert(new_shared_perm & BLK_PERM_WRITE_UNCHANGED);
@@ -1353,8 +1487,47 @@ static int bdrv_check_update_perm(BlockDriverState *bs, uint64_t new_used_perm,
             error_setg(errp, "Conflicts with %s", user ?: "another operation");
             return -EPERM;
         }
+
+        cumulative_perms |= c->perm;
+        cumulative_shared_perms &= c->shared_perm;
+    }
+
+    return bdrv_check_perm(bs, cumulative_perms, cumulative_shared_perms, errp);
+}
+
+/* Needs to be followed by a call to either bdrv_child_set_perm() or
+ * bdrv_child_abort_perm_update(). */
+int bdrv_child_check_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
+                          Error **errp)
+{
+    return bdrv_check_update_perm(c->bs, perm, shared, c, errp);
+}
+
+void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared)
+{
+    c->perm = perm;
+    c->shared_perm = shared;
+    bdrv_update_perm(c->bs);
+}
+
+void bdrv_child_abort_perm_update(BdrvChild *c)
+{
+    bdrv_abort_perm_update(c->bs);
+}
+
+int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
+                            Error **errp)
+{
+    int ret;
+
+    ret = bdrv_child_check_perm(c, perm, shared, errp);
+    if (ret < 0) {
+        bdrv_child_abort_perm_update(c);
+        return ret;
     }
 
+    bdrv_child_set_perm(c, perm, shared);
+
     return 0;
 }
 
@@ -1367,6 +1540,7 @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
             child->role->drained_end(child);
         }
         QLIST_REMOVE(child, next_parent);
+        bdrv_update_perm(old_bs);
     }
 
     child->bs = new_bs;
@@ -1376,6 +1550,7 @@ static void bdrv_replace_child(BdrvChild *child, BlockDriverState *new_bs)
         if (new_bs->quiesce_counter && child->role->drained_begin) {
             child->role->drained_begin(child);
         }
+        bdrv_update_perm(new_bs);
     }
 }
 
@@ -1390,6 +1565,7 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
 
     ret = bdrv_check_update_perm(child_bs, perm, shared_perm, NULL, errp);
     if (ret < 0) {
+        bdrv_abort_perm_update(child_bs);
         return NULL;
     }
 
@@ -1403,6 +1579,7 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
         .opaque         = opaque,
     };
 
+    /* This performs the matching bdrv_set_perm() for the above check. */
     bdrv_replace_child(child, child_bs);
 
     return child;
diff --git a/include/block/block_int.h b/include/block/block_int.h
index ed63bad..cef2b6e 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -320,6 +320,59 @@ struct BlockDriver {
     void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child,
                            Error **errp);
 
+    /**
+     * Informs the block driver that a permission change is intended. The
+     * driver checks whether the change is permissible and may take other
+     * preparations for the change (e.g. get file system locks). This operation
+     * is always followed either by a call to either .bdrv_set_perm or
+     * .bdrv_abort_perm_update.
+     *
+     * Checks whether the requested set of cumulative permissions in @perm
+     * can be granted for accessing @bs and whether no other users are using
+     * permissions other than those given in @shared (both arguments take
+     * BLK_PERM_* bitmasks).
+     *
+     * If both conditions are met, 0 is returned. Otherwise, -errno is returned
+     * and errp is set to an error describing the conflict.
+     */
+    int (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
+                           uint64_t shared, Error **errp);
+
+    /**
+     * Called to inform the driver that the set of cumulative set of used
+     * permissions for @bs has changed to @perm, and the set of sharable
+     * permission to @shared. The driver can use this to propagate changes to
+     * its children (i.e. request permissions only if a parent actually needs
+     * them).
+     *
+     * This function is only invoked after bdrv_check_perm(), so block drivers
+     * may rely on preparations made in their .bdrv_check_perm implementation.
+     */
+    void (*bdrv_set_perm)(BlockDriverState *bs, uint64_t perm, uint64_t shared);
+
+    /*
+     * Called to inform the driver that after a previous bdrv_check_perm()
+     * call, the permission update is not performed and any preparations made
+     * for it (e.g. taken file locks) need to be undone.
+     *
+     * This function can be called even for nodes that never saw a
+     * bdrv_check_perm() call. It is a no-op then.
+     */
+    void (*bdrv_abort_perm_update)(BlockDriverState *bs);
+
+    /**
+     * Returns in @nperm and @nshared the permissions that the driver for @bs
+     * needs on its child @c, based on the cumulative permissions requested by
+     * the parents in @parent_perm and @parent_shared.
+     *
+     * If @c is NULL, return the permissions for attaching a new child for the
+     * given @role.
+     */
+     void (*bdrv_child_perm)(BlockDriverState *bs, BdrvChild *c,
+                             const BdrvChildRole *role,
+                             uint64_t parent_perm, uint64_t parent_shared,
+                             uint64_t *nperm, uint64_t *nshared);
+
     QLIST_ENTRY(BlockDriver) list;
 };
 
@@ -812,6 +865,14 @@ BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs,
                                   void *opaque, Error **errp);
 void bdrv_root_unref_child(BdrvChild *child);
 
+int bdrv_child_check_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
+                          Error **errp);
+void bdrv_child_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared);
+void bdrv_child_abort_perm_update(BdrvChild *c);
+int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared,
+                            Error **errp);
+
+
 const char *bdrv_get_parent_name(const BlockDriverState *bs);
 void blk_dev_change_media_cb(BlockBackend *blk, bool load);
 bool blk_dev_has_removable_media(BlockBackend *blk);
-- 
1.8.3.1

  parent reply	other threads:[~2017-02-27 20:10 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-27 20:09 [Qemu-devel] [PATCH v2 00/43] New op blocker system, part 1 Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 01/43] block: Add op blocker permission constants Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 02/43] block: Add Error argument to bdrv_attach_child() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 03/43] block: Let callers request permissions when attaching a child node Kevin Wolf
2017-02-27 20:09 ` Kevin Wolf [this message]
2017-02-28  8:18   ` [Qemu-devel] [PATCH v2 04/43] block: Involve block drivers in permission granting Fam Zheng
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 05/43] block: Default .bdrv_child_perm() for filter drivers Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 06/43] block: Request child permissions in " Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 07/43] block: Default .bdrv_child_perm() for format drivers Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 08/43] block: Request child permissions in " Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 09/43] vvfat: Implement .bdrv_child_perm() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 10/43] block: Require .bdrv_child_perm() with child nodes Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 11/43] block: Request real permissions in bdrv_attach_child() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 12/43] block: Add permissions to BlockBackend Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 13/43] block: Add permissions to blk_new() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 14/43] block: Add error parameter to blk_insert_bs() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 15/43] block: Add BDRV_O_RESIZE for blk_new_open() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 16/43] block: Request real permissions in blk_new_open() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 17/43] block: Allow error return in BlockDevOps.change_media_cb() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 18/43] hw/block: Request permissions Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 19/43] hw/block: Introduce share-rw qdev property Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 20/43] blockjob: Add permissions to block_job_create() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 21/43] block: Add BdrvChildRole.get_parent_desc() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 22/43] block: Include details on permission errors in message Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 23/43] block: Add BdrvChildRole.stay_at_node Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 24/43] blockjob: Add permissions to block_job_add_bdrv() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 25/43] commit: Use real permissions in commit block job Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 26/43] commit: Use real permissions for HMP 'commit' Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 27/43] backup: Use real permissions in backup block job Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 28/43] block: Fix pending requests check in bdrv_append() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 29/43] block: BdrvChildRole.attach/detach() callbacks Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 30/43] block: Allow backing file links in change_parent_backing_link() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 31/43] mirror: Use real permissions in mirror/active commit block job Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 32/43] stream: Use real permissions in streaming " Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 33/43] mirror: Add filter-node-name to blockdev-mirror Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 34/43] commit: Add filter-node-name to block-commit Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 35/43] hmp: Request permissions in qemu-io Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 36/43] migration/block: Use real permissions Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 37/43] nbd/server: Use real permissions for NBD exports Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 38/43] tests: Remove FIXME comments Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 39/43] block: Pass BdrvChild to bdrv_aligned_preadv/pwritev and copy-on-read Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 40/43] block: Assertions for write permissions Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 41/43] block: Assertions for resize permission Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 42/43] block: Add Error parameter to bdrv_set_backing_hd() Kevin Wolf
2017-02-27 20:09 ` [Qemu-devel] [PATCH v2 43/43] block: Add Error parameter to bdrv_append() Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1488226184-9044-5-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=famz@redhat.com \
    --cc=jcody@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).