Rather than unnecessarily setting bits that are already set, re-use 
cow_find_streak to find how
many bits are already set for this sector, and only set unset bits. Do this 
before the flush to
avoid it if no bits need to be set at all.

Signed-off-by: Charlie Shepherd <char...@ctshepherd.com>
---
 block/cow.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/block/cow.c b/block/cow.c
index 41097d8..93207eb 100644
--- a/block/cow.c
+++ b/block/cow.c
@@ -203,7 +203,7 @@ static int cow_update_bitmap(BlockDriverState *bs, int64_t 
sector_num,
     bool first = true;
 
     while (nb_sectors) {
-        int ret;
+        int ret, set;
         uint8_t bitmap[BDRV_SECTOR_SIZE];
 
         bitnum &= BITS_PER_BITMAP_SECTOR - 1;
@@ -214,6 +214,15 @@ static int cow_update_bitmap(BlockDriverState *bs, int64_t 
sector_num,
             return ret;
         }
 
+        /* Skip over any already set bits */
+        set = cow_find_streak(bitmap, 1, bitnum, sector_bits);
+        bitnum += set;
+        sector_bits -= set;
+        nb_sectors -= set;
+        if (set == sector_bits) {
+            continue;
+        }
+
         if (first) {
             ret = bdrv_flush(bs->file);
             if (ret < 0) {
@@ -228,7 +237,6 @@ static int cow_update_bitmap(BlockDriverState *bs, int64_t 
sector_num,
         if (ret < 0) {
             return ret;
         }
-
         bitnum += sector_bits;
         nb_sectors -= sector_bits;
         offset += BDRV_SECTOR_SIZE;
-- 
1.8.4.rc3


Reply via email to