[Devel] [PATCH VZ10 v2 07/10] drivers/md/dm-qcow2: unmap cluster when discard clears last allocated subclusters

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Wed Aug 12 23:20:02 MSK 2026


A subclusters discard only clears bits in the extended L2 bitmap and
keeps the cluster mapped. Teach dm-qcow2 to clear L2 entry if every
subcluster is discarded.

Feature: dm-qcow2: block device over QCOW2 files driver
https://virtuozzo.atlassian.net/browse/VSTOR-139406
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 drivers/md/dm-qcow2-map.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 9b40a985ac3cc..dbf5464eb9ab0 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -122,6 +122,17 @@ static u32 qio_full_subclus_mask(struct qcow2 *qcow2, struct qio *qio)
 	return GENMASK(end_bit - 1, first_bit);
 }
 
+static bool qio_discard_unmaps_cluster(struct qcow2 *qcow2, struct qio *qio,
+				       struct qcow2_map *map)
+{
+	if (qio_covers_full_clu(qcow2, qio))
+		return true;
+	if (!qcow2->ext_l2 || map->compressed)
+		return false;
+	return (u32)map->ext_l2 &&
+	       !((u32)map->ext_l2 & ~qio_full_subclus_mask(qcow2, qio));
+}
+
 static loff_t compressed_clu_end_pos(loff_t start, sector_t compressed_sectors)
 {
 	if (start % SECTOR_SIZE == 0)
@@ -2044,9 +2055,10 @@ static int parse_metadata(struct qcow2 *qcow2, struct qio **qio,
 	if (!write)
 		return 0;
 
-	/* discards also need to update r1r2 */
+	/* cluster unmapping discards also need to update r1r2 */
 	if (!map->clu_is_cow &&
-	    !(op_is_discard((*qio)->bi_op) && qio_covers_full_clu(qcow2, *qio)))
+	    !(op_is_discard((*qio)->bi_op) &&
+	      qio_discard_unmaps_cluster(qcow2, *qio, map)))
 		return 0;
 
 	/* Now refcounters table/block */
@@ -3399,6 +3411,8 @@ static bool qio_discard_updates_metadata(struct qcow2 *qcow2, struct qio *qio,
  * Subclusters fully covered by the discard are marked unallocated in
  * ext_l2 bitmap: the cluster itself remains allocated, so refcounts
  * are not touched (hence empty unuse range).
+ * Discarding last subclusters in a cluster clears it from L2 like a
+ * whole cluster discard.
  * If the backing is present, set 'reads as zeroes' to avoid exposing
  * stale data.
  */
@@ -3409,7 +3423,7 @@ static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio,
 	u32 index_in_page = map->l2.index_in_page;
 	struct md_page *md = map->l2.md;
 	loff_t unuse_pos, unuse_end;
-	bool whole_clu;
+	bool whole_clu, unmap;
 	u64 new_ext_l2 = 0;
 	struct qio_ext *ext;
 	int ret;
@@ -3427,6 +3441,8 @@ static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio,
 	spin_unlock_irq(&qcow2->md_pages_lock);
 
 	whole_clu = qio_covers_full_clu(qcow2, *qio);
+	unmap = qio_discard_unmaps_cluster(qcow2, *qio, map);
+
 	if (whole_clu) {
 		if (zeroes && qcow2->ext_l2)
 			new_ext_l2 = (u64)U32_MAX << 32;
@@ -3438,7 +3454,7 @@ static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio,
 			new_ext_l2 |= mask << 32;
 	}
 
-	if (whole_clu) {
+	if (unmap) {
 		if (map->clu_is_cow) {
 			/* Cluster is shared or compressed. Decrement refcount. */
 			unuse_pos = map->cow_clu_pos;
@@ -3462,7 +3478,7 @@ static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio,
 	ext->lx_md = md;
 
 	ext->new_ext_l2 = new_ext_l2;
-	if (!whole_clu)
+	if (!unmap)
 		ext->only_set_ext_l2 = true;
 	else if (zeroes && !qcow2->ext_l2)
 		ext->set_all_zeroes = true;
-- 
2.43.5



More information about the Devel mailing list