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 BC0CB8002A for ; Thu, 19 Nov 2026 11:36:52 +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 6AJBZeXw019212; Thu, 19 Nov 2026 14:35:41 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AJBZeXw019212 Authentication-Results: mail.openvz.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=virtuozzo.com header.i=@virtuozzo.com header.b="WG2L8Qha" Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by mail.openvz.org (8.14.4/8.14.4) with ESMTP id 6AJBYpoj019027 (version=TLSv1/SSLv3 cipher=AES128-GCM-SHA256 bits=128 verify=FAIL) for ; Thu, 19 Nov 2026 14:34:52 +0300 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.openvz.org 6AJBYpoj019027 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4a35e1d2dddso16898885e9.0 for ; Thu, 19 Nov 2026 03:34:52 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHdqchHIQGpP1iOio1iezDW0cQZlNe//Ziq9wfxC7j/TQgl0hmo5oe/uOD50hFGoGmVYxJb9Q==@openvz.org X-Gm-Message-State: APuU4Z9zHPlJBpiaXaTYKrFIm7fg5SX0cn3kMGNVQdrbqeB1SG6cUFR5 iTNA5AhAH/FizHWANH+dgb2MfTKHZfknJAdnSF3d0QDNHHPOJ/Vi/vLkROoh0bnWty6SBhvEFGc SfiojToggdS7ccJwr8XDYnX40k+XNONkrRg9agwvAkLeOoT7lbECaHg== X-Gm-Gg: AYBFou2gdr2+yii2gGrGAGRRlhWQ0ZCuAejgF9hRrwfp64AA76vltrfwbwnUPlEcukz RE0nDz8hk/KhmKq0/pry5KlHMl0oNm1cjHK+SQABp0NnRAe8zh7Lz6AAlTpflZPQ1zKAGFh9t5x amjyg+0sQjpG4IUB/BWZBvsDD34SzKFIvzerD6WaNuhvdP+lW9ZWVxvnyB/XZn7skz2k/GseW6V t7TTjPqSbgutZpuRy5/x5ETeowid5n7i49DyxZcQnaFMO8ExlpHF2oxvpfoxERY5O8KhiU+P8Bk BGQ2Cg2zUZGNosQUoe4c1Ntf1ATLQwucUGUFWYOJeCePCcknepFgqVZaM9xTOlZ9jwdUDMBU0iP R/QX4e/waNE7ownQVaAa7B3p0STg= X-Received: by 2002:a05:600c:a54:b0:4a3:5d64:df91 with SMTP id 5b1f17b1804b1-4bb7da3e3e0mr110102495e9.18.1795088091621; Thu, 19 Nov 2026 03:34:51 -0800 (PST) X-Received: by 2002:a05:600c:a54:b0:4a3:5d64:df91 with SMTP id 5b1f17b1804b1-4bb7da3e3e0mr110102015e9.18.1795088091080; Thu, 19 Nov 2026 03:34:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1795088091; cv=none; d=google.com; s=arc-20260327; b=k/mU6KoEwKTAVRZLioaUotvlFUOAm9loH/RGFtY6hjvbicVT+WhZtonb5gptxzjX7d 8B51OiBd0nY/0KAcRy8IJcQUeB2vW6XvLj84q9g01LQss9WaNw4Eji9j0OcsK+/3CIj2 oTFnyes7v2pr4ag1cSIPDBsCKz3D1Auo3UNE22xy6Yl5p4OkHl3LB0drAcTv/qkJLEuC grRmBTvJKmsbN54FG73O096ASXOky4GE1+zc8naPTHXdCb0q9ybc5MeL0hsMhU9vpaq9 r2UUmQKZhoU/cMezoAXwpkqmbDM9C+T/ojEnZLkFiP5pHe+ZraIPNpChkD8TNUm6AEyF ENVQ== 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=FL0sH6tmMeCB58BgWzlgjqockLwqw/vu/5+W5181ARk=; fh=CMILZ+goaghq6B4UpCDhf0EQp6kYD0LAlmOkUiVCOvs=; b=oEcUidmLhUY22pWPegdTG8k9TmYUQp3+eanMxrTG/F/mDiq0tPND7WM90iP2batjDM V3aL8TytDZCGERZHWjjukfO7vDEj515rV14L6RaM6lkhnyde4t0xADjNQmV1OuNsFXFK fO4fpYthzJ8ys5vrdiV1CAHfT++PEYH3dwepV/YvGkxyqr88hHu0P5yZMxjpIXIoD2Ew D2bZ056gPP2rbWFVsjVU3yYOOAAj3IPQXY9Zy3e3EI1MEop4EzPp3YRZGcdFifif3/sG mq/xnGKLw2wixF9dgF7TtbA2JhKYWVgO2Wy7f6Y/xiWlrs5PN+0Tfa82LLebVMbK5LnV QR0A==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=WG2L8Qha; 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 5b1f17b1804b1-4bb853d84c3si32012905e9.14.2026.11.19.03.34.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Nov 2026 03:34:51 -0800 (PST) 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=WG2L8Qha; 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=FL0sH6tmMeCB58BgWzlgjqockLwqw/vu/5+W5181ARk=; b=WG2L8Qha0kSq t8vAHaxD/J3Qtji3zCSIt+h6x/LM4G5vY6VMbZwc7QWoIDyldfi9tnyZq/wGtkOCe1p+Rp7v7OFgl YvI99YBTuCtwP8j5gg+eVjaNSbTsL4+aICxjnYWGS7tFG2wkhonSo50egB4rWkPrOZv6JEj0kM6/M YFciuecuzLBHQe24lA8OxLfnxyxhrRsNelT2tGWcTF+HOEnUoTA5Zyqd2n/XnzCXt5uwY7x0CLWDg HDtCe5xOX+xYXk5NyHAi6ZtIU3Y10ojlUt9RSaziDcevBZZQt0oxHLkrU/AhRacRJjvlKfNMQ7p5E wlRrUka7Rwt6Hz7JTgnU8g==; 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 1xU0Pg-00CjzV-2E; Thu, 19 Nov 2026 12:34:48 +0100 From: Alexander Ivanov To: andrey.zhadchenko@virtuozzo.com Date: Thu, 19 Nov 2026 12:34:46 +0100 Message-ID: <20261119113446.3579457-10-alexander.ivanov@virtuozzo.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261119113446.3579457-1-alexander.ivanov@virtuozzo.com> References: <20261119113446.3579457-1-alexander.ivanov@virtuozzo.com> MIME-Version: 1.0 X-OZ-Fwd: true Cc: devel@openvz.org Subject: [Devel] [PATCH hci-8.2 v9 9/9] 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. Userspace writes format extension (CBT) behind the last data cluster of suspended device on umount, resumes it, and then removes the device. 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. Removal of a live device runs the suspend hooks, and so the truncation, from __dm_destroy() without dm_suspend() waiting for inflight IO. This is safe only because an open device cannot be removed. 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. Take it only after the waits, so that a stuck top delta doesn't block status and messages: a message slipping in before the lock can at worst make the truncation skip, as the checks under the lock catch pending prealloc, file size change and unclean md pages. Skip read-only devices and raw top delta. Skip the truncation if md pages are not clean, as on-disk BAT may miss allocated clusters then. On error just leave the file as is. Skip devices in standby mode as well: the top delta file is dead then, and the truncation may hang with md->suspend_lock held, so dm_replace() of libploop could neither replace nor remove the device. Also skip it if the file size differs from top->file_size. Then another ploop on the same images writes to the top delta (e.g. a live one left by libploop error path), and on-disk BAT may miss clusters it has just allocated, so the truncation would cut its data. The check is best effort: a ploop that preallocated before our last metadata reload and allocates clusters in that area later does not change the size. https://virtuozzo.atlassian.net/browse/VSTOR-148171 Feature: dm-ploop: ploop target driver Signed-off-by: Alexander Ivanov --- drivers/md/dm-ploop-bat.c | 54 +++++++++++++++++++++++- drivers/md/dm-ploop-target.c | 79 +++++++++++++++++++++++++++++++----- drivers/md/dm-ploop.h | 4 +- 3 files changed, 124 insertions(+), 13 deletions(-) diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c index 76502cad57e4..2e2bcbf7bd4b 100644 --- a/drivers/md/dm-ploop-bat.c +++ b/drivers/md/dm-ploop-bat.c @@ -569,7 +569,7 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r } ALLOW_ERROR_INJECTION(ploop_add_delta, ERRNO); -static bool ploop_md_pages_are_clean(struct ploop *ploop) +bool ploop_md_pages_are_clean(struct ploop *ploop) { struct rb_node *node; struct md_page *md; @@ -706,6 +706,58 @@ int ploop_reload_metadata(struct ploop *ploop) } ALLOW_ERROR_INJECTION(ploop_reload_metadata, ERRNO); +/* + * Returns the end of the last cluster of delta @file used by BAT area, + * mapped clusters or format extension, according to on-disk metadata. + * 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; + + 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; + } + } + + /* The header is the last cluster, see delta_save_optional_header() */ + hdr = ploop_md_first_entry(&md_root)->kmpage; + ext_off = le64_to_cpu(hdr->m_FormatExtensionOffset); + if (ext_off) { + 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 a7366640df56..2e8cd9462621 100644 --- a/drivers/md/dm-ploop-target.c +++ b/drivers/md/dm-ploop-target.c @@ -451,8 +451,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_unused_tail). + * 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) { @@ -715,14 +716,6 @@ static void ploop_set_wants_suspend(struct dm_target *ti, bool wants) ploop->wants_suspend = wants; spin_unlock_irq(&ploop->deferred_lock); } -static void ploop_set_suspended(struct dm_target *ti, bool suspended) -{ - struct ploop *ploop = ti->private; - - down_write(&ploop->ctl_rwsem); - ploop->suspended = suspended; - up_write(&ploop->ctl_rwsem); -} static bool ploop_is_standby(struct ploop *ploop) { @@ -751,9 +744,67 @@ 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; + if (ploop_is_standby(ploop)) + return; + if (!ploop_no_pending_prealloc(ploop)) + return; + /* BAT on disk may miss allocated clusters until writeback is done */ + if (!ploop_md_pages_are_clean(ploop)) + return; + if (i_size_read(file_inode(top->file)) != top->file_size) { + PL_ERR("Top delta is changed by someone else, don't truncate it"); + 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; + + /* BAT writeback finished after ploop_presuspend() is not durable yet */ + ret = vfs_fsync(top->file, 0); + if (ret) { + PL_ERR("Can't sync top delta: %d", ret); + 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; /* * Let the next resumed ploop (maybe of reloaded table) see @@ -770,7 +821,13 @@ static void ploop_postsuspend(struct dm_target *ti) if (!ploop_is_standby(ploop)) wait_event(ploop->inflight_md_wq, !atomic_read(&ploop->inflight_md)); - ploop_set_suspended(ti, true); + down_write(&ploop->ctl_rwsem); + /* 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 b00e3eef7b66..ccc0c713e078 100644 --- a/drivers/md/dm-ploop.h +++ b/drivers/md/dm-ploop.h @@ -37,7 +37,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) @@ -644,6 +644,8 @@ 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 bool ploop_md_pages_are_clean(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