f2fs: introduce sb_lock to make encrypt pwsalt update exclusive
authorChao Yu <yuchao0@huawei.com>
Sun, 11 Feb 2018 14:53:20 +0000 (22:53 +0800)
committerJaegeuk Kim <jaegeuk@kernel.org>
Sun, 8 Apr 2018 11:11:21 +0000 (04:11 -0700)
f2fs_super_block.encrypt_pw_salt can be udpated and persisted
concurrently, result in getting different pwsalt in separated
threads, so let's introduce sb_lock to exclude concurrent
accessers.

Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
fs/f2fs/f2fs.h
fs/f2fs/file.c
fs/f2fs/super.c

index 25a1f1aa18723e5010dc3a67119f256ed1f7d36e..b763502b063e60e6e8d52766381861760212a130 100644 (file)
@@ -1047,6 +1047,7 @@ struct f2fs_sb_info {
        struct super_block *sb;                 /* pointer to VFS super block */
        struct proc_dir_entry *s_proc;          /* proc entry */
        struct f2fs_super_block *raw_super;     /* raw super block pointer */
+       struct mutex sb_lock;                   /* lock for raw super block */
        int valid_super_block;                  /* valid super block no */
        unsigned long s_flag;                           /* flags for sbi */
 
index de5053f348107b8d96fb898a25abfb99b7bb1624..589603a48fe931da90f2e2c0a464018e8b91dea1 100644 (file)
@@ -1961,13 +1961,15 @@ static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
        if (!f2fs_sb_has_encrypt(inode->i_sb))
                return -EOPNOTSUPP;
 
-       if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
-               goto got_it;
-
        err = mnt_want_write_file(filp);
        if (err)
                return err;
 
+       mutex_lock(&sbi->sb_lock);
+
+       if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
+               goto got_it;
+
        /* update superblock with uuid */
        generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
 
@@ -1975,15 +1977,16 @@ static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
        if (err) {
                /* undo new data */
                memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
-               mnt_drop_write_file(filp);
-               return err;
+               goto out_err;
        }
-       mnt_drop_write_file(filp);
 got_it:
        if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
                                                                        16))
-               return -EFAULT;
-       return 0;
+               err = -EFAULT;
+out_err:
+       mutex_unlock(&sbi->sb_lock);
+       mnt_drop_write_file(filp);
+       return err;
 }
 
 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
index 00f055c58c99ec9324ae771dc3d59ff0513ee8bc..be7274262bbc7565fadb1c6b422a1820aa8faf59 100644 (file)
@@ -2221,6 +2221,8 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
 
        sbi->dirty_device = 0;
        spin_lock_init(&sbi->dev_lock);
+
+       mutex_init(&sbi->sb_lock);
 }
 
 static int init_percpu_info(struct f2fs_sb_info *sbi)