qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
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 13/29] qcow2: add dirty bitmaps extension
Date: Mon,  8 Aug 2016 18:05:04 +0300	[thread overview]
Message-ID: <1470668720-211300-14-git-send-email-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <1470668720-211300-1-git-send-email-vsementsov@virtuozzo.com>

Add dirty bitmap extension as specified in docs/specs/qcow2.txt.

Load bitmap headers on open. Handle close and update_header.

Handle resize: just delete all bitmaps from the image. Not loaded
bitmaps will be lost (for now - all except autoloading ones). This may
be fixed in future.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 block/qcow2-bitmap.c |  8 +++++
 block/qcow2.c        | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+)

diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
index 92d95c5..a34cfe8 100644
--- a/block/qcow2-bitmap.c
+++ b/block/qcow2-bitmap.c
@@ -68,6 +68,14 @@ static inline bool can_write(BlockDriverState *bs)
     return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
 }
 
+void qcow2_free_bitmaps(BlockDriverState *bs)
+{
+    BDRVQcow2State *s = bs->opaque;
+
+    g_free(s->bitmap_directory);
+    s->bitmap_directory = NULL;
+}
+
 static inline void bitmap_header_to_cpu(QCow2BitmapHeader *h)
 {
     be64_to_cpus(&h->bitmap_table_offset);
diff --git a/block/qcow2.c b/block/qcow2.c
index bad7b83..3e9b47e 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -63,6 +63,7 @@ typedef struct {
 #define  QCOW2_EXT_MAGIC_END 0
 #define  QCOW2_EXT_MAGIC_BACKING_FORMAT 0xE2792ACA
 #define  QCOW2_EXT_MAGIC_FEATURE_TABLE 0x6803f857
+#define  QCOW2_EXT_MAGIC_DIRTY_BITMAPS 0x23852875
 
 static int qcow2_probe(const uint8_t *buf, int buf_size, const char *filename)
 {
@@ -92,6 +93,7 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
     QCowExtension ext;
     uint64_t offset;
     int ret;
+    Qcow2BitmapHeaderExt bitmaps_ext;
 
 #ifdef DEBUG_EXT
     printf("qcow2_read_extensions: start=%ld end=%ld\n", start_offset, end_offset);
@@ -162,6 +164,67 @@ static int qcow2_read_extensions(BlockDriverState *bs, uint64_t start_offset,
             }
             break;
 
+        case QCOW2_EXT_MAGIC_DIRTY_BITMAPS:
+            ret = bdrv_pread(bs->file, offset, &bitmaps_ext, ext.len);
+            if (ret < 0) {
+                error_setg_errno(errp, -ret, "ERROR: bitmaps_ext: "
+                                 "Could not read ext header");
+                return ret;
+            }
+
+            if (bitmaps_ext.reserved32 != 0) {
+                error_setg_errno(errp, -ret, "ERROR: bitmaps_ext: "
+                                 "Reserved field is not zero.");
+                return -EINVAL;
+            }
+
+            be32_to_cpus(&bitmaps_ext.nb_bitmaps);
+            be64_to_cpus(&bitmaps_ext.bitmap_directory_size);
+            be64_to_cpus(&bitmaps_ext.bitmap_directory_offset);
+
+            if (bitmaps_ext.nb_bitmaps > QCOW_MAX_DIRTY_BITMAPS) {
+                error_setg(errp, "ERROR: bitmaps_ext: "
+                                 "too many dirty bitmaps");
+                return -EINVAL;
+            }
+
+            if (bitmaps_ext.nb_bitmaps == 0) {
+                error_setg(errp, "ERROR: bitmaps_ext: "
+                                 "found bitmaps extension with zero bitmaps");
+                return -EINVAL;
+            }
+
+            if (bitmaps_ext.bitmap_directory_offset & (s->cluster_size - 1)) {
+                error_setg(errp, "ERROR: bitmaps_ext: "
+                                 "wrong dirty bitmap directory offset");
+                return -EINVAL;
+            }
+
+            if (bitmaps_ext.bitmap_directory_size >
+                QCOW_MAX_DIRTY_BITMAP_DIRECTORY_SIZE) {
+                error_setg(errp, "ERROR: bitmaps_ext: "
+                                 "too large dirty bitmap directory");
+                return -EINVAL;
+            }
+
+            s->nb_bitmaps = bitmaps_ext.nb_bitmaps;
+            s->bitmap_directory_offset =
+                    bitmaps_ext.bitmap_directory_offset;
+            s->bitmap_directory_size =
+                    bitmaps_ext.bitmap_directory_size;
+
+            ret = qcow2_read_bitmaps(bs, errp);
+            if (ret < 0) {
+                return ret;
+            }
+
+#ifdef DEBUG_EXT
+            printf("Qcow2: Got dirty bitmaps extension:"
+                   " offset=%" PRIu64 " nb_bitmaps=%" PRIu32 "\n",
+                   s->bitmap_directory_offset, s->nb_bitmaps);
+#endif
+            break;
+
         default:
             /* unknown magic - save it in case we need to rewrite the header */
             {
@@ -1179,6 +1242,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
     qcow2_free_snapshots(bs);
+    qcow2_free_bitmaps(bs);
     qcow2_refcount_close(bs);
     qemu_vfree(s->l1_table);
     /* else pre-write overlap checks in cache_destroy may crash */
@@ -1749,6 +1813,7 @@ static void qcow2_close(BlockDriverState *bs)
     qemu_vfree(s->cluster_data);
     qcow2_refcount_close(bs);
     qcow2_free_snapshots(bs);
+    qcow2_free_bitmaps(bs);
 }
 
 static void qcow2_invalidate_cache(BlockDriverState *bs, Error **errp)
@@ -1939,6 +2004,24 @@ int qcow2_update_header(BlockDriverState *bs)
         buflen -= ret;
     }
 
+    if (s->nb_bitmaps > 0) {
+        Qcow2BitmapHeaderExt bitmaps_header = {
+            .nb_bitmaps = cpu_to_be32(s->nb_bitmaps),
+            .bitmap_directory_size =
+                    cpu_to_be64(s->bitmap_directory_size),
+            .bitmap_directory_offset =
+                    cpu_to_be64(s->bitmap_directory_offset)
+        };
+        ret = header_ext_add(buf, QCOW2_EXT_MAGIC_DIRTY_BITMAPS,
+                             &bitmaps_header, sizeof(bitmaps_header),
+                             buflen);
+        if (ret < 0) {
+            goto fail;
+        }
+        buf += ret;
+        buflen -= ret;
+    }
+
     /* Keep unknown header extensions */
     QLIST_FOREACH(uext, &s->unknown_header_ext, next) {
         ret = header_ext_add(buf, uext->magic, uext->data, uext->len, buflen);
@@ -2509,6 +2592,16 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset)
         return -ENOTSUP;
     }
 
+    /* cannot proceed if image has bitmaps */
+    if (s->nb_bitmaps) {
+        /* FIXME: not loaded bitmaps will be lost */
+        ret = qcow2_delete_bitmaps(bs);
+        if (ret < 0) {
+            error_report("Can't remove bitmaps from qcow2 on truncate");
+            return ret;
+        }
+    }
+
     /* shrinking is currently not supported */
     if (offset < bs->total_sectors * 512) {
         error_report("qcow2 doesn't support shrinking images yet");
-- 
1.8.3.1

  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 ` [Qemu-devel] [PATCH 06/29] qcow2-bitmap: add qcow2_read_bitmaps() Vladimir Sementsov-Ogievskiy
2016-08-11  9:36   ` 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 ` Vladimir Sementsov-Ogievskiy [this message]
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-14-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).