From 076ea47536a5653203107495d527c23a11afc2db Mon Sep 17 00:00:00 2001
Message-Id: <076ea47536a5653203107495d527c23a11afc2db.1334770230.git.minovotn@redhat.com>
In-Reply-To: <5e4659718c6d6ee9ab11b269d929a292a71b3ab0.1334770230.git.minovotn@redhat.com>
References: <5e4659718c6d6ee9ab11b269d929a292a71b3ab0.1334770230.git.minovotn@redhat.com>
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Fri, 13 Apr 2012 16:27:15 +0200
Subject: [PATCH 07/18] Count dirty blocks and expose an API to get dirty
 count

RH-Author: Paolo Bonzini <pbonzini@redhat.com>
Message-id: <1334334446-31987-6-git-send-email-pbonzini@redhat.com>
Patchwork-id: 39217
O-Subject: [RHEL 6.3 qemu-kvm PATCH 05/16] Count dirty blocks and expose an API to get dirty count
Bugzilla: 806432
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>

From: Liran Schour <lirans@il.ibm.com>

Bugzilla: 806432

This will manage dirty counter for each device and will allow to get the
dirty counter from above.

Signed-off-by: Liran Schour <lirans@il.ibm.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
(cherry-picked from upstream commit aaa0eb75e2e56d483c89731a447c999985713b43)
---
 block.c     |   16 ++++++++++++++--
 block.h     |    1 +
 block_int.h |    1 +
 3 files changed, 16 insertions(+), 2 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 block.c     |   16 ++++++++++++++--
 block.h     |    1 +
 block_int.h |    1 +
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/block.c b/block.c
index 46de187..bfd479f 100644
--- a/block.c
+++ b/block.c
@@ -1383,9 +1383,15 @@ static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
         bit = start % (sizeof(unsigned long) * 8);
         val = bs->dirty_bitmap[idx];
         if (dirty) {
-            val |= 1 << bit;
+            if (!(val & (1 << bit))) {
+                bs->dirty_count++;
+                val |= 1 << bit;
+            }
         } else {
-            val &= ~(1 << bit);
+            if (val & (1 << bit)) {
+                bs->dirty_count--;
+                val &= ~(1 << bit);
+            }
         }
         bs->dirty_bitmap[idx] = val;
     }
@@ -3412,6 +3418,7 @@ void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
 {
     int64_t bitmap_size;
 
+    bs->dirty_count = 0;
     if (enable) {
         if (!bs->dirty_bitmap) {
             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
@@ -3447,6 +3454,11 @@ void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
 }
 
+int64_t bdrv_get_dirty_count(BlockDriverState *bs)
+{
+    return bs->dirty_count;
+}
+
 void bdrv_iostatus_enable(BlockDriverState *bs)
 {
     bs->iostatus = BDRV_IOS_OK;
diff --git a/block.h b/block.h
index 36c4a1b..cb8900c 100644
--- a/block.h
+++ b/block.h
@@ -312,6 +312,7 @@ void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable);
 int bdrv_get_dirty(BlockDriverState *bs, int64_t sector);
 void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
                       int nr_sectors);
+int64_t bdrv_get_dirty_count(BlockDriverState *bs);
 
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
 void bdrv_disable_copy_on_read(BlockDriverState *bs);
diff --git a/block_int.h b/block_int.h
index e195ffe..c97a14d 100644
--- a/block_int.h
+++ b/block_int.h
@@ -254,6 +254,7 @@ struct BlockDriverState {
     BlockIOStatus iostatus;
     char device_name[32];
     unsigned long *dirty_bitmap;
+    int64_t dirty_count;
     int in_use; /* users other than guest access, eg. block migration */
     QTAILQ_ENTRY(BlockDriverState) list;
     void *private;
-- 
1.7.7.6

