From: "Denis V. Lunev" <den@openvz.org>
To: andrey.drobyshev@virtuozzo.com
Cc: svt-core@virtuozzo.com, den@openvz.org
Subject: [PATCH hci-8.1 1/1] qcow2: fix subcluster discard punching data shared with a snapshot #VSTOR-144873
Date: Mon, 26 Oct 2026 20:37:15 +0100 [thread overview]
Message-ID: <20261026193715.828224-2-den@openvz.org> (raw)
In-Reply-To: <20261026193715.828224-1-den@openvz.org>
From: Denis V. Lunev <den@openvz.org>
discard_l2_subclusters() passes the discarded range down to the host
file even when the cluster is still referenced by an internal snapshot.
The active L2 table is copied before it is changed, so the snapshot
metadata stays correct, but the host range under it is punched out and
the snapshot reads zeroes there afterwards. qemu-img check finds nothing
wrong.
Pass the discard down only for a cluster with QCOW_OFLAG_COPIED set,
i.e. one owned by the active layer alone. A shared cluster gets only
its active L2 bitmap updated, as a whole-cluster discard already does
by dropping the refcount instead of punching the host file.
Cover it in iotest 271.
Fixes: 225b4de1249 ("qcow2: make subclusters discardable")
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
block/qcow2-cluster.c | 10 +++++++---
tests/qemu-iotests/271 | 18 ++++++++++++++++++
tests/qemu-iotests/271.out | 5 +++++
3 files changed, 30 insertions(+), 3 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index e40995904b5..dfe728cd8a5 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -2196,9 +2196,13 @@ discard_l2_subclusters(BlockDriverState *bs, uint64_t offset,
qcow2_cache_entry_mark_dirty(s->l2_table_cache, scri.l2_slice);
}
- discard_no_unref_any_file(
- bs, (scri.l2_entry & L2E_OFFSET_MASK) + offset_into_cluster(s, offset),
- nb_subclusters * s->subcluster_size, scri.ctype, type);
+ /* A cluster shared with a snapshot must keep its data */
+ if (scri.l2_entry & QCOW_OFLAG_COPIED) {
+ discard_no_unref_any_file(
+ bs,
+ (scri.l2_entry & L2E_OFFSET_MASK) + offset_into_cluster(s, offset),
+ nb_subclusters * s->subcluster_size, scri.ctype, type);
+ }
return 0;
}
diff --git a/tests/qemu-iotests/271 b/tests/qemu-iotests/271
index 026a40cdaed..2f60fb5fc96 100755
--- a/tests/qemu-iotests/271
+++ b/tests/qemu-iotests/271
@@ -605,6 +605,24 @@ done
############################################################
############################################################
+echo
+echo "### Discarding subclusters shared with an internal snapshot ###"
+echo
+
+_make_test_img -o extended_l2=on 1M
+$QEMU_IO -c "write -q -P 0xab 0 64k" "$TEST_IMG" | _filter_qemu_io
+$QEMU_IMG snapshot -c snap "$TEST_IMG"
+$QEMU_IO -c "discard -q 0 4k" -c "write -q -z -u 8k 4k" "$TEST_IMG" | _filter_qemu_io
+$QEMU_IO -c "read -q -P 0 0 4k" -c "read -q -P 0xab 4k 4k" \
+ -c "read -q -P 0 8k 4k" "$TEST_IMG" | _filter_qemu_io
+$QEMU_IMG snapshot -a snap "$TEST_IMG"
+$QEMU_IO -c "read -q -P 0xab 0 64k" "$TEST_IMG" | _filter_qemu_io
+_check_test_img
+
+############################################################
+############################################################
+############################################################
+
# Test that corrupted L2 entries are detected in both read and write
# operations
for corruption_test_cmd in read write; do
diff --git a/tests/qemu-iotests/271.out b/tests/qemu-iotests/271.out
index c03e4c9bc2a..e7970d33516 100644
--- a/tests/qemu-iotests/271.out
+++ b/tests/qemu-iotests/271.out
@@ -504,6 +504,11 @@ Image resized.
Image resized.
L2 entry #1: 0x0000000000000000 0000000000000000
+### Discarding subclusters shared with an internal snapshot ###
+
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576
+No errors were found on the image.
+
### Corrupted L2 entries - read test (allocated) ###
# 'cluster is zero' bit set on the standard cluster descriptor
--
2.53.0
next prev parent reply other threads:[~2026-10-26 19:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-26 19:37 [PATCH hci-8.1 0/1] qcow2: fix guest TRIM destroying internal snapshot data #VSTOR-144873 Denis V. Lunev
2026-10-26 19:37 ` Denis V. Lunev [this message]
2026-10-27 9:21 ` [PATCH hci-8.1 1/1] qcow2: fix subcluster discard punching data shared with a snapshot #VSTOR-144873 Andrey Drobyshev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261026193715.828224-2-den@openvz.org \
--to=den@openvz.org \
--cc=andrey.drobyshev@virtuozzo.com \
--cc=svt-core@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox