From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-devel@nongnu.org
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 08/22] qcow2-dirty-bitmap: read dirty bitmap directory
Date: Tue, 15 Mar 2016 23:04:14 +0300 [thread overview]
Message-ID: <1458072268-53705-9-git-send-email-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <1458072268-53705-1-git-send-email-vsementsov@virtuozzo.com>
Adds qcow2_read_bitmaps, reading bitmap directory as
specified in docs/specs/qcow2.txt
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
block/qcow2-dirty-bitmap.c | 165 +++++++++++++++++++++++++++++++++++++++++++++
block/qcow2.h | 10 +++
2 files changed, 175 insertions(+)
diff --git a/block/qcow2-dirty-bitmap.c b/block/qcow2-dirty-bitmap.c
index 2c749ab..e57058c 100644
--- a/block/qcow2-dirty-bitmap.c
+++ b/block/qcow2-dirty-bitmap.c
@@ -25,6 +25,11 @@
* THE SOFTWARE.
*/
+#include "qemu/osdep.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. */
@@ -45,3 +50,163 @@
typedef enum BitmapType {
BT_DIRTY_TRACKING_BITMAP = 1
} BitmapType;
+
+void qcow2_free_bitmaps(BlockDriverState *bs)
+{
+ BDRVQcow2State *s = bs->opaque;
+ int i;
+
+ for (i = 0; i < s->nb_bitmaps; i++) {
+ g_free(s->bitmaps[i].name);
+ }
+ g_free(s->bitmaps);
+ s->bitmaps = NULL;
+ s->nb_bitmaps = 0;
+
+ g_free(s->bitmap_directory);
+ s->bitmap_directory = NULL;
+}
+
+static 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 int calc_dir_entry_size(size_t name_size)
+{
+ return align_offset(sizeof(QCow2BitmapHeader) + name_size, 8);
+}
+
+static int dir_entry_size(QCow2BitmapHeader *h)
+{
+ return calc_dir_entry_size(h->name_size);
+}
+
+static int check_constraints(QCow2BitmapHeader *h, int cluster_size,
+ uint64_t disk_size)
+{
+ uint64_t phys_bitmap_bytes =
+ (uint64_t)h->bitmap_table_size * cluster_size;
+ uint64_t max_virtual_bits = (phys_bitmap_bytes * 8) << h->granularity_bits;
+
+ int fail =
+ (h->bitmap_table_offset % cluster_size) ||
+ (h->bitmap_table_size > BME_MAX_TABLE_SIZE) ||
+ (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) ||
+ (disk_size > max_virtual_bits) ||
+ (h->granularity_bits > BME_MAX_GRANULARITY_BITS) ||
+ (h->granularity_bits < BME_MIN_GRANULARITY_BITS) ||
+ (h->flags & BME_RESERVED_FLAGS) ||
+ (h->name_size > BME_MAX_NAME_SIZE) ||
+ (h->type != BT_DIRTY_TRACKING_BITMAP);
+
+ return fail ? -EINVAL : 0;
+}
+
+static int directory_read(BlockDriverState *bs, Error **errp)
+{
+ int ret;
+ BDRVQcow2State *s = bs->opaque;
+ QCow2Bitmap *bm, *end;
+ int64_t nb_sectors = bdrv_nb_sectors(bs);
+ size_t offset;
+
+ if (nb_sectors < 0) {
+ error_setg(errp, "Can't calculate number of disk sectors.");
+ return nb_sectors;
+ }
+
+ if (s->bitmap_directory != NULL) {
+ /* already read */
+ error_setg(errp, "Try read bitmaps, when they are already read.");
+ return -EEXIST;
+ }
+
+ s->bitmap_directory = g_try_malloc0(s->bitmap_directory_size);
+ if (s->bitmap_directory == NULL) {
+ error_setg(errp, "Can't allocate space for bitmap directory.");
+ return -ENOMEM;
+ }
+
+ ret = bdrv_pread(bs->file->bs,
+ s->bitmap_directory_offset,
+ s->bitmap_directory,
+ s->bitmap_directory_size);
+ if (ret < 0) {
+ error_setg(errp, "Can't read bitmap directory.");
+ goto fail;
+ }
+
+ offset = 0;
+ end = s->bitmaps + s->nb_bitmaps;
+ for (bm = s->bitmaps; bm < end; ++bm) {
+ QCow2BitmapHeader *h =
+ (QCow2BitmapHeader *)(s->bitmap_directory + offset);
+
+ if (offset >= s->bitmap_directory_size) {
+ error_setg(errp, "Broken bitmap directory.");
+ goto fail;
+ }
+
+ bitmap_header_to_cpu(h);
+
+ ret = check_constraints(h, s->cluster_size,
+ nb_sectors << BDRV_SECTOR_BITS);
+ if (ret < 0) {
+ error_setg(errp, "Bitmap doesn't satisfy the constraints.");
+ goto fail;
+ }
+
+ bm->offset = offset;
+ bm->name = g_strndup((char *)(h + 1), h->name_size);
+
+ offset += dir_entry_size(h);
+ }
+ return 0;
+
+fail:
+ g_free(s->bitmap_directory);
+ s->bitmap_directory = NULL;
+
+ return ret;
+}
+
+int qcow2_read_bitmaps(BlockDriverState *bs, Error **errp)
+{
+ int ret;
+ BDRVQcow2State *s = bs->opaque;
+
+ if (s->bitmap_directory != NULL || s->bitmaps != NULL) {
+ /* already read */
+ 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->bitmaps = g_try_new0(QCow2Bitmap, s->nb_bitmaps);
+ if (s->bitmaps == NULL) {
+ error_setg(errp, "Can't allocate space for qcow2 bitmaps.");
+ ret = -ENOMEM;
+ goto fail;
+ }
+
+ ret = directory_read(bs, errp);
+ if (ret < 0) {
+ goto fail;
+ }
+
+ return 0;
+
+fail:
+ qcow2_free_bitmaps(bs);
+
+ return ret;
+}
diff --git a/block/qcow2.h b/block/qcow2.h
index 3f7429e..48fb2a5 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -297,6 +297,12 @@ 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;
+ QCow2Bitmap *bitmaps;
+
int flags;
int qcow_version;
bool use_lazy_refcounts;
@@ -610,6 +616,10 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs,
void qcow2_free_snapshots(BlockDriverState *bs);
int qcow2_read_snapshots(BlockDriverState *bs);
+/* qcow2-dirty-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);
--
1.8.3.1
next prev parent reply other threads:[~2016-03-15 20:05 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-15 20:04 [Qemu-devel] [PATCH v5 00/22] qcow2: persistent dirty bitmaps Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 01/22] block: Add two dirty bitmap getters Vladimir Sementsov-Ogievskiy
2016-03-15 20:08 ` Vladimir Sementsov-Ogievskiy
2016-03-22 16:37 ` Eric Blake
2016-03-22 18:06 ` John Snow
2016-03-15 20:04 ` [Qemu-devel] [PATCH 02/22] block: fix bdrv_dirty_bitmap_granularity signature Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 03/22] iotests: maintain several vms in test Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 04/22] iotests: add default node-name Vladimir Sementsov-Ogievskiy
2016-03-22 18:08 ` John Snow
2016-03-15 20:04 ` [Qemu-devel] [PATCH 05/22] qapi: add md5 checksum of last dirty bitmap level to query-block Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 06/22] hbitmap: load/store Vladimir Sementsov-Ogievskiy
2016-03-21 22:42 ` John Snow
2016-03-22 10:47 ` Vladimir Sementsov-Ogievskiy
2016-03-22 21:49 ` John Snow
2016-03-23 8:22 ` Vladimir Sementsov-Ogievskiy
2016-03-28 20:20 ` John Snow
2016-03-15 20:04 ` [Qemu-devel] [PATCH 07/22] qcow2: Bitmaps extension: structs and consts Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` Vladimir Sementsov-Ogievskiy [this message]
2016-03-15 20:04 ` [Qemu-devel] [PATCH 09/22] qcow2-dirty-bitmap: add qcow2_bitmap_load() Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 10/22] qcow2-dirty-bitmap: add qcow2_bitmap_store() Vladimir Sementsov-Ogievskiy
2016-03-22 18:49 ` Eric Blake
2016-03-23 8:25 ` Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 11/22] qcow2: add dirty bitmaps extension Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 12/22] qcow2-dirty-bitmap: add qcow2_bitmap_load_check() Vladimir Sementsov-Ogievskiy
2016-03-22 18:49 ` Eric Blake
2016-03-15 20:04 ` [Qemu-devel] [PATCH 13/22] block: store persistent dirty bitmaps Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 14/22] block: add bdrv_load_dirty_bitmap() Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 15/22] qcow2-dirty-bitmap: add autoclear bit Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 16/22] qemu: command line option for dirty bitmaps Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 17/22] qcow2-dirty-bitmap: add IN_USE flag Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 18/22] qcow2-dirty-bitmaps: disallow stroing bitmap to other bs Vladimir Sementsov-Ogievskiy
2016-03-22 18:51 ` Eric Blake
2016-03-15 20:04 ` [Qemu-devel] [PATCH 19/22] iotests: add VM.test_launcn() Vladimir Sementsov-Ogievskiy
2016-03-22 18:25 ` Eric Blake
2016-03-15 20:04 ` [Qemu-devel] [PATCH 20/22] iotests: test internal persistent dirty bitmap Vladimir Sementsov-Ogievskiy
2016-03-22 18:27 ` Eric Blake
2016-03-23 8:28 ` Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 21/22] qcow2-dirty-bitmap: add AUTO flag Vladimir Sementsov-Ogievskiy
2016-03-15 20:04 ` [Qemu-devel] [PATCH 22/22] qcow2-dirty-bitmap: add EXTRA_DATA_COMPATIBLE flag Vladimir Sementsov-Ogievskiy
2016-03-22 18:51 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1458072268-53705-9-git-send-email-vsementsov@virtuozzo.com \
--to=vsementsov@virtuozzo.com \
--cc=den@openvz.org \
--cc=famz@redhat.com \
--cc=jsnow@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).