f2fs: compress: fix to check compress flag w/ .i_sem lock
authorChao Yu <chao@kernel.org>
Mon, 19 Feb 2024 02:28:44 +0000 (10:28 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Thu, 29 Feb 2024 16:34:34 +0000 (08:34 -0800)
It needs to check compress flag w/ .i_sem lock, otherwise, compressed
inode may be disabled after the check condition, it's not needed to
set compress option on non-compress inode.

Fixes: e1e8debec656 ("f2fs: add F2FS_IOC_SET_COMPRESS_OPTION ioctl")
Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/file.c

index 6371bd33bf6db2704d8b0f99edc44a2f331a67f2..1c044fb19c87d83c984099a83857c83373d71831 100644 (file)
@@ -4028,16 +4028,20 @@ static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
                                sizeof(option)))
                return -EFAULT;
 
-       if (!f2fs_compressed_file(inode) ||
-                       option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
-                       option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
-                       option.algorithm >= COMPRESS_MAX)
+       if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
+               option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
+               option.algorithm >= COMPRESS_MAX)
                return -EINVAL;
 
        file_start_write(filp);
        inode_lock(inode);
 
        f2fs_down_write(&F2FS_I(inode)->i_sem);
+       if (!f2fs_compressed_file(inode)) {
+               ret = -EINVAL;
+               goto out;
+       }
+
        if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
                ret = -EBUSY;
                goto out;