Blame view
fs/sysfs/group.c
12.3 KB
619daeeeb
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
1da177e4c
|
2 3 4 5 6 |
/* * fs/sysfs/group.c - Operations for adding/removing multiple files at once. * * Copyright (c) 2003 Patrick Mochel * Copyright (c) 2003 Open Source Development Lab |
9e2a47ed6
|
7 8 |
* Copyright (c) 2013 Greg Kroah-Hartman * Copyright (c) 2013 The Linux Foundation |
1da177e4c
|
9 10 11 12 13 |
*/ #include <linux/kobject.h> #include <linux/module.h> #include <linux/dcache.h> |
5f45f1a78
|
14 |
#include <linux/namei.h> |
1da177e4c
|
15 16 |
#include <linux/err.h> #include "sysfs.h" |
9f70a4012
|
17 |
static void remove_files(struct kernfs_node *parent, |
608e266a2
|
18 |
const struct attribute_group *grp) |
1da177e4c
|
19 |
{ |
995d8ed94
|
20 21 |
struct attribute *const *attr; struct bin_attribute *const *bin_attr; |
1da177e4c
|
22 |
|
6ab9cea16
|
23 24 |
if (grp->attrs) for (attr = grp->attrs; *attr; attr++) |
324a56e16
|
25 |
kernfs_remove_by_name(parent, (*attr)->name); |
6ab9cea16
|
26 27 |
if (grp->bin_attrs) for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) |
9f70a4012
|
28 |
kernfs_remove_by_name(parent, (*bin_attr)->attr.name); |
1da177e4c
|
29 |
} |
324a56e16
|
30 |
static int create_files(struct kernfs_node *parent, struct kobject *kobj, |
5f81880d5
|
31 |
kuid_t uid, kgid_t gid, |
0f4238958
|
32 |
const struct attribute_group *grp, int update) |
1da177e4c
|
33 |
{ |
995d8ed94
|
34 35 |
struct attribute *const *attr; struct bin_attribute *const *bin_attr; |
d4acd722b
|
36 |
int error = 0, i; |
1da177e4c
|
37 |
|
6ab9cea16
|
38 39 |
if (grp->attrs) { for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { |
da4759c73
|
40 |
umode_t mode = (*attr)->mode; |
0f4238958
|
41 |
|
6ab9cea16
|
42 43 44 45 46 47 |
/* * In update mode, we're changing the permissions or * visibility. Do this by first removing then * re-adding (if required) the file. */ if (update) |
324a56e16
|
48 |
kernfs_remove_by_name(parent, (*attr)->name); |
6ab9cea16
|
49 50 51 52 53 |
if (grp->is_visible) { mode = grp->is_visible(kobj, *attr, i); if (!mode) continue; } |
d8bf8c92e
|
54 55 56 57 58 59 60 |
WARN(mode & ~(SYSFS_PREALLOC | 0664), "Attribute %s: Invalid permissions 0%o ", (*attr)->name, mode); mode &= SYSFS_PREALLOC | 0664; |
324a56e16
|
61 |
error = sysfs_add_file_mode_ns(parent, *attr, false, |
5f81880d5
|
62 |
mode, uid, gid, NULL); |
6ab9cea16
|
63 64 65 66 |
if (unlikely(error)) break; } if (error) { |
9f70a4012
|
67 |
remove_files(parent, grp); |
6ab9cea16
|
68 69 70 71 72 |
goto exit; } } if (grp->bin_attrs) { |
7f5028cf6
|
73 74 |
for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) { umode_t mode = (*bin_attr)->attr.mode; |
6ab9cea16
|
75 |
if (update) |
aabaf4c20
|
76 77 |
kernfs_remove_by_name(parent, (*bin_attr)->attr.name); |
7f5028cf6
|
78 79 80 81 82 83 84 85 86 87 88 89 |
if (grp->is_bin_visible) { mode = grp->is_bin_visible(kobj, *bin_attr, i); if (!mode) continue; } WARN(mode & ~(SYSFS_PREALLOC | 0664), "Attribute %s: Invalid permissions 0%o ", (*bin_attr)->attr.name, mode); mode &= SYSFS_PREALLOC | 0664; |
aabaf4c20
|
90 91 |
error = sysfs_add_file_mode_ns(parent, &(*bin_attr)->attr, true, |
5f81880d5
|
92 93 |
mode, uid, gid, NULL); |
6ab9cea16
|
94 95 |
if (error) break; |
0f4238958
|
96 |
} |
6ab9cea16
|
97 |
if (error) |
9f70a4012
|
98 |
remove_files(parent, grp); |
0f4238958
|
99 |
} |
6ab9cea16
|
100 |
exit: |
1da177e4c
|
101 102 |
return error; } |
0f4238958
|
103 104 |
static int internal_create_group(struct kobject *kobj, int update, const struct attribute_group *grp) |
1da177e4c
|
105 |
{ |
324a56e16
|
106 |
struct kernfs_node *kn; |
5f81880d5
|
107 108 |
kuid_t uid; kgid_t gid; |
1da177e4c
|
109 |
int error; |
de96e9fea
|
110 111 |
if (WARN_ON(!kobj || (!update && !kobj->sd))) return -EINVAL; |
0f4238958
|
112 113 114 115 |
/* Updates may happen before the object has been instantiated */ if (unlikely(update && !kobj->sd)) return -EINVAL; |
388a8c353
|
116 117 118 |
if (!grp->attrs && !grp->bin_attrs) { WARN(1, "sysfs: (bin_)attrs not set by subsystem for group: %s/%s ", |
adf305f77
|
119 |
kobj->name, grp->name ?: ""); |
5631f2c18
|
120 121 |
return -EINVAL; } |
5f81880d5
|
122 |
kobject_get_ownership(kobj, &uid, &gid); |
1da177e4c
|
123 |
if (grp->name) { |
c855cf275
|
124 125 126 127 128 129 130 131 132 |
if (update) { kn = kernfs_find_and_get(kobj->sd, grp->name); if (!kn) { pr_warn("Can't update unknown attr grp name: %s/%s ", kobj->name, grp->name); return -EINVAL; } } else { |
a18d783fe
|
133 134 135 |
kn = kernfs_create_dir_ns(kobj->sd, grp->name, S_IRWXU | S_IRUGO | S_IXUGO, uid, gid, kobj, NULL); |
c855cf275
|
136 137 138 139 140 |
if (IS_ERR(kn)) { if (PTR_ERR(kn) == -EEXIST) sysfs_warn_dup(kobj->sd, grp->name); return PTR_ERR(kn); } |
93b2b8e4a
|
141 |
} |
1da177e4c
|
142 |
} else |
324a56e16
|
143 144 |
kn = kobj->sd; kernfs_get(kn); |
5f81880d5
|
145 |
error = create_files(kn, kobj, uid, gid, grp, update); |
608e266a2
|
146 |
if (error) { |
1da177e4c
|
147 |
if (grp->name) |
324a56e16
|
148 |
kernfs_remove(kn); |
1da177e4c
|
149 |
} |
324a56e16
|
150 |
kernfs_put(kn); |
c855cf275
|
151 152 153 |
if (grp->name && update) kernfs_put(kn); |
1da177e4c
|
154 155 |
return error; } |
0f4238958
|
156 157 158 159 160 161 162 163 |
/** * sysfs_create_group - given a directory kobject, create an attribute group * @kobj: The kobject to create the group on * @grp: The attribute group to create * * This function creates a group for the first time. It will explicitly * warn and error if any of the attribute files being created already exist. * |
ed1dc8a89
|
164 |
* Returns 0 on success or error code on failure. |
0f4238958
|
165 166 167 168 169 170 |
*/ int sysfs_create_group(struct kobject *kobj, const struct attribute_group *grp) { return internal_create_group(kobj, 0, grp); } |
d363bc53e
|
171 |
EXPORT_SYMBOL_GPL(sysfs_create_group); |
0f4238958
|
172 |
|
aac1f7f95
|
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
static int internal_create_groups(struct kobject *kobj, int update, const struct attribute_group **groups) { int error = 0; int i; if (!groups) return 0; for (i = 0; groups[i]; i++) { error = internal_create_group(kobj, update, groups[i]); if (error) { while (--i >= 0) sysfs_remove_group(kobj, groups[i]); break; } } return error; } |
0f4238958
|
192 |
/** |
3e9b2bae8
|
193 194 195 196 197 198 199 200 201 202 |
* sysfs_create_groups - given a directory kobject, create a bunch of attribute groups * @kobj: The kobject to create the group on * @groups: The attribute groups to create, NULL terminated * * This function creates a bunch of attribute groups. If an error occurs when * creating a group, all previously created groups will be removed, unwinding * everything back to the original state when this function was called. * It will explicitly warn and error if any of the attribute files being * created already exist. * |
ed1dc8a89
|
203 |
* Returns 0 on success or error code from sysfs_create_group on failure. |
3e9b2bae8
|
204 205 206 207 |
*/ int sysfs_create_groups(struct kobject *kobj, const struct attribute_group **groups) { |
aac1f7f95
|
208 |
return internal_create_groups(kobj, 0, groups); |
3e9b2bae8
|
209 210 211 212 |
} EXPORT_SYMBOL_GPL(sysfs_create_groups); /** |
aac1f7f95
|
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
* sysfs_update_groups - given a directory kobject, create a bunch of attribute groups * @kobj: The kobject to update the group on * @groups: The attribute groups to update, NULL terminated * * This function update a bunch of attribute groups. If an error occurs when * updating a group, all previously updated groups will be removed together * with already existing (not updated) attributes. * * Returns 0 on success or error code from sysfs_update_group on failure. */ int sysfs_update_groups(struct kobject *kobj, const struct attribute_group **groups) { return internal_create_groups(kobj, 1, groups); } EXPORT_SYMBOL_GPL(sysfs_update_groups); /** |
1f8e1cdac
|
231 232 233 |
* sysfs_update_group - given a directory kobject, update an attribute group * @kobj: The kobject to update the group on * @grp: The attribute group to update |
0f4238958
|
234 235 236 237 238 239 |
* * This function updates an attribute group. Unlike * sysfs_create_group(), it will explicitly not warn or error if any * of the attribute files being created already exist. Furthermore, * if the visibility of the files has changed through the is_visible() * callback, it will update the permissions and add or remove the |
c855cf275
|
240 241 |
* relevant files. Changing a group's name (subdirectory name under * kobj's directory in sysfs) is not allowed. |
0f4238958
|
242 243 244 245 |
* * The primary use for this function is to call it after making a change * that affects group visibility. * |
ed1dc8a89
|
246 |
* Returns 0 on success or error code on failure. |
0f4238958
|
247 248 249 250 251 252 |
*/ int sysfs_update_group(struct kobject *kobj, const struct attribute_group *grp) { return internal_create_group(kobj, 1, grp); } |
d363bc53e
|
253 |
EXPORT_SYMBOL_GPL(sysfs_update_group); |
0f4238958
|
254 |
|
f9ae443b5
|
255 256 257 258 259 260 261 262 |
/** * sysfs_remove_group: remove a group from a kobject * @kobj: kobject to remove the group from * @grp: group to remove * * This function removes a group of attributes from a kobject. The attributes * previously have to have been created for this group, otherwise it will fail. */ |
995d8ed94
|
263 264 |
void sysfs_remove_group(struct kobject *kobj, const struct attribute_group *grp) |
1da177e4c
|
265 |
{ |
324a56e16
|
266 267 |
struct kernfs_node *parent = kobj->sd; struct kernfs_node *kn; |
1da177e4c
|
268 |
|
057f6c019
|
269 |
if (grp->name) { |
324a56e16
|
270 271 272 |
kn = kernfs_find_and_get(parent, grp->name); if (!kn) { WARN(!kn, KERN_WARNING |
78618d395
|
273 274 275 |
"sysfs group '%s' not found for kobject '%s' ", grp->name, kobject_name(kobj)); |
969affd27
|
276 277 |
return; } |
ccf73cf33
|
278 |
} else { |
324a56e16
|
279 280 |
kn = parent; kernfs_get(kn); |
ccf73cf33
|
281 |
} |
1da177e4c
|
282 |
|
9f70a4012
|
283 |
remove_files(kn, grp); |
1da177e4c
|
284 |
if (grp->name) |
324a56e16
|
285 |
kernfs_remove(kn); |
608e266a2
|
286 |
|
324a56e16
|
287 |
kernfs_put(kn); |
1da177e4c
|
288 |
} |
d363bc53e
|
289 |
EXPORT_SYMBOL_GPL(sysfs_remove_group); |
1da177e4c
|
290 |
|
69d44ffbd
|
291 |
/** |
3e9b2bae8
|
292 293 |
* sysfs_remove_groups - remove a list of groups * |
f9ae443b5
|
294 295 |
* @kobj: The kobject for the groups to be removed from * @groups: NULL terminated list of groups to be removed |
3e9b2bae8
|
296 |
* |
09239ed4a
|
297 |
* If groups is not NULL, remove the specified groups from the kobject. |
3e9b2bae8
|
298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
*/ void sysfs_remove_groups(struct kobject *kobj, const struct attribute_group **groups) { int i; if (!groups) return; for (i = 0; groups[i]; i++) sysfs_remove_group(kobj, groups[i]); } EXPORT_SYMBOL_GPL(sysfs_remove_groups); /** |
69d44ffbd
|
312 313 314 315 316 317 318 319 320 321 322 |
* sysfs_merge_group - merge files into a pre-existing attribute group. * @kobj: The kobject containing the group. * @grp: The files to create and the attribute group they belong to. * * This function returns an error if the group doesn't exist or any of the * files already exist in that group, in which case none of the new files * are created. */ int sysfs_merge_group(struct kobject *kobj, const struct attribute_group *grp) { |
324a56e16
|
323 |
struct kernfs_node *parent; |
5f81880d5
|
324 325 |
kuid_t uid; kgid_t gid; |
69d44ffbd
|
326 327 328 |
int error = 0; struct attribute *const *attr; int i; |
324a56e16
|
329 330 |
parent = kernfs_find_and_get(kobj->sd, grp->name); if (!parent) |
69d44ffbd
|
331 |
return -ENOENT; |
5f81880d5
|
332 |
kobject_get_ownership(kobj, &uid, &gid); |
69d44ffbd
|
333 |
for ((i = 0, attr = grp->attrs); *attr && !error; (++i, ++attr)) |
5f81880d5
|
334 335 |
error = sysfs_add_file_mode_ns(parent, *attr, false, (*attr)->mode, uid, gid, NULL); |
69d44ffbd
|
336 337 |
if (error) { while (--i >= 0) |
324a56e16
|
338 |
kernfs_remove_by_name(parent, (*--attr)->name); |
69d44ffbd
|
339 |
} |
324a56e16
|
340 |
kernfs_put(parent); |
69d44ffbd
|
341 342 343 344 345 346 347 348 349 350 351 352 353 |
return error; } EXPORT_SYMBOL_GPL(sysfs_merge_group); /** * sysfs_unmerge_group - remove files from a pre-existing attribute group. * @kobj: The kobject containing the group. * @grp: The files to remove and the attribute group they belong to. */ void sysfs_unmerge_group(struct kobject *kobj, const struct attribute_group *grp) { |
324a56e16
|
354 |
struct kernfs_node *parent; |
69d44ffbd
|
355 |
struct attribute *const *attr; |
324a56e16
|
356 357 |
parent = kernfs_find_and_get(kobj->sd, grp->name); if (parent) { |
69d44ffbd
|
358 |
for (attr = grp->attrs; *attr; ++attr) |
324a56e16
|
359 360 |
kernfs_remove_by_name(parent, (*attr)->name); kernfs_put(parent); |
69d44ffbd
|
361 362 363 |
} } EXPORT_SYMBOL_GPL(sysfs_unmerge_group); |
0bb8f3d6a
|
364 365 366 367 368 369 370 371 372 373 |
/** * sysfs_add_link_to_group - add a symlink to an attribute group. * @kobj: The kobject containing the group. * @group_name: The name of the group. * @target: The target kobject of the symlink to create. * @link_name: The name of the symlink to create. */ int sysfs_add_link_to_group(struct kobject *kobj, const char *group_name, struct kobject *target, const char *link_name) { |
324a56e16
|
374 |
struct kernfs_node *parent; |
0bb8f3d6a
|
375 |
int error = 0; |
324a56e16
|
376 377 |
parent = kernfs_find_and_get(kobj->sd, group_name); if (!parent) |
0bb8f3d6a
|
378 |
return -ENOENT; |
324a56e16
|
379 380 |
error = sysfs_create_link_sd(parent, target, link_name); kernfs_put(parent); |
0bb8f3d6a
|
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
return error; } EXPORT_SYMBOL_GPL(sysfs_add_link_to_group); /** * sysfs_remove_link_from_group - remove a symlink from an attribute group. * @kobj: The kobject containing the group. * @group_name: The name of the group. * @link_name: The name of the symlink to remove. */ void sysfs_remove_link_from_group(struct kobject *kobj, const char *group_name, const char *link_name) { |
324a56e16
|
395 |
struct kernfs_node *parent; |
0bb8f3d6a
|
396 |
|
324a56e16
|
397 398 399 400 |
parent = kernfs_find_and_get(kobj->sd, group_name); if (parent) { kernfs_remove_by_name(parent, link_name); kernfs_put(parent); |
0bb8f3d6a
|
401 402 403 |
} } EXPORT_SYMBOL_GPL(sysfs_remove_link_from_group); |
37c1c04cc
|
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
/** * __compat_only_sysfs_link_entry_to_kobj - add a symlink to a kobject pointing * to a group or an attribute * @kobj: The kobject containing the group. * @target_kobj: The target kobject. * @target_name: The name of the target group or attribute. */ int __compat_only_sysfs_link_entry_to_kobj(struct kobject *kobj, struct kobject *target_kobj, const char *target_name) { struct kernfs_node *target; struct kernfs_node *entry; struct kernfs_node *link; /* * We don't own @target_kobj and it may be removed at any time. * Synchronize using sysfs_symlink_target_lock. See sysfs_remove_dir() * for details. */ spin_lock(&sysfs_symlink_target_lock); target = target_kobj->sd; if (target) kernfs_get(target); spin_unlock(&sysfs_symlink_target_lock); if (!target) return -ENOENT; entry = kernfs_find_and_get(target_kobj->sd, target_name); if (!entry) { kernfs_put(target); return -ENOENT; } link = kernfs_create_link(kobj->sd, target_name, entry); if (IS_ERR(link) && PTR_ERR(link) == -EEXIST) sysfs_warn_dup(kobj->sd, target_name); kernfs_put(entry); kernfs_put(target); |
90b3d2f6c
|
445 |
return PTR_ERR_OR_ZERO(link); |
37c1c04cc
|
446 447 |
} EXPORT_SYMBOL_GPL(__compat_only_sysfs_link_entry_to_kobj); |