@@ -23,35 +23,50 @@ static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
2323 int i ;
2424
2525 for (i = 0 , attr = grp -> attrs ; * attr ; i ++ , attr ++ )
26- if (!grp -> is_visible ||
27- grp -> is_visible (kobj , * attr , i ))
28- sysfs_hash_and_remove (dir_sd , (* attr )-> name );
26+ sysfs_hash_and_remove (dir_sd , (* attr )-> name );
2927}
3028
3129static int create_files (struct sysfs_dirent * dir_sd , struct kobject * kobj ,
32- const struct attribute_group * grp )
30+ const struct attribute_group * grp , int update )
3331{
3432 struct attribute * const * attr ;
3533 int error = 0 , i ;
3634
37- for (i = 0 , attr = grp -> attrs ; * attr && !error ; i ++ , attr ++ )
38- if (!grp -> is_visible ||
39- grp -> is_visible (kobj , * attr , i ))
40- error |=
41- sysfs_add_file (dir_sd , * attr , SYSFS_KOBJ_ATTR );
35+ 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 )
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 ;
47+ }
48+ error = sysfs_add_file_mode (dir_sd , * attr , SYSFS_KOBJ_ATTR ,
49+ (* attr )-> mode | mode );
50+ if (unlikely (error ))
51+ break ;
52+ }
4253 if (error )
4354 remove_files (dir_sd , kobj , grp );
4455 return error ;
4556}
4657
4758
48- int sysfs_create_group (struct kobject * kobj ,
49- const struct attribute_group * grp )
59+ static int internal_create_group (struct kobject * kobj , int update ,
60+ const struct attribute_group * grp )
5061{
5162 struct sysfs_dirent * sd ;
5263 int error ;
5364
54- BUG_ON (!kobj || !kobj -> sd );
65+ 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 ;
5570
5671 if (grp -> name ) {
5772 error = sysfs_create_subdir (kobj , grp -> name , & sd );
@@ -60,7 +75,7 @@ int sysfs_create_group(struct kobject * kobj,
6075 } else
6176 sd = kobj -> sd ;
6277 sysfs_get (sd );
63- error = create_files (sd , kobj , grp );
78+ error = create_files (sd , kobj , grp , update );
6479 if (error ) {
6580 if (grp -> name )
6681 sysfs_remove_subdir (sd );
@@ -69,6 +84,47 @@ int sysfs_create_group(struct kobject * kobj,
6984 return error ;
7085}
7186
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
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+ */
97+ int 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+ */
120+ int 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+
72128void sysfs_remove_group (struct kobject * kobj ,
73129 const struct attribute_group * grp )
74130{
@@ -95,4 +151,5 @@ void sysfs_remove_group(struct kobject * kobj,
95151
96152
97153EXPORT_SYMBOL_GPL (sysfs_create_group );
154+ EXPORT_SYMBOL_GPL (sysfs_update_group );
98155EXPORT_SYMBOL_GPL (sysfs_remove_group );
0 commit comments