blob: 23c1e598792a088cfdd757030f9d733251943fb7 [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>
16#include "sysfs.h"
17
18
James Bottomleyd4acd722007-10-31 09:38:04 -050019static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
Tejun Heo608e2662007-06-14 04:27:22 +090020 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct attribute *const* attr;
James Bottomleyd4acd722007-10-31 09:38:04 -050023 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
James Bottomleyd4acd722007-10-31 09:38:04 -050025 for (i = 0, attr = grp->attrs; *attr; i++, attr++)
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070026 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
28
James Bottomleyd4acd722007-10-31 09:38:04 -050029static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
James Bottomley0f423892008-03-20 20:47:52 -050030 const struct attribute_group *grp, int update)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct attribute *const* attr;
James Bottomleyd4acd722007-10-31 09:38:04 -050033 int error = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
James Bottomley0f423892008-03-20 20:47:52 -050035 for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
36 mode_t mode = 0;
37
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)
Eric W. Biederman3ff195b2010-03-30 11:31:26 -070042 sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
James Bottomley0f423892008-03-20 20:47:52 -050043 if (grp->is_visible) {
44 mode = grp->is_visible(kobj, *attr, i);
45 if (!mode)
46 continue;
47 }
48 error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
49 (*attr)->mode | mode);
50 if (unlikely(error))
51 break;
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (error)
James Bottomleyd4acd722007-10-31 09:38:04 -050054 remove_files(dir_sd, kobj, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 return error;
56}
57
58
James Bottomley0f423892008-03-20 20:47:52 -050059static int internal_create_group(struct kobject *kobj, int update,
60 const struct attribute_group *grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Tejun Heo608e2662007-06-14 04:27:22 +090062 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int error;
64
James Bottomley0f423892008-03-20 20:47:52 -050065 BUG_ON(!kobj || (!update && !kobj->sd));
66
67 /* Updates may happen before the object has been instantiated */
68 if (unlikely(update && !kobj->sd))
69 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (grp->name) {
Tejun Heo608e2662007-06-14 04:27:22 +090072 error = sysfs_create_subdir(kobj, grp->name, &sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (error)
74 return error;
75 } else
Tejun Heo608e2662007-06-14 04:27:22 +090076 sd = kobj->sd;
77 sysfs_get(sd);
James Bottomley0f423892008-03-20 20:47:52 -050078 error = create_files(sd, kobj, grp, update);
Tejun Heo608e2662007-06-14 04:27:22 +090079 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (grp->name)
Tejun Heo608e2662007-06-14 04:27:22 +090081 sysfs_remove_subdir(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Tejun Heo608e2662007-06-14 04:27:22 +090083 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return error;
85}
86
James Bottomley0f423892008-03-20 20:47:52 -050087/**
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
91 *
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.
94 *
95 * Returns 0 on success or error.
96 */
97int sysfs_create_group(struct kobject *kobj,
98 const struct attribute_group *grp)
99{
100 return internal_create_group(kobj, 0, grp);
101}
102
103/**
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
107 *
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.
114 *
115 * The primary use for this function is to call it after making a change
116 * that affects group visibility.
117 *
118 * Returns 0 on success or error.
119 */
120int sysfs_update_group(struct kobject *kobj,
121 const struct attribute_group *grp)
122{
123 return internal_create_group(kobj, 1, grp);
124}
125
126
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128void sysfs_remove_group(struct kobject * kobj,
129 const struct attribute_group * grp)
130{
Tejun Heo608e2662007-06-14 04:27:22 +0900131 struct sysfs_dirent *dir_sd = kobj->sd;
132 struct sysfs_dirent *sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
James Morris057f6c02007-04-26 00:12:05 -0700134 if (grp->name) {
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700135 sd = sysfs_get_dirent(dir_sd, NULL, grp->name);
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500136 if (!sd) {
Arjan van de Ven99fcd772008-07-25 19:45:41 -0700137 WARN(!sd, KERN_WARNING "sysfs group %p not found for "
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500138 "kobject '%s'\n", grp, kobject_name(kobj));
Greg Kroah-Hartman969affd2008-02-07 11:58:54 -0500139 return;
140 }
Tejun Heo608e2662007-06-14 04:27:22 +0900141 } else
142 sd = sysfs_get(dir_sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
James Bottomleyd4acd722007-10-31 09:38:04 -0500144 remove_files(sd, kobj, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (grp->name)
Tejun Heo608e2662007-06-14 04:27:22 +0900146 sysfs_remove_subdir(sd);
147
148 sysfs_put(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151
152EXPORT_SYMBOL_GPL(sysfs_create_group);
James Bottomley0f423892008-03-20 20:47:52 -0500153EXPORT_SYMBOL_GPL(sysfs_update_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154EXPORT_SYMBOL_GPL(sysfs_remove_group);