ext4: ignore i_size_high for directories
Theodore Ts'o
maintainerRFC PATCH 1/1 ext4: ignore i_size_high for directories
Directories are not allowed to be bigger than 2TB, so don't use i_size_high when reading the inode. E2fsck should complain about these inodes, but the simplest thing to do for the kernel is simply to not consult i_size_high for directory inodes.
This prevents an intentially corrupted filesystem from causing the kernel to burn a huge amount of CPU and issuing error messages such as:
EXT4-fs warning (device loop0): ext4_block_to_path: block 135090028 > max
Thanks to David Maciejak from Fortinet's FortiGuard Global Security Research Team for reporting this issue.
http://bugzilla.kernel.org/show_bug.cgi?id=12375
--- fs/ext4/ext4.h | 7 +++++-- fs/ext4/inode.c | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-)
fs/ext4/ext4.h +5 -2
@@ -1206,8 +1206,11 @@ static inline void ext4_r_blocks_count_set(struct ext4_super_block *es,static inline loff_t ext4_isize (struct ext4_inode *raw_inode) { -return ((loff_t )le32_to_cpu (raw_inode->i_size_high) <<32 ) | -le32_to_cpu (raw_inode->i_size_lo); +if (S_ISDIR (le16_to_cpu (raw_inode->i_mode))) +return (loff_t )le32_to_cpu (raw_inode->i_size_lo); +else +return ((loff_t )le32_to_cpu (raw_inode->i_size_high) <<32 ) | +le32_to_cpu (raw_inode->i_size_lo); }static inline void ext4_isize_set (struct ext4_inode *raw_inode,loff_t i_size)
fs/ext4/inode.c +2 -2
@@ -360,9 +360,9 @@ static int ext4_block_to_path(struct inode *inode, final = ptrs; }else {ext4_warning (inode->i_sb,"ext4_block_to_path" , -"block %lu > max" , +"block %lu > max in inode %lu" , i_block + direct_blocks + - indirect_blocks + double_blocks); + indirect_blocks + double_blocks, inode->i_ino); }if (boundary) *boundary = final -1 - (i_block & (ptrs -1 ));
Signature · 1 line
1.6.0.4.8.g36f27.dirty
Quoted 1 line · Theodore Ts'o
Directories are not allowed to be bigger than 2TB, so don't use
... bigger than 2GB?
Quoted 3 lines
i_size_highwhen reading the inode. E2fsck should complain about these inodes, but the simplest thing to do for the kernel is simply to not consulti_size_highfor directory inodes.
Actually, it would be preferable to allow directories to grow beyond the 2GB limit. 2GB only allows about 30M files. While the htree code is currently limited to only 2 levels deep, if you have 8kB+ block size it is possible to have directories larger than 2GB with only 2 levels of htree.
fs/ext4/ext4.h static inline void ext4_r_blocks_count_set(struct ext4_super_block *es, · line 1213
Quoted 7 lines
static inline
loff_text4_isize(struct ext4_inode *raw_inode){fs/ext4/ext4.h +5
-0+if (S_ISDIR (le16_to_cpu (raw_inode->i_mode))) +return (loff_t )le32_to_cpu (raw_inode->i_size_lo); +else +return ((loff_t )le32_to_cpu (raw_inode->i_size_high) <<32 ) | +le32_to_cpu (raw_inode->i_size_lo);
If you are going to limit this it should be "if (!S_ISREG(...))" instead of "if (S_ISDIR(...))" because none of the special files should use i_size_high - e2fsck will clear a special inode if it has a non-zero size.
Cheers, Andreas
--
Andreas Dilger
Sr. Staff Engineer, Lustre Group
Sun Microsystems of Canada, Inc.
Theodore Tso
maintainerQuoted 6 lines · Andreas Dilger
Actually, it would be preferable to allow directories to grow beyond the 2GB limit. 2GB only allows about 30M files. While the htree code is currently limited to only 2 levels deep, if you have 8kB+ block size it is possible to have directories larger than 2GB with only 2 levels of htree.
At some point, maybe, although e2fsprogs doesn't have any support for directories > 2GB, and I strongly suspect there are other places in the kernel where we assume directories are < 2GB. So this is something that I'd prefer we implement with a feature flag, explicitly, probably after we get a better b-tree implementation into ext4 (say, like the one you gave me a few months ago, if one of us actually has time to try to get it integrated into the kernel and into e2fsprogs).
Quoted 4 lines
Quoted 4 lines
static inline
loff_text4_isize(struct ext4_inode *raw_inode){fs/ext4/ext4.h +2
-0+if (S_ISDIR (le16_to_cpu (raw_inode->i_mode))) +return (loff_t )le32_to_cpu (raw_inode->i_size_lo);
Quoted 8 lines
fs/ext4/ext4.h static inline void ext4_r_blocks_count_set(struct ext4_super_block *es, · line 1213
Quoted 3 lines
fs/ext4/ext4.h +3
-0+else +return ((loff_t )le32_to_cpu (raw_inode->i_size_high) <<32 ) | +le32_to_cpu (raw_inode->i_size_lo);If you are going to limit this it should be "if (!S_ISREG(...))" instead of "if (
S_ISDIR(...))" because none of the special files should usei_size_high- e2fsck will clear a special inode if it has a non-zero size.
Hmm, yes, that's probably a better check; although for most special inodes we ignore i_size so having an absurd i_size is harmless. Looking at things, we should probably add a check to make sure that i_size for symlinks is sane.
- Ted