blob: 92682fcc41f6c976f1d38cf3111eab509a9028d8 [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>
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070016#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "sysfs.h"
19
Tejun Heoba7443b2013-11-28 14:54:40 -050020static struct kernfs_root *sysfs_root;
Tejun Heo324a56e2013-12-11 14:11:53 -050021struct kernfs_node *sysfs_root_kn;
Tejun Heo061447a2013-11-28 14:54:39 -050022
Al Virod0e46f82010-07-26 13:30:36 +040023static struct dentry *sysfs_mount(struct file_system_type *fs_type,
24 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Tejun Heo4b93dc92013-11-28 14:54:43 -050026 struct dentry *root;
27 void *ns;
Al Viro7b745a42018-05-14 00:03:34 -040028 bool new_sb = false;
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070029
Linus Torvalds1751e8a2017-11-27 13:05:09 -080030 if (!(flags & SB_KERNMOUNT)) {
Tejun Heoc84a3b22013-11-23 18:01:46 -050031 if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET))
32 return ERR_PTR(-EPERM);
Eric W. Biederman7dc5dbc2013-03-25 20:07:01 -070033 }
Eric W. Biederman87a8ebd2013-03-24 14:28:27 -070034
Tejun Heo4b93dc92013-11-28 14:54:43 -050035 ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET);
Jianyu Zhanc9482a52014-04-26 15:40:28 +080036 root = kernfs_mount_ns(fs_type, flags, sysfs_root,
37 SYSFS_MAGIC, &new_sb, ns);
Al Viro7b745a42018-05-14 00:03:34 -040038 if (!new_sb)
Tejun Heo4b93dc92013-11-28 14:54:43 -050039 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Al Viro7b745a42018-05-14 00:03:34 -040040 else if (!IS_ERR(root))
Eric W. Biederman29a517c2016-06-10 13:03:05 -050041 root->d_sb->s_iflags |= SB_I_USERNS_VISIBLE;
Eric W. Biederman90f85722015-06-29 14:42:03 -050042
Tejun Heo4b93dc92013-11-28 14:54:43 -050043 return root;
44}
45
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070046static void sysfs_kill_sb(struct super_block *sb)
47{
Tejun Heoa7560a02013-12-10 10:22:30 -050048 void *ns = (void *)kernfs_super_ns(sb);
49
Tejun Heo4b93dc92013-11-28 14:54:43 -050050 kernfs_kill_sb(sb);
Tejun Heoa7560a02013-12-10 10:22:30 -050051 kobj_ns_drop(KOBJ_NS_TYPE_NET, ns);
Tejun Heo4b93dc92013-11-28 14:54:43 -050052}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct file_system_type sysfs_fs_type = {
55 .name = "sysfs",
Al Virod0e46f82010-07-26 13:30:36 +040056 .mount = sysfs_mount,
Eric W. Biederman9e7fdd22010-03-30 11:31:24 -070057 .kill_sb = sysfs_kill_sb,
Eric W. Biederman8654df42016-06-09 16:06:06 -050058 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
61int __init sysfs_init(void)
62{
Tejun Heo9e30cc92013-11-28 14:54:38 -050063 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Tejun Heo555724a2014-05-12 13:56:27 -040065 sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
66 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}