x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / fs / sysfs / group.c
blobfe611949a7f7419f789dbb8b4335b76d03e9fab9
1 /*
2 * fs/sysfs/group.c - Operations for adding/removing multiple files at once.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released undert the GPL v2.
9 */
11 #include <linux/kobject.h>
12 #include <linux/module.h>
13 #include <linux/dcache.h>
14 #include <linux/namei.h>
15 #include <linux/err.h>
16 #include "sysfs.h"
19 static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
20 const struct attribute_group *grp)
22 struct attribute *const* attr;
23 int i;
25 for (i = 0, attr = grp->attrs; *attr; i++, attr++)
26 sysfs_hash_and_remove(dir_sd, (*attr)->name);
29 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
30 const struct attribute_group *grp, int update)
32 struct attribute *const* attr;
33 int error = 0, i;
35 for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
36 mode_t mode = 0;
38 /* in update mode, we're changing the permissions or
39 * visibility. Do this by first removing then
40 * re-adding (if required) the file */
41 if (update)
42 sysfs_hash_and_remove(dir_sd, (*attr)->name);
43 if (grp->is_visible) {
44 mode = grp->is_visible(kobj, *attr, i);
45 if (!mode)
46 continue;
48 error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
49 (*attr)->mode | mode);
50 if (unlikely(error))
51 break;
53 if (error)
54 remove_files(dir_sd, kobj, grp);
55 return error;
59 static int internal_create_group(struct kobject *kobj, int update,
60 const struct attribute_group *grp)
62 struct sysfs_dirent *sd;
63 int error;
65 BUG_ON(!kobj || (!update && !kobj->sd));
67 /* Updates may happen before the object has been instantiated */
68 if (unlikely(update && !kobj->sd))
69 return -EINVAL;
71 if (grp->name) {
72 error = sysfs_create_subdir(kobj, grp->name, &sd);
73 if (error)
74 return error;
75 } else
76 sd = kobj->sd;
77 sysfs_get(sd);
78 error = create_files(sd, kobj, grp, update);
79 if (error) {
80 if (grp->name)
81 sysfs_remove_subdir(sd);
83 sysfs_put(sd);
84 return error;
87 /**
88 * sysfs_create_group - given a directory kobject, create an attribute group
89 * @kobj: The kobject to create the group on
90 * @grp: The attribute group to create
92 * This function creates a group for the first time. It will explicitly
93 * warn and error if any of the attribute files being created already exist.
95 * Returns 0 on success or error.
97 int sysfs_create_group(struct kobject *kobj,
98 const struct attribute_group *grp)
100 return internal_create_group(kobj, 0, grp);
104 * sysfs_update_group - given a directory kobject, create an attribute group
105 * @kobj: The kobject to create the group on
106 * @grp: The attribute group to create
108 * This function updates an attribute group. Unlike
109 * sysfs_create_group(), it will explicitly not warn or error if any
110 * of the attribute files being created already exist. Furthermore,
111 * if the visibility of the files has changed through the is_visible()
112 * callback, it will update the permissions and add or remove the
113 * relevant files.
115 * The primary use for this function is to call it after making a change
116 * that affects group visibility.
118 * Returns 0 on success or error.
120 int sysfs_update_group(struct kobject *kobj,
121 const struct attribute_group *grp)
123 return internal_create_group(kobj, 1, grp);
128 void sysfs_remove_group(struct kobject * kobj,
129 const struct attribute_group * grp)
131 struct sysfs_dirent *dir_sd = kobj->sd;
132 struct sysfs_dirent *sd;
134 if (grp->name) {
135 sd = sysfs_get_dirent(dir_sd, grp->name);
136 if (!sd) {
137 WARN(!sd, KERN_WARNING "sysfs group %p not found for "
138 "kobject '%s'\n", grp, kobject_name(kobj));
139 return;
141 } else
142 sd = sysfs_get(dir_sd);
144 remove_files(sd, kobj, grp);
145 if (grp->name)
146 sysfs_remove_subdir(sd);
148 sysfs_put(sd);
152 EXPORT_SYMBOL_GPL(sysfs_create_group);
153 EXPORT_SYMBOL_GPL(sysfs_update_group);
154 EXPORT_SYMBOL_GPL(sysfs_remove_group);