block: Add bdrv_try_set_aio_context()

Eventually, we want to make sure that all parents and all children of a
node are in the same AioContext as the node itself. This means that
changing the AioContext may fail because one of the other involved
parties (e.g. a guest device that was configured with an iothread)
cannot allow switching to a different AioContext.

Introduce a set of functions that allow to first check whether all
involved nodes can switch to a new context and only then do the actual
switch. The check recursively covers children and parents.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
Kevin Wolf 2019-05-06 19:17:56 +02:00
parent 25349e8250
commit 5d2318499f
3 changed files with 103 additions and 0 deletions

92
block.c
View File

@ -936,6 +936,13 @@ static int bdrv_child_cb_inactivate(BdrvChild *child)
return 0; return 0;
} }
static bool bdrv_child_cb_can_set_aio_ctx(BdrvChild *child, AioContext *ctx,
GSList **ignore, Error **errp)
{
BlockDriverState *bs = child->opaque;
return bdrv_can_set_aio_context(bs, ctx, ignore, errp);
}
/* /*
* Returns the options and flags that a temporary snapshot should get, based on * Returns the options and flags that a temporary snapshot should get, based on
* the originally requested flags (the originally requested image will have * the originally requested flags (the originally requested image will have
@ -1003,6 +1010,7 @@ const BdrvChildRole child_file = {
.attach = bdrv_child_cb_attach, .attach = bdrv_child_cb_attach,
.detach = bdrv_child_cb_detach, .detach = bdrv_child_cb_detach,
.inactivate = bdrv_child_cb_inactivate, .inactivate = bdrv_child_cb_inactivate,
.can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
}; };
/* /*
@ -1029,6 +1037,7 @@ const BdrvChildRole child_format = {
.attach = bdrv_child_cb_attach, .attach = bdrv_child_cb_attach,
.detach = bdrv_child_cb_detach, .detach = bdrv_child_cb_detach,
.inactivate = bdrv_child_cb_inactivate, .inactivate = bdrv_child_cb_inactivate,
.can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
}; };
static void bdrv_backing_attach(BdrvChild *c) static void bdrv_backing_attach(BdrvChild *c)
@ -1152,6 +1161,7 @@ const BdrvChildRole child_backing = {
.drained_end = bdrv_child_cb_drained_end, .drained_end = bdrv_child_cb_drained_end,
.inactivate = bdrv_child_cb_inactivate, .inactivate = bdrv_child_cb_inactivate,
.update_filename = bdrv_backing_update_filename, .update_filename = bdrv_backing_update_filename,
.can_set_aio_ctx = bdrv_child_cb_can_set_aio_ctx,
}; };
static int bdrv_open_flags(BlockDriverState *bs, int flags) static int bdrv_open_flags(BlockDriverState *bs, int flags)
@ -5750,6 +5760,88 @@ void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context)
aio_context_release(new_context); aio_context_release(new_context);
} }
static bool bdrv_parent_can_set_aio_context(BdrvChild *c, AioContext *ctx,
GSList **ignore, Error **errp)
{
if (g_slist_find(*ignore, c)) {
return true;
}
*ignore = g_slist_prepend(*ignore, c);
/* A BdrvChildRole that doesn't handle AioContext changes cannot
* tolerate any AioContext changes */
if (!c->role->can_set_aio_ctx) {
char *user = bdrv_child_user_desc(c);
error_setg(errp, "Changing iothreads is not supported by %s", user);
g_free(user);
return false;
}
if (!c->role->can_set_aio_ctx(c, ctx, ignore, errp)) {
assert(!errp || *errp);
return false;
}
return true;
}
bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx,
GSList **ignore, Error **errp)
{
if (g_slist_find(*ignore, c)) {
return true;
}
*ignore = g_slist_prepend(*ignore, c);
return bdrv_can_set_aio_context(c->bs, ctx, ignore, errp);
}
/* @ignore will accumulate all visited BdrvChild object. The caller is
* responsible for freeing the list afterwards. */
bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx,
GSList **ignore, Error **errp)
{
BdrvChild *c;
if (bdrv_get_aio_context(bs) == ctx) {
return true;
}
QLIST_FOREACH(c, &bs->parents, next_parent) {
if (!bdrv_parent_can_set_aio_context(c, ctx, ignore, errp)) {
return false;
}
}
QLIST_FOREACH(c, &bs->children, next) {
if (!bdrv_child_can_set_aio_context(c, ctx, ignore, errp)) {
return false;
}
}
return true;
}
int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
BdrvChild *ignore_child, Error **errp)
{
GSList *ignore;
bool ret;
ignore = ignore_child ? g_slist_prepend(NULL, ignore_child) : NULL;
ret = bdrv_can_set_aio_context(bs, ctx, &ignore, errp);
g_slist_free(ignore);
if (!ret) {
return -EPERM;
}
bdrv_set_aio_context(bs, ctx);
return 0;
}
int bdrv_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
Error **errp)
{
return bdrv_child_try_set_aio_context(bs, ctx, NULL, errp);
}
void bdrv_add_aio_context_notifier(BlockDriverState *bs, void bdrv_add_aio_context_notifier(BlockDriverState *bs,
void (*attached_aio_context)(AioContext *new_context, void *opaque), void (*attached_aio_context)(AioContext *new_context, void *opaque),
void (*detach_aio_context)(void *opaque), void *opaque) void (*detach_aio_context)(void *opaque), void *opaque)

View File

@ -586,6 +586,14 @@ void bdrv_coroutine_enter(BlockDriverState *bs, Coroutine *co);
* This function must be called with iothread lock held. * This function must be called with iothread lock held.
*/ */
void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context); void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context);
int bdrv_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
Error **errp);
int bdrv_child_try_set_aio_context(BlockDriverState *bs, AioContext *ctx,
BdrvChild *ignore_child, Error **errp);
bool bdrv_child_can_set_aio_context(BdrvChild *c, AioContext *ctx,
GSList **ignore, Error **errp);
bool bdrv_can_set_aio_context(BlockDriverState *bs, AioContext *ctx,
GSList **ignore, Error **errp);
int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz); int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz);
int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo); int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo);

View File

@ -691,6 +691,9 @@ struct BdrvChildRole {
* can update its reference. */ * can update its reference. */
int (*update_filename)(BdrvChild *child, BlockDriverState *new_base, int (*update_filename)(BdrvChild *child, BlockDriverState *new_base,
const char *filename, Error **errp); const char *filename, Error **errp);
bool (*can_set_aio_ctx)(BdrvChild *child, AioContext *ctx,
GSList **ignore, Error **errp);
}; };
extern const BdrvChildRole child_file; extern const BdrvChildRole child_file;