Blame SOURCES/kvm-block-dirty-bitmap-Add-bdrv_dirty_iter_next_area.patch

383d26
From 7e539b23b6911cb38bf70dcbf818acbc10790816 Mon Sep 17 00:00:00 2001
383d26
From: John Snow <jsnow@redhat.com>
383d26
Date: Wed, 6 Feb 2019 22:12:25 +0100
383d26
Subject: [PATCH 15/33] block/dirty-bitmap: Add bdrv_dirty_iter_next_area
383d26
383d26
RH-Author: John Snow <jsnow@redhat.com>
383d26
Message-id: <20190206221243.7407-6-jsnow@redhat.com>
383d26
Patchwork-id: 84265
383d26
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH v2 05/23] block/dirty-bitmap: Add bdrv_dirty_iter_next_area
383d26
Bugzilla: 1658343
383d26
RH-Acked-by: Thomas Huth <thuth@redhat.com>
383d26
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
383d26
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
383d26
383d26
From: Max Reitz <mreitz@redhat.com>
383d26
383d26
This new function allows to look for a consecutively dirty area in a
383d26
dirty bitmap.
383d26
383d26
Signed-off-by: Max Reitz <mreitz@redhat.com>
383d26
Reviewed-by: Fam Zheng <famz@redhat.com>
383d26
Reviewed-by: John Snow <jsnow@redhat.com>
383d26
Message-id: 20180613181823.13618-10-mreitz@redhat.com
383d26
Signed-off-by: Max Reitz <mreitz@redhat.com>
383d26
(cherry picked from commit 72d10a94213a954ad569095cb4491f2ae0853c40)
383d26
Signed-off-by: John Snow <jsnow@redhat.com>
383d26
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
383d26
---
383d26
 block/dirty-bitmap.c         | 55 ++++++++++++++++++++++++++++++++++++++++++++
383d26
 include/block/dirty-bitmap.h |  2 ++
383d26
 2 files changed, 57 insertions(+)
383d26
383d26
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
383d26
index f4a4cb7..59027d4 100644
383d26
--- a/block/dirty-bitmap.c
383d26
+++ b/block/dirty-bitmap.c
383d26
@@ -528,6 +528,61 @@ int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
383d26
     return hbitmap_iter_next(&iter->hbi, true);
383d26
 }
383d26
 
383d26
+/**
383d26
+ * Return the next consecutively dirty area in the dirty bitmap
383d26
+ * belonging to the given iterator @iter.
383d26
+ *
383d26
+ * @max_offset: Maximum value that may be returned for
383d26
+ *              *offset + *bytes
383d26
+ * @offset:     Will contain the start offset of the next dirty area
383d26
+ * @bytes:      Will contain the length of the next dirty area
383d26
+ *
383d26
+ * Returns: True if a dirty area could be found before max_offset
383d26
+ *          (which means that *offset and *bytes then contain valid
383d26
+ *          values), false otherwise.
383d26
+ *
383d26
+ * Note that @iter is never advanced if false is returned.  If an area
383d26
+ * is found (which means that true is returned), it will be advanced
383d26
+ * past that area.
383d26
+ */
383d26
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
383d26
+                               uint64_t *offset, int *bytes)
383d26
+{
383d26
+    uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
383d26
+    uint64_t gran_max_offset;
383d26
+    int64_t ret;
383d26
+    int size;
383d26
+
383d26
+    if (max_offset == iter->bitmap->size) {
383d26
+        /* If max_offset points to the image end, round it up by the
383d26
+         * bitmap granularity */
383d26
+        gran_max_offset = ROUND_UP(max_offset, granularity);
383d26
+    } else {
383d26
+        gran_max_offset = max_offset;
383d26
+    }
383d26
+
383d26
+    ret = hbitmap_iter_next(&iter->hbi, false);
383d26
+    if (ret < 0 || ret + granularity > gran_max_offset) {
383d26
+        return false;
383d26
+    }
383d26
+
383d26
+    *offset = ret;
383d26
+    size = 0;
383d26
+
383d26
+    assert(granularity <= INT_MAX);
383d26
+
383d26
+    do {
383d26
+        /* Advance iterator */
383d26
+        ret = hbitmap_iter_next(&iter->hbi, true);
383d26
+        size += granularity;
383d26
+    } while (ret + granularity <= gran_max_offset &&
383d26
+             hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
383d26
+             size <= INT_MAX - granularity);
383d26
+
383d26
+    *bytes = MIN(size, max_offset - *offset);
383d26
+    return true;
383d26
+}
383d26
+
383d26
 /* Called within bdrv_dirty_bitmap_lock..unlock */
383d26
 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
383d26
                                   int64_t offset, int64_t bytes)
383d26
diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h
383d26
index bf68dd7..259bd27 100644
383d26
--- a/include/block/dirty-bitmap.h
383d26
+++ b/include/block/dirty-bitmap.h
383d26
@@ -83,6 +83,8 @@ void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
383d26
 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
383d26
                                     int64_t offset, int64_t bytes);
383d26
 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter);
383d26
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
383d26
+                               uint64_t *offset, int *bytes);
383d26
 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *hbi, int64_t offset);
383d26
 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap);
383d26
 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap);
383d26
-- 
383d26
1.8.3.1
383d26