ext4: fix a inode checksum error
Zhang Yi
reviewerv4 cover ext4: fix a inode checksum error
We find a checksum error and a inode corruption problem while doing stress test, this 6 patches address to fix them. The first patch is relate to the error simulation, and the 2-5 patches are prepare to do the fix. The last patch fix these two issue.
- Checksum error
EXT4-fs error (device sda): ext4_lookup:1784: inode #131074: comm cat: iget: checksum invalid
- Inode corruption
e2fsck 1.46.0 (29-Jan-2020)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Entry 'foo' in / (2) has deleted/unused inode 17. Clear<y>? yes Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Inode bitmap differences: -17
Fix<y>? yes
Free inodes count wrong for group #0 (32750, counted=32751). Fix<y>? yes
Free inodes count wrong (32750, counted=32751). Fix<y>? yes
Changes since v3:
- Postpone initialization to ext4_do_update_inode() may cause zeroout newly set xattr entry. So switch to do initialization in __ext4_get_inode_loc().
Changes since v2:
- Instead of using WARN_ON_ONCE to prevent ext4_do_update_inode() return before filling the inode buffer, keep the error and postpone the report after the updating in the third patch.
- Fix some language mistacks in the last patch.
Changes since v1:
- Add a patch to prevent ext4_do_update_inode() return before filling the inode buffer.
- Do not use BH_New flag to indicate the empty buffer, postpone the zero and uptodate logic into ext4_do_update_inode() before filling the inode buffer.
Thanks,
Yi.
Zhang Yi (6):
ext4: move inode eio simulation behind io completeion
ext4: remove an unnecessary if statement in __ext4_get_inode_loc()
ext4: make the updating inode data procedure atomic
ext4: factor out ext4_fill_raw_inode()
ext4: move ext4_fill_raw_inode() related functions
ext4: prevent getting empty inode buffer
fs/ext4/inode.c | 490 +++++++++++++++++++++++++----------------------- 1 file changed, 257 insertions(+), 233 deletions(-)
Signature · 1 line
2.31.1
Zhang Yi
reviewerPATCH v4 1/6 ext4: move inode eio simulation behind io completeion
No EIO simulation is required if the buffer is uptodate, so move the simulation behind read bio completeion just like inode/block bitmap simulation does.
--- fs/ext4/inode.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
fs/ext4/inode.c +1 -3
@@ -4330,8 +4330,6 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, bh =sb_getblk (sb, block);if (unlikely (!bh))return -ENOMEM ; -if (ext4_simulate_fail (sb,EXT4_SIM_INODE_EIO )) - goto simulate_eio;if (!buffer_uptodate (bh)) {lock_buffer (bh);
@@ -4418,8 +4416,8 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,ext4_read_bh_nowait (bh,REQ_META |REQ_PRIO ,NULL );blk_finish_plug (&plug);wait_on_buffer (bh); +ext4_simulate_fail_bh (sb, bh,EXT4_SIM_INODE_EIO );if (!buffer_uptodate (bh)) { - simulate_eio:if (ret_block) *ret_block = block;brelse (bh);
Signature · 1 line
2.31.1
Re: [PATCH v4 1/6] ext4: move inode eio simulation behind io completeion
Quoted 6 lines · Zhang Yi
No EIO simulation is required if the buffer is uptodate, so move the simulation behind read bio completeion just like inode/block bitmap simulation does.
Signed-off-by Zhang Yi <[email protected]>Reviewed-by Jan Kara <[email protected]>
Thanks, applied.
- Ted
Zhang Yi
reviewerPATCH v4 2/6 ext4: remove an unnecessary if statement in __ext4_get_inode_loc()
In reply to Zhang YiThe "if (!buffer_uptodate(bh))" hunk covered almost the whole code after getting buffer in __ext4_get_inode_loc() which seems unnecessary, remove it and switch to check ext4_buffer_uptodate(), it simplify code and make it more readable.
--- fs/ext4/inode.c | 162 +++++++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 84 deletions(-)
fs/ext4/inode.c +78 -84
@@ -4330,99 +4330,93 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, bh =sb_getblk (sb, block);if (unlikely (!bh))return -ENOMEM ; -if (!buffer_uptodate (bh)) { -lock_buffer (bh); - -if (ext4_buffer_uptodate (bh)) { -/* someone brought it uptodate while we waited */ -unlock_buffer (bh); - goto has_buffer; - } +if (ext4_buffer_uptodate (bh)) + goto has_buffer; -/* -* If we have all information of the inode in memory and this -* is the only valid inode in the block, we need not read the -* block. -*/ -if (in_mem) { -struct buffer_head *bitmap_bh; -int i, start; +lock_buffer (bh); +/* +* If we have all information of the inode in memory and this +* is the only valid inode in the block, we need not read the +* block. +*/ +if (in_mem) { +struct buffer_head *bitmap_bh; +int i, start; - start = inode_offset & ~(inodes_per_block -1 ); + start = inode_offset & ~(inodes_per_block -1 ); -/* Is the inode bitmap in cache? */ - bitmap_bh =sb_getblk (sb,ext4_inode_bitmap (sb, gdp)); -if (unlikely (!bitmap_bh)) - goto make_io; +/* Is the inode bitmap in cache? */ + bitmap_bh =sb_getblk (sb,ext4_inode_bitmap (sb, gdp)); +if (unlikely (!bitmap_bh)) + goto make_io; -/* -* If the inode bitmap isn't in cache then the -* optimisation may end up performing two reads instead -* of one, so skip it. -*/ -if (!buffer_uptodate (bitmap_bh)) { -brelse (bitmap_bh); - goto make_io; - } -for (i = start; i < start + inodes_per_block; i++) { -if (i == inode_offset) -continue ; -if (ext4_test_bit (i, bitmap_bh->b_data)) -break ; - } +/* +* If the inode bitmap isn't in cache then the +* optimisation may end up performing two reads instead +* of one, so skip it. +*/ +if (!buffer_uptodate (bitmap_bh)) {brelse (bitmap_bh); -if (i == start + inodes_per_block) { -/* all other inodes are free, so skip I/O */ -memset (bh->b_data,0 , bh->b_size); -set_buffer_uptodate (bh); -unlock_buffer (bh); - goto has_buffer; - } + goto make_io; + } +for (i = start; i < start + inodes_per_block; i++) { +if (i == inode_offset) +continue ; +if (ext4_test_bit (i, bitmap_bh->b_data)) +break ; } +brelse (bitmap_bh); +if (i == start + inodes_per_block) { +/* all other inodes are free, so skip I/O */ +memset (bh->b_data,0 , bh->b_size); +set_buffer_uptodate (bh); +unlock_buffer (bh); + goto has_buffer; + } + } make_io: -/* -* If we need to do any I/O, try to pre-readahead extra -* blocks from the inode table. -*/ -blk_start_plug (&plug); -if (EXT4_SB (sb)->s_inode_readahead_blks) { -ext4_fsblk_t b, end, table; -unsigned num; -__u32 ra_blks =EXT4_SB (sb)->s_inode_readahead_blks; - - table =ext4_inode_table (sb, gdp); -/* s_inode_readahead_blks is always a power of 2 */ - b = block & ~((ext4_fsblk_t ) ra_blks -1 ); -if (table > b) - b = table; - end = b + ra_blks; - num =EXT4_INODES_PER_GROUP (sb); -if (ext4_has_group_desc_csum (sb)) - num -=ext4_itable_unused_count (sb, gdp); - table += num / inodes_per_block; -if (end > table) - end = table; -while (b <= end) -ext4_sb_breadahead_unmovable (sb, b++); - } +/* +* If we need to do any I/O, try to pre-readahead extra +* blocks from the inode table. +*/ +blk_start_plug (&plug); +if (EXT4_SB (sb)->s_inode_readahead_blks) { +ext4_fsblk_t b, end, table; +unsigned num; +__u32 ra_blks =EXT4_SB (sb)->s_inode_readahead_blks; + + table =ext4_inode_table (sb, gdp); +/* s_inode_readahead_blks is always a power of 2 */ + b = block & ~((ext4_fsblk_t ) ra_blks -1 ); +if (table > b) + b = table; + end = b + ra_blks; + num =EXT4_INODES_PER_GROUP (sb); +if (ext4_has_group_desc_csum (sb)) + num -=ext4_itable_unused_count (sb, gdp); + table += num / inodes_per_block; +if (end > table) + end = table; +while (b <= end) +ext4_sb_breadahead_unmovable (sb, b++); + } -/* -* There are other valid inodes in the buffer, this inode -* has in-inode xattrs, or we don't have this inode in memory. -* Read the block from disk. -*/ -trace_ext4_load_inode (sb, ino); -ext4_read_bh_nowait (bh,REQ_META |REQ_PRIO ,NULL ); -blk_finish_plug (&plug); -wait_on_buffer (bh); -ext4_simulate_fail_bh (sb, bh,EXT4_SIM_INODE_EIO ); -if (!buffer_uptodate (bh)) { -if (ret_block) - *ret_block = block; -brelse (bh); -return -EIO ; - } +/* +* There are other valid inodes in the buffer, this inode +* has in-inode xattrs, or we don't have this inode in memory. +* Read the block from disk. +*/ +trace_ext4_load_inode (sb, ino); +ext4_read_bh_nowait (bh,REQ_META |REQ_PRIO ,NULL ); +blk_finish_plug (&plug); +wait_on_buffer (bh); +ext4_simulate_fail_bh (sb, bh,EXT4_SIM_INODE_EIO ); +if (!buffer_uptodate (bh)) { +if (ret_block) + *ret_block = block; +brelse (bh); +return -EIO ; } has_buffer: iloc->bh = bh;
Signature · 1 line
2.31.1
Re: [PATCH v4 2/6] ext4: remove an unnecessary if statement in __ext4_get_inode_loc()
Quoted 7 lines · Zhang Yi
The "if (!buffer_uptodate(bh))" hunk covered almost the whole code after getting buffer in
__ext4_get_inode_loc()which seems unnecessary, remove it and switch to checkext4_buffer_uptodate(), it simplify code and make it more readable.Signed-off-by Zhang Yi <[email protected]>Reviewed-by Jan Kara <[email protected]>
Thanks, applied.
- Ted
Zhang Yi
reviewerPATCH v4 3/6 ext4: make the updating inode data procedure atomic
In reply to Zhang YiNow that ext4_do_update_inode() return error before filling the whole inode data if we fail to set inode blocks in ext4_inode_blocks_set(). This error should never happen in theory since sb->s_maxbytes should not have allowed this, we have already init sb->s_maxbytes according to this feature in ext4_fill_super(). So even through that could only happen due to the filesystem corruption, we'd better to return after we finish updating the inode because it may left an uninitialized buffer and we could read this buffer later in "errors=continue" mode.
This patch make the updating inode data procedure atomic, call EXT4_ERROR_INODE() after we dropping i_raw_lock after something bad happened, make sure that the inode is integrated, and also drop a BUG_ON and do some small cleanups.
--- fs/ext4/inode.c | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-)
fs/ext4/inode.c +28 -16
@@ -4920,8 +4920,14 @@ static int ext4_inode_blocks_set(handle_t *handle,ext4_clear_inode_flag (inode,EXT4_INODE_HUGE_FILE );return 0 ; } + +/* +* This should never happen since sb->s_maxbytes should not have +* allowed this, sb->s_maxbytes was set according to the huge_file +* feature in ext4_fill_super(). +*/ if (!ext4_has_feature_huge_file (sb)) -return -EFBIG ; +return -EFSCORRUPTED ;if (i_blocks <=0xffffffffffffULL ) {/*
@@ -5024,16 +5030,14 @@ static int ext4_do_update_inode(handle_t *handle,spin_lock (&ei->i_raw_lock); -/* For fields not tracked in the in-memory inode, -* initialise them to zero for new inodes. */ +/* +* For fields not tracked in the in-memory inode, initialise them +* to zero for new inodes. +*/ if (ext4_test_inode_state (inode,EXT4_STATE_NEW ))memset (raw_inode,0 ,EXT4_SB (inode->i_sb)->s_inode_size); err =ext4_inode_blocks_set (handle, raw_inode, ei); -if (err) { -spin_unlock (&ei->i_raw_lock); - goto out_brelse; - } raw_inode->i_mode =cpu_to_le16 (inode->i_mode); i_uid =i_uid_read (inode);
@@ -5042,10 +5046,11 @@ static int ext4_do_update_inode(handle_t *handle,if (!(test_opt (inode->i_sb,NO_UID32 ))) { raw_inode->i_uid_low =cpu_to_le16 (low_16_bits (i_uid)); raw_inode->i_gid_low =cpu_to_le16 (low_16_bits (i_gid)); -/* -* Fix up interoperability with old kernels. Otherwise, old inodes get -* re-used with the upper 16 bits of the uid/gid intact -*/ +/* +* Fix up interoperability with old kernels. Otherwise, +* old inodes get re-used with the upper 16 bits of the +* uid/gid intact. +*/ if (ei->i_dtime &&list_empty (&ei->i_orphan)) { raw_inode->i_uid_high =0 ; raw_inode->i_gid_high =0 ;
@@ -5114,8 +5119,9 @@ static int ext4_do_update_inode(handle_t *handle, } } -BUG_ON (!ext4_has_feature_project (inode->i_sb) && - i_projid !=EXT4_DEF_PROJID ); +if (i_projid !=EXT4_DEF_PROJID && + !ext4_has_feature_project (inode->i_sb)) + err = err ?: -EFSCORRUPTED ;if (EXT4_INODE_SIZE (inode->i_sb) >EXT4_GOOD_OLD_INODE_SIZE &&EXT4_FITS_IN_INODE (raw_inode, ei, i_projid))
@@ -5123,6 +5129,11 @@ static int ext4_do_update_inode(handle_t *handle,ext4_inode_csum_set (inode, raw_inode, ei);spin_unlock (&ei->i_raw_lock); +if (err) { +EXT4_ERROR_INODE (inode,"corrupted inode contents" ); + goto out_brelse; + } +if (inode->i_sb->s_flags &SB_LAZYTIME )ext4_update_other_inodes_time (inode->i_sb, inode->i_ino, bh->b_data);
@@ -5130,13 +5141,13 @@ static int ext4_do_update_inode(handle_t *handle,BUFFER_TRACE (bh,"call ext4_handle_dirty_metadata" ); err =ext4_handle_dirty_metadata (handle,NULL , bh);if (err) - goto out_brelse; + goto out_error;ext4_clear_inode_state (inode,EXT4_STATE_NEW );if (set_large_file) {BUFFER_TRACE (EXT4_SB (sb)->s_sbh,"get write access" ); err =ext4_journal_get_write_access (handle,EXT4_SB (sb)->s_sbh);if (err) - goto out_brelse; + goto out_error;lock_buffer (EXT4_SB (sb)->s_sbh);ext4_set_feature_large_file (sb);ext4_superblock_csum_set (sb);
@@ -5146,9 +5157,10 @@ static int ext4_do_update_inode(handle_t *handle, EXT4_SB(sb)->s_sbh); }ext4_update_inode_fsync_trans (handle, inode, need_datasync); +out_error: +ext4_std_error (inode->i_sb, err); out_brelse:brelse (bh); -ext4_std_error (inode->i_sb, err);return err; }
Signature · 1 line
2.31.1
Re: [PATCH v4 3/6] ext4: make the updating inode data procedure atomic
Quoted 9 lines · Zhang Yi
Now that
ext4_do_update_inode()return error before filling the whole inode data if we fail to set inode blocks inext4_inode_blocks_set(). This error should never happen in theory sincesb->s_maxbytesshould not have allowed this, we have already initsb->s_maxbytesaccording to this feature inext4_fill_super(). So even through that could only happen due to the filesystem corruption, we'd better to return after we finish updating the inode because it may left an uninitialized buffer and we could read this buffer later in "errors=continue" mode.
Quoted 7 lines
This patch make the updating inode data procedure atomic, call
EXT4_ERROR_INODE()after we droppingi_raw_lockafter something bad happened, make sure that the inode is integrated, and also drop aBUG_ONand do some small cleanups.Signed-off-by Zhang Yi <[email protected]>Reviewed-by Jan Kara <[email protected]>
Thanks, applied.
- Ted
Zhang Yi
reviewerPATCH v4 4/6 ext4: factor out ext4_fill_raw_inode()
In reply to Zhang YiFactor out ext4_fill_raw_inode() from ext4_do_update_inode(), which is use to fill the in-mem inode contents into the inode table buffer, in preparation for initializing the exclusive inode buffer without reading the block in __ext4_get_inode_loc().
--- fs/ext4/inode.c | 85 +++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 38 deletions(-)
fs/ext4/inode.c +47 -38
@@ -4902,9 +4902,8 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,return ERR_PTR (ret); } -static int ext4_inode_blocks_set (handle_t *handle, -struct ext4_inode *raw_inode, -struct ext4_inode_info *ei) +static int ext4_inode_blocks_set (struct ext4_inode *raw_inode, +struct ext4_inode_info *ei) {struct inode *inode = &(ei->vfs_inode);u64 i_blocks =READ_ONCE (inode->i_blocks);
@@ -5007,37 +5006,16 @@ static void ext4_update_other_inodes_time(struct super_block *sb,rcu_read_unlock (); } -/* -* Post the struct inode info into an on-disk inode location in the -* buffer-cache. This gobbles the caller's reference to the -* buffer_head in the inode location struct. -* -* The caller must have write access to iloc->bh. -*/ -static int ext4_do_update_inode (handle_t *handle, -struct inode *inode, -struct ext4_iloc *iloc) +static int ext4_fill_raw_inode (struct inode *inode,struct ext4_inode *raw_inode) { -struct ext4_inode *raw_inode =ext4_raw_inode (iloc);struct ext4_inode_info *ei =EXT4_I (inode); -struct buffer_head *bh = iloc->bh; -struct super_block *sb = inode->i_sb; -int err =0 , block; -int need_datasync =0 , set_large_file =0 ;uid_t i_uid;gid_t i_gid;projid_t i_projid; +int block; +int err; -spin_lock (&ei->i_raw_lock); - -/* -* For fields not tracked in the in-memory inode, initialise them -* to zero for new inodes. -*/ -if (ext4_test_inode_state (inode,EXT4_STATE_NEW )) -memset (raw_inode,0 ,EXT4_SB (inode->i_sb)->s_inode_size); - - err =ext4_inode_blocks_set (handle, raw_inode, ei); + err =ext4_inode_blocks_set (raw_inode, ei); raw_inode->i_mode =cpu_to_le16 (inode->i_mode); i_uid =i_uid_read (inode);
@@ -5079,16 +5057,8 @@ static int ext4_do_update_inode(handle_t *handle, raw_inode->i_file_acl_high =cpu_to_le16 (ei->i_file_acl >>32 ); raw_inode->i_file_acl_lo =cpu_to_le32 (ei->i_file_acl); -if (READ_ONCE (ei->i_disksize) !=ext4_isize (inode->i_sb, raw_inode)) { -ext4_isize_set (raw_inode, ei->i_disksize); - need_datasync =1 ; - } -if (ei->i_disksize >0x7fffffffULL ) { -if (!ext4_has_feature_large_file (sb) || -EXT4_SB (sb)->s_es->s_rev_level == -cpu_to_le32 (EXT4_GOOD_OLD_REV )) - set_large_file =1 ; - } +ext4_isize_set (raw_inode, ei->i_disksize); + raw_inode->i_generation =cpu_to_le32 (inode->i_generation);if (S_ISCHR (inode->i_mode) ||S_ISBLK (inode->i_mode)) {if (old_valid_dev (inode->i_rdev)) {
@@ -5128,6 +5098,45 @@ static int ext4_do_update_inode(handle_t *handle, raw_inode->i_projid =cpu_to_le32 (i_projid);ext4_inode_csum_set (inode, raw_inode, ei); +return err; +} + +/* +* Post the struct inode info into an on-disk inode location in the +* buffer-cache. This gobbles the caller's reference to the +* buffer_head in the inode location struct. +* +* The caller must have write access to iloc->bh. +*/ +static int ext4_do_update_inode (handle_t *handle, +struct inode *inode, +struct ext4_iloc *iloc) +{ +struct ext4_inode *raw_inode =ext4_raw_inode (iloc); +struct ext4_inode_info *ei =EXT4_I (inode); +struct buffer_head *bh = iloc->bh; +struct super_block *sb = inode->i_sb; +int err; +int need_datasync =0 , set_large_file =0 ; + +spin_lock (&ei->i_raw_lock); + +/* +* For fields not tracked in the in-memory inode, initialise them +* to zero for new inodes. +*/ +if (ext4_test_inode_state (inode,EXT4_STATE_NEW )) +memset (raw_inode,0 ,EXT4_SB (inode->i_sb)->s_inode_size); + +if (READ_ONCE (ei->i_disksize) !=ext4_isize (inode->i_sb, raw_inode)) + need_datasync =1 ; +if (ei->i_disksize >0x7fffffffULL ) { +if (!ext4_has_feature_large_file (sb) || +EXT4_SB (sb)->s_es->s_rev_level ==cpu_to_le32 (EXT4_GOOD_OLD_REV )) + set_large_file =1 ; + } + + err =ext4_fill_raw_inode (inode, raw_inode);spin_unlock (&ei->i_raw_lock);if (err) {EXT4_ERROR_INODE (inode,"corrupted inode contents" );
Signature · 1 line
2.31.1
Zhang Yi
reviewerPATCH v4 5/6 ext4: move ext4_fill_raw_inode() related functions
In reply to Zhang YiIn preparation for calling ext4_fill_raw_inode() in __ext4_get_inode_loc(), move three related functions before __ext4_get_inode_loc(), no logical change.
--- fs/ext4/inode.c | 293 ++++++++++++++++++++++++------------------------ 1 file changed, 147 insertions(+), 146 deletions(-)
fs/ext4/inode.c +147 -146
@@ -4292,6 +4292,153 @@ int ext4_truncate(struct inode *inode)return err; } +static inline u64 ext4_inode_peek_iversion (const struct inode *inode) +{ +if (unlikely (EXT4_I (inode)->i_flags &EXT4_EA_INODE_FL )) +return inode_peek_iversion_raw (inode); +else +return inode_peek_iversion (inode); +} + +static int ext4_inode_blocks_set (struct ext4_inode *raw_inode, +struct ext4_inode_info *ei) +{ +struct inode *inode = &(ei->vfs_inode); +u64 i_blocks =READ_ONCE (inode->i_blocks); +struct super_block *sb = inode->i_sb; + +if (i_blocks <= ~0U ) { +/* +* i_blocks can be represented in a 32 bit variable +* as multiple of 512 bytes +*/ + raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); + raw_inode->i_blocks_high =0 ; +ext4_clear_inode_flag (inode,EXT4_INODE_HUGE_FILE ); +return 0 ; + } + +/* +* This should never happen since sb->s_maxbytes should not have +* allowed this, sb->s_maxbytes was set according to the huge_file +* feature in ext4_fill_super(). +*/ +if (!ext4_has_feature_huge_file (sb)) +return -EFSCORRUPTED ; + +if (i_blocks <=0xffffffffffffULL ) { +/* +* i_blocks can be represented in a 48 bit variable +* as multiple of 512 bytes +*/ + raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); + raw_inode->i_blocks_high =cpu_to_le16 (i_blocks >>32 ); +ext4_clear_inode_flag (inode,EXT4_INODE_HUGE_FILE ); + }else { +ext4_set_inode_flag (inode,EXT4_INODE_HUGE_FILE ); +/* i_block is stored in file system block size */ + i_blocks = i_blocks >> (inode->i_blkbits -9 ); + raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); + raw_inode->i_blocks_high =cpu_to_le16 (i_blocks >>32 ); + } +return 0 ; +} + +static int ext4_fill_raw_inode (struct inode *inode,struct ext4_inode *raw_inode) +{ +struct ext4_inode_info *ei =EXT4_I (inode); +uid_t i_uid; +gid_t i_gid; +projid_t i_projid; +int block; +int err; + + err =ext4_inode_blocks_set (raw_inode, ei); + + raw_inode->i_mode =cpu_to_le16 (inode->i_mode); + i_uid =i_uid_read (inode); + i_gid =i_gid_read (inode); + i_projid =from_kprojid (&init_user_ns, ei->i_projid); +if (!(test_opt (inode->i_sb,NO_UID32 ))) { + raw_inode->i_uid_low =cpu_to_le16 (low_16_bits (i_uid)); + raw_inode->i_gid_low =cpu_to_le16 (low_16_bits (i_gid)); +/* +* Fix up interoperability with old kernels. Otherwise, +* old inodes get re-used with the upper 16 bits of the +* uid/gid intact. +*/ +if (ei->i_dtime &&list_empty (&ei->i_orphan)) { + raw_inode->i_uid_high =0 ; + raw_inode->i_gid_high =0 ; + }else { + raw_inode->i_uid_high = +cpu_to_le16 (high_16_bits (i_uid)); + raw_inode->i_gid_high = +cpu_to_le16 (high_16_bits (i_gid)); + } + }else { + raw_inode->i_uid_low =cpu_to_le16 (fs_high2lowuid (i_uid)); + raw_inode->i_gid_low =cpu_to_le16 (fs_high2lowgid (i_gid)); + raw_inode->i_uid_high =0 ; + raw_inode->i_gid_high =0 ; + } + raw_inode->i_links_count =cpu_to_le16 (inode->i_nlink); + +EXT4_INODE_SET_XTIME (i_ctime, inode, raw_inode); +EXT4_INODE_SET_XTIME (i_mtime, inode, raw_inode); +EXT4_INODE_SET_XTIME (i_atime, inode, raw_inode); +EXT4_EINODE_SET_XTIME (i_crtime, ei, raw_inode); + + raw_inode->i_dtime =cpu_to_le32 (ei->i_dtime); + raw_inode->i_flags =cpu_to_le32 (ei->i_flags &0xFFFFFFFF ); +if (likely (!test_opt2 (inode->i_sb,HURD_COMPAT ))) + raw_inode->i_file_acl_high = +cpu_to_le16 (ei->i_file_acl >>32 ); + raw_inode->i_file_acl_lo =cpu_to_le32 (ei->i_file_acl); +ext4_isize_set (raw_inode, ei->i_disksize); + + raw_inode->i_generation =cpu_to_le32 (inode->i_generation); +if (S_ISCHR (inode->i_mode) ||S_ISBLK (inode->i_mode)) { +if (old_valid_dev (inode->i_rdev)) { + raw_inode->i_block[0 ] = +cpu_to_le32 (old_encode_dev (inode->i_rdev)); + raw_inode->i_block[1 ] =0 ; + }else { + raw_inode->i_block[0 ] =0 ; + raw_inode->i_block[1 ] = +cpu_to_le32 (new_encode_dev (inode->i_rdev)); + raw_inode->i_block[2 ] =0 ; + } + }else if (!ext4_has_inline_data (inode)) { +for (block =0 ; block <EXT4_N_BLOCKS ; block++) + raw_inode->i_block[block] = ei->i_data[block]; + } + +if (likely (!test_opt2 (inode->i_sb,HURD_COMPAT ))) { +u64 ivers =ext4_inode_peek_iversion (inode); + + raw_inode->i_disk_version =cpu_to_le32 (ivers); +if (ei->i_extra_isize) { +if (EXT4_FITS_IN_INODE (raw_inode, ei, i_version_hi)) + raw_inode->i_version_hi = +cpu_to_le32 (ivers >>32 ); + raw_inode->i_extra_isize = +cpu_to_le16 (ei->i_extra_isize); + } + } + +if (i_projid !=EXT4_DEF_PROJID && + !ext4_has_feature_project (inode->i_sb)) + err = err ?: -EFSCORRUPTED ; + +if (EXT4_INODE_SIZE (inode->i_sb) >EXT4_GOOD_OLD_INODE_SIZE && +EXT4_FITS_IN_INODE (raw_inode, ei, i_projid)) + raw_inode->i_projid =cpu_to_le32 (i_projid); + +ext4_inode_csum_set (inode, raw_inode, ei); +return err; +} +/* * ext4_get_inode_loc returns with an extra refcount against the inode's * underlying buffer_head on success. If 'in_mem' is true, we have all
@@ -4580,13 +4727,6 @@ static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)else inode_set_iversion_queried (inode, val); } -static inline u64 ext4_inode_peek_iversion (const struct inode *inode) -{ -if (unlikely (EXT4_I (inode)->i_flags &EXT4_EA_INODE_FL )) -return inode_peek_iversion_raw (inode); -else -return inode_peek_iversion (inode); -} struct inode *__ext4_iget(struct super_block *sb, unsigned long ino, ext4_iget_flags flags, const char *function,
@@ -4902,50 +5042,6 @@ struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,return ERR_PTR (ret); } -static int ext4_inode_blocks_set (struct ext4_inode *raw_inode, -struct ext4_inode_info *ei) -{ -struct inode *inode = &(ei->vfs_inode); -u64 i_blocks =READ_ONCE (inode->i_blocks); -struct super_block *sb = inode->i_sb; - -if (i_blocks <= ~0U ) { -/* -* i_blocks can be represented in a 32 bit variable -* as multiple of 512 bytes -*/ - raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); - raw_inode->i_blocks_high =0 ; -ext4_clear_inode_flag (inode,EXT4_INODE_HUGE_FILE ); -return 0 ; - } - -/* -* This should never happen since sb->s_maxbytes should not have -* allowed this, sb->s_maxbytes was set according to the huge_file -* feature in ext4_fill_super(). -*/ -if (!ext4_has_feature_huge_file (sb)) -return -EFSCORRUPTED ; - -if (i_blocks <=0xffffffffffffULL ) { -/* -* i_blocks can be represented in a 48 bit variable -* as multiple of 512 bytes -*/ - raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); - raw_inode->i_blocks_high =cpu_to_le16 (i_blocks >>32 ); -ext4_clear_inode_flag (inode,EXT4_INODE_HUGE_FILE ); - }else { -ext4_set_inode_flag (inode,EXT4_INODE_HUGE_FILE ); -/* i_block is stored in file system block size */ - i_blocks = i_blocks >> (inode->i_blkbits -9 ); - raw_inode->i_blocks_lo =cpu_to_le32 (i_blocks); - raw_inode->i_blocks_high =cpu_to_le16 (i_blocks >>32 ); - } -return 0 ; -} - static void __ext4_update_other_inode_time(struct super_block *sb, unsigned long orig_ino, unsigned long ino,
@@ -5006,101 +5102,6 @@ static void ext4_update_other_inodes_time(struct super_block *sb,rcu_read_unlock (); } -static int ext4_fill_raw_inode (struct inode *inode,struct ext4_inode *raw_inode) -{ -struct ext4_inode_info *ei =EXT4_I (inode); -uid_t i_uid; -gid_t i_gid; -projid_t i_projid; -int block; -int err; - - err =ext4_inode_blocks_set (raw_inode, ei); - - raw_inode->i_mode =cpu_to_le16 (inode->i_mode); - i_uid =i_uid_read (inode); - i_gid =i_gid_read (inode); - i_projid =from_kprojid (&init_user_ns, ei->i_projid); -if (!(test_opt (inode->i_sb,NO_UID32 ))) { - raw_inode->i_uid_low =cpu_to_le16 (low_16_bits (i_uid)); - raw_inode->i_gid_low =cpu_to_le16 (low_16_bits (i_gid)); -/* -* Fix up interoperability with old kernels. Otherwise, -* old inodes get re-used with the upper 16 bits of the -* uid/gid intact. -*/ -if (ei->i_dtime &&list_empty (&ei->i_orphan)) { - raw_inode->i_uid_high =0 ; - raw_inode->i_gid_high =0 ; - }else { - raw_inode->i_uid_high = -cpu_to_le16 (high_16_bits (i_uid)); - raw_inode->i_gid_high = -cpu_to_le16 (high_16_bits (i_gid)); - } - }else { - raw_inode->i_uid_low =cpu_to_le16 (fs_high2lowuid (i_uid)); - raw_inode->i_gid_low =cpu_to_le16 (fs_high2lowgid (i_gid)); - raw_inode->i_uid_high =0 ; - raw_inode->i_gid_high =0 ; - } - raw_inode->i_links_count =cpu_to_le16 (inode->i_nlink); - -EXT4_INODE_SET_XTIME (i_ctime, inode, raw_inode); -EXT4_INODE_SET_XTIME (i_mtime, inode, raw_inode); -EXT4_INODE_SET_XTIME (i_atime, inode, raw_inode); -EXT4_EINODE_SET_XTIME (i_crtime, ei, raw_inode); - - raw_inode->i_dtime =cpu_to_le32 (ei->i_dtime); - raw_inode->i_flags =cpu_to_le32 (ei->i_flags &0xFFFFFFFF ); -if (likely (!test_opt2 (inode->i_sb,HURD_COMPAT ))) - raw_inode->i_file_acl_high = -cpu_to_le16 (ei->i_file_acl >>32 ); - raw_inode->i_file_acl_lo =cpu_to_le32 (ei->i_file_acl); -ext4_isize_set (raw_inode, ei->i_disksize); - - raw_inode->i_generation =cpu_to_le32 (inode->i_generation); -if (S_ISCHR (inode->i_mode) ||S_ISBLK (inode->i_mode)) { -if (old_valid_dev (inode->i_rdev)) { - raw_inode->i_block[0 ] = -cpu_to_le32 (old_encode_dev (inode->i_rdev)); - raw_inode->i_block[1 ] =0 ; - }else { - raw_inode->i_block[0 ] =0 ; - raw_inode->i_block[1 ] = -cpu_to_le32 (new_encode_dev (inode->i_rdev)); - raw_inode->i_block[2 ] =0 ; - } - }else if (!ext4_has_inline_data (inode)) { -for (block =0 ; block <EXT4_N_BLOCKS ; block++) - raw_inode->i_block[block] = ei->i_data[block]; - } - -if (likely (!test_opt2 (inode->i_sb,HURD_COMPAT ))) { -u64 ivers =ext4_inode_peek_iversion (inode); - - raw_inode->i_disk_version =cpu_to_le32 (ivers); -if (ei->i_extra_isize) { -if (EXT4_FITS_IN_INODE (raw_inode, ei, i_version_hi)) - raw_inode->i_version_hi = -cpu_to_le32 (ivers >>32 ); - raw_inode->i_extra_isize = -cpu_to_le16 (ei->i_extra_isize); - } - } - -if (i_projid !=EXT4_DEF_PROJID && - !ext4_has_feature_project (inode->i_sb)) - err = err ?: -EFSCORRUPTED ; - -if (EXT4_INODE_SIZE (inode->i_sb) >EXT4_GOOD_OLD_INODE_SIZE && -EXT4_FITS_IN_INODE (raw_inode, ei, i_projid)) - raw_inode->i_projid =cpu_to_le32 (i_projid); - -ext4_inode_csum_set (inode, raw_inode, ei); -return err; -} -/* * Post the struct inode info into an on-disk inode location in the * buffer-cache. This gobbles the caller's reference to the
Signature · 1 line
2.31.1
Zhang Yi
reviewerPATCH v4 6/6 ext4: prevent getting empty inode buffer
In reply to Zhang YiIn ext4_get_inode_loc(), we may skip IO and get an zero && uptodate inode buffer when the inode monopolize an inode block for performance reason. For most cases, ext4_mark_iloc_dirty() will fill the inode buffer to make it fine, but we could miss this call if something bad happened. Finally, __ext4_get_inode_loc_noinmem() may probably get an empty inode buffer and trigger ext4 error.
For example, if we remove a nonexistent xattr on inode A, ext4_xattr_set_handle() will return ENODATA before invoking ext4_mark_iloc_dirty(), it will left an uptodate but zero buffer. We will get checksum error message in ext4_iget() when getting inode again.
EXT4-fs error (device sda): ext4_lookup:1784: inode #131074: comm cat:
iget: checksum invalid
Even worse, if we allocate another inode B at the same inode block, it will corrupt the inode A on disk when write back inode B.
So this patch initialize the inode buffer by filling the in-mem inode contents if we skip read I/O, ensure that the buffer is really uptodate.
--- fs/ext4/inode.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
fs/ext4/inode.c +16 -6
@@ -4446,8 +4446,8 @@ static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode* inode. */ static int __ext4_get_inode_loc (struct super_block *sb,unsigned long ino, -struct ext4_iloc *iloc,int in_mem, -ext4_fsblk_t *ret_block) +struct inode *inode,struct ext4_iloc *iloc, +int in_mem,ext4_fsblk_t *ret_block) {struct ext4_group_desc *gdp;struct buffer_head *bh;
@@ -4514,8 +4514,13 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino, }brelse (bitmap_bh);if (i == start + inodes_per_block) { +struct ext4_inode *raw_inode = + (struct ext4_inode *) (bh->b_data + iloc->offset); +/* all other inodes are free, so skip I/O */ memset (bh->b_data,0 , bh->b_size); +if (!ext4_test_inode_state (inode,EXT4_STATE_NEW )) +ext4_fill_raw_inode (inode, raw_inode);set_buffer_uptodate (bh);unlock_buffer (bh); goto has_buffer;
@@ -4576,7 +4581,7 @@ static int __ext4_get_inode_loc_noinmem(struct inode *inode,ext4_fsblk_t err_blk;int ret; - ret =__ext4_get_inode_loc (inode->i_sb, inode->i_ino, iloc,0 , + ret =__ext4_get_inode_loc (inode->i_sb, inode->i_ino,NULL , iloc,0 , &err_blk);if (ret == -EIO )
@@ -4592,8 +4597,13 @@ int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)int ret;/* We have all inode data except xattrs in memory here. */ - ret =__ext4_get_inode_loc (inode->i_sb, inode->i_ino, iloc, - !ext4_test_inode_state (inode,EXT4_STATE_XATTR ), &err_blk); +if (ext4_test_inode_state (inode,EXT4_STATE_XATTR )) { + ret =__ext4_get_inode_loc (inode->i_sb, inode->i_ino,NULL , + iloc, false, &err_blk); + }else { + ret =__ext4_get_inode_loc (inode->i_sb, inode->i_ino, inode, + iloc, true, &err_blk); + }if (ret == -EIO ) ext4_error_inode_block(inode, err_blk, EIO,
@@ -4606,7 +4616,7 @@ int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)int ext4_get_fc_inode_loc (struct super_block *sb,unsigned long ino,struct ext4_iloc *iloc) { -return __ext4_get_inode_loc (sb, ino, iloc,0 ,NULL ); +return __ext4_get_inode_loc (sb, ino,NULL , iloc,0 ,NULL ); }static bool ext4_should_enable_dax (struct inode *inode)
Signature · 1 line
2.31.1
Theodore Ts'o
maintainerRe: [PATCH v4 6/6] ext4: prevent getting empty inode buffer
Quoted 14 lines · Zhang Yi
So this patch initialize the inode buffer by filling the in-mem inode contents if we skip read I/O, ensure that the buffer is really uptodate.
Signed-off-by Zhang Yi <[email protected]>--- fs/ext4/inode.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)fs/ext4/inode.c +0
-0
fs/ext4/inode.c static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode · line 4450
Quoted 7 lines
fs/ext4/inode.c +2
-2@@ -4446,8 +4446,8 @@ static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode* inode. */ static int __ext4_get_inode_loc (struct super_block *sb,unsigned long ino, -struct ext4_iloc *iloc,int in_mem, -ext4_fsblk_t *ret_block) +struct inode *inode,struct ext4_iloc *iloc, +int in_mem,ext4_fsblk_t *ret_block)
In this patch you've added a new argument 'inode'. However, if in_mem is true, and inode is NULL, the kernel will crash with a null pointer dereference. Furthermore, whenever in_mem is false, the callers pass in NULL for inode.
Given that, perhaps we should just drop the in_mem argument, and then instead of
if (in_mem) {we do:
if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR) {with the comments adjusted accordingly?
I think it will make the code a bit simpler and readable.
What do you think?
- Ted
Zhang Yi
reviewerRe: [PATCH v4 6/6] ext4: prevent getting empty inode buffer
Quoted 36 lines · Theodore Ts'o
Quoted 14 lines · Zhang Yi
So this patch initialize the inode buffer by filling the in-mem inode contents if we skip read I/O, ensure that the buffer is really uptodate.
Signed-off-by Zhang Yi <[email protected]>--- fs/ext4/inode.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)fs/ext4/inode.c +0
-0fs/ext4/inode.c static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode · line 4450
Quoted 7 lines
fs/ext4/inode.c +2
-2@@ -4446,8 +4446,8 @@ static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode* inode. */ static int __ext4_get_inode_loc (struct super_block *sb,unsigned long ino, -struct ext4_iloc *iloc,int in_mem, -ext4_fsblk_t *ret_block) +struct inode *inode,struct ext4_iloc *iloc, +int in_mem,ext4_fsblk_t *ret_block)In this patch you've added a new argument 'inode'. However, if
in_memis true, and inode is NULL, the kernel will crash with a null pointer dereference. Furthermore, wheneverin_memis false, the callers pass in NULL for inode.Given that, perhaps we should just drop the
in_memargument, and then instead ofif (in_mem) {we do:
Quoted 8 lines
if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR) {with the comments adjusted accordingly?
I think it will make the code a bit simpler and readable.
What do you think?
Yes,although I have already prevent passing 'in_mem' is true but 'inode' is NULL in ext4_get_inode_loc(), using two arguments show the inode in-mem case is not safe. I will remove the 'in_mem' parameter.
Thanks,
Yi.