1 kset数据结构
kset的定义在前文已有描述,我们再回顾一下:
struct kset {
/*与子kobject的entry成员组成链表*/
struct list_head list;
/*自旋锁*/
spinlock_t list_lock;
/*kobject*/
struct kobject kobj;
const struct kset_uevent_ops *uevent_ops;
};
2 kset创建流程
2.1 kset_create_and_add
kset的创建函数为kset_create_and_add,详细如下:
/**
* kset_create_and_add - create a struct kset dynamically and add it to sysfs
*
* @name: the name for the kset
* @uevent_ops: a struct kset_uevent_ops for the kset
* @parent_kobj: the parent kobject of this kset, if any.
*
* This function creates a kset structure dynamically and registers it
* with sysfs. When you are finished with this structure, call
* kset_unregister() and the structure will be dynamically freed when it
* is no longer being used.
*
* If the kset was not able to be created, NULL will be returned.
*/
struct kset *kset_create_and_add(const char *name,
const struct kset_uevent_ops *uevent_ops,
struct kobject *parent_kobj)
{
struct kset *kset;
int error;
/*创建kset对象*/
kset = kset_create(name, uevent_ops, parent_kobj);
if (!kset)
return NULL;
/*注册kset*/
error = kset_register(kset);
if (error) {
kfree(kset);
return NULL;
}
return kset;
}
它调用了kset_create和kset_register函数。
下面看kset_create的实现:
/**
* kset_create - create a struct kset dynamically
*
* @name: the name for the kset
* @uevent_ops: a struct kset_uevent_ops for the kset
* @parent_kobj: the parent kobject of this kset, if any.
*
* This function creates a kset structure dynamically. This structure can
* then be registered with the system and show up in sysfs with a call to
* kset_register(). When you are finished with this structure, if
* kset_register() has been called, call kset_unregister() and the
* structure will be dynamically freed when it is no longer being used.
*
* If the kset was not able to be created, NULL will be returned.
*/
static struct kset *kset_create(const char *name,
const struct kset_uevent_ops *uevent_ops,
struct kobject *parent_kobj)
{
struct kset *kset;
int retval;
/*申请空间*/
kset = kzalloc(sizeof(*kset), GFP_KERNEL);
if (!kset)
return NULL;
/*设置kset的kobject名称*/
retval = kobject_set_name(&kset->kobj, name);
if (retval) {
kfree(kset);
return NULL;
}
kset->uevent_ops = uevent_ops;
/*设置kobject的parent*/
kset->kobj.parent = parent_kobj;
/*
* The kobject of this kset will have a type of kset_ktype