26ba25
From 4122c0993f5b6de8db0227f5751e1f49e0b4354c Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Tue, 20 Nov 2018 18:18:13 +0000
26ba25
Subject: [PATCH 19/35] dirty-bitmap: make it possible to restore bitmap after
26ba25
 merge
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20181120181828.15132-10-jsnow@redhat.com>
26ba25
Patchwork-id: 83057
26ba25
O-Subject: [RHEL8/rhel qemu-kvm PATCH 09/24] dirty-bitmap: make it possible to restore bitmap after merge
26ba25
Bugzilla: 1518989
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
26ba25
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
26ba25
Add backup parameter to bdrv_merge_dirty_bitmap() to be used then with
26ba25
bdrv_restore_dirty_bitmap() if it needed to restore the bitmap after
26ba25
merge operation.
26ba25
26ba25
This is needed to implement bitmap merge transaction action in further
26ba25
commit.
26ba25
26ba25
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
Reviewed-by: John Snow <jsnow@redhat.com>
26ba25
(cherry picked from commit fa000f2f9fd96a75a0a33d50ead247fce11da92a)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 block/dirty-bitmap.c         | 17 ++++++++++++++---
26ba25
 blockdev.c                   |  2 +-
26ba25
 include/block/dirty-bitmap.h |  2 +-
26ba25
 include/qemu/hbitmap.h       | 25 ++++++++++++++++---------
26ba25
 util/hbitmap.c               | 11 ++++++++---
26ba25
 5 files changed, 40 insertions(+), 17 deletions(-)
26ba25
26ba25
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
26ba25
index 017ee9d..8ac933c 100644
26ba25
--- a/block/dirty-bitmap.c
26ba25
+++ b/block/dirty-bitmap.c
26ba25
@@ -314,7 +314,7 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap_locked(BlockDriverState *bs,
26ba25
         return NULL;
26ba25
     }
26ba25
 
26ba25
-    if (!hbitmap_merge(parent->bitmap, successor->bitmap)) {
26ba25
+    if (!hbitmap_merge(parent->bitmap, successor->bitmap, parent->bitmap)) {
26ba25
         error_setg(errp, "Merging of parent and successor bitmap failed");
26ba25
         return NULL;
26ba25
     }
26ba25
@@ -791,8 +791,10 @@ int64_t bdrv_dirty_bitmap_next_zero(BdrvDirtyBitmap *bitmap, uint64_t offset)
26ba25
 }
26ba25
 
26ba25
 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
26ba25
-                             Error **errp)
26ba25
+                             HBitmap **backup, Error **errp)
26ba25
 {
26ba25
+    bool ret;
26ba25
+
26ba25
     /* only bitmaps from one bds are supported */
26ba25
     assert(dest->mutex == src->mutex);
26ba25
 
26ba25
@@ -810,11 +812,20 @@ void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
26ba25
         goto out;
26ba25
     }
26ba25
 
26ba25
-    if (!hbitmap_merge(dest->bitmap, src->bitmap)) {
26ba25
+    if (!hbitmap_can_merge(dest->bitmap, src->bitmap)) {
26ba25
         error_setg(errp, "Bitmaps are incompatible and can't be merged");
26ba25
         goto out;
26ba25
     }
26ba25
 
26ba25
+    if (backup) {
26ba25
+        *backup = dest->bitmap;
26ba25
+        dest->bitmap = hbitmap_alloc(dest->size, hbitmap_granularity(*backup));
26ba25
+        ret = hbitmap_merge(*backup, src->bitmap, dest->bitmap);
26ba25
+    } else {
26ba25
+        ret = hbitmap_merge(dest->bitmap, src->bitmap, dest->bitmap);
26ba25
+    }
26ba25
+    assert(ret);
26ba25
+
26ba25
 out:
26ba25
     qemu_mutex_unlock(dest->mutex);
26ba25
 }
26ba25
diff --git a/blockdev.c b/blockdev.c
26ba25
index 2d86465..a10fbbd 100644
26ba25
--- a/blockdev.c
26ba25
+++ b/blockdev.c
26ba25
@@ -3077,7 +3077,7 @@ void qmp_x_block_dirty_bitmap_merge(const char *node, const char *dst_name,
26ba25
         return;
26ba25
     }
26ba25
 
26ba25
-    bdrv_merge_dirty_bitmap(dst, src, errp);
26ba25
+    bdrv_merge_dirty_bitmap(dst, src, NULL, errp);
26ba25
 }
26ba25
 
26ba25
 BlockDirtyBitmapSha256 *qmp_x_debug_block_dirty_bitmap_sha256(const char *node,
26ba25
diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h
26ba25
index 259bd27..201ff7f 100644
26ba25
--- a/include/block/dirty-bitmap.h
26ba25
+++ b/include/block/dirty-bitmap.h
26ba25
@@ -71,7 +71,7 @@ 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
 void bdrv_merge_dirty_bitmap(BdrvDirtyBitmap *dest, const BdrvDirtyBitmap *src,
26ba25
-                             Error **errp);
26ba25
+                             HBitmap **backup, Error **errp);
26ba25
 
26ba25
 /* Functions that require manual locking.  */
26ba25
 void bdrv_dirty_bitmap_lock(BdrvDirtyBitmap *bitmap);
26ba25
diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h
26ba25
index ddca52c..a7cb780 100644
26ba25
--- a/include/qemu/hbitmap.h
26ba25
+++ b/include/qemu/hbitmap.h
26ba25
@@ -73,16 +73,23 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size);
26ba25
 
26ba25
 /**
26ba25
  * hbitmap_merge:
26ba25
- * @a: The bitmap to store the result in.
26ba25
- * @b: The bitmap to merge into @a.
26ba25
- * @return true if the merge was successful,
26ba25
- *         false if it was not attempted.
26ba25
- *
26ba25
- * Merge two bitmaps together.
26ba25
- * A := A (BITOR) B.
26ba25
- * B is left unmodified.
26ba25
+ *
26ba25
+ * Store result of merging @a and @b into @result.
26ba25
+ * @result is allowed to be equal to @a or @b.
26ba25
+ *
26ba25
+ * Return true if the merge was successful,
26ba25
+ *        false if it was not attempted.
26ba25
+ */
26ba25
+bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result);
26ba25
+
26ba25
+/**
26ba25
+ * hbitmap_can_merge:
26ba25
+ *
26ba25
+ * hbitmap_can_merge(a, b) && hbitmap_can_merge(a, result) is sufficient and
26ba25
+ * necessary for hbitmap_merge will not fail.
26ba25
+ *
26ba25
  */
26ba25
-bool hbitmap_merge(HBitmap *a, const HBitmap *b);
26ba25
+bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b);
26ba25
 
26ba25
 /**
26ba25
  * hbitmap_empty:
26ba25
diff --git a/util/hbitmap.c b/util/hbitmap.c
26ba25
index bcd3040..d5aca51 100644
26ba25
--- a/util/hbitmap.c
26ba25
+++ b/util/hbitmap.c
26ba25
@@ -723,6 +723,10 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size)
26ba25
     }
26ba25
 }
26ba25
 
26ba25
+bool hbitmap_can_merge(const HBitmap *a, const HBitmap *b)
26ba25
+{
26ba25
+    return (a->size == b->size) && (a->granularity == b->granularity);
26ba25
+}
26ba25
 
26ba25
 /**
26ba25
  * Given HBitmaps A and B, let A := A (BITOR) B.
26ba25
@@ -731,14 +735,15 @@ void hbitmap_truncate(HBitmap *hb, uint64_t size)
26ba25
  * @return true if the merge was successful,
26ba25
  *         false if it was not attempted.
26ba25
  */
26ba25
-bool hbitmap_merge(HBitmap *a, const HBitmap *b)
26ba25
+bool hbitmap_merge(const HBitmap *a, const HBitmap *b, HBitmap *result)
26ba25
 {
26ba25
     int i;
26ba25
     uint64_t j;
26ba25
 
26ba25
-    if ((a->size != b->size) || (a->granularity != b->granularity)) {
26ba25
+    if (!hbitmap_can_merge(a, b) || !hbitmap_can_merge(a, result)) {
26ba25
         return false;
26ba25
     }
26ba25
+    assert(hbitmap_can_merge(b, result));
26ba25
 
26ba25
     if (hbitmap_count(b) == 0) {
26ba25
         return true;
26ba25
@@ -750,7 +755,7 @@ bool hbitmap_merge(HBitmap *a, const HBitmap *b)
26ba25
      */
26ba25
     for (i = HBITMAP_LEVELS - 1; i >= 0; i--) {
26ba25
         for (j = 0; j < a->sizes[i]; j++) {
26ba25
-            a->levels[i][j] |= b->levels[i][j];
26ba25
+            result->levels[i][j] = a->levels[i][j] | b->levels[i][j];
26ba25
         }
26ba25
     }
26ba25
 
26ba25
-- 
26ba25
1.8.3.1
26ba25