Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 14 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include "sysfs.h" |
| 17 | |
| 18 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 19 | static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 20 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | { |
| 22 | struct attribute *const* attr; |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 23 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 25 | for (i = 0, attr = grp->attrs; *attr; i++, attr++) |
| 26 | if (!grp->is_visible || |
| 27 | grp->is_visible(kobj, *attr, i)) |
| 28 | sysfs_hash_and_remove(dir_sd, (*attr)->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | } |
| 30 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 31 | static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 32 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
| 34 | struct attribute *const* attr; |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 35 | int error = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 37 | for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) |
| 38 | if (!grp->is_visible || |
| 39 | grp->is_visible(kobj, *attr, i)) |
| 40 | error |= |
| 41 | sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | if (error) |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 43 | remove_files(dir_sd, kobj, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | return error; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | int sysfs_create_group(struct kobject * kobj, |
| 49 | const struct attribute_group * grp) |
| 50 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 51 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int error; |
| 53 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 54 | BUG_ON(!kobj || !kobj->sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 57 | error = sysfs_create_subdir(kobj, grp->name, &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | if (error) |
| 59 | return error; |
| 60 | } else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 61 | sd = kobj->sd; |
| 62 | sysfs_get(sd); |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 63 | error = create_files(sd, kobj, grp); |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 64 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 66 | sysfs_remove_subdir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 68 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | return error; |
| 70 | } |
| 71 | |
| 72 | void sysfs_remove_group(struct kobject * kobj, |
| 73 | const struct attribute_group * grp) |
| 74 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 75 | struct sysfs_dirent *dir_sd = kobj->sd; |
| 76 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 78 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 79 | sd = sysfs_get_dirent(dir_sd, grp->name); |
Greg Kroah-Hartman | 969affd | 2008-02-07 11:58:54 -0500 | [diff] [blame] | 80 | if (!sd) { |
| 81 | printk(KERN_WARNING "sysfs group %p not found for " |
| 82 | "kobject '%s'\n", grp, kobject_name(kobj)); |
| 83 | WARN_ON(!sd); |
| 84 | return; |
| 85 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 86 | } else |
| 87 | sd = sysfs_get(dir_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
James Bottomley | d4acd72 | 2007-10-31 09:38:04 -0500 | [diff] [blame] | 89 | remove_files(sd, kobj, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 91 | sysfs_remove_subdir(sd); |
| 92 | |
| 93 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | |
| 97 | EXPORT_SYMBOL_GPL(sysfs_create_group); |
| 98 | EXPORT_SYMBOL_GPL(sysfs_remove_group); |