26ba25
From 61f7fec68d66c61a6d3058991470cce978fe1e10 Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Wed, 18 Jul 2018 22:54:40 +0200
26ba25
Subject: [PATCH 222/268] qapi: add x-block-dirty-bitmap-merge
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20180718225511.14878-5-jsnow@redhat.com>
26ba25
Patchwork-id: 81397
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 04/35] qapi: add x-block-dirty-bitmap-merge
26ba25
Bugzilla: 1207657
26ba25
RH-Acked-by: Eric Blake <eblake@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Fam Zheng <famz@redhat.com>
26ba25
26ba25
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
26ba25
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Reviewed-by: Jeff Cody <jcody@redhat.com>
26ba25
Message-id: 20180606182449.1607-5-jsnow@redhat.com
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
(cherry picked from commit b598e531f1123d2fb72615f1161c66093be751ea)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block/dirty-bitmap.c         | 18 ++++++++++++++++++
26ba25
 blockdev.c                   | 30 ++++++++++++++++++++++++++++++
26ba25
 include/block/dirty-bitmap.h |  3 ++-
26ba25
 qapi/block-core.json         | 38 ++++++++++++++++++++++++++++++++++++++
26ba25
 4 files changed, 88 insertions(+), 1 deletion(-)
26ba25
26ba25
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
26ba25
index 5623425..4159d39 100644
26ba25
--- a/block/dirty-bitmap.c
26ba25
+++ b/block/dirty-bitmap.c
26ba25
@@ -757,3 +757,21 @@ int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
26ba25
 {
26ba25
     return hbitmap_next_zero(bitmap->bitmap, offset);
26ba25
 }
26ba25
+
26ba25
+void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
26ba25
+                             Error **errp)
26ba25
+{
26ba25
+    /* only bitmaps from one bds are supported */
26ba25
+    assert(dest->mutex == src->mutex);
26ba25
+
26ba25
+    qemu_mutex_lock(dest->mutex);
26ba25
+
26ba25
+    assert(bdrv_dirty_bitmap_enabled(dest));
26ba25
+    assert(!bdrv_dirty_bitmap_readonly(dest));
26ba25
+
26ba25
+    if (!hbitmap_merge(dest->bitmap, src->bitmap)) {
26ba25
+        error_setg(errp, "Bitmaps are incompatible and can't be merged");
26ba25
+    }
26ba25
+
26ba25
+    qemu_mutex_unlock(dest->mutex);
26ba25
+}
26ba25
diff --git a/blockdev.c b/blockdev.c
26ba25
index b74b7d4..837183c 100644
26ba25
--- a/blockdev.c
26ba25
+++ b/blockdev.c
26ba25
@@ -3045,6 +3045,36 @@ void qmp_x_block_dirty_bitmap_disable(const char *node, const char *name,
26ba25
     bdrv_disable_dirty_bitmap(bitmap);
26ba25
 }
26ba25
 
26ba25
+void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name,
26ba25
+                                    const char *src_name, Error **errp)
26ba25
+{
26ba25
+    BlockDriverState *bs;
26ba25
+    BdrvDirtyBitmap *dst, *src;
26ba25
+
26ba25
+    dst = block_dirty_bitmap_lookup(node, dst_name, &bs, errp);
26ba25
+    if (!dst) {
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    if (bdrv_dirty_bitmap_frozen(dst)) {
26ba25
+        error_setg(errp, "Bitmap '%s' is frozen and cannot be modified",
26ba25
+                   dst_name);
26ba25
+        return;
26ba25
+    } else if (bdrv_dirty_bitmap_readonly(dst)) {
26ba25
+        error_setg(errp, "Bitmap '%s' is readonly and cannot be modified",
26ba25
+                   dst_name);
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    src = bdrv_find_dirty_bitmap(bs, src_name);
26ba25
+    if (!src) {
26ba25
+        error_setg(errp, "Dirty bitmap '%s' not found", src_name);
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    bdrv_merge_dirty_bitmap(dst, src, errp);
26ba25
+}
26ba25
+
26ba25
 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
26ba25
                                                               const char *name,
26ba25
                                                               Error **errp)
26ba25
diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h
26ba25
index 1ff8949..1e14743 100644
26ba25
--- a/include/block/dirty-bitmap.h
26ba25
+++ b/include/block/dirty-bitmap.h
26ba25
@@ -70,7 +70,8 @@ void bdrv_dirty_bitmap_set_readonly(BdrvDirtyBitmap *bitmap, bool value);
26ba25
 void bdrv_dirty_bitmap_set_persistance(BdrvDirtyBitmap *bitmap,
26ba25
                                        bool persistent);
26ba25
 void bdrv_dirty_bitmap_set_qmp_locked(BdrvDirtyBitmap *bitmap, bool qmp_locked);
26ba25
-
26ba25
+void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
26ba25
+                             Error **errp);
26ba25
 
26ba25
 /* Functions that require manual locking.  */
26ba25
 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap);
26ba25
diff --git a/qapi/block-core.json b/qapi/block-core.json
26ba25
index 6f3fdf4..50a2763 100644
26ba25
--- a/qapi/block-core.json
26ba25
+++ b/qapi/block-core.json
26ba25
@@ -1741,6 +1741,20 @@
26ba25
             '*persistent': 'bool', '*autoload': 'bool' } }
26ba25
 
26ba25
 ##
26ba25
+# @BlockDirtyBitmapMerge:
26ba25
+#
26ba25
+# @node: name of device/node which the bitmap is tracking
26ba25
+#
26ba25
+# @dst_name: name of the destination dirty bitmap
26ba25
+#
26ba25
+# @src_name: name of the source dirty bitmap
26ba25
+#
26ba25
+# Since: 3.0
26ba25
+##
26ba25
+{ 'struct': 'BlockDirtyBitmapMerge',
26ba25
+  'data': { 'node': 'str', 'dst_name': 'str', 'src_name': 'str' } }
26ba25
+
26ba25
+##
26ba25
 # @block-dirty-bitmap-add:
26ba25
 #
26ba25
 # Create a dirty bitmap with a name on the node, and start tracking the writes.
26ba25
@@ -1851,6 +1865,30 @@
26ba25
       'data': 'BlockDirtyBitmap' }
26ba25
 
26ba25
 ##
26ba25
+# @x-block-dirty-bitmap-merge:
26ba25
+#
26ba25
+# Merge @src_name dirty bitmap to @dst_name dirty bitmap. @src_name dirty
26ba25
+# bitmap is unchanged. On error, @dst_name is unchanged.
26ba25
+#
26ba25
+# Returns: nothing on success
26ba25
+#          If @node is not a valid block device, DeviceNotFound
26ba25
+#          If @dst_name or @src_name is not found, GenericError
26ba25
+#          If bitmaps has different sizes or granularities, GenericError
26ba25
+#
26ba25
+# Since: 3.0
26ba25
+#
26ba25
+# Example:
26ba25
+#
26ba25
+# -> { "execute": "x-block-dirty-bitmap-merge",
26ba25
+#      "arguments": { "node": "drive0", "dst_name": "bitmap0",
26ba25
+#                     "src_name": "bitmap1" } }
26ba25
+# <- { "return": {} }
26ba25
+#
26ba25
+##
26ba25
+      { 'command': 'x-block-dirty-bitmap-merge',
26ba25
+        'data': 'BlockDirtyBitmapMerge' }
26ba25
+
26ba25
+##
26ba25
 # @BlockDirtyBitmapSha256:
26ba25
 #
26ba25
 # SHA256 hash of dirty bitmap data
26ba25
-- 
26ba25
1.8.3.1
26ba25