From 18c7913475342f10b4723c7e22409acc573e6436 Mon Sep 17 00:00:00 2001 From: Jan Kara Date: Tue, 18 Feb 2020 10:05:21 +0100 Subject: [PATCH] fs: Provide functions for getting locked and thawed superblock from a path Provide functions to get locked (with s_umount) and if desired also thawed superblock for a given struct path. We additionally also get passive reference count of the superblock so that the superblock can be unlocked with put_super() / put_super_exlusive() similarly to how get_super_*() class of functions operates. Signed-off-by: Jan Kara --- fs/super.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 3 +++ 2 files changed, 67 insertions(+) diff --git a/fs/super.c b/fs/super.c index cd352530eca9..2c78eac57fa5 100644 --- a/fs/super.c +++ b/fs/super.c @@ -836,6 +836,70 @@ struct super_block *get_super_exclusive_thawed(struct block_device *bdev) } EXPORT_SYMBOL(get_super_exclusive_thawed); +static struct super_block *__hold_super(struct path *path, bool excl, + bool thawed) +{ + struct super_block *sb = path->mnt->mnt_sb; + + while (1) { + if (excl) + down_write(&sb->s_umount); + else + down_read(&sb->s_umount); + if (!thawed || sb->s_writers.frozen == SB_UNFROZEN) { + spin_lock(&sb_lock); + sb->s_count++; + spin_unlock(&sb_lock); + return sb; + } + if (excl) + up_write(&sb->s_umount); + else + up_read(&sb->s_umount); + wait_event(sb->s_writers.wait_unfrozen, + sb->s_writers.frozen == SB_UNFROZEN); + } +} + +/** + * hold_super - get locked superblock for a path + * @path: path to get superblock for + * + * This function gets superblock for @path and returns it with s_umount + * held in shared mode and superblock's passive refcount (sb->s_count) + * incremented. + */ +struct super_block *hold_super(struct path *path) +{ + return __hold_super(path, false, false); +} + +/** + * hold_super_thawed - get locked thawed superblock for a path + * @path: path to get superblock for + * + * This function gets superblock for @path, makes sure it is not frozen, + * and returns it with s_umount held in shared mode and superblock's + * passive refcount (sb->s_count) incremented. + */ +struct super_block *hold_super_thawed(struct path *path) +{ + return __hold_super(path, false, true); +} + +/** + * hold_super_thawed_exclusive - get locked thawed superblock for a path + * @path: path to get superblock for + * + * This function gets superblock for @path, makes sure it is not frozen, + * and returns it with s_umount held in exclusive mode and superblock's + * passive refcount (sb->s_count) incremented. + */ +struct super_block *hold_super_thawed_exclusive(struct path *path) +{ + return __hold_super(path, true, true); +} + /** * get_active_super - get an active reference to the superblock of a device * @bdev: device to get the superblock for diff --git a/include/linux/fs.h b/include/linux/fs.h index 3cd4fe6b845e..2406494d2f54 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3305,6 +3305,9 @@ extern struct file_system_type *get_fs_type(const char *name); extern struct super_block *get_super(struct block_device *); extern struct super_block *get_super_thawed(struct block_device *); extern struct super_block *get_super_exclusive_thawed(struct block_device *bdev); +struct super_block *hold_super(struct path *path); +struct super_block *hold_super_thawed(struct path *path); +struct super_block *hold_super_thawed_exclusive(struct path *path); extern struct super_block *get_active_super(struct block_device *bdev); extern void drop_super(struct super_block *sb); extern void drop_super_exclusive(struct super_block *sb); -- 2.16.4