Skip to content

Commit a099b09

Browse files
coolgwjankara
authored andcommitted
ext2: Handle fiemap on empty files to prevent EINVAL
Previously, ext2_fiemap would unconditionally apply "len = min_t(u64, len, i_size_read(inode));", When inode->i_size was 0 (for an empty file), this would reduce the requested len to 0. Passing len = 0 to iomap_fiemap could then result in an -EINVAL error, even for valid queries on empty files. Link: linux-test-project/ltp#1246 Signed-off-by: Wei Gao <[email protected]> Signed-off-by: Jan Kara <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 27605c8 commit a099b09

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

fs/ext2/inode.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,19 @@ int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
895895
u64 start, u64 len)
896896
{
897897
int ret;
898+
loff_t i_size;
898899

899900
inode_lock(inode);
900-
len = min_t(u64, len, i_size_read(inode));
901+
i_size = i_size_read(inode);
902+
/*
903+
* iomap_fiemap() returns EINVAL for 0 length. Make sure we don't trim
904+
* length to 0 but still trim the range as much as possible since
905+
* ext2_get_blocks() iterates unmapped space block by block which is
906+
* slow.
907+
*/
908+
if (i_size == 0)
909+
i_size = 1;
910+
len = min_t(u64, len, i_size);
901911
ret = iomap_fiemap(inode, fieinfo, start, len, &ext2_iomap_ops);
902912
inode_unlock(inode);
903913

0 commit comments

Comments
 (0)