From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
Subject: [Devel] [PATCH VZ10 07/10] drivers/md/dm-qcow2: unmap cluster when discard clears last allocated subclusters
Date: Wed, 12 Aug 2026 21:56:35 +0300 [thread overview]
Message-ID: <20260812185638.110779-8-andrey.zhadchenko@virtuozzo.com> (raw)
In-Reply-To: <20260812185638.110779-1-andrey.zhadchenko@virtuozzo.com>
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@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 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
next prev parent reply other threads:[~2026-08-12 18:56 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 18:56 [Devel] [PATCH VZ10 00/10] dm-qcow2: improve discard and read-only merge handling Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 01/10] drivers/md/dm-qcow2: fix revert_cluster_alloc() for ext_l2 case Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 02/10] drivers/md/dm-qcow2: generalize COW index update machinery Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 03/10] drivers/md/dm-qcow2: update metadata on whole cluster discard Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 04/10] drivers/md/dm-qcow2: never trigger COW or allocation on discard Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 05/10] drivers/md/dm-qcow2: set L2_READS_ALL_ZEROES after some discards Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 06/10] drivers/md/dm-qcow2: update metadata on subclusters discard Andrey Zhadchenko
2026-08-12 18:56 ` Andrey Zhadchenko [this message]
2026-08-12 18:56 ` [Devel] [PATCH VZ10 08/10] drivers/md/dm-qcow2: do discards during backward merge only for writable image Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 09/10] drivers/md/dm-qcow2: allow shared L1 entries during merge from RO image Andrey Zhadchenko
2026-08-12 18:56 ` [Devel] [PATCH VZ10 10/10] drivers/md/dm-qcow2: respect zeroes during merge Andrey Zhadchenko
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=20260812185638.110779-8-andrey.zhadchenko@virtuozzo.com \
--to=andrey.zhadchenko@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