f2fs: remove race condition in releasing cblocks
PATCH 1/1 f2fs: remove race condition in releasing cblocks
From: Daeho Jeong <[email protected]>
Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged.
We have to execute releasing compress blocks only one time for a file without being interfered by writepages path.
--- fs/f2fs/file.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/file.c +24 -7
@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)pgoff_t page_idx =0 , last_idx;unsigned int released_blocks =0 ;int ret; +int writecount;if (!f2fs_sb_has_compression (F2FS_I_SB (inode)))return -EOPNOTSUPP ;
@@ -3509,13 +3510,29 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)inode_lock (inode); -if (!IS_IMMUTABLE (inode)) { -F2FS_I (inode)->i_flags |=F2FS_IMMUTABLE_FL ; -f2fs_set_inode_flags (inode); - inode->i_ctime =current_time (inode); -f2fs_mark_inode_dirty_sync (inode, true); + writecount =atomic_read (&inode->i_writecount); +if ((filp->f_mode &FMODE_WRITE && writecount !=1 ) || writecount) { + ret = -EBUSY ; + goto out; } +if (IS_IMMUTABLE (inode)) { + ret = -EINVAL ; + goto out; + } + + ret =filemap_write_and_wait_range (inode->i_mapping,0 ,LLONG_MAX ); +if (ret) + goto out; + +if (!F2FS_I (inode)->i_compr_blocks) + goto out; + +F2FS_I (inode)->i_flags |=F2FS_IMMUTABLE_FL ; +f2fs_set_inode_flags (inode); + inode->i_ctime =current_time (inode); +f2fs_mark_inode_dirty_sync (inode, true); +down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);down_write (&F2FS_I (inode)->i_mmap_sem);
@@ -3554,9 +3571,9 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);up_write (&F2FS_I (inode)->i_mmap_sem); - -inode_unlock (inode); out: +inode_unlock (inode); +mnt_drop_write_file (filp);if (ret >=0 ) {
Signature · 1 line
2.26.2.526.g744177e7f7-goog
Quoted 20 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged.
We have to execute releasing compress blocks only one time for a file without being interfered by writepages path.
Signed-off-by Daeho Jeong <[email protected]>--- fs/f2fs/file.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)fs/f2fs/file.c +0
-0
fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3494
Quoted 7 lines
fs/f2fs/file.c +1
-0@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)pgoff_t page_idx =0 , last_idx;unsigned int released_blocks =0 ;int ret; +int writecount;if (!f2fs_sb_has_compression (F2FS_I_SB (inode)))return -EOPNOTSUPP ;
Before inode_lock(), there is one case we may jump to out label, in this case, we may unlock inode incorrectly.
if (!F2FS_I(inode)->i_compr_blocks) goto out;
fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3577
Quoted 6 lines
fs/f2fs/file.c +2
-2- -inode_unlock (inode); out: +inode_unlock (inode); +mnt_drop_write_file (filp);
Thanks,
I moved checking i_compr_blocks phrase after calling inode_lock() already, because we should check this after writing pages.
So, if it fails to check i_compr_blocks, we need to call inode_unlock().
Am I missing something?
2020년 5월 8일 (금) 오후 3:50, Chao Yu <[email protected]>님이 작성:
Quoted 44 lines · Daeho Jeong
Quoted 20 lines
From: Daeho Jeong <[email protected]>
Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged.
We have to execute releasing compress blocks only one time for a file without being interfered by writepages path.
Signed-off-by Daeho Jeong <[email protected]>--- fs/f2fs/file.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)fs/f2fs/file.c +0
-0fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3494
Quoted 7 lines
fs/f2fs/file.c +1
-0@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)pgoff_t page_idx =0 , last_idx;unsigned int released_blocks =0 ;int ret; +int writecount;if (!f2fs_sb_has_compression (F2FS_I_SB (inode)))return -EOPNOTSUPP ;Before
inode_lock(), there is one case we may jump to out label, in this case, we may unlock inode incorrectly.if (!F2FS_I(inode)->i_compr_blocks) goto out;fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3577
Quoted 6 lines
fs/f2fs/file.c +2
-2- -inode_unlock (inode); out: +inode_unlock (inode); +mnt_drop_write_file (filp);Thanks,
Quoted 6 lines · Daeho Jeong
I moved checking
i_compr_blocksphrase after callinginode_lock()already, because we should check this after writing pages.So, if it fails to check
i_compr_blocks, we need to callinode_unlock().Am I missing something?
After applying this patch, I get this:
ret = mnt_want_write_file(filp); if (ret) return ret; if (!F2FS_I(inode)->i_compr_blocks) goto out; f2fs_balance_fs(F2FS_I_SB(inode), true); inode_lock(inode);
Quoted 48 lines
2020년 5월 8일 (금) 오후 3:50, Chao Yu <[email protected]>님이 작성:
Quoted 36 lines · Daeho Jeong
Quoted 20 lines
From: Daeho Jeong <[email protected]>
Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged.
We have to execute releasing compress blocks only one time for a file without being interfered by writepages path.
Signed-off-by Daeho Jeong <[email protected]>--- fs/f2fs/file.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)fs/f2fs/file.c +0
-0fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3494
Quoted 7 lines
fs/f2fs/file.c +1
-0@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)pgoff_t page_idx =0 , last_idx;unsigned int released_blocks =0 ;int ret; +int writecount;if (!f2fs_sb_has_compression (F2FS_I_SB (inode)))return -EOPNOTSUPP ;Before
inode_lock(), there is one case we may jump to out label, in this case, we may unlock inode incorrectly.if (!F2FS_I(inode)->i_compr_blocks) goto out;Quoted 8 lines
fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3577
Quoted 6 lines
fs/f2fs/file.c +2
-2- -inode_unlock (inode); out: +inode_unlock (inode); +mnt_drop_write_file (filp);Thanks,
.
Oops,
I will re-check it.
Thanks,
2020년 5월 8일 (금) 오후 4:09, Chao Yu <[email protected]>님이 작성:
Quoted 70 lines · Daeho Jeong
Quoted 6 lines
I moved checking
i_compr_blocksphrase after callinginode_lock()already, because we should check this after writing pages.So, if it fails to check
i_compr_blocks, we need to callinode_unlock().Am I missing something?
After applying this patch, I get this:
ret = mnt_want_write_file(filp); if (ret) return ret; if (!F2FS_I(inode)->i_compr_blocks) goto out; f2fs_balance_fs(F2FS_I_SB(inode), true); inode_lock(inode);Quoted 48 lines
2020년 5월 8일 (금) 오후 3:50, Chao Yu <[email protected]>님이 작성:
Quoted 36 lines · Daeho Jeong
Quoted 20 lines
From: Daeho Jeong <[email protected]>
Now, if writing pages and releasing compress blocks occur simultaneously, and releasing cblocks is executed more than one time to a file, then total block count of filesystem and block count of the file could be incorrect and damaged.
We have to execute releasing compress blocks only one time for a file without being interfered by writepages path.
Signed-off-by Daeho Jeong <[email protected]>--- fs/f2fs/file.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-)fs/f2fs/file.c +0
-0fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3494
Quoted 7 lines
fs/f2fs/file.c +1
-0@@ -3488,6 +3488,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)pgoff_t page_idx =0 , last_idx;unsigned int released_blocks =0 ;int ret; +int writecount;if (!f2fs_sb_has_compression (F2FS_I_SB (inode)))return -EOPNOTSUPP ;Before
inode_lock(), there is one case we may jump to out label, in this case, we may unlock inode incorrectly.if (!F2FS_I(inode)->i_compr_blocks) goto out;Quoted 8 lines
fs/f2fs/file.c static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) · line 3577
Quoted 6 lines
fs/f2fs/file.c +2
-2- -inode_unlock (inode); out: +inode_unlock (inode); +mnt_drop_write_file (filp);Thanks,
.