From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org, qemu-devel@nongnu.org
Cc: kwolf@redhat.com, mreitz@redhat.com, armbru@redhat.com,
eblake@redhat.com, jsnow@redhat.com, famz@redhat.com,
den@openvz.org, stefanha@redhat.com, vsementsov@virtuozzo.com,
pbonzini@redhat.com
Subject: [Qemu-devel] [PATCH 06/29] qcow2-bitmap: add qcow2_read_bitmaps()
Date: Mon, 8 Aug 2016 18:04:57 +0300 [thread overview]
Message-ID: <1470668720-211300-7-git-send-email-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <1470668720-211300-1-git-send-email-vsementsov@virtuozzo.com>
Add qcow2_read_bitmaps, reading bitmap directory as specified in
docs/specs/qcow2.txt
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
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);
--
1.8.3.1
next prev parent reply other threads:[~2016-08-08 15:05 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-08-08 15:04 [Qemu-devel] [PATCH v6 00/29] qcow2: persistent dirty bitmaps Vladimir Sementsov-Ogievskiy
2016-08-08 15:04 ` [Qemu-devel] [PATCH 01/29] hbitmap: fix dirty iter Vladimir Sementsov-Ogievskiy
2016-08-10 13:41 ` Kevin Wolf
2016-08-10 13:59 ` Vladimir Sementsov-Ogievskiy
2016-08-08 15:04 ` [Qemu-devel] [PATCH 02/29] tests: add hbitmap iter test Vladimir Sementsov-Ogievskiy
2016-08-08 15:04 ` [Qemu-devel] [PATCH 03/29] block: fix bdrv_dirty_bitmap_granularity signature Vladimir Sementsov-Ogievskiy
2016-08-10 13:42 ` Kevin Wolf
2016-08-08 15:04 ` [Qemu-devel] [PATCH 04/29] block/dirty-bitmap: add deserialize_ones func Vladimir Sementsov-Ogievskiy
2016-08-10 13:49 ` Kevin Wolf
2016-08-08 15:04 ` [Qemu-devel] [PATCH 05/29] qcow2-bitmap: structs and consts Vladimir Sementsov-Ogievskiy
2016-08-11 9:09 ` Kevin Wolf
2016-08-08 15:04 ` Vladimir Sementsov-Ogievskiy [this message]
2016-08-11 9:36 ` [Qemu-devel] [PATCH 06/29] qcow2-bitmap: add qcow2_read_bitmaps() Kevin Wolf
2016-08-11 12:00 ` Vladimir Sementsov-Ogievskiy
2016-08-11 12:54 ` Kevin Wolf
2016-08-08 15:04 ` [Qemu-devel] [PATCH 07/29] qcow2-bitmap: add qcow2_bitmap_load() Vladimir Sementsov-Ogievskiy
2016-08-11 13:00 ` Kevin Wolf
2016-08-08 15:04 ` [Qemu-devel] [PATCH 08/29] qcow2-bitmap: delete bitmap from qcow2 after load Vladimir Sementsov-Ogievskiy
2016-08-11 13:18 ` Kevin Wolf
2016-08-30 15:03 ` Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 09/29] qcow2-bitmap: add qcow2_bitmap_store() Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 10/29] qcow2-bitmap: add IN_USE flag Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 11/29] qcow2-bitmap: check constraints Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 12/29] qcow2: add qcow2_delete_bitmaps Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 13/29] qcow2: add dirty bitmaps extension Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 14/29] qcow2-bitmap: add qcow2_bitmap_load_check() Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 15/29] block/dirty-bitmap: introduce persistent bitmaps Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 16/29] block: add bdrv_load_dirty_bitmap() Vladimir Sementsov-Ogievskiy
2016-08-11 11:24 ` Kevin Wolf
2016-08-11 11:29 ` Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 17/29] qcow2-bitmap: add autoclear bit Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 18/29] qcow2-bitmap: disallow storing bitmap to other bs Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 19/29] block/dirty-bitmap: add autoload field to BdrvDirtyBitmap Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 20/29] qcow2-bitmap: add AUTO flag Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 21/29] qcow2-bitmap: add EXTRA_DATA_COMPATIBLE flag Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 22/29] qmp: add persistent flag to block-dirty-bitmap-add Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 23/29] qmp: add autoload parameter " Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 24/29] qcow2-bitmap: maintian BlockDirtyBitmap.autoload Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 25/29] qapi: add md5 checksum of last dirty bitmap level to query-block Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 26/29] iotests: test qcow2 persistent dirty bitmap Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 27/29] qcow2-bitmap: delete in_use bitmaps on image load Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 28/29] qcow2-bitmap: do not try reloading bitmaps Vladimir Sementsov-Ogievskiy
2016-08-08 15:05 ` [Qemu-devel] [PATCH 29/29] qcow2-dirty-bitmap: refcounts Vladimir Sementsov-Ogievskiy
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=1470668720-211300-7-git-send-email-vsementsov@virtuozzo.com \
--to=vsementsov@virtuozzo.com \
--cc=armbru@redhat.com \
--cc=den@openvz.org \
--cc=eblake@redhat.com \
--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).