Skip to content

Commit 9df9393

Browse files
committed
ext3: Use bitops to read/modify EXT3_I(inode)->i_state
At several places we modify EXT3_I(inode)->i_state without holding i_mutex (ext3_release_file, ext3_bmap, ext3_journalled_writepage, ext3_do_update_inode, ...). These modifications are racy and we can lose updates to i_state. So convert handling of i_state to use bitops which are atomic. Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 26245c9 commit 9df9393

5 files changed

Lines changed: 44 additions & 27 deletions

File tree

‎fs/ext3/file.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
*/
3434
static int ext3_release_file (struct inode * inode, struct file * filp)
3535
{
36-
if (EXT3_I(inode)->i_state & EXT3_STATE_FLUSH_ON_CLOSE) {
36+
if (ext3_test_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE)) {
3737
filemap_flush(inode->i_mapping);
38-
EXT3_I(inode)->i_state &= ~EXT3_STATE_FLUSH_ON_CLOSE;
38+
ext3_clear_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
3939
}
4040
/* if we are the last writer on the inode, drop the block reservation */
4141
if ((filp->f_mode & FMODE_WRITE) &&

‎fs/ext3/inode.c‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ static int ext3_journalled_write_end(struct file *file,
13781378
*/
13791379
if (pos + len > inode->i_size && ext3_can_truncate(inode))
13801380
ext3_orphan_add(handle, inode);
1381-
EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1381+
ext3_set_inode_state(inode, EXT3_STATE_JDATA);
13821382
if (inode->i_size > EXT3_I(inode)->i_disksize) {
13831383
EXT3_I(inode)->i_disksize = inode->i_size;
13841384
ret2 = ext3_mark_inode_dirty(handle, inode);
@@ -1417,7 +1417,7 @@ static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
14171417
journal_t *journal;
14181418
int err;
14191419

1420-
if (EXT3_I(inode)->i_state & EXT3_STATE_JDATA) {
1420+
if (ext3_test_inode_state(inode, EXT3_STATE_JDATA)) {
14211421
/*
14221422
* This is a REALLY heavyweight approach, but the use of
14231423
* bmap on dirty files is expected to be extremely rare:
@@ -1436,7 +1436,7 @@ static sector_t ext3_bmap(struct address_space *mapping, sector_t block)
14361436
* everything they get.
14371437
*/
14381438

1439-
EXT3_I(inode)->i_state &= ~EXT3_STATE_JDATA;
1439+
ext3_clear_inode_state(inode, EXT3_STATE_JDATA);
14401440
journal = EXT3_JOURNAL(inode);
14411441
journal_lock_updates(journal);
14421442
err = journal_flush(journal);
@@ -1670,7 +1670,7 @@ static int ext3_journalled_writepage(struct page *page,
16701670
PAGE_CACHE_SIZE, NULL, write_end_fn);
16711671
if (ret == 0)
16721672
ret = err;
1673-
EXT3_I(inode)->i_state |= EXT3_STATE_JDATA;
1673+
ext3_set_inode_state(inode, EXT3_STATE_JDATA);
16741674
unlock_page(page);
16751675
} else {
16761676
/*
@@ -2402,7 +2402,7 @@ void ext3_truncate(struct inode *inode)
24022402
goto out_notrans;
24032403

24042404
if (inode->i_size == 0 && ext3_should_writeback_data(inode))
2405-
ei->i_state |= EXT3_STATE_FLUSH_ON_CLOSE;
2405+
ext3_set_inode_state(inode, EXT3_STATE_FLUSH_ON_CLOSE);
24062406

24072407
/*
24082408
* We have to lock the EOF page here, because lock_page() nests
@@ -2721,7 +2721,7 @@ int ext3_get_inode_loc(struct inode *inode, struct ext3_iloc *iloc)
27212721
{
27222722
/* We have all inode data except xattrs in memory here. */
27232723
return __ext3_get_inode_loc(inode, iloc,
2724-
!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR));
2724+
!ext3_test_inode_state(inode, EXT3_STATE_XATTR));
27252725
}
27262726

27272727
void ext3_set_inode_flags(struct inode *inode)
@@ -2893,7 +2893,7 @@ struct inode *ext3_iget(struct super_block *sb, unsigned long ino)
28932893
EXT3_GOOD_OLD_INODE_SIZE +
28942894
ei->i_extra_isize;
28952895
if (*magic == cpu_to_le32(EXT3_XATTR_MAGIC))
2896-
ei->i_state |= EXT3_STATE_XATTR;
2896+
ext3_set_inode_state(inode, EXT3_STATE_XATTR);
28972897
}
28982898
} else
28992899
ei->i_extra_isize = 0;
@@ -2955,7 +2955,7 @@ static int ext3_do_update_inode(handle_t *handle,
29552955

29562956
/* For fields not not tracking in the in-memory inode,
29572957
* initialise them to zero for new inodes. */
2958-
if (ei->i_state & EXT3_STATE_NEW)
2958+
if (ext3_test_inode_state(inode, EXT3_STATE_NEW))
29592959
memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
29602960

29612961
ext3_get_inode_flags(ei);
@@ -3052,7 +3052,7 @@ static int ext3_do_update_inode(handle_t *handle,
30523052
rc = ext3_journal_dirty_metadata(handle, bh);
30533053
if (!err)
30543054
err = rc;
3055-
ei->i_state &= ~EXT3_STATE_NEW;
3055+
ext3_clear_inode_state(inode, EXT3_STATE_NEW);
30563056

30573057
atomic_set(&ei->i_sync_tid, handle->h_transaction->t_tid);
30583058
out_brelse:

‎fs/ext3/xattr.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
274274
void *end;
275275
int error;
276276

277-
if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
277+
if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
278278
return -ENODATA;
279279
error = ext3_get_inode_loc(inode, &iloc);
280280
if (error)
@@ -403,7 +403,7 @@ ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
403403
void *end;
404404
int error;
405405

406-
if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
406+
if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
407407
return 0;
408408
error = ext3_get_inode_loc(inode, &iloc);
409409
if (error)
@@ -882,7 +882,7 @@ ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
882882
is->s.base = is->s.first = IFIRST(header);
883883
is->s.here = is->s.first;
884884
is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
885-
if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
885+
if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
886886
error = ext3_xattr_check_names(IFIRST(header), is->s.end);
887887
if (error)
888888
return error;
@@ -914,10 +914,10 @@ ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
914914
header = IHDR(inode, ext3_raw_inode(&is->iloc));
915915
if (!IS_LAST_ENTRY(s->first)) {
916916
header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
917-
EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
917+
ext3_set_inode_state(inode, EXT3_STATE_XATTR);
918918
} else {
919919
header->h_magic = cpu_to_le32(0);
920-
EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
920+
ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
921921
}
922922
return 0;
923923
}
@@ -967,10 +967,10 @@ ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
967967
if (error)
968968
goto cleanup;
969969

970-
if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
970+
if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
971971
struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
972972
memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
973-
EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
973+
ext3_clear_inode_state(inode, EXT3_STATE_NEW);
974974
}
975975

976976
error = ext3_xattr_ibody_find(inode, &i, &is);

‎include/linux/ext3_fs.h‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,6 @@ static inline __u32 ext3_mask_flags(umode_t mode, __u32 flags)
202202
return flags & EXT3_OTHER_FLMASK;
203203
}
204204

205-
/*
206-
* Inode dynamic state flags
207-
*/
208-
#define EXT3_STATE_JDATA 0x00000001 /* journaled data exists */
209-
#define EXT3_STATE_NEW 0x00000002 /* inode is newly created */
210-
#define EXT3_STATE_XATTR 0x00000004 /* has in-inode xattrs */
211-
#define EXT3_STATE_FLUSH_ON_CLOSE 0x00000008
212-
213205
/* Used to pass group descriptor data when online resize is done */
214206
struct ext3_new_group_input {
215207
__u32 group; /* Group number for this data */
@@ -560,6 +552,31 @@ static inline int ext3_valid_inum(struct super_block *sb, unsigned long ino)
560552
(ino >= EXT3_FIRST_INO(sb) &&
561553
ino <= le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count));
562554
}
555+
556+
/*
557+
* Inode dynamic state flags
558+
*/
559+
enum {
560+
EXT3_STATE_JDATA, /* journaled data exists */
561+
EXT3_STATE_NEW, /* inode is newly created */
562+
EXT3_STATE_XATTR, /* has in-inode xattrs */
563+
EXT3_STATE_FLUSH_ON_CLOSE, /* flush dirty pages on close */
564+
};
565+
566+
static inline int ext3_test_inode_state(struct inode *inode, int bit)
567+
{
568+
return test_bit(bit, &EXT3_I(inode)->i_state);
569+
}
570+
571+
static inline void ext3_set_inode_state(struct inode *inode, int bit)
572+
{
573+
set_bit(bit, &EXT3_I(inode)->i_state);
574+
}
575+
576+
static inline void ext3_clear_inode_state(struct inode *inode, int bit)
577+
{
578+
clear_bit(bit, &EXT3_I(inode)->i_state);
579+
}
563580
#else
564581
/* Assume that user mode programs are passing in an ext3fs superblock, not
565582
* a kernel struct super_block. This will allow us to call the feature-test

‎include/linux/ext3_fs_i.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct ext3_inode_info {
8787
* near to their parent directory's inode.
8888
*/
8989
__u32 i_block_group;
90-
__u32 i_state; /* Dynamic state flags for ext3 */
90+
unsigned long i_state; /* Dynamic state flags for ext3 */
9191

9292
/* block reservation info */
9393
struct ext3_block_alloc_info *i_block_alloc_info;

0 commit comments

Comments
 (0)