blob: e747c135c1d1f4cbf5ef6040312084b42381a0d3 [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 *
Mauro Carvalho Chehab0c1bc6b2020-04-14 18:48:37 +02009 * Please see Documentation/filesystems/sysfs.rst 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) {
Al Virof7a99452019-05-12 12:42:58 -040075 put_user_ns(fc->user_ns);
Al Viroab81dab2019-03-16 09:45:42 -040076 fc->user_ns = get_user_ns(netns->user_ns);
77 }
David Howells23bf1b62018-11-01 23:07:26 +000078 fc->global = true;
79 return 0;
Tejun Heo4b93dc92013-11-28 14:54:43 -050080}
81
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070082static void sysfs_kill_sb(struct super_block *sb)
83{
Tejun Heoa7560a02013-12-10 10:22:30 -050084 void *ns = (void *)kernfs_super_ns(sb);
85
Tejun Heo4b93dc92013-11-28 14:54:43 -050086 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050087 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050088}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static struct file_system_type sysfs_fs_type = {
David Howells23bf1b62018-11-01 23:07:26 +000091 .name = "sysfs",
92 .init_fs_context = sysfs_init_fs_context,
93 .kill_sb = sysfs_kill_sb,
94 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97int __init sysfs_init(void)
98{
Tejun Heo9e30cc92013-11-28 14:54:38 -050099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Tejun Heo555724a2014-05-12 13:56:27 -0400101 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
102 NULL);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500103 if (IS_ERR(sysfs_root))
104 return PTR_ERR(sysfs_root);
105
Tejun Heo324a56e2013-12-11 14:11:53 -0500106 sysfs_root_kn = sysfs_root->kn;
Tejun Heoba7443b2013-11-28 14:54:40 -0500107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 err = register_filesystem(&sysfs_fs_type);
Tejun Heo4b93dc92013-11-28 14:54:43 -0500109 if (err) {
110 kernfs_destroy_root(sysfs_root);
111 return err;
112 }
Tejun Heo9e30cc92013-11-28 14:54:38 -0500113
114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}