mount.c 1.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
T
Tejun Heo 已提交
2 3 4 5 6 7 8 9 10
 * 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>
 *
 * This file is released under the GPLv2.
 *
 * Please see Documentation/filesystems/sysfs.txt for more information.
L
Linus Torvalds 已提交
11 12
 */

13
#define DEBUG
L
Linus Torvalds 已提交
14 15 16 17

#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/init.h>
18
#include <linux/user_namespace.h>
L
Linus Torvalds 已提交
19 20 21

#include "sysfs.h"

22
static struct kernfs_root *sysfs_root;
23
struct kernfs_node *sysfs_root_kn;
T
Tejun Heo 已提交
24

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

32 33 34 35
	if (!(flags & MS_KERNMOUNT)) {
		if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
			return ERR_PTR(-EPERM);

36 37
		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
			return ERR_PTR(-EPERM);
38
	}
39

40
	ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
L
Li Zefan 已提交
41 42
	root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
	if (IS_ERR(root) || !new_sb)
43 44 45 46
		kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
	return root;
}

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

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

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

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

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

71
	sysfs_root_kn = sysfs_root->kn;
72

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

	return 0;
L
Linus Torvalds 已提交
80
}