Block drivers can implement this new operation .bdrv_lockf to actually lock the image in the protocol specific way.
Signed-off-by: Fam Zheng <f...@redhat.com> --- block.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++ include/block/block_int.h | 11 +++++++++++ 2 files changed, 59 insertions(+) diff --git a/block.c b/block.c index 18a497f..153f9d2 100644 --- a/block.c +++ b/block.c @@ -844,6 +844,41 @@ out: g_free(gen_node_name); } +static int bdrv_lock_unlock_image_do(BlockDriverState *bs, bool lock_image) +{ + int cmd = BDRV_LOCKF_UNLOCK; + int ret; + + if (bs->image_locked == lock_image) { + return 0; + } else if (!bs->drv) { + return -ENOMEDIUM; + } else if (!bs->drv->bdrv_lockf) { + return 0; + } + if (lock_image) { + cmd = (bs->open_flags & BDRV_O_RDWR) && + !(bs->open_flags & BDRV_O_SHARED_LOCK) ? BDRV_LOCKF_EXCLUSIVE : + BDRV_LOCKF_SHARED; + } + ret = bs->drv->bdrv_lockf(bs, cmd); + if (ret == -ENOTSUP) { + /* Handle it the same way as !bs->drv->bdrv_lockf */ + ret = 0; + } + return ret; +} + +static int bdrv_lock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, true); +} + +static int bdrv_unlock_image(BlockDriverState *bs) +{ + return bdrv_lock_unlock_image_do(bs, false); +} + static QemuOptsList bdrv_runtime_opts = { .name = "bdrv_common", .head = QTAILQ_HEAD_INITIALIZER(bdrv_runtime_opts.head), @@ -993,6 +1028,14 @@ static int bdrv_open_common(BlockDriverState *bs, BdrvChild *file, goto free_and_fail; } + if (!(open_flags & (BDRV_O_NO_LOCK | BDRV_O_INACTIVE))) { + ret = bdrv_lock_image(bs); + if (ret) { + error_setg(errp, "Failed to lock image"); + goto free_and_fail; + } + } + ret = refresh_total_sectors(bs, bs->total_sectors); if (ret < 0) { error_setg_errno(errp, -ret, "Could not refresh total sector count"); @@ -2142,6 +2185,7 @@ static void bdrv_close(BlockDriverState *bs) if (bs->drv) { BdrvChild *child, *next; + bdrv_unlock_image(bs); bs->drv->bdrv_close(bs); bs->drv = NULL; @@ -3235,6 +3279,9 @@ void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) error_setg_errno(errp, -ret, "Could not refresh total sector count"); return; } + if (!(bs->open_flags & BDRV_O_NO_LOCK)) { + bdrv_lock_image(bs); + } } void bdrv_invalidate_cache_all(Error **errp) @@ -3276,6 +3323,7 @@ static int bdrv_inactivate_recurse(BlockDriverState *bs, } if (setting_flag) { + ret = bdrv_unlock_image(bs); bs->open_flags |= BDRV_O_INACTIVE; } return 0; diff --git a/include/block/block_int.h b/include/block/block_int.h index a029c20..2b82d49 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -85,6 +85,12 @@ typedef struct BdrvTrackedRequest { struct BdrvTrackedRequest *waiting_for; } BdrvTrackedRequest; +typedef enum { + BDRV_LOCKF_EXCLUSIVE, + BDRV_LOCKF_SHARED, + BDRV_LOCKF_UNLOCK, +} BdrvLockfCmd; + struct BlockDriver { const char *format_name; int instance_size; @@ -318,6 +324,10 @@ struct BlockDriver { Error **errp); void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child, Error **errp); + /** + * Lock/unlock the image. + */ + int (*bdrv_lockf)(BlockDriverState *bs, BdrvLockfCmd cmd); QLIST_ENTRY(BlockDriver) list; }; @@ -496,6 +506,7 @@ struct BlockDriverState { unsigned io_plug_disabled; int quiesce_counter; + bool image_locked; }; struct BlockBackendRootState { -- 2.8.2