blob: 46a277b0838e31e46c80bdf7d863738df99effca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hellwig5f45f1a2005-06-23 00:09:12 -070014#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/err.h>
Oliver Neukum94bebf42006-12-20 10:52:44 +010016#include <asm/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "sysfs.h"
18
19
20static void remove_files(struct dentry * dir,
21 const struct attribute_group * grp)
22{
23 struct attribute *const* attr;
24
25 for (attr = grp->attrs; *attr; attr++)
26 sysfs_hash_and_remove(dir,(*attr)->name);
27}
28
29static int create_files(struct dentry * dir,
30 const struct attribute_group * grp)
31{
32 struct attribute *const* attr;
33 int error = 0;
34
35 for (attr = grp->attrs; *attr && !error; attr++) {
36 error = sysfs_add_file(dir, *attr, SYSFS_KOBJ_ATTR);
37 }
38 if (error)
39 remove_files(dir,grp);
40 return error;
41}
42
43
44int sysfs_create_group(struct kobject * kobj,
45 const struct attribute_group * grp)
46{
47 struct dentry * dir;
48 int error;
49
50 BUG_ON(!kobj || !kobj->dentry);
51
52 if (grp->name) {
53 error = sysfs_create_subdir(kobj,grp->name,&dir);
54 if (error)
55 return error;
56 } else
57 dir = kobj->dentry;
58 dir = dget(dir);
59 if ((error = create_files(dir,grp))) {
60 if (grp->name)
61 sysfs_remove_subdir(dir);
62 }
63 dput(dir);
64 return error;
65}
66
67void sysfs_remove_group(struct kobject * kobj,
68 const struct attribute_group * grp)
69{
70 struct dentry * dir;
71
72 if (grp->name)
Christoph Hellwig5f45f1a2005-06-23 00:09:12 -070073 dir = lookup_one_len(grp->name, kobj->dentry,
74 strlen(grp->name));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 else
76 dir = dget(kobj->dentry);
77
78 remove_files(dir,grp);
79 if (grp->name)
80 sysfs_remove_subdir(dir);
81 /* release the ref. taken in this routine */
82 dput(dir);
83}
84
85
86EXPORT_SYMBOL_GPL(sysfs_create_group);
87EXPORT_SYMBOL_GPL(sysfs_remove_group);