bdrv_replace_child_noperm() is the heart of block graph changes. It is
called when a node is inserted, removed, or replaced. These changes
happen without notifying parents in the graph, so it is currently not
possible to monitor changes.
Add BdrvChildClass callbacks to propagate attach/detach operations to
the roots of the graph. The next commit will introduce a BlockBackend
API for monitoring changes using this mechanism.
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
include/block/block_int-common.h | 11 +++++++
block.c | 56 +++++++++++++++++++++++++-------
2 files changed, 56 insertions(+), 11 deletions(-)
diff --git a/include/block/block_int-common.h b/include/block/block_int-common.h
index 034c0634c8..0368a3191c 100644
--- a/include/block/block_int-common.h
+++ b/include/block/block_int-common.h
@@ -963,6 +963,17 @@ struct BdrvChildClass {
void GRAPH_RDLOCK_PTR (*activate)(BdrvChild *child, Error **errp);
int GRAPH_RDLOCK_PTR (*inactivate)(BdrvChild *child);
+ /*
+ * Optional callbacks when a descendant (child, grandchild, etc) attaches
+ * or detaches a BlockDriverState. Allows monitoring changes to the graph.
+ *
+ * Called after ->attach() and before ->detach().
+ */
+ void GRAPH_WRLOCK_PTR (*propagate_attach)(BdrvChild *self,
+ BdrvChild *descendant);
+ void GRAPH_WRLOCK_PTR (*propagate_detach)(BdrvChild *self,
+ BdrvChild *descendant);
+
void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
diff --git a/block.c b/block.c
index 8848e9a7ed..e1480dda04 100644
--- a/block.c
+++ b/block.c
@@ -1497,6 +1497,32 @@ static void GRAPH_WRLOCK bdrv_child_cb_detach(BdrvChild *child)
}
}
+static void GRAPH_WRLOCK
+bdrv_child_cb_propagate_attach(BdrvChild *self, BdrvChild *descendant)
+{
+ BlockDriverState *bs = self->opaque;
+ BdrvChild *c;
+
+ QLIST_FOREACH(c, &bs->parents, next_parent) {
+ if (c->klass->propagate_attach) {
+ c->klass->propagate_attach(c, descendant);
+ }
+ }
+}
+
+static void GRAPH_WRLOCK
+bdrv_child_cb_propagate_detach(BdrvChild *self, BdrvChild *descendant)
+{
+ BlockDriverState *bs = self->opaque;
+ BdrvChild *c;
+
+ QLIST_FOREACH(c, &bs->parents, next_parent) {
+ if (c->klass->propagate_detach) {
+ c->klass->propagate_detach(c, descendant);
+ }
+ }
+}
+
static int bdrv_child_cb_update_filename(BdrvChild *c, BlockDriverState *base,
const char *filename,
bool backing_mask_protocol,
@@ -1519,17 +1545,19 @@ AioContext *child_of_bds_get_parent_aio_context(BdrvChild *c)
}
const BdrvChildClass child_of_bds = {
- .parent_is_bds = true,
- .get_parent_desc = bdrv_child_get_parent_desc,
- .inherit_options = bdrv_inherited_options,
- .drained_begin = bdrv_child_cb_drained_begin,
- .drained_poll = bdrv_child_cb_drained_poll,
- .drained_end = bdrv_child_cb_drained_end,
- .attach = bdrv_child_cb_attach,
- .detach = bdrv_child_cb_detach,
- .inactivate = bdrv_child_cb_inactivate,
- .change_aio_ctx = bdrv_child_cb_change_aio_ctx,
- .update_filename = bdrv_child_cb_update_filename,
+ .parent_is_bds = true,
+ .get_parent_desc = bdrv_child_get_parent_desc,
+ .inherit_options = bdrv_inherited_options,
+ .drained_begin = bdrv_child_cb_drained_begin,
+ .drained_poll = bdrv_child_cb_drained_poll,
+ .drained_end = bdrv_child_cb_drained_end,
+ .attach = bdrv_child_cb_attach,
+ .detach = bdrv_child_cb_detach,
+ .propagate_attach = bdrv_child_cb_propagate_attach,
+ .propagate_detach = bdrv_child_cb_propagate_detach,
+ .inactivate = bdrv_child_cb_inactivate,
+ .change_aio_ctx = bdrv_child_cb_change_aio_ctx,
+ .update_filename = bdrv_child_cb_update_filename,
.get_parent_aio_context = child_of_bds_get_parent_aio_context,
};
@@ -2967,6 +2995,9 @@ bdrv_replace_child_noperm(BdrvChild *child, BlockDriverState *new_bs)
}
if (old_bs) {
+ if (child->klass->propagate_detach) {
+ child->klass->propagate_detach(child, child);
+ }
if (child->klass->detach) {
child->klass->detach(child);
}
@@ -2980,6 +3011,9 @@ bdrv_replace_child_noperm(BdrvChild *child, BlockDriverState *new_bs)
if (child->klass->attach) {
child->klass->attach(child);
}
+ if (child->klass->propagate_attach) {
+ child->klass->propagate_attach(child, child);
+ }
}
/*
--
2.51.0
© 2016 - 2025 Red Hat, Inc.