[PATCH 2/4] hbitmap: introduce hbitmap_reverse()

Andrey Zhadchenko posted 4 patches 6 months ago
Maintainers: John Snow <jsnow@redhat.com>, Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>, Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Eric Blake <eblake@redhat.com>
There is a newer version of this series
[PATCH 2/4] hbitmap: introduce hbitmap_reverse()
Posted by Andrey Zhadchenko 6 months ago
and bdrv_dirty_bitmap_reverse() helper

Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
 block/dirty-bitmap.c         |  9 +++++++++
 include/block/block_int-io.h |  1 +
 include/qemu/hbitmap.h       |  8 ++++++++
 util/hbitmap.c               | 15 +++++++++++++++
 4 files changed, 33 insertions(+)

diff --git a/block/dirty-bitmap.c b/block/dirty-bitmap.c
index 13a1979755..c7f453fdb9 100644
--- a/block/dirty-bitmap.c
+++ b/block/dirty-bitmap.c
@@ -888,3 +888,12 @@ void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
         }
     }
 }
+
+void bdrv_dirty_bitmap_reverse(BdrvDirtyBitmap *bitmap)
+{
+    assert(!bdrv_dirty_bitmap_readonly(bitmap));
+    assert(!bdrv_dirty_bitmap_inconsistent(bitmap));
+    bdrv_dirty_bitmaps_lock(bitmap->bs);
+    hbitmap_reverse(bitmap->bitmap);
+    bdrv_dirty_bitmaps_unlock(bitmap->bs);
+}
diff --git a/include/block/block_int-io.h b/include/block/block_int-io.h
index 4a7cf2b4fd..093613e7d1 100644
--- a/include/block/block_int-io.h
+++ b/include/block/block_int-io.h
@@ -109,6 +109,7 @@ void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out);
 void bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest,
                                       const BdrvDirtyBitmap *src,
                                       HBitmap **backup, bool lock);
+void bdrv_dirty_bitmap_reverse(BdrvDirtyBitmap *bitmap);
 
 void bdrv_inc_in_flight(BlockDriverState *bs);
 void bdrv_dec_in_flight(BlockDriverState *bs);
diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h
index 8136e33674..dbdc9aa2d4 100644
--- a/include/qemu/hbitmap.h
+++ b/include/qemu/hbitmap.h
@@ -350,4 +350,12 @@ bool hbitmap_status(const HBitmap *hb, int64_t start, int64_t count,
  */
 int64_t hbitmap_iter_next(HBitmapIter *hbi);
 
+/**
+ * hbitmap_reverse:
+ * @bitmap: The HBitmap to operate on
+ *
+ * Reverse the bits in the bitmap.
+ */
+void hbitmap_reverse(HBitmap *bitmap);
+
 #endif
diff --git a/util/hbitmap.c b/util/hbitmap.c
index 16674f33e4..b99c4b1eec 100644
--- a/util/hbitmap.c
+++ b/util/hbitmap.c
@@ -940,3 +940,18 @@ char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
 
     return hash;
 }
+
+void hbitmap_reverse(HBitmap *bitmap)
+{
+    int64_t pnum, pos = 0;
+    int64_t size = bitmap->orig_size;
+
+    while (pos < size) {
+        if (hbitmap_status(bitmap, pos, size - pos, &pnum)) {
+            hbitmap_reset(bitmap, pos, pnum);
+        } else {
+            hbitmap_set(bitmap, pos, pnum);
+        }
+        pos += pnum;
+    }
+}
-- 
2.43.0
Re: [PATCH 2/4] hbitmap: introduce hbitmap_reverse()
Posted by Eric Blake 5 months, 4 weeks ago
On Tue, May 13, 2025 at 03:32:36AM +0200, Andrey Zhadchenko wrote:
> and bdrv_dirty_bitmap_reverse() helper

Is 'inverse' a better name than 'reverse'?

> 
> Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
> ---
> +++ b/util/hbitmap.c
> @@ -940,3 +940,18 @@ char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
>  
>      return hash;
>  }
> +
> +void hbitmap_reverse(HBitmap *bitmap)
> +{
> +    int64_t pnum, pos = 0;
> +    int64_t size = bitmap->orig_size;
> +
> +    while (pos < size) {
> +        if (hbitmap_status(bitmap, pos, size - pos, &pnum)) {
> +            hbitmap_reset(bitmap, pos, pnum);
> +        } else {
> +            hbitmap_set(bitmap, pos, pnum);
> +        }

To me, reverse on 1110000 would be 0000111 (swapping the order); while
inverse would be 0001111 (swapping the bits but preserving the order).

The naming change will require respinning the series, but the concept
makes sense.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org
Re: [PATCH 2/4] hbitmap: introduce hbitmap_reverse()
Posted by Andrey Zhadchenko 5 months, 4 weeks ago

On 5/20/25 18:29, Eric Blake wrote:
> [?? ??????? ????????? ?????? ?? eblake@redhat.com. ???????, ?????? ??? ?????, ?? ?????? https://aka.ms/LearnAboutSenderIdentification ]
> 
> On Tue, May 13, 2025 at 03:32:36AM +0200, Andrey Zhadchenko wrote:
>> and bdrv_dirty_bitmap_reverse() helper
> 
> Is 'inverse' a better name than 'reverse'?

Yeah, it sounds much better this way!
I will re-do the patchset

> 
>>
>> Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
>> ---
>> +++ b/util/hbitmap.c
>> @@ -940,3 +940,18 @@ char *hbitmap_sha256(const HBitmap *bitmap, Error **errp)
>>
>>       return hash;
>>   }
>> +
>> +void hbitmap_reverse(HBitmap *bitmap)
>> +{
>> +    int64_t pnum, pos = 0;
>> +    int64_t size = bitmap->orig_size;
>> +
>> +    while (pos < size) {
>> +        if (hbitmap_status(bitmap, pos, size - pos, &pnum)) {
>> +            hbitmap_reset(bitmap, pos, pnum);
>> +        } else {
>> +            hbitmap_set(bitmap, pos, pnum);
>> +        }
> 
> To me, reverse on 1110000 would be 0000111 (swapping the order); while
> inverse would be 0001111 (swapping the bits but preserving the order).
> 
> The naming change will require respinning the series, but the concept
> makes sense.
 > > --
> Eric Blake, Principal Software Engineer
> Red Hat, Inc.
> Virtualization:  qemu.org | libguestfs.org
>