xref: /openbmc/linux/fs/sysfs/mount.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1619daeeeSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
36d66f5cdSTejun Heo  * fs/sysfs/symlink.c - operations for initializing and mounting sysfs
46d66f5cdSTejun Heo  *
56d66f5cdSTejun Heo  * Copyright (c) 2001-3 Patrick Mochel
66d66f5cdSTejun Heo  * Copyright (c) 2007 SUSE Linux Products GmbH
76d66f5cdSTejun Heo  * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
86d66f5cdSTejun Heo  *
90c1bc6b8SMauro Carvalho Chehab  * Please see Documentation/filesystems/sysfs.rst for more information.
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/fs.h>
13c9482a5bSJianyu Zhan #include <linux/magic.h>
141da177e4SLinus Torvalds #include <linux/mount.h>
151da177e4SLinus Torvalds #include <linux/init.h>
1623bf1b6bSDavid Howells #include <linux/slab.h>
1787a8ebd6SEric W. Biederman #include <linux/user_namespace.h>
1823bf1b6bSDavid Howells #include <linux/fs_context.h>
1923bf1b6bSDavid Howells #include <net/net_namespace.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include "sysfs.h"
221da177e4SLinus Torvalds 
23ba7443bcSTejun Heo static struct kernfs_root *sysfs_root;
24324a56e1STejun Heo struct kernfs_node *sysfs_root_kn;
25061447a4STejun Heo 
sysfs_get_tree(struct fs_context * fc)2623bf1b6bSDavid Howells static int sysfs_get_tree(struct fs_context *fc)
271da177e4SLinus Torvalds {
2823bf1b6bSDavid Howells 	struct kernfs_fs_context *kfc = fc->fs_private;
2923bf1b6bSDavid Howells 	int ret;
309e7fdd25SEric W. Biederman 
3123bf1b6bSDavid Howells 	ret = kernfs_get_tree(fc);
3223bf1b6bSDavid Howells 	if (ret)
3323bf1b6bSDavid Howells 		return ret;
3423bf1b6bSDavid Howells 
3523bf1b6bSDavid Howells 	if (kfc->new_sb_created)
3623bf1b6bSDavid Howells 		fc->root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
3723bf1b6bSDavid Howells 	return 0;
387dc5dbc8SEric W. Biederman }
397dc5dbc8SEric W. Biederman 
sysfs_fs_context_free(struct fs_context * fc)4023bf1b6bSDavid Howells static void sysfs_fs_context_free(struct fs_context *fc)
4123bf1b6bSDavid Howells {
4223bf1b6bSDavid Howells 	struct kernfs_fs_context *kfc = fc->fs_private;
4390f8572bSEric W. Biederman 
4423bf1b6bSDavid Howells 	if (kfc->ns_tag)
4523bf1b6bSDavid Howells 		kobj_ns_drop(KOBJ_NS_TYPE_NET, kfc->ns_tag);
4623bf1b6bSDavid Howells 	kernfs_free_fs_context(fc);
4723bf1b6bSDavid Howells 	kfree(kfc);
4823bf1b6bSDavid Howells }
4923bf1b6bSDavid Howells 
5023bf1b6bSDavid Howells static const struct fs_context_operations sysfs_fs_context_ops = {
5123bf1b6bSDavid Howells 	.free		= sysfs_fs_context_free,
5223bf1b6bSDavid Howells 	.get_tree	= sysfs_get_tree,
5323bf1b6bSDavid Howells };
5423bf1b6bSDavid Howells 
sysfs_init_fs_context(struct fs_context * fc)5523bf1b6bSDavid Howells static int sysfs_init_fs_context(struct fs_context *fc)
5623bf1b6bSDavid Howells {
5723bf1b6bSDavid Howells 	struct kernfs_fs_context *kfc;
5823bf1b6bSDavid Howells 	struct net *netns;
5923bf1b6bSDavid Howells 
6023bf1b6bSDavid Howells 	if (!(fc->sb_flags & SB_KERNMOUNT)) {
6123bf1b6bSDavid Howells 		if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
6223bf1b6bSDavid Howells 			return -EPERM;
6323bf1b6bSDavid Howells 	}
6423bf1b6bSDavid Howells 
6523bf1b6bSDavid Howells 	kfc = kzalloc(sizeof(struct kernfs_fs_context), GFP_KERNEL);
6623bf1b6bSDavid Howells 	if (!kfc)
6723bf1b6bSDavid Howells 		return -ENOMEM;
6823bf1b6bSDavid Howells 
6923bf1b6bSDavid Howells 	kfc->ns_tag = netns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
7023bf1b6bSDavid Howells 	kfc->root = sysfs_root;
7123bf1b6bSDavid Howells 	kfc->magic = SYSFS_MAGIC;
7223bf1b6bSDavid Howells 	fc->fs_private = kfc;
7323bf1b6bSDavid Howells 	fc->ops = &sysfs_fs_context_ops;
74ab81dabdSAl Viro 	if (netns) {
7523bf1b6bSDavid Howells 		put_user_ns(fc->user_ns);
7623bf1b6bSDavid Howells 		fc->user_ns = get_user_ns(netns->user_ns);
77ab81dabdSAl Viro 	}
7823bf1b6bSDavid Howells 	fc->global = true;
7923bf1b6bSDavid Howells 	return 0;
804b93dc9bSTejun Heo }
814b93dc9bSTejun Heo 
sysfs_kill_sb(struct super_block * sb)829e7fdd25SEric W. Biederman static void sysfs_kill_sb(struct super_block *sb)
839e7fdd25SEric W. Biederman {
84a7560a01STejun Heo 	void *ns = (void *)kernfs_super_ns(sb);
85a7560a01STejun Heo 
864b93dc9bSTejun Heo 	kernfs_kill_sb(sb);
87a7560a01STejun Heo 	kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
884b93dc9bSTejun Heo }
894b93dc9bSTejun Heo 
901da177e4SLinus Torvalds static struct file_system_type sysfs_fs_type = {
911da177e4SLinus Torvalds 	.name			= "sysfs",
9223bf1b6bSDavid Howells 	.init_fs_context	= sysfs_init_fs_context,
939e7fdd25SEric W. Biederman 	.kill_sb		= sysfs_kill_sb,
948654df4eSEric W. Biederman 	.fs_flags		= FS_USERNS_MOUNT,
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
sysfs_init(void)971da177e4SLinus Torvalds int __init sysfs_init(void)
981da177e4SLinus Torvalds {
999e30cc95STejun Heo 	int err;
1001da177e4SLinus Torvalds 
101555724a8STejun Heo 	sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
102555724a8STejun Heo 					NULL);
1034b93dc9bSTejun Heo 	if (IS_ERR(sysfs_root))
1044b93dc9bSTejun Heo 		return PTR_ERR(sysfs_root);
1054b93dc9bSTejun Heo 
106*f2eb478fSGreg Kroah-Hartman 	sysfs_root_kn = kernfs_root_to_node(sysfs_root);
107ba7443bcSTejun Heo 
1081da177e4SLinus Torvalds 	err = register_filesystem(&sysfs_fs_type);
1094b93dc9bSTejun Heo 	if (err) {
1104b93dc9bSTejun Heo 		kernfs_destroy_root(sysfs_root);
1114b93dc9bSTejun Heo 		return err;
1124b93dc9bSTejun Heo 	}
1139e30cc95STejun Heo 
1149e30cc95STejun Heo 	return 0;
1151da177e4SLinus Torvalds }
116