Re: [PATCH v3 01/22] ext4: simplify size updating in ext4_setattr()

From: Jan Kara

Date: Thu Apr 30 2026 - 08:44:16 EST


On Wed 22-04-26 10:10:21, Zhang Yi wrote:
> From: Zhang Yi <yi.zhang@xxxxxxxxxx>
>
> The logic for updating the file size in ext4_setattr() is currently
> somewhat messy. By directly entering the error-handling path after
> failing to add an orphan inode, the unnecessary recovery process
> involving old_disksize and the file size can be avoided.
>
> Signed-off-by: Zhang Yi <yi.zhang@xxxxxxxxxx>

Indeed, that code looks confusing for no good reason. Feel free to add:

Reviewed-by: Jan Kara <jack@xxxxxxx>

Honza

> ---
> fs/ext4/inode.c | 22 +++++++++-------------
> 1 file changed, 9 insertions(+), 13 deletions(-)
>
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index c2c2d6ac7f3d..0751dc55e94f 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -5953,7 +5953,6 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> if (attr->ia_valid & ATTR_SIZE) {
> handle_t *handle;
> loff_t oldsize = inode->i_size;
> - loff_t old_disksize;
> int shrink = (attr->ia_size < inode->i_size);
>
> if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
> @@ -6037,6 +6036,8 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> if (ext4_handle_valid(handle) && shrink) {
> error = ext4_orphan_add(handle, inode);
> orphan = 1;
> + if (error)
> + goto out_handle;
> }
>
> if (shrink)
> @@ -6052,23 +6053,18 @@ int ext4_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
> (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
> inode->i_sb->s_blocksize_bits);
>
> - down_write(&EXT4_I(inode)->i_data_sem);
> - old_disksize = EXT4_I(inode)->i_disksize;
> - EXT4_I(inode)->i_disksize = attr->ia_size;
> -
> /*
> * We have to update i_size under i_data_sem together
> * with i_disksize to avoid races with writeback code
> - * running ext4_wb_update_i_disksize().
> + * updating disksize in mpage_map_and_submit_extent().
> */
> - if (!error)
> - i_size_write(inode, attr->ia_size);
> - else
> - EXT4_I(inode)->i_disksize = old_disksize;
> + down_write(&EXT4_I(inode)->i_data_sem);
> + i_size_write(inode, attr->ia_size);
> + EXT4_I(inode)->i_disksize = attr->ia_size;
> up_write(&EXT4_I(inode)->i_data_sem);
> - rc = ext4_mark_inode_dirty(handle, inode);
> - if (!error)
> - error = rc;
> +
> + error = ext4_mark_inode_dirty(handle, inode);
> +out_handle:
> ext4_journal_stop(handle);
> if (error)
> goto out_mmap_sem;
> --
> 2.52.0
>
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR