From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ve2gB-0001N5-7W for qemu-devel@nongnu.org; Wed, 06 Nov 2013 07:57:57 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Ve2fz-0001Nt-TG for qemu-devel@nongnu.org; Wed, 06 Nov 2013 07:57:51 -0500 Received: from mail6.webfaction.com ([74.55.86.74]:46881 helo=smtp.webfaction.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Ve2fz-0001Np-OJ for qemu-devel@nongnu.org; Wed, 06 Nov 2013 07:57:39 -0500 From: Charlie Shepherd Date: Wed, 6 Nov 2013 13:56:56 +0100 Message-Id: <1383742617-14742-3-git-send-email-charlie@ctshepherd.com> In-Reply-To: <1383742617-14742-1-git-send-email-charlie@ctshepherd.com> References: <1383742617-14742-1-git-send-email-charlie@ctshepherd.com> Subject: [Qemu-devel] [PATCH v2 2/3] COW: Extend checking allocated bits to beyond one sector List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, pbonzini@redhat.com, gabriel@kerneis.info, Charlie Shepherd , stefanha@gmail.com cow_co_is_allocated() only checks one sector's worth of allocated bits before returning. This is allowed but (slightly) inefficient, so extend it to check all of the file's metadata sectors. Signed-off-by: Charlie Shepherd Reviewed-by: Paolo Bonzini --- block/cow.c | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/block/cow.c b/block/cow.c index 5dfffb0..41097d8 100644 --- a/block/cow.c +++ b/block/cow.c @@ -152,18 +152,34 @@ static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs, { int64_t bitnum = sector_num + sizeof(struct cow_header_v2) * 8; uint64_t offset = (bitnum / 8) & -BDRV_SECTOR_SIZE; - uint8_t bitmap[BDRV_SECTOR_SIZE]; - int ret; - int changed; + bool first = true; + int changed, same = 0; - ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); - if (ret < 0) { - return ret; - } + do { + int ret; + uint8_t bitmap[BDRV_SECTOR_SIZE]; + + bitnum &= BITS_PER_BITMAP_SECTOR - 1; + int sector_bits = MIN(nb_sectors, BITS_PER_BITMAP_SECTOR - bitnum); + + ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)); + if (ret < 0) { + return ret; + } + + if (first) { + changed = cow_test_bit(bitnum, bitmap); + first = false; + } + + same += cow_find_streak(bitmap, changed, bitnum, nb_sectors); + + bitnum += sector_bits; + nb_sectors -= sector_bits; + offset += BDRV_SECTOR_SIZE; + } while (nb_sectors); - bitnum &= BITS_PER_BITMAP_SECTOR - 1; - changed = cow_test_bit(bitnum, bitmap); - *num_same = cow_find_streak(bitmap, changed, bitnum, nb_sectors); + *num_same = same; return changed; } -- 1.8.4.rc3