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=1796409643; b=IRJ3LfIwxvHlP1NS33NgjKAuoswcL5o33/pbreeT6GJG/Ota99qTqpionX5foSVv4bMZ 5KmHsDUUaaqUoKPZWTHeLXqYMOSMkojsx+/bkbpUwubz41HIFv8eBUkt1pzKH3HkrdAnQ J61/PouaBArUgg/rYxuKigs8TsH1b3LMzm4PFL0QnrbsS3UtXfg/Rpp7pBgpW1Q6J/tnG pYmkptdy/7qqngOP7Kqd0nK0VP/kLl7tlfh0WdG7yBYZPLkhSG58MHH0sm+GtzIsqt3sq XaddtIMKoCbroTemewy4EBdRe/ugSu664BiD0dU8Ol93bzZWE8/iKNpP4x6Wp4/9PqA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409643; 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=OT3+stYaj71kD+Ff6Bt2kwqkoAU5Liv+1nfvUZqpKAk=; b=yiCILbbSpZXLCSSCNiKIlmlS5JqkCyzrpVrhK2OEujp/aX3Om/EVXgXiv3ApqJdqaTYv bioNrSPky/ieViQrk+e76Z2UyaqqosH+Fmbu8GCuEydxpQKQ4VmlsCQpyGtBL5ekWGUIE 3l93HdAFrt+8PXxh0cSTlgbUizmZk8Ne7ByfQHFyoectRz+nXOBX902NQeWMbjinvsxQR JF1LoZO3YxKEpuWqE69Q300Gkg5hukBGYvU49owph4U0IhBZIS5LbsVCR3bVJxpyg3Eqp dYrlIsNARyIVGn8QqWA0OCE20JInyj72OWNDjT0k68TzogQbuIA9c8zifb/tcBod+Ig== 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=NpnNtLQO; 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 4B70E60A260F; Fri, 4 Dec 2026 18:40:43 +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=NpnNtLQO 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=1796409642; b=A09Tq7hQTNM9F8RmoXEHnMgZHPlYS6QnJ0reAif2+ekd2UdAWMauniPsGU5TO5IFAVlC g7CLEa+3PjEDeq6zXFMlM9zUEIb4Fm/nSLlLevV8RWYcNC44HbB4FZrJhK7Th7LQxDmiN GzvmOIN8f8MI8MBkgzY5ZfLK6gjbDT+2ZrhaD5ymLoJOlRvfFFMRZOf+9jD2RlX0fhGyr tQyP0gVofTI5vJyWP1pTbIl4GpHIEgIJiHkRHrfWbMgIdGcDMTk7wTvkGFXMcv1Ob7ix5 ihM3TbO8qqaFcPHph7UqfTb2mdUdZqUaR5DetwU7VNU69ibk/Md9bRO9RckanNWldgA== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409642; 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=OT3+stYaj71kD+Ff6Bt2kwqkoAU5Liv+1nfvUZqpKAk=; b=YnlkGxnDvk9+f4INy6aySeKeXpyIx6tAORauojK3XpuzCxG6lQusYxO5z0lND4HXJmtP a04QtuGv/3zZ3yUO9CO8coCO3D2cYGmMtULihH0kqx9+G8I2EkLHS2SBvklurNLtanGYk mkwjQyGiRzcR3McOcTGNF7j/x3WqK2ZFnic66FsB2tK2LzBJ6XiXykSaCmDaCAp4RWKSK bUnwabnjBDnA2YF8WFww3zBzy8J3X2dTDNVJCGAjAiwyiRE+I9AcfGHFokDCF6+Dzkg2z Tq/2BlAM4tyHeDRY8XX9hg8H0I9soItdDruoolvRPA+2vCOLTuLCFTHxxC9m6i8KGkg== 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 C99581F00A1D; 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=1796409642; bh=OT3+stYaj71kD+Ff6Bt2kwqkoAU5Liv+1nfvUZqpKAk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=NpnNtLQOXs8eDWLOyFnM10CZ9EuleAdUbZ2Gu8ggPmxHct/4COeagD3ciy039c5uu NUTdzDwB2ySxttKrFx7EzMtyImcPm0aGul/SBPjVYRq06I+5HHEOr3kQzwrdZ1VwcG nl8Qv39EHVtvgFTWUwDydAV6KIaPkr29KbY6rCi7JEsyt78BePORgzpoSS6QFRoR6/ y72H97RUDhAnV+SWPvHxDlEaxoWhWNKBvwW1mp0DdtU8dmuY4Id/buqW7i5T+ia9WR yEHIZFQDcpmoNNB2MRHWrC8Ic1rPi2d5fL5z0eJLZpz+lFZmdMkBYgqn/m51jKWhiQ 29sN2f0FzCyNg== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 17C324DC4A; Fri, 4 Dec 2026 18:40:36 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:39 +0100 Subject: [PATCH v5 08/13] 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: <20261204-b4-ntfs-delalloc-fixes-v5-8-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, Ernest Borowski X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=11703; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=mZP5iDRB9VT45UhppLUdg/yKF+fidfTEdAhj1tNHGyc=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhToX0G4YVp+4z5NgVsiQv3zjv+bePdUuKtY70zWFYF 1nNm2fYUcrCIMbFICumyNLx/0aIT8i3T8vitPpg5rAygQxh4OIUgInU3WRkmDrD7ytfvfLdV5Jh fl4TDprOMdFUaJvK375b5Nvu09ErbzAyTOy/fKxVOPy2YqHwads875A4xpj+3Lki081CBOdmMH7 nBQA= 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. A failed write that moved the file size is then rolled back by ntfs_write_rollback(), which drops the folios past the old size and truncates the attribute back to the old allocation, so a delalloc cluster that ->iomap_end() kept for its dirty folio is released by that truncate. A FALLOC_FL_KEEP_SIZE preallocation past a hole keeps the allocation, so a zeroed edge cluster in that hole stayed reserved. Punch out the delalloc clusters of the write that the rollback leaves wholly past the new size and below what the truncate keeps. 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/file.c | 29 +++++++++++++++++++++++ fs/ntfs/iomap.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 163 insertions(+), 1 deletion(-) diff --git a/fs/ntfs/attrib.c b/fs/ntfs/attrib.c index 71fc4ca006..3de589ca0b 100644 --- a/fs/ntfs/attrib.c +++ b/fs/ntfs/attrib.c @@ -4453,6 +4453,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 8d72e9180c..dd073f9fd7 100644 --- a/fs/ntfs/attrib.h +++ b/fs/ntfs/attrib.h @@ -111,6 +111,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/file.c b/fs/ntfs/file.c index 31ff35176e..a66b615ddd 100644 --- a/fs/ntfs/file.c +++ b/fs/ntfs/file.c @@ -923,6 +923,33 @@ static int ntfs_zero_range_unkillable(struct inode *inode, loff_t pos, return zw.err; } +/* + * ->iomap_end() punches out the delalloc clusters of a failed write that no + * dirty folio covers, and keeps those that do, such as a cluster that + * ->iomap_begin() zeroed in part. The rollback then drops such a folio along + * with the size, and its cluster, wholly past the new size, would stay + * reserved: writeback never allocates it. __ntfs_attr_truncate_vfs() + * releases what lies past @keep, so punch out the clusters of [@pos, @end) + * between the new size and @keep, where a FALLOC_FL_KEEP_SIZE preallocation + * keeps the attribute allocated. The caller holds the invalidate lock and + * the base inode's mrec_lock, and has truncated the page cache to @new_size. + */ +static void ntfs_write_punch_past_eof(struct ntfs_inode *ni, loff_t pos, + loff_t end, loff_t new_size, loff_t keep) +{ + struct ntfs_volume *vol = ni->vol; + loff_t start = max(round_up(new_size, vol->cluster_size), + round_down(pos, vol->cluster_size)); + loff_t stop = min(keep, round_up(end, vol->cluster_size)); + + if (start >= stop) + return; + down_write(&ni->runlist.lock); + ntfs_attr_punch_delalloc(ni, ntfs_bytes_to_cluster(vol, start), + ntfs_bytes_to_cluster(vol, stop - start)); + up_write(&ni->runlist.lock); +} + /* * Both sizes were moved to the end of the write before the copy, so a short * write, like a failed one, must take back what it did not write. @written @@ -1023,6 +1050,8 @@ static void ntfs_write_rollback(struct kiocb *iocb, ssize_t written, loff_t keep = max(new_data_size, old_alloc_size); + ntfs_write_punch_past_eof(ni, pos, end, new_data_size, + keep); if (ni->allocated_size > keep) data_err = __ntfs_attr_truncate_vfs(ni, keep, keep); diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 8124c5f627..dbf1ee9e4a 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -377,6 +377,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) { @@ -548,7 +552,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; } } @@ -767,6 +777,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) @@ -774,6 +839,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