qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com
Subject: [Qemu-devel] [PATCH v3 04/16] block: introduce new dirty bitmap functionality
Date: Thu, 18 Oct 2012 16:49:18 +0200	[thread overview]
Message-ID: <1350571770-9836-5-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1350571770-9836-1-git-send-email-pbonzini@redhat.com>

Assert that write_compressed is never used with the dirty bitmap.
Setting the bits early is wrong, because a coroutine might concurrently
examine them and copy incomplete data from the source.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 block.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------
 block.h |  5 +++--
 2 file modificati, 48 inserzioni(+), 8 rimozioni(-)

diff --git a/block.c b/block.c
index 4787312..057b2b2 100644
--- a/block.c
+++ b/block.c
@@ -2389,7 +2389,7 @@ static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
     }
 
     if (bs->dirty_bitmap) {
-        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
+        bdrv_set_dirty(bs, sector_num, nb_sectors);
     }
 
     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
@@ -2958,9 +2958,7 @@ int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
     if (bdrv_check_request(bs, sector_num, nb_sectors))
         return -EIO;
 
-    if (bs->dirty_bitmap) {
-        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
-    }
+    assert(!bs->dirty_bitmap);
 
     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
 }
@@ -4219,13 +4217,54 @@ int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
 
     if (bs->dirty_bitmap &&
         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
-        return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
-            (1UL << (chunk % (sizeof(unsigned long) * 8))));
+        return !!(bs->dirty_bitmap[chunk / BITS_PER_LONG] &
+            (1UL << (chunk % BITS_PER_LONG)));
     } else {
         return 0;
     }
 }
 
+int64_t bdrv_get_next_dirty(BlockDriverState *bs, int64_t sector)
+{
+    int64_t chunk;
+    int bit, elem;
+
+    /* Avoid an infinite loop.  */
+    assert(bs->dirty_count > 0);
+
+    sector = (sector | (BDRV_SECTORS_PER_DIRTY_CHUNK - 1)) + 1;
+    chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
+
+    QEMU_BUILD_BUG_ON(sizeof(bs->dirty_bitmap[0]) * 8 != BITS_PER_LONG);
+    elem = chunk / BITS_PER_LONG;
+    bit = chunk % BITS_PER_LONG;
+    for (;;) {
+        if (sector >= bs->total_sectors) {
+            sector = 0;
+            bit = elem = 0;
+        }
+        if (bit == 0 && bs->dirty_bitmap[elem] == 0) {
+            sector += BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG;
+            elem++;
+        } else {
+            if (bs->dirty_bitmap[elem] & (1UL << bit)) {
+                return sector;
+            }
+            sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
+            if (++bit == BITS_PER_LONG) {
+                bit = 0;
+                elem++;
+            }
+        }
+    }
+}
+
+void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
+                    int nr_sectors)
+{
+    set_dirty_bitmap(bs, cur_sector, nr_sectors, 1);
+}
+
 void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
                       int nr_sectors)
 {
diff --git a/block.h b/block.h
index 096fa09..27b8f80 100644
--- a/block.h
+++ b/block.h
@@ -353,8 +353,9 @@ void *qemu_blockalign(BlockDriverState *bs, size_t size);
 
 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);
+void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors);
+void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors);
+int64_t bdrv_get_next_dirty(BlockDriverState *bs, int64_t sector);
 int64_t bdrv_get_dirty_count(BlockDriverState *bs);
 
 void bdrv_enable_copy_on_read(BlockDriverState *bs);
-- 
1.7.12.1

  parent reply	other threads:[~2012-10-18 14:50 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-18 14:49 [Qemu-devel] [PULL for Kevin 00/16] Block job improvements part 2 Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 01/16] block: add bdrv_query_info Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 02/16] block: add bdrv_query_stats Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 03/16] block: add bdrv_open_backing_file Paolo Bonzini
2012-10-18 14:49 ` Paolo Bonzini [this message]
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 05/16] block: export dirty bitmap information in query-block Paolo Bonzini
2012-10-18 16:51   ` Eric Blake
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 06/16] block: rename block_job_complete to block_job_completed Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 07/16] block: add block-job-complete Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 08/16] block: introduce BLOCK_JOB_READY event Paolo Bonzini
2012-10-18 16:58   ` Eric Blake
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 09/16] mirror: introduce mirror job Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 10/16] qmp: add drive-mirror command Paolo Bonzini
2012-10-18 17:34   ` Eric Blake
2012-10-19 12:54   ` Kevin Wolf
2012-10-19 13:13     ` Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 11/16] mirror: implement completion Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 12/16] qemu-iotests: add mirroring test case Paolo Bonzini
2012-10-19 16:19   ` Kevin Wolf
2012-10-20 13:47     ` Paolo Bonzini
2012-10-22  8:44       ` Kevin Wolf
2012-10-22 15:38         ` Paolo Bonzini
2012-10-23 14:39         ` [Qemu-devel] [PATCH v4 " Paolo Bonzini
2012-10-23 14:39         ` [Qemu-devel] [PATCH v4 16/16] qemu-iotests: add testcases for mirroring on-source-error/on-target-error Paolo Bonzini
2012-10-19 17:24   ` [Qemu-devel] [PATCH v3 12/16] qemu-iotests: add mirroring test case Kevin Wolf
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 13/16] iostatus: forward block_job_iostatus_reset to block job Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 14/16] mirror: add support for on-source-error/on-target-error Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 15/16] qmp: add pull_event function Paolo Bonzini
2012-10-18 14:49 ` [Qemu-devel] [PATCH v3 16/16] qemu-iotests: add testcases for mirroring on-source-error/on-target-error Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350571770-9836-5-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).