f2fs: introduce sb_lock to make encrypt pwsalt update exclusive

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>
This commit is contained in:
Chao Yu 2018-02-11 22:53:20 +08:00 committed by Jaegeuk Kim
parent 8fe326cb99
commit d0d3f1b329
3 changed files with 14 additions and 8 deletions

View file

@ -1047,6 +1047,7 @@ struct f2fs_sb_info {
struct super_block *sb; /* pointer to VFS super block */ struct super_block *sb; /* pointer to VFS super block */
struct proc_dir_entry *s_proc; /* proc entry */ struct proc_dir_entry *s_proc; /* proc entry */
struct f2fs_super_block *raw_super; /* raw super block pointer */ 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 */ int valid_super_block; /* valid super block no */
unsigned long s_flag; /* flags for sbi */ unsigned long s_flag; /* flags for sbi */

View file

@ -1962,13 +1962,15 @@ static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
if (!f2fs_sb_has_encrypt(inode->i_sb)) if (!f2fs_sb_has_encrypt(inode->i_sb))
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
goto got_it;
err = mnt_want_write_file(filp); err = mnt_want_write_file(filp);
if (err) if (err)
return 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 */ /* update superblock with uuid */
generate_random_uuid(sbi->raw_super->encrypt_pw_salt); generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
@ -1976,15 +1978,16 @@ static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
if (err) { if (err) {
/* undo new data */ /* undo new data */
memset(sbi->raw_super->encrypt_pw_salt, 0, 16); memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
mnt_drop_write_file(filp); goto out_err;
return err;
} }
mnt_drop_write_file(filp);
got_it: got_it:
if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt, if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
16)) 16))
return -EFAULT; err = -EFAULT;
return 0; 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) static int f2fs_ioc_gc(struct file *filp, unsigned long arg)

View file

@ -2221,6 +2221,8 @@ static void init_sb_info(struct f2fs_sb_info *sbi)
sbi->dirty_device = 0; sbi->dirty_device = 0;
spin_lock_init(&sbi->dev_lock); spin_lock_init(&sbi->dev_lock);
mutex_init(&sbi->sb_lock);
} }
static int init_percpu_info(struct f2fs_sb_info *sbi) static int init_percpu_info(struct f2fs_sb_info *sbi)