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 | |
| 21 | static void remove_files(struct dentry * dir, |
| 22 | const struct attribute_group * grp) |
| 23 | { |
| 24 | struct attribute *const* attr; |
| 25 | |
| 26 | for (attr = grp->attrs; *attr; attr++) |
| 27 | sysfs_hash_and_remove(dir,(*attr)->name); |
| 28 | } |
| 29 | |
| 30 | static int create_files(struct dentry * dir, |
| 31 | const struct attribute_group * grp) |
| 32 | { |
| 33 | struct attribute *const* attr; |
| 34 | int error = 0; |
| 35 | |
| 36 | for (attr = grp->attrs; *attr && !error; attr++) { |
| 37 | error = sysfs_add_file(dir, *attr, SYSFS_KOBJ_ATTR); |
| 38 | } |
| 39 | if (error) |
| 40 | remove_files(dir,grp); |
| 41 | return error; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | int sysfs_create_group(struct kobject * kobj, |
| 46 | const struct attribute_group * grp) |
| 47 | { |
| 48 | struct dentry * dir; |
| 49 | int error; |
| 50 | |
| 51 | BUG_ON(!kobj || !kobj->dentry); |
| 52 | |
| 53 | if (grp->name) { |
| 54 | error = sysfs_create_subdir(kobj,grp->name,&dir); |
| 55 | if (error) |
| 56 | return error; |
| 57 | } else |
| 58 | dir = kobj->dentry; |
| 59 | dir = dget(dir); |
| 60 | if ((error = create_files(dir,grp))) { |
| 61 | if (grp->name) |
| 62 | sysfs_remove_subdir(dir); |
| 63 | } |
| 64 | dput(dir); |
| 65 | return error; |
| 66 | } |
| 67 | |
| 68 | void sysfs_remove_group(struct kobject * kobj, |
| 69 | const struct attribute_group * grp) |
| 70 | { |
| 71 | struct dentry * dir; |
| 72 | |
| 73 | if (grp->name) |
Christoph Hellwig | 5f45f1a | 2005-06-23 00:09:12 -0700 | [diff] [blame] | 74 | dir = lookup_one_len(grp->name, kobj->dentry, |
| 75 | strlen(grp->name)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | else |
| 77 | dir = dget(kobj->dentry); |
| 78 | |
| 79 | remove_files(dir,grp); |
| 80 | if (grp->name) |
| 81 | sysfs_remove_subdir(dir); |
| 82 | /* release the ref. taken in this routine */ |
| 83 | dput(dir); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | EXPORT_SYMBOL_GPL(sysfs_create_group); |
| 88 | EXPORT_SYMBOL_GPL(sysfs_remove_group); |