blob: 3eaf5c6622eb4fafc82137bc501aac803d7952f4 [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;
Li Zefanfed95ba2014-02-25 19:28:44 +080030 bool new_sb;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070031
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070032 if (!(flags & MS_KERNMOUNT)) {
33 if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type))
34 return ERR_PTR(-EPERM);
35
Tejun Heoc84a3b22013-11-23 18:01:46 -050036 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
37 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070038 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070039
Tejun Heo4b93dc92013-11-28 14:54:43 -050040 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
Li Zefanfed95ba2014-02-25 19:28:44 +080041 root = kernfs_mount_ns(fs_type, flags, sysfs_root, &new_sb, ns);
42 if (IS_ERR(root) || !new_sb)
Tejun Heo4b93dc92013-11-28 14:54:43 -050043 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
44 return root;
45}
46
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070047static void sysfs_kill_sb(struct super_block *sb)
48{
Tejun Heoa7560a02013-12-10 10:22:30 -050049 void *ns = (void *)kernfs_super_ns(sb);
50
Tejun Heo4b93dc92013-11-28 14:54:43 -050051 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050052 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static struct file_system_type sysfs_fs_type = {
56 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +040057 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070058 .kill_sb = sysfs_kill_sb,
Eric W. Biederman4f326c02012-07-27 05:56:48 -070059 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
62int __init sysfs_init(void)
63{
Tejun Heo9e30cc92013-11-28 14:54:38 -050064 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tejun Heo80b9bbe2013-12-11 16:03:00 -050066 sysfs_root = kernfs_create_root(NULL, NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -050067 if (IS_ERR(sysfs_root))
68 return PTR_ERR(sysfs_root);
69
Tejun Heo324a56e2013-12-11 14:11:53 -050070 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -050071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -050073 if (err) {
74 kernfs_destroy_root(sysfs_root);
75 return err;
76 }
Tejun Heo9e30cc92013-11-28 14:54:38 -050077
78 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}