[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 63/76] block: Resize bitmaps on bdrv_truncate
From: |
Kevin Wolf |
Subject: |
[Qemu-block] [PULL 63/76] block: Resize bitmaps on bdrv_truncate |
Date: |
Tue, 28 Apr 2015 17:00:45 +0200 |
From: John Snow <address@hidden>
Signed-off-by: John Snow <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
Reviewed-by: Stefan Hajnoczi <address@hidden>
Message-id: address@hidden
Signed-off-by: Stefan Hajnoczi <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block.c | 18 ++++++++++++++++++
include/qemu/hbitmap.h | 10 ++++++++++
util/hbitmap.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+)
diff --git a/block.c b/block.c
index b7d6df5..ec23594 100644
--- a/block.c
+++ b/block.c
@@ -114,6 +114,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;
@@ -3610,6 +3611,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);
}
@@ -5659,6 +5661,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 6cb2d0e..f0a85f8 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 150d6e9..a10c7ae 100644
--- a/util/hbitmap.c
+++ b/util/hbitmap.c
@@ -400,6 +400,54 @@ 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.
+ */
+ if (shrink) {
+ /* Don't clear partial granularity groups;
+ * start at the first full one. */
+ uint64_t start = QEMU_ALIGN_UP(num_elements, 1 << hb->granularity);
+ uint64_t fix_count = (hb->size << hb->granularity) - start;
+
+ assert(fix_count);
+ hbitmap_reset(hb, start, fix_count);
+ }
+
+ 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.
--
1.8.3.1
- [Qemu-block] [PULL 54/76] hbitmap: cache array lengths, (continued)
- [Qemu-block] [PULL 54/76] hbitmap: cache array lengths, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 55/76] hbitmap: add hbitmap_merge, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 56/76] block: Add bitmap disabled status, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 57/76] block: Add bitmap successors, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 58/76] qmp: Add support of "dirty-bitmap" sync mode for drive-backup, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 59/76] qmp: add block-dirty-bitmap-clear, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 60/76] qmp: Add dirty bitmap status field in query-block, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 61/76] block: add BdrvDirtyBitmap documentation, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 62/76] block: Ensure consistent bitmap function prototypes, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 64/76] hbitmap: truncate tests, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 63/76] block: Resize bitmaps on bdrv_truncate,
Kevin Wolf <=
- [Qemu-block] [PULL 65/76] iotests: add invalid input incremental backup tests, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 68/76] iotests: add incremental backup failure recovery test, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 67/76] iotests: add simple incremental backup case, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 70/76] block/mirror: Always call block_job_sleep_ns(), Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 69/76] iotests: add incremental backup granularity tests, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 71/76] block/dmg: make it modular, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 72/76] vmdk: Widen before shifting 32 bit header field, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 73/76] block: replace bdrv_states iteration with bdrv_next(), Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 74/76] block: add bdrv_set_dirty()/bdrv_reset_dirty() to block_int.h, Kevin Wolf, 2015/04/28
- [Qemu-block] [PULL 75/76] block: extract bdrv_setup_io_funcs(), Kevin Wolf, 2015/04/28