Commit 7f5028cf authored by Emilio López's avatar Emilio López Committed by Olof Johansson

sysfs: Support is_visible() on binary attributes

According to the sysfs header file:

    "The returned value will replace static permissions defined in
     struct attribute or struct bin_attribute."

but this isn't the case, as is_visible is only called on struct attribute
only. This patch introduces a new is_bin_visible() function to implement
the same functionality for binary attributes, and updates documentation
accordingly.

Note that to keep functionality and code similar to that of normal
attributes, the mode is now checked as well to ensure it contains only
read/write permissions or SYSFS_PREALLOC.
Reviewed-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarEmilio López <emilio.lopez@collabora.co.uk>
Acked-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarOlof Johansson <olof@lixom.net>
parent f14ae099
...@@ -73,13 +73,26 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj, ...@@ -73,13 +73,26 @@ static int create_files(struct kernfs_node *parent, struct kobject *kobj,
} }
if (grp->bin_attrs) { if (grp->bin_attrs) {
for (bin_attr = grp->bin_attrs; *bin_attr; bin_attr++) { for (i = 0, bin_attr = grp->bin_attrs; *bin_attr; i++, bin_attr++) {
umode_t mode = (*bin_attr)->attr.mode;
if (update) if (update)
kernfs_remove_by_name(parent, kernfs_remove_by_name(parent,
(*bin_attr)->attr.name); (*bin_attr)->attr.name);
if (grp->is_bin_visible) {
mode = grp->is_bin_visible(kobj, *bin_attr, i);
if (!mode)
continue;
}
WARN(mode & ~(SYSFS_PREALLOC | 0664),
"Attribute %s: Invalid permissions 0%o\n",
(*bin_attr)->attr.name, mode);
mode &= SYSFS_PREALLOC | 0664;
error = sysfs_add_file_mode_ns(parent, error = sysfs_add_file_mode_ns(parent,
&(*bin_attr)->attr, true, &(*bin_attr)->attr, true,
(*bin_attr)->attr.mode, NULL); mode, NULL);
if (error) if (error)
break; break;
} }
......
...@@ -64,10 +64,18 @@ do { \ ...@@ -64,10 +64,18 @@ do { \
* a new subdirectory with this name. * a new subdirectory with this name.
* @is_visible: Optional: Function to return permissions associated with an * @is_visible: Optional: Function to return permissions associated with an
* attribute of the group. Will be called repeatedly for each * attribute of the group. Will be called repeatedly for each
* attribute in the group. Only read/write permissions as well as * non-binary attribute in the group. Only read/write
* SYSFS_PREALLOC are accepted. Must return 0 if an attribute is * permissions as well as SYSFS_PREALLOC are accepted. Must
* not visible. The returned value will replace static permissions * return 0 if an attribute is not visible. The returned value
* defined in struct attribute or struct bin_attribute. * will replace static permissions defined in struct attribute.
* @is_bin_visible:
* Optional: Function to return permissions associated with a
* binary attribute of the group. Will be called repeatedly
* for each binary attribute in the group. Only read/write
* permissions as well as SYSFS_PREALLOC are accepted. Must
* return 0 if a binary attribute is not visible. The returned
* value will replace static permissions defined in
* struct bin_attribute.
* @attrs: Pointer to NULL terminated list of attributes. * @attrs: Pointer to NULL terminated list of attributes.
* @bin_attrs: Pointer to NULL terminated list of binary attributes. * @bin_attrs: Pointer to NULL terminated list of binary attributes.
* Either attrs or bin_attrs or both must be provided. * Either attrs or bin_attrs or both must be provided.
...@@ -76,6 +84,8 @@ struct attribute_group { ...@@ -76,6 +84,8 @@ struct attribute_group {
const char *name; const char *name;
umode_t (*is_visible)(struct kobject *, umode_t (*is_visible)(struct kobject *,
struct attribute *, int); struct attribute *, int);
umode_t (*is_bin_visible)(struct kobject *,
struct bin_attribute *, int);
struct attribute **attrs; struct attribute **attrs;
struct bin_attribute **bin_attrs; struct bin_attribute **bin_attrs;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment