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> |
Eric W. Biederman | b592fcf | 2007-01-24 12:35:52 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 17 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "sysfs.h" |
| 19 | |
| 20 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 21 | static void remove_files(struct sysfs_dirent *dir_sd, |
| 22 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
| 24 | struct attribute *const* attr; |
| 25 | |
| 26 | for (attr = grp->attrs; *attr; attr++) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 27 | sysfs_hash_and_remove(dir_sd, (*attr)->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 30 | static int create_files(struct sysfs_dirent *dir_sd, |
| 31 | const struct attribute_group *grp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | struct attribute *const* attr; |
| 34 | int error = 0; |
| 35 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 36 | for (attr = grp->attrs; *attr && !error; attr++) |
| 37 | error = sysfs_add_file(dir_sd, *attr, SYSFS_KOBJ_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | if (error) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 39 | remove_files(dir_sd, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | return error; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | int sysfs_create_group(struct kobject * kobj, |
| 45 | const struct attribute_group * grp) |
| 46 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 47 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | int error; |
| 49 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 50 | BUG_ON(!kobj || !kobj->sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 53 | error = sysfs_create_subdir(kobj, grp->name, &sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | if (error) |
| 55 | return error; |
| 56 | } else |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 57 | sd = kobj->sd; |
| 58 | sysfs_get(sd); |
| 59 | error = create_files(sd, grp); |
| 60 | if (error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 62 | sysfs_remove_subdir(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 64 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | return error; |
| 66 | } |
| 67 | |
| 68 | void sysfs_remove_group(struct kobject * kobj, |
| 69 | const struct attribute_group * grp) |
| 70 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 71 | struct sysfs_dirent *dir_sd = kobj->sd; |
| 72 | struct sysfs_dirent *sd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 74 | if (grp->name) { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 75 | sd = sysfs_get_dirent(dir_sd, grp->name); |
| 76 | BUG_ON(!sd); |
| 77 | } else |
| 78 | sd = sysfs_get(dir_sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 80 | remove_files(sd, grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | if (grp->name) |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 82 | sysfs_remove_subdir(sd); |
| 83 | |
| 84 | sysfs_put(sd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
| 88 | EXPORT_SYMBOL_GPL(sysfs_create_group); |
| 89 | EXPORT_SYMBOL_GPL(sysfs_remove_group); |