Skip to contentSkip to conversation

block: block-jobs and bitmaps patches

GIT PULL Applied Vladimir Sementsov-Ogievskiy 2 participants 4 messages Oct 19 Block layer

lore
Share & tools
lore
#0 lore raw

The following changes since commit 62c31d1b6aa8e255c528df41c51a35dec9516be9:

Merge tag 'pull-riscv-to-apply-20261016' of https://github.com/alistair23/qemu into staging (2026-10-16 06:09:05 +0200)

are available in the Git repository at:

https://gitlab.com/vsementsov/qemu.git tags/pull-block-2026-10-16

for you to fetch changes up to 5689c59055e7652cc4b6ac0a47b6e48faad4d59d:

block/mirror: inherit the source node discard flag in mirror_top (2026-10-16 09:46:54 +0300)

----------------------------------------------------------------
block: block-jobs and bitmaps patches

----------------------------------------------------------------

Haoqian He (1):
block/mirror: inherit the source node discard flag in mirror_top

Jean-Louis Dupond (1):
qcow2: add functionality to repair bitmaps

 block/mirror.c         | 10 ++++++++--
 block/qcow2-bitmap.c   | 41 ++++++++++++++++++++++++++++++++++++++---
 block/qcow2-refcount.c |  3 ++-
 block/qcow2.c          | 16 ++++++++++++----
 block/qcow2.h          |  3 ++-
 5 files changed, 62 insertions(+), 11 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

[PULL 1/2] qcow2: add functionality to repair bitmaps

From: Jean-Louis Dupond <[email protected]>

In some cases, it might happen that bitmaps become corrupt. For example when adding/removing bitmaps on a live image.

Of course this should not happen, but in case this happens, the image is corrupt and even cannot be opened anymore. You'll get something like the following:
qemu-img: Could not open 'disk.qcow2': Bitmap '' doesn't satisfy the constraints

So the image becomes useless, and cannot be repaired. This while in fact only (one) bitmap entry is corrupt, and the rest of the data is just intact.

This commit adds a way to fix this corruption, by just replacing the bitmap list in the qcow2 image with the valid bitmaps, and dropping the bitmaps that are corrupt.

$ qemu-img check disk.qcow2
qemu-img: Check failed: Invalid argument
qemu-img: Lost persistent bitmaps during inactivation of node '#block147': Bitmap '' doesn't satisfy the constraints
$ qemu-img check -r all disk.qcow2
qcow2_free_clusters failed: Invalid argument
Leaked cluster 3 refcount=1 reference=0
Leaked cluster 26 refcount=1 reference=0
ERROR cluster 983214 refcount=0 reference=1
Rebuilding refcount structure
Repairing cluster 1 refcount=1 reference=0
Repairing cluster 2 refcount=1 reference=0
Repairing cluster 32768 refcount=1 reference=0
....
Repairing cluster 983214 refcount=1 reference=0
The following inconsistencies were found and repaired:

2 leaked clusters
3 corruptions

Double checking the fixed image now...
No errors were found on the image.
983056/1048576 = 93.75% allocated, 0.05% fragmented, 0.00% compressed clusters Image end offset: 64437878784

And the image is valid again!
Worst case you lose all bitmaps, but at least the image and the data itself is useable again.

Message-ID: <[email protected]>

---
 block/qcow2-bitmap.c   | 41 ++++++++++++++++++++++++++++++++++++++---
 block/qcow2-refcount.c |  3 ++-
 block/qcow2.c          | 16 ++++++++++++----
 block/qcow2.h          |  3 ++-
 4 files changed, 54 insertions(+), 9 deletions(-)
block/qcow2-bitmap.c +38 -3
index ac5a7245885..853181da633 100644
@@ -100,6 +100,9 @@ typedef enum BitmapType {
     BT_DIRTY_TRACKING_BITMAP = 1
 } BitmapType;
 
+static int GRAPH_RDLOCK
+update_ext_header_and_dir(BlockDriverState *bs, Qcow2BitmapList *bm_list);
+
 static inline bool can_write(BlockDriverState *bs)
 {
     return !bdrv_is_read_only(bs) && !(bdrv_get_flags(bs) & BDRV_O_INACTIVE);
@@ -655,12 +658,14 @@ fail:
 int coroutine_fn
 qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                               void **refcount_table,
-                              int64_t *refcount_table_size)
+                              int64_t *refcount_table_size,
+                              BdrvCheckMode fix)
 {
     int ret;
     BDRVQcow2State *s = bs->opaque;
     Qcow2BitmapList *bm_list;
-    Qcow2Bitmap *bm;
+    Qcow2Bitmap *bm, *bm_next;
+    int bm_list_fixed = 0;
 
     if (s->nb_bitmaps == 0) {
         return 0;
@@ -677,12 +682,21 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                                s->bitmap_directory_size, NULL);
     if (bm_list == NULL) {
         res->corruptions++;
+
+        if (fix & BDRV_FIX_ERRORS) {
+            ret = update_ext_header_and_dir(bs, NULL);
+            if (ret >= 0) {
+                res->corruptions_fixed++;
+            }
+            return ret;
+        }
         return -EINVAL;
     }
 
-    QSIMPLEQ_FOREACH(bm, bm_list, entry) {
+    QSIMPLEQ_FOREACH_SAFE(bm, bm_list, entry, bm_next) {
         uint64_t *bitmap_table = NULL;
         int i;
+        int nb_corrupted_entries = 0;
 
         ret = qcow2_inc_refcounts_imrt(bs, res,
                                        refcount_table, refcount_table_size,
@@ -695,6 +709,12 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
         ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
         if (ret < 0) {
             res->corruptions++;
+            if (fix & BDRV_FIX_ERRORS) {
+                QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
+                bitmap_free(bm);
+                bm_list_fixed++;
+                continue;
+            }
             goto out;
         }
 
@@ -704,6 +724,7 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
 
             if (check_table_entry(entry, s->cluster_size) < 0) {
                 res->corruptions++;
+                nb_corrupted_entries++;
                 continue;
             }
 
@@ -720,9 +741,23 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
             }
         }
 
+        if ((fix & BDRV_FIX_ERRORS) && nb_corrupted_entries > 0) {
+            QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry);
+            bitmap_free(bm);
+            bm_list_fixed += nb_corrupted_entries;
+        }
+
         g_free(bitmap_table);
     }
 
+    /* If fixing, update the bitmap directory with the repaired list */
+    if ((fix & BDRV_FIX_ERRORS) && bm_list_fixed > 0) {
+        ret = update_ext_header_and_dir(bs, bm_list);
+        if (ret >= 0) {
+            res->corruptions_fixed += bm_list_fixed;
+        }
+    }
+
 out:
     bitmap_list_free(bm_list);
 
block/qcow2-refcount.c +2 -1
index 6512cda407c..df7f6d2f234 100644
@@ -2305,7 +2305,8 @@ calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
     }
 
     /* bitmaps */
-    ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clusters);
+    ret = qcow2_check_bitmaps_refcounts(bs, res, refcount_table,
+                                        nb_clusters, fix);
     if (ret < 0) {
         return ret;
     }
block/qcow2.c +12 -4
index 1543255eba8..0358bc04f33 100644
@@ -1927,11 +1927,19 @@ qcow2_do_open(BlockDriverState *bs, QDict *options, int flags,
     if (!(bdrv_get_flags(bs) & BDRV_O_INACTIVE)) {
         /* It's case 1, 2 or 3.2. Or 3.1 which is BUG in management layer. */
         bool header_updated;
-        if (!qcow2_load_dirty_bitmaps(bs, &header_updated, errp)) {
-            ret = -EINVAL;
-            goto fail;
+        Error *local_err = NULL;
+        if (!qcow2_load_dirty_bitmaps(bs, &header_updated, &local_err)) {
+            /*
+             * Allow this to fail in check mode
+             * because otherwise we can't open the image at all.
+             */
+            if (!(flags & BDRV_O_CHECK)) {
+                ret = -EINVAL;
+                error_propagate(errp, local_err);
+                goto fail;
+            }
+            error_reportf_err(local_err, "Could not load dirty bitmaps: ");
         }
-
         update_header = update_header && !header_updated;
     }
 
block/qcow2.h +2 -1
index ce517040c47..ba9fe681308 100644
@@ -1038,7 +1038,8 @@ void qcow2_cache_discard(Qcow2Cache *c, void *table);
 int coroutine_fn GRAPH_RDLOCK
 qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                               void **refcount_table,
-                              int64_t *refcount_table_size);
+                              int64_t *refcount_table_size,
+                              BdrvCheckMode fix);
 
 bool coroutine_fn GRAPH_RDLOCK
 qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated,
Signature · 1 line
2.43.0
#2 lore raw

[PULL 2/2] block/mirror: inherit the source node discard flag in mirror_top

In reply to Vladimir Sementsov-Ogievskiy

From: Haoqian He <[email protected]>

mirror_start_job() inserts a mirror_top filter above the source node for both mirror and active commit jobs. Previously, the filter was opened with BDRV_O_RDWR only, even when the source had BDRV_O_UNMAP enabled.

The block layer checks BDRV_O_UNMAP before invoking a node's discard callback. Without this flag on mirror_top, discard requests return success without reaching bdrv_mirror_top_pdiscard(). Consequently, starting a job can silently suppress guest UNMAP/TRIM requests that would otherwise reach the discard-enabled source.

Initialize the filter's open flags with BDRV_O_RDWR and conditionally add BDRV_O_UNMAP from bs->open_flags. Pass the resulting flags to bdrv_new_open_driver() so that mirror_top follows the source node's discard setting.

This allows discard requests to reach the existing filter callback, which forwards them to the source and either tracks the affected range in the dirty bitmap or synchronously propagates the discard to the target, depending on the copy mode.

Message-ID: <[email protected]>

---
 block/mirror.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/mirror.c +8 -2
index 089856f4a84..a551d392c66 100644
@@ -1854,6 +1854,7 @@ static BlockJob *mirror_start_job(
     BlockDriverState *mirror_top_bs;
     bool target_is_backing;
     uint64_t target_perms, target_shared_perms;
+    int flags;
     int ret;
 
     GLOBAL_STATE_CODE();
@@ -1886,8 +1887,13 @@ static BlockJob *mirror_start_job(
     /* In the case of active commit, add dummy driver to provide consistent
      * reads on the top, while disabling it in the intermediate nodes, and make
      * the backing chain writable. */
-    mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top, filter_node_name,
-                                         BDRV_O_RDWR, errp);
+    flags = BDRV_O_RDWR;
+    if (bs->open_flags & BDRV_O_UNMAP) {
+        flags |= BDRV_O_UNMAP;
+    }
+    mirror_top_bs = bdrv_new_open_driver(&bdrv_mirror_top,
+                                         filter_node_name,
+                                         flags, errp);
     if (mirror_top_bs == NULL) {
         return NULL;
     }
Signature · 1 line
2.43.0
#3 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

The following changes since commit 62c31d1b6aa8e255c528df41c51a35dec9516be9:

   Merge tag 'pull-riscv-to-apply-20261016' ofhttps://github.com/alistair23/qemu into staging (2026-10-16 06:09:05 +0200)

are available in the Git repository at:

Quoted 8 lines
   https://gitlab.com/vsementsov/qemu.git tags/pull-block-2026-10-16

for you to fetch changes up to 5689c59055e7652cc4b6ac0a47b6e48faad4d59d:

   block/mirror: inherit the source node discard flag in mirror_top (2026-10-16 09:46:54 +0300)

----------------------------------------------------------------
block: block-jobs and bitmaps patches

Applied, thanks.

r~