From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (mail.openvz.org [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 203D18002A for ; Tue, 24 Nov 2026 12:47:38 +0000 (UTC) Received: from mail.openvz.org (localhost [127.0.0.1]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AOCkIQ3023610; Tue, 24 Nov 2026 15:46:19 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AOCkIQ3023610 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=default; t=1795524381; bh=JsRpGwyIPTibkXcIyv/G6JJ55NpaOncsv/JtYs2mzWo=; h=From:To:Date:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe; b=Neibhj8Dz276DyRS905q8UOQ6zHbGlczZkDgJZkhI8LItEROFuAth46xCgpJIeRR4 9hrF4zBcRimpmhEbU6oYmGDSCd9oF/rII5XBh6ILq5bsgaPBpTjJc2oAvr2/u0T1vW CNC0OsQeYC7k2Jp3/2uNfvIlGU60tnhRLS6r+HnZjOizxJA+fgSGnE1WD/wX8kAt4t R1iP16ORf8Za9mI2QXmCl3Y7A1APAlp/WyKeMn+/ZuKnn3ynvH8bfI3tKgry9u2da+ L7o6+1v0uPemcVIBrYvj8Lzcj11+2g8Qddzy7PYbrk2FQIjcSL4U8EA/NP/6TdMSPl koEugJ3aPoC1A== Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AOCkH5n023603 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Tue, 24 Nov 2026 15:46:17 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AOCkH5n023603 Authentication-Results: mail.openvz.org; dkim=pass (2048-bit key) header.d=openvz.org header.i=@openvz.org header.b="XwSQBGmq" Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-48e0fc747a7so3049138f8f.1 for ; Tue, 24 Nov 2026 04:46:17 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErGG2N7sFYWPiJ8fzX6KNI0eLH9Soi6YIZ9yAb5Te6mvjj/d9o/l8NzUJpIfBcFkuJyACiS0jg==@openvz.org X-Gm-Message-State: APuU4Z8trYDRazK5a9iJsQLDUgFaQBK9E9MP3d76OX5sxsrO+0pW3qPb Ur8U5awQSuQFr/jM1za0hy8mwYp+aqKY+8sRh4AyYyy+5LYvcEbNDjU4sXuf1EH71UXi3v6IiBs rkKDGLceFrmgYYlhQnBVcNx7ACjRQPq0YdEiuRgksvy8VutuoDmNxog== X-Gm-Gg: AZFdTZfU8z+TJNtjEgmsoAQ9u3sc1EB+KbXI0K7Wse35wAEoALKvCssNCvacv2sDQRs bmNOjz8YaG3PKfJkIk2WME54OnUbAFpCy3lGcusyBuTDpHlgEmE4B/6OgC4QUbhohpKKQp+M9pB Wnzcnx96cl+4QVTLKUOGm3zF/vpkn0j1IyslVpWNpTDHJpKB3vHKQezEf+in/Q/szcQ2m773BOU mAMxM1jjh+SbwH0fZNmog+STKJZr/XDA3XvTK8gcDmRVEsXhcbzP7X5OEoOyPBnorWBkF88AEOw 4zxpHdepvw== X-Received: by 2002:a05:6000:454f:b0:48e:6419:ddf2 with SMTP id ffacd0b85a97d-4abb2168ec7mr18613066f8f.17.1795524377051; Tue, 24 Nov 2026 04:46:17 -0800 (PST) X-Received: by 2002:a05:6000:454f:b0:48e:6419:ddf2 with SMTP id ffacd0b85a97d-4abb2168ec7mr18613034f8f.17.1795524376638; Tue, 24 Nov 2026 04:46:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795524376; cv=none; d=google.com; s=arc-20260327; b=XPVjwBXtxKzzyX+3bOLX1dl7857wbPSLFJJDSNKzq2ejlycuPJwtzqYaALvrxp8VkQ BMNS5dMZkxhm7Dpq/UEEMq8VdXjNQX5qxZoMHj2hzMPTG1Kje9d++2yr5MN/enKbW9/O jkMoXV2Td3u/OHUoOBmyaFple7lSwltKAk5CY0usDu4Q5We4fclKCPNRf1fCyhVLGhty fLEyQ51OsWi8SRfszwa1Z7aPyY0KuDyula6dQlehZ+ykfxTUMs6iezNKe+NwFeNyz+dz XaMD/Nug/+VT6SRRXlg5nWAIXrV4LlG64HuKyY5ALT186vbrmeTQMoZpsvaJ3GJNNl7u FnVA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=+N6+R+x27TpPDPRHdp3k1A/UCpO8xr04u6xTplAGWMI=; fh=POkcDn3E2mjb2PA85kOK4tMvCJrlBlA7QlqJQFRsTyM=; b=SmpRQ3mWRZ7njMNY11/7yYkeLVAskFXFt/ZMi9XjqdEwhGL+NuHMCc2kj03deuEnRA ennNlXeyM8l0aQ7rmsp4GYmxK7LMOdCATKkg6ql9lQLkqgtHR2jgmflS+PyzjadslCwz O/ddmunXlNUY0oyE7qKw8BM1/RxdIBxuy1vtZJLIO8r4Tor4FBcbLfKejG0LjVBRDtVz 5GqYX+fYe+wfyh5fTcYiOZ7bYicpufbDBsH59Jo1vujo6SCxC3qWNl+Tv4HhMG9UOU8n gKrVL+4IShgo8w8zjCkkgQCaUBoTgmWeJs6FKpeb5TOiNMKyjG99Z1ashDFfJ4x5vOiC bCGg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=XwSQBGmq; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:8dcd:31dc:bf64:9564 as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id ffacd0b85a97d-4ab6ae2c7b5sor2643415f8f.10.2026.11.24.04.46.16 for (Google Transport Security); Tue, 24 Nov 2026 04:46:16 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:8dcd:31dc:bf64:9564 as permitted sender) client-ip=2a06:5b06:b600:300:8dcd:31dc:bf64:9564; Authentication-Results: mx.google.com; dkim=pass header.i=@openvz.org header.s=google header.b=XwSQBGmq; spf=softfail (google.com: domain of transitioning den@openvz.org does not designate 2a06:5b06:b600:300:8dcd:31dc:bf64:9564 as permitted sender) smtp.mailfrom=den@openvz.org; dara=neutral header.i=@openvz.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz.org; s=google; t=1795524376; x=1796129176; darn=openvz.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=+N6+R+x27TpPDPRHdp3k1A/UCpO8xr04u6xTplAGWMI=; b=XwSQBGmqogYq180qJw5robkjzitlOrU7GQfStbywrR9D5940nLf5x4V/f5IYi1n8Up R0bW+5nJxDDY2EvgP3apCvSwAGbQPuzMqSmeZbH5ol9FsFSRos6ZWzMIatnxhX3TaqVd 5bx/4fNiZMad1MfYDk1VaA2gp+4RhbmdvQ+6zCO/crUsGlg9pEzf1HL9w7IvfQXZ504Z spBaT/fdwXodRLUNOa2o1ZPrdhYPQNX6RQ7uKsHt19i+0xKCSYcjDvHMoP14vHk0O9OT WsdAVx+l5hK+PQAYh2x9z0WxQYRnX4Cxp3G7X/BLjjXEXMU7o372AthVY8CGdzz9x4X7 Umjw== X-Forwarded-Encrypted: i=1; AL+hErFOdm7B9KxQNvEWgNdfnvTMOvj54sjz3DfdSnHLGevDVGohPX64z4qcLBB5ibx/uOnXmqMcRw==@openvz.org X-Received: by 2002:a05:600c:3b94:b0:4bb:6f41:39be with SMTP id 5b1f17b1804b1-4bb8880ca3cmr372638355e9.15.1795524375999; Tue, 24 Nov 2026 04:46:15 -0800 (PST) Received: from artemis.sw.ru ([2a06:5b06:b600:300:8dcd:31dc:bf64:9564]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853a9846sm511369095e9.11.2026.11.24.04.46.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Nov 2026 04:46:15 -0800 (PST) From: "Denis V. Lunev" To: andrey.zhadchenko@virtuozzo.com, alexander.ivanov@virtuozzo.com, vasileios.almpanis@virtuozzo.com Date: Tue, 24 Nov 2026 13:46:13 +0100 Message-ID: <20261124124613.455047-1-den@openvz.org> X-Mailer: git-send-email 2.55.0 MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v2] dm-qcow2: fix stale host data showing in a zero cluster after a write X-BeenThere: devel@openvz.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: OpenVZ development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: devel-bounces@openvz.org Errors-To: devel-bounces@openvz.org From: Denis V. Lunev 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. An unallocated subcluster of an allocated cluster may hold stale data as well, since qemu allocates a cluster for a write into one subcluster and may reuse a freed one for it. A write that covers such a unit only in part drops the zero flag or bit, or sets the alloc 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 with one border in an unallocated subcluster and the other in a zero subcluster copies only the unallocated one. Send such a write through the sliced COW, as a write with a border in an unallocated unit over a backing image already goes; an unallocated subcluster is taken only when the write starts outside the backing data. The slices 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. only_set_ext_l2 cannot mark such an in-place write on an image without extended L2 entries, where the L2 entry must still be rewritten to drop the zero flag, so qio_ext gets cow_in_place, set in both modes. As in qemu, the first partial write into such a unit now writes the whole unit. On an image with extended L2 entries and no backing data this includes the first partial write into each subcluster of a cluster dm-qcow2 allocated and zeroed itself, which the driver cannot tell from one allocated by qemu. 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 --- v2: also cover an unallocated subcluster of an allocated cluster without backing data, share the border mask helper with qio_border_is_inside_unmapped_unit(), leave the unchanged lines alone and reword the message, per Konstantin's review. v1: http://lore.virtuozzo.com/kernel/20261123191317.199611-1-den@openvz.org/ drivers/md/dm-qcow2-map.c | 87 ++++++++++++++++++++++++++++++--------- drivers/md/dm-qcow2.h | 1 + 2 files changed, 69 insertions(+), 19 deletions(-) diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c index 853e7a1230ee..2ab14fd939c8 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; @@ -1756,14 +1756,28 @@ static u32 qio_all_zeroes_size(struct qcow2 *qcow2, struct qio *qio, return qio_subclus_covered_start_size(qcow2, qio, map->ext_l2 >> 32); } +/* Subclusters of @mask that a border of @qio falls inside */ +static u32 qio_border_subclus_mask(struct qcow2 *qcow2, struct qio *qio, + u32 mask) +{ + u64 start_off = bytes_off_in_cluster(qcow2, qio); + u64 end_off = start_off + qio->bi_iter.bi_size; + u8 start_bit, end_bit; + u32 ret = 0; + + start_bit = qio_subclu_indexes(qcow2, qio, &end_bit); + if (SUBCLU_OFF(qcow2, start_off) && (mask & (1 << start_bit))) + ret |= 1 << start_bit; + if (SUBCLU_OFF(qcow2, end_off) && (mask & (1 << end_bit))) + ret |= 1 << end_bit; + return ret; +} + static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2, struct qio *qio, struct qcow2_map *map) { - u64 start_off, end_off; - u8 start_bit, end_bit; u32 mapped_mask; - bool ret; if (WARN_ON_ONCE(!(map->level & L2_LEVEL))) return false; @@ -1774,14 +1788,44 @@ static bool qio_border_is_inside_unmapped_unit(struct qcow2 *qcow2, if (!qcow2->ext_l2) return !map->data_clu_alloced && !map->all_zeroes; - start_bit = qio_subclu_indexes(qcow2, qio, &end_bit); - start_off = bytes_off_in_cluster(qcow2, qio); - end_off = start_off + qio->bi_iter.bi_size; mapped_mask = (u32)map->ext_l2 | (map->ext_l2 >> 32); + return qio_border_subclus_mask(qcow2, qio, ~mapped_mask) != 0; +} - ret = SUBCLU_OFF(qcow2, start_off) != 0 && ((1 << start_bit) & ~mapped_mask); - ret |= SUBCLU_OFF(qcow2, end_off) != 0 && ((1 << end_bit) & ~mapped_mask); - return ret; +/* + * Subclusters of an allocated cluster with a border of @qio inside, which + * read as zeroes over host blocks: zero ones, and unallocated ones when + * @qio starts outside the backing data. + */ +static u32 qio_border_stale_subclus_mask(struct qcow2 *qcow2, struct qio *qio, + struct qcow2_map *map) +{ + u32 zeroes = map->ext_l2 >> 32; + u32 mask = zeroes; + + if (!map->data_clu_alloced || map->clu_is_cow) + return 0; + if (!maybe_mapped_in_lower_delta(qcow2, qio)) + mask |= ~((u32)map->ext_l2 | zeroes); + return qio_border_subclus_mask(qcow2, qio, mask); +} + +/* + * A zero cluster, or a zero or unallocated subcluster of an allocated + * cluster, 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_stale_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_stale_subclus_mask(qcow2, qio, map) != 0; } static bool qio_is_fully_alloced(struct qcow2 *qcow2, struct qio *qio, @@ -3563,15 +3607,18 @@ static void submit_read_sliced_cow_clu(struct qcow2_map *map, struct qio *qio) WARN_ON_ONCE(!(map->level & L2_LEVEL)); + if (map->data_clu_alloced && !map->clu_is_cow) { + qio->ext->cow_in_place = true; + qio->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->only_set_ext_l2 = qio->ext->cow_in_place; qio->ext->cow_mask = calc_cow_mask(qcow2, map->ext_l2, qio, true, map->clu_is_cow, false); + qio->ext->cow_mask |= qio_border_stale_subclus_mask(qcow2, qio, map); } submit_read_sliced_clu(map, qio, sliced_cow_read_complete); @@ -3785,14 +3832,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_stale_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 stale 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 +3986,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 +4424,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