blob: 1b56686ab178dc6d4363ea2f76fde6cff90926da [file] [log] [blame]
Greg Kroah-Hartman619daee2018-01-22 16:18:13 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09003 * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
4 *
5 * Copyright (c) 2001-3 Patrick Mochel
6 * Copyright (c) 2007 SUSE Linux Products GmbH
7 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
8 *
Tejun Heo6d66f5c2007-09-20 17:31:38 +09009 * Please see Documentation/filesystems/sysfs.txt for more information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/fs.h>
Jianyu Zhanc9482a52014-04-26 15:40:28 +080013#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
David Howells23bf1b62018-11-01 23:07:26 +000016#include <linux/slab.h>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070017#include <linux/user_namespace.h>
David Howells23bf1b62018-11-01 23:07:26 +000018#include <linux/fs_context.h>
19#include <net/net_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
David Howells23bf1b62018-11-01 23:07:26 +000026static int sysfs_get_tree(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
David Howells23bf1b62018-11-01 23:07:26 +000028 struct kernfs_fs_context *kfc = fc->fs_private;
29 int ret;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070030
David Howells23bf1b62018-11-01 23:07:26 +000031 ret = kernfs_get_tree(fc);
32 if (ret)
33 return ret;
34
35 if (kfc->new_sb_created)
36 fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
37 return 0;
38}
39
40static void sysfs_fs_context_free(struct fs_context *fc)
41{
42 struct kernfs_fs_context *kfc = fc->fs_private;
43
44 if (kfc->ns_tag)
45 kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
46 kernfs_free_fs_context(fc);
47 kfree(kfc);
48}
49
50static const struct fs_context_operations sysfs_fs_context_ops = {
51 .free = sysfs_fs_context_free,
52 .get_tree = sysfs_get_tree,
53};
54
55static int sysfs_init_fs_context(struct fs_context *fc)
56{
57 struct kernfs_fs_context *kfc;
58 struct net *netns;
59
60 if (!(fc->sb_flags & SB_KERNMOUNT)) {
Tejun Heoc84a3b22013-11-23 18:01:46 -050061 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
David Howells23bf1b62018-11-01 23:07:26 +000062 return -EPERM;
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070063 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070064
David Howells23bf1b62018-11-01 23:07:26 +000065 kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
66 if (!kfc)
67 return -ENOMEM;
Eric W. Biederman90f85722015-06-29 14:42:03 -050068
David Howells23bf1b62018-11-01 23:07:26 +000069 kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
70 kfc->root = sysfs_root;
71 kfc->magic = SYSFS_MAGIC;
72 fc->fs_private = kfc;
73 fc->ops = &sysfs_fs_context_ops;
Al Viroab81dab2019-03-16 09:45:42 -040074 if (netns) {
75 if (fc->user_ns)
76 put_user_ns(fc->user_ns);
77 fc->user_ns = get_user_ns(netns->user_ns);
78 }
David Howells23bf1b62018-11-01 23:07:26 +000079 fc->global = true;
80 return 0;
Tejun Heo4b93dc92013-11-28 14:54:43 -050081}
82
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070083static void sysfs_kill_sb(struct super_block *sb)
84{
Tejun Heoa7560a02013-12-10 10:22:30 -050085 void *ns = (void *)kernfs_super_ns(sb);
86
Tejun Heo4b93dc92013-11-28 14:54:43 -050087 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050088 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050089}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static struct file_system_type sysfs_fs_type = {
David Howells23bf1b62018-11-01 23:07:26 +000092 .name = "sysfs",
93 .init_fs_context = sysfs_init_fs_context,
94 .kill_sb = sysfs_kill_sb,
95 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98int __init sysfs_init(void)
99{
Tejun Heo9e30cc92013-11-28 14:54:38 -0500100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Tejun Heo555724a2014-05-12 13:56:27 -0400102 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
103 NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500104 if (IS_ERR(sysfs_root))
105 return PTR_ERR(sysfs_root);
106
Tejun Heo324a56e2013-12-11 14:11:53 -0500107 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500110 if (err) {
111 kernfs_destroy_root(sysfs_root);
112 return err;
113 }
Tejun Heo9e30cc92013-11-28 14:54:38 -0500114
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}