blob: 8a49486bf30c9859a5474f3cc5055bc7b9917de4 [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>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080016#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/init.h>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070019#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include "sysfs.h"
22
Tejun Heoba7443b2013-11-28 14:54:40 -050023static struct kernfs_root *sysfs_root;
Tejun Heo324a56e2013-12-11 14:11:53 -050024struct kernfs_node *sysfs_root_kn;
Tejun Heo061447a2013-11-28 14:54:39 -050025
Al Virod0e46f82010-07-26 13:30:36 +040026static struct dentry *sysfs_mount(struct file_system_type *fs_type,
27 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Tejun Heo4b93dc92013-11-28 14:54:43 -050029 struct dentry *root;
30 void *ns;
Li Zefanfed95ba2014-02-25 19:28:44 +080031 bool new_sb;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070032
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070033 if (!(flags & MS_KERNMOUNT)) {
34 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
35 return ERR_PTR(-EPERM);
36
Tejun Heoc84a3b22013-11-23 18:01:46 -050037 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
38 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070039 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070040
Tejun Heo4b93dc92013-11-28 14:54:43 -050041 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
Jianyu Zhanc9482a52014-04-26 15:40:28 +080042 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
43 SYSFS_MAGIC, &new_sb, ns);
Li Zefanfed95ba2014-02-25 19:28:44 +080044 if (IS_ERR(root) || !new_sb)
Tejun Heo4b93dc92013-11-28 14:54:43 -050045 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
46 return root;
47}
48
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070049static void sysfs_kill_sb(struct super_block *sb)
50{
Tejun Heoa7560a02013-12-10 10:22:30 -050051 void *ns = (void *)kernfs_super_ns(sb);
52
Tejun Heo4b93dc92013-11-28 14:54:43 -050053 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050054 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050055}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static struct file_system_type sysfs_fs_type = {
58 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +040059 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070060 .kill_sb = sysfs_kill_sb,
Eric W. Biederman4f326c02012-07-27 05:56:48 -070061 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
64int __init sysfs_init(void)
65{
Tejun Heo9e30cc92013-11-28 14:54:38 -050066 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Tejun Heo555724a2014-05-12 13:56:27 -040068 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
69 NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -050070 if (IS_ERR(sysfs_root))
71 return PTR_ERR(sysfs_root);
72
Tejun Heo324a56e2013-12-11 14:11:53 -050073 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -050074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -050076 if (err) {
77 kernfs_destroy_root(sysfs_root);
78 return err;
79 }
Tejun Heo9e30cc92013-11-28 14:54:38 -050080
81 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}