From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andrey Zhadchenko Date: Wed, 12 Aug 2026 21:56:35 +0300 Subject: [Devel] [PATCH VZ10 07/10] drivers/md/dm-qcow2: unmap cluster when discard clears last allocated subclusters In-Reply-To: <20260812185638.110779-1-andrey.zhadchenko@virtuozzo.com> References: <20260812185638.110779-1-andrey.zhadchenko@virtuozzo.com> Message-ID: <20260812185638.110779-8-andrey.zhadchenko@virtuozzo.com> List-Id: 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 --- 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 bc50af2ec205b..f80504927eaae 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 */ @@ -3390,6 +3402,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. */ @@ -3400,7 +3414,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; @@ -3418,6 +3432,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; @@ -3429,7 +3445,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; @@ -3453,7 +3469,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