1
2
3
4
5
6
7
8
9
10
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>
16#include "sysfs.h"
17
18
19static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
20 const struct attribute_group *grp)
21{
22 struct attribute *const* attr;
23 int i;
24
25 for (i = 0, attr = grp->attrs; *attr; i++, attr++)
26 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
27}
28
29static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
30 const struct attribute_group *grp, int update)
31{
32 struct attribute *const* attr;
33 int error = 0, i;
34
35 for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
36 umode_t mode = 0;
37
38
39
40
41 if (update)
42 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
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 }
53 if (error)
54 remove_files(dir_sd, kobj, grp);
55 return error;
56}
57
58
59static int internal_create_group(struct kobject *kobj, int update,
60 const struct attribute_group *grp)
61{
62 struct sysfs_dirent *sd;
63 int error;
64
65 BUG_ON(!kobj || (!update && !kobj->sd));
66
67
68 if (unlikely(update && !kobj->sd))
69 return -EINVAL;
70 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 }
75 if (grp->name) {
76 error = sysfs_create_subdir(kobj, grp->name, &sd);
77 if (error)
78 return error;
79 } else
80 sd = kobj->sd;
81 sysfs_get(sd);
82 error = create_files(sd, kobj, grp, update);
83 if (error) {
84 if (grp->name)
85 sysfs_remove_subdir(sd);
86 }
87 sysfs_put(sd);
88 return error;
89}
90
91
92
93
94
95
96
97
98
99
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
132void sysfs_remove_group(struct kobject * kobj,
133 const struct attribute_group * grp)
134{
135 struct sysfs_dirent *dir_sd = kobj->sd;
136 struct sysfs_dirent *sd;
137
138 if (grp->name) {
139 sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
140 if (!sd) {
141 WARN(!sd, KERN_WARNING "sysfs group %p not found for "
142 "kobject '%s'\n", grp, kobject_name(kobj));
143 return;
144 }
145 } else
146 sd = sysfs_get(dir_sd);
147
148 remove_files(sd, kobj, grp);
149 if (grp->name)
150 sysfs_remove_subdir(sd);
151
152 sysfs_put(sd);
153}
154
155
156
157
158
159
160
161
162
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
172 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
173 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
190
191
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
199 dir_sd = sysfs_get_dirent(kobj->sd, NULL, grp->name);
200 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
208
209EXPORT_SYMBOL_GPL(sysfs_create_group);
210EXPORT_SYMBOL_GPL(sysfs_update_group);
211EXPORT_SYMBOL_GPL(sysfs_remove_group);
212