26ba25
From 36323c9384909d2213fafb77b1fcf0ddbfcaaffc Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Tue, 20 Nov 2018 18:18:09 +0000
26ba25
Subject: [PATCH 15/35] block/dirty-bitmap: Add bdrv_dirty_iter_next_area
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20181120181828.15132-6-jsnow@redhat.com>
26ba25
Patchwork-id: 83054
26ba25
O-Subject: [RHEL8/rhel qemu-kvm PATCH 05/24] block/dirty-bitmap: Add bdrv_dirty_iter_next_area
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: Max Reitz <mreitz@redhat.com>
26ba25
26ba25
This new function allows to look for a consecutively dirty area in a
26ba25
dirty bitmap.
26ba25
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
Reviewed-by: Fam Zheng <famz@redhat.com>
26ba25
Reviewed-by: John Snow <jsnow@redhat.com>
26ba25
Message-id: 20180613181823.13618-10-mreitz@redhat.com
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 72d10a94213a954ad569095cb4491f2ae0853c40)
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         | 55 ++++++++++++++++++++++++++++++++++++++++++++
26ba25
 include/block/dirty-bitmap.h |  2 ++
26ba25
 2 files changed, 57 insertions(+)
26ba25
26ba25
diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
26ba25
index 236dce1..c9b8a6f 100644
26ba25
--- a/block/dirty-bitmap.c
26ba25
+++ b/block/dirty-bitmap.c
26ba25
@@ -528,6 +528,61 @@ int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter)
26ba25
     return hbitmap_iter_next(&iter->hbi, true);
26ba25
 }
26ba25
 
26ba25
+/**
26ba25
+ * Return the next consecutively dirty area in the dirty bitmap
26ba25
+ * belonging to the given iterator @iter.
26ba25
+ *
26ba25
+ * @max_offset: Maximum value that may be returned for
26ba25
+ *              *offset + *bytes
26ba25
+ * @offset:     Will contain the start offset of the next dirty area
26ba25
+ * @bytes:      Will contain the length of the next dirty area
26ba25
+ *
26ba25
+ * Returns: True if a dirty area could be found before max_offset
26ba25
+ *          (which means that *offset and *bytes then contain valid
26ba25
+ *          values), false otherwise.
26ba25
+ *
26ba25
+ * Note that @iter is never advanced if false is returned.  If an area
26ba25
+ * is found (which means that true is returned), it will be advanced
26ba25
+ * past that area.
26ba25
+ */
26ba25
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
26ba25
+                               uint64_t *offset, int *bytes)
26ba25
+{
26ba25
+    uint32_t granularity = bdrv_dirty_bitmap_granularity(iter->bitmap);
26ba25
+    uint64_t gran_max_offset;
26ba25
+    int64_t ret;
26ba25
+    int size;
26ba25
+
26ba25
+    if (max_offset == iter->bitmap->size) {
26ba25
+        /* If max_offset points to the image end, round it up by the
26ba25
+         * bitmap granularity */
26ba25
+        gran_max_offset = ROUND_UP(max_offset, granularity);
26ba25
+    } else {
26ba25
+        gran_max_offset = max_offset;
26ba25
+    }
26ba25
+
26ba25
+    ret = hbitmap_iter_next(&iter->hbi, false);
26ba25
+    if (ret < 0 || ret + granularity > gran_max_offset) {
26ba25
+        return false;
26ba25
+    }
26ba25
+
26ba25
+    *offset = ret;
26ba25
+    size = 0;
26ba25
+
26ba25
+    assert(granularity <= INT_MAX);
26ba25
+
26ba25
+    do {
26ba25
+        /* Advance iterator */
26ba25
+        ret = hbitmap_iter_next(&iter->hbi, true);
26ba25
+        size += granularity;
26ba25
+    } while (ret + granularity <= gran_max_offset &&
26ba25
+             hbitmap_iter_next(&iter->hbi, false) == ret + granularity &&
26ba25
+             size <= INT_MAX - granularity);
26ba25
+
26ba25
+    *bytes = MIN(size, max_offset - *offset);
26ba25
+    return true;
26ba25
+}
26ba25
+
26ba25
 /* Called within bdrv_dirty_bitmap_lock..unlock */
26ba25
 void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
26ba25
                                   int64_t offset, int64_t bytes)
26ba25
diff --git a/include/block/dirty-bitmap.h b/include/block/dirty-bitmap.h
26ba25
index bf68dd7..259bd27 100644
26ba25
--- a/include/block/dirty-bitmap.h
26ba25
+++ b/include/block/dirty-bitmap.h
26ba25
@@ -83,6 +83,8 @@ void bdrv_set_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
26ba25
 void bdrv_reset_dirty_bitmap_locked(BdrvDirtyBitmap *bitmap,
26ba25
                                     int64_t offset, int64_t bytes);
26ba25
 int64_t bdrv_dirty_iter_next(BdrvDirtyBitmapIter *iter);
26ba25
+bool bdrv_dirty_iter_next_area(BdrvDirtyBitmapIter *iter, uint64_t max_offset,
26ba25
+                               uint64_t *offset, int *bytes);
26ba25
 void bdrv_set_dirty_iter(BdrvDirtyBitmapIter *hbi, int64_t offset);
26ba25
 int64_t bdrv_get_dirty_count(BdrvDirtyBitmap *bitmap);
26ba25
 int64_t bdrv_get_meta_dirty_count(BdrvDirtyBitmap *bitmap);
26ba25
-- 
26ba25
1.8.3.1
26ba25