Skip to content

Commit

Permalink
sysfs: Add sysfs_update_groups function
Browse files Browse the repository at this point in the history
Adding sysfs_update_groups function to update
multiple groups.

  sysfs_update_groups - given a directory kobject, create a bunch of attribute groups
  @kobj:      The kobject to update the group on
  @groups:    The attribute groups to update, NULL terminated

This function update a bunch of attribute groups.  If an error occurs when
updating a group, all previously updated groups will be removed together
with already existing (not updated) attributes.

Signed-off-by: Jiri Olsa <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Cc: Alexander Shishkin <[email protected]>
Cc: Arnaldo Carvalho de Melo <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Namhyung Kim <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: Ingo Molnar <[email protected]>
  • Loading branch information
olsajiri authored and Ingo Molnar committed Jun 3, 2019
1 parent 6e86d3d commit aac1f7f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
54 changes: 39 additions & 15 deletions fs/sysfs/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ int sysfs_create_group(struct kobject *kobj,
}
EXPORT_SYMBOL_GPL(sysfs_create_group);

static int internal_create_groups(struct kobject *kobj, int update,
const struct attribute_group **groups)
{
int error = 0;
int i;

if (!groups)
return 0;

for (i = 0; groups[i]; i++) {
error = internal_create_group(kobj, update, groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(kobj, groups[i]);
break;
}
}
return error;
}

/**
* sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
* @kobj: The kobject to create the group on
Expand All @@ -191,24 +211,28 @@ EXPORT_SYMBOL_GPL(sysfs_create_group);
int sysfs_create_groups(struct kobject *kobj,
const struct attribute_group **groups)
{
int error = 0;
int i;

if (!groups)
return 0;

for (i = 0; groups[i]; i++) {
error = sysfs_create_group(kobj, groups[i]);
if (error) {
while (--i >= 0)
sysfs_remove_group(kobj, groups[i]);
break;
}
}
return error;
return internal_create_groups(kobj, 0, groups);
}
EXPORT_SYMBOL_GPL(sysfs_create_groups);

/**
* sysfs_update_groups - given a directory kobject, create a bunch of attribute groups
* @kobj: The kobject to update the group on
* @groups: The attribute groups to update, NULL terminated
*
* This function update a bunch of attribute groups. If an error occurs when
* updating a group, all previously updated groups will be removed together
* with already existing (not updated) attributes.
*
* Returns 0 on success or error code from sysfs_update_group on failure.
*/
int sysfs_update_groups(struct kobject *kobj,
const struct attribute_group **groups)
{
return internal_create_groups(kobj, 1, groups);
}
EXPORT_SYMBOL_GPL(sysfs_update_groups);

/**
* sysfs_update_group - given a directory kobject, update an attribute group
* @kobj: The kobject to update the group on
Expand Down
8 changes: 8 additions & 0 deletions include/linux/sysfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ int __must_check sysfs_create_group(struct kobject *kobj,
const struct attribute_group *grp);
int __must_check sysfs_create_groups(struct kobject *kobj,
const struct attribute_group **groups);
int __must_check sysfs_update_groups(struct kobject *kobj,
const struct attribute_group **groups);
int sysfs_update_group(struct kobject *kobj,
const struct attribute_group *grp);
void sysfs_remove_group(struct kobject *kobj,
Expand Down Expand Up @@ -433,6 +435,12 @@ static inline int sysfs_create_groups(struct kobject *kobj,
return 0;
}

static inline int sysfs_update_groups(struct kobject *kobj,
const struct attribute_group **groups)
{
return 0;
}

static inline int sysfs_update_group(struct kobject *kobj,
const struct attribute_group *grp)
{
Expand Down

0 comments on commit aac1f7f

Please sign in to comment.