From nobody Mon Nov 23 04:44:18 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1792393165; cv=none; d=zohomail.com; s=zohoarc; b=FDpnqW+EC9uf9515JlHPdUjEUMx5FQR0HM8H86Rk7GP5DxLKhrlLPD+PdvnfRizWm4wksz5fXY3umlz4GVRQNX/k/8W+wAxGERVMdXurezMQ+JCgg98TJYUAO6cljrBaYSEX3nJdhavLMSIUc6ww9oh4xGh+CG+HiJY79ivYlBw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792393165; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=MhVHhUFb1K3DTgCkBAkxGSeq4zbx3cm08wzfDRn+aUQ=; b=mWY8SBGNIpXREoDwtqMh8emF6Be0olx+nOQC5z6t9mHWMZzn2lPZoV/ZODxFpsxX3RlKFVr7orthvoissEB06jFudKLD8w5c6lOBHuJiflCCHL31LFzJ8L0s5xsG/BSJRNEbnp2B+K2dF5yCnZJJPQhrNRvm6jHfGD9nzowfCU0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792393164290460.0666754042894; Sun, 18 Oct 2026 23:59:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xIhKm-0006x7-C8; Mon, 19 Oct 2026 02:59:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIhKk-0006vP-Lz; Mon, 19 Oct 2026 02:58:58 -0400 Received: from forwardcorp2d.mail.yandex.net ([178.154.239.207]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIhKi-0006lA-Ff; Mon, 19 Oct 2026 02:58:58 -0400 Received: from mail-nwsmtp-smtp-corp-main-68.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-68.yp-c.yandex.net) by forwardcorp2d.mail.yandex.net (postfix) with ESMTPS id 01CC1809F8; Mon, 19 Oct 2026 09:58:51 +0300 (MSK) Received: from i115954770.yandex-team.ru by mail-nwsmtp-smtp-corp-main-68 with ESMTPSA id nwSS6342ba60-lxpKuEEb; Mon, 19 Oct 2026 09:58:50 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1792393130; bh=MhVHhUFb1K3DTgCkBAkxGSeq4zbx3cm08wzfDRn+aUQ=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=UEPzPdtjCDMOOb22xMe0c09gVIMPMTH3O89VthmYSE/u8J1Z0SnZl9xGY/j0xCr1G UoBZzKJVayvGr0QYGUPGNvRGPF6Pi1uEPa044v38kuju8Z3ZG6a4Z+d0oxIf34Esqw wwwChcuMUIKtycJLu9OKkpWQ0h+mToWZwYn868ls= Authentication-Results: mail-nwsmtp-smtp-corp-main-68; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, vsementsov@yandex-team.ru, Jean-Louis Dupond Subject: [PULL 1/2] qcow2: add functionality to repair bitmaps Date: Mon, 19 Oct 2026 09:58:27 +0300 Message-ID: <20261019065828.16335-2-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261019065828.16335-1-vsementsov@yandex-team.ru> References: <20261019065828.16335-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=178.154.239.207; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp2d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1792393168251158500 Content-Type: text/plain; charset="utf-8" From: Jean-Louis Dupond 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 constr= aints 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=3D1 reference=3D0 Leaked cluster 26 refcount=3D1 reference=3D0 ERROR cluster 983214 refcount=3D0 reference=3D1 Rebuilding refcount structure Repairing cluster 1 refcount=3D1 reference=3D0 Repairing cluster 2 refcount=3D1 reference=3D0 Repairing cluster 32768 refcount=3D1 reference=3D0 .... Repairing cluster 983214 refcount=3D1 reference=3D0 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 =3D 93.75% allocated, 0.05% fragmented, 0.00% compressed clu= sters 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. Signed-off-by: Jean-Louis Dupond Message-ID: <20260826101005.2345883-2-jean-louis@dupond.be> Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Vladimir Sementsov-Ogievskiy --- 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(-) diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c index ac5a7245885..853181da633 100644 --- a/block/qcow2-bitmap.c +++ b/block/qcow2-bitmap.c @@ -100,6 +100,9 @@ typedef enum BitmapType { BT_DIRTY_TRACKING_BITMAP =3D 1 } BitmapType; =20 +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_INACTIV= E); @@ -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 =3D bs->opaque; Qcow2BitmapList *bm_list; - Qcow2Bitmap *bm; + Qcow2Bitmap *bm, *bm_next; + int bm_list_fixed =3D 0; =20 if (s->nb_bitmaps =3D=3D 0) { return 0; @@ -677,12 +682,21 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, B= drvCheckResult *res, s->bitmap_directory_size, NULL); if (bm_list =3D=3D NULL) { res->corruptions++; + + if (fix & BDRV_FIX_ERRORS) { + ret =3D update_ext_header_and_dir(bs, NULL); + if (ret >=3D 0) { + res->corruptions_fixed++; + } + return ret; + } return -EINVAL; } =20 - QSIMPLEQ_FOREACH(bm, bm_list, entry) { + QSIMPLEQ_FOREACH_SAFE(bm, bm_list, entry, bm_next) { uint64_t *bitmap_table =3D NULL; int i; + int nb_corrupted_entries =3D 0; =20 ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_size, @@ -695,6 +709,12 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, Bd= rvCheckResult *res, ret =3D 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; } =20 @@ -704,6 +724,7 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, Bdr= vCheckResult *res, =20 if (check_table_entry(entry, s->cluster_size) < 0) { res->corruptions++; + nb_corrupted_entries++; continue; } =20 @@ -720,9 +741,23 @@ qcow2_check_bitmaps_refcounts(BlockDriverState *bs, Bd= rvCheckResult *res, } } =20 + if ((fix & BDRV_FIX_ERRORS) && nb_corrupted_entries > 0) { + QSIMPLEQ_REMOVE(bm_list, bm, Qcow2Bitmap, entry); + bitmap_free(bm); + bm_list_fixed +=3D nb_corrupted_entries; + } + g_free(bitmap_table); } =20 + /* If fixing, update the bitmap directory with the repaired list */ + if ((fix & BDRV_FIX_ERRORS) && bm_list_fixed > 0) { + ret =3D update_ext_header_and_dir(bs, bm_list); + if (ret >=3D 0) { + res->corruptions_fixed +=3D bm_list_fixed; + } + } + out: bitmap_list_free(bm_list); =20 diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 6512cda407c..df7f6d2f234 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -2305,7 +2305,8 @@ calculate_refcounts(BlockDriverState *bs, BdrvCheckRe= sult *res, } =20 /* bitmaps */ - ret =3D qcow2_check_bitmaps_refcounts(bs, res, refcount_table, nb_clus= ters); + ret =3D qcow2_check_bitmaps_refcounts(bs, res, refcount_table, + nb_clusters, fix); if (ret < 0) { return ret; } diff --git a/block/qcow2.c b/block/qcow2.c index 1543255eba8..0358bc04f33 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -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 =3D -EINVAL; - goto fail; + Error *local_err =3D 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 =3D -EINVAL; + error_propagate(errp, local_err); + goto fail; + } + error_reportf_err(local_err, "Could not load dirty bitmaps: "); } - update_header =3D update_header && !header_updated; } =20 diff --git a/block/qcow2.h b/block/qcow2.h index ce517040c47..ba9fe681308 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -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); =20 bool coroutine_fn GRAPH_RDLOCK qcow2_load_dirty_bitmaps(BlockDriverState *bs, bool *header_updated, --=20 2.43.0 From nobody Mon Nov 23 04:44:18 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1792393189; cv=none; d=zohomail.com; s=zohoarc; b=am74m+SHTVkv8ps3bpHOTgdL+jvZ2C4PP4xT44aWmY1x6NJd53W18+/fQvGciO/6FsAB8Y+hCIwxF/8UVmdegnJ/oN+Mpon+wfJ4Cv0bmQfZa0NZUeLiKJ8azy5+ENoCvXpXgvMNVteUbQnp5VXFejGZHSOw/norn7cSsqpleF8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1792393189; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=U+McxGX0whB/xNmAgaoCFyDpykoIgtkfN0wL4bdZxzg=; b=mktqTRLRmPulhSIiOUArsnMFC5MAXBnuSi0w+b2rVDBh4yo07VPNO8TfKdJARL7e4rJlNJu6++d1J+LIoEVRPhzJoChTE0MnN6JUSwC3yGlQqkXpIxEQhl28Ajgdb+DBPIskcCKs1tDdOTOLfai+AhJExyQ2TEqwPe64uGAJwto= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1792393189523947.5267999928416; Sun, 18 Oct 2026 23:59:49 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xIhKp-000729-O5; Mon, 19 Oct 2026 02:59:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIhKl-0006wB-GD; Mon, 19 Oct 2026 02:58:59 -0400 Received: from forwardcorp1d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:df01]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xIhKi-0006lD-Hy; Mon, 19 Oct 2026 02:58:59 -0400 Received: from mail-nwsmtp-smtp-corp-main-68.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-68.yp-c.yandex.net) by forwardcorp1d.mail.yandex.net (postfix) with ESMTPS id 03A9080804; Mon, 19 Oct 2026 09:58:52 +0300 (MSK) Received: from i115954770.yandex-team.ru by mail-nwsmtp-smtp-corp-main-68 with ESMTPSA id nwSS6342ba60-J69Jh7rT; Mon, 19 Oct 2026 09:58:51 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1792393131; bh=U+McxGX0whB/xNmAgaoCFyDpykoIgtkfN0wL4bdZxzg=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=0p5KGPu5wlTtXhRu6GslIB4LjPYJ23rbSgl8toQjsYogkyvvfnUgS5UQqGx4lc5IY k/4cUOcctu+iTKvvWdKzEj2XWMdButLP1fzMbD6KIZItXE5pXqjpwIOo06lUcZe4uQ JNdYSUQHdg+SIbtQq32AwEEY8MiUixjUkmS2ZbGA= Authentication-Results: mail-nwsmtp-smtp-corp-main-68; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org Cc: qemu-block@nongnu.org, vsementsov@yandex-team.ru, Haoqian He Subject: [PULL 2/2] block/mirror: inherit the source node discard flag in mirror_top Date: Mon, 19 Oct 2026 09:58:28 +0300 Message-ID: <20261019065828.16335-3-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261019065828.16335-1-vsementsov@yandex-team.ru> References: <20261019065828.16335-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a02:6b8:c41:1300:1:45:d181:df01; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1792393190429158500 Content-Type: text/plain; charset="utf-8" From: Haoqian He 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. Signed-off-by: Haoqian He Message-ID: <20261013060549.3008438-1-haoqian.he@smartx.com> Reviewed-by: Vladimir Sementsov-Ogievskiy Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/mirror.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/block/mirror.c b/block/mirror.c index 089856f4a84..a551d392c66 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -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; =20 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 =3D bdrv_new_open_driver(&bdrv_mirror_top, filter_node_n= ame, - BDRV_O_RDWR, errp); + flags =3D BDRV_O_RDWR; + if (bs->open_flags & BDRV_O_UNMAP) { + flags |=3D BDRV_O_UNMAP; + } + mirror_top_bs =3D bdrv_new_open_driver(&bdrv_mirror_top, + filter_node_name, + flags, errp); if (mirror_top_bs =3D=3D NULL) { return NULL; } --=20 2.43.0