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 9404D80799 for ; Thu, 8 Oct 2026 14:04:59 +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 698E3hk9024509; Thu, 8 Oct 2026 17:03:43 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 698E3hk9024509 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="XP9hzhfC" 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 698E3dnj024486 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 8 Oct 2026 17:03:39 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 698E3dnj024486 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-48c46582442so2787651f8f.0 for ; Thu, 08 Oct 2026 07:03:39 -0700 (PDT) X-Forwarded-Encrypted: i=2; AKwUvBzaqYwYSRgTtlTmgt/ZDfcXNrXMNDEiBbnGL66l9AGR46/wYIkV+2nsmealtcxTw9NOx1lTzg==@openvz.org X-Gm-Message-State: AFq9FYI/9L69oNwQsxjX2ldvDNa/+Z55r237D2XzQYhZKhTIXg73zfV4 7nW67PQsz67M1xvLpX1/g2riLTDBMIHVCNsq69JPFCyGUdFfLVnoeyPtv9VX3WRaO3cN4V6lbCm 7Ou8l35kk6M3AIYMd13eTY9qcIXhs8vZcHLA+rXKJbyEPpCa+RBhBiQ== X-Gm-Gg: AYBFou0LqeOlzvrTCV3sHJBv/Hetoq4I687p5FoYI3mcIfccF0UWlCEl6+v2uplGDyh yHX9x5vqnyqeJ3KEPSTP6Nc4g89I4BSDQLPacSTx4VwkgIpx7ZiSdOLZZkz7H89qq6Sw+93Vv44 D5/FUSYTZE4+pw2yEx0h55SEvAEQXmeOVcvtv3dCU6QjyGkirNTKdF6QRmfY94NL+LwTAPvMyqg MXwkGTwIqs9V5HCzsHET8n2/qCpYkqAGPuwSDiICsmkW1wf/EowkYrxrJi+sUQ8UlpwQNaaWZVF 5vn3QfNogwIV9pwL5+/0wwAwYph1vIO6Sd1urOayahSI0Ri0cNDEU0gU4NEdGa7r/os+Nn4Djq/ k+c6SMsTzG5Sdrp7IGorZVrFbrUnM X-Received: by 2002:a05:6000:2585:b0:48a:f8fb:cd49 with SMTP id ffacd0b85a97d-48c72889ca6mr10736090f8f.22.1791468219377; Thu, 08 Oct 2026 07:03:39 -0700 (PDT) X-Received: by 2002:a05:6000:2585:b0:48a:f8fb:cd49 with SMTP id ffacd0b85a97d-48c72889ca6mr10735992f8f.22.1791468218691; Thu, 08 Oct 2026 07:03:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1791468218; cv=none; d=google.com; s=arc-20260327; b=lnR8VYL75ngEuzos+eBipqqRjuPbfKGpm5t/Pya5rhi1roCHg7uucZSSWWMSSYX6JK FtLkwNMOOQPnuUcMOoy4PIfYHwEbm1R4Dvbl9cmh3b0snlmgtNmHyo+1LUAl63qCxstD FSLPp5mC3jyFk0zuedRIikgTfi7zLogW6GhhaMI1fV+Kc3T9XVGjUZXxwDY9WvmyzVvU rPbPioL8fJiWez9ahNAP00y3QcSc1oTaIjy6AoCTLWrD2HVUACefHw9jmgRX419AK4+o d+rsLvTEDedzn8Jdswjqcmk986hJEjb9wI4ZpnQ3R6InHFD3xPVAy5hEayrFHq2ge02/ ldRw== 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=V3HavcaK3xXuTk2OKmr/YSu9oxm5rAXFKY+sLhMxFEQ=; fh=jiFD8gjZllyls1UFJ+1kA4uyOnjRU7bVpV3P+S6CTV8=; b=Bb/nnMxhsvMZ0tOGg/jfis/zOckL0Sk0XwJApgfUvBqjVEQnYj/2Ijnz2DHlOe2ZBs JLYmeRljjayLrD4aYb/tf2j4RKoQFtHxSVT840nGe1VqwFlUXLDIN6eXzgFkcIcQayn7 en0xCiWr/KMBgZHDpIL7P7+ooOArmCWlpnIza3y1E2xp9BUxvsYZvLD7tZUmrjGovfkl +uKQKygEpUq2EXWoEejmk++Lc1is9ODfEpa6PC9FVIjvvITLr2JiYU6fmEDH6MCuGAsD 8X0vhWgcmjmeGUXDJnpubeZd1FH9fYBCA6OUQ7mU6MygbZ51p8np6zD2t35D3yk8Fpvc EjSw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=XP9hzhfC; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@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-48c7247b8d6si11132974f8f.220.2026.10.08.07.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Oct 2026 07:03:38 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.ivanov@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=XP9hzhfC; spf=pass (google.com: domain of alexander.ivanov@virtuozzo.com designates 130.117.225.111 as permitted sender) smtp.mailfrom=alexander.ivanov@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=V3HavcaK3xXuTk2OKmr/YSu9oxm5rAXFKY+sLhMxFEQ=; b=XP9hzhfC6pxM Af7gEK8twQE3V2SdSMs945426NaykPHyR+1gZ3DMJrTtp7gU/nfOCGdlgNNEfA9HaU1aHwxj1zJgI Try7QQFZUPGLM2cUrvBRWsUcdzOkyKP9eEyvkEzXkogM3uz9/8rKUspqzD9fBhAuAUyebBNcRbQ55 U27Rbpx1b8/Z6B2fS8wVOJH0AVcxaXXKVtI04L+m7Ekb06JKT3u6movcVKWuFckr+4BK9IUVXaHtB +sbWNuzhaMAHyNkcS4xWJiXipTyOjiKLO0OyISONxFux2Lu2Fo11WuU/9rZPVU9m7z/ow6mpUqj2b 5KfLvNk7zffkXghUG5GjKw==; Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=sky) by relay.virtuozzo.com with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1xEofO-00FAkW-1D; Thu, 08 Oct 2026 16:03:36 +0200 From: Alexander Ivanov To: den@openvz.org Date: Thu, 8 Oct 2026 16:03:33 +0200 Message-ID: <20261008140333.614909-6-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261008140333.614909-1-alexander.ivanov@virtuozzo.com> References: <20261008140333.614909-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.1 v3 5/5] dm-ploop: truncate unused tail of top delta on suspend 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 Preallocated space and clusters at the end of the top delta freed by discard are never given back, so the file size never shrinks. Now that ploop reloads metadata on resume, we can truncate the top delta on suspend: the next resumed ploop (maybe of reloaded table) picks up the new file size. Add ploop_calc_used_end(), which returns the end of the last cluster used by the delta: either BAT area, the last mapped cluster or format extension. Metadata is read from disk rather than taken from memory: the next table load checks BAT entries against the file size (-EPROTO otherwise), while in-memory BAT may differ from on-disk one after failed BAT writeback. Skip it if md pages are not clean, as on-disk BAT may miss allocated clusters then. Userspace writes format extension (CBT) behind the last data cluster of suspended device on umount, resumes it, and then removes the device, which suspends it again. So keep the area up to the end of format extension header, which is the last cluster of the extension, as userspace does. Name the corresponding header field properly instead of m_Reserved. The field was never interpreted before, so if it is not cluster-aligned or out of range, don't truncate anything: unaligned truncation would make the next resume fail with -EPROTO. Truncate the top delta to the used end in ploop_postsuspend() after pending preallocation is completed. Do it under ctl_rwsem, so that ploop messages (e.g. resize) don't change the image in parallel. Skip read-only devices and raw top delta. On error just leave the file as is. https://virtuozzo.atlassian.net/browse/VSTOR-148171 Feature: dm-ploop: ploop target driver Signed-off-by: Alexander Ivanov --- drivers/md/dm-ploop-bat.c | 74 ++++++++++++++++++++++++++++++++++++ drivers/md/dm-ploop-target.c | 56 +++++++++++++++++++++++++-- drivers/md/dm-ploop.h | 3 +- 3 files changed, 128 insertions(+), 5 deletions(-) diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c index 2e7949a1b154..5b5b5b1c4356 100644 --- a/drivers/md/dm-ploop-bat.c +++ b/drivers/md/dm-ploop-bat.c @@ -693,6 +693,80 @@ int ploop_reload_metadata(struct ploop *ploop) } ALLOW_ERROR_INJECTION(ploop_reload_metadata, ERRNO); +/* + * Returns the end of the last cluster of delta @file, which is used + * according to its header and BAT: either BAT area, the last mapped + * cluster or format extension. Clusters freed by discard may leave the + * tail of file unused. Metadata is read from disk, as the next table + * load checks BAT entries against the file size, while in-memory BAT + * may differ from it after failed BAT writeback. Also userspace may + * write format extension (e.g. CBT) behind the last data cluster of + * suspended device. This must be called on suspended device. + */ +loff_t ploop_calc_used_end(struct ploop *ploop, struct file *file) +{ + u32 i, end, nr_be, dst_clu, used_clus, *bat_entries; + struct rb_root md_root = RB_ROOT; + struct ploop_pvd_header *hdr; + struct rb_node *node; + struct md_page *md; + u64 ext_off, ext_clu; + loff_t used_end; + int ret; + + /* BAT on disk may miss allocated clusters until writeback is done */ + if (WARN_ON_ONCE(!ploop_md_pages_are_clean(ploop))) + return -EBUSY; + + ret = ploop_read_delta_metadata(ploop, file, &md_root, &nr_be); + if (ret) + return ret; + + used_clus = ploop_nr_bat_clusters(ploop, nr_be); + rb_root_for_each_md_page(&md_root, md, node) { + bat_entries = md->kmpage; + init_be_iter(nr_be, md->id, &i, &end); + for (; i <= end; i++) { + dst_clu = READ_ONCE(bat_entries[i]); + if (dst_clu != BAT_ENTRY_NONE && dst_clu >= used_clus) + used_clus = dst_clu + 1; + } + } + + /* + * Format extension (CBT) is written by userspace on umount, see + * delta_save_optional_header() in ploop library: dirty bitmap + * blocks are appended at EOF one by one, then the header cluster + * is appended behind them and m_FormatExtensionOffset points to + * it. So the header is the last cluster of the extension, and + * userspace treats L1 pointers beyond it as corruption (see + * check_ext_blocks_from_raw()). + */ + hdr = ploop_md_first_entry(&md_root)->kmpage; + ext_off = le64_to_cpu(hdr->m_FormatExtensionOffset); + if (ext_off) { + /* + * Userspace places extension at cluster boundary. Don't + * truncate anything if the offset is bogus, as we don't + * know where the extension ends then. + */ + ext_clu = SEC_TO_CLU(ploop, ext_off); + if (CLU_TO_SEC(ploop, ext_clu) != ext_off || + ext_clu >= BAT_ENTRY_NONE) { + used_end = -EPROTO; + goto out; + } + if (ext_clu >= used_clus) + used_clus = ext_clu + 1; + } + + used_end = CLU_TO_POS(ploop, used_clus); +out: + ploop_free_md_pages_tree(&md_root); + return used_end; +} +ALLOW_ERROR_INJECTION(ploop_calc_used_end, ERRNO); + void ploop_put_delta(struct ploop *ploop, struct ploop_delta *delta) { int i; diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c index 1fbcb48cff7a..e82d5b6c9095 100644 --- a/drivers/md/dm-ploop-target.c +++ b/drivers/md/dm-ploop-target.c @@ -442,8 +442,9 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop, * ploop_dtr, as ploop_dtr is called on the old ploop after the new one is * resumed, so we can't possibly modify the image from the old ploop context * at this point, as new ploop may be already doing its own IO or - * preallocation. This truncation should be done on ploop_*suspend (see - * dm-qcow2 as it already implements that in qcow2_truncate_preallocations). + * preallocation. So this truncation is done on ploop_postsuspend (see + * ploop_truncate_unused_tail), and the new ploop picks up the new size + * of the top delta on metadata reload. */ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv) { @@ -734,10 +735,51 @@ static void ploop_presuspend_undo(struct dm_target *ti) { ploop_set_wants_suspend(ti, false); } + +/* + * Truncate preallocations and the tail of the top delta, which became + * unused after discard. The next resumed ploop (maybe of reloaded + * table) re-reads the size of the top delta in ploop_preresume(). + */ +static void ploop_truncate_unused_tail(struct ploop *ploop) +{ + struct ploop_delta *top = ploop_top_delta(ploop); + unsigned long flags; + loff_t end; + int ret; + + lockdep_assert_held_write(&ploop->ctl_rwsem); + + if (ploop_is_ro(ploop) || top->is_raw) + return; + + end = ploop_calc_used_end(ploop, top->file); + if (end < 0) { + PL_ERR("Can't find used end of top delta: %lld", end); + return; + } + if (end >= top->file_size) + return; + + ret = do_ftruncate(top->file, end, 0); + if (ret) { + PL_ERR("Can't truncate unused tail of top delta: %d", ret); + return; + } + + spin_lock_irqsave(&ploop->bat_lock, flags); + top->file_size = end; + if (top->file_preallocated_area_start > end) + top->file_preallocated_area_start = end; + spin_unlock_irqrestore(&ploop->bat_lock, flags); +} + static void ploop_postsuspend(struct dm_target *ti) { struct ploop *ploop = ti->private; + unsigned int noio_flags; + down_write(&ploop->ctl_rwsem); /* * Let the next resumed ploop (maybe of reloaded table) * see final size of the top delta. @@ -746,10 +788,16 @@ static void ploop_postsuspend(struct dm_target *ti) /* * Data bios are completed before BAT writeback is finished * (see ploop_put_piwb()), so wait for it explicitly to make - * md pages clean for ploop_reload_metadata(). + * md pages clean for ploop_reload_metadata() and + * ploop_calc_used_end(). */ wait_event(ploop->inflight_md_wq, !atomic_read(&ploop->inflight_md)); - ploop_set_suspended(ti, true); + /* The device is suspended, see ploop_preresume() */ + noio_flags = memalloc_noio_save(); + ploop_truncate_unused_tail(ploop); + memalloc_noio_restore(noio_flags); + ploop->suspended = true; + up_write(&ploop->ctl_rwsem); } static int ploop_preresume(struct dm_target *ti) { diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h index 1cf0d5e0cea8..e46d6efdce59 100644 --- a/drivers/md/dm-ploop.h +++ b/drivers/md/dm-ploop.h @@ -35,7 +35,7 @@ struct ploop_pvd_header { __u32 m_DiskInUse; /* Disk in use */ __u32 m_FirstBlockOffset; /* First data block offset (in sectors) */ __u32 m_Flags; /* Misc flags */ - __u8 m_Reserved[8]; /* Reserved */ + __u64 m_FormatExtensionOffset; /* Format extension offset (in sectors) */ }; #pragma pack(pop) @@ -634,6 +634,7 @@ extern void ploop_pio_prepare_offsets(struct ploop *ploop, struct pio *pio, u32 extern int ploop_setup_metadata(struct ploop *ploop, struct page *page, bool check_in_use); extern int ploop_reload_metadata(struct ploop *ploop); +extern loff_t ploop_calc_used_end(struct ploop *ploop, struct file *file); extern int ploop_read_delta_metadata(struct ploop *ploop, struct file *file, struct rb_root *md_root, u32 *delta_nr_be); extern void ploop_index_wb_init(struct ploop_index_wb *piwb, -- 2.53.0 _______________________________________________ Devel mailing list Devel@openvz.org https://lists.openvz.org/mailman/listinfo/devel