[Devel] [PATCH hci-8.2] dm-qcow2: fix stale host data showing in a zero cluster after a write

Konstantin Khorenko khorenko at virtuozzo.com
Tue Nov 24 15:08:20 MSK 2026


I won't apply this version, please send v2 with the changes below.

On Mon, 23 Nov 2026 20:13:17 +0100, Denis V. Lunev wrote:
> dm-qcow2: fix stale host data showing in a zero cluster after a write

- Cover an unallocated subcluster of an allocated cluster too, when
  there is no backing image or the write is past its end: it shows the
  same stale data.
- Share one border-mask helper with
  qio_border_is_inside_unmapped_unit() instead of a third copy of the
  border test.
- Keep the unchanged new_ext_l2 and calc_cow_mask() lines as they were.
- Commit message: drop the sentence that retells the rename, say why
  cow_in_place is needed besides only_set_ext_l2, reword "whose other
  border".

The zero unit part is right: the zero slices come from the map with no
disk read, the cluster is written in place, and on a non-extended image
the L2 entry is rewritten only after the data is on disk.

> -		    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))) {

A subcluster with neither the alloc nor the zero bit set, in an
allocated cluster, can keep old host data as well: qemu allocates a
cluster for a subcluster write, copies only that subcluster, and may
reuse a freed cluster for it. Without a backing image
maybe_mapped_in_lower_delta() is false and the zero mask is 0, so a 4K
write into such a subcluster goes to prepare_l1l2_allocation() and the
rest of the subcluster reads back the old data instead of zeroes. qemu
copies this subcluster type whole on a partial write, like a zero one.
The sliced path already zero-fills an unmapped slice when there is no
lower image, so taking an allocated cluster here as well should be
enough.

> +/* 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)

This repeats the border test of qio_border_is_inside_unmapped_unit()
(and of calc_cow_mask()): qio_subclu_indexes(), then SUBCLU_OFF() and
a bit test for each end; only the mask differs. One helper that takes
the mask would serve both, and the case above becomes a mask change.

> +		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);

The new_ext_l2 and calc_cow_mask() lines change only from qio->ext-> to
ext->; please leave them as they were, so the diff shows the logic
change only.

> 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 rename needs no sentence. What the reader needs is why cow_in_place
exists: only_set_ext_l2 cannot mark an in-place write on a non-extended
image, where the L2 entry must still be rewritten to drop the zero
flag. It is set in both modes, so "for the non-extended case" reads as
if it were set only there.

> backing file COW of a write whose other border is in an unallocated
> subcluster copies only that subcluster.

"Other" than which border? Something like "a write with one border in
an unallocated subcluster and the other in a zero subcluster" says it
directly.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team



More information about the Devel mailing list