Skip to content

Commit b5ab327

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to avoid mmap vs set_compress_option case
Compression option in inode should not be changed after they have been used, however, it may happen in below race case: Thread A Thread B - f2fs_ioc_set_compress_option - check f2fs_is_mmap_file() - check get_dirty_pages() - check F2FS_HAS_BLOCKS() - f2fs_file_mmap - set_inode_flag(FI_MMAP_FILE) - fault - do_page_mkwrite - f2fs_vm_page_mkwrite - f2fs_get_block_locked - fault_dirty_shared_page - set_page_dirty - update i_compress_algorithm - update i_log_cluster_size - update i_cluster_size Avoid such race condition by covering f2fs_file_mmap() w/ i_sem lock, meanwhile add mmap file check condition in f2fs_may_compress() as well. Fixes: e1e8deb ("f2fs: add F2FS_IOC_SET_COMPRESS_OPTION ioctl") Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent c709d09 commit b5ab327

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

fs/f2fs/f2fs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4483,7 +4483,8 @@ static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
44834483
static inline bool f2fs_may_compress(struct inode *inode)
44844484
{
44854485
if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
4486-
f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode))
4486+
f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode) ||
4487+
f2fs_is_mmap_file(inode))
44874488
return false;
44884489
return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
44894490
}

fs/f2fs/file.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,11 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
526526

527527
file_accessed(file);
528528
vma->vm_ops = &f2fs_file_vm_ops;
529+
530+
f2fs_down_read(&F2FS_I(inode)->i_sem);
529531
set_inode_flag(inode, FI_MMAP_FILE);
532+
f2fs_up_read(&F2FS_I(inode)->i_sem);
533+
530534
return 0;
531535
}
532536

@@ -1919,12 +1923,19 @@ static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
19191923
int err = f2fs_convert_inline_inode(inode);
19201924
if (err)
19211925
return err;
1922-
if (!f2fs_may_compress(inode))
1923-
return -EINVAL;
1924-
if (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))
1926+
1927+
f2fs_down_write(&F2FS_I(inode)->i_sem);
1928+
if (!f2fs_may_compress(inode) ||
1929+
(S_ISREG(inode->i_mode) &&
1930+
F2FS_HAS_BLOCKS(inode))) {
1931+
f2fs_up_write(&F2FS_I(inode)->i_sem);
19251932
return -EINVAL;
1926-
if (set_compress_context(inode))
1927-
return -EOPNOTSUPP;
1933+
}
1934+
err = set_compress_context(inode);
1935+
f2fs_up_write(&F2FS_I(inode)->i_sem);
1936+
1937+
if (err)
1938+
return err;
19281939
}
19291940
}
19301941

@@ -3976,6 +3987,7 @@ static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
39763987
file_start_write(filp);
39773988
inode_lock(inode);
39783989

3990+
f2fs_down_write(&F2FS_I(inode)->i_sem);
39793991
if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
39803992
ret = -EBUSY;
39813993
goto out;
@@ -3995,6 +4007,7 @@ static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
39954007
f2fs_warn(sbi, "compression algorithm is successfully set, "
39964008
"but current kernel doesn't support this algorithm.");
39974009
out:
4010+
f2fs_up_write(&F2FS_I(inode)->i_sem);
39984011
inode_unlock(inode);
39994012
file_end_write(filp);
40004013

0 commit comments

Comments
 (0)