From patchwork Sat Feb 28 00:47:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Snow X-Patchwork-Id: 444544 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 D00AC140119 for ; Sat, 28 Feb 2015 11:53:14 +1100 (AEDT) Received: from localhost ([::1]:39632 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YRVeb-0003r0-3n for incoming@patchwork.ozlabs.org; Fri, 27 Feb 2015 19:53:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36316) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YRVZr-00028s-FD for qemu-devel@nongnu.org; Fri, 27 Feb 2015 19:48:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YRVZq-0000QX-Af for qemu-devel@nongnu.org; Fri, 27 Feb 2015 19:48:19 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50822) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YRVZn-0000OY-VN; Fri, 27 Feb 2015 19:48:16 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t1S0mF5Q015076 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Fri, 27 Feb 2015 19:48:15 -0500 Received: from scv.usersys.redhat.com (dhcp-17-38.bos.redhat.com [10.18.17.38]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t1S0lwNm014604; Fri, 27 Feb 2015 19:48:14 -0500 From: John Snow To: qemu-block@nongnu.org Date: Fri, 27 Feb 2015 19:47:54 -0500 Message-Id: <1425084477-31602-15-git-send-email-jsnow@redhat.com> In-Reply-To: <1425084477-31602-1-git-send-email-jsnow@redhat.com> References: <1425084477-31602-1-git-send-email-jsnow@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 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] [PATCH RESEND 14/17] 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 --- block.c | 22 ++++++++++++++++++++ include/block/block.h | 1 + include/qemu/hbitmap.h | 10 ++++++++++ util/hbitmap.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+) diff --git a/block.c b/block.c index e6b2696..5eaa874 100644 --- a/block.c +++ b/block.c @@ -3543,6 +3543,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 +5563,27 @@ BdrvDirtyBitmap *bdrv_reclaim_dirty_bitmap(BlockDriverState *bs, return parent; } +static void dirty_bitmap_truncate(BdrvDirtyBitmap *bitmap, uint64_t size) +{ + /* Should only be frozen during a block backup job, which should have + * blocked any resize actions. */ + assert(!bdrv_dirty_bitmap_frozen(bitmap)); + hbitmap_truncate(bitmap->bitmap, size); +} + +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; + } + dirty_bitmap_truncate(bitmap, size); + } +} + void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) { BdrvDirtyBitmap *bm, *next; diff --git a/include/block/block.h b/include/block/block.h index a8c6369..3a85690 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -454,6 +454,7 @@ BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, uint32_t granularity, const char *name, Error **errp); +void bdrv_dirty_bitmap_truncate(BlockDriverState *bs); int bdrv_dirty_bitmap_create_successor(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, Error **errp); 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 962ff29..0934a61 100644 --- a/util/hbitmap.c +++ b/util/hbitmap.c @@ -90,6 +90,9 @@ struct HBitmap { * bitmap will still allocate HBITMAP_LEVELS arrays. */ unsigned long *levels[HBITMAP_LEVELS]; + + /* The length of each levels[] array. */ + uint64_t sizes[HBITMAP_LEVELS]; }; /* Advance hbi to the next nonzero word and return it. hbi->pos @@ -384,6 +387,7 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) hb->granularity = granularity; for (i = HBITMAP_LEVELS; i-- > 0; ) { size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1); + hb->sizes[i] = size; hb->levels[i] = g_new0(unsigned long, size); } @@ -396,6 +400,56 @@ HBitmap *hbitmap_alloc(uint64_t size, int granularity) return hb; } +void hbitmap_truncate(HBitmap *hb, uint64_t size) +{ + bool truncate; + 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)); + truncate = size < hb->size; + + if (size == hb->size) { + /* A hard day's work */ + return; + } + + hb->size = size; + for (i = HBITMAP_LEVELS; i-- > 0; ) { + size = MAX((size + BITS_PER_LONG - 1) >> BITS_PER_LEVEL, 1); + if (hb->sizes[i] == size) { + continue; + } + old = hb->sizes[i]; + hb->sizes[i] = size; + hb->levels[i] = g_realloc_n(hb->levels[i], size, sizeof(unsigned long)); + if (!truncate) { + memset(&hb->levels[i][old], 0x00, size - old); + } + } + assert(size == 1); + + /* Clear out any "extra space" we may have that the user didn't request: + * It may have garbage data in it, now. */ + if (truncate) { + /* Due to granularity fuzziness, we may accidentally reset some of + * the last bits that are actually valid. So, record the current value, + * reset the "dead range," then re-set the one element we care about. */ + uint64_t fix_count = (hb->size << hb->granularity) - num_elements; + if (fix_count) { + bool set = hbitmap_get(hb, num_elements - 1); + hbitmap_reset(hb, num_elements, fix_count); + if (set) { + hbitmap_set(hb, num_elements - 1, 1); + } + } + } +} + + /** * Given HBitmaps A and B, let A := A (BITOR) B. * Bitmap B will not be modified.