ext4: handle ext4_mark_inode_dirty errors
PATCH 1/1 ext4: handle ext4_mark_inode_dirty errors
ext4_mark_inode_dirty() can fail for real reasons. Ignoring its return value may lead ext4 to ignore real failures that would result in corruption / crashes. Harden ext4_mark_inode_dirty error paths to fail as soon as possible and return errors to the caller whenever appropriate.
One of the possible scnearios when this bug could affected is that while creating a new inode, its directory entry gets added successfully but while writing the inode itself mark_inode_dirty returns error which is ignored. This would result in inconsistency that the directory entry points to a non-existent inode.
Ran gce-xfstests smoke tests and verified that there were no regressions.
--- fs/ext4/acl.c | 2 +- fs/ext4/ext4.h | 2 +- fs/ext4/ext4_jbd2.h | 5 ++- fs/ext4/extents.c | 34 +++++++++++--------- fs/ext4/file.c | 11 +++++-- fs/ext4/indirect.c | 4 ++- fs/ext4/inline.c | 6 ++-- fs/ext4/inode.c | 38 ++++++++++++++++------- fs/ext4/migrate.c | 12 ++++--- fs/ext4/namei.c | 76 +++++++++++++++++++++++++++++---------------- fs/ext4/super.c | 16 ++++++---- fs/ext4/xattr.c | 6 ++-- 12 files changed, 139 insertions(+), 73 deletions(-)
fs/ext4/acl.c +1 -1
@@ -256,7 +256,7 @@ ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)if (!error && update_mode) { inode->i_mode = mode; inode->i_ctime =current_time (inode); -ext4_mark_inode_dirty (handle, inode); + error =ext4_mark_inode_dirty (handle, inode); } out_stop:ext4_journal_stop (handle);
fs/ext4/ext4.h +1 -1
@@ -3354,7 +3354,7 @@ struct ext4_extent;*/ #define EXT_MAX_BLOCKS 0xffffffff -extern int ext4_ext_tree_init (handle_t *handle,struct inode *); +extern void ext4_ext_tree_init (handle_t *handle,struct inode *inode);extern int ext4_ext_index_trans_blocks (struct inode *inode,int extents);extern int ext4_ext_map_blocks (handle_t *handle,struct inode *inode,struct ext4_map_blocks *map,int flags);
fs/ext4/ext4_jbd2.h +4 -1
@@ -222,7 +222,10 @@ ext4_mark_iloc_dirty(handle_t *handle,int ext4_reserve_inode_write (handle_t *handle,struct inode *inode,struct ext4_iloc *iloc); -int ext4_mark_inode_dirty (handle_t *handle,struct inode *inode); +#define ext4_mark_inode_dirty (__h, __i) \ + __ext4_mark_inode_dirty((__h), (__i), __func__, __LINE__) +int __ext4_mark_inode_dirty (handle_t *handle,struct inode *inode, +const char *func,unsigned int line); int ext4_expand_extra_isize(struct inode *inode, unsigned int new_extra_isize,
fs/ext4/extents.c +20 -14
@@ -816,7 +816,7 @@ ext4_ext_binsearch(struct inode *inode, } -int ext4_ext_tree_init (handle_t *handle,struct inode *inode) +void ext4_ext_tree_init (handle_t *handle,struct inode *inode) {struct ext4_extent_header *eh;
@@ -826,7 +826,6 @@ int ext4_ext_tree_init(handle_t *handle, struct inode *inode) eh->eh_magic =EXT4_EXT_MAGIC ; eh->eh_max =cpu_to_le16 (ext4_ext_space_root (inode,0 ));ext4_mark_inode_dirty (handle, inode); -return 0 ; } struct ext4_ext_path *
@@ -1319,7 +1318,7 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, ext4_idx_pblock(EXT_FIRST_INDEX(neh)));le16_add_cpu (&neh->eh_depth,1 ); -ext4_mark_inode_dirty (handle, inode); + err =ext4_mark_inode_dirty (handle, inode); out:brelse (bh);
@@ -4367,7 +4366,7 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,struct inode *inode =file_inode (file);handle_t *handle;int ret =0 ; -int ret2 =0 ; +int ret2 =0 , ret3 =0 ;int retries =0 ;int depth =0 ;struct ext4_map_blocks map;
@@ -4427,10 +4426,11 @@ static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,if (ext4_update_inode_size (inode, epos) &0x1 ) inode->i_mtime = inode->i_ctime; } -ext4_mark_inode_dirty (handle, inode); + ret2 =ext4_mark_inode_dirty (handle, inode);ext4_update_inode_fsync_trans (handle, inode,1 ); - ret2 =ext4_journal_stop (handle); -if (ret2) + ret3 =ext4_journal_stop (handle); + ret2 = ret3 ? ret3 : ret2; +if (unlikely (ret2))break ; } if (ret == -ENOSPC &&
@@ -4581,7 +4581,9 @@ static long ext4_zero_range(struct file *file, loff_t offset, inode->i_mtime = inode->i_ctime =current_time (inode);if (new_size)ext4_update_inode_size (inode, new_size); -ext4_mark_inode_dirty (handle, inode); + ret =ext4_mark_inode_dirty (handle, inode); +if (unlikely (ret)) + goto out_handle;/* Zero out partial block at the edges of the range */ ret =ext4_zero_partial_blocks (handle, inode, offset, len);
@@ -4591,6 +4593,7 @@ static long ext4_zero_range(struct file *file, loff_t offset,if (file->f_flags &O_SYNC )ext4_handle_sync (handle); +out_handle:ext4_journal_stop (handle); out_mutex:inode_unlock (inode);
@@ -4704,8 +4707,7 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, loff_t offset, ssize_t len) {unsigned int max_blocks; -int ret =0 ; -int ret2 =0 ; +int ret =0 , ret2 =0 , ret3 =0 ;struct ext4_map_blocks map;unsigned int blkbits = inode->i_blkbits;unsigned int credits =0 ;
@@ -4738,9 +4740,13 @@ int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,"ext4_ext_map_blocks returned %d" , inode->i_ino, map.m_lblk, map.m_len, ret); -ext4_mark_inode_dirty (handle, inode); -if (credits) - ret2 =ext4_journal_stop (handle); + ret2 =ext4_mark_inode_dirty (handle, inode); +if (credits) { + ret3 =ext4_journal_stop (handle); +if (unlikely (ret3)) + ret2 = ret3; + } +if (ret <=0 || ret2)break ; }
@@ -5277,7 +5283,7 @@ static int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)if (IS_SYNC (inode))ext4_handle_sync (handle); inode->i_mtime = inode->i_ctime =current_time (inode); -ext4_mark_inode_dirty (handle, inode); + ret =ext4_mark_inode_dirty (handle, inode);ext4_update_inode_fsync_trans (handle, inode,1 ); out_stop:
fs/ext4/file.c +9 -2
@@ -287,6 +287,7 @@ static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset,bool truncate = false;u8 blkbits = inode->i_blkbits;ext4_lblk_t written_blk, end_blk; +int ret;/* * Note that EXT4_I(inode)->i_disksize can get extended up to
@@ -327,8 +328,14 @@ static ssize_t ext4_handle_inode_extension(struct inode *inode, loff_t offset, goto truncate; } -if (ext4_update_inode_size (inode, offset + written)) -ext4_mark_inode_dirty (handle, inode); +if (ext4_update_inode_size (inode, offset + written)) { + ret =ext4_mark_inode_dirty (handle, inode); +if (unlikely (ret)) { + written = ret; +ext4_journal_stop (handle); + goto truncate; + } + }/* * We may need to truncate allocated but not written blocks beyond EOF.
fs/ext4/indirect.c +3 -1
@@ -467,7 +467,9 @@ static int ext4_splice_branch(handle_t *handle,/* * OK, we spliced it into the inode itself on a direct block. */ -ext4_mark_inode_dirty (handle, ar->inode); + err =ext4_mark_inode_dirty (handle, ar->inode); +if (unlikely (err)) + goto err_out;jbd_debug (5 ,"splicing direct\n" ); }return err;
fs/ext4/inline.c +4 -2
@@ -1260,7 +1260,7 @@ static int ext4_convert_inline_data_nolock(handle_t *handle,int ext4_try_add_inline_entry (handle_t *handle,struct ext4_filename *fname,struct inode *dir,struct inode *inode) { -int ret, inline_size, no_expand; +int ret, ret2, inline_size, no_expand;void *inline_start;struct ext4_iloc iloc;
@@ -1314,7 +1314,9 @@ int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname, out:ext4_write_unlock_xattr (dir, &no_expand); -ext4_mark_inode_dirty (handle, dir); + ret2 =ext4_mark_inode_dirty (handle, dir); +if (unlikely (ret2 && !ret)) + ret = ret2;brelse (iloc.bh);return ret; }
fs/ext4/inode.c +26 -12
@@ -1296,7 +1296,7 @@ static int ext4_write_end(struct file *file,* filesystems. */ if (i_size_changed || inline_data) -ext4_mark_inode_dirty (handle, inode); + ret =ext4_mark_inode_dirty (handle, inode);if (pos + len > inode->i_size && !verity &&ext4_can_truncate (inode))/* if we have allocated more blocks and copied
@@ -3077,7 +3077,7 @@ static int ext4_da_write_end(struct file *file,* new_i_size is less that inode->i_size * bu greater than i_disksize.(hint delalloc) */ -ext4_mark_inode_dirty (handle, inode); + ret =ext4_mark_inode_dirty (handle, inode); } }
@@ -3094,7 +3094,7 @@ static int ext4_da_write_end(struct file *file,if (ret2 <0 ) ret = ret2; ret2 =ext4_journal_stop (handle); -if (!ret) +if (unlikely (ret2 && !ret)) ret = ret2;return ret ? ret : copied;
@@ -3886,6 +3886,8 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset, loff_t len) {handle_t *handle; +int ret; +loff_t size =i_size_read (inode);WARN_ON (!inode_is_locked (inode));
@@ -3899,10 +3901,10 @@ int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,if (IS_ERR (handle))return PTR_ERR (handle);ext4_update_i_disksize (inode, size); -ext4_mark_inode_dirty (handle, inode); + ret =ext4_mark_inode_dirty (handle, inode);ext4_journal_stop (handle); -return 0 ; +return ret; }static void ext4_wait_dax_page (struct ext4_inode_info *ei)
@@ -3954,7 +3956,7 @@ int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)loff_t first_block_offset, last_block_offset;handle_t *handle;unsigned int credits; -int ret =0 ; +int ret =0 , ret2 =0 ;trace_ext4_punch_hole (inode, offset, length,0 );
@@ -4077,7 +4079,9 @@ int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)ext4_handle_sync (handle); inode->i_mtime = inode->i_ctime =current_time (inode); -ext4_mark_inode_dirty (handle, inode); + ret2 =ext4_mark_inode_dirty (handle, inode); +if (unlikely (ret2)) + ret = ret2;if (ret >=0 )ext4_update_inode_fsync_trans (handle, inode,1 ); out_stop:
@@ -4146,7 +4150,7 @@ int ext4_truncate(struct inode *inode) {struct ext4_inode_info *ei =EXT4_I (inode);unsigned int credits; -int err =0 ; +int err =0 , err2;handle_t *handle;struct address_space *mapping = inode->i_mapping;
@@ -4234,7 +4238,9 @@ int ext4_truncate(struct inode *inode)ext4_orphan_del (handle, inode); inode->i_mtime = inode->i_ctime =current_time (inode); -ext4_mark_inode_dirty (handle, inode); + err2 =ext4_mark_inode_dirty (handle, inode); +if (unlikely (err2 && !err)) + err = err2;ext4_journal_stop (handle);trace_ext4_truncate_exit (inode);
@@ -5292,6 +5298,8 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr) inode->i_gid = attr->ia_gid; error =ext4_mark_inode_dirty (handle, inode);ext4_journal_stop (handle); +if (unlikely (error)) +return error; }if (attr->ia_valid &ATTR_SIZE ) {
@@ -5777,7 +5785,8 @@ int ext4_expand_extra_isize(struct inode *inode,* Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) * we start and wait on commits. */ -int ext4_mark_inode_dirty (handle_t *handle,struct inode *inode) +int __ext4_mark_inode_dirty (handle_t *handle,struct inode *inode, +const char *func,unsigned int line) {struct ext4_iloc iloc;struct ext4_sb_info *sbi =EXT4_SB (inode->i_sb);
@@ -5787,13 +5796,18 @@ int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)trace_ext4_mark_inode_dirty (inode, _RET_IP_); err =ext4_reserve_inode_write (handle, inode, &iloc);if (err) -return err; + goto out;if (EXT4_I (inode)->i_extra_isize < sbi->s_want_extra_isize)ext4_try_to_expand_extra_isize (inode, sbi->s_want_extra_isize, iloc, handle); -return ext4_mark_iloc_dirty (handle, inode, &iloc); + err =ext4_mark_iloc_dirty (handle, inode, &iloc); +out: +if (unlikely (err)) +ext4_error_inode_err (inode, func, line,0 , err, +"mark_inode_dirty error" ); +return err; }/*
fs/ext4/migrate.c +8 -4
@@ -287,7 +287,7 @@ static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)static int ext4_ext_swap_inode_data (handle_t *handle,struct inode *inode,struct inode *tmp_inode) { -int retval; +int retval, retval2 =0 ;__le32 i_data[3 ];struct ext4_inode_info *ei =EXT4_I (inode);struct ext4_inode_info *tmp_ei =EXT4_I (tmp_inode);
@@ -342,7 +342,9 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,* i_blocks when freeing the indirect meta-data blocks */ retval =free_ind_block (handle, inode, i_data); -ext4_mark_inode_dirty (handle, inode); + retval2 =ext4_mark_inode_dirty (handle, inode); +if (unlikely (retval2 && !retval)) + retval = retval2; err_out:return retval;
@@ -601,7 +603,7 @@ int ext4_ind_migrate(struct inode *inode)ext4_lblk_t start, end;ext4_fsblk_t blk;handle_t *handle; -int ret; +int ret, ret2 =0 ;if (!ext4_has_feature_extents (inode->i_sb) || (!ext4_test_inode_flag (inode,EXT4_INODE_EXTENTS )))
@@ -655,7 +657,9 @@ int ext4_ind_migrate(struct inode *inode)memset (ei->i_data,0 ,sizeof (ei->i_data));for (i = start; i <= end; i++) ei->i_data[i] =cpu_to_le32 (blk++); -ext4_mark_inode_dirty (handle, inode); + ret2 =ext4_mark_inode_dirty (handle, inode); +if (unlikely (ret2 && !ret)) + ret = ret2; errout:ext4_journal_stop (handle);up_write (&EXT4_I (inode)->i_data_sem);
fs/ext4/namei.c +49 -27
192 lines not on this page · raw message
fs/ext4/super.c +10 -6
@@ -5885,7 +5885,7 @@ static int ext4_quota_on(struct super_block *sb, int type, int format_id,EXT4_I (inode)->i_flags |=EXT4_NOATIME_FL |EXT4_IMMUTABLE_FL ;inode_set_flags (inode,S_NOATIME |S_IMMUTABLE ,S_NOATIME |S_IMMUTABLE ); -ext4_mark_inode_dirty (handle, inode); + err =ext4_mark_inode_dirty (handle, inode);ext4_journal_stop (handle); unlock_inode:inode_unlock (inode);
@@ -5987,12 +5987,14 @@ static int ext4_quota_off(struct super_block *sb, int type)* this is not a hard failure and quotas are already disabled. */ handle =ext4_journal_start (inode,EXT4_HT_QUOTA ,1 ); -if (IS_ERR (handle)) +if (IS_ERR (handle)) { + err =PTR_ERR (handle); goto out_unlock; + }EXT4_I (inode)->i_flags &= ~(EXT4_NOATIME_FL |EXT4_IMMUTABLE_FL );inode_set_flags (inode,0 ,S_NOATIME |S_IMMUTABLE ); inode->i_mtime = inode->i_ctime =current_time (inode); -ext4_mark_inode_dirty (handle, inode); + err =ext4_mark_inode_dirty (handle, inode);ext4_journal_stop (handle); out_unlock:inode_unlock (inode);
@@ -6050,7 +6052,7 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type, {struct inode *inode =sb_dqopt (sb)->files[type];ext4_lblk_t blk = off >>EXT4_BLOCK_SIZE_BITS (sb); -int err, offset = off & (sb->s_blocksize -1 ); +int err =0 , err2 =0 , offset = off & (sb->s_blocksize -1 );int retries =0 ;struct buffer_head *bh;handle_t *handle =journal_current_handle ();
@@ -6098,9 +6100,11 @@ static ssize_t ext4_quota_write(struct super_block *sb, int type,if (inode->i_size < off + len) {i_size_write (inode, off + len);EXT4_I (inode)->i_disksize = inode->i_size; -ext4_mark_inode_dirty (handle, inode); + err2 =ext4_mark_inode_dirty (handle, inode); +if (unlikely (err2 && !err)) + err = err2; } - return len; + return err ? err : len; }#endif
fs/ext4/xattr.c +4 -2
@@ -1327,7 +1327,7 @@ static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,int blocksize = ea_inode->i_sb->s_blocksize;int max_blocks = (bufsize + blocksize -1 ) >> ea_inode->i_blkbits;int csize, wsize =0 ; -int ret =0 ; +int ret =0 , ret2 =0 ;int retries =0 ; retry:
@@ -1385,7 +1385,9 @@ static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,ext4_update_i_disksize (ea_inode, wsize);inode_unlock (ea_inode); -ext4_mark_inode_dirty (handle, ea_inode); + ret2 =ext4_mark_inode_dirty (handle, ea_inode); +if (unlikely (ret2 && !ret)) + ret = ret2; out:brelse (bh);
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
PATCH v3 1/1 ext4: don't ignore return values from ext4_ext_dirty()
Don't ignore return values from ext4_ext_dirty, since the errors indicate valid failures below Ext4. In all of the other instances of ext4_ext_dirty calls, the error return value is handled in some way. This patch makes those remaining couple of places to handle ext4_ext_dirty errors as well. In case of ext4_split_extent_at(), the ignorance of return value is intentional. The reason is that we are already in error path and there isn't much we can do if ext4_ext_dirty returns error. This patch adds a comment for that case explaining why we ignore the return value.
In the longer run, we probably should
make sure that errors from other mark_dirty routines are handled as well.
Ran gce-xfstests smoke tests and verified that there were no regressions.
---
Changes since V2:
* Reorganized commit message
Changes since V1:
* Fixed incorrect return value handling in ext4_split_extent_at()
fs/ext4/extents.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext4/extents.c +5 -1
@@ -3244,6 +3244,10 @@ static int ext4_split_extent_at(handle_t *handle, fix_extent_len: ex->ee_len = orig_ex.ee_len; +/* +* Ignore ext4_ext_dirty return value since we are already in error path +* and err is a non-zero error code. +*/ ext4_ext_dirty (handle, inode, path + path->p_depth);return err; }
@@ -3503,7 +3507,7 @@ static int ext4_ext_convert_to_initialized(handle_t *handle, }if (allocated) {/* Mark the block containing both extents as dirty */ -ext4_ext_dirty (handle, inode, path + depth); + err =ext4_ext_dirty (handle, inode, path + depth);/* Update path to point to the right extent */ path[depth].p_ext = abut_ex;
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
Re: [PATCH v3] ext4: don't ignore return values from ext4_ext_dirty()
Quoted 14 lines · Harshad Shirwadkar
Don't ignore return values from
ext4_ext_dirty, since the errors indicate valid failures below Ext4. In all of the other instances ofext4_ext_dirtycalls, the error return value is handled in some way. This patch makes those remaining couple of places to handleext4_ext_dirtyerrors as well. In case ofext4_split_extent_at(), the ignorance of return value is intentional. The reason is that we are already in error path and there isn't much we can do ifext4_ext_dirtyreturns error. This patch adds a comment for that case explaining why we ignore the return value.In the longer run, we probably should
make sure that errors from othermark_dirtyroutines are handled as well.
Quoted 5 lines
Ran gce-xfstests smoke tests and verified that there were no regressions.
Signed-off-by Harshad Shirwadkar <[email protected]>Reviewed-by Jan Kara <[email protected]>
Thanks, applied.
- Ted
Quoted 12 lines · Harshad Shirwadkar
ext4_mark_inode_dirty()can fail for real reasons. Ignoring its return value may lead ext4 to ignore real failures that would result in corruption / crashes. Hardenext4_mark_inode_dirtyerror paths to fail as soon as possible and return errors to the caller whenever appropriate.One of the possible scnearios when this bug could affected is that while creating a new inode, its directory entry gets added successfully but while writing the inode itself
mark_inode_dirtyreturns error which is ignored. This would result in inconsistency that the directory entry points to a non-existent inode.
Quoted 4 lines
Ran gce-xfstests smoke tests and verified that there were no regressions.
Signed-off-by Harshad Shirwadkar <[email protected]>
Applied, thanks.
- Ted