From patchwork Tue Mar 15 20:04:15 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Sementsov-Ogievskiy X-Patchwork-Id: 8592181 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 6A9569F44D for ; Tue, 15 Mar 2016 20:09:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A3EE82026C for ; Tue, 15 Mar 2016 20:09:23 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B185220263 for ; Tue, 15 Mar 2016 20:09:22 +0000 (UTC) Received: from localhost ([::1]:51190 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afvHO-00078H-0l for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Mar 2016 16:09:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38743) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afvDV-0007e9-DF for qemu-devel@nongnu.org; Tue, 15 Mar 2016 16:05:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1afvDU-0008Gk-5k for qemu-devel@nongnu.org; Tue, 15 Mar 2016 16:05:21 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:29411 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afvDT-0008Fl-QX for qemu-devel@nongnu.org; Tue, 15 Mar 2016 16:05:20 -0400 Received: from kvm.sw.ru. ([10.28.8.145]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id u2FK4bCj005992; Tue, 15 Mar 2016 23:05:03 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Date: Tue, 15 Mar 2016 23:04:15 +0300 Message-Id: <1458072268-53705-10-git-send-email-vsementsov@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1458072268-53705-1-git-send-email-vsementsov@virtuozzo.com> References: <1458072268-53705-1-git-send-email-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: OpenBSD 3.x X-Received-From: 195.214.232.25 Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, famz@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, den@openvz.org, jsnow@redhat.com Subject: [Qemu-devel] [PATCH 09/22] qcow2-dirty-bitmap: add qcow2_bitmap_load() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This function loads block dirty bitmap from qcow2. Signed-off-by: Vladimir Sementsov-Ogievskiy --- 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(+) diff --git a/block/qcow2-dirty-bitmap.c b/block/qcow2-dirty-bitmap.c index e57058c..c9f7ef1 100644 --- a/block/qcow2-dirty-bitmap.c +++ b/block/qcow2-dirty-bitmap.c @@ -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); +} diff --git a/block/qcow2.c b/block/qcow2.c index 1ce6264..5f54528 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -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, diff --git a/block/qcow2.h b/block/qcow2.h index 48fb2a5..cc4c776 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -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); diff --git a/include/block/block_int.h b/include/block/block_int.h index dda5ba0..d0e3db8 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -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,