From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail.openvz.org (unknown [69.168.225.77]) by lore.virtuozzo.com (Postfix) with ESMTPS id 5CE6780024 for ; Thu, 27 Aug 2026 16:10:40 +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 67RG9RtK001633; Thu, 27 Aug 2026 19:09:28 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 67RG9RtK001633 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="Xdd2QlSI" Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 67RG9Bo2001568 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 27 Aug 2026 19:09:11 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 67RG9Bo2001568 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-482f2b8a116so581368f8f.1 for ; Thu, 27 Aug 2026 09:09:11 -0700 (PDT) X-Gm-Message-State: AFuF++kxzUhvy88v/s3DnwXvDZDVXZA0Epc+5OmDiLLv8gZ30SRB7vNG 6cqvjZTYHCR32JGk/Y1rFb2Bb1JdfWkHqa5onmBVarv6n5H1BHDjC8IkuJLKbLIiJUtcRDE6VxJ FckmHnA3tQxgYTh5zCg1jCkJ4h7mOF/k1HATc/FOb05OFEvUKB2emRQ== X-Gm-Gg: AR+sD11EY7SMbNeOR0G0dV63yBa2+zC9KYBBB3bWA7f+fGi5i7EaiVdSr/pWSGjmIGJ b+GpnG+sZnkOM5CQUuNdXZtiKMJKZStUdgisTfuwZdyIMEqdWl2i82wC50QJBWynvMWVN2h+jNB OHpQB9MPmSAXuBoU3ZCl3c5HjOsaUofdCIZsXZfC9/f60MdKKwwNF5fU09ct+6KLrr7S0vliRqh czvvTwV5GHlsq5W3tljteAz77Y11vAtnb1z/qUgIsm9C7KoWOyfZy4Jx1hdmi7CzL9fyfmKVyBo sDCZwUqbehOjyalf8+bAUpDkIw1NmXAieKuHbDjlLWksMi83Cyfoi/OpHMYqhhaKFrgOpxsoxc/ m5d2DLhMfTf2yxrkvIefT1Jp/CnTl X-Received: by 2002:a05:6000:4549:b0:482:f756:46e with SMTP id ffacd0b85a97d-482f7560628mr339451f8f.15.1787846951290; Thu, 27 Aug 2026 09:09:11 -0700 (PDT) X-Received: by 2002:a05:6000:4549:b0:482:f756:46e with SMTP id ffacd0b85a97d-482f7560628mr339355f8f.15.1787846950764; Thu, 27 Aug 2026 09:09:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1787846950; cv=none; d=google.com; s=arc-20260327; b=Z40eM1e7umK15i6iYibl4LGQHvXzwoOMBc0Ly3fNw/5ippCjGRUXeryWfXfuuyNBW2 HfpQBzOxReNwegknLxNRLZ8l0w7S/hWEPGJqqTajpNRFN3QvCpo3dzdBGHULzG28NghF 7Tl7sWZl5sapHxvy6rmR5QtfYOayze8IBSMmYLs79eqx5LF50sKAUF7iANRSrQlBIbmA YPlCyuHVrPcfrJ5or7u1QSxQf8+ouZjXuNgHlNDPHcKbChWDPsrwZnSBI/FOcuV4svyC i4drJf2nwHSYHmoM63l5yz/k+vVIwLfNvaFD4OXlflCYkDjMMj6L4+ROLPZ2rweghhyL 09ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=NgyT3auOjX7Fcg5FU62BkV98DZ+bmbQ8nEOgOgKlHGM=; fh=0/brz1B8ohImS2ERrXZY0dPGpSEyX2H6iLC7bneXJkE=; b=VhD24ly2KGQcTmqajPrX3jX8JfBuoBc/AjaZ4O9LVI7L0+hd8jTdG1bsVgCYyVdzsd mH21I5ScrdlLa+CJHwOX/clDQiV2TnhVTC2irvnY6u/8jGTqYgBRSQUkknK1ix4lWom9 xDtDUx0VahRuABoNzDmsgmk6iOeOg3eqI/RK5kNfadxi4W1KUAA2om+xGT6M2aWYs9Rw mJHuRWFIVmXlJkjvOC5bzObEZwnqgEKP7DXtzCGFjUKbMpNcpdgYfNG97TysiTvKhtrl 1wpEczo1Q1tbNdehUaBX3YQR6AJlndMasOtLf+nnP0KEo1cFyaESmPclrAjiPbdauduX +ssg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=Xdd2QlSI; spf=pass (google.com: domain of andrey.zhadchenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=andrey.zhadchenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: from relay.virtuozzo.com (relay.virtuozzo.com. [130.117.225.111]) by mx.google.com with ESMTPS id ffacd0b85a97d-482e2790d3dsi8159468f8f.106.2026.08.27.09.09.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2026 09:09:10 -0700 (PDT) Received-SPF: pass (google.com: domain of andrey.zhadchenko@virtuozzo.com designates 130.117.225.111 as permitted sender) client-ip=130.117.225.111; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=Xdd2QlSI; spf=pass (google.com: domain of andrey.zhadchenko@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=andrey.zhadchenko@virtuozzo.com; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-ID:Date:Subject:From: Content-Type; bh=NgyT3auOjX7Fcg5FU62BkV98DZ+bmbQ8nEOgOgKlHGM=; b=Xdd2QlSI2+4j ZC+63cxMyLTxKvBsBKfTleZ0WhPwZz51ZmIyhif7sUwMIf1XrE0klI54pWlAMIXM/Z+6O9wOk5cq8 pugkZ40buaG0xBpn0PcMuZJ1oDZv+fJ25bxIS7dehNZlIE2e/Ix+DrJaB0nsux+mr9wp5goZV1clW bpSTEUIg3hGWeLmJlO+jNQLC8Q9UtSP+a/Rpi3NGdLpsvg3J33gtychjceF+zGRljreS5p05QzYSk jwkhcl0ag5syNQy5gw7M1iylzJKg2aYIEww0Tuvvwlp/9NAxHGAqt+yHb7g2azGtAr9OpNzr9AuLA EZVpF2QEmBb8b+Xf3+tVAw==; Received: from [130.117.225.5] (helo=vz9-demens-1.aci.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1wzccs-00FHQJ-0m; Thu, 27 Aug 2026 18:09:08 +0200 From: Andrey Zhadchenko To: ptikhomirov@virtuozzo.com, vasileios.almpanis@virtuozzo.com Date: Thu, 27 Aug 2026 19:06:12 +0300 Message-ID: <20260827160619.303398-5-andrey.zhadchenko@virtuozzo.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20260827160619.303398-1-andrey.zhadchenko@virtuozzo.com> References: <20260827160619.303398-1-andrey.zhadchenko@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH VZ10 v4 04/11] drivers/md/dm-qcow2: update metadata on whole cluster discard 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 Discard of a mapped cluster used to only punch a hole in the image file: the L2 entry and refcounts were left untouched, so the cluster remained allocated in qcow2 metadata forever. Handle discards covering a whole cluster via the L1/L2 entry replace machinery: prepare_cluster_discard() locks the L2 entry, then issue_discard() punches the data cluster out of the image file and queues the qio to write the zeroed L2 entry (and extended L2 bitmap) in process_indexes_write(). Refcounts of the discarded cluster are decremented in process_indexes_end() after the L2 writeback, like COW does with its source clusters. Clusters shared with internal snapshots or holding compressed data are not punched: only their usage count is decremented. Also teach revert_l_entries_update() that a changed index may contain a discard-cleared entry, which has no allocation to revert. Partial cluster discards keep the previous behavior: punch a hole without touching metadata. Feature: dm-qcow2: block device over QCOW2 files driver https://virtuozzo.atlassian.net/browse/VSTOR-139406 Signed-off-by: Andrey Zhadchenko --- drivers/md/dm-qcow2-map.c | 110 +++++++++++++++++++++++++++++++++++--- drivers/md/dm-qcow2.h | 2 +- 2 files changed, 104 insertions(+), 8 deletions(-) diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c index 83b540ccefee6..a1bd36a112170 100644 --- a/drivers/md/dm-qcow2-map.c +++ b/drivers/md/dm-qcow2-map.c @@ -101,6 +101,12 @@ static loff_t bio_sector_to_file_pos(struct qcow2 *qcow2, struct qio *qio, return map->data_clu_pos + bytes_off_in_cluster(qcow2, qio); } +static bool qio_covers_full_clu(struct qcow2 *qcow2, struct qio *qio) +{ + return bytes_off_in_cluster(qcow2, qio) == 0 && + qio->bi_iter.bi_size == qcow2->clu_size; +} + static loff_t compressed_clu_end_pos(loff_t start, sector_t compressed_sectors) { if (start % SECTOR_SIZE == 0) @@ -1324,6 +1330,8 @@ static void revert_l_entries_update(struct qcow2 *qcow2, struct wb_desc *wbd) set_u64_to_be_page(wbd->md->page, i, 0); if (skip_odd && (i & 1)) continue; /* pos contains ext_l2 part of L2 entry */ + if (!pos) + continue; /* no cluster was allocated */ spin_unlock(&qcow2->md_pages_lock); pos &= ~LX_REFCOUNT_EXACTLY_ONE; @@ -2015,7 +2023,12 @@ static int parse_metadata(struct qcow2 *qcow2, struct qio **qio, return ret; map->data_clu_pos = pos; - if (!write || !map->clu_is_cow) + if (!write) + return 0; + + /* discards also need to update r1r2 */ + if (!map->clu_is_cow && + !(op_is_discard((*qio)->bi_op) && qio_covers_full_clu(qcow2, *qio))) return 0; /* Now refcounters table/block */ @@ -3317,12 +3330,86 @@ static void issue_discard(struct qcow2_map *map, struct qio *qio) int ret; WARN_ON_ONCE(!(map->level & L2_LEVEL)); - pos = bio_sector_to_file_pos(qcow2, qio, map); - ret = qcow2_punch_hole(qcow2->file, pos, qio->bi_iter.bi_size); - if (ret) - qio->bi_status = errno_to_blk_status(ret); - qio_endio(qio); + if (!map->clu_is_cow) { + pos = bio_sector_to_file_pos(qcow2, qio, map); + ret = qcow2_punch_hole(qcow2->file, pos, qio->bi_iter.bi_size); + + if (ret) { + qio->bi_status = errno_to_blk_status(ret); + qio_endio(qio); + return; + } + } + + /* Clear metadata if needed */ + if (qio->flags & QIO_IS_DISCARD_FL) { + qio->queue_list_id = QLIST_INDEXES_WRITE; + qcow2_dispatch_qios(qcow2, qio, NULL); + } else { + qio_endio(qio); + } +} + +static bool qio_discard_updates_metadata(struct qcow2 *qcow2, struct qio *qio, + struct qcow2_map *map) +{ + if (!(map->level & L2_LEVEL) || !map->data_clu_alloced) + return false; + if (qio_covers_full_clu(qcow2, qio)) + return true; + return false; +} + +/* + * Discard changes metadata: prepare L2 entry update. It gets locked + * here, new value is written in process_indexes_write(), and + * refcounts are handled after the L2 writeback in process_indexes_end(). + * + * Discard covering the whole cluster replaces the entry and unuses + * the discarded (or COW source) cluster. + */ +static int prepare_cluster_discard(struct qcow2 *qcow2, struct qio **qio, + struct qcow2_map *map) +{ + u32 index_in_page = map->l2.index_in_page; + struct md_page *md = map->l2.md; + loff_t unuse_pos, unuse_end; + struct qio_ext *ext; + int ret; + + WARN_ON_ONCE(!(map->level & L2_LEVEL) || !map->data_clu_alloced); + + spin_lock_irq(&qcow2->md_pages_lock); + if (delay_if_dirty(qcow2, md, index_in_page, qio) || + __delay_if_writeback(qcow2, md, index_in_page, qio, true) || + (qcow2->ext_l2 && + delay_if_dirty(qcow2, md, index_in_page + 1, qio))) { + spin_unlock_irq(&qcow2->md_pages_lock); + return 0; + } + spin_unlock_irq(&qcow2->md_pages_lock); + + if (map->clu_is_cow) { + /* Cluster is shared or compressed. Decrement refcount. */ + unuse_pos = map->cow_clu_pos; + unuse_end = map->cow_clu_end; + } else { + /* Nobody else refers the cluster: unuse it after discard */ + unuse_pos = map->data_clu_pos; + unuse_end = map->data_clu_pos + qcow2->clu_size; + } + + ret = prepare_l_entry_replace(qcow2, map, *qio, md, index_in_page, + unuse_pos, unuse_end, L2_LEVEL); + if (ret <= 0) + return ret; + + ext = (*qio)->ext; + ext->lx_md = md; + + (*qio)->flags |= QIO_IS_DISCARD_FL; + return 1; } static int handle_metadata(struct qcow2 *qcow2, struct qio **qio, @@ -3344,6 +3431,9 @@ static int handle_metadata(struct qcow2 *qcow2, struct qio **qio, /* Nothing to COW or L1 is mapped exactly once */ qio_endio(*qio); ret = 0; + } else if (unlikely(op_is_discard((*qio)->bi_op)) && + qio_discard_updates_metadata(qcow2, *qio, map)) { + ret = prepare_cluster_discard(qcow2, qio, map); } else if (write && (!qio_is_fully_alloced(qcow2, *qio, map) || map->clu_is_cow)) { if (map->clu_is_cow) { @@ -3365,7 +3455,7 @@ static int handle_metadata(struct qcow2 *qcow2, struct qio **qio, qio_endio(*qio); ret = 0; } - /* Otherwise issue_discard(). TODO: update L2 */ + /* Otherwise issue_discard(). */ } else { /* Wants L1 or L2 entry allocation */ ret = prepare_l1l2_allocation(qcow2, *qio, map); @@ -3479,6 +3569,12 @@ static void process_one_qio(struct qcow2 *qcow2, struct qio *qio) write = op_is_write(qio->bi_op); + /* Discard with prepared metadata update, see prepare_cluster_discard() */ + if (unlikely(qio->flags & QIO_IS_DISCARD_FL)) { + issue_discard(&map, qio); + return; + } + if (unlikely(map.compressed)) { /* Compressed qio never uses sub-clus */ submit_read_compressed(&map, qio, write); diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h index 230e7a4a34e76..8a24e04130e4d 100644 --- a/drivers/md/dm-qcow2.h +++ b/drivers/md/dm-qcow2.h @@ -340,7 +340,7 @@ struct qio { blk_status_t bi_status; #define QIO_FREE_ON_ENDIO_FL (1 << 0) /* Free this qio memory from qio_endio() */ #define QIO_IS_MERGE_FL (1 << 3) /* This is service merge qio */ -#define QIO_IS_DISCARD_FL (1 << 4) /* This zeroes index on backward merge */ +#define QIO_IS_DISCARD_FL (1 << 4) /* This zeroes index (discard or backward merge) */ #define QIO_IS_L1COW_FL (1 << 5) /* This qio only wants COW at L1 */ #define QIO_SPLIT_INHERITED_FLAGS (QIO_IS_DISCARD_FL) u8 flags; -- 2.43.5 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel