From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796146988; b=0FiRsYpUDMnqiM2CAXEx0H7OcJUVT6UVvA6KUlaE5g8pMWWJn67+tjGoNF6TFFUZjL53 EVSkS4X9UFuD+qpcCCdORdUhd7vx80NW9urrKicMUQS3zxm4ndIxfFBpZg5yoYvw456QG Ii7FO0LUE+3cypfR2FiJSwa2K4fn7a2A+OL9FQLPyfqIcdyszREBLNyn+FUdHezHCdkyS s6HWZ6GHYySesCnEt0oIcDpA2N2n1dSomxs0VIKx7d47cY/IYbQPYd1Nz9wTipk6H1fFN qqV107IrkI8B+AYfyqI8bIEQjh4oWHGmNlgAZNalNtPKEhbWSJIemgepJ2xomAESclg== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796146988; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=O4zbHKproPU71z5fvhT2T0+yadyZnhoGVWdO3+gOYbE=; b=UXQ+plFa1xOatpCSF3qcCEZcYC8fbkWa00Ek2uwqyknuKKJmX4MmCQBFnfLvOhnxnMbr 1ErTa6Vhk/HF5tHd9VLa8MQissN8g9v3Wxfyme7zmAi9i6QBB+0l/iyafcuW6HUJoM17k SfeW12d39OLXhJOmz6RT4nzo7gqiJjVZIFFbnGndS0Dvde9DJmINWIxJkUyYX8Wokv6Ku A6tFXSK0yrFOAFc0rDikQPM+PJQieYqnSwQpC7XOQTfnUNE4W/KjQeh460MWOWtgMAbhK rn/s8ieBp8VW03nv5GuQKv5ltcTE8O/oyihaU8arMB+EBwSOKwwi4zffp2W/PB+iHGQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oEcpmT09; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B82A9606A689; Tue, 1 Dec 2026 17:43:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=oEcpmT09 Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1796146988; b=FShXG/PbT4n7lpZY1+YfErf/OSnlg2c2nzxYdfb27c/90MR69dKE2hhWrKpMJwqxWkzO jV8kEKmEeD1KrbkwIhcJkz5f0WkzKsSl8fK1qvT6kJdVQJykrXixzwVRtmRReBy5cVn5f IrjkWUf7exLH8lyOCmqn0U+LrCOZg5h4zHkhrp3eydiqi3McNt2m6db30TSVzQ6i9SVMr bRPIzDhe2EFlKh8jMidEqBw15ehodaQN9tZ6+et1BXpLG0fQSumEzw/KdQsSF0FPTEirq vmn/ticogFP/yas/IuO56+D8r8uQ/stsBXRsZsMztW5URdv4/paSmsOxfBE1bRDpQyw== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796146988; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Message-Id:References:In-Reply-To:To:Cc:X-Mailer: X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To:Content-Transfer-Encoding; bh=O4zbHKproPU71z5fvhT2T0+yadyZnhoGVWdO3+gOYbE=; b=OUuTyHUdP7KRxxXIgFpdGAKmVaCeGwYfd59etC4IbLZg+64eUcyn9sXRFr4NokbANNv0 wKTCMDWeeY5LNwyi4pC6huG2KDqRoyO4R2ZqTKbo2kLi8uxWOtSCjS5lU/RxLaih3H/it x/75bD5UFdbjjAxefoIFRdsP8ZdI4qk8blgsG/eFR1DbQZ341SI47XBWaxpEFDwz+RmKe mJQThZ1vx2KBqGt8o4ZQiWVvykOHAzlGl6NmAneC2LhR7X3lbdwJn6c5GKioMPJGutcvi WcWI6D6HCTQnTbizGvnkxcH25WNsKkm3BiT9KNrqb4D8NbfKI7WiC5PtP1rIUTKJRKQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id 321CC1F00A1E; Tue, 1 Dec 2026 17:42:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796146988; bh=O4zbHKproPU71z5fvhT2T0+yadyZnhoGVWdO3+gOYbE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=oEcpmT09+mX29mxbUtsfco4eHH75qDyT9QIy8lHP5F5c85TJ1YUiUl5ki1yPlmZqW 4kDjOLjn4rXkSdLDsO7pWD0vpYujwWx1gYL6Syeqon8kIcPIK8II8ubbUCAfd2x+hR ul1ygtXNaSNYu1+y/M84Gk8cmX9t/OiMIfxjr4g8c+sv0gT3kLmxO4q4yuiB1kaBye IBpPO7r4NG8mBqnHdkwyTta2mRGMEBdUsENYob3XaHeu8S58qzq6c16a/Vk1vbnr8p 8z+nJVHSaN+mq/k6kdYUD5a5Uf+GLMZF/bwe9geHk0DeAAg/KfOH1+BtDwBMNixCvZ ZOj3C3H/oajEQ== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 02E3E4EF1F; Tue, 1 Dec 2026 17:42:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Tue, 01 Dec 2026 18:42:40 +0100 Subject: [PATCH v4 05/11] ntfs: keep delalloc reservations when a cluster allocation fails X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Message-Id: <20261201-b4-ntfs-delalloc-fixes-v4-5-e5dd6f77016a@proton.me> References: <20261201-b4-ntfs-delalloc-fixes-v4-0-e5dd6f77016a@proton.me> In-Reply-To: <20261201-b4-ntfs-delalloc-fixes-v4-0-e5dd6f77016a@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: Christian Brauner , Christoph Hellwig , "Darrick J. Wong" , ntfs@lists.linux.dev, linux-fsdevel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-kernel@vger.kernel.org, Zhan Xusheng , stable@vger.kernel.org, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=5991; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=11QckI8EIcOZpImpDuB9ZvptbCxJM5UvtLtxklxZU+I=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmx+dh6GvBNr9gp08GVLH9pf8fdlmc0P328Oxqn8n13aH ltfvmzZUcrCIMbFICumyNLx/0aIT8i3T8vitPpg5rAygQ3h4hSAifDIMPwznRTVc+nPdqn3q1Zf YP484/DnSf9lNv9OvlzibN54TiLkCsMfrtyddiuL+SYIiPyRjWJcHPP5tufyy9rOxmLJyy7L17T xAAA= X-Developer-Key: i=ernest.borowski.git@proton.me; a=openpgp; fpr=88FFD8544C54F6F2A65E2A8E730769A37A6214FF X-Endpoint-Received: by B4 Relay for ernest.borowski.git@proton.me/default with auth_id=1589 X-Original-From: Ernest Borowski Reply-To: ernest.borowski.git@proton.me Content-Transfer-Encoding: 7bit From: Ernest Borowski ntfs_cluster_alloc() sets vol->dirty_clusters to 0 when it fails after the free space check, which in practice means -ENOMEM or -EIO while it scans the bitmap. vol->dirty_clusters is the sum of the reservations of every delalloc run on the volume, and those runs are still there, so the reset makes statfs() and the next allocations treat the reserved clusters as free. Buffered writes then reserve the same clusters a second time, and writeback of the delayed data later fails with ENOSPC and loses it. ntfs_attr_map_cluster() has the converse problem on its own error paths: once the allocator succeeded for a delalloc run it has taken that run's reservation, but when the runlist copy, the runlist merge or the mapping pairs update fails afterwards, the clusters are freed and the run stays delalloc without a reservation. Reproducer (fault injection): write 4 MiB to file A without syncing, then fsync another file with /proc/thread-self/fail-nth set so that the kvzalloc() in ntfs_cluster_alloc() fails. statfs() free space jumps back up by 1025 of the 1028 clusters A holds (4 KiB clusters). Writing a file of the size statfs() then reports and syncing loses about 4 MiB of that file's data; on 512 byte, 4 KiB and 64 KiB clusters alike. Fault sites after the allocation (the runlist copy in ntfs_attr_map_cluster()) raise the free space by the clusters of the failed request. Leave vol->dirty_clusters alone when the allocation fails, and hold the reservation again when ntfs_attr_map_cluster() puts a delalloc run back after the allocator consumed it, before the clusters are freed, so that no other writer can reserve them in between. If freeing them fails too, the run keeps the clusters and the hold is dropped again. The error path after a failed mapping pairs update now returns the error without also reporting the freed clusters as allocated. Fixes: 11ccc9107dc4 ("ntfs: update runlist handling and cluster allocator") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 24 +++++++++++++++++++++++- fs/ntfs/lcnalloc.c | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 31ee811458..3944e32764 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -5465,6 +5465,19 @@ int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize) HOLES_NO : HOLES_OK); } +/* + * The allocator has taken the reservation of a delalloc run whose clusters + * are about to be freed again, the run staying delalloc: hold the + * reservation again first, so that no other writer can reserve the freed + * clusters in between. + */ +static void ntfs_map_cluster_rehold(struct ntfs_volume *vol, bool was_delalloc, + s64 clu_count) +{ + if (was_delalloc) + ntfs_hold_dirty_clusters(vol, clu_count); +} + int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, bool skip_holes) @@ -5476,6 +5489,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, struct runlist_element *old_rl = NULL; s64 vcn = vcn_start, lcn, clu_count; s64 lcn_seek_from = -1; + bool was_delalloc; int err = 0; size_t new_rl_count, old_rl_count; @@ -5563,8 +5577,9 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, * fallocate(), direct I/O or page_mkwrite() have none and must leave * the reservations of other writers alone. */ + was_delalloc = lcn == LCN_DELALLOC; rlc = ntfs_cluster_alloc(vol, vcn, clu_count, lcn_seek_from, DATA_ZONE, - false, true, lcn == LCN_DELALLOC); + false, true, was_delalloc); if (IS_ERR(rlc)) { err = PTR_ERR(rlc); goto out; @@ -5578,6 +5593,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, old_rl_count * sizeof(*old_rl), GFP_NOFS); if (!old_rl) { err = -ENOMEM; + ntfs_map_cluster_rehold(vol, was_delalloc, clu_count); if (ntfs_cluster_free_from_rl(vol, rlc)) { ntfs_error(vol->sb, "Failed to free cluster allocation after runlist backup failure."); @@ -5591,6 +5607,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, if (IS_ERR(rl)) { ntfs_error(vol->sb, "Failed to merge runlists"); err = PTR_ERR(rl); + ntfs_map_cluster_rehold(vol, was_delalloc, clu_count); if (ntfs_cluster_free_from_rl(vol, rlc)) ntfs_error(vol->sb, "Failed to free hot clusters."); kvfree(rlc); @@ -5613,11 +5630,15 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, if (err) { s64 freed; + ntfs_map_cluster_rehold(vol, was_delalloc, clu_count); freed = ntfs_cluster_free(ni, vcn, clu_count, ctx); if (freed < 0 || freed != clu_count) { ntfs_error(vol->sb, "Failed to free cluster allocation. Leaving inconsistent metadata.\n"); NVolSetErrors(vol); + /* The run keeps its clusters: drop the hold again. */ + if (was_delalloc) + ntfs_release_dirty_clusters(vol, clu_count); goto out; } @@ -5635,6 +5656,7 @@ int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, "Failed to restore mapping pairs after allocation rollback.\n"); NVolSetErrors(vol); } + goto out; } } else { VFS_I(ni)->i_blocks += clu_count << (vol->cluster_size_bits - 9); diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c index 7e00016f42..2fbc24e4c6 100644 --- a/fs/ntfs/lcnalloc.c +++ b/fs/ntfs/lcnalloc.c @@ -799,7 +799,6 @@ switch_to_data1_zone: search_zone = 2; } else if (err == -ENOSPC) ntfs_debug("No space left at all, err = -ENOSPC, first free lcn = 0x%llx.", vol->data1_zone_pos); - atomic64_set(&vol->dirty_clusters, 0); out_restore: up_write(&vol->lcnbmp_lock); -- 2.56.1