From: Ernest Borowski Date: Tue, 01 Dec 2026 18:42:37 +0100 Subject: [PATCH v4 02/11] iomap: add a helper to mark a range of a folio dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261201-b4-ntfs-delalloc-fixes-v4-2-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=3685; i=ernest.borowski.git@proton.me; s=default; h=from:subject:message-id; bh=kVEf2BHXyJlkjr75kU2M/ab5XEp5CAH6JK3ScQM9xjg=; b=owGbwMvMwCVWzJ65uCpJ5D/jabUkhmx+dm6re8d5H2rHXJFrOlXIERr+MFzuqL+VbcwDBX7H7 6debozoKGVhEONikBVTZOn4fyPEJ+Tbp2VxWn0wc1iZQIYwcHEKwERuz2b4n3z+QofynlvH7PvC L5d81XLrD1V2DtmkmV+QIXN217bmGkaG1+lcLVFp6pJ39Iz0ek7OSwtRbdsvI3RtX2OOm6axczM bAA== 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 List-Id: B4 Relay Submissions A filesystem that allocates in units larger than a block may need writeback to write blocks that a buffered write did not copy into. ntfs reserves whole clusters for delayed allocation, and after a short copy into a cluster that was a hole, writeback must also write the rest of that cluster, or the cluster shows its old contents on the disk there. folio_mark_dirty() marks every block of the folio dirty, so the delalloc release that follows no longer finds the clean blocks of the clusters the write did not reach and keeps them reserved: with 1 KiB and 2 KiB clusters, writeback then allocates a cluster that holds no data. Add iomap_dirty_folio_range(), which marks the folio and only the blocks covering a byte range dirty. It and iomap_dirty_folio() share one internal helper; iomap_dirty_folio() keeps passing the mapping it is given, as ->dirty_folio may be called without the folio lock. "ntfs: zero the rest of a new cluster after a short 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 | 32 ++++++++++++++++++++++++++------ include/linux/iomap.h | 1 + 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/fs/iomap/buffered-io.c b/fs/iomap/buffered-io.c index b6dd56f1bd..6fb783808f 100644 --- a/fs/iomap/buffered-io.c +++ b/fs/iomap/buffered-io.c @@ -847,15 +847,35 @@ void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len) } EXPORT_SYMBOL_GPL(iomap_invalidate_folio); -bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio) +static bool __iomap_dirty_folio_range(struct address_space *mapping, + struct folio *folio, size_t off, size_t len) { - struct inode *inode = mapping->host; - size_t len = folio_size(folio); - - ifs_alloc(inode, folio, 0); - iomap_set_range_dirty(folio, 0, len); + ifs_alloc(mapping->host, folio, 0); + iomap_set_range_dirty(folio, off, len); return filemap_dirty_folio(mapping, folio); } + +/** + * iomap_dirty_folio_range - mark a range of a folio dirty + * @folio: the locked, uptodate folio + * @off: byte offset of the range in @folio + * @len: length of the range in bytes + * + * Mark the blocks that cover [@off, @off + @len) and @folio dirty, but none + * of the other blocks of @folio, so that writeback writes only those blocks. + * + * Return: true if @folio was clean before, as filemap_dirty_folio() does. + */ +bool iomap_dirty_folio_range(struct folio *folio, size_t off, size_t len) +{ + return __iomap_dirty_folio_range(folio->mapping, folio, off, len); +} +EXPORT_SYMBOL_GPL(iomap_dirty_folio_range); + +bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio) +{ + return __iomap_dirty_folio_range(mapping, folio, 0, folio_size(folio)); +} EXPORT_SYMBOL_GPL(iomap_dirty_folio); static void diff --git a/include/linux/iomap.h b/include/linux/iomap.h index 78a1b7603a..f80da4d8ff 100644 --- a/include/linux/iomap.h +++ b/include/linux/iomap.h @@ -445,6 +445,7 @@ struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len); bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags); void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len); bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio); +bool iomap_dirty_folio_range(struct folio *folio, size_t off, size_t len); void iomap_folio_mark_uptodate(struct folio *folio); int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len, iomap_iter_next_fn next, -- 2.56.1