blob: aec3d5c98c94fa1d3d79de15a44dfa2a4a77e329 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hellwig5f45f1a2005-06-23 00:09:12 -070014#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/err.h>
16#include "sysfs.h"
17
18
James Bottomleyd4acd722007-10-31 09:38:04 -050019static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
Tejun Heo608e2662007-06-14 04:27:22 +090020 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct attribute *const* attr;
James Bottomleyd4acd722007-10-31 09:38:04 -050023 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
James Bottomleyd4acd722007-10-31 09:38:04 -050025 for (i = 0, attr = grp->attrs; *attr; i++, attr++)
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070026 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
James Bottomleyd4acd722007-10-31 09:38:04 -050029static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
James Bottomley0f423892008-03-20 20:47:52 -050030 const struct attribute_group *grp, int update)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct attribute *const* attr;
James Bottomleyd4acd722007-10-31 09:38:04 -050033 int error = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
James Bottomley0f423892008-03-20 20:47:52 -050035 for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
Al Viro587a1f12011-07-23 23:11:19 -040036 umode_t mode = 0;
James Bottomley0f423892008-03-20 20:47:52 -050037
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. Biederman3ff195b2010-03-30 11:31:26 -070042 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
James Bottomley0f423892008-03-20 20:47:52 -050043 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 Torvalds1da177e2005-04-16 15:20:36 -070053 if (error)
James Bottomleyd4acd722007-10-31 09:38:04 -050054 remove_files(dir_sd, kobj, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return error;
56}
57
58
James Bottomley0f423892008-03-20 20:47:52 -050059static int internal_create_group(struct kobject *kobj, int update,
60 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Tejun Heo608e2662007-06-14 04:27:22 +090062 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int error;
64
James Bottomley0f423892008-03-20 20:47:52 -050065 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;
Bruno Prémont5631f2c2012-04-03 09:59:48 +020070 if (!grp->attrs) {
71 WARN(1, "sysfs: attrs not set by subsystem for group: %s/%s\n",
72 kobj->name, grp->name ? "" : grp->name);
73 return -EINVAL;
74 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (grp->name) {
Tejun Heo608e2662007-06-14 04:27:22 +090076 error = sysfs_create_subdir(kobj, grp->name, &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 if (error)
78 return error;
79 } else
Tejun Heo608e2662007-06-14 04:27:22 +090080 sd = kobj->sd;
81 sysfs_get(sd);
James Bottomley0f423892008-03-20 20:47:52 -050082 error = create_files(sd, kobj, grp, update);
Tejun Heo608e2662007-06-14 04:27:22 +090083 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (grp->name)
Tejun Heo608e2662007-06-14 04:27:22 +090085 sysfs_remove_subdir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
Tejun Heo608e2662007-06-14 04:27:22 +090087 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return error;
89}
90
James Bottomley0f423892008-03-20 20:47:52 -050091/**
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
95 *
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.
98 *
99 * Returns 0 on success or error.
100 */
101int sysfs_create_group(struct kobject *kobj,
102 const struct attribute_group *grp)
103{
104 return internal_create_group(kobj, 0, grp);
105}
106
107/**
Robert P. J. Day1f8e1cd2011-05-07 17:18:20 -0400108 * 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
James Bottomley0f423892008-03-20 20:47:52 -0500111 *
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
117 * relevant files.
118 *
119 * The primary use for this function is to call it after making a change
120 * that affects group visibility.
121 *
122 * Returns 0 on success or error.
123 */
124int sysfs_update_group(struct kobject *kobj,
125 const struct attribute_group *grp)
126{
127 return internal_create_group(kobj, 1, grp);
128}
129
130
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132void sysfs_remove_group(struct kobject * kobj,
133 const struct attribute_group * grp)
134{
Tejun Heo608e2662007-06-14 04:27:22 +0900135 struct sysfs_dirent *dir_sd = kobj->sd;
136 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
James Morris057f6c02007-04-26 00:12:05 -0700138 if (grp->name) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700139 sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500140 if (!sd) {
Arjan van de Ven99fcd772008-07-25 19:45:41 -0700141 WARN(!sd, KERN_WARNING "sysfs group %p not found for "
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500142 "kobject '%s'\n", grp, kobject_name(kobj));
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500143 return;
144 }
Tejun Heo608e2662007-06-14 04:27:22 +0900145 } else
146 sd = sysfs_get(dir_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
James Bottomleyd4acd722007-10-31 09:38:04 -0500148 remove_files(sd, kobj, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (grp->name)
Tejun Heo608e2662007-06-14 04:27:22 +0900150 sysfs_remove_subdir(sd);
151
152 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Alan Stern69d44ff2010-09-25 23:34:22 +0200155/**
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.
159 *
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
162 * are created.
163 */
164int sysfs_merge_group(struct kobject *kobj,
165 const struct attribute_group *grp)
166{
167 struct sysfs_dirent *dir_sd;
168 int error = 0;
169 struct attribute *const *attr;
170 int i;
171
Alan Sterne030d582010-11-15 15:46:07 -0500172 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
Alan Stern69d44ff2010-09-25 23:34:22 +0200173 if (!dir_sd)
174 return -ENOENT;
175
176 for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr))
177 error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR);
178 if (error) {
179 while (--i >= 0)
180 sysfs_hash_and_remove(dir_sd, NULL, (*--attr)->name);
181 }
182 sysfs_put(dir_sd);
183
184 return error;
185}
186EXPORT_SYMBOL_GPL(sysfs_merge_group);
187
188/**
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.
192 */
193void sysfs_unmerge_group(struct kobject *kobj,
194 const struct attribute_group *grp)
195{
196 struct sysfs_dirent *dir_sd;
197 struct attribute *const *attr;
198
Alan Sterne030d582010-11-15 15:46:07 -0500199 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
Alan Stern69d44ff2010-09-25 23:34:22 +0200200 if (dir_sd) {
201 for (attr = grp->attrs; *attr; ++attr)
202 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
203 sysfs_put(dir_sd);
204 }
205}
206EXPORT_SYMBOL_GPL(sysfs_unmerge_group);
207
Rafael J. Wysocki0bb8f3d2013-01-25 21:51:13 +0100208/**
209 * sysfs_add_link_to_group - add a symlink to an attribute group.
210 * @kobj: The kobject containing the group.
211 * @group_name: The name of the group.
212 * @target: The target kobject of the symlink to create.
213 * @link_name: The name of the symlink to create.
214 */
215int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name,
216 struct kobject *target, const char *link_name)
217{
218 struct sysfs_dirent *dir_sd;
219 int error = 0;
220
221 dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
222 if (!dir_sd)
223 return -ENOENT;
224
225 error = sysfs_create_link_sd(dir_sd, target, link_name);
226 sysfs_put(dir_sd);
227
228 return error;
229}
230EXPORT_SYMBOL_GPL(sysfs_add_link_to_group);
231
232/**
233 * sysfs_remove_link_from_group - remove a symlink from an attribute group.
234 * @kobj: The kobject containing the group.
235 * @group_name: The name of the group.
236 * @link_name: The name of the symlink to remove.
237 */
238void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name,
239 const char *link_name)
240{
241 struct sysfs_dirent *dir_sd;
242
243 dir_sd = sysfs_get_dirent(kobj->sd, NULL, group_name);
244 if (dir_sd) {
245 sysfs_hash_and_remove(dir_sd, NULL, link_name);
246 sysfs_put(dir_sd);
247 }
248}
249EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251EXPORT_SYMBOL_GPL(sysfs_create_group);
James Bottomley0f423892008-03-20 20:47:52 -0500252EXPORT_SYMBOL_GPL(sysfs_update_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253EXPORT_SYMBOL_GPL(sysfs_remove_group);