Skip to content

Commit 202ee5d

Browse files
committed
ext4: add a new spinlock i_raw_lock to protect the ext4's raw inode
To avoid potential data races, use a spinlock which protects the raw (on-disk) inode. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Reviewed-by: Jan Kara <jack@suse.cz>
1 parent f5ccfe1 commit 202ee5d

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,8 @@ struct ext4_inode_info {
875875
struct inode vfs_inode;
876876
struct jbd2_inode *jinode;
877877

878+
spinlock_t i_raw_lock; /* protects updates to the raw inode */
879+
878880
/*
879881
* File creation time. Its function is same as that of
880882
* struct timespec i_{a,c,m}time in the generic inode.

‎fs/ext4/inode.c‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4305,12 +4305,15 @@ static int ext4_do_update_inode(handle_t *handle,
43054305
struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
43064306
struct ext4_inode_info *ei = EXT4_I(inode);
43074307
struct buffer_head *bh = iloc->bh;
4308+
struct super_block *sb = inode->i_sb;
43084309
int err = 0, rc, block;
4309-
int need_datasync = 0;
4310+
int need_datasync = 0, set_large_file = 0;
43104311
uid_t i_uid;
43114312
gid_t i_gid;
43124313

4313-
/* For fields not not tracking in the in-memory inode,
4314+
spin_lock(&ei->i_raw_lock);
4315+
4316+
/* For fields not tracked in the in-memory inode,
43144317
* initialise them to zero for new inodes. */
43154318
if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
43164319
memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
@@ -4348,8 +4351,10 @@ static int ext4_do_update_inode(handle_t *handle,
43484351
EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
43494352
EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
43504353

4351-
if (ext4_inode_blocks_set(handle, raw_inode, ei))
4354+
if (ext4_inode_blocks_set(handle, raw_inode, ei)) {
4355+
spin_unlock(&ei->i_raw_lock);
43524356
goto out_brelse;
4357+
}
43534358
raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
43544359
raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
43554360
if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
@@ -4361,24 +4366,11 @@ static int ext4_do_update_inode(handle_t *handle,
43614366
need_datasync = 1;
43624367
}
43634368
if (ei->i_disksize > 0x7fffffffULL) {
4364-
struct super_block *sb = inode->i_sb;
43654369
if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
43664370
EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
43674371
EXT4_SB(sb)->s_es->s_rev_level ==
4368-
cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4369-
/* If this is the first large file
4370-
* created, add a flag to the superblock.
4371-
*/
4372-
err = ext4_journal_get_write_access(handle,
4373-
EXT4_SB(sb)->s_sbh);
4374-
if (err)
4375-
goto out_brelse;
4376-
ext4_update_dynamic_rev(sb);
4377-
EXT4_SET_RO_COMPAT_FEATURE(sb,
4378-
EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4379-
ext4_handle_sync(handle);
4380-
err = ext4_handle_dirty_super(handle, sb);
4381-
}
4372+
cpu_to_le32(EXT4_GOOD_OLD_REV))
4373+
set_large_file = 1;
43824374
}
43834375
raw_inode->i_generation = cpu_to_le32(inode->i_generation);
43844376
if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
@@ -4410,12 +4402,23 @@ static int ext4_do_update_inode(handle_t *handle,
44104402

44114403
ext4_inode_csum_set(inode, raw_inode, ei);
44124404

4405+
spin_unlock(&ei->i_raw_lock);
4406+
44134407
BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
44144408
rc = ext4_handle_dirty_metadata(handle, NULL, bh);
44154409
if (!err)
44164410
err = rc;
44174411
ext4_clear_inode_state(inode, EXT4_STATE_NEW);
4418-
4412+
if (set_large_file) {
4413+
err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
4414+
if (err)
4415+
goto out_brelse;
4416+
ext4_update_dynamic_rev(sb);
4417+
EXT4_SET_RO_COMPAT_FEATURE(sb,
4418+
EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4419+
ext4_handle_sync(handle);
4420+
err = ext4_handle_dirty_super(handle, sb);
4421+
}
44194422
ext4_update_inode_fsync_trans(handle, inode, need_datasync);
44204423
out_brelse:
44214424
brelse(bh);

‎fs/ext4/super.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ static struct inode *ext4_alloc_inode(struct super_block *sb)
879879
return NULL;
880880

881881
ei->vfs_inode.i_version = 1;
882+
spin_lock_init(&ei->i_raw_lock);
882883
INIT_LIST_HEAD(&ei->i_prealloc_list);
883884
spin_lock_init(&ei->i_prealloc_lock);
884885
ext4_es_init_tree(&ei->i_es_tree);

0 commit comments

Comments
 (0)