This flag is set on bitmap load and unset on store. If it is already set when loading the bitmap, the bitmap should not be load (it is in use by other drive or it is inconsistent (was not successfully saved))
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@virtuozzo.com> --- block/qcow2-dirty-bitmap.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/block/qcow2-dirty-bitmap.c b/block/qcow2-dirty-bitmap.c index 384ccea..f210fee 100644 --- a/block/qcow2-dirty-bitmap.c +++ b/block/qcow2-dirty-bitmap.c @@ -42,7 +42,8 @@ #define BME_MAX_NAME_SIZE 1023 /* Bitmap directory entry flags */ -#define BME_RESERVED_FLAGS 0xffffffff +#define BME_RESERVED_FLAGS 0xfffffffe +#define BME_FLAG_IN_USE 1 /* bits [1, 8] U [56, 63] are reserved */ #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001fe @@ -134,6 +135,29 @@ static QCow2BitmapHeader *bitmap_header(BDRVQcow2State *s, (s->bitmap_directory + bitmap->offset); } +static int update_bitmap_header_sync(BlockDriverState *bs, QCow2Bitmap *bitmap) +{ + int ret; + BDRVQcow2State *s = bs->opaque; + QCow2BitmapHeader *h = bitmap_header(s, bitmap); + + bitmap_header_to_be(h); + ret = bdrv_pwrite(bs->file->bs, + s->bitmap_directory_offset + bitmap->offset, + h, dir_entry_size(h)); + bitmap_header_to_cpu(h); + if (ret < 0) { + return ret; + } + + ret = bdrv_flush(bs); + if (ret < 0) { + return ret; + } + + return 0; +} + static int directory_read(BlockDriverState *bs, Error **errp) { int ret; @@ -293,6 +317,11 @@ static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs, QCow2Bitmap *bm, bmh = bitmap_header(s, bm); + if (bmh->flags & BME_FLAG_IN_USE) { + error_setg(errp, "Bitmap '%s' is in use", bm->name); + return NULL; + } + bitmap_table = g_try_malloc(bmh->bitmap_table_size * sizeof(uint64_t)); if (bitmap_table == NULL) { error_setg_errno(errp, -ENOMEM, @@ -321,6 +350,13 @@ static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs, QCow2Bitmap *bm, goto fail; } + bmh->flags |= BME_FLAG_IN_USE; + ret = update_bitmap_header_sync(bs, bm); + if (ret < 0) { + error_setg_errno(errp, -ret, "Could not set in_use in bitmap header"); + goto fail; + } + g_free(bitmap_table); return bitmap; @@ -769,6 +805,13 @@ void qcow2_bitmap_store(BlockDriverState *bs, goto finish; } + bmh->flags &= ~BME_FLAG_IN_USE; + ret = update_bitmap_header_sync(bs, bm); + if (ret < 0) { + error_setg_errno(errp, ret, "Can't update bitmap header."); + goto finish; + } + finish: g_free(bitmap_table); } -- 1.8.3.1