[PATCH v5 4/5] hfsplus: return -EIO from hfsplus_map_extent() if no extent covers the block
From: Viacheslav Dubeyko
Date: Tue Nov 17 2026 - 19:49:35 EST
hfsplus_ext_find_block() walks one 8-entry extent record and returns
the number of contiguous allocation blocks starting at the requested
offset. If the offset lies beyond all entries of the record, it sets
*dblock to 0 and returns 0. hfsplus_map_extent() ignores this and
returns success, so the caller gets allocation block 0 as the mapping.
This can only happen on a corrupted volume, for example when the
extents overflow b-tree lacks a record for a range that is below
alloc_blocks, or when the found record does not cover the requested
block. THe hfsplus_get_block() then maps the file block to allocation
block 0, which holds the boot blocks and the volume header. A read
returns that metadata as file data, and writeback of a dirty page
of such a file overwrites the volume header.
Return -EIO from hfsplus_map_extent() when hfsplus_ext_find_block()
finds no extent, for both the fork's first extents and the cached
extent record. The iomap conversion later in this series relies on
this too: __hfsplus_iomap_begin() derives the mapping length from
max_blocks, and a zero max_blocks would produce an invalid zero-length
mapping instead of an error.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
cc: John Paul Adrian Glaubitz <glaubitz@xxxxxxxxxxxxxxxxxxx>
cc: Yangtao Li <frank.li@xxxxxxxx>
cc: linux-fsdevel@xxxxxxxxxxxxxxx
Signed-off-by: Viacheslav Dubeyko <slava@xxxxxxxxxxx>
---
fs/hfsplus/extents.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/fs/hfsplus/extents.c b/fs/hfsplus/extents.c
index cdb337ba3924..bb5a1508498d 100644
--- a/fs/hfsplus/extents.c
+++ b/fs/hfsplus/extents.c
@@ -67,7 +67,7 @@ static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off,
}
off -= count;
}
- /* panic? */
+ /* 'off' lies beyond this extent record; callers treat 0 as an error */
*dblock = 0;
return 0;
}
@@ -273,6 +273,8 @@ int hfsplus_map_extent(struct inode *inode, u32 ablock, int create,
*max_blocks = hfsplus_ext_find_block(hip->first_extents,
ablock,
dblock);
+ if (!*max_blocks)
+ return -EIO;
return 0;
}
@@ -300,6 +302,9 @@ int hfsplus_map_extent(struct inode *inode, u32 ablock, int create,
if (was_dirty)
mark_inode_dirty(inode);
+ if (!*max_blocks)
+ return -EIO;
+
return 0;
}
--
2.53.0