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 | * |
| 7 | * This file is released undert the GPL v2. |
| 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 | { |
| 22 | struct attribute *const* attr; |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 23 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 25 | for (i = 0, attr = grp->attrs; *attr; i++, attr++) |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 26 | sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | } |
| 28 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 29 | static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 30 | const struct attribute_group *grp, int update) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
| 32 | struct attribute *const* attr; |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 33 | int error = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 35 | for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 36 | umode_t mode = 0; |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 37 | |
| 38 | /* in update mode, we're changing the permissions or |
| 39 | * visibility. Do this by first removing then |
| 40 | * re-adding (if required) the file */ |
| 41 | if (update) |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 42 | sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 43 | if (grp->is_visible) { |
| 44 | mode = grp->is_visible(kobj, *attr, i); |
| 45 | if (!mode) |
| 46 | continue; |
| 47 | } |
| 48 | error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR, |
| 49 | (*attr)->mode | mode); |
| 50 | if (unlikely(error)) |
| 51 | break; |
| 52 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (error) |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 54 | remove_files(dir_sd, kobj, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | return error; |
| 56 | } |
| 57 | |
| 58 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 59 | static int internal_create_group(struct kobject *kobj, int update, |
| 60 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 62 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | int error; |
| 64 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 65 | BUG_ON(!kobj || (!update && !kobj->sd)); |
| 66 | |
| 67 | /* Updates may happen before the object has been instantiated */ |
| 68 | if (unlikely(update && !kobj->sd)) |
| 69 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 72 | error = sysfs_create_subdir(kobj, grp->name, &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | if (error) |
| 74 | return error; |
| 75 | } else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 76 | sd = kobj->sd; |
| 77 | sysfs_get(sd); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 78 | error = create_files(sd, kobj, grp, update); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 79 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 81 | sysfs_remove_subdir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 83 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | return error; |
| 85 | } |
| 86 | |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 87 | /** |
| 88 | * sysfs_create_group - given a directory kobject, create an attribute group |
| 89 | * @kobj: The kobject to create the group on |
| 90 | * @grp: The attribute group to create |
| 91 | * |
| 92 | * This function creates a group for the first time. It will explicitly |
| 93 | * warn and error if any of the attribute files being created already exist. |
| 94 | * |
| 95 | * Returns 0 on success or error. |
| 96 | */ |
| 97 | int sysfs_create_group(struct kobject *kobj, |
| 98 | const struct attribute_group *grp) |
| 99 | { |
| 100 | return internal_create_group(kobj, 0, grp); |
| 101 | } |
| 102 | |
| 103 | /** |
Robert P. J. Day | 1f8e1cd | 2011-05-07 17:18:20 -0400 | [diff] [blame] | 104 | * sysfs_update_group - given a directory kobject, update an attribute group |
| 105 | * @kobj: The kobject to update the group on |
| 106 | * @grp: The attribute group to update |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 107 | * |
| 108 | * This function updates an attribute group. Unlike |
| 109 | * sysfs_create_group(), it will explicitly not warn or error if any |
| 110 | * of the attribute files being created already exist. Furthermore, |
| 111 | * if the visibility of the files has changed through the is_visible() |
| 112 | * callback, it will update the permissions and add or remove the |
| 113 | * relevant files. |
| 114 | * |
| 115 | * The primary use for this function is to call it after making a change |
| 116 | * that affects group visibility. |
| 117 | * |
| 118 | * Returns 0 on success or error. |
| 119 | */ |
| 120 | int sysfs_update_group(struct kobject *kobj, |
| 121 | const struct attribute_group *grp) |
| 122 | { |
| 123 | return internal_create_group(kobj, 1, grp); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | void sysfs_remove_group(struct kobject * kobj, |
| 129 | const struct attribute_group * grp) |
| 130 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 131 | struct sysfs_dirent *dir_sd = kobj->sd; |
| 132 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 134 | if (grp->name) { |
Eric W. Biederman | 3ff195b | 2010-03-30 11:31:26 -0700 | [diff] [blame] | 135 | sd = sysfs_get_dirent(dir_sd, NULL, grp->name); |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 136 | if (!sd) { |
Arjan van de Ven | 99fcd77 | 2008-07-25 19:45:41 -0700 | [diff] [blame] | 137 | WARN(!sd, KERN_WARNING "sysfs group %p not found for " |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 138 | "kobject '%s'\n", grp, kobject_name(kobj)); |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 139 | return; |
| 140 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 141 | } else |
| 142 | sd = sysfs_get(dir_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 144 | remove_files(sd, kobj, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 146 | sysfs_remove_subdir(sd); |
| 147 | |
| 148 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 151 | /** |
| 152 | * sysfs_merge_group - merge files into a pre-existing attribute group. |
| 153 | * @kobj: The kobject containing the group. |
| 154 | * @grp: The files to create and the attribute group they belong to. |
| 155 | * |
| 156 | * This function returns an error if the group doesn't exist or any of the |
| 157 | * files already exist in that group, in which case none of the new files |
| 158 | * are created. |
| 159 | */ |
| 160 | int sysfs_merge_group(struct kobject *kobj, |
| 161 | const struct attribute_group *grp) |
| 162 | { |
| 163 | struct sysfs_dirent *dir_sd; |
| 164 | int error = 0; |
| 165 | struct attribute *const *attr; |
| 166 | int i; |
| 167 | |
Alan Stern | e030d58 | 2010-11-15 15:46:07 -0500 | [diff] [blame] | 168 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name); |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 169 | if (!dir_sd) |
| 170 | return -ENOENT; |
| 171 | |
| 172 | for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr)) |
| 173 | error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR); |
| 174 | if (error) { |
| 175 | while (--i >= 0) |
| 176 | sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name); |
| 177 | } |
| 178 | sysfs_put(dir_sd); |
| 179 | |
| 180 | return error; |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(sysfs_merge_group); |
| 183 | |
| 184 | /** |
| 185 | * sysfs_unmerge_group - remove files from a pre-existing attribute group. |
| 186 | * @kobj: The kobject containing the group. |
| 187 | * @grp: The files to remove and the attribute group they belong to. |
| 188 | */ |
| 189 | void sysfs_unmerge_group(struct kobject *kobj, |
| 190 | const struct attribute_group *grp) |
| 191 | { |
| 192 | struct sysfs_dirent *dir_sd; |
| 193 | struct attribute *const *attr; |
| 194 | |
Alan Stern | e030d58 | 2010-11-15 15:46:07 -0500 | [diff] [blame] | 195 | dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name); |
Alan Stern | 69d44ff | 2010-09-25 23:34:22 +0200 | [diff] [blame] | 196 | if (dir_sd) { |
| 197 | for (attr = grp->attrs; *attr; ++attr) |
| 198 | sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); |
| 199 | sysfs_put(dir_sd); |
| 200 | } |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(sysfs_unmerge_group); |
| 203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | EXPORT_SYMBOL_GPL(sysfs_create_group); |
James Bottomley | 0f42389 | 2008-03-20 20:47:52 -0500 | [diff] [blame] | 206 | EXPORT_SYMBOL_GPL(sysfs_update_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | EXPORT_SYMBOL_GPL(sysfs_remove_group); |