[PATCH 14/16] vfs: Implement mount_super_once

From: Eric W. Biederman
Date: Fri Apr 15 2016 - 11:50:24 EST


The devpts filesystem has a notion of a system or primary instance of
devpts. To retain the notion of a primary system instance of devpts
the code needs a way to allow userspace to mount the internally
mounted instance of devpts when it is not currently mounted by
userspace. The new helper mount_super_once allows that.

The function mount_super_once ignores vfsmounts that are in use but
are not mounted in userspace. The set of ignored mounts includes
internal mounts, lazily unmounted instances of mounts, and
loopback/bind mounts show root directory is not a filesystems root
directory.

Furthermore mount_super_once only allows exporting a mount to
userspace if it can account for every reference in s_active
on the superblock. Ensuring races do not allow two simultaneous
mount requestions export the same filesystem to userspace.

Signed-off-by: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
---
fs/super.c | 34 ++++++++++++++++++++++++++++++++++
include/linux/fs.h | 2 ++
2 files changed, 36 insertions(+)

diff --git a/fs/super.c b/fs/super.c
index 74914b1bae70..4a6552395fad 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -33,6 +33,7 @@
#include <linux/cleancache.h>
#include <linux/fsnotify.h>
#include <linux/lockdep.h>
+#include "mount.h"
#include "internal.h"


@@ -1101,6 +1102,39 @@ struct dentry *mount_single(struct file_system_type *fs_type,
}
EXPORT_SYMBOL(mount_single);

+struct dentry *mount_super_once(struct super_block *sb, int flags, void *data)
+{
+ /* Allow mounting the specified superblock by userspace if there
+ * are not any existing userspace mounts of it.
+ */
+ struct mount *mnt;
+ int count = 0;
+
+ /* Walk through the existing mounts of this superblock. Fail
+ * if any of those mounts are exported to userspace. Otherwise
+ * increment s_active if the superblock s_active count matches
+ * the number of mounts in the list. This ensures that multiple
+ * simultaneous calls to mount_super_once do not race and result
+ * in the filesystem exported to userspace multiple times.
+ */
+ lock_mount_hash();
+ list_for_each_entry(mnt, &sb->s_mounts, mnt_instance) {
+ if (is_mounted(&mnt->mnt) && (mnt->mnt.mnt_root == sb->s_root)) {
+ unlock_mount_hash();
+ return ERR_PTR(-EBUSY);
+ }
+ count++;
+ }
+ if (atomic_cmpxchg(&sb->s_active, count, count + 1) != count) {
+ unlock_mount_hash();
+ return ERR_PTR(-EBUSY);
+ }
+ unlock_mount_hash();
+ down_write(&sb->s_umount);
+ do_remount_sb(sb, flags, data, 0);
+ return dget(sb->s_root);
+}
+
struct dentry *
mount_fs(struct file_system_type *type, int flags, const char *name, void *data)
{
diff --git a/include/linux/fs.h b/include/linux/fs.h
index aade033bed49..465c1155bf72 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -2002,6 +2002,8 @@ extern struct dentry *mount_single(struct file_system_type *fs_type,
extern struct dentry *mount_nodev(struct file_system_type *fs_type,
int flags, void *data,
int (*fill_super)(struct super_block *, void *, int));
+extern struct dentry *mount_super_once(struct super_block *sb,
+ int flags, void *data);
extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
void generic_shutdown_super(struct super_block *sb);
void kill_block_super(struct super_block *sb);
--
2.8.1