* [PATCH] sysfs: add sysfs_create/remove_groups()
@ 2013-08-21 20:35 Greg Kroah-Hartman
2013-08-21 20:38 ` Randy Dunlap
0 siblings, 1 reply; 6+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-21 20:35 UTC (permalink / raw)
To: linux-kernel
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
These functions are being open-coded in 3 different places in the driver
core, and other driver subsystems will want to start doing this as well,
so move it to the sysfs core to keep it all in one place, where we know
it is written properly.
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
drivers/base/bus.c | 23 +---------------------
drivers/base/core.c | 22 +--------------------
drivers/base/driver.c | 22 +--------------------
fs/sysfs/group.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 4 +++
5 files changed, 62 insertions(+), 61 deletions(-)
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -884,32 +884,13 @@ static void bus_remove_attrs(struct bus_
static int bus_add_groups(struct bus_type *bus,
const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&bus->p->subsys.kobj,
- groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&bus->p->subsys.kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&bus->p->subsys.kobj, groups);
}
static void bus_remove_groups(struct bus_type *bus,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&bus->p->subsys.kobj, groups[i]);
+ sysfs_remove_groups(&bus->p->subsys.kobj, groups);
}
static void klist_devices_get(struct klist_node *n)
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -493,31 +493,13 @@ static void device_remove_bin_attributes
int device_add_groups(struct device *dev, const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&dev->kobj, groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&dev->kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&dev->kobj, groups);
}
void device_remove_groups(struct device *dev,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&dev->kobj, groups[i]);
+ sysfs_remove_groups(&dev->kobj, groups);
}
static int device_add_attrs(struct device *dev)
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -126,31 +126,13 @@ EXPORT_SYMBOL_GPL(driver_remove_file);
int driver_add_groups(struct device_driver *drv,
const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&drv->p->kobj, groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&drv->p->kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&drv->p->kobj, groups);
}
void driver_remove_groups(struct device_driver *drv,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&drv->p->kobj, groups[i]);
+ sysfs_remove_groups(&drv->p->kobj, groups);
}
/**
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -131,6 +131,39 @@ int sysfs_create_group(struct kobject *k
}
/**
+ * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
+ * @kobj: The kobject to create the group on
+ * @groups: The attribute groups to create, NULL terminated
+ *
+ * This function creates a bunch of attribute groups. If an error occurs when
+ * creating a group, all previously created groups will be removed, unwinding
+ * everything back to the original state when this function was called.
+ * It will explicitly warn and error if any of the attribute files being
+ * created already exist.
+ *
+ * Returns 0 on success or error.
+ */
+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;
+}
+
+/**
* sysfs_update_group - given a directory kobject, update an attribute group
* @kobj: The kobject to update the group on
* @grp: The attribute group to update
@@ -179,6 +212,25 @@ void sysfs_remove_group(struct kobject *
}
/**
+ * sysfs_remove_groups - remove a list of groups
+ *
+ * kobj: The kobject for the groups to be removed from
+ * groups: NULL terminated list of groups to be removed
+ *
+ * If groups is not NULL, the all groups will be removed from the kobject
+ */
+void sysfs_remove_groups(struct kobject *kobj,
+ const struct attribute_group **groups)
+{
+ int i;
+
+ if (!groups)
+ return;
+ for (i = 0; groups[i]; i++)
+ sysfs_remove_group(kobj, groups[i]);
+}
+
+/**
* sysfs_merge_group - merge files into a pre-existing attribute group.
* @kobj: The kobject containing the group.
* @grp: The files to create and the attribute group they belong to.
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -215,10 +215,14 @@ void sysfs_delete_link(struct kobject *d
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 sysfs_update_group(struct kobject *kobj,
const struct attribute_group *grp);
void sysfs_remove_group(struct kobject *kobj,
const struct attribute_group *grp);
+void sysfs_remove_groups(struct kobject *kobj,
+ const struct attribute_group **groups);
int sysfs_add_file_to_group(struct kobject *kobj,
const struct attribute *attr, const char *group);
void sysfs_remove_file_from_group(struct kobject *kobj,
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] sysfs: add sysfs_create/remove_groups()
2013-08-21 20:35 [PATCH] sysfs: add sysfs_create/remove_groups() Greg Kroah-Hartman
@ 2013-08-21 20:38 ` Randy Dunlap
2013-08-21 20:44 ` Greg Kroah-Hartman
2013-08-21 20:47 ` Greg Kroah-Hartman
0 siblings, 2 replies; 6+ messages in thread
From: Randy Dunlap @ 2013-08-21 20:38 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: linux-kernel
On 08/21/13 13:35, Greg Kroah-Hartman wrote:
>
> --- a/fs/sysfs/group.c
> +++ b/fs/sysfs/group.c
> @@ -131,6 +131,39 @@ int sysfs_create_group(struct kobject *k
> }
>
> /**
> + * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
> + * @kobj: The kobject to create the group on
> + * @groups: The attribute groups to create, NULL terminated
> + *
> + * This function creates a bunch of attribute groups. If an error occurs when
> + * creating a group, all previously created groups will be removed, unwinding
> + * everything back to the original state when this function was called.
> + * It will explicitly warn and error if any of the attribute files being
> + * created already exist.
> + *
> + * Returns 0 on success or error.
ambiguous. How about:
Returns 0 on success or error code on error.
> + */
> +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;
> +}
--
~Randy
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] sysfs: add sysfs_create/remove_groups()
2013-08-21 20:38 ` Randy Dunlap
@ 2013-08-21 20:44 ` Greg Kroah-Hartman
2013-08-21 20:47 ` Greg Kroah-Hartman
1 sibling, 0 replies; 6+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-21 20:44 UTC (permalink / raw)
To: Randy Dunlap; +Cc: linux-kernel
On Wed, Aug 21, 2013 at 01:38:35PM -0700, Randy Dunlap wrote:
> On 08/21/13 13:35, Greg Kroah-Hartman wrote:
> >
> > --- a/fs/sysfs/group.c
> > +++ b/fs/sysfs/group.c
> > @@ -131,6 +131,39 @@ int sysfs_create_group(struct kobject *k
> > }
> >
> > /**
> > + * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
> > + * @kobj: The kobject to create the group on
> > + * @groups: The attribute groups to create, NULL terminated
> > + *
> > + * This function creates a bunch of attribute groups. If an error occurs when
> > + * creating a group, all previously created groups will be removed, unwinding
> > + * everything back to the original state when this function was called.
> > + * It will explicitly warn and error if any of the attribute files being
> > + * created already exist.
> > + *
> > + * Returns 0 on success or error.
>
> ambiguous. How about:
> Returns 0 on success or error code on error.
Ok, I also forgot to export these symbols, so I'll go do a v2 of the
patch, thanks for the review.
greg k-h
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH] sysfs: add sysfs_create/remove_groups()
2013-08-21 20:38 ` Randy Dunlap
2013-08-21 20:44 ` Greg Kroah-Hartman
@ 2013-08-21 20:47 ` Greg Kroah-Hartman
2013-08-22 4:55 ` Anthony Foiani
1 sibling, 1 reply; 6+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-21 20:47 UTC (permalink / raw)
To: Randy Dunlap; +Cc: linux-kernel
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
These functions are being open-coded in 3 different places in the driver
core, and other driver subsystems will want to start doing this as well,
so move it to the sysfs core to keep it all in one place, where we know
it is written properly.
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
v2: - export new functions so modules can use them
- return value better documented thanks to Randy.
drivers/base/bus.c | 23 +--------------------
drivers/base/core.c | 22 +-------------------
drivers/base/driver.c | 22 +-------------------
fs/sysfs/group.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/sysfs.h | 4 +++
5 files changed, 64 insertions(+), 61 deletions(-)
--- a/drivers/base/bus.c
+++ b/drivers/base/bus.c
@@ -884,32 +884,13 @@ static void bus_remove_attrs(struct bus_
static int bus_add_groups(struct bus_type *bus,
const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&bus->p->subsys.kobj,
- groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&bus->p->subsys.kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&bus->p->subsys.kobj, groups);
}
static void bus_remove_groups(struct bus_type *bus,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&bus->p->subsys.kobj, groups[i]);
+ sysfs_remove_groups(&bus->p->subsys.kobj, groups);
}
static void klist_devices_get(struct klist_node *n)
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -493,31 +493,13 @@ static void device_remove_bin_attributes
int device_add_groups(struct device *dev, const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&dev->kobj, groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&dev->kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&dev->kobj, groups);
}
void device_remove_groups(struct device *dev,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&dev->kobj, groups[i]);
+ sysfs_remove_groups(&dev->kobj, groups);
}
static int device_add_attrs(struct device *dev)
--- a/drivers/base/driver.c
+++ b/drivers/base/driver.c
@@ -126,31 +126,13 @@ EXPORT_SYMBOL_GPL(driver_remove_file);
int driver_add_groups(struct device_driver *drv,
const struct attribute_group **groups)
{
- int error = 0;
- int i;
-
- if (groups) {
- for (i = 0; groups[i]; i++) {
- error = sysfs_create_group(&drv->p->kobj, groups[i]);
- if (error) {
- while (--i >= 0)
- sysfs_remove_group(&drv->p->kobj,
- groups[i]);
- break;
- }
- }
- }
- return error;
+ return sysfs_create_groups(&drv->p->kobj, groups);
}
void driver_remove_groups(struct device_driver *drv,
const struct attribute_group **groups)
{
- int i;
-
- if (groups)
- for (i = 0; groups[i]; i++)
- sysfs_remove_group(&drv->p->kobj, groups[i]);
+ sysfs_remove_groups(&drv->p->kobj, groups);
}
/**
--- a/fs/sysfs/group.c
+++ b/fs/sysfs/group.c
@@ -131,6 +131,40 @@ int sysfs_create_group(struct kobject *k
}
/**
+ * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
+ * @kobj: The kobject to create the group on
+ * @groups: The attribute groups to create, NULL terminated
+ *
+ * This function creates a bunch of attribute groups. If an error occurs when
+ * creating a group, all previously created groups will be removed, unwinding
+ * everything back to the original state when this function was called.
+ * It will explicitly warn and error if any of the attribute files being
+ * created already exist.
+ *
+ * Returns 0 on success or error code from sysfs_create_groups on error.
+ */
+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;
+}
+EXPORT_SYMBOL_GPL(sysfs_create_groups);
+
+/**
* sysfs_update_group - given a directory kobject, update an attribute group
* @kobj: The kobject to update the group on
* @grp: The attribute group to update
@@ -179,6 +213,26 @@ void sysfs_remove_group(struct kobject *
}
/**
+ * sysfs_remove_groups - remove a list of groups
+ *
+ * kobj: The kobject for the groups to be removed from
+ * groups: NULL terminated list of groups to be removed
+ *
+ * If groups is not NULL, the all groups will be removed from the kobject
+ */
+void sysfs_remove_groups(struct kobject *kobj,
+ const struct attribute_group **groups)
+{
+ int i;
+
+ if (!groups)
+ return;
+ for (i = 0; groups[i]; i++)
+ sysfs_remove_group(kobj, groups[i]);
+}
+EXPORT_SYMBOL_GPL(sysfs_remove_groups);
+
+/**
* sysfs_merge_group - merge files into a pre-existing attribute group.
* @kobj: The kobject containing the group.
* @grp: The files to create and the attribute group they belong to.
--- a/include/linux/sysfs.h
+++ b/include/linux/sysfs.h
@@ -215,10 +215,14 @@ void sysfs_delete_link(struct kobject *d
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 sysfs_update_group(struct kobject *kobj,
const struct attribute_group *grp);
void sysfs_remove_group(struct kobject *kobj,
const struct attribute_group *grp);
+void sysfs_remove_groups(struct kobject *kobj,
+ const struct attribute_group **groups);
int sysfs_add_file_to_group(struct kobject *kobj,
const struct attribute *attr, const char *group);
void sysfs_remove_file_from_group(struct kobject *kobj,
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] sysfs: add sysfs_create/remove_groups()
2013-08-21 20:47 ` Greg Kroah-Hartman
@ 2013-08-22 4:55 ` Anthony Foiani
2013-08-22 16:22 ` Greg Kroah-Hartman
0 siblings, 1 reply; 6+ messages in thread
From: Anthony Foiani @ 2013-08-22 4:55 UTC (permalink / raw)
To: Greg Kroah-Hartman; +Cc: Randy Dunlap, linux-kernel
Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
> + * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
> + * @kobj: The kobject to create the group on
> + * @groups: The attribute groups to create, NULL terminated
> + *
> + * This function creates a bunch of attribute groups. If an error occurs when
> + * creating a group, all previously created groups will be removed, unwinding
> + * everything back to the original state when this function was called.
> + * It will explicitly warn and error if any of the attribute files being
> + * created already exist.
> + *
> + * Returns 0 on success or error code from sysfs_create_groups on error.
"... from sysfs_create_group on error"
^^^^^ (note singular, not plural)
Otherwise it's a bit of a tautology...
Maybe rename to "sysfs_(create|remove)_multiple_groups" (or
..._many_groups) to avoid this kind of one-char difference?
> + * sysfs_remove_groups - remove a list of groups
> + *
> + * kobj: The kobject for the groups to be removed from
> + * groups: NULL terminated list of groups to be removed
> + *
> + * If groups is not NULL, the all groups will be removed from the kobject
"If groups is not NULL, remove listed groups from the kobject"
(I also played around with using pointer arithmetic instead of array
accesses for these two functions; it cut a few lines of code, but not
enough to bother, I don't think.)
Thanks,
Tony
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] sysfs: add sysfs_create/remove_groups()
2013-08-22 4:55 ` Anthony Foiani
@ 2013-08-22 16:22 ` Greg Kroah-Hartman
0 siblings, 0 replies; 6+ messages in thread
From: Greg Kroah-Hartman @ 2013-08-22 16:22 UTC (permalink / raw)
To: Anthony Foiani; +Cc: Randy Dunlap, linux-kernel
On Wed, Aug 21, 2013 at 10:55:19PM -0600, Anthony Foiani wrote:
> Greg Kroah-Hartman <gregkh@linuxfoundation.org> writes:
>
> > + * sysfs_create_groups - given a directory kobject, create a bunch of attribute groups
> > + * @kobj: The kobject to create the group on
> > + * @groups: The attribute groups to create, NULL terminated
> > + *
> > + * This function creates a bunch of attribute groups. If an error occurs when
> > + * creating a group, all previously created groups will be removed, unwinding
> > + * everything back to the original state when this function was called.
> > + * It will explicitly warn and error if any of the attribute files being
> > + * created already exist.
> > + *
> > + * Returns 0 on success or error code from sysfs_create_groups on error.
>
> "... from sysfs_create_group on error"
> ^^^^^ (note singular, not plural)
>
> Otherwise it's a bit of a tautology...
Ah, thanks, will fix that up.
> Maybe rename to "sysfs_(create|remove)_multiple_groups" (or
> ..._many_groups) to avoid this kind of one-char difference?
Nah, terse is good :)
> > + * sysfs_remove_groups - remove a list of groups
> > + *
> > + * kobj: The kobject for the groups to be removed from
> > + * groups: NULL terminated list of groups to be removed
> > + *
> > + * If groups is not NULL, the all groups will be removed from the kobject
>
> "If groups is not NULL, remove listed groups from the kobject"
Nice, will go fix it up now, thanks.
> (I also played around with using pointer arithmetic instead of array
> accesses for these two functions; it cut a few lines of code, but not
> enough to bother, I don't think.)
Yeah, it's better to not be "tricky" so that others can review the code
easier, and the end result would be the same.
thanks for the review.
greg k-h
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-08-22 16:22 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-21 20:35 [PATCH] sysfs: add sysfs_create/remove_groups() Greg Kroah-Hartman
2013-08-21 20:38 ` Randy Dunlap
2013-08-21 20:44 ` Greg Kroah-Hartman
2013-08-21 20:47 ` Greg Kroah-Hartman
2013-08-22 4:55 ` Anthony Foiani
2013-08-22 16:22 ` Greg Kroah-Hartman
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.