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