sysfs: add sysfs_create/remove_groups()
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 21 Aug 2013 20:47:50 +0000 (13:47 -0700)
committerCaesar Wang <wxt@rock-chips.com>
Tue, 1 Sep 2015 03:11:27 +0000 (11:11 +0800)
commit 3e9b2bae8369661070622d05570cbcdfa01770e6 upstream

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.

Conflicts:
git checkout drivers/base/bus.c, In actul we don't use the interfaces in
drivers/base/bus.c

Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Change-Id: I0046a5572c780a0ea2b175ef753c408f6c10ba85
Signed-off-by: Caesar Wang <wxt@rock-chips.com>
drivers/base/core.c
drivers/base/driver.c
fs/sysfs/group.c
include/linux/sysfs.h

index 2a19097a7cb19c87ae8a2d0015a41e3edd7b9571..00a3be583c037b3226a6cc22602400a7c0935d4b 100644 (file)
@@ -464,31 +464,13 @@ static void device_remove_bin_attributes(struct device *dev,
 static 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);
 }
 
 static 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)
index 974e301a1ef07ae02064be5e003bc4ac034df5c7..4a991167da94b597f7868759f15851984bfd0a9d 100644 (file)
@@ -126,31 +126,13 @@ EXPORT_SYMBOL_GPL(driver_remove_file);
 static 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);
 }
 
 static 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);
 }
 
 /**
index aec3d5c98c94fa1d3d79de15a44dfa2a4a77e329..01c95382a77c9cce1c836be5d290d9bb276a25cd 100644 (file)
@@ -104,6 +104,40 @@ int sysfs_create_group(struct kobject *kobj,
        return internal_create_group(kobj, 0, grp);
 }
 
+/**
+ * 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
@@ -152,6 +186,26 @@ void sysfs_remove_group(struct kobject * kobj,
        sysfs_put(sd);
 }
 
+/**
+ * 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.
index 7dd65cbfcdb375ad4895a684f4a600bb73f5c0de..1ae212450725abfeca4c7fe2320a3498b003b446 100644 (file)
@@ -220,10 +220,14 @@ void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
 
 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,