mount.c 1.7 KB
Newer Older
G
Greg Kroah-Hartman 已提交
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2
/*
T
Tejun Heo 已提交
3 4 5 6 7 8 9
 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
 *
 * Copyright (c) 2001-3 Patrick Mochel
 * Copyright (c) 2007 SUSE Linux Products GmbH
 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
 *
 * Please see Documentation/filesystems/sysfs.txt for more information.
L
Linus Torvalds 已提交
10 11 12
 */

#include <linux/fs.h>
13
#include <linux/magic.h>
L
Linus Torvalds 已提交
14 15
#include <linux/mount.h>
#include <linux/init.h>
16
#include <linux/user_namespace.h>
L
Linus Torvalds 已提交
17 18 19

#include "sysfs.h"

20
static struct kernfs_root *sysfs_root;
21
struct kernfs_node *sysfs_root_kn;
T
Tejun Heo 已提交
22

A
Al Viro 已提交
23 24
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
25
{
26 27
	struct dentry *root;
	void *ns;
L
Li Zefan 已提交
28
	bool new_sb;
29

30
	if (!(flags & SB_KERNMOUNT)) {
31 32
		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
			return ERR_PTR(-EPERM);
33
	}
34

35
	ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
36 37
	root = kernfs_mount_ns(fs_type, flags, sysfs_root,
				SYSFS_MAGIC, &new_sb, ns);
L
Li Zefan 已提交
38
	if (IS_ERR(root) || !new_sb)
39
		kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
40
	else if (new_sb)
41
		root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
42

43 44 45
	return root;
}

46
static void sysfs_kill_sb(struct super_block *sb)
47
{
48 49
	void *ns = (void *)kernfs_super_ns(sb);

50
	kernfs_kill_sb(sb);
51
	kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
52 53
}

L
Linus Torvalds 已提交
54 55
static struct file_system_type sysfs_fs_type = {
	.name		= "sysfs",
A
Al Viro 已提交
56
	.mount		= sysfs_mount,
57
	.kill_sb	= sysfs_kill_sb,
58
	.fs_flags	= FS_USERNS_MOUNT,
L
Linus Torvalds 已提交
59 60
};

61 62 63
int __init sysfs_init(void)
{
	int err;
P
Peter Zijlstra 已提交
64

65 66
	sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
					NULL);
67 68 69
	if (IS_ERR(sysfs_root))
		return PTR_ERR(sysfs_root);

70
	sysfs_root_kn = sysfs_root->kn;
71

L
Linus Torvalds 已提交
72
	err = register_filesystem(&sysfs_fs_type);
73 74 75 76
	if (err) {
		kernfs_destroy_root(sysfs_root);
		return err;
	}
77 78

	return 0;
L
Linus Torvalds 已提交
79
}