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

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

#include "sysfs.h"

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

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

33
	if (!(flags & MS_KERNMOUNT)) {
34 35
		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
			return ERR_PTR(-EPERM);
36
	}
37

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

46 47 48
	return root;
}

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

53
	kernfs_kill_sb(sb);
54
	kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
55 56
}

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

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

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

73
	sysfs_root_kn = sysfs_root->kn;
74

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

	return 0;
L
Linus Torvalds 已提交
82
}