[PATCH v2 2/2] exfat: don't map EXFAT_EOF_CLUSTER as a physical cluster

From: Youling Tang

Date: Mon Nov 16 2026 - 04:08:17 EST


From: Youling Tang <tangyouling@xxxxxxxxxx>

exfat_map_cluster() reports a hole by returning success with
*clu == EXFAT_EOF_CLUSTER for !create. __exfat_iomap_begin() does not
check that sentinel and converts it into a physical address far beyond
the device. A writeback that races with the i_size/i_blocks window in
exfat_cont_expand() can hit this: the writeback submits a bio past the
end of the device, and fsync() or msync() returns -EIO. If the
resulting iomap length is zero, iomap_add_to_ioend() can loop on a
zero-length bio and hang a concurrent truncate.

When a range starts before the end of the cluster chain but extends past
it, exfat_map_cluster() returns the sentinel for the whole range. Make
it map the valid prefix in that case, and return the sentinel only if
the whole range is beyond the chain:

offset chain end offset+len
|--- prefix ----|--- tail ---|
|== allocated ==|
MAPPED HOLE

Reporting the whole straddling range as a hole instead would skip
valid dirty data.

Report the remaining range as a hole instead of turning the sentinel
into an address, matching the old exfat_get_block() behavior.

Fixes: 82a81a7352bc ("exfat: add iomap buffered I/O support")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Youling Tang <tangyouling@xxxxxxxxxx>
---
v2: move the clamp into exfat_map_cluster().

fs/exfat/inode.c | 15 ++++++++++++---
fs/exfat/iomap.c | 12 ++++++++++++
2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/fs/exfat/inode.c b/fs/exfat/inode.c
index c1d15d4fc5e7..b175d2f3e459 100644
--- a/fs/exfat/inode.c
+++ b/fs/exfat/inode.c
@@ -134,6 +134,9 @@ void exfat_sync_inode(struct inode *inode)
* Input: inode, (logical) clu_offset, target allocation area
* Output: errcode, cluster number
* *clu = (~0), if it's unable to allocate a new cluster
+ *
+ * For !create, *count is limited to the part of the range backed by the
+ * cluster chain; the caller reports the remainder as a hole.
*/
int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
unsigned int *clu, unsigned int *count, int create,
@@ -153,9 +156,15 @@ int exfat_map_cluster(struct inode *inode, unsigned int clu_offset,
*count > num_clusters - clu_offset)
num_to_be_allocated = clu_offset + *count - num_clusters;

- if (!create && (num_to_be_allocated > 0)) {
- *clu = EXFAT_EOF_CLUSTER;
- return 0;
+ if (!create && num_to_be_allocated > 0) {
+ if (clu_offset >= num_clusters) {
+ *clu = EXFAT_EOF_CLUSTER;
+ return 0;
+ }
+
+ /* Range straddles the chain end: map the valid prefix only. */
+ *count = num_clusters - clu_offset;
+ num_to_be_allocated = 0;
}

*clu = last_clu = ei->start_clu;
diff --git a/fs/exfat/iomap.c b/fs/exfat/iomap.c
index 11823591b3a8..ed17b367f820 100644
--- a/fs/exfat/iomap.c
+++ b/fs/exfat/iomap.c
@@ -90,6 +90,18 @@ static int __exfat_iomap_begin(struct inode *inode, loff_t offset, loff_t length
if (err)
goto out;

+ /*
+ * exfat_map_cluster() can return EXFAT_EOF_CLUSTER when racing with
+ * exfat_cont_expand(). Report it as a hole.
+ */
+ if (!may_alloc && cluster == EXFAT_EOF_CLUSTER) {
+ iomap->type = IOMAP_HOLE;
+ iomap->addr = IOMAP_NULL_ADDR;
+ iomap->offset = offset;
+ iomap->length = length;
+ goto out;
+ }
+
cluster_offset = exfat_cluster_offset(sbi, offset);
cluster_length = exfat_cluster_to_bytes(sbi, num_clusters);

--
2.43.0