@@ -107,6 +107,13 @@ static int check_constraints(QCow2BitmapHeader *h, int cluster_size,
return fail ? -EINVAL : 0;
}
+static QCow2BitmapHeader *bitmap_header(BDRVQcow2State *s,
+ QCow2Bitmap *bitmap)
+{
+ return (QCow2BitmapHeader *)
+ (s->bitmap_directory + bitmap->offset);
+}
+
static int directory_read(BlockDriverState *bs, Error **errp)
{
int ret;
@@ -210,3 +217,106 @@ fail:
return ret;
}
+
+static QCow2Bitmap *find_bitmap_by_name(BlockDriverState *bs, const char *name)
+{
+ BDRVQcow2State *s = bs->opaque;
+ QCow2Bitmap *bm, *end = s->bitmaps + s->nb_bitmaps;
+
+ for (bm = s->bitmaps; bm < end; ++bm) {
+ if (strcmp(bm->name, name) == 0) {
+ return bm;
+ }
+ }
+
+ return NULL;
+}
+
+/* load_bitmap_data()
+ * load dirty bitmap from bitmap table
+ * Bitmap table entries are assumed to be in big endian format */
+static int load_bitmap_data(BlockDriverState *bs, const uint64_t *bitmap_table,
+ uint32_t bitmap_table_size, BdrvDirtyBitmap *bitmap)
+{
+ int ret = 0;
+ BDRVQcow2State *s = bs->opaque;
+ uint32_t i;
+ uint64_t *tab = g_memdup(bitmap_table,
+ bitmap_table_size * sizeof(uint64_t));
+
+ for (i = 0; i < bitmap_table_size; ++i) {
+ be64_to_cpus(tab + i);
+ }
+
+ ret = bdrv_dirty_bitmap_load(bitmap, bs->file->bs, tab, bitmap_table_size,
+ s->cluster_size);
+
+ g_free(tab);
+
+ return ret;
+}
+
+static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs, QCow2Bitmap *bm,
+ Error **errp)
+{
+ BDRVQcow2State *s = bs->opaque;
+ int ret;
+ QCow2BitmapHeader *bmh;
+ uint64_t *bitmap_table = NULL;
+ uint32_t granularity;
+ BdrvDirtyBitmap *bitmap = NULL;
+
+ bmh = bitmap_header(s, bm);
+
+ bitmap_table = g_try_malloc(bmh->bitmap_table_size * sizeof(uint64_t));
+ if (bitmap_table == NULL) {
+ error_setg_errno(errp, -ENOMEM,
+ "Could not allocate bitmap table");
+ return NULL;
+ }
+
+ ret = bdrv_pread(bs->file->bs, bmh->bitmap_table_offset,
+ bitmap_table,
+ bmh->bitmap_table_size * sizeof(uint64_t));
+ if (ret < 0) {
+ error_setg_errno(errp, -ret,
+ "Could not read bitmap_table table from image");
+ goto fail;
+ }
+
+ granularity = 1U << bmh->granularity_bits;
+ bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
+ if (bitmap == NULL) {
+ goto fail;
+ }
+
+ ret = load_bitmap_data(bs, bitmap_table, bmh->bitmap_table_size, bitmap);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Could not read bitmap from image");
+ goto fail;
+ }
+
+ g_free(bitmap_table);
+ return bitmap;
+
+fail:
+ g_free(bitmap_table);
+ bdrv_release_dirty_bitmap(bs, bitmap);
+
+ return NULL;
+}
+
+BdrvDirtyBitmap *qcow2_bitmap_load(BlockDriverState *bs, const char *name,
+ Error **errp)
+{
+ QCow2Bitmap *bm;
+
+ bm = find_bitmap_by_name(bs, name);
+ if (bm == NULL) {
+ error_setg(errp, "Could not find bitmap '%s' in the node '%s'", name,
+ bdrv_get_device_or_node_name(bs));
+ return NULL;
+ }
+
+ return load_bitmap(bs, bm, errp);
+}
@@ -3352,6 +3352,8 @@ BlockDriver bdrv_qcow2 = {
.bdrv_get_info = qcow2_get_info,
.bdrv_get_specific_info = qcow2_get_specific_info,
+ .bdrv_dirty_bitmap_load = qcow2_bitmap_load,
+
.bdrv_save_vmstate = qcow2_save_vmstate,
.bdrv_load_vmstate = qcow2_load_vmstate,
@@ -620,6 +620,9 @@ int qcow2_read_snapshots(BlockDriverState *bs);
void qcow2_free_bitmaps(BlockDriverState *bs);
int qcow2_read_bitmaps(BlockDriverState *bs, Error **errp);
+BdrvDirtyBitmap *qcow2_bitmap_load(BlockDriverState *bs, const char *name,
+ Error **errp);
+
/* qcow2-cache.c functions */
Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
@@ -215,6 +215,10 @@ struct BlockDriver {
int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs);
+ BdrvDirtyBitmap *(*bdrv_dirty_bitmap_load)(BlockDriverState *bs,
+ const char *name,
+ Error **errp);
+
int (*bdrv_save_vmstate)(BlockDriverState *bs, QEMUIOVector *qiov,
int64_t pos);
int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
This function loads block dirty bitmap from qcow2. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> --- block/qcow2-dirty-bitmap.c | 110 +++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.c | 2 + block/qcow2.h | 3 ++ include/block/block_int.h | 4 ++ 4 files changed, 119 insertions(+)