Greg Kroah-Hartman | 619daee | 2018-01-22 16:18:13 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 3 | * fs/sysfs/dir.c - sysfs core and dir operation implementation |
| 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 | * |
Mauro Carvalho Chehab | 0c1bc6b | 2020-04-14 18:48:37 +0200 | [diff] [blame] | 9 | * Please see Documentation/filesystems/sysfs.rst for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Greg Kroah-Hartman | 5d54f94 | 2018-01-22 15:57:59 +0100 | [diff] [blame] | 12 | #define pr_fmt(fmt) "sysfs: " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/kobject.h> |
Robert P. J. Day | c6f8773 | 2008-03-13 22:41:52 -0400 | [diff] [blame] | 16 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "sysfs.h" |
| 18 | |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 19 | DEFINE_SPINLOCK(sysfs_symlink_target_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 21 | void sysfs_warn_dup(struct kernfs_node *parent, const char *name) |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 22 | { |
Tejun Heo | 3abb1d9 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 23 | char *buf; |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 24 | |
Tejun Heo | 3eef34a | 2014-02-07 13:32:07 -0500 | [diff] [blame] | 25 | buf = kzalloc(PATH_MAX, GFP_KERNEL); |
| 26 | if (buf) |
Tejun Heo | 3abb1d9 | 2016-08-10 11:23:44 -0400 | [diff] [blame] | 27 | kernfs_path(parent, buf, PATH_MAX); |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 28 | |
Greg Kroah-Hartman | 5d54f94 | 2018-01-22 15:57:59 +0100 | [diff] [blame] | 29 | pr_warn("cannot create duplicate filename '%s/%s'\n", buf, name); |
| 30 | dump_stack(); |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 31 | |
Tejun Heo | 3eef34a | 2014-02-07 13:32:07 -0500 | [diff] [blame] | 32 | kfree(buf); |
Tejun Heo | d1c1459 | 2013-10-24 11:49:11 -0400 | [diff] [blame] | 33 | } |
| 34 | |
Alex Chiang | 425cb02 | 2009-02-12 10:56:59 -0700 | [diff] [blame] | 35 | /** |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 36 | * sysfs_create_dir_ns - create a directory for an object with a namespace tag |
| 37 | * @kobj: object we're creating directory for |
| 38 | * @ns: the namespace tag to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | */ |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 40 | int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 42 | struct kernfs_node *parent, *kn; |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 43 | kuid_t uid; |
| 44 | kgid_t gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Greg Kroah-Hartman | de96e9f | 2019-01-03 10:23:47 +0100 | [diff] [blame] | 46 | if (WARN_ON(!kobj)) |
| 47 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Eric W. Biederman | 90bc613 | 2007-07-31 19:15:08 +0900 | [diff] [blame] | 49 | if (kobj->parent) |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 50 | parent = kobj->parent->sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | else |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 52 | parent = sysfs_root_kn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 54 | if (!parent) |
Dan Williams | 3a19888 | 2012-04-06 13:41:06 -0700 | [diff] [blame] | 55 | return -ENOENT; |
| 56 | |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 57 | kobject_get_ownership(kobj, &uid, &gid); |
| 58 | |
Tejun Heo | bb8b9d0 | 2013-12-11 16:02:55 -0500 | [diff] [blame] | 59 | kn = kernfs_create_dir_ns(parent, kobject_name(kobj), |
Dmitry Torokhov | 5f81880 | 2018-07-20 21:56:48 +0000 | [diff] [blame] | 60 | S_IRWXU | S_IRUGO | S_IXUGO, uid, gid, |
Dmitry Torokhov | 488dee9 | 2018-07-20 21:56:47 +0000 | [diff] [blame] | 61 | kobj, ns); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 62 | if (IS_ERR(kn)) { |
| 63 | if (PTR_ERR(kn) == -EEXIST) |
| 64 | sysfs_warn_dup(parent, kobject_name(kobj)); |
| 65 | return PTR_ERR(kn); |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 68 | kobj->sd = kn; |
Tejun Heo | 93b2b8e | 2013-11-28 14:54:15 -0500 | [diff] [blame] | 69 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Tejun Heo | 7eed6ec | 2013-10-24 11:49:10 -0400 | [diff] [blame] | 72 | /** |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 73 | * sysfs_remove_dir - remove an object's directory. |
| 74 | * @kobj: object. |
| 75 | * |
| 76 | * The only thing special about this is that we remove any files in |
| 77 | * the directory before we remove the directory, and we've inlined |
| 78 | * what used to be sysfs_rmdir() below, instead of calling separately. |
| 79 | */ |
Greg Kroah-Hartman | 1b18dc2 | 2013-08-21 16:28:26 -0700 | [diff] [blame] | 80 | void sysfs_remove_dir(struct kobject *kobj) |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 81 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 82 | struct kernfs_node *kn = kobj->sd; |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 83 | |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 84 | /* |
| 85 | * In general, kboject owner is responsible for ensuring removal |
| 86 | * doesn't race with other operations and sysfs doesn't provide any |
| 87 | * protection; however, when @kobj is used as a symlink target, the |
| 88 | * symlinking entity usually doesn't own @kobj and thus has no |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 89 | * control over removal. @kobj->sd may be removed anytime |
| 90 | * and symlink code may end up dereferencing an already freed node. |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 91 | * |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 92 | * sysfs_symlink_target_lock synchronizes @kobj->sd |
| 93 | * disassociation against symlink operations so that symlink code |
| 94 | * can safely dereference @kobj->sd. |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 95 | */ |
| 96 | spin_lock(&sysfs_symlink_target_lock); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 97 | kobj->sd = NULL; |
Tejun Heo | 0cae60f | 2013-10-30 10:28:36 -0400 | [diff] [blame] | 98 | spin_unlock(&sysfs_symlink_target_lock); |
Tejun Heo | aecdced | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 99 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 100 | if (kn) { |
Tejun Heo | df23fc3 | 2013-12-11 14:11:56 -0500 | [diff] [blame] | 101 | WARN_ON_ONCE(kernfs_type(kn) != KERNFS_DIR); |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 102 | kernfs_remove(kn); |
Tejun Heo | 250f7c3 | 2013-09-18 17:15:38 -0400 | [diff] [blame] | 103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 106 | int sysfs_rename_dir_ns(struct kobject *kobj, const char *new_name, |
| 107 | const void *new_ns) |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 108 | { |
Tejun Heo | 3eef34a | 2014-02-07 13:32:07 -0500 | [diff] [blame] | 109 | struct kernfs_node *parent; |
| 110 | int ret; |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 111 | |
Tejun Heo | 3eef34a | 2014-02-07 13:32:07 -0500 | [diff] [blame] | 112 | parent = kernfs_get_parent(kobj->sd); |
| 113 | ret = kernfs_rename_ns(kobj->sd, parent, new_name, new_ns); |
| 114 | kernfs_put(parent); |
| 115 | return ret; |
Eric W. Biederman | ca1bab3 | 2009-11-20 16:08:57 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Tejun Heo | e34ff49 | 2013-09-11 22:29:05 -0400 | [diff] [blame] | 118 | int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj, |
| 119 | const void *new_ns) |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 120 | { |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 121 | struct kernfs_node *kn = kobj->sd; |
| 122 | struct kernfs_node *new_parent; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 123 | |
Tejun Heo | 324a56e | 2013-12-11 14:11:53 -0500 | [diff] [blame] | 124 | new_parent = new_parent_kobj && new_parent_kobj->sd ? |
| 125 | new_parent_kobj->sd : sysfs_root_kn; |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 126 | |
Tejun Heo | adc5e8b | 2013-12-11 14:11:54 -0500 | [diff] [blame] | 127 | return kernfs_rename_ns(kn, new_parent, kn->name, new_ns); |
Cornelia Huck | 8a82472 | 2006-11-20 17:07:51 +0100 | [diff] [blame] | 128 | } |
Eric W. Biederman | 87d2846 | 2015-05-13 16:31:40 -0500 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * sysfs_create_mount_point - create an always empty directory |
| 132 | * @parent_kobj: kobject that will contain this always empty directory |
| 133 | * @name: The name of the always empty directory to add |
| 134 | */ |
| 135 | int sysfs_create_mount_point(struct kobject *parent_kobj, const char *name) |
| 136 | { |
| 137 | struct kernfs_node *kn, *parent = parent_kobj->sd; |
| 138 | |
| 139 | kn = kernfs_create_empty_dir(parent, name); |
| 140 | if (IS_ERR(kn)) { |
| 141 | if (PTR_ERR(kn) == -EEXIST) |
| 142 | sysfs_warn_dup(parent, name); |
| 143 | return PTR_ERR(kn); |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(sysfs_create_mount_point); |
| 149 | |
| 150 | /** |
| 151 | * sysfs_remove_mount_point - remove an always empty directory. |
| 152 | * @parent_kobj: kobject that will contain this always empty directory |
| 153 | * @name: The name of the always empty directory to remove |
| 154 | * |
| 155 | */ |
| 156 | void sysfs_remove_mount_point(struct kobject *parent_kobj, const char *name) |
| 157 | { |
| 158 | struct kernfs_node *parent = parent_kobj->sd; |
| 159 | |
| 160 | kernfs_remove_by_name_ns(parent, name, NULL); |
| 161 | } |
| 162 | EXPORT_SYMBOL_GPL(sysfs_remove_mount_point); |