mount.c 1.65 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Tejun Heo's avatar
Tejun Heo committed
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.
Linus Torvalds's avatar
Linus Torvalds committed
11 12
 */

13
#define DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
14 15 16 17

#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/init.h>
18
#include <linux/user_namespace.h>
Linus Torvalds's avatar
Linus Torvalds committed
19 20 21

#include "sysfs.h"

22
static struct kernfs_root *sysfs_root;
23
struct kernfs_node *sysfs_root_kn;
24

Al Viro's avatar
Al Viro committed
25 26
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
Linus Torvalds's avatar
Linus Torvalds committed
27
{
28 29
	struct dentry *root;
	void *ns;
30

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

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

39 40 41 42 43 44 45
	ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
	root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
	if (IS_ERR(root))
		kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
	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
}

Linus Torvalds's avatar
Linus Torvalds committed
54 55
static struct file_system_type sysfs_fs_type = {
	.name		= "sysfs",
Al Viro's avatar
Al Viro committed
56
	.mount		= sysfs_mount,
57
	.kill_sb	= sysfs_kill_sb,
58
	.fs_flags	= FS_USERNS_MOUNT,
Linus Torvalds's avatar
Linus Torvalds committed
59 60
};

61 62 63
int __init sysfs_init(void)
{
	int err;
Peter Zijlstra's avatar
Peter Zijlstra committed
64

Tejun Heo's avatar
Tejun Heo committed
65
	sysfs_root = kernfs_create_root(NULL, NULL);
66 67 68
	if (IS_ERR(sysfs_root))
		return PTR_ERR(sysfs_root);

69
	sysfs_root_kn = sysfs_root->kn;
70

Linus Torvalds's avatar
Linus Torvalds committed
71
	err = register_filesystem(&sysfs_fs_type);
72 73 74 75
	if (err) {
		kernfs_destroy_root(sysfs_root);
		return err;
	}
76 77

	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
78
}