blob: 6211230814fd89dcc1a0a4b0acbabe000428e5b8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070018#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include "sysfs.h"
21
Tejun Heoba7443b2013-11-28 14:54:40 -050022static struct kernfs_root *sysfs_root;
Tejun Heo324a56e2013-12-11 14:11:53 -050023struct kernfs_node *sysfs_root_kn;
Tejun Heo061447a2013-11-28 14:54:39 -050024
Al Virod0e46f82010-07-26 13:30:36 +040025static struct dentry *sysfs_mount(struct file_system_type *fs_type,
26 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Tejun Heo4b93dc92013-11-28 14:54:43 -050028 struct dentry *root;
29 void *ns;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070030
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070031 if (!(flags & MS_KERNMOUNT)) {
32 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
33 return ERR_PTR(-EPERM);
34
Tejun Heoc84a3b22013-11-23 18:01:46 -050035 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
36 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070037 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070038
Tejun Heo4b93dc92013-11-28 14:54:43 -050039 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
40 root = kernfs_mount_ns(fs_type, flags, sysfs_root, ns);
41 if (IS_ERR(root))
42 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
43 return root;
44}
45
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070046static void sysfs_kill_sb(struct super_block *sb)
47{
Tejun Heoa7560a02013-12-10 10:22:30 -050048 void *ns = (void *)kernfs_super_ns(sb);
49
Tejun Heo4b93dc92013-11-28 14:54:43 -050050 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050051 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050052}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct file_system_type sysfs_fs_type = {
55 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +040056 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070057 .kill_sb = sysfs_kill_sb,
Eric W. Biederman4f326c02012-07-27 05:56:48 -070058 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61int __init sysfs_init(void)
62{
Tejun Heo9e30cc92013-11-28 14:54:38 -050063 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Tejun Heo80b9bbe2013-12-11 16:03:00 -050065 sysfs_root = kernfs_create_root(NULL, NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -050066 if (IS_ERR(sysfs_root))
67 return PTR_ERR(sysfs_root);
68
Tejun Heo324a56e2013-12-11 14:11:53 -050069 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -050070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -050072 if (err) {
73 kernfs_destroy_root(sysfs_root);
74 return err;
75 }
Tejun Heo9e30cc92013-11-28 14:54:38 -050076
77 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}