一、sysfs文件系統下的每個目錄對應於一個kobj,kset是kobj的封裝,內嵌了一個kobj,其代表kset自身,ktype代表屬性操作集,但由於通用性,因此把ktype單獨剝離出來,kobj,kset,ktype成為了各個驅動模型最底層的關聯元素,並由此形成了sys下的各種拓撲結構。
二、關於kobject
首先看一下kobject的原型
[cpp]
- struct kobject {
- const char *name; //名字
- struct list_head entry; //連接到kset建立層次結構
- struct kobject *parent; //指向父節點,面向對象的層次架構
- struct kset *kset;
- struct kobj_type *ktype; //屬性文件
- struct sysfs_dirent *sd;
- struct kref kref; //引用計數
- unsigned int state_initialized:1; //初始化狀態...
- unsigned int state_in_sysfs:1;
- unsigned int state_add_uevent_sent:1;
- unsigned int state_remove_uevent_sent:1;
- unsigned int uevent_suppress:1;
- };
分析一下kobject的初始化過程
初始化函數為
[cpp]
- ---int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, //參數為kobject和屬性結構體
- struct kobject *parent, const char *fmt, ...)
- {
- va_list args;
- int retval;
- kobject_init(kobj, ktype);
- va_start(args, fmt);
- retval = kobject_add_varg(kobj, parent, fmt, args);
- va_end(args);
- return retval;
- }
- ---void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
- {
- char *err_str;
- if (!kobj) { //kobj為NULL錯誤退出
- err_str = "invalid kobject pointer!";
- goto error;
- }
- if (!ktype) { //ktype為NULL錯誤退出
- err_str = "must have a ktype to be initialized properly!/n";
- goto error;
- }
- if (kobj->state_initialized) { //如果初始化狀態為1報錯
- /* do not error out as sometimes we can recover */
- printk(KERN_ERR "kobject (%p): tried to init an initialized "
- "object, something is seriously wrong./n", kobj);
- dump_stack();
- }
- kobject_init_internal(kobj); //初始化kobj
- kobj->ktype = ktype; //關聯obj和ktype
- return;
- error:
- printk(KERN_ERR "kobject (%p): %s/n", kobj, err_str);
- dump_stack();
- }
- -------static void kobject_init_internal(struct kobject *kobj)
- {
- if (!kobj)
- return;
- kref_init(&kobj->kref); //計數變成1
- INIT_LIST_HEAD(&kobj->entry); //都指向自己,prev和next
- kobj->state_in_sysfs = 0;
- kobj->state_add_uevent_sent = 0;
- kobj->state_remove_uevent_sent = 0;
- kobj->state_initialized = 1;
- }
- -------static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
- const char *fmt, va_list vargs)
- {
- int retval;
- retval = kobject_set_name_vargs(kobj, fmt, vargs); //設置名字,名字中不能有“/”
- if (retval) {
- printk(KERN_ERR "kobject: can not set name properly!/n");
- return retval;
- }
- kobj->parent = parent; //設置parent,其父節點
- return kobject_add_internal(kobj);
- }
- ----static int kobject_add_internal(struct kobject *kobj)
- {
- int error = 0;
- struct kobject *parent;
- if (!kobj)
- return -ENOENT;
- if (!kobj->name || !kobj->name[0]) { //名字不能為空
- WARN(1, "kobject: (%p): attempted to be registered with empty "
- "name!/n", kobj);
- return -EINVAL;
- }
- parent = kobject_get(kobj->parent); //如果parent為真,則增加kobj->kref計數,也就是父節點的引用計數
- /* join kset if set, use it as parent if we do not already have one */
- if (kobj->kset) {
- if (!parent)
- parent = kobject_get(&kobj->kset->kobj); //如果kobj-parent父節點為NULL那麼就用kobj->kset->kobj
- // 作其父節點,並增加其引用計數
- kobj_kset_join(kobj); //把kobj的entry成員添加到kobj->kset>list的尾部,現在的層次就是
- kobj->parent = parent; //kobj->kset->list指向kobj->parent
- } // ->parent 指向kset->kobj
- pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'/n",
- kobject_name(kobj), kobj, __func__,
- parent ? kobject_name(parent) : "<NULL>",
- kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>");
- error = create_dir(kobj); //利用kobj創建目錄和屬性文件,其中會判斷,如果parent為NULL那麼就在sysfs_root下創建
- if (error) {
- kobj_kset_leave(kobj);
- kobject_put(parent);
- kobj->parent = NULL;
- /* be noisy on error issues */
- if (error == -EEXIST)
- printk(KERN_ERR "%s failed for %s with "
- "-EEXIST, don't try to register things with "
- "the same name in the same directory./n",
- __func__, kobject_name(kobj));
- else
- printk(KERN_ERR "%s failed for %s (%d)/n",
- __func__, kobject_name(kobj), error);
- dump_stack();
- } else
- kobj->state_in_sysfs = 1;
- return error;
- }
- ---static int create_dir(struct kobject *kobj)
- {
- int error = 0;
- if (kobject_name(kobj)) {
- error = sysfs_create_dir(kobj); //創建目錄
- if (!error) {
- error = populate_dir(kobj); //創建屬性文件
- if (error)
- sysfs_remove_dir(kobj);
- }
- }
- return error;
- }