ext4_resize_fs() reads block n_blocks_count - 1 to check that the device is large enough before it checks whether the request would shrink the file system. EXT4_IOC_RESIZE_FS passes the user supplied 64-bit value straight through, so n_blocks_count == 0 underflows to reading block ~0ULL. That read fails (the block layer also logs an "out-of-range block" error), and the ioctl returns -ENOSPC with a misleading "can't read last block" warning instead of the -EINVAL every other shrink request gets. Reject requests smaller than the current block count before probing the device. Since a mounted file system never has zero blocks, this also guarantees n_blocks_count - 1 cannot underflow. The check after the retry label is kept, as the retry path can lower n_blocks_count. Fixes: 59e31c156a24 ("ext4: fix online resizing when the # of block groups is constant") Signed-off-by: Theodore Ts'o --- fs/ext4/resize-test.c | 2 ++ fs/ext4/resize.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/fs/ext4/resize-test.c b/fs/ext4/resize-test.c index 36b7f0b8167f..1a4c1963648f 100644 --- a/fs/ext4/resize-test.c +++ b/fs/ext4/resize-test.c @@ -1300,6 +1300,8 @@ static void test_resize_fs_across_resize_inode_to_meta_bg(struct kunit *test) -ENOSPC); KUNIT_EXPECT_EQ(test, ext4_resize_fs(sb, o_blks - 100), -EINVAL); KUNIT_EXPECT_EQ(test, ext4_resize_fs(sb, o_blks), 0); + /* n_blocks_count == 0 is a shrink, not a read of block ~0ULL */ + KUNIT_EXPECT_EQ(test, ext4_resize_fs(sb, 0), -EINVAL); /* 2. Simultaneous resize_inode + meta_bg -> -EINVAL */ ext4_set_feature_meta_bg(sb); diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c index dc09fcc91c38..11c0b3659d08 100644 --- a/fs/ext4/resize.c +++ b/fs/ext4/resize.c @@ -2045,6 +2045,15 @@ int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count) int meta_bg; unsigned int flexbg_size = ext4_flex_bg_size(sbi); + /* + * Reject shrinking before probing the device; this also keeps + * n_blocks_count - 1 below from underflowing for n_blocks_count == 0. + */ + if (n_blocks_count < ext4_blocks_count(es)) { + ext4_warning(sb, "can't shrink FS - resize aborted"); + return -EINVAL; + } + /* See if the device is actually as big as what was requested */ bh = ext4_sb_bread(sb, n_blocks_count - 1, 0); if (IS_ERR(bh)) { -- 2.56.1.708.g77570a6a1d-goog