Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
Bin Wang | 6b8fbde | 2012-12-06 17:08:56 +0800 | [diff] [blame] | 13 | #define DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/fs.h> |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 16 | #include <linux/magic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mount.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 19 | #include <linux/user_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "sysfs.h" |
| 22 | |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 23 | static struct kernfs_root *sysfs_root; |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 24 | struct kernfs_node *sysfs_root_kn; |
Tejun Heo | 061447a | 2013-11-28 14:54:39 -0500 | [diff] [blame] | 25 | |
Al Viro | d0e46f8 | 2010-07-26 13:30:36 +0400 | [diff] [blame] | 26 | static struct dentry *sysfs_mount(struct file_system_type *fs_type, |
| 27 | int flags, const char *dev_name, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 29 | struct dentry *root; |
| 30 | void *ns; |
Li Zefan | fed95ba | 2014-02-25 19:28:44 +0800 | [diff] [blame] | 31 | bool new_sb; |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 32 | |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 33 | if (!(flags & MS_KERNMOUNT)) { |
| 34 | if (!capable(CAP_SYS_ADMIN) && !fs_fully_visible(fs_type)) |
| 35 | return ERR_PTR(-EPERM); |
| 36 | |
Tejun Heo | c84a3b2 | 2013-11-23 18:01:46 -0500 | [diff] [blame] | 37 | if (!kobj_ns_current_may_mount(KOBJ_NS_TYPE_NET)) |
| 38 | return ERR_PTR(-EPERM); |
Eric W. Biederman | 7dc5dbc | 2013-03-25 20:07:01 -0700 | [diff] [blame] | 39 | } |
Eric W. Biederman | 87a8ebd | 2013-03-24 14:28:27 -0700 | [diff] [blame] | 40 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 41 | ns = kobj_ns_grab_current(KOBJ_NS_TYPE_NET); |
Jianyu Zhan | c9482a5 | 2014-04-26 15:40:28 +0800 | [diff] [blame] | 42 | root = kernfs_mount_ns(fs_type, flags, sysfs_root, |
| 43 | SYSFS_MAGIC, &new_sb, ns); |
Li Zefan | fed95ba | 2014-02-25 19:28:44 +0800 | [diff] [blame] | 44 | if (IS_ERR(root) || !new_sb) |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 45 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
| 46 | return root; |
| 47 | } |
| 48 | |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 49 | static void sysfs_kill_sb(struct super_block *sb) |
| 50 | { |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 51 | void *ns = (void *)kernfs_super_ns(sb); |
| 52 | |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 53 | kernfs_kill_sb(sb); |
Tejun Heo | a7560a0 | 2013-12-10 10:22:30 -0500 | [diff] [blame] | 54 | kobj_ns_drop(KOBJ_NS_TYPE_NET, ns); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | static struct file_system_type sysfs_fs_type = { |
| 58 | .name = "sysfs", |
Al Viro | d0e46f8 | 2010-07-26 13:30:36 +0400 | [diff] [blame] | 59 | .mount = sysfs_mount, |
Eric W. Biederman | 9e7fdd2 | 2010-03-30 11:31:24 -0700 | [diff] [blame] | 60 | .kill_sb = sysfs_kill_sb, |
Eric W. Biederman | 4f326c0 | 2012-07-27 05:56:48 -0700 | [diff] [blame] | 61 | .fs_flags = FS_USERNS_MOUNT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | int __init sysfs_init(void) |
| 65 | { |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 66 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Tejun Heo | 555724a | 2014-05-12 13:56:27 -0400 | [diff] [blame] | 68 | sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK, |
| 69 | NULL); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 70 | if (IS_ERR(sysfs_root)) |
| 71 | return PTR_ERR(sysfs_root); |
| 72 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 73 | sysfs_root_kn = sysfs_root->kn; |
Tejun Heo | ba7443b | 2013-11-28 14:54:40 -0500 | [diff] [blame] | 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | err = register_filesystem(&sysfs_fs_type); |
Tejun Heo | 4b93dc9 | 2013-11-28 14:54:43 -0500 | [diff] [blame] | 76 | if (err) { |
| 77 | kernfs_destroy_root(sysfs_root); |
| 78 | return err; |
| 79 | } |
Tejun Heo | 9e30cc9 | 2013-11-28 14:54:38 -0500 | [diff] [blame] | 80 | |
| 81 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |