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=1796409639; b=nf47izgsxOQx+PIuLb8+dA/0wixZLYy6NLZcL4sEw9Kidmkrf19JNRm8Dis9X4c52dHF Kd4PEMdYF7Syt8cHsKgk2N+AX8YwyCW9SqOuRAvpJcK6/Tjq7bKPQKVxnNn33/lqz8396 uyf7yvj57Iog2N5iebp1X5FB/49TbB5xGWUCj1lnIz7rxvnOOB9ajG5/BPBxq0DD5hNWp OnIgDPI8yMhGVLUfg7XREOKDmh+hUNnSHgaMpy06Zp6qU23+bY1lVCcAETVukZEFF+gu1 TFXeM7ZMrLS5FpLf7TsWtVIkK2qpDzeKelSwprzOr8Sk3YGR7CW1/9Ks9dHnX0W1VIw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409639; 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=vW8uZnYQKc/O8aGAkWVJhDZVTd2Z+otH42B9fd3Ud/4=; b=BjSmwaW9xCJLMSKrFhA2xk09pQB7n8u43EREKoH14GTGpJke9boxmh59Epzqh66CR8m6 FL+y+zVXXYhTRvaqHllOpb92Z0zi9tOMmP2Fc2naopsEtXSuAFF6Y1iZiiTyYMTHHAQvn afi1Oqe/s9BivkoRrn9x9COab0loT0uva57oRYdhXLhY2gPg9dqx0RrNg/DHBZlStBVlN uW7abngaULKR62wiWpfGNk8PYlG/iv+Ek/rz391szch9aKs7q73dJgYW33oUiClgw/HYr +uh7YFfytzuq0QP4+tB/zyqNhFcAsy2IUXf3VSCJ3iSBNQMwGrMmb+Zwij0uqqCklfQ== 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=Srstprg9; 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 3057C606A68B; Fri, 4 Dec 2026 18:40:39 +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=Srstprg9 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=1796409639; b=cKtA4EPNzQRC4U2wrGOsR1ydKg3y3EfeKbdkJYWd/y7gSOcaxI8/jqaQIAx6tBHtgYgW JFKXbX8FKT3rVx1E8a7f/6D1s5PUg48Ki/IrIPx3aunDuOnFSaWtsYl+QkmiSgmCAFrU+ qzcKnZ/a7YaMCacaM7jiQ7oe50RlxdfUMh6JOjrO//fbN5luL8ZNrrZxAGhSga40mn2NM NtdZubsTv6Tgt6qGGRox4itRVsCKhENeRcPeVXkARRzoHPm73YSSKSLEWtfGf96l45PYM e3/taOhVBpOLEBt2CPUdVPk51wskhrBvo+qD+LhfyUcU+iZ7xoIPZkIfnsTglNJE21g== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409639; 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=vW8uZnYQKc/O8aGAkWVJhDZVTd2Z+otH42B9fd3Ud/4=; b=TnoppQttSmnZn3mscQhgFl/ceSORv19upJpYLGsHkQ2oVqFQ16N9JYo4m0Kerv2LOUNA 7783iy/Zi29TlchD+Qi+y5mm5LKY01JizZGwODzAFDOHPKZ/vtQlUoojQnTk7ASD+Pzoz tRmTybztF8vGPlsWxZFuz9jHjD8NveibhsDmYWIj+9IvgmZlz+XozYIycz/KcOvt0zJ3q HnxRC7b4AkinI1tfxQZp7ZL71L7BCz4mYHeizhcazARmktiKJfND+PyOPSDJAOtF1lmDY Ak1+0GS/3dCp4tzZkA8orv1VKp0D7Vm7B/wFSBjfkRDY8xn4dujEuv+E8PUHE15ymgg== 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 A51801F00A06; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796409639; bh=vW8uZnYQKc/O8aGAkWVJhDZVTd2Z+otH42B9fd3Ud/4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=Srstprg92C3vYEWLN6UgJlBkQKQDbZ6avo6M/ZRu/aIrUGgHYjz7u8EpLf63Nfbo+ EJP9UkeOVxYXHoJx/slNU9QmQlu7Lp+G0OlLfMT8DRTHsn5m9qVfVzxrqDnOb1E/wq +M7kFnR+pQvEdE2ihcc3hUKnRQsrQUPdNOLzBf6EPo60sChZ0lNIbmaiTjxWClwRdP wyY+KyfSyRATHRqMUSIQVOWu8b4IC0w/oTWpBvt10Ul1DCWr9HQ8NheAWUgg4X+8Xr R7CKArFPBamVpVr+bFPN5xcvvhsbSyQ0zEZhbn2ZSP0H8EFRaTY1L7PD6CZXbFn2oI BoMh3VPPJsnaA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 92E194DC3E; Fri, 4 Dec 2026 18:40:35 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:36 +0100 Subject: [PATCH v5 05/13] 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: <20261204-b4-ntfs-delalloc-fixes-v5-5-1cd0267df2cd@proton.me> References: <20261204-b4-ntfs-delalloc-fixes-v5-0-1cd0267df2cd@proton.me> In-Reply-To: <20261204-b4-ntfs-delalloc-fixes-v5-0-1cd0267df2cd@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, Baolin Liu , Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=6039; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=lHbRHHlcYZGWDL2Fo0hpNBMrF7mBcxpkY2ltlfftO2g=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhTgWrGO7VPYXp35Qv6AXoGOkHNmdlOW8ImiE47f9W5 jVt0XIdpSwMYlwMsmKKLB3/b4T4hHz7tCxOqw9mDisTyBAGLk4BmEixDyPDrOXRQX+dfgskG/Ye LG31LJKJTl6/I7xHz1Pb1EexSK2W4Z+CP8uB5+8+Xjh7LEz4hOo5NVFOdqngd18V9v/WOZrKt5k FAA== 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 Reviewed-by: Baolin Liu 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