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;
25 for (i = 0, attr = grp->attrs; *attr; i++, attr++)
26 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
29 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
30 const struct attribute_group *grp, int update)
32 struct attribute *const* attr;
35 for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
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 */
42 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
43 if (grp->is_visible) {
44 mode = grp->is_visible(kobj, *attr, i);
48 error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
49 (*attr)->mode | mode);
54 remove_files(dir_sd, kobj, grp);
59 static int internal_create_group(struct kobject *kobj, int update,
60 const struct attribute_group *grp)
62 struct sysfs_dirent *sd;
65 BUG_ON(!kobj || (!update && !kobj->sd));
67 /* Updates may happen before the object has been instantiated */
68 if (unlikely(update && !kobj->sd))
71 WARN(1, "sysfs: attrs not set by subsystem for group: %s/%s\n",
72 kobj->name, grp->name ? "" : grp->name);
76 error = sysfs_create_subdir(kobj, grp->name, &sd);
82 error = create_files(sd, kobj, grp, update);
85 sysfs_remove_subdir(sd);
92 * sysfs_create_group - given a directory kobject, create an attribute group
93 * @kobj: The kobject to create the group on
94 * @grp: The attribute group to create
96 * This function creates a group for the first time. It will explicitly
97 * warn and error if any of the attribute files being created already exist.
99 * Returns 0 on success or error.
101 int sysfs_create_group(struct kobject *kobj,
102 const struct attribute_group *grp)
104 return internal_create_group(kobj, 0, grp);
108 * sysfs_update_group - given a directory kobject, update an attribute group
109 * @kobj: The kobject to update the group on
110 * @grp: The attribute group to update
112 * This function updates an attribute group. Unlike
113 * sysfs_create_group(), it will explicitly not warn or error if any
114 * of the attribute files being created already exist. Furthermore,
115 * if the visibility of the files has changed through the is_visible()
116 * callback, it will update the permissions and add or remove the
119 * The primary use for this function is to call it after making a change
120 * that affects group visibility.
122 * Returns 0 on success or error.
124 int sysfs_update_group(struct kobject *kobj,
125 const struct attribute_group *grp)
127 return internal_create_group(kobj, 1, grp);
132 void sysfs_remove_group(struct kobject * kobj,
133 const struct attribute_group * grp)
135 struct sysfs_dirent *dir_sd = kobj->sd;
136 struct sysfs_dirent *sd;
139 sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
141 WARN(!sd, KERN_WARNING "sysfs group %p not found for "
142 "kobject '%s'\n", grp, kobject_name(kobj));
146 sd = sysfs_get(dir_sd);
148 remove_files(sd, kobj, grp);
150 sysfs_remove_subdir(sd);
156 * sysfs_merge_group - merge files into a pre-existing attribute group.
157 * @kobj: The kobject containing the group.
158 * @grp: The files to create and the attribute group they belong to.
160 * This function returns an error if the group doesn't exist or any of the
161 * files already exist in that group, in which case none of the new files
164 int sysfs_merge_group(struct kobject *kobj,
165 const struct attribute_group *grp)
167 struct sysfs_dirent *dir_sd;
169 struct attribute *const *attr;
172 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
176 for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
177 error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
180 sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
186 EXPORT_SYMBOL_GPL(sysfs_merge_group);
189 * sysfs_unmerge_group - remove files from a pre-existing attribute group.
190 * @kobj: The kobject containing the group.
191 * @grp: The files to remove and the attribute group they belong to.
193 void sysfs_unmerge_group(struct kobject *kobj,
194 const struct attribute_group *grp)
196 struct sysfs_dirent *dir_sd;
197 struct attribute *const *attr;
199 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
201 for (attr = grp->attrs; *attr; ++attr)
202 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
206 EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
209 EXPORT_SYMBOL_GPL(sysfs_create_group);
210 EXPORT_SYMBOL_GPL(sysfs_update_group);
211 EXPORT_SYMBOL_GPL(sysfs_remove_group);