[Devel] [PATCH hci-8.2] dm-qcow2: report the unit L2 can unmap as discard granularity
Vasileios Anagnostoulis
vasileios.anagnostoulis at virtuozzo.com
Wed Nov 25 14:16:15 MSK 2026
qcow2_io_hints() reports the block size of the host file system as
discard_granularity and one cluster as max_hw_discard_sectors.
bio_split_discard() cuts a longer discard into pieces of
max_discard_sectors and aligns each split point only to
discard_granularity, i.e. to 4K. A discard that does not start on a
cluster boundary is therefore cut at the same offset inside every
cluster it spans.
dm-qcow2 unmaps only a subcluster (with extended_l2) or a cluster
(without it) that a single discard qio covers entirely. The unit under
each split point is covered by two qios, each only in part, so neither
unmaps it: its host blocks are punched, but it stays allocated in L2
and the cluster stays allocated in the refcount table. Every long
discard leaves such a unit behind in each cluster it spans, whether it
comes from the FITRIM of ploop-balloon discard or from fstrim inside a
Container.
On a 512M image with 1M clusters and extended_l2, one FITRIM after
freeing half of ext4 left 83 clusters that ext4 no longer uses mapped,
each with exactly one 32K subcluster still allocated.
Report the unit L2 can unmap as discard_granularity: the subcluster
with extended_l2, the cluster otherwise. bio_split_discard() then cuts
discards on unit boundaries. dm-ploop already reports its cluster.
https://virtuozzo.atlassian.net/browse/VSTOR-154496
Fixes: 2a728299abdb ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Vasileios Anagnostoulis <vasileios.anagnostoulis at virtuozzo.com>
---
Tested on 6.12.0-211.75.1.24.1.vz10, with dm-qcow2 rebuilt from the
rh10-6.12.0-211.75.1.24.1.vz10 sources plus this patch and loaded in
place of the stock module. qcow2_io_hints() and the discard path are
the same there as on hci-8.2. Ploop test suite (ploop.ovz, test/):
# python3 -m pytest tests/test_ploop_balloon.py -k shrunk_offline \
--format qcow2 --bug-id VSTOR-154496
test_ploop_balloon_discard_should_release_removed_data_when_image_shrunk_offline PASSED
This is the reproducer from VSTOR-154496 (512M image, 1M clusters,
extended_l2). Without the fix, 83 clusters that ext4 no longer used
stayed mapped after the FITRIM.
# python3 -m pytest tests/test_ploop_balloon.py --format qcow2
33 passed, 23 deselected
The reproducer is among the deselected tests: the suite runs it only
with --bug-id, as above.
---
drivers/md/dm-qcow2-target.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index d445bfe1232a..a343266fb1be 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -1117,10 +1117,15 @@ static void qcow2_io_hints(struct dm_target *ti, struct queue_limits *limits)
if (sb->s_bdev)
block_size = bdev_logical_block_size(sb->s_bdev);
/*
- * Even if this is less than discard_granularity of bdev,
- * we can free a block on filesystem.
+ * Report the unit L2 can unmap: the subcluster with extended L2,
+ * the cluster otherwise. The block layer aligns discard split
+ * points to this granularity only; with a smaller one, a long
+ * discard that starts mid-cluster is split at the same offset
+ * inside every cluster, and the (sub)cluster under each split is
+ * covered only in part by both pieces, so neither unmaps it.
*/
- limits->discard_granularity = sb->s_blocksize;
+ limits->discard_granularity = qcow2->ext_l2 ? qcow2->subclu_size :
+ qcow2->clu_size;
limits->max_hw_discard_sectors = to_sector(qcow2->clu_size);
limits->max_discard_segments = 1;
---
base-commit: aef96420256e57bf1291859a844800a462f2790e
change-id: 20261125-br-vstor-154496-42580b50cfb2
Best regards,
--
Vasileios Anagnostoulis <vasileios.anagnostoulis at virtuozzo.com>
More information about the Devel
mailing list