Commit c3f575f0 authored by Patrick Mochel's avatar Patrick Mochel

kobjects: add array of default attributes to subsystems, and create on registration.

struct subsystem may now contain a pointer to a NULL-terminated array of 
default attributes to be exported when an object is registered with the subsystem.
kobject registration will check the return values of the directory creation and 
the creation of each file, and handle it appropriately. 

The documentation has also been updated.
parent 332ad69d
...@@ -22,7 +22,7 @@ struct kobject { ...@@ -22,7 +22,7 @@ struct kobject {
struct list_head entry; struct list_head entry;
struct kobject * parent; struct kobject * parent;
struct subsystem * subsys; struct subsystem * subsys;
struct sysfs_dir dir; struct dentry * dentry;
}; };
void kobject_init(struct kobject *); void kobject_init(struct kobject *);
...@@ -41,13 +41,15 @@ contain lists of kobjects that belong to that subsystem. Objects of a ...@@ -41,13 +41,15 @@ contain lists of kobjects that belong to that subsystem. Objects of a
subsystem (the embedder objects in which kobjects live) are all of the subsystem (the embedder objects in which kobjects live) are all of the
same type. The interface looks like: same type. The interface looks like:
struct subsystem { struct subsystem {
struct kobject kobj; struct kobject kobj;
struct list_head list; struct list_head list;
struct rw_semaphore rwsem; struct rw_semaphore rwsem;
struct subsystem * parent; struct subsystem * parent;
struct sysfs_ops * sysfs_ops;
void (*release)(struct kobject *); void (*release)(struct kobject *);
struct sysfs_ops * sysfs_ops;
struct attribute ** default_attrs;
}; };
void subsystem_init(struct subsystem *); void subsystem_init(struct subsystem *);
...@@ -125,6 +127,14 @@ have been moved to reside in the subsystem, since they are common for ...@@ -125,6 +127,14 @@ have been moved to reside in the subsystem, since they are common for
all kobjects. all kobjects.
Default Attributes
Most subsystems have a set of default attributes associated with an
object that registers with them. A subsystem definition may contain a
NULL-terminated array of attributes that will be exported when an
object is registered with the subsystem.
Reference Counting Reference Counting
All objects contain reference counts. All functions accessing objects All objects contain reference counts. All functions accessing objects
......
...@@ -37,6 +37,7 @@ struct subsystem { ...@@ -37,6 +37,7 @@ struct subsystem {
struct subsystem * parent; struct subsystem * parent;
void (*release)(struct kobject *); void (*release)(struct kobject *);
struct sysfs_ops * sysfs_ops; struct sysfs_ops * sysfs_ops;
struct attribute ** default_attrs;
}; };
extern void subsystem_init(struct subsystem *); extern void subsystem_init(struct subsystem *);
......
...@@ -8,6 +8,34 @@ ...@@ -8,6 +8,34 @@
#include <linux/module.h> #include <linux/module.h>
#include <linux/stat.h> #include <linux/stat.h>
/**
* kobject_populate_dir - populate directory with attributes.
* @kobj: object we're working on.
*
* Most subsystems have a set of default attributes that
* are associated with an object that registers with them.
* This is a helper called during object registration that
* loops through the default attributes of the subsystem
* and creates attributes files for them in sysfs.
*
*/
static int kobject_populate_dir(struct kobject * kobj)
{
struct subsystem * s = kobj->subsys;
struct attribute * attr;
int error = 0;
int i;
if (s && s->default_attrs) {
for (i = 0; (attr = s->default_attrs[i]); i++) {
if ((error = sysfs_create_file(kobj,attr)))
break;
}
}
return error;
}
/** /**
* kobject_init - initialize object. * kobject_init - initialize object.
* @kobj: object in question. * @kobj: object in question.
...@@ -31,10 +59,13 @@ void kobject_init(struct kobject * kobj) ...@@ -31,10 +59,13 @@ void kobject_init(struct kobject * kobj)
int kobject_register(struct kobject * kobj) int kobject_register(struct kobject * kobj)
{ {
int error = 0;
struct subsystem * s = subsys_get(kobj->subsys); struct subsystem * s = subsys_get(kobj->subsys);
struct kobject * parent = kobject_get(kobj->parent); struct kobject * parent = kobject_get(kobj->parent);
pr_debug("kobject %s: registering\n",kobj->name); pr_debug("kobject %s: registering\n",kobj->name);
if (parent)
pr_debug(" parent is %s\n",parent->name);
if (s) { if (s) {
down_write(&s->rwsem); down_write(&s->rwsem);
if (parent) if (parent)
...@@ -45,7 +76,13 @@ int kobject_register(struct kobject * kobj) ...@@ -45,7 +76,13 @@ int kobject_register(struct kobject * kobj)
} }
up_write(&s->rwsem); up_write(&s->rwsem);
} }
return sysfs_create_dir(kobj); error = sysfs_create_dir(kobj);
if (!error) {
error = kobject_populate_dir(kobj);
if (error)
sysfs_remove_dir(kobj);
}
return error;
} }
/** /**
...@@ -141,6 +178,8 @@ int subsystem_register(struct subsystem * s) ...@@ -141,6 +178,8 @@ int subsystem_register(struct subsystem * s)
if (s->parent) if (s->parent)
s->kobj.parent = &s->parent->kobj; s->kobj.parent = &s->parent->kobj;
pr_debug("subsystem %s: registering\n",s->kobj.name); pr_debug("subsystem %s: registering\n",s->kobj.name);
if (s->parent)
pr_debug(" parent is %s\n",s->parent->kobj.name);
return kobject_register(&s->kobj); return kobject_register(&s->kobj);
} }
......
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