aboutsummaryrefslogtreecommitdiff
path: root/block/qcow2-bitmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'block/qcow2-bitmap.c')
-rw-r--r--block/qcow2-bitmap.c170
1 files changed, 117 insertions, 53 deletions
diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
index 9d968bdcda..e53a1609d7 100644
--- a/block/qcow2-bitmap.c
+++ b/block/qcow2-bitmap.c
@@ -343,11 +343,17 @@ static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs,
uint32_t granularity;
BdrvDirtyBitmap *bitmap = NULL;
- if (bm->flags & BME_FLAG_IN_USE) {
- error_setg(errp, "Bitmap '%s' is in use", bm->name);
+ granularity = 1U << bm->granularity_bits;
+ bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
+ if (bitmap == NULL) {
goto fail;
}
+ if (bm->flags & BME_FLAG_IN_USE) {
+ /* Data is unusable, skip loading it */
+ return bitmap;
+ }
+
ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
if (ret < 0) {
error_setg_errno(errp, -ret,
@@ -356,12 +362,6 @@ static BdrvDirtyBitmap *load_bitmap(BlockDriverState *bs,
goto fail;
}
- granularity = 1U << bm->granularity_bits;
- bitmap = bdrv_create_dirty_bitmap(bs, granularity, bm->name, errp);
- if (bitmap == NULL) {
- goto fail;
- }
-
ret = load_bitmap_data(bs, bitmap_table, bm->table.size, bitmap);
if (ret < 0) {
error_setg_errno(errp, -ret, "Could not read bitmap '%s' from image",
@@ -462,10 +462,25 @@ static int check_dir_entry(BlockDriverState *bs, Qcow2BitmapDirEntry *entry)
return len;
}
- fail = (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) ||
- (len > ((phys_bitmap_bytes * 8) << entry->granularity_bits));
+ if (phys_bitmap_bytes > BME_MAX_PHYS_SIZE) {
+ return -EINVAL;
+ }
+
+ if (!(entry->flags & BME_FLAG_IN_USE) &&
+ (len > ((phys_bitmap_bytes * 8) << entry->granularity_bits)))
+ {
+ /*
+ * We've loaded a valid bitmap (IN_USE not set) or we are going to
+ * store a valid bitmap, but the allocated bitmap table size is not
+ * enough to store this bitmap.
+ *
+ * Note, that it's OK to have an invalid bitmap with invalid size due
+ * to a bitmap that was not correctly saved after image resize.
+ */
+ return -EINVAL;
+ }
- return fail ? -EINVAL : 0;
+ return 0;
}
static inline void bitmap_directory_to_be(uint8_t *dir, size_t size)
@@ -950,6 +965,7 @@ bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
Qcow2Bitmap *bm;
GSList *created_dirty_bitmaps = NULL;
bool header_updated = false;
+ bool needs_update = false;
if (s->nb_bitmaps == 0) {
/* No bitmaps - nothing to do */
@@ -963,35 +979,39 @@ bool qcow2_load_dirty_bitmaps(BlockDriverState *bs, Error **errp)
}
QSIMPLEQ_FOREACH(bm, bm_list, entry) {
- if (!(bm->flags & BME_FLAG_IN_USE)) {
- BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp);
- if (bitmap == NULL) {
- goto fail;
- }
+ BdrvDirtyBitmap *bitmap = load_bitmap(bs, bm, errp);
+ if (bitmap == NULL) {
+ goto fail;
+ }
- if (!(bm->flags & BME_FLAG_AUTO)) {
- bdrv_disable_dirty_bitmap(bitmap);
- }
- bdrv_dirty_bitmap_set_persistance(bitmap, true);
+ bdrv_dirty_bitmap_set_persistence(bitmap, true);
+ if (bm->flags & BME_FLAG_IN_USE) {
+ bdrv_dirty_bitmap_set_inconsistent(bitmap);
+ } else {
+ /* NB: updated flags only get written if can_write(bs) is true. */
bm->flags |= BME_FLAG_IN_USE;
- created_dirty_bitmaps =
- g_slist_append(created_dirty_bitmaps, bitmap);
+ needs_update = true;
+ }
+ if (!(bm->flags & BME_FLAG_AUTO)) {
+ bdrv_disable_dirty_bitmap(bitmap);
}
+ created_dirty_bitmaps =
+ g_slist_append(created_dirty_bitmaps, bitmap);
}
- if (created_dirty_bitmaps != NULL) {
- if (can_write(bs)) {
- /* in_use flags must be updated */
- int ret = update_ext_header_and_dir_in_place(bs, bm_list);
- if (ret < 0) {
- error_setg_errno(errp, -ret, "Can't update bitmap directory");
- goto fail;
- }
- header_updated = true;
- } else {
- g_slist_foreach(created_dirty_bitmaps, set_readonly_helper,
- (gpointer)true);
+ if (needs_update && can_write(bs)) {
+ /* in_use flags must be updated */
+ int ret = update_ext_header_and_dir_in_place(bs, bm_list);
+ if (ret < 0) {
+ error_setg_errno(errp, -ret, "Can't update bitmap directory");
+ goto fail;
}
+ header_updated = true;
+ }
+
+ if (!can_write(bs)) {
+ g_slist_foreach(created_dirty_bitmaps, set_readonly_helper,
+ (gpointer)true);
}
g_slist_free(created_dirty_bitmaps);
@@ -1113,23 +1133,21 @@ int qcow2_reopen_bitmaps_rw_hint(BlockDriverState *bs, bool *header_updated,
}
QSIMPLEQ_FOREACH(bm, bm_list, entry) {
- if (!(bm->flags & BME_FLAG_IN_USE)) {
- BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
- if (bitmap == NULL) {
- continue;
- }
-
- if (!bdrv_dirty_bitmap_readonly(bitmap)) {
- error_setg(errp, "Bitmap %s is not readonly but not marked"
- "'IN_USE' in the image. Something went wrong,"
- "all the bitmaps may be corrupted", bm->name);
- ret = -EINVAL;
- goto out;
- }
+ BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
+ if (bitmap == NULL) {
+ continue;
+ }
- bm->flags |= BME_FLAG_IN_USE;
- ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap);
+ if (!bdrv_dirty_bitmap_readonly(bitmap)) {
+ error_setg(errp, "Bitmap %s was loaded prior to rw-reopen, but was "
+ "not marked as readonly. This is a bug, something went "
+ "wrong. All of the bitmaps may be corrupted", bm->name);
+ ret = -EINVAL;
+ goto out;
}
+
+ bm->flags |= BME_FLAG_IN_USE;
+ ro_dirty_bitmaps = g_slist_append(ro_dirty_bitmaps, bitmap);
}
if (ro_dirty_bitmaps != NULL) {
@@ -1157,6 +1175,52 @@ int qcow2_reopen_bitmaps_rw(BlockDriverState *bs, Error **errp)
return qcow2_reopen_bitmaps_rw_hint(bs, NULL, errp);
}
+/* Checks to see if it's safe to resize bitmaps */
+int qcow2_truncate_bitmaps_check(BlockDriverState *bs, Error **errp)
+{
+ BDRVQcow2State *s = bs->opaque;
+ Qcow2BitmapList *bm_list;
+ Qcow2Bitmap *bm;
+ int ret = 0;
+
+ if (s->nb_bitmaps == 0) {
+ return 0;
+ }
+
+ bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
+ s->bitmap_directory_size, errp);
+ if (bm_list == NULL) {
+ return -EINVAL;
+ }
+
+ QSIMPLEQ_FOREACH(bm, bm_list, entry) {
+ BdrvDirtyBitmap *bitmap = bdrv_find_dirty_bitmap(bs, bm->name);
+ if (bitmap == NULL) {
+ /*
+ * We rely on all bitmaps being in-memory to be able to resize them,
+ * Otherwise, we'd need to resize them on disk explicitly
+ */
+ error_setg(errp, "Cannot resize qcow2 with persistent bitmaps that "
+ "were not loaded into memory");
+ ret = -ENOTSUP;
+ goto out;
+ }
+
+ /*
+ * The checks against readonly and busy are redundant, but certainly
+ * do no harm. checks against inconsistent are crucial:
+ */
+ if (bdrv_dirty_bitmap_check(bitmap, BDRV_BITMAP_DEFAULT, errp)) {
+ ret = -ENOTSUP;
+ goto out;
+ }
+ }
+
+out:
+ bitmap_list_free(bm_list);
+ return ret;
+}
+
/* store_bitmap_data()
* Store bitmap to image, filling bitmap table accordingly.
*/
@@ -1424,9 +1488,9 @@ void qcow2_store_persistent_dirty_bitmaps(BlockDriverState *bs, Error **errp)
uint32_t granularity = bdrv_dirty_bitmap_granularity(bitmap);
Qcow2Bitmap *bm;
- if (!bdrv_dirty_bitmap_get_persistance(bitmap) ||
- bdrv_dirty_bitmap_readonly(bitmap))
- {
+ if (!bdrv_dirty_bitmap_get_persistence(bitmap) ||
+ bdrv_dirty_bitmap_readonly(bitmap) ||
+ bdrv_dirty_bitmap_inconsistent(bitmap)) {
continue;
}
@@ -1542,7 +1606,7 @@ int qcow2_reopen_bitmaps_ro(BlockDriverState *bs, Error **errp)
for (bitmap = bdrv_dirty_bitmap_next(bs, NULL); bitmap != NULL;
bitmap = bdrv_dirty_bitmap_next(bs, bitmap))
{
- if (bdrv_dirty_bitmap_get_persistance(bitmap)) {
+ if (bdrv_dirty_bitmap_get_persistence(bitmap)) {
bdrv_dirty_bitmap_set_readonly(bitmap, true);
}
}