From: Fam Zheng <f...@redhat.com> Signed-off-by: Fam Zheng <f...@redhat.com> Signed-off-by: John Snow <js...@redhat.com> Reviewed-by: Max Reitz <mre...@redhat.com> --- block.c | 35 +++++++++++++++++++++++++++++++---- include/block/block.h | 4 ++++ 2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/block.c b/block.c index a0d1150..2d08b9f 100644 --- a/block.c +++ b/block.c @@ -53,6 +53,8 @@ struct BdrvDirtyBitmap { HBitmap *bitmap; + int64_t size; + int64_t granularity; char *name; QLIST_ENTRY(BdrvDirtyBitmap) list; }; @@ -5346,6 +5348,26 @@ void bdrv_dirty_bitmap_make_anon(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) bitmap->name = NULL; } +BdrvDirtyBitmap *bdrv_copy_dirty_bitmap(BlockDriverState *bs, + const BdrvDirtyBitmap *bitmap, + const char *name) +{ + BdrvDirtyBitmap *new_bitmap; + + new_bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); + new_bitmap->bitmap = hbitmap_copy(bitmap->bitmap); + new_bitmap->size = bitmap->size; + new_bitmap->granularity = bitmap->granularity; + new_bitmap->name = g_strdup(name); + QLIST_INSERT_HEAD(&bs->dirty_bitmaps, new_bitmap, list); + return new_bitmap; +} + +void bdrv_reset_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) +{ + hbitmap_reset(bitmap->bitmap, 0, bitmap->size); +} + BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity, const char *name, @@ -5353,6 +5375,7 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, { int64_t bitmap_size; BdrvDirtyBitmap *bitmap; + int sector_granularity; assert((granularity & (granularity - 1)) == 0); @@ -5360,8 +5383,8 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, error_setg(errp, "Bitmap already exists: %s", name); return NULL; } - granularity >>= BDRV_SECTOR_BITS; - assert(granularity); + sector_granularity = granularity >> BDRV_SECTOR_BITS; + assert(sector_granularity); bitmap_size = bdrv_nb_sectors(bs); if (bitmap_size < 0) { error_setg_errno(errp, -bitmap_size, "could not get length of device"); @@ -5369,7 +5392,9 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, return NULL; } bitmap = g_new0(BdrvDirtyBitmap, 1); - bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); + bitmap->size = bitmap_size; + bitmap->granularity = granularity; + bitmap->bitmap = hbitmap_alloc(bitmap->size, ffs(sector_granularity) - 1); bitmap->name = g_strdup(name); QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); return bitmap; @@ -5441,7 +5466,9 @@ uint64_t bdrv_dbm_calc_def_granularity(BlockDriverState *bs) int64_t bdrv_dirty_bitmap_granularity(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) { - return BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap); + g_assert(BDRV_SECTOR_SIZE << hbitmap_granularity(bitmap->bitmap) == + bitmap->granularity); + return bitmap->granularity; } void bdrv_dirty_iter_init(BlockDriverState *bs, diff --git a/include/block/block.h b/include/block/block.h index f180f93..5522eba 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -437,6 +437,10 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bdrv_find_dirty_bitmap(BlockDriverState *bs, const char *name); void bdrv_dirty_bitmap_make_anon(BlockDriverState *bs, BdrvDirtyBitmap *bitmap); +void bdrv_reset_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap); +BdrvDirtyBitmap *bdrv_copy_dirty_bitmap(BlockDriverState *bs, + const BdrvDirtyBitmap *bitmap, + const char *name); void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap); BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs); uint64_t bdrv_dbm_calc_def_granularity(BlockDriverState *bs); -- 1.9.3