[Devel] [PATCH hci-8.2 3/4] dm-qcow2: delay a backing-file COW while its L2 entry awaits writeback

Denis V. Lunev den at openvz.org
Thu Nov 19 04:08:53 MSK 2026


From: Denis V. Lunev <den at openvz.org>

On an ext_l2 image with a backing file, a write that covers part of an
unallocated subcluster is a backing-file COW. Writes are not delayed on
a dirty L2 entry, so such a write can follow, in the same worker pass,
the write that allocated its cluster. It finds the cluster in memory,
keeps it (only_set_ext_l2) and computes the new subcluster bitmap from
the dirty entry, but does not join the writeback of that entry. If the
writeback fails, the revert sets the entry back to offset 0, and the
COW then installs its bitmap over it and completes. Its data went to a
cluster that nothing maps, and later I/O to those subclusters goes to
cluster 0, where the image header lives.

Delay the COW while its L2 entry or the subcluster bitmap is dirty, as
prepare_cluster_discard() does. Only a write that partly covers a
subcluster of a cluster allocated in the same worker pass waits longer.

https://virtuozzo.atlassian.net/browse/VSTOR-154310

Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den at openvz.org>
---
 drivers/md/dm-qcow2-map.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index d1ec79189f81..fedf9ac4e6e3 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -3703,6 +3703,21 @@ static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio,
 	return 1;
 }
 
+static bool delay_if_l2_dirty(struct qcow2 *qcow2, struct qcow2_map *map,
+			      struct qio **qio)
+{
+	struct md_page *md = map->l2.md;
+	u32 index_in_page = map->l2.index_in_page;
+	bool ret;
+
+	spin_lock_irq(&qcow2->md_pages_lock);
+	ret = delay_if_dirty(qcow2, md, index_in_page, qio) ||
+	      (qcow2->ext_l2 &&
+	       delay_if_dirty(qcow2, md, index_in_page + 1, qio));
+	spin_unlock_irq(&qcow2->md_pages_lock);
+	return ret;
+}
+
 static int handle_metadata(struct qcow2 *qcow2, struct qio **qio,
 			   struct qcow2_map *map)
 {
@@ -3748,7 +3763,10 @@ static int handle_metadata(struct qcow2 *qcow2, struct qio **qio,
 			 * snapshots). Here is data COW on L2_LEVEL.
 			 */
 			map->backing_file_cow = true;
-			ret = prepare_l1l2_replace(qcow2, *qio, map);
+			if (delay_if_l2_dirty(qcow2, map, qio))
+				ret = 0;
+			else
+				ret = prepare_l1l2_replace(qcow2, *qio, map);
 		} else {
 			/* Wants L1 or L2 entry allocation */
 			ret = prepare_l1l2_allocation(qcow2, *qio, map);
-- 
2.55.0



More information about the Devel mailing list