OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write
@ 2026-11-23 19:13 Denis V. Lunev
  2026-11-23 19:21 ` no-reply
  2026-11-24 12:08 ` Konstantin Khorenko
  0 siblings, 2 replies; 3+ messages in thread
From: Denis V. Lunev @ 2026-11-23 19:13 UTC (permalink / raw)
  To: andrey.zhadchenko, alexander.ivanov, vasileios.almpanis; +Cc: devel

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

An L2 entry with the zero flag may keep its host cluster, and a zero
subcluster of an extended L2 entry keeps its part of the host cluster.
Their host blocks may hold stale data: qemu leaves it there after a
zero write without unmap. A write that covers such a unit only in part
drops the zero flag or bit but writes only its own range, so the rest
of the unit reads back the stale data instead of zeroes.
prepare_l1l2_allocation() reuses the host cluster as it is, and the
backing file COW of a write whose other border is in an unallocated
subcluster copies only that subcluster.

Send such a write through the sliced COW, as a write with a border in
an unallocated unit over a backing image already goes. The slices of a
zero unit come from the map as zeroes, with no read from disk, and are
written into the host cluster in place together with the data, so the
cluster stays where it is. map->backing_file_cow becomes sliced_cow,
and qio_ext gets cow_in_place for the non-extended case, where the L2
entry is still rewritten. The first partial write into such a unit now
writes the whole unit; writes into other units take the path they did.

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

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@openvz.org>
---
 drivers/md/dm-qcow2-map.c | 69 +++++++++++++++++++++++++++++++--------
 drivers/md/dm-qcow2.h     |  1 +
 2 files changed, 57 insertions(+), 13 deletions(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 853e7a1230ee..f603ac9158e2 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -56,7 +56,7 @@ struct qcow2_map {
 	 * read" or "sublu is not allocated" may be set).
 	 */
 	bool data_clu_alloced:1;
-	bool backing_file_cow:1;
+	bool sliced_cow:1;
 
 	u32 compressed_sectors;
 	u32 subclus_mask;
@@ -1784,6 +1784,43 @@ static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2,
 	return ret;
 }
 
+/* Zero subclusters with a border of @qio inside, which keep host blocks */
+static u32 qio_border_zero_subclus_mask(struct qcow2 *qcow2, struct qio *qio,
+					struct qcow2_map *map)
+{
+	u64 start_off = bytes_off_in_cluster(qcow2, qio);
+	u64 end_off = start_off + qio->bi_iter.bi_size;
+	u32 zeroes = map->ext_l2 >> 32, mask = 0;
+	u8 start_bit, end_bit;
+
+	if (!map->data_clu_alloced || map->clu_is_cow)
+		return 0;
+
+	start_bit = qio_subclu_indexes(qcow2, qio, &end_bit);
+	if (SUBCLU_OFF(qcow2, start_off) && (zeroes & (1 << start_bit)))
+		mask |= 1 << start_bit;
+	if (SUBCLU_OFF(qcow2, end_off) && (zeroes & (1 << end_bit)))
+		mask |= 1 << end_bit;
+	return mask;
+}
+
+/*
+ * A zero cluster or subcluster may keep host blocks with stale data.
+ * A write that does not cover it entirely must zero the rest.
+ */
+static bool qio_border_is_inside_zero_unit(struct qcow2 *qcow2,
+					   struct qio *qio,
+					   struct qcow2_map *map)
+{
+	if (qio->bi_iter.bi_size == qcow2->clu_size)
+		return false;
+
+	if (!qcow2->ext_l2)
+		return map->prealloced;
+
+	return qio_border_zero_subclus_mask(qcow2, qio, map) != 0;
+}
+
 static bool qio_is_fully_alloced(struct qcow2 *qcow2, struct qio *qio,
 				 struct qcow2_map *map)
 {
@@ -3559,19 +3596,23 @@ static void submit_read_sliced_clu(struct qcow2_map *map, struct qio *qio,
 static void submit_read_sliced_cow_clu(struct qcow2_map *map, struct qio *qio)
 {
 	struct qcow2 *qcow2 = map->qcow2;
+	struct qio_ext *ext = qio->ext;
 	u64 mask = 0;
 
 	WARN_ON_ONCE(!(map->level & L2_LEVEL));
 
+	if (map->data_clu_alloced && !map->clu_is_cow) {
+		ext->cow_in_place = true;
+		ext->allocated_clu_pos = map->data_clu_pos;
+	}
+
 	if (qcow2->ext_l2) {
 		mask = map->ext_l2 & ~((u64)map->subclus_mask << 32);
-		qio->ext->new_ext_l2 = mask | map->subclus_mask;
-		if (map->data_clu_alloced && !map->clu_is_cow) {
-			qio->ext->only_set_ext_l2 = true;
-			qio->ext->allocated_clu_pos = map->data_clu_pos;
-		}
-		qio->ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
-					     true, map->clu_is_cow, false);
+		ext->new_ext_l2 = mask | map->subclus_mask;
+		ext->only_set_ext_l2 = ext->cow_in_place;
+		ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio,
+					      true, map->clu_is_cow, false);
+		ext->cow_mask |= qio_border_zero_subclus_mask(qcow2, qio, map);
 	}
 
 	submit_read_sliced_clu(map, qio, sliced_cow_read_complete);
@@ -3785,14 +3826,16 @@ static int handle_metadata(struct qcow2 *qcow2, struct qio **qio,
 			/* COW to compressed or shared with snapshot cluster */
 			ret = prepare_l1l2_replace(qcow2, *qio, map);
 		} else if ((map->level & L2_LEVEL) &&
-		    qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
-		    maybe_mapped_in_lower_delta(qcow2, *qio)) {
+			   ((qio_border_is_inside_unmapped_unit(qcow2, *qio, map) &&
+			     maybe_mapped_in_lower_delta(qcow2, *qio)) ||
+			    qio_border_is_inside_zero_unit(qcow2, *qio, map))) {
 			/*
 			 * Backing file is about data COW, and it is
 			 * never about metadata COW (unlike internal
 			 * snapshots). Here is data COW on L2_LEVEL.
+			 * A zero unit COWs zeroes over its host blocks.
 			 */
-			map->backing_file_cow = true;
+			map->sliced_cow = true;
 			if (delay_if_l2_dirty(qcow2, map, qio))
 				ret = 0;
 			else
@@ -3937,7 +3980,7 @@ static void process_one_qio(struct qcow2 *qcow2, struct qio *qio)
 	} else { /* write */
 		if (unlikely(map.clu_is_cow && !(map.level & L2_LEVEL)))
 			submit_read_whole_cow_clu(&map, qio);
-		else if (unlikely(map.clu_is_cow || map.backing_file_cow))
+		else if (unlikely(map.clu_is_cow || map.sliced_cow))
 			submit_read_sliced_cow_clu(&map, qio);
 		else
 			perform_rw_mapped(&map, qio);
@@ -4375,7 +4418,7 @@ static void process_cow_data_write(struct qcow2 *qcow2, struct list_head *cow_li
 			break;
 		ext = qio->ext;
 
-		if (ext->only_set_ext_l2) {
+		if (ext->cow_in_place) {
 			WARN_ON_ONCE(ext->lx_level != L2_LEVEL);
 			pos = ext->allocated_clu_pos;
 			goto submit;
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 4acddbe7c95f..c2460c2bea06 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -325,6 +325,7 @@ struct qio_ext {
 	u64 new_ext_l2;
 	u32 cow_mask;
 	bool only_set_ext_l2:1;
+	bool cow_in_place:1;
 	bool set_all_zeroes:1;
 	bool wait_writers:1;
 
-- 
2.55.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 12:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 19:13 [Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write Denis V. Lunev
2026-11-23 19:21 ` no-reply
2026-11-24 12:08 ` Konstantin Khorenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox