From patchwork Mon Aug 8 15:04:57 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: 9268543 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3DF6C60754 for ; Mon, 8 Aug 2016 15:10:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2E09B283F7 for ; Mon, 8 Aug 2016 15:10:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2273F283FE; Mon, 8 Aug 2016 15:10:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id 976E7283F7 for ; Mon, 8 Aug 2016 15:10:42 +0000 (UTC) Received: from localhost ([::1]:58011 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWmCP-00034e-9z for patchwork-qemu-devel@patchwork.kernel.org; Mon, 08 Aug 2016 11:10:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42496) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWm7V-00071O-Db for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:05:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bWm7Q-0004TY-BI for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:05:37 -0400 Received: from mailhub.sw.ru ([195.214.232.25]:32872 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bWm7P-0004Pr-P8 for qemu-devel@nongnu.org; Mon, 08 Aug 2016 11:05:32 -0400 Received: from kvm.qa.sw.ru. ([10.28.8.145]) by relay.sw.ru (8.13.4/8.13.4) with ESMTP id u77NfZcI001857; Mon, 8 Aug 2016 02:41:37 +0300 (MSK) From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Mon, 8 Aug 2016 18:04:57 +0300 Message-Id: <1470668720-211300-7-git-send-email-vsementsov@virtuozzo.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1470668720-211300-1-git-send-email-vsementsov@virtuozzo.com> References: <1470668720-211300-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 Subject: [Qemu-devel] [PATCH 06/29] qcow2-bitmap: add qcow2_read_bitmaps() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, famz@redhat.com, armbru@redhat.com, mreitz@redhat.com, stefanha@redhat.com, pbonzini@redhat.com, den@openvz.org, jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add qcow2_read_bitmaps, reading bitmap directory as specified in docs/specs/qcow2.txt Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-bitmap.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.h | 9 +++++ 2 files changed, 109 insertions(+) diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index cd18b07..91ddd5f 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -25,6 +25,12 @@ * THE SOFTWARE. */ +#include "qemu/osdep.h" +#include "qapi/error.h" + +#include "block/block_int.h" +#include "block/qcow2.h" + /* NOTICE: BME here means Bitmaps Extension and used as a namespace for * _internal_ constants. Please do not use this _internal_ abbreviation for * other needs and/or outside of this file. */ @@ -42,6 +48,100 @@ /* bits [1, 8] U [56, 63] are reserved */ #define BME_TABLE_ENTRY_RESERVED_MASK 0xff000000000001fe +#define for_each_bitmap_header_in_dir(h, dir, size) \ + for (h = (QCow2BitmapHeader *)(dir); \ + h < (QCow2BitmapHeader *)((uint8_t *)(dir) + size); \ + h = next_dir_entry(h)) + typedef enum BitmapType { BT_DIRTY_TRACKING_BITMAP = 1 } BitmapType; + +static inline void bitmap_header_to_cpu(QCow2BitmapHeader *h) +{ + be64_to_cpus(&h->bitmap_table_offset); + be32_to_cpus(&h->bitmap_table_size); + be32_to_cpus(&h->flags); + be16_to_cpus(&h->name_size); + be32_to_cpus(&h->extra_data_size); +} + +static inline int calc_dir_entry_size(size_t name_size) +{ + return align_offset(sizeof(QCow2BitmapHeader) + name_size, 8); +} + +static inline int dir_entry_size(QCow2BitmapHeader *h) +{ + return calc_dir_entry_size(h->name_size); +} + +static inline QCow2BitmapHeader *next_dir_entry(QCow2BitmapHeader *entry) +{ + return (QCow2BitmapHeader *)((uint8_t *)entry + dir_entry_size(entry)); +} + +/* directory_read + * Read bitmaps directory from bs by @offset and @size. Convert it to cpu + * format from BE. + */ +static uint8_t *directory_read(BlockDriverState *bs, + uint64_t offset, uint64_t size, Error **errp) +{ + int ret; + uint8_t *dir; + QCow2BitmapHeader *h; + + dir = g_try_malloc0(size); + if (dir == NULL) { + error_setg(errp, "Can't allocate space for bitmap directory."); + return NULL; + } + + ret = bdrv_pread(bs->file, offset, dir, size); + if (ret < 0) { + error_setg_errno(errp, -ret, "Can't read bitmap directory."); + goto fail; + } + + /* loop is safe because next entry offset is calculated after conversion to + * cpu format */ + for_each_bitmap_header_in_dir(h, dir, size) { + bitmap_header_to_cpu(h); + } + + if ((uint8_t *)h != dir + size) { + error_setg(errp, "Broken bitmap directory."); + goto fail; + } + + return dir; + +fail: + g_free(dir); + + return NULL; +} + +int qcow2_read_bitmaps(BlockDriverState *bs, Error **errp) +{ + BDRVQcow2State *s = bs->opaque; + + if (s->bitmap_directory != NULL) { + error_setg(errp, "Try read bitmaps, when they are already read."); + return -EEXIST; + } + + if (s->nb_bitmaps == 0) { + /* No bitmaps - nothing to do */ + return 0; + } + + s->bitmap_directory = directory_read(bs, s->bitmap_directory_offset, + s->bitmap_directory_size, errp); + if (s->bitmap_directory == NULL) { + return -EINVAL; + } + + return 0; +} diff --git a/block/qcow2.h b/block/qcow2.h index b12cecc..7f6e023 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -292,6 +292,11 @@ typedef struct BDRVQcow2State { unsigned int nb_snapshots; QCowSnapshot *snapshots; + uint64_t bitmap_directory_offset; + uint64_t bitmap_directory_size; + uint8_t *bitmap_directory; + unsigned int nb_bitmaps; + int flags; int qcow_version; bool use_lazy_refcounts; @@ -599,6 +604,10 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, void qcow2_free_snapshots(BlockDriverState *bs); int qcow2_read_snapshots(BlockDriverState *bs); +/* qcow2-bitmap.c functions */ +void qcow2_free_bitmaps(BlockDriverState *bs); +int qcow2_read_bitmaps(BlockDriverState *bs, Error **errp); + /* qcow2-cache.c functions */ Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables); int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);