blob: 5384732700ba66980cf589ef15f1bd8bc37d930d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
3 *
4 * Copyright (c) 2001-3 Patrick Mochel
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 *
8 * This file is released under the GPLv2.
9 *
10 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Bin Wang6b8fbde2012-12-06 17:08:56 +080013#define DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/fs.h>
16#include <linux/mount.h>
17#include <linux/pagemap.h>
18#include <linux/init.h>
Neil Brownf1282c82008-07-16 08:58:04 +100019#include <linux/module.h>
Qinghuang Feng8231f2f2009-01-14 15:45:13 +080020#include <linux/magic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070022#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "sysfs.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Christoph Lametere18b8902006-12-06 20:33:20 -080027struct kmem_cache *sysfs_dir_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080029static const struct super_operations sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .statfs = simple_statfs,
Eric W. Biederman90bc6132007-07-31 19:15:08 +090031 .drop_inode = generic_delete_inode,
Al Viro01cd9fe2010-06-04 22:21:54 -040032 .evict_inode = sysfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Tejun Heoba7443b2013-11-28 14:54:40 -050035static struct kernfs_root *sysfs_root;
36struct sysfs_dirent *sysfs_root_sd;
Tejun Heo061447a2013-11-28 14:54:39 -050037
Tejun Heoccc532d2013-11-28 14:54:36 -050038static int sysfs_fill_super(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Tejun Heodf394fb2013-11-28 14:54:42 -050040 struct sysfs_super_info *info = sysfs_info(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct inode *inode;
42 struct dentry *root;
43
44 sb->s_blocksize = PAGE_CACHE_SIZE;
45 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
46 sb->s_magic = SYSFS_MAGIC;
47 sb->s_op = &sysfs_ops;
48 sb->s_time_gran = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Tejun Heoe080e432007-07-18 14:29:06 +090050 /* get root inode, initialize and unlock it */
Eric W. Biederman4a67a1b2009-01-21 11:55:11 -080051 mutex_lock(&sysfs_mutex);
Tejun Heodf394fb2013-11-28 14:54:42 -050052 inode = sysfs_get_inode(sb, info->root->sd);
Eric W. Biederman4a67a1b2009-01-21 11:55:11 -080053 mutex_unlock(&sysfs_mutex);
Tejun Heofc9f54b2007-06-14 03:45:17 +090054 if (!inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 pr_debug("sysfs: could not get root inode\n");
56 return -ENOMEM;
57 }
58
Tejun Heoe080e432007-07-18 14:29:06 +090059 /* instantiate and link root dentry */
Al Viro48fde702012-01-08 22:15:13 -050060 root = d_make_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (!root) {
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -070062 pr_debug("%s: could not get root dentry!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return -ENOMEM;
64 }
Tejun Heodf394fb2013-11-28 14:54:42 -050065 kernfs_get(info->root->sd);
66 root->d_fsdata = info->root->sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 sb->s_root = root;
Al Viro469796d2012-06-07 20:51:39 -040068 sb->s_d_op = &sysfs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return 0;
70}
71
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070072static int sysfs_test_super(struct super_block *sb, void *data)
73{
74 struct sysfs_super_info *sb_info = sysfs_info(sb);
75 struct sysfs_super_info *info = data;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070076
Tejun Heodf394fb2013-11-28 14:54:42 -050077 return sb_info->root == info->root && sb_info->ns == info->ns;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070078}
79
80static int sysfs_set_super(struct super_block *sb, void *data)
81{
82 int error;
83 error = set_anon_super(sb, data);
84 if (!error)
85 sb->s_fs_info = data;
86 return error;
87}
88
Tejun Heo4b93dc92013-11-28 14:54:43 -050089/**
90 * kernfs_super_ns - determine the namespace tag of a kernfs super_block
91 * @sb: super_block of interest
92 *
93 * Return the namespace tag associated with kernfs super_block @sb.
94 */
95const void *kernfs_super_ns(struct super_block *sb)
Al Viroa685e082011-06-08 21:13:01 -040096{
Tejun Heo4b93dc92013-11-28 14:54:43 -050097 struct sysfs_super_info *info = sysfs_info(sb);
98
99 return info->ns;
Al Viroa685e082011-06-08 21:13:01 -0400100}
101
Al Virod0e46f82010-07-26 13:30:36 +0400102static struct dentry *sysfs_mount(struct file_system_type *fs_type,
103 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Tejun Heo4b93dc92013-11-28 14:54:43 -0500105 struct dentry *root;
106 void *ns;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700107
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -0700108 if (!(flags & MS_KERNMOUNT)) {
109 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
110 return ERR_PTR(-EPERM);
111
Tejun Heoc84a3b22013-11-23 18:01:46 -0500112 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
113 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -0700114 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -0700115
Tejun Heo4b93dc92013-11-28 14:54:43 -0500116 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
117 root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
118 if (IS_ERR(root))
119 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
120 return root;
121}
122
123/**
124 * kernfs_mount_ns - kernfs mount helper
125 * @fs_type: file_system_type of the fs being mounted
126 * @flags: mount flags specified for the mount
127 * @root: kernfs_root of the hierarchy being mounted
128 * @ns: optional namespace tag of the mount
129 *
130 * This is to be called from each kernfs user's file_system_type->mount()
131 * implementation, which should pass through the specified @fs_type and
132 * @flags, and specify the hierarchy and namespace tag to mount via @root
133 * and @ns, respectively.
134 *
135 * The return value can be passed to the vfs layer verbatim.
136 */
137struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags,
138 struct kernfs_root *root, const void *ns)
139{
140 struct super_block *sb;
141 struct sysfs_super_info *info;
142 int error;
143
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700144 info = kzalloc(sizeof(*info), GFP_KERNEL);
145 if (!info)
Al Virod0e46f82010-07-26 13:30:36 +0400146 return ERR_PTR(-ENOMEM);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700147
Tejun Heo4b93dc92013-11-28 14:54:43 -0500148 info->root = root;
149 info->ns = ns;
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700150
David Howells9249e172012-06-25 12:55:37 +0100151 sb = sget(fs_type, sysfs_test_super, sysfs_set_super, flags, info);
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700152 if (IS_ERR(sb) || sb->s_fs_info != info)
Tejun Heo4b93dc92013-11-28 14:54:43 -0500153 kfree(info);
Al Virod0e46f82010-07-26 13:30:36 +0400154 if (IS_ERR(sb))
155 return ERR_CAST(sb);
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700156 if (!sb->s_root) {
Tejun Heoccc532d2013-11-28 14:54:36 -0500157 error = sysfs_fill_super(sb);
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700158 if (error) {
159 deactivate_locked_super(sb);
Al Virod0e46f82010-07-26 13:30:36 +0400160 return ERR_PTR(error);
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700161 }
162 sb->s_flags |= MS_ACTIVE;
163 }
164
Al Virod0e46f82010-07-26 13:30:36 +0400165 return dget(sb->s_root);
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700166}
167
168static void sysfs_kill_sb(struct super_block *sb)
169{
Tejun Heo4b93dc92013-11-28 14:54:43 -0500170 kernfs_kill_sb(sb);
171 kobj_ns_drop(KOBJ_NS_TYPE_NET, (void *)kernfs_super_ns(sb));
172}
173
174/**
175 * kernfs_kill_sb - kill_sb for kernfs
176 * @sb: super_block being killed
177 *
178 * This can be used directly for file_system_type->kill_sb(). If a kernfs
179 * user needs extra cleanup, it can implement its own kill_sb() and call
180 * this function at the end.
181 */
182void kernfs_kill_sb(struct super_block *sb)
183{
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700184 struct sysfs_super_info *info = sysfs_info(sb);
Tejun Heoba7443b2013-11-28 14:54:40 -0500185 struct sysfs_dirent *root_sd = sb->s_root->d_fsdata;
186
187 /*
188 * Remove the superblock from fs_supers/s_instances
Eric W. Biederman68d75ed2010-05-18 12:58:33 -0700189 * so we can't find it, before freeing sysfs_super_info.
190 */
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700191 kill_anon_super(sb);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500192 kfree(info);
Tejun Heoba7443b2013-11-28 14:54:40 -0500193 kernfs_put(root_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static struct file_system_type sysfs_fs_type = {
197 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +0400198 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -0700199 .kill_sb = sysfs_kill_sb,
Eric W. Biederman4f326c02012-07-27 05:56:48 -0700200 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
Tejun Heo4b93dc92013-11-28 14:54:43 -0500203void __init kernfs_init(void)
204{
205 sysfs_dir_cachep = kmem_cache_create("sysfs_dir_cache",
206 sizeof(struct sysfs_dirent),
207 0, SLAB_PANIC, NULL);
208 sysfs_inode_init();
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211int __init sysfs_init(void)
212{
Tejun Heo9e30cc92013-11-28 14:54:38 -0500213 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Tejun Heoba7443b2013-11-28 14:54:40 -0500215 sysfs_root = kernfs_create_root(NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500216 if (IS_ERR(sysfs_root))
217 return PTR_ERR(sysfs_root);
218
Tejun Heoba7443b2013-11-28 14:54:40 -0500219 sysfs_root_sd = sysfs_root->sd;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500222 if (err) {
223 kernfs_destroy_root(sysfs_root);
224 return err;
225 }
Tejun Heo9e30cc92013-11-28 14:54:38 -0500226
227 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}