Skip to content

Commit de01f48

Browse files
zhangyi089tytso
authored andcommitted
ext4: prevent getting empty inode buffer
In 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. Signed-off-by: Zhang Yi <[email protected]> Reviewed-by: Jan Kara <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 9a1bf32 commit de01f48

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

fs/ext4/inode.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4380,12 +4380,12 @@ static int ext4_fill_raw_inode(struct inode *inode, struct ext4_inode *raw_inode
43804380

43814381
/*
43824382
* ext4_get_inode_loc returns with an extra refcount against the inode's
4383-
* underlying buffer_head on success. If 'in_mem' is true, we have all
4384-
* data in memory that is needed to recreate the on-disk version of this
4385-
* inode.
4383+
* underlying buffer_head on success. If we pass 'inode' and it does not
4384+
* have in-inode xattr, we have all inode data in memory that is needed
4385+
* to recreate the on-disk version of this inode.
43864386
*/
43874387
static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
4388-
struct ext4_iloc *iloc, int in_mem,
4388+
struct inode *inode, struct ext4_iloc *iloc,
43894389
ext4_fsblk_t *ret_block)
43904390
{
43914391
struct ext4_group_desc *gdp;
@@ -4431,7 +4431,7 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
44314431
* is the only valid inode in the block, we need not read the
44324432
* block.
44334433
*/
4434-
if (in_mem) {
4434+
if (inode && !ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
44354435
struct buffer_head *bitmap_bh;
44364436
int i, start;
44374437

@@ -4459,8 +4459,13 @@ static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
44594459
}
44604460
brelse(bitmap_bh);
44614461
if (i == start + inodes_per_block) {
4462+
struct ext4_inode *raw_inode =
4463+
(struct ext4_inode *) (bh->b_data + iloc->offset);
4464+
44624465
/* all other inodes are free, so skip I/O */
44634466
memset(bh->b_data, 0, bh->b_size);
4467+
if (!ext4_test_inode_state(inode, EXT4_STATE_NEW))
4468+
ext4_fill_raw_inode(inode, raw_inode);
44644469
set_buffer_uptodate(bh);
44654470
unlock_buffer(bh);
44664471
goto has_buffer;
@@ -4521,7 +4526,7 @@ static int __ext4_get_inode_loc_noinmem(struct inode *inode,
45214526
ext4_fsblk_t err_blk;
45224527
int ret;
45234528

4524-
ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0,
4529+
ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, NULL, iloc,
45254530
&err_blk);
45264531

45274532
if (ret == -EIO)
@@ -4536,9 +4541,8 @@ int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
45364541
ext4_fsblk_t err_blk;
45374542
int ret;
45384543

4539-
/* We have all inode data except xattrs in memory here. */
4540-
ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc,
4541-
!ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk);
4544+
ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, inode, iloc,
4545+
&err_blk);
45424546

45434547
if (ret == -EIO)
45444548
ext4_error_inode_block(inode, err_blk, EIO,
@@ -4551,7 +4555,7 @@ int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
45514555
int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
45524556
struct ext4_iloc *iloc)
45534557
{
4554-
return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL);
4558+
return __ext4_get_inode_loc(sb, ino, NULL, iloc, NULL);
45554559
}
45564560

45574561
static bool ext4_should_enable_dax(struct inode *inode)

0 commit comments

Comments
 (0)