mount.c 1.6 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 23
static struct kernfs_root *sysfs_root;
struct sysfs_dirent *sysfs_root_sd;
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;
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
}

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
	sysfs_root = kernfs_create_root(NULL);
66 67 68
	if (IS_ERR(sysfs_root))
		return PTR_ERR(sysfs_root);

69 70
	sysfs_root_sd = sysfs_root->sd;

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

	return 0;
L
Linus Torvalds 已提交
78
}