From patchwork Fri Mar 13 18:30:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 450026 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 26A11140083 for ; Sat, 14 Mar 2015 05:32:57 +1100 (AEDT) Received: from localhost ([::1]:37997 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWUOE-0004uU-3i for incoming@patchwork.ozlabs.org; Fri, 13 Mar 2015 14:32:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWUNZ-0003me-QP for qemu-devel@nongnu.org; Fri, 13 Mar 2015 14:32:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YWUM9-0007PN-Jr for qemu-devel@nongnu.org; Fri, 13 Mar 2015 14:30:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:41561) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YWUM3-0007KD-Ut; Fri, 13 Mar 2015 14:30:40 -0400 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t2DIUdjo030230 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 13 Mar 2015 14:30:39 -0400 Received: from scv.usersys.redhat.com (dhcp-17-19.bos.redhat.com [10.18.17.19]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t2DIUK7C023187; Fri, 13 Mar 2015 14:30:38 -0400 From: John Snow To: qemu-block@nongnu.org Date: Fri, 13 Mar 2015 14:30:15 -0400 Message-Id: <1426271419-8277-16-git-send-email-jsnow@redhat.com> In-Reply-To: <1426271419-8277-1-git-send-email-jsnow@redhat.com> References: <1426271419-8277-1-git-send-email-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, famz@redhat.com, John Snow , qemu-devel@nongnu.org, armbru@redhat.com, vsementsov@parallels.com, stefanha@redhat.com, mreitz@redhat.com Subject: [Qemu-devel] [2.4 PATCH v3 15/19] block: Resize bitmaps on bdrv_truncate X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: John Snow Reviewed-by: Max Reitz --- block.c | 18 +++++++++++++++++ include/qemu/hbitmap.h | 10 ++++++++++ util/hbitmap.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/block.c b/block.c index 1eee394..f40b014 100644 --- a/block.c +++ b/block.c @@ -113,6 +113,7 @@ static void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors); static void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors); +static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs); /* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist; @@ -3543,6 +3544,7 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset) ret = drv->bdrv_truncate(bs, offset); if (ret == 0) { ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); + bdrv_dirty_bitmap_truncate(bs); if (bs->blk) { blk_dev_resize_cb(bs->blk); } @@ -5562,6 +5564,22 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, return parent; } +/** + * Truncates _all_ bitmaps attached to a BDS. + */ +static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs) +{ + BdrvDirtyBitmap *bitmap; + uint64_t size = bdrv_nb_sectors(bs); + + QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { + if (bdrv_dirty_bitmap_frozen(bitmap)) { + continue; + } + hbitmap_truncate(bitmap->bitmap, size); + } +} + void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) { BdrvDirtyBitmap *bm, *next; diff --git a/include/qemu/hbitmap.h b/include/qemu/hbitmap.h index c19c1cb..a75157e 100644 --- a/include/qemu/hbitmap.h +++ b/include/qemu/hbitmap.h @@ -65,6 +65,16 @@ struct HBitmapIter { HBitmap *hbitmap_alloc(uint64_t size, int granularity); /** + * hbitmap_truncate: + * @hb: The bitmap to change the size of. + * @size: The number of elements to change the bitmap to accommodate. + * + * truncate or grow an existing bitmap to accommodate a new number of elements. + * This may invalidate existing HBitmapIterators. + */ +void hbitmap_truncate(HBitmap *hb, uint64_t size); + +/** * hbitmap_merge: * @a: The bitmap to store the result in. * @b: The bitmap to merge into @a. diff --git a/util/hbitmap.c b/util/hbitmap.c index ba11fd3..4505ef7 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -400,6 +400,58 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) return hb; } +void hbitmap_truncate(HBitmap *hb, uint64_t size) +{ + bool shrink; + unsigned i; + uint64_t num_elements = size; + uint64_t old; + + /* Size comes in as logical elements, adjust for granularity. */ + size = (size + (1ULL << hb->granularity) - 1) >> hb->granularity; + assert(size <= ((uint64_t)1 << HBITMAP_LOG_MAX_SIZE)); + shrink = size < hb->size; + + /* bit sizes are identical; nothing to do. */ + if (size == hb->size) { + return; + } + + /* If we're losing bits, let's clear those bits before we invalidate all of + * our invariants. This helps keep the bitcount consistent, and will prevent + * us from carrying around garbage bits beyond the end of the map. + * + * Because clearing bits past the end of map might reset bits we care about + * within the array, record the current value of the last bit we're keeping. + */ + if (shrink) { + bool set = hbitmap_get(hb, num_elements - 1); + uint64_t fix_count = (hb->size << hb->granularity) - num_elements; + + assert(fix_count); + hbitmap_reset(hb, num_elements, fix_count); + if (set) { + hbitmap_set(hb, num_elements - 1, 1); + } + } + + hb->size = size; + for (i = HBITMAP_LEVELS; i-- > 0; ) { + size = MAX(BITS_TO_LONGS(size), 1); + if (hb->sizes[i] == size) { + break; + } + old = hb->sizes[i]; + hb->sizes[i] = size; + hb->levels[i] = g_realloc(hb->levels[i], size * sizeof(unsigned long)); + if (!shrink) { + memset(&hb->levels[i][old], 0x00, + (size - old) * sizeof(*hb->levels[i])); + } + } +} + + /** * Given HBitmaps A and B, let A := A (BITOR) B. * Bitmap B will not be modified.