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=1796409636; b=s6is1oRqwFDP7rPfimw9FnBdpc22uf1HtKEqo78540auhRHORMh2d45o+oaL1xsZ3ZnI d11MQ+W96OX6DZc85he6kGoEzqSswIfDY4QwOg/cjUQvE9lr/KGoC6kTzaMH+uPym/4G9 SsTfSRFeEXFLAsgg98kmf+luQZi/DfqArfB4fmdV7yj4tzvprP68K0FhNm0DUhgkp5drU YUKTY2UY4Hyvc6S2zPdYNK9j8r/K7pV7QvN21TjO07xYDSZjVH8I2ZhInsTRCgL1NO3F2 DvJcZnZmslu6FTKZ3KLeDfSQMTvCv7PMnXcXjYF9DDIoCK/J04qCV0i/LJpcJiZCHLA== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796409636; 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=5oeLgkeUnpxmIweBbhTYeyC1EE0x1yLlqj1MEQ/Ktos=; b=IkC62nnY7K+Ooc9TOcxFacYDwyUuKZd0VCV0zgEfRWHNS04NImoYUQ/XhqWEvoYpLb79 SUivcSZqrsDsxvcnlJjoUmbxxuRDaYLXt18eTtZj3r4ldzqil60vvNH19pWvcTFpo1Lzy cqOGQjHRwHaJ7kmXJVApgoUUiQEDU/TlKUsci3km4FIPS8QgNX4ieDojFuYuo2lkDI04s 3Cg6rY7ReYtKY2h9SUJQfG80Q+vPkAb8AWI1R5s3ceQJMGpkOhibuqO0kuiEwNttQ0ogl Di8x3SrMi42xiO8DpzPeZmtIobyZ0jfvItoQB3w1myl1DiB8OrKzZrutP/0lP7j8CsQ== 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=FvkhsZMy; 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 7A4DD606A32C; Fri, 4 Dec 2026 18:40:36 +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=FvkhsZMy 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=1796409636; b=NZUm+gm2+KW5S2FCYZWIL0LRx/HyGzg00RpT2drhdASI2OVVPso0oBpJrRlKkYryshaN F05474Cl3G2WP1U0mxlf16Otd96wbRgtT4N8l6U7qPb9vLzAVAorm/2/ywlID4eoY0MQJ 0Yyud1CRLU3lZFIhIbO2DiX/Kq/QwfBK9qnNL1LVOA3OAc2IxCdB1yJeUQxmTZz+Ob2bf meRLVAiCLGFhT2r76hCaa61w1keldkHeoJjL496CV0jfBWEeOgZKNsK76AqY7gs6ErBQe 0zwJtmgEG0kbw+iDFJecCuieO7MjMzF/ts0yyRcIEEJkhig0m/O43R8o7vzho4b2M5A== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1796409636; 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=5oeLgkeUnpxmIweBbhTYeyC1EE0x1yLlqj1MEQ/Ktos=; b=0UBQ9CI3auZ1+FpxVBYcRVIBFsPBLSZwhD5SYCw2d0GRMVhXJHrUH8LMowqLiLE0ygPP qIKMSEW7SN02sH3Ay3v29ivkJc7NeFq5QmlQr6UFNswDOIyUQbu6+Pc0rXHwYkNWkvuPO 0l/xmRyHJtXPsGlIgveF1vD/S9jcli9BlLSCjqmDuaAZftRc+aLEXvGy5B1cVc7+4ZOmh k5ExLkwaVj9ZnzWnZQ10Gw8FE5+Vfo0SSGEaRCvTTUtaQeHpI4brd+gEGp4GUno6aYzLa GA8PwI9l6+ieKWJOfd0PoXwg0RD2Tiz/HAxp6izMyaxt2nL7PdV0OZ3LuqiDU2KLltA== 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 627B21F00A06; Fri, 4 Dec 2026 18:40:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796409636; bh=5oeLgkeUnpxmIweBbhTYeyC1EE0x1yLlqj1MEQ/Ktos=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=FvkhsZMyJIIpYJMDVNAyghVMe8DVmqf9uOPAi6uWtsydlbTmu0WXqzlpsk45tIm5c tBsGyJLQiAumjnWz92VIK8tixegywjOwrAAC7d634iqTXE8lOXboHLPzgFbaxbWVsg 0OlKuMATnkqLS1AlGFH4ZMGfO2n5sBjHBq7ukAXWVlMdeWpaspQGGyX76pXQsRB3af ccQrfSDxp3FlPfHHEQ1h0Py5m4U0IYRSpTTht9KQyeV2RFVUzJG6VjcGhjk/aPaKap 88d9FNU1kzVWQ3zK8Fv7RcV9nuhq/ofbnP1pXg7lhQitChjlfC0vVp3d5EJCWZ3I8G v0ehH/d9RNfcA== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id E2C264DC2A; Fri, 4 Dec 2026 18:40:34 +0000 (UTC) From: Ernest Borowski via B4 Relay Date: Fri, 04 Dec 2026 19:40:32 +0100 Subject: [PATCH v5 01/13] iomap: punch out a run of clean blocks in one call 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-1-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=5633; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=v4ficqDQIkPqdi1vSXl/PmPVv0rIAHOOfXAUulKkAgU=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmxhTnlZFhHl1P/PkxZcZvxT7u4mXsAvdv35k/W/nLUFJ NTL9m7pKGVhEONikBVTZOn4fyPEJ+Tbp2VxWn0wc1iZQIYwcHEKwERSAxgZ9i52W9csqHpDrUUj r8jx1U6+wDrLCQKb63ukLmy22VR2npFh0oFn3xX/bMxibXCrL6sy5rt7Pa597zvfhwde3+M7tuY NDwA= 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 iomap_write_delalloc_ifs_punch() calls ->punch() once for every clean block of a dirty folio. A file system that reserves delayed allocation space in units larger than a block can release a unit only when one call covers it whole. ntfs reserves whole clusters and uses 512 byte blocks, so with 1 KiB clusters a buffered write into a hole that fails after zeroing the first cluster of a 4 KiB folio leaves the other three clusters of that folio reserved, and writeback later allocates them. Pass each range of contiguous clean blocks to ->punch() in one call. The clean blocks at the start of a dirty folio join the range that ends there, and the clean blocks at the end of the part of the folio being scanned are left to the next punch, which also covers what follows them: the next folio, the gap to the next cached data, or the range past the end of the scan. Otherwise a clean 1 KiB cluster that straddles the end of the scan, which stops at the block that holds EOF, still goes to ->punch() in two parts and stays reserved. The blocks left over are punched after their folio is unlocked, as the range past the end of the scan always was. XFS and ext4 punch any range they are given, so for them this only means fewer calls. "ntfs: punch out the delalloc reservation of a failed buffered write" later in this series needs this, so it goes to stable with it. Cc: stable@vger.kernel.org Signed-off-by: Ernest Borowski --- fs/iomap/buffered-io.c | 82 +++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index 25915c0f5e..2b9bcff7ce 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -1350,58 +1350,58 @@ int iomap_fsverity_write(struct file *file, loff_t pos, size_t length, } EXPORT_SYMBOL_GPL(iomap_fsverity_write); -static void iomap_write_delalloc_ifs_punch(struct inode *inode, - struct folio *folio, loff_t start_byte, loff_t end_byte, - struct iomap *iomap, iomap_punch_t punch) -{ - unsigned int first_blk, last_blk; - loff_t last_byte; - u8 blkbits = inode->i_blkbits; - struct iomap_folio_state *ifs; - - /* - * When we have per-block dirty tracking, there can be - * blocks within a folio which are marked uptodate - * but not dirty. In that case it is necessary to punch - * out such blocks to avoid leaking any delalloc blocks. - */ - ifs = folio->private; - if (!ifs) - return; - - last_byte = min_t(loff_t, end_byte - 1, folio_next_pos(folio) - 1); - first_blk = offset_in_folio(folio, start_byte) >> blkbits; - last_blk = offset_in_folio(folio, last_byte) >> blkbits; - while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk)) - <= last_blk) { - punch(inode, folio_pos(folio) + (first_blk << blkbits), - 1 << blkbits, iomap); - first_blk++; - } -} - static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio, loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte, struct iomap *iomap, iomap_punch_t punch) { + loff_t range_end = min_t(loff_t, end_byte, folio_next_pos(folio)); + unsigned int first_blk, last_blk, blk; + u8 blkbits = inode->i_blkbits; + if (!folio_test_dirty(folio)) return; - /* if dirty, punch up to offset */ - if (start_byte > *punch_start_byte) { - punch(inode, *punch_start_byte, start_byte - *punch_start_byte, - iomap); + /* Without per-block state the whole folio is dirty. */ + if (!folio->private) { + /* if dirty, punch up to offset */ + if (start_byte > *punch_start_byte) + punch(inode, *punch_start_byte, + start_byte - *punch_start_byte, iomap); + /* + * Make sure the next punch start is correctly bound to + * the end of this data range, not the end of the folio. + */ + *punch_start_byte = range_end; + return; } - /* Punch non-dirty blocks within folio */ - iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte, - iomap, punch); - /* - * Make sure the next punch start is correctly bound to - * the end of this data range, not the end of the folio. + * When we have per-block dirty tracking, there can be blocks within a + * folio which are marked uptodate but not dirty. In that case it is + * necessary to punch out such blocks to avoid leaking any delalloc + * blocks. + * + * A file system that reserves space in units larger than a block can + * only release a unit that one ->punch() call covers whole, so pass + * every clean range in one call: the clean blocks before the first + * dirty block join the range that ends at start_byte, and clean blocks + * at the end of the range are not punched here but left to the next + * punch, which also covers what follows them. The next punch start + * stays bound to the end of this data range, not the end of the folio. */ - *punch_start_byte = min_t(loff_t, end_byte, folio_next_pos(folio)); + first_blk = offset_in_folio(folio, start_byte) >> blkbits; + last_blk = offset_in_folio(folio, range_end - 1) >> blkbits; + while ((blk = ifs_next_dirty_block(folio, first_blk, last_blk)) <= + last_blk) { + loff_t pos = folio_pos(folio) + ((loff_t)blk << blkbits); + + if (pos > *punch_start_byte) + punch(inode, *punch_start_byte, + pos - *punch_start_byte, iomap); + first_blk = ifs_next_clean_block(folio, blk + 1, last_blk); + *punch_start_byte = min_t(loff_t, range_end, folio_pos(folio) + + ((loff_t)first_blk << blkbits)); + } } /* -- 2.56.1