sysfs: add support for binary attributes in groups
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sun, 14 Jul 2013 23:05:55 +0000 (16:05 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 16 Jul 2013 17:57:36 +0000 (10:57 -0700)
groups should be able to support binary attributes, just like it
supports "normal" attributes.  This lets us only handle one type of
structure, groups, throughout the driver core and subsystems, making
binary attributes a "full fledged" part of the driver model, and not
something just "tacked on".

Reported-by: Oliver Schinagl <oliver@schinagl.nl>
Reviewed-by: Guenter Roeck <linux@roeck-us.net>
Tested-by: Guenter Roeck <linux@roeck-us.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/sysfs/group.c
include/linux/sysfs.h

index aec3d5c98c94fa1d3d79de15a44dfa2a4a77e329..e5719c6095c3de13114a684c03d274cc2ff0dea2 100644 (file)
@@ -20,38 +20,64 @@ static void remove_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
                         const struct attribute_group *grp)
 {
        struct attribute *const* attr;
-       int i;
+       struct bin_attribute *const* bin_attr;
 
-       for (i = 0, attr = grp->attrs; *attr; i++, attr++)
-               sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
+       if (grp->attrs)
+               for (attr = grp->attrs; *attr; attr++)
+                       sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
+       if (grp->bin_attrs)
+               for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++)
+                       sysfs_remove_bin_file(kobj, *bin_attr);
 }
 
 static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj,
                        const struct attribute_group *grp, int update)
 {
        struct attribute *const* attr;
+       struct bin_attribute *const* bin_attr;
        int error = 0, i;
 
-       for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
-               umode_t mode = 0;
+       if (grp->attrs) {
+               for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) {
+                       umode_t mode = 0;
+
+                       /*
+                        * In update mode, we're changing the permissions or
+                        * visibility.  Do this by first removing then
+                        * re-adding (if required) the file.
+                        */
+                       if (update)
+                               sysfs_hash_and_remove(dir_sd, NULL,
+                                                     (*attr)->name);
+                       if (grp->is_visible) {
+                               mode = grp->is_visible(kobj, *attr, i);
+                               if (!mode)
+                                       continue;
+                       }
+                       error = sysfs_add_file_mode(dir_sd, *attr,
+                                                   SYSFS_KOBJ_ATTR,
+                                                   (*attr)->mode | mode);
+                       if (unlikely(error))
+                               break;
+               }
+               if (error) {
+                       remove_files(dir_sd, kobj, grp);
+                       goto exit;
+               }
+       }
 
-               /* in update mode, we're changing the permissions or
-                * visibility.  Do this by first removing then
-                * re-adding (if required) the file */
-               if (update)
-                       sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name);
-               if (grp->is_visible) {
-                       mode = grp->is_visible(kobj, *attr, i);
-                       if (!mode)
-                               continue;
+       if (grp->bin_attrs) {
+               for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) {
+                       if (update)
+                               sysfs_remove_bin_file(kobj, *bin_attr);
+                       error = sysfs_create_bin_file(kobj, *bin_attr);
+                       if (error)
+                               break;
                }
-               error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR,
-                                           (*attr)->mode | mode);
-               if (unlikely(error))
-                       break;
+               if (error)
+                       remove_files(dir_sd, kobj, grp);
        }
-       if (error)
-               remove_files(dir_sd, kobj, grp);
+exit:
        return error;
 }
 
index d50a96b9bb6d47a4a1485d1ee2b94fdf64a72f0f..2c3b6a30697d5de824fc9dd0b701d87e5c392855 100644 (file)
@@ -21,6 +21,7 @@
 
 struct kobject;
 struct module;
+struct bin_attribute;
 enum kobj_ns_type;
 
 struct attribute {
@@ -59,10 +60,9 @@ struct attribute_group {
        umode_t                 (*is_visible)(struct kobject *,
                                              struct attribute *, int);
        struct attribute        **attrs;
+       struct bin_attribute    **bin_attrs;
 };
 
-
-
 /**
  * Use these macros to make defining attributes easier. See include/linux/device.h
  * for examples..