2 * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released undert the GPL v2.
11 #include <linux/kobject.h>
12 #include <linux/module.h>
13 #include <linux/dcache.h>
14 #include <linux/namei.h>
15 #include <linux/err.h>
19 static void remove_files(struct sysfs_dirent
*dir_sd
, struct kobject
*kobj
,
20 const struct attribute_group
*grp
)
22 struct attribute
*const* attr
;
23 struct bin_attribute
*const* bin_attr
;
26 for (attr
= grp
->attrs
; *attr
; attr
++)
27 sysfs_hash_and_remove(dir_sd
, NULL
, (*attr
)->name
);
29 for (bin_attr
= grp
->bin_attrs
; *bin_attr
; bin_attr
++)
30 sysfs_remove_bin_file(kobj
, *bin_attr
);
33 static int create_files(struct sysfs_dirent
*dir_sd
, struct kobject
*kobj
,
34 const struct attribute_group
*grp
, int update
)
36 struct attribute
*const* attr
;
37 struct bin_attribute
*const* bin_attr
;
41 for (i
= 0, attr
= grp
->attrs
; *attr
&& !error
; i
++, attr
++) {
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.
50 sysfs_hash_and_remove(dir_sd
, NULL
,
52 if (grp
->is_visible
) {
53 mode
= grp
->is_visible(kobj
, *attr
, i
);
57 error
= sysfs_add_file_mode(dir_sd
, *attr
,
59 (*attr
)->mode
| mode
);
64 remove_files(dir_sd
, kobj
, grp
);
70 for (bin_attr
= grp
->bin_attrs
; *bin_attr
; bin_attr
++) {
72 sysfs_remove_bin_file(kobj
, *bin_attr
);
73 error
= sysfs_create_bin_file(kobj
, *bin_attr
);
78 remove_files(dir_sd
, kobj
, grp
);
85 static int internal_create_group(struct kobject
*kobj
, int update
,
86 const struct attribute_group
*grp
)
88 struct sysfs_dirent
*sd
;
91 BUG_ON(!kobj
|| (!update
&& !kobj
->sd
));
93 /* Updates may happen before the object has been instantiated */
94 if (unlikely(update
&& !kobj
->sd
))
96 if (!grp
->attrs
&& !grp
->bin_attrs
) {
97 WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s\n",
98 kobj
->name
, grp
->name
? "" : grp
->name
);
102 error
= sysfs_create_subdir(kobj
, grp
->name
, &sd
);
108 error
= create_files(sd
, kobj
, grp
, update
);
111 sysfs_remove_subdir(sd
);
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
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.
125 * Returns 0 on success or error.
127 int sysfs_create_group(struct kobject
*kobj
,
128 const struct attribute_group
*grp
)
130 return internal_create_group(kobj
, 0, grp
);
134 * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
135 * @kobj: The kobject to create the group on
136 * @groups: The attribute groups to create, NULL terminated
138 * This function creates a bunch of attribute groups. If an error occurs when
139 * creating a group, all previously created groups will be removed, unwinding
140 * everything back to the original state when this function was called.
141 * It will explicitly warn and error if any of the attribute files being
142 * created already exist.
144 * Returns 0 on success or error code from sysfs_create_groups on error.
146 int sysfs_create_groups(struct kobject
*kobj
,
147 const struct attribute_group
**groups
)
155 for (i
= 0; groups
[i
]; i
++) {
156 error
= sysfs_create_group(kobj
, groups
[i
]);
159 sysfs_remove_group(kobj
, groups
[i
]);
165 EXPORT_SYMBOL_GPL(sysfs_create_groups
);
168 * sysfs_update_group - given a directory kobject, update an attribute group
169 * @kobj: The kobject to update the group on
170 * @grp: The attribute group to update
172 * This function updates an attribute group. Unlike
173 * sysfs_create_group(), it will explicitly not warn or error if any
174 * of the attribute files being created already exist. Furthermore,
175 * if the visibility of the files has changed through the is_visible()
176 * callback, it will update the permissions and add or remove the
179 * The primary use for this function is to call it after making a change
180 * that affects group visibility.
182 * Returns 0 on success or error.
184 int sysfs_update_group(struct kobject
*kobj
,
185 const struct attribute_group
*grp
)
187 return internal_create_group(kobj
, 1, grp
);
192 void sysfs_remove_group(struct kobject
* kobj
,
193 const struct attribute_group
* grp
)
195 struct sysfs_dirent
*dir_sd
= kobj
->sd
;
196 struct sysfs_dirent
*sd
;
199 sd
= sysfs_get_dirent(dir_sd
, NULL
, grp
->name
);
201 WARN(!sd
, KERN_WARNING
"sysfs group %p not found for "
202 "kobject '%s'\n", grp
, kobject_name(kobj
));
206 sd
= sysfs_get(dir_sd
);
208 remove_files(sd
, kobj
, grp
);
210 sysfs_remove_subdir(sd
);
216 * sysfs_remove_groups - remove a list of groups
218 * kobj: The kobject for the groups to be removed from
219 * groups: NULL terminated list of groups to be removed
221 * If groups is not NULL, the all groups will be removed from the kobject
223 void sysfs_remove_groups(struct kobject
*kobj
,
224 const struct attribute_group
**groups
)
230 for (i
= 0; groups
[i
]; i
++)
231 sysfs_remove_group(kobj
, groups
[i
]);
233 EXPORT_SYMBOL_GPL(sysfs_remove_groups
);
236 * sysfs_merge_group - merge files into a pre-existing attribute group.
237 * @kobj: The kobject containing the group.
238 * @grp: The files to create and the attribute group they belong to.
240 * This function returns an error if the group doesn't exist or any of the
241 * files already exist in that group, in which case none of the new files
244 int sysfs_merge_group(struct kobject
*kobj
,
245 const struct attribute_group
*grp
)
247 struct sysfs_dirent
*dir_sd
;
249 struct attribute
*const *attr
;
252 dir_sd
= sysfs_get_dirent(kobj
->sd
, NULL
, grp
->name
);
256 for ((i
= 0, attr
= grp
->attrs
); *attr
&& !error
; (++i
, ++attr
))
257 error
= sysfs_add_file(dir_sd
, *attr
, SYSFS_KOBJ_ATTR
);
260 sysfs_hash_and_remove(dir_sd
, NULL
, (*--attr
)->name
);
266 EXPORT_SYMBOL_GPL(sysfs_merge_group
);
269 * sysfs_unmerge_group - remove files from a pre-existing attribute group.
270 * @kobj: The kobject containing the group.
271 * @grp: The files to remove and the attribute group they belong to.
273 void sysfs_unmerge_group(struct kobject
*kobj
,
274 const struct attribute_group
*grp
)
276 struct sysfs_dirent
*dir_sd
;
277 struct attribute
*const *attr
;
279 dir_sd
= sysfs_get_dirent(kobj
->sd
, NULL
, grp
->name
);
281 for (attr
= grp
->attrs
; *attr
; ++attr
)
282 sysfs_hash_and_remove(dir_sd
, NULL
, (*attr
)->name
);
286 EXPORT_SYMBOL_GPL(sysfs_unmerge_group
);
289 * sysfs_add_link_to_group - add a symlink to an attribute group.
290 * @kobj: The kobject containing the group.
291 * @group_name: The name of the group.
292 * @target: The target kobject of the symlink to create.
293 * @link_name: The name of the symlink to create.
295 int sysfs_add_link_to_group(struct kobject
*kobj
, const char *group_name
,
296 struct kobject
*target
, const char *link_name
)
298 struct sysfs_dirent
*dir_sd
;
301 dir_sd
= sysfs_get_dirent(kobj
->sd
, NULL
, group_name
);
305 error
= sysfs_create_link_sd(dir_sd
, target
, link_name
);
310 EXPORT_SYMBOL_GPL(sysfs_add_link_to_group
);
313 * sysfs_remove_link_from_group - remove a symlink from an attribute group.
314 * @kobj: The kobject containing the group.
315 * @group_name: The name of the group.
316 * @link_name: The name of the symlink to remove.
318 void sysfs_remove_link_from_group(struct kobject
*kobj
, const char *group_name
,
319 const char *link_name
)
321 struct sysfs_dirent
*dir_sd
;
323 dir_sd
= sysfs_get_dirent(kobj
->sd
, NULL
, group_name
);
325 sysfs_hash_and_remove(dir_sd
, NULL
, link_name
);
329 EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group
);
331 EXPORT_SYMBOL_GPL(sysfs_create_group
);
332 EXPORT_SYMBOL_GPL(sysfs_update_group
);
333 EXPORT_SYMBOL_GPL(sysfs_remove_group
);