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=1795371594; b=txR0vW/eqDlGlO4QX8yDUrd4KLaPRSqrjpdJ/H+7JHmAfjTQRJBSiRRCpQh5+FhdmiHX SxnsC1TJE0rQx7KxCJRN/Qqnv7LFxfU0HHNLfPCYKm3xfmX/u213AV5RieT38hLAvdGBz bG+tTDpjVMS4if+46ylcyfXFyNsDAbuN4OuUkk/kTxGfLQasLBvxpOmKR8V713lArDvzj 7a1IJUcfKZxtBXOXsyktOi/U59e3H7bgE/1sPJjDIwoELu6KpAPQtrByemJsf9BrtPuFV pLUfB4eFVhI3XaP5QbsCXyvE2kKYHvSRDn7Tzg93W9zkMEWAK4hDmLIdmi5JVB6+nQQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; 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=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; b=40UyphIKWp4UXu0xQp555x18JnTW4c03bpRK7+UXd3dhwylms0ygD14Jtk2u4oCvV6BW I+CbrRt2PS0p6+ggcKV1/puej/8eLnVfUtUPTGBr0m3F3rviuH6GIuE0uVJX9WlVVKhhV UgRKjVW6X1Od8BwmBVYTqJRhOYKeVVazRvnVqDxGppNVLe+j7Cbcpt6NiXnLagBxg7etx ipA0/46t28DtKatPTN++Yh3wBrc8NLE4+2T/ccm19/7qqaDFOOKYbXIeuVg1Brp6YpchN 4t6huBEvDYDo7JWIvkGest+W67qiz++yTZwQb8ML/VEPdAQkqZ3I/94ps9OMC9hdwkA== 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=AZFyoKX5; 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 30903606A698; Sun, 22 Nov 2026 18:19:54 +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=AZFyoKX5 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=1795371594; b=aX5S4ZlQ8satUouxw2xxfp2Hv6RPG+AH4+4XoONixL8bgbnnZuDJ7fhWo4E6joPEOxmZ lvf0i4ztRKmdq7ntYcMOiugrMLc8xmRp5vtuzh5gmg+KAp/4fC0jay4iuCeHBwIf8IIM3 RwgnW6pvtqqxUpdSqdAyybDMoLsDw4WYkHRUP2bHkam7+Vj9ODeOLDYv/0a4SzoVCddwB WoC7YqG6HBtwzOMAmWYWRzbB1qmfmCP/evxagRuAgg2RHD3+hf3MWWNi1xMVemxiKPbmy XTqA0chf5S38uHcG5K7xOJMURn12byqdO1gdc8l2cs1jFtI2xz8KBhJMocR4t7FPQJQ== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795371594; 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=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; b=GssBGwkG0eKV2olunf4PImFakYHnW2MetgiysNI9v+W65gF+pHn/E/vLW0Srcjz46Mg7 Sx4NFVQabKBgz52o7G2Za/Hkennjm9NXFivMP3T1jtBoC4QUXWvutsEyj26y6lk4Lf/tb 8nYAmYBIo+GV7oR8DPPGCmP2kA3jWkYwgWTRz1Ky3CJAJDaigDUpHVAUAWFT5W2KGZUdI caTGxYJ9DTQgk8PtTTn1de5lwiXIH8nb+h8y3ha9C1/lc+e3UFc8JxmSL9y1BbtYCNQOT xs5ybp5RElmMwOCJksLy1/r1oz45BoBZKp5aBWhqrqlpG36BI2Qj3kxjWLkMRsY0p8A== 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 120641F000EB; Sun, 22 Nov 2026 18:19:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795371594; bh=R0f3OJAgm4ujtP5f3KKgLJeANRzsR2n5JuwmMJaa2AQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=AZFyoKX5hgGawpoP4c43DMrT/1VSjkua2wsRdaWz42i1mfjuKN6RKDB59itDYiyPv kjFgOFXZeRuWs1Yq2rADIc0GGxmG0PE+5XXhjvlTj/XMOsZ982odplET7SWjg/N6IK GsCvEICKScK/woKpvl1+BuUM5qbK+H2nE9bBacZxb6iQmtSZv2IkfJR9fvURQDGiSN rm+zXiUU9tiDxcPMGfXelUPiLt4XuIpf1Xya+CJsW2Ya0oCyz6BPFj/72doayhImr0 O1WUDvHpyVEdOALfc0AXj6xT0Eyp7JozMeZDqppB0XmpGKYOb4dZ5UtZJvCkahfdKb uVI0pOdJBQ18A== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 0423A4C188; Sun, 22 Nov 2026 18:19:39 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Sun, 22 Nov 2026 19:19:41 +0100 Subject: [PATCH 5/7] ntfs: punch out the delalloc reservation of a failed buffered write 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: <20261122-b4-ntfs-delalloc-fixes-v1-5-0726f0dce403@proton.me> References: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> In-Reply-To: <20261122-b4-ntfs-delalloc-fixes-v1-0-0726f0dce403@proton.me> To: Namjae Jeon , Hyunchul Lee , Baolin Liu Cc: ntfs@lists.linux.dev, 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=9090; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=OfhotPEQBsrIVWZ7Allecp+BirX5T5buedwwuU3fnB8=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxmI8s5glw5t/Yt3PbUV7VN+NXpzdkH+25mm2+e13DH4 YxT1oL8jlIWBjEuBlkxRZaO/zdCfEK+fVoWp9UHM4eVCWQIAxenAEwktYmRYdl5nnkdS9bYnJ/T kL5nlsn0i5+ny1pf/N7V7GE547HxkThGhgm8UZ690xsurwjw7RYy2bm2ctqkayaG9rk9a7qctU7 95gMA 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_write_simple_iomap_begin_non_resident() turns the holes a buffered write covers into LCN_DELALLOC runs and reserves their clusters before any data is copied. If the copy then fails or comes up short (EFAULT from the user buffer, a fatal signal), the clusters that no dirty folio covers stay delalloc. Writeback never allocates them, so the reservation is kept until the inode is evicted, and the stale runs survive the writeback that fallocate() relies on before it edits the runlist. Reproducer: a pwrite() of 512 KiB from an unmapped address into a hole of a 1 MiB sparse file fails with EFAULT and leaves 128 (4 KiB) clusters reserved after sync. A FALLOC_FL_PUNCH_HOLE or COLLAPSE_RANGE through the range then hits the run splitting bug fixed by the previous patch. Do what XFS does: in ->iomap_end() of a delalloc mapping that was not written completely, let iomap_write_delalloc_release() find the ranges without dirty data and turn the clusters entirely inside them back into holes, releasing their reservation. Clusters that still hold dirty data stay delalloc, including the partial first and last clusters of an unaligned write that ->iomap_begin() zeroed in the page cache; those are written back as zeros, as before. Delalloc runs are holes on disk, so the mapping pairs do not change. The error path of ->iomap_begin() itself, taken when zeroing the edges of the new run fails, only subtracted the reservation and left the run and i_dealloc_clusters in place, so a later truncate or eviction would release it a second time. Use the same release there. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/ntfs/attrib.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ fs/ntfs/attrib.h | 1 + fs/ntfs/iomap.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 39fa983cd8..7c19e4534a 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -4416,6 +4416,69 @@ void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn) ni->i_dealloc_clusters -= min_t(s64, nr, ni->i_dealloc_clusters); } +/* + * ntfs_attr_punch_delalloc - turn delalloc runs back into holes + * @ni: non-resident ntfs attribute + * @start_vcn: first vcn of the range + * @len: number of clusters in the range + * + * Turn the LCN_DELALLOC parts of [@start_vcn, @start_vcn + @len) into holes + * and give back their reservation. Used when a buffered write that reserved + * them failed before it dirtied any folio over them, so that writeback will + * never allocate them. Runs that are not delalloc (anymore) are left alone. + * Delalloc runs are stored as holes on disk, so the mapping pairs need no + * update. + * + * The caller holds the mrec_lock of the base inode and the runlist lock for + * writing. + */ +void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 len) +{ + struct runlist_element *rl, *punch_rl; + s64 vcn = start_vcn, end_vcn = start_vcn + len, run_end; + size_t new_rl_count; + int rl_count, err; + + lockdep_assert_held_write(&ni->runlist.lock); + + err = ntfs_attr_map_whole_runlist(ni); + if (err) { + ntfs_error(ni->vol->sb, + "Failed to map the runlist to drop delalloc runs: %d", err); + return; + } + + while (vcn < end_vcn) { + rl = ntfs_rl_find_vcn_nolock(ni->runlist.rl, vcn); + if (!rl || !rl->length) + break; + run_end = min(rl->vcn + rl->length, end_vcn); + if (rl->lcn != LCN_DELALLOC) { + vcn = run_end; + continue; + } + + for (rl = ni->runlist.rl, rl_count = 0; rl->length; rl++) + rl_count++; + rl = ntfs_rl_punch_hole(ni->runlist.rl, rl_count + 1, vcn, + run_end - vcn, &punch_rl, &new_rl_count); + if (IS_ERR(rl)) { + ntfs_error(ni->vol->sb, + "Failed to drop delalloc run at vcn 0x%llx: %ld", + vcn, PTR_ERR(rl)); + break; + } + kvfree(punch_rl); + ni->runlist.rl = rl; + ni->runlist.count = new_rl_count; + + ntfs_release_dirty_clusters(ni->vol, run_end - vcn); + ni->i_dealloc_clusters -= min_t(s64, run_end - vcn, + ni->i_dealloc_clusters); + vcn = run_end; + } +} + /* * ntfs_non_resident_attr_shrink - shrink a non-resident, open ntfs attribute * @ni: non-resident ntfs attribute to shrink diff --git a/fs/ntfs/attrib.h b/fs/ntfs/attrib.h index faa2dfba3a..ca2a8b53a4 100644 --- a/fs/ntfs/attrib.h +++ b/fs/ntfs/attrib.h @@ -110,6 +110,7 @@ int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s6 int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len); int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len); void ntfs_release_delalloc_from(struct ntfs_inode *ni, s64 start_vcn); +void ntfs_attr_punch_delalloc(struct ntfs_inode *ni, s64 start_vcn, s64 len); int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize, const s64 i_size); int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 prealloc_size); diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index ce916707f6..1909824c30 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -372,6 +372,10 @@ static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length) NULL); } +static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap); + static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset, loff_t length, struct iomap *iomap) { @@ -543,7 +547,13 @@ static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_ if (err == -EPERM) err = 0; if (err) { - ntfs_release_dirty_clusters(vol, max_clu_count); + /* + * Nothing will be copied: drop the delalloc + * clusters that no zeroed folio covers. + */ + ntfs_write_delalloc_release(inode, offset, + iomap->length, 0, + IOMAP_WRITE, iomap); return err; } } @@ -762,6 +772,61 @@ static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos, return written; } +static void ntfs_write_delalloc_punch(struct inode *inode, loff_t offset, + loff_t length, struct iomap *iomap) +{ + struct ntfs_inode *ni = NTFS_I(inode); + struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? + ntfs_base_inode(ni) : ni; + struct ntfs_volume *vol = ni->vol; + /* Clusters that still hold some dirty data stay delalloc. */ + loff_t start = round_up(offset, vol->cluster_size); + loff_t end = round_down(offset + length, vol->cluster_size); + + if (start >= end) + return; + + mutex_lock(&mrec_ni->mrec_lock); + down_write(&ni->runlist.lock); + ntfs_attr_punch_delalloc(ni, ntfs_bytes_to_cluster(vol, start), + ntfs_bytes_to_cluster(vol, end - start)); + up_write(&ni->runlist.lock); + mutex_unlock(&mrec_ni->mrec_lock); +} + +/* + * A buffered write that failed or copied less than it mapped leaves delalloc + * clusters that no dirty folio covers: writeback never allocates them, so + * their reservation would stay until the inode is evicted, and the stale + * delalloc runs would outlive the flush done by fallocate() and friends. + * Punch them out again, keeping every cluster that has dirty data in the + * page cache. + */ +static void ntfs_write_delalloc_release(struct inode *inode, loff_t pos, + loff_t length, ssize_t written, + unsigned int flags, struct iomap *iomap) +{ + struct ntfs_volume *vol = NTFS_I(inode)->vol; + loff_t start_byte, end_byte; + + /* + * Delalloc is reserved per cluster, which can be larger than the + * block size iomap_last_written_block() would round to. + */ + if (written) + start_byte = round_up(pos + written, vol->cluster_size); + else + start_byte = round_down(pos, vol->cluster_size); + end_byte = round_up(pos + length, vol->cluster_size); + if (start_byte >= end_byte) + return; + + filemap_invalidate_lock(inode->i_mapping); + iomap_write_delalloc_release(inode, start_byte, end_byte, flags, iomap, + ntfs_write_delalloc_punch); + filemap_invalidate_unlock(inode->i_mapping); +} + static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length, ssize_t written, unsigned int flags, struct iomap *iomap) @@ -769,6 +834,10 @@ static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length, if (iomap->type == IOMAP_INLINE) return ntfs_write_iomap_end_resident(inode, pos, length, written, flags, iomap); + if (iomap->type == IOMAP_DELALLOC && iomap->flags & IOMAP_F_NEW && + !(flags & IOMAP_FAULT) && written < length) + ntfs_write_delalloc_release(inode, pos, length, written, flags, + iomap); return written; } -- 2.56.1