Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/sysfs/group.c - Operations for adding/removing multiple files at once. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Lab |
| 6 | * |
Greg Kroah-Hartman | e6c5692 | 2013-08-21 16:06:14 -0700 | [diff] [blame] | 7 | * This file is released undert the GPL v2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kobject.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/dcache.h> |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 14 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include "sysfs.h" |
| 17 | |
| 18 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 19 | static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 20 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
Greg Kroah-Hartman | 995d8ed | 2013-08-21 16:07:29 -0700 | [diff] [blame^] | 22 | struct attribute *const *attr; |
| 23 | struct bin_attribute *const *bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Greg Kroah-Hartman | 6ab9cea | 2013-07-14 16:05:55 -0700 | [diff] [blame] | 25 | if (grp->attrs) |
| 26 | for (attr = grp->attrs; *attr; attr++) |
| 27 | sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); |
| 28 | if (grp->bin_attrs) |
| 29 | for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) |
| 30 | sysfs_remove_bin_file(kobj, *bin_attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
| 32 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 33 | static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 34 | const struct attribute_group *grp, int update) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Greg Kroah-Hartman | 995d8ed | 2013-08-21 16:07:29 -0700 | [diff] [blame^] | 36 | struct attribute *const *attr; |
| 37 | struct bin_attribute *const *bin_attr; |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 38 | int error = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Greg Kroah-Hartman | 6ab9cea | 2013-07-14 16:05:55 -0700 | [diff] [blame] | 40 | if (grp->attrs) { |
| 41 | for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { |
| 42 | umode_t mode = 0; |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 43 | |
Greg Kroah-Hartman | 6ab9cea | 2013-07-14 16:05:55 -0700 | [diff] [blame] | 44 | /* |
| 45 | * In update mode, we're changing the permissions or |
| 46 | * visibility. Do this by first removing then |
| 47 | * re-adding (if required) the file. |
| 48 | */ |
| 49 | if (update) |
| 50 | sysfs_hash_and_remove(dir_sd, NULL, |
| 51 | (*attr)->name); |
| 52 | if (grp->is_visible) { |
| 53 | mode = grp->is_visible(kobj, *attr, i); |
| 54 | if (!mode) |
| 55 | continue; |
| 56 | } |
| 57 | error = sysfs_add_file_mode(dir_sd, *attr, |
| 58 | SYSFS_KOBJ_ATTR, |
| 59 | (*attr)->mode | mode); |
| 60 | if (unlikely(error)) |
| 61 | break; |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 62 | } |
Greg Kroah-Hartman | 6ab9cea | 2013-07-14 16:05:55 -0700 | [diff] [blame] | 63 | if (error) { |
| 64 | remove_files(dir_sd, kobj, grp); |
| 65 | goto exit; |
| 66 | } |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 67 | } |
Greg Kroah-Hartman | 6ab9cea | 2013-07-14 16:05:55 -0700 | [diff] [blame] | 68 | |
| 69 | if (grp->bin_attrs) { |
| 70 | for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) { |
| 71 | if (update) |
| 72 | sysfs_remove_bin_file(kobj, *bin_attr); |
| 73 | error = sysfs_create_bin_file(kobj, *bin_attr); |
| 74 | if (error) |
| 75 | break; |
| 76 | } |
| 77 | if (error) |
| 78 | remove_files(dir_sd, kobj, grp); |
| 79 | } |
| 80 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | return error; |
| 82 | } |
| 83 | |
| 84 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 85 | static int internal_create_group(struct kobject *kobj, int update, |
| 86 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 88 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | int error; |
| 90 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 91 | BUG_ON(!kobj || (!update && !kobj->sd)); |
| 92 | |
| 93 | /* Updates may happen before the object has been instantiated */ |
| 94 | if (unlikely(update && !kobj->sd)) |
| 95 | return -EINVAL; |
Oliver Schinagl | 388a8c3 | 2013-07-14 16:05:56 -0700 | [diff] [blame] | 96 | if (!grp->attrs && !grp->bin_attrs) { |
| 97 | WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n", |
Bruno Prémont | 5631f2c | 2012-04-03 09:59:48 +0200 | [diff] [blame] | 98 | kobj->name, grp->name ? "" : grp->name); |
| 99 | return -EINVAL; |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 102 | error = sysfs_create_subdir(kobj, grp->name, &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | if (error) |
| 104 | return error; |
| 105 | } else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 106 | sd = kobj->sd; |
| 107 | sysfs_get(sd); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 108 | error = create_files(sd, kobj, grp, update); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 109 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 111 | sysfs_remove_subdir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 113 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | return error; |
| 115 | } |
| 116 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 117 | /** |
| 118 | * sysfs_create_group - given a directory kobject, create an attribute group |
| 119 | * @kobj: The kobject to create the group on |
| 120 | * @grp: The attribute group to create |
| 121 | * |
| 122 | * This function creates a group for the first time. It will explicitly |
| 123 | * warn and error if any of the attribute files being created already exist. |
| 124 | * |
| 125 | * Returns 0 on success or error. |
| 126 | */ |
| 127 | int sysfs_create_group(struct kobject *kobj, |
| 128 | const struct attribute_group *grp) |
| 129 | { |
| 130 | return internal_create_group(kobj, 0, grp); |
| 131 | } |
Greg Kroah-Hartman | d363bc5 | 2013-08-21 16:04:12 -0700 | [diff] [blame] | 132 | EXPORT_SYMBOL_GPL(sysfs_create_group); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 133 | |
| 134 | /** |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 135 | * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups |
| 136 | * @kobj: The kobject to create the group on |
| 137 | * @groups: The attribute groups to create, NULL terminated |
| 138 | * |
| 139 | * This function creates a bunch of attribute groups. If an error occurs when |
| 140 | * creating a group, all previously created groups will be removed, unwinding |
| 141 | * everything back to the original state when this function was called. |
| 142 | * It will explicitly warn and error if any of the attribute files being |
| 143 | * created already exist. |
| 144 | * |
| 145 | * Returns 0 on success or error code from sysfs_create_groups on error. |
| 146 | */ |
| 147 | int sysfs_create_groups(struct kobject *kobj, |
| 148 | const struct attribute_group **groups) |
| 149 | { |
| 150 | int error = 0; |
| 151 | int i; |
| 152 | |
| 153 | if (!groups) |
| 154 | return 0; |
| 155 | |
| 156 | for (i = 0; groups[i]; i++) { |
| 157 | error = sysfs_create_group(kobj, groups[i]); |
| 158 | if (error) { |
| 159 | while (--i >= 0) |
| 160 | sysfs_remove_group(kobj, groups[i]); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | return error; |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(sysfs_create_groups); |
| 167 | |
| 168 | /** |
Robert P. J. Day | 1f8e1cd | 2011-05-07 17:18:20 -0400 | [diff] [blame] | 169 | * sysfs_update_group - given a directory kobject, update an attribute group |
| 170 | * @kobj: The kobject to update the group on |
| 171 | * @grp: The attribute group to update |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 172 | * |
| 173 | * This function updates an attribute group. Unlike |
| 174 | * sysfs_create_group(), it will explicitly not warn or error if any |
| 175 | * of the attribute files being created already exist. Furthermore, |
| 176 | * if the visibility of the files has changed through the is_visible() |
| 177 | * callback, it will update the permissions and add or remove the |
| 178 | * relevant files. |
| 179 | * |
| 180 | * The primary use for this function is to call it after making a change |
| 181 | * that affects group visibility. |
| 182 | * |
| 183 | * Returns 0 on success or error. |
| 184 | */ |
| 185 | int sysfs_update_group(struct kobject *kobj, |
| 186 | const struct attribute_group *grp) |
| 187 | { |
| 188 | return internal_create_group(kobj, 1, grp); |
| 189 | } |
Greg Kroah-Hartman | d363bc5 | 2013-08-21 16:04:12 -0700 | [diff] [blame] | 190 | EXPORT_SYMBOL_GPL(sysfs_update_group); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 191 | |
Greg Kroah-Hartman | 995d8ed | 2013-08-21 16:07:29 -0700 | [diff] [blame^] | 192 | void sysfs_remove_group(struct kobject *kobj, |
| 193 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 195 | struct sysfs_dirent *dir_sd = kobj->sd; |
| 196 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 198 | if (grp->name) { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 199 | sd = sysfs_get_dirent(dir_sd, NULL, grp->name); |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 200 | if (!sd) { |
Arjan van de Ven | 99fcd77 | 2008-07-25 19:45:41 -0700 | [diff] [blame] | 201 | WARN(!sd, KERN_WARNING "sysfs group %p not found for " |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 202 | "kobject '%s'\n", grp, kobject_name(kobj)); |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 203 | return; |
| 204 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 205 | } else |
| 206 | sd = sysfs_get(dir_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 208 | remove_files(sd, kobj, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 210 | sysfs_remove_subdir(sd); |
| 211 | |
| 212 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Greg Kroah-Hartman | d363bc5 | 2013-08-21 16:04:12 -0700 | [diff] [blame] | 214 | EXPORT_SYMBOL_GPL(sysfs_remove_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 216 | /** |
Greg Kroah-Hartman | 3e9b2ba | 2013-08-21 13:47:50 -0700 | [diff] [blame] | 217 | * sysfs_remove_groups - remove a list of groups |
| 218 | * |
| 219 | * kobj: The kobject for the groups to be removed from |
| 220 | * groups: NULL terminated list of groups to be removed |
| 221 | * |
| 222 | * If groups is not NULL, the all groups will be removed from the kobject |
| 223 | */ |
| 224 | void sysfs_remove_groups(struct kobject *kobj, |
| 225 | const struct attribute_group **groups) |
| 226 | { |
| 227 | int i; |
| 228 | |
| 229 | if (!groups) |
| 230 | return; |
| 231 | for (i = 0; groups[i]; i++) |
| 232 | sysfs_remove_group(kobj, groups[i]); |
| 233 | } |
| 234 | EXPORT_SYMBOL_GPL(sysfs_remove_groups); |
| 235 | |
| 236 | /** |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 237 | * sysfs_merge_group - merge files into a pre-existing attribute group. |
| 238 | * @kobj: The kobject containing the group. |
| 239 | * @grp: The files to create and the attribute group they belong to. |
| 240 | * |
| 241 | * This function returns an error if the group doesn't exist or any of the |
| 242 | * files already exist in that group, in which case none of the new files |
| 243 | * are created. |
| 244 | */ |
| 245 | int sysfs_merge_group(struct kobject *kobj, |
| 246 | const struct attribute_group *grp) |
| 247 | { |
| 248 | struct sysfs_dirent *dir_sd; |
| 249 | int error = 0; |
| 250 | struct attribute *const *attr; |
| 251 | int i; |
| 252 | |
Alan Stern | e030d58 | 2010-11-15 15:46:07 -0500 | [diff] [blame] | 253 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name); |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 254 | if (!dir_sd) |
| 255 | return -ENOENT; |
| 256 | |
| 257 | for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr)) |
| 258 | error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR); |
| 259 | if (error) { |
| 260 | while (--i >= 0) |
| 261 | sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name); |
| 262 | } |
| 263 | sysfs_put(dir_sd); |
| 264 | |
| 265 | return error; |
| 266 | } |
| 267 | EXPORT_SYMBOL_GPL(sysfs_merge_group); |
| 268 | |
| 269 | /** |
| 270 | * sysfs_unmerge_group - remove files from a pre-existing attribute group. |
| 271 | * @kobj: The kobject containing the group. |
| 272 | * @grp: The files to remove and the attribute group they belong to. |
| 273 | */ |
| 274 | void sysfs_unmerge_group(struct kobject *kobj, |
| 275 | const struct attribute_group *grp) |
| 276 | { |
| 277 | struct sysfs_dirent *dir_sd; |
| 278 | struct attribute *const *attr; |
| 279 | |
Alan Stern | e030d58 | 2010-11-15 15:46:07 -0500 | [diff] [blame] | 280 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name); |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 281 | if (dir_sd) { |
| 282 | for (attr = grp->attrs; *attr; ++attr) |
| 283 | sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); |
| 284 | sysfs_put(dir_sd); |
| 285 | } |
| 286 | } |
| 287 | EXPORT_SYMBOL_GPL(sysfs_unmerge_group); |
| 288 | |
Rafael J. Wysocki | 0bb8f3d | 2013-01-25 21:51:13 +0100 | [diff] [blame] | 289 | /** |
| 290 | * sysfs_add_link_to_group - add a symlink to an attribute group. |
| 291 | * @kobj: The kobject containing the group. |
| 292 | * @group_name: The name of the group. |
| 293 | * @target: The target kobject of the symlink to create. |
| 294 | * @link_name: The name of the symlink to create. |
| 295 | */ |
| 296 | int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, |
| 297 | struct kobject *target, const char *link_name) |
| 298 | { |
| 299 | struct sysfs_dirent *dir_sd; |
| 300 | int error = 0; |
| 301 | |
| 302 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name); |
| 303 | if (!dir_sd) |
| 304 | return -ENOENT; |
| 305 | |
| 306 | error = sysfs_create_link_sd(dir_sd, target, link_name); |
| 307 | sysfs_put(dir_sd); |
| 308 | |
| 309 | return error; |
| 310 | } |
| 311 | EXPORT_SYMBOL_GPL(sysfs_add_link_to_group); |
| 312 | |
| 313 | /** |
| 314 | * sysfs_remove_link_from_group - remove a symlink from an attribute group. |
| 315 | * @kobj: The kobject containing the group. |
| 316 | * @group_name: The name of the group. |
| 317 | * @link_name: The name of the symlink to remove. |
| 318 | */ |
| 319 | void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, |
| 320 | const char *link_name) |
| 321 | { |
| 322 | struct sysfs_dirent *dir_sd; |
| 323 | |
| 324 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name); |
| 325 | if (dir_sd) { |
| 326 | sysfs_hash_and_remove(dir_sd, NULL, link_name); |
| 327 | sysfs_put(dir_sd); |
| 328 | } |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group); |