Skip to content

Commit

Permalink
kobject: Add support for default attribute groups to kobj_type
Browse files Browse the repository at this point in the history
kobj_type currently uses a list of individual attributes to store
default attributes. Attribute groups are more flexible than a list of
attributes because groups provide support for attribute visibility. So,
add support for default attribute groups to kobj_type.

In future patches, the existing uses of kobj_type’s attribute list will
be converted to attribute groups. When that is complete, kobj_type’s
attribute list, “default_attrs”, will be removed.

Signed-off-by: Kimberly Brown <kimbrownkd@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
kbrown9 authored and gregkh committed Apr 25, 2019
1 parent 0b777ee commit aa30f47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion include/linux/kobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ static inline bool kobject_has_children(struct kobject *kobj)
struct kobj_type {
void (*release)(struct kobject *kobj);
const struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs;
struct attribute **default_attrs; /* use default_groups instead */
const struct attribute_group **default_groups;
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
Expand Down
14 changes: 14 additions & 0 deletions lib/kobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ static int populate_dir(struct kobject *kobj)

static int create_dir(struct kobject *kobj)
{
const struct kobj_type *ktype = get_ktype(kobj);
const struct kobj_ns_type_operations *ops;
int error;

Expand All @@ -95,6 +96,14 @@ static int create_dir(struct kobject *kobj)
return error;
}

if (ktype) {
error = sysfs_create_groups(kobj, ktype->default_groups);
if (error) {
sysfs_remove_dir(kobj);
return error;
}
}

/*
* @kobj->sd may be deleted by an ancestor going away. Hold an
* extra reference so that it stays until @kobj is gone.
Expand Down Expand Up @@ -584,11 +593,16 @@ EXPORT_SYMBOL_GPL(kobject_move);
void kobject_del(struct kobject *kobj)
{
struct kernfs_node *sd;
const struct kobj_type *ktype = get_ktype(kobj);

if (!kobj)
return;

sd = kobj->sd;

if (ktype)
sysfs_remove_groups(kobj, ktype->default_groups);

sysfs_remove_dir(kobj);
sysfs_put(sd);

Expand Down

0 comments on commit aa30f47

Please sign in to comment.