[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 14/21] block: add bdrv_attach_child_noperm() transaction action
From: |
Vladimir Sementsov-Ogievskiy |
Subject: |
[PATCH 14/21] block: add bdrv_attach_child_noperm() transaction action |
Date: |
Mon, 23 Nov 2020 23:12:26 +0300 |
The code partly duplicates bdrv_root_attach_child() and
bdrv_attach_child(). Still refactoring these two functions by renaming
them to *_common with new noperm argument is more complicating. When
all operations moved to new graph update paradigm (update permissions
only on updated graph) all duplications should leave.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
block.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 90 insertions(+), 4 deletions(-)
diff --git a/block.c b/block.c
index 08501350b7..5f6ad1d016 100644
--- a/block.c
+++ b/block.c
@@ -2974,16 +2974,102 @@ BdrvChild *bdrv_attach_child(BlockDriverState
*parent_bs,
return child;
}
-static void bdrv_detach_child(BdrvChild *child)
+static void bdrv_remove_empty_child(BdrvChild *child)
{
+ assert(!child->bs);
QLIST_SAFE_REMOVE(child, next);
-
- bdrv_replace_child(child, NULL);
-
g_free(child->name);
g_free(child);
}
+typedef struct BdrvAttachChildNopermState {
+ BdrvChild *child;
+ AioContext *old_aio_context; /* NULL if not changed */
+} BdrvAttachChildNopermState;
+
+static void bdrv_attach_child_noperm_abort(void *opaque)
+{
+ BdrvAttachChildNopermState *s = opaque;
+ BlockDriverState *bs = s->child->bs;
+
+ bdrv_replace_child_noperm(s->child, NULL);
+ bdrv_remove_empty_child(s->child);
+
+ /*
+ * refcnt was positive prior to bdrv_ref() in bdrv_attach_child_noperm(),
+ * so bs should not be deleted now.
+ */
+ assert(bs->refcnt > 1);
+ bdrv_unref(bs);
+ if (s->old_aio_context) {
+ bdrv_try_set_aio_context(bs, s->old_aio_context, NULL);
+ }
+}
+
+static BdrvActionDrv bdrv_attach_child_noperm_drv = {
+ .abort = bdrv_attach_child_noperm_abort,
+ .clean = g_free,
+};
+
+__attribute__((unused))
+static BdrvChild *bdrv_attach_child_noperm(BlockDriverState *parent_bs,
+ BlockDriverState *child_bs,
+ const char *child_name,
+ BdrvChildRole child_role,
+ GSList **tran,
+ Error **errp)
+{
+ int ret;
+ BdrvChild *child;
+ uint64_t perm, shared_perm;
+ AioContext *parent_ctx = bdrv_get_aio_context(parent_bs);
+ AioContext *child_ctx = bdrv_get_aio_context(child_bs);
+ BdrvAttachChildNopermState *s;
+
+ if (child_ctx != parent_ctx) {
+ ret = bdrv_try_set_aio_context(child_bs, parent_ctx, errp);
+ if (ret < 0) {
+ return NULL;
+ }
+ }
+
+ bdrv_get_cumulative_perm(parent_bs, &perm, &shared_perm);
+
+ assert(parent_bs->drv);
+ bdrv_child_perm(parent_bs, child_bs, NULL, child_role, NULL,
+ perm, shared_perm, &perm, &shared_perm);
+
+ child = g_new(BdrvChild, 1);
+ *child = (BdrvChild) {
+ .bs = NULL,
+ .name = g_strdup(child_name),
+ .klass = &child_of_bds,
+ .role = child_role,
+ .perm = perm,
+ .shared_perm = shared_perm,
+ .opaque = parent_bs,
+ };
+ bdrv_ref(child_bs);
+ bdrv_replace_child_noperm(child, child_bs);
+
+ QLIST_INSERT_HEAD(&parent_bs->children, child, next);
+
+ s = g_new(BdrvAttachChildNopermState, 1);
+ *s = (BdrvAttachChildNopermState) {
+ .child = child,
+ .old_aio_context = child_ctx == parent_ctx ? NULL : child_ctx,
+ };
+ tran_prepend(tran, &bdrv_attach_child_noperm_drv, s);
+
+ return child;
+}
+
+static void bdrv_detach_child(BdrvChild *child)
+{
+ bdrv_replace_child(child, NULL);
+ bdrv_remove_empty_child(child);
+}
+
/* Callers must ensure that child->frozen is false. */
void bdrv_root_unref_child(BdrvChild *child)
{
--
2.21.3
- [PATCH 04/21] block: bdrv_refresh_perms: check parents compliance, (continued)
- [PATCH 04/21] block: bdrv_refresh_perms: check parents compliance, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 06/21] block: rewrite bdrv_child_try_set_perm() using bdrv_refresh_perms(), Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 07/21] block: inline bdrv_child_*() permission functions calls, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 08/21] block: use topological sort for permission update, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 05/21] block: refactor bdrv_child* permission functions, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 09/21] block: add bdrv_drv_set_perm transaction action, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 10/21] block: add bdrv_list_* permission update functions, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 11/21] block: add bdrv_replace_child_safe() transaction action, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 12/21] block: return value from bdrv_replace_node(), Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 14/21] block: add bdrv_attach_child_noperm() transaction action,
Vladimir Sementsov-Ogievskiy <=
- [PATCH 15/21] block: split out bdrv_replace_node_noperm(), Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 13/21] block: fix bdrv_replace_node_common, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 16/21] block: bdrv_append(): don't consume reference, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 21/21] block/backup-top: drop .active, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 19/21] block: add bdrv_remove_backing transaction action, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 17/21] block: bdrv_append(): return status, Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 20/21] block: introduce bdrv_drop_filter(), Vladimir Sementsov-Ogievskiy, 2020/11/23
- [PATCH 18/21] block: adapt bdrv_append() for inserting filters, Vladimir Sementsov-Ogievskiy, 2020/11/23