block: Use BdrvChild callbacks for change_media/resize

We want to get rid of BlockDriverState.blk in order to allow multiple
BlockBackends per BDS. Converting the device callbacks in block.c (which
assume a single BlockBackend) to per-child callbacks gets us rid of the
first few instances.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Kevin Wolf 2016-02-24 15:13:35 +01:00
parent cbe1beb7a1
commit 5c8cab4808
3 changed files with 43 additions and 15 deletions

39
block.c
View File

@ -1214,6 +1214,27 @@ void bdrv_unref_child(BlockDriverState *parent, BdrvChild *child)
bdrv_root_unref_child(child); bdrv_root_unref_child(child);
} }
static void bdrv_parent_cb_change_media(BlockDriverState *bs, bool load)
{
BdrvChild *c;
QLIST_FOREACH(c, &bs->parents, next_parent) {
if (c->role->change_media) {
c->role->change_media(c, load);
}
}
}
static void bdrv_parent_cb_resize(BlockDriverState *bs)
{
BdrvChild *c;
QLIST_FOREACH(c, &bs->parents, next_parent) {
if (c->role->resize) {
c->role->resize(c);
}
}
}
/* /*
* Sets the backing file link of a BDS. A new reference is created; callers * Sets the backing file link of a BDS. A new reference is created; callers
* which don't need their own reference any more must call bdrv_unref(). * which don't need their own reference any more must call bdrv_unref().
@ -1673,9 +1694,7 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
} }
if (!bdrv_key_required(bs)) { if (!bdrv_key_required(bs)) {
if (bs->blk) { bdrv_parent_cb_change_media(bs, true);
blk_dev_change_media_cb(bs->blk, true);
}
} else if (!runstate_check(RUN_STATE_PRELAUNCH) } else if (!runstate_check(RUN_STATE_PRELAUNCH)
&& !runstate_check(RUN_STATE_INMIGRATE) && !runstate_check(RUN_STATE_INMIGRATE)
&& !runstate_check(RUN_STATE_PAUSED)) { /* HACK */ && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */
@ -2121,9 +2140,7 @@ static void bdrv_close(BlockDriverState *bs)
bdrv_release_named_dirty_bitmaps(bs); bdrv_release_named_dirty_bitmaps(bs);
assert(QLIST_EMPTY(&bs->dirty_bitmaps)); assert(QLIST_EMPTY(&bs->dirty_bitmaps));
if (bs->blk) { bdrv_parent_cb_change_media(bs, false);
blk_dev_change_media_cb(bs->blk, false);
}
if (bs->drv) { if (bs->drv) {
BdrvChild *child, *next; BdrvChild *child, *next;
@ -2574,9 +2591,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset)
if (ret == 0) { if (ret == 0) {
ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
bdrv_dirty_bitmap_truncate(bs); bdrv_dirty_bitmap_truncate(bs);
if (bs->blk) { bdrv_parent_cb_resize(bs);
blk_dev_resize_cb(bs->blk);
}
} }
return ret; return ret;
} }
@ -2686,11 +2701,9 @@ int bdrv_set_key(BlockDriverState *bs, const char *key)
if (ret < 0) { if (ret < 0) {
bs->valid_key = 0; bs->valid_key = 0;
} else if (!bs->valid_key) { } else if (!bs->valid_key) {
/* call the change callback now, we skipped it on open */
bs->valid_key = 1; bs->valid_key = 1;
if (bs->blk) { bdrv_parent_cb_change_media(bs, true);
/* call the change callback now, we skipped it on open */
blk_dev_change_media_cb(bs->blk, true);
}
} }
return ret; return ret;
} }

View File

@ -94,9 +94,15 @@ static void blk_root_inherit_options(int *child_flags, QDict *child_options,
static void blk_root_drained_begin(BdrvChild *child); static void blk_root_drained_begin(BdrvChild *child);
static void blk_root_drained_end(BdrvChild *child); static void blk_root_drained_end(BdrvChild *child);
static void blk_root_change_media(BdrvChild *child, bool load);
static void blk_root_resize(BdrvChild *child);
static const BdrvChildRole child_root = { static const BdrvChildRole child_root = {
.inherit_options = blk_root_inherit_options, .inherit_options = blk_root_inherit_options,
.change_media = blk_root_change_media,
.resize = blk_root_resize,
.drained_begin = blk_root_drained_begin, .drained_begin = blk_root_drained_begin,
.drained_end = blk_root_drained_end, .drained_end = blk_root_drained_end,
}; };
@ -556,6 +562,11 @@ void blk_dev_change_media_cb(BlockBackend *blk, bool load)
} }
} }
static void blk_root_change_media(BdrvChild *child, bool load)
{
blk_dev_change_media_cb(child->opaque, load);
}
/* /*
* Does @blk's attached device model have removable media? * Does @blk's attached device model have removable media?
* %true if no device model is attached. * %true if no device model is attached.
@ -610,8 +621,10 @@ bool blk_dev_is_medium_locked(BlockBackend *blk)
/* /*
* Notify @blk's attached device model of a backend size change. * Notify @blk's attached device model of a backend size change.
*/ */
void blk_dev_resize_cb(BlockBackend *blk) static void blk_root_resize(BdrvChild *child)
{ {
BlockBackend *blk = child->opaque;
if (blk->dev_ops && blk->dev_ops->resize_cb) { if (blk->dev_ops && blk->dev_ops->resize_cb) {
blk->dev_ops->resize_cb(blk->dev_opaque); blk->dev_ops->resize_cb(blk->dev_opaque);
} }

View File

@ -365,6 +365,9 @@ struct BdrvChildRole {
void (*inherit_options)(int *child_flags, QDict *child_options, void (*inherit_options)(int *child_flags, QDict *child_options,
int parent_flags, QDict *parent_options); int parent_flags, QDict *parent_options);
void (*change_media)(BdrvChild *child, bool load);
void (*resize)(BdrvChild *child);
/* /*
* If this pair of functions is implemented, the parent doesn't issue new * If this pair of functions is implemented, the parent doesn't issue new
* requests after returning from .drained_begin() until .drained_end() is * requests after returning from .drained_begin() until .drained_end() is
@ -722,7 +725,6 @@ bool blk_dev_has_tray(BlockBackend *blk);
void blk_dev_eject_request(BlockBackend *blk, bool force); void blk_dev_eject_request(BlockBackend *blk, bool force);
bool blk_dev_is_tray_open(BlockBackend *blk); bool blk_dev_is_tray_open(BlockBackend *blk);
bool blk_dev_is_medium_locked(BlockBackend *blk); bool blk_dev_is_medium_locked(BlockBackend *blk);
void blk_dev_resize_cb(BlockBackend *blk);
void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors); void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors);
bool bdrv_requests_pending(BlockDriverState *bs); bool bdrv_requests_pending(BlockDriverState *bs);