mount.c 3.9 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 18

#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/init.h>
19
#include <linux/module.h>
20
#include <linux/magic.h>
21
#include <linux/slab.h>
22
#include <linux/user_namespace.h>
L
Linus Torvalds 已提交
23 24 25 26

#include "sysfs.h"


27
struct kmem_cache *sysfs_dir_cachep;
L
Linus Torvalds 已提交
28

29
static const struct super_operations sysfs_ops = {
L
Linus Torvalds 已提交
30
	.statfs		= simple_statfs,
31
	.drop_inode	= generic_delete_inode,
A
Al Viro 已提交
32
	.evict_inode	= sysfs_evict_inode,
L
Linus Torvalds 已提交
33 34
};

T
Tejun Heo 已提交
35
static struct sysfs_dirent sysfs_root = {
36
	.s_name		= "",
T
Tejun Heo 已提交
37
	.s_count	= ATOMIC_INIT(1),
38
	.s_flags	= SYSFS_DIR,
39
	.s_mode		= S_IFDIR | S_IRUGO | S_IXUGO,
40
	.s_ino		= 1,
L
Linus Torvalds 已提交
41 42
};

T
Tejun Heo 已提交
43 44
struct sysfs_dirent *sysfs_root_sd = &sysfs_root;

45
static int sysfs_fill_super(struct super_block *sb)
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55
{
	struct inode *inode;
	struct dentry *root;

	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = SYSFS_MAGIC;
	sb->s_op = &sysfs_ops;
	sb->s_time_gran = 1;

56
	/* get root inode, initialize and unlock it */
57
	mutex_lock(&sysfs_mutex);
T
Tejun Heo 已提交
58
	inode = sysfs_get_inode(sb, sysfs_root_sd);
59
	mutex_unlock(&sysfs_mutex);
60
	if (!inode) {
L
Linus Torvalds 已提交
61 62 63 64
		pr_debug("sysfs: could not get root inode\n");
		return -ENOMEM;
	}

65
	/* instantiate and link root dentry */
66
	root = d_make_root(inode);
L
Linus Torvalds 已提交
67
	if (!root) {
68
		pr_debug("%s: could not get root dentry!\n", __func__);
L
Linus Torvalds 已提交
69 70
		return -ENOMEM;
	}
T
Tejun Heo 已提交
71
	root->d_fsdata = sysfs_root_sd;
L
Linus Torvalds 已提交
72
	sb->s_root = root;
73
	sb->s_d_op = &sysfs_dentry_ops;
L
Linus Torvalds 已提交
74 75 76
	return 0;
}

77 78 79 80
static int sysfs_test_super(struct super_block *sb, void *data)
{
	struct sysfs_super_info *sb_info = sysfs_info(sb);
	struct sysfs_super_info *info = data;
81

82
	return sb_info->ns == info->ns;
83 84 85 86 87 88 89 90 91 92 93
}

static int sysfs_set_super(struct super_block *sb, void *data)
{
	int error;
	error = set_anon_super(sb, data);
	if (!error)
		sb->s_fs_info = data;
	return error;
}

94 95
static void free_sysfs_super_info(struct sysfs_super_info *info)
{
96
	kobj_ns_drop(KOBJ_NS_TYPE_NET, (void *)info->ns);
97 98 99
	kfree(info);
}

A
Al Viro 已提交
100 101
static struct dentry *sysfs_mount(struct file_system_type *fs_type,
	int flags, const char *dev_name, void *data)
L
Linus Torvalds 已提交
102
{
103 104 105 106
	struct sysfs_super_info *info;
	struct super_block *sb;
	int error;

107 108 109 110
	if (!(flags & MS_KERNMOUNT)) {
		if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
			return ERR_PTR(-EPERM);

111 112
		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
			return ERR_PTR(-EPERM);
113
	}
114

115 116
	info = kzalloc(sizeof(*info), GFP_KERNEL);
	if (!info)
A
Al Viro 已提交
117
		return ERR_PTR(-ENOMEM);
118

119
	info->ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
120

D
David Howells 已提交
121
	sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
122
	if (IS_ERR(sb) || sb->s_fs_info != info)
123
		free_sysfs_super_info(info);
A
Al Viro 已提交
124 125
	if (IS_ERR(sb))
		return ERR_CAST(sb);
126
	if (!sb->s_root) {
127
		error = sysfs_fill_super(sb);
128 129
		if (error) {
			deactivate_locked_super(sb);
A
Al Viro 已提交
130
			return ERR_PTR(error);
131 132 133 134
		}
		sb->s_flags |= MS_ACTIVE;
	}

A
Al Viro 已提交
135
	return dget(sb->s_root);
136 137 138 139 140
}

static void sysfs_kill_sb(struct super_block *sb)
{
	struct sysfs_super_info *info = sysfs_info(sb);
141 142 143
	/* Remove the superblock from fs_supers/s_instances
	 * so we can't find it, before freeing sysfs_super_info.
	 */
144
	kill_anon_super(sb);
145
	free_sysfs_super_info(info);
L
Linus Torvalds 已提交
146 147 148 149
}

static struct file_system_type sysfs_fs_type = {
	.name		= "sysfs",
A
Al Viro 已提交
150
	.mount		= sysfs_mount,
151
	.kill_sb	= sysfs_kill_sb,
152
	.fs_flags	= FS_USERNS_MOUNT,
L
Linus Torvalds 已提交
153 154 155 156
};

int __init sysfs_init(void)
{
157
	int err;
L
Linus Torvalds 已提交
158 159 160

	sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
					      sizeof(struct sysfs_dirent),
161
					      0, 0, NULL);
L
Linus Torvalds 已提交
162
	if (!sysfs_dir_cachep)
163
		return -ENOMEM;
L
Linus Torvalds 已提交
164

P
Peter Zijlstra 已提交
165 166 167 168
	err = sysfs_inode_init();
	if (err)
		goto out_err;

L
Linus Torvalds 已提交
169
	err = register_filesystem(&sysfs_fs_type);
170
	if (err)
L
Linus Torvalds 已提交
171
		goto out_err;
172 173 174

	return 0;

L
Linus Torvalds 已提交
175 176 177
out_err:
	kmem_cache_destroy(sysfs_dir_cachep);
	sysfs_dir_cachep = NULL;
178
	return err;
L
Linus Torvalds 已提交
179
}