Commit 6ab9cea16075ea707022753395f340b67f64304c
1 parent
ced321bf91
Exists in
master
and in
20 other branches
sysfs: add support for binary attributes in groups
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>
Showing 2 changed files with 48 additions and 22 deletions Side-by-side Diff
fs/sysfs/group.c
... | ... | @@ -20,38 +20,64 @@ |
20 | 20 | const struct attribute_group *grp) |
21 | 21 | { |
22 | 22 | struct attribute *const* attr; |
23 | - int i; | |
23 | + struct bin_attribute *const* bin_attr; | |
24 | 24 | |
25 | - for (i = 0, attr = grp->attrs; *attr; i++, attr++) | |
26 | - sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); | |
25 | + if (grp->attrs) | |
26 | + for (attr = grp->attrs; *attr; attr++) | |
27 | + sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); | |
28 | + if (grp->bin_attrs) | |
29 | + for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) | |
30 | + sysfs_remove_bin_file(kobj, *bin_attr); | |
27 | 31 | } |
28 | 32 | |
29 | 33 | static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, |
30 | 34 | const struct attribute_group *grp, int update) |
31 | 35 | { |
32 | 36 | struct attribute *const* attr; |
37 | + struct bin_attribute *const* bin_attr; | |
33 | 38 | int error = 0, i; |
34 | 39 | |
35 | - for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { | |
36 | - umode_t mode = 0; | |
40 | + if (grp->attrs) { | |
41 | + for (i = 0, attr = grp->attrs; *attr && !error; i++, attr++) { | |
42 | + umode_t mode = 0; | |
37 | 43 | |
38 | - /* in update mode, we're changing the permissions or | |
39 | - * visibility. Do this by first removing then | |
40 | - * re-adding (if required) the file */ | |
41 | - if (update) | |
42 | - sysfs_hash_and_remove(dir_sd, NULL, (*attr)->name); | |
43 | - if (grp->is_visible) { | |
44 | - mode = grp->is_visible(kobj, *attr, i); | |
45 | - if (!mode) | |
46 | - continue; | |
44 | + /* | |
45 | + * In update mode, we're changing the permissions or | |
46 | + * visibility. Do this by first removing then | |
47 | + * re-adding (if required) the file. | |
48 | + */ | |
49 | + if (update) | |
50 | + sysfs_hash_and_remove(dir_sd, NULL, | |
51 | + (*attr)->name); | |
52 | + if (grp->is_visible) { | |
53 | + mode = grp->is_visible(kobj, *attr, i); | |
54 | + if (!mode) | |
55 | + continue; | |
56 | + } | |
57 | + error = sysfs_add_file_mode(dir_sd, *attr, | |
58 | + SYSFS_KOBJ_ATTR, | |
59 | + (*attr)->mode | mode); | |
60 | + if (unlikely(error)) | |
61 | + break; | |
47 | 62 | } |
48 | - error = sysfs_add_file_mode(dir_sd, *attr, SYSFS_KOBJ_ATTR, | |
49 | - (*attr)->mode | mode); | |
50 | - if (unlikely(error)) | |
51 | - break; | |
63 | + if (error) { | |
64 | + remove_files(dir_sd, kobj, grp); | |
65 | + goto exit; | |
66 | + } | |
52 | 67 | } |
53 | - if (error) | |
54 | - remove_files(dir_sd, kobj, grp); | |
68 | + | |
69 | + if (grp->bin_attrs) { | |
70 | + for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) { | |
71 | + if (update) | |
72 | + sysfs_remove_bin_file(kobj, *bin_attr); | |
73 | + error = sysfs_create_bin_file(kobj, *bin_attr); | |
74 | + if (error) | |
75 | + break; | |
76 | + } | |
77 | + if (error) | |
78 | + remove_files(dir_sd, kobj, grp); | |
79 | + } | |
80 | +exit: | |
55 | 81 | return error; |
56 | 82 | } |
57 | 83 |
include/linux/sysfs.h
... | ... | @@ -21,6 +21,7 @@ |
21 | 21 | |
22 | 22 | struct kobject; |
23 | 23 | struct module; |
24 | +struct bin_attribute; | |
24 | 25 | enum kobj_ns_type; |
25 | 26 | |
26 | 27 | struct attribute { |
27 | 28 | |
... | ... | @@ -59,9 +60,8 @@ |
59 | 60 | umode_t (*is_visible)(struct kobject *, |
60 | 61 | struct attribute *, int); |
61 | 62 | struct attribute **attrs; |
63 | + struct bin_attribute **bin_attrs; | |
62 | 64 | }; |
63 | - | |
64 | - | |
65 | 65 | |
66 | 66 | /** |
67 | 67 | * Use these macros to make defining attributes easier. See include/linux/device.h |