Skip to content

Commit

Permalink
sysfs: Add sysfs_add/remove_files utility functions
Browse files Browse the repository at this point in the history
Adding/Removing a whole array of attributes is very common. Add a standard
utility function to do this with a simple function call, instead of
requiring drivers to open code this.

Signed-off-by: Andi Kleen <ak@linux.intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
  • Loading branch information
Andi Kleen authored and Greg Kroah-Hartman committed Mar 8, 2010
1 parent 265d2e2 commit 1c205ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions fs/sysfs/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@ int sysfs_create_file(struct kobject * kobj, const struct attribute * attr)

}

int sysfs_create_files(struct kobject *kobj, const struct attribute **ptr)
{
int err = 0;
int i;

for (i = 0; ptr[i] && !err; i++)
err = sysfs_create_file(kobj, ptr[i]);
if (err)
while (--i >= 0)
sysfs_remove_file(kobj, ptr[i]);
return err;
}

/**
* sysfs_add_file_to_group - add an attribute file to a pre-existing group.
Expand Down Expand Up @@ -614,6 +626,12 @@ void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr)
sysfs_hash_and_remove(kobj->sd, attr->name);
}

void sysfs_remove_files(struct kobject * kobj, const struct attribute **ptr)
{
int i;
for (i = 0; ptr[i]; i++)
sysfs_remove_file(kobj, ptr[i]);
}

/**
* sysfs_remove_file_from_group - remove an attribute file from a group.
Expand Down Expand Up @@ -732,3 +750,5 @@ EXPORT_SYMBOL_GPL(sysfs_schedule_callback);

EXPORT_SYMBOL_GPL(sysfs_create_file);
EXPORT_SYMBOL_GPL(sysfs_remove_file);
EXPORT_SYMBOL_GPL(sysfs_remove_files);
EXPORT_SYMBOL_GPL(sysfs_create_files);
14 changes: 14 additions & 0 deletions include/linux/sysfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ int __must_check sysfs_move_dir(struct kobject *kobj,

int __must_check sysfs_create_file(struct kobject *kobj,
const struct attribute *attr);
int __must_check sysfs_create_files(struct kobject *kobj,
const struct attribute **attr);
int __must_check sysfs_chmod_file(struct kobject *kobj, struct attribute *attr,
mode_t mode);
void sysfs_remove_file(struct kobject *kobj, const struct attribute *attr);
void sysfs_remove_files(struct kobject *kobj, const struct attribute **attr);

int __must_check sysfs_create_bin_file(struct kobject *kobj,
const struct bin_attribute *attr);
Expand Down Expand Up @@ -164,6 +167,12 @@ static inline int sysfs_create_file(struct kobject *kobj,
return 0;
}

static inline int sysfs_create_files(struct kobject *kobj,
const struct attribute **attr)
{
return 0;
}

static inline int sysfs_chmod_file(struct kobject *kobj,
struct attribute *attr, mode_t mode)
{
Expand All @@ -175,6 +184,11 @@ static inline void sysfs_remove_file(struct kobject *kobj,
{
}

static inline void sysfs_remove_files(struct kobject *kobj,
const struct attribute **attr)
{
}

static inline int sysfs_create_bin_file(struct kobject *kobj,
const struct bin_attribute *attr)
{
Expand Down

0 comments on commit 1c205ae

Please sign in to comment.