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