kobject.c 添加注释

xiaoxiao2021-02-28  108

/** * 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 populate_dir(struct kobject * kobj) { /* [cgw]: 从kobject取出kobj_type成员指针 */ struct kobj_type * t = get_ktype(kobj); struct attribute * attr; int error = 0; int i; /* [cgw]: kobj_type中默认属性default_attrs不为空,即已经添加一个或以上 * 属性项 */ if (t && t->default_attrs) { /* [cgw]: 轮询default_attrs中的指针不为空,即已经添加属性 */ for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { /* [cgw]: 在sysfs中为这个kobj创建一个属性文件 */ if ((error = sysfs_create_file(kobj,attr))) break; } } return error; } static int create_dir(struct kobject * kobj, struct dentry *shadow_parent) { int error = 0; /* [cgw]: 获得这个kobj的名字,名字不为空 */ if (kobject_name(kobj)) { /* [cgw]: 为kobj创建一个目录 * @shadow_parent: parent parent object. (父对象) * */ error = sysfs_create_dir(kobj, shadow_parent); if (!error) { /* [cgw]: 添加kobj属性*/ if ((error = populate_dir(kobj))) /* [cgw]: 添加失败,删除kobj对象目录 */ sysfs_remove_dir(kobj); } } return error; } static inline struct kobject * to_kobj(struct list_head * entry) { /* [cgw]: 从链表list_head取出一个entry * 根据这个entry,找到包含这个entry的 * kobject结构体地址 */ return container_of(entry,struct kobject,entry); } static int get_kobj_path_length(struct kobject *kobj) { int length = 1; struct kobject * parent = kobj; /* walk up the ancestors until we hit the one pointing to the * root. * Add 1 to strlen for leading '/' of each level. */ /* [cgw]: 逐个节点查找对象的名字,计算每节点对象的名字长度, * 并加1,加1是每个名字前有个'/',并累计所有名字长度 */ do { /* [cgw]: 名字不为空 */ if (kobject_name(parent) == NULL) return 0; /* [cgw]: 累计名字长度 */ length += strlen(kobject_name(parent)) + 1; /* [cgw]: 指向上一节点(当前对象的父辈) */ parent = parent->parent; } while (parent); return length; } static void fill_kobj_path(struct kobject *kobj, char *path, int length) { struct kobject * parent; /* [cgw]: 逐个节点查找对象的名字,并填装到path中 */ --length; for (parent = kobj; parent; parent = parent->parent) { /* [cgw]: 计算当前节点对象名字长度 */ int cur = strlen(kobject_name(parent)); /* back up enough to print this name with '/' */ /* [cgw]: 总长度减去一个名字的长度 */ length -= cur; /* [cgw]: 把这个对象名字填装到path的path + length位置 */ strncpy (path + length, kobject_name(parent), cur); /* [cgw]: 在path的path + length -1的位置插入'/' */ *(path + --length) = '/'; } pr_debug("%s: path = '%s'\n",__FUNCTION__,path); } /** * kobject_get_path - generate and return the path associated with a given kobj and kset pair. * * @kobj: kobject in question, with which to build the path * @gfp_mask: the allocation type used to allocate the path * * The result must be freed by the caller with kfree(). */ char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) { char *path; int len; /* [cgw]: 根据当前节点kobj,统计从当前节点开始到所有父辈节点的名字 * 总长度 */ len = get_kobj_path_length(kobj); /* [cgw]: 长度为0,错误 */ if (len == 0) return NULL; /* [cgw]: 分配长度len个字节的内存空间,并清0内存 */ path = kzalloc(len, gfp_mask); if (!path) return NULL; /* [cgw]: 把所有节点对象名字填装到path中 */ fill_kobj_path(kobj, path, len); return path; } EXPORT_SYMBOL_GPL(kobject_get_path); /** * kobject_init - initialize object. * @kobj: object in question. */ void kobject_init(struct kobject * kobj) { /* [cgw]: kobj为空,错误 */ if (!kobj) return; /* [cgw]: 初始化kobj->kref引用计数为1 */ kref_init(&kobj->kref); /* [cgw]: 初始化kobj->entry链表 */ INIT_LIST_HEAD(&kobj->entry); /* [cgw]: 初始化kobj->poll */ init_waitqueue_head(&kobj->poll); /* [cgw]: 获得kset成员的指针 */ kobj->kset = kset_get(kobj->kset); } /** * unlink - remove kobject from kset list. * @kobj: kobject. * * Remove the kobject from the kset list and decrement * its parent's refcount. * This is separated out, so we can use it in both * kobject_del() and kobject_add() on error. */ static void unlink(struct kobject * kobj) { /* [cgw]: kobj所在kobj->kset集合存在 */ if (kobj->kset) { spin_lock(&kobj->kset->list_lock); /* [cgw]: 从链表中删除一个kobj条目 */ list_del_init(&kobj->entry); spin_unlock(&kobj->kset->list_lock); } /* [cgw]: kobj引用计数-1 */ kobject_put(kobj); } /** * kobject_shadow_add - add an object to the hierarchy. * @kobj: object. * @shadow_parent: sysfs directory to add to. */ int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent) { int error = 0; struct kobject * parent; /* [cgw]: kobj引用计数+1,并返回kobj指针 */ if (!(kobj = kobject_get(kobj))) return -ENOENT; /* [cgw]: kobj对象名字为空,即指针为空 */ if (!kobj->k_name) /* [cgw]: 添加名字,k_name指针指向name */ kobj->k_name = kobj->name; /* [cgw]: kobj对象名字为空,即指针为空 */ if (!*kobj->k_name) { pr_debug("kobject attempted to be registered with no name!\n"); WARN_ON(1); /* [cgw]: kobj引用计数-1,并返回kobj指针 * 如果kobj引用计数为0,则kobj将被移除 */ kobject_put(kobj); return -EINVAL; } /* [cgw]: 找到kobj的父节点 */ parent = kobject_get(kobj->parent); /* [cgw]: 打印当前节点和上一节点kobj的名字,和所属集合kset的kobj名字*/ pr_debug("kobject %s: registering. parent: %s, set: %s\n", kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", kobj->kset ? kobj->kset->kobj.name : "<NULL>" ); /* [cgw]: kobj所在集合kset指针存在 */ if (kobj->kset) { /* [cgw]: 进入临界区 */ spin_lock(&kobj->kset->list_lock); /* [cgw]: kobj父节点不为空 */ if (!parent) /* [cgw]: 找出kobj的成员kset的kobj对象 */ parent = kobject_get(&kobj->kset->kobj); /* [cgw]: 把当前kobj成员entry插入到kobj->kset->list链表中, * entry插入到kobj->kset->list节点前 */ list_add_tail(&kobj->entry,&kobj->kset->list); /* [cgw]: 退出临界区 */ spin_unlock(&kobj->kset->list_lock); /* [cgw]: kobj的父节点指针指向kobj的成员kset的kobj * 因为entry已经加入到kobj->kset->list中,即kobj已经加入了kset * 集合 */ kobj->parent = parent; } /* [cgw]: 为这个kobj创建目录,即加入到sysfs目录, * 并为这个kobj添加属性 */ error = create_dir(kobj, shadow_parent); /* [cgw]: 创建kobj目录发生错误 */ if (error) { /* unlink does the kobject_put() for us */ /* [cgw]: 移除当前kobj->entery节点 */ unlink(kobj); /* [cgw]: kobj父节点引用计数-1*/ kobject_put(parent); /* be noisy on error issues */ if (error == -EEXIST) /* [cgw]: 在相同目录下出现相同的对象名 */ printk(KERN_ERR "kobject_add failed for %s with " "-EEXIST, don't try to register things with " "the same name in the same directory.\n", kobject_name(kobj)); else /* [cgw]: kobject_add操作失败,因本函数是直接在kobject_add调用 **/ printk(KERN_ERR "kobject_add failed for %s (%d)\n", kobject_name(kobj), error); dump_stack(); } return error; } /** * kobject_add - add an object to the hierarchy. * @kobj: object. */ int kobject_add(struct kobject * kobj) { /* [cgw]: 添加kobj到层,实际上是把kobj添加到kset集合 * entry插入到kobj->kset->list链表中, 为这个kobj创建目录, * 即加入到sysfs目录,并为这个kobj添加属性 */ return kobject_shadow_add(kobj, NULL); } /** * @kobj: object in question. */ int kobject_register(struct kobject * kobj) { int error = -EINVAL; /* [cgw]: 已经定义了一个kobj对象 */ if (kobj) { /* [cgw]: 初始化这个kobj对象 */ kobject_init(kobj); /* [cgw]: 添加这个kobj到sysfs目录,kset集合 */ error = kobject_add(kobj); /* [cgw]: 添加成功 */ if (!error) /* [cgw]: 通知用户空间发生了KOBJ_ADD事件 */ kobject_uevent(kobj, KOBJ_ADD); } return error; } /** * kobject_set_name - Set the name of an object * @kobj: object. * @fmt: format string used to build the name * * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated * string that @kobj->k_name points to. Otherwise, use the static * @kobj->name array. */ int kobject_set_name(struct kobject * kobj, const char * fmt, ...) { int error = 0;、 /* [cgw]: kobj名字长度限制 */ int limit = KOBJ_NAME_LEN; int need; va_list args; char * name; /* * First, try the static array */ /* [cgw]: 格式化参数中的字符到kobj->name数组 */ va_start(args,fmt); need = vsnprintf(kobj->name,limit,fmt,args); va_end(args); /* [cgw]: 长度在限制以内 */ if (need < limit) /* [cgw]: name指向kobj->name */ name = kobj->name; else { /* * Need more space? Allocate it and try again */ /* [cgw]: 长度超出限制以外 */ limit = need + 1; /* [cgw]: 分配长度为need + 1个字节的内存空间 */ name = kmalloc(limit,GFP_KERNEL); /* [cgw]: 分配失败 */ if (!name) { error = -ENOMEM; goto Done; } /* [cgw]: 重新格式化到name */ va_start(args,fmt); need = vsnprintf(name,limit,fmt,args); va_end(args); /* Still? Give up. */ /* [cgw]: 还是不够? 算了,有心无力了 */ if (need >= limit) { /* [cgw]: 释放分配给name的内存空间 */ kfree(name); error = -EFAULT; goto Done; } } /* Free the old name, if necessary. */ /* [cgw]: 旧的名字不要了 */ if (kobj->k_name && kobj->k_name != kobj->name) /* [cgw]: 释放旧名字的内存空间 */ kfree(kobj->k_name); /* Now, set the new name */ /* [cgw]: 重置新名字 */ kobj->k_name = name; Done: return error; } EXPORT_SYMBOL(kobject_set_name); /** * kobject_rename - change the name of an object * @kobj: object in question. * @new_name: object's new name */ int kobject_rename(struct kobject * kobj, const char *new_name) { int error = 0; const char *devpath = NULL; char *devpath_string = NULL; char *envp[2]; /* [cgw]: kobj引用计数+1 */ kobj = kobject_get(kobj); /* [cgw]: kobj为空 */ if (!kobj) return -EINVAL; /* [cgw]: kobj的父节点为空 */ if (!kobj->parent) return -EINVAL; /* [cgw]: 获得当前节点kobj和所有父节点的名字 */ devpath = kobject_get_path(kobj, GFP_KERNEL); /* [cgw]: 获取失败 */ if (!devpath) { error = -ENOMEM; goto out; } /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */ devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); /* [cgw]: 分配失败 */ if (!devpath_string) { error = -ENOMEM; goto out; } /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */ sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); /* [cgw]: 存入环境变量 */ envp[0] = devpath_string; envp[1] = NULL; /* Note : if we want to send the new name alone, not the full path, * we could probably use kobject_name(kobj); */ /* [cgw]: 重命名kobj对象 */ error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name); /* This function is mostly/only used for network interface. * Some hotplug package track interfaces by their name and * therefore want to know when the name is changed by the user. */ /* [cgw]: 重命名成功 */ if (!error) /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */ kobject_uevent_env(kobj, KOBJ_MOVE, envp); out: /* [cgw]: 释放devpath_string和devpath内存 */ kfree(devpath_string); kfree(devpath); /* [cgw]: kobj引用计数-1,并删除kobj */ kobject_put(kobj); return error; } /** * kobject_rename - change the name of an object * @kobj: object in question. * @new_parent: object's new parent * @new_name: object's new name */ int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent, const char *new_name) { int error = 0; /* [cgw]: kobj引用计数+1*/ kobj = kobject_get(kobj); if (!kobj) return -EINVAL; /* [cgw]: 重命名kobj对象 */ error = sysfs_rename_dir(kobj, new_parent, new_name); /* [cgw]: kobj引用计数-1,并删除kobj */ kobject_put(kobj); return error; } /** * kobject_move - move object to another parent * @kobj: object in question. * @new_parent: object's new parent (can be NULL) */ int kobject_move(struct kobject *kobj, struct kobject *new_parent) { int error; struct kobject *old_parent; const char *devpath = NULL; char *devpath_string = NULL; char *envp[2]; /* [cgw]: kobj引用计数+1*/ kobj = kobject_get(kobj); /* [cgw]: kobj指针为空*/ if (!kobj) return -EINVAL; /* [cgw]: kobj父节点引用计数+1*/ new_parent = kobject_get(new_parent); /* [cgw]: kobj指针为空*/ if (!new_parent) { /* [cgw]: kobj所属kset存在 */ if (kobj->kset) /* [cgw]: kobj->kset->kobj引用计数+1 */ new_parent = kobject_get(&kobj->kset->kobj); } /* old object path */ /* [cgw]: 获得kobj 和所有父节点对象名字,即路径名*/ devpath = kobject_get_path(kobj, GFP_KERNEL); /* [cgw]: 获取失败 */ if (!devpath) { error = -ENOMEM; goto out; } /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */ devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); /* [cgw]: 分配失败 */ if (!devpath_string) { error = -ENOMEM; goto out; } /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */ sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); /* [cgw]: 存入环境变量 */ envp[0] = devpath_string; envp[1] = NULL; /* [cgw]: 移动kobj到新的父节点对象 */ error = sysfs_move_dir(kobj, new_parent); /* [cgw]: 失败 */ if (error) goto out; /* [cgw]: 记录kobj旧的的父节点对象 */ old_parent = kobj->parent; /* [cgw]: 用新的父节点对象代替旧的 */ kobj->parent = new_parent; new_parent = NULL; /* [cgw]: kobj旧的父节点对象引用计数-1, 并删除旧的父节点对象 */ kobject_put(old_parent); /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */ kobject_uevent_env(kobj, KOBJ_MOVE, envp); out: /* [cgw]: kobj新的父节点对象引用计数-1, 并删除新的父节点对象 */ kobject_put(new_parent); /* [cgw]: kobj引用计数-1, 并删除kobj */ kobject_put(kobj); /* [cgw]: 释放devpath_string和devpath内存 */ kfree(devpath_string); kfree(devpath); return error; } /** * kobject_del - unlink kobject from hierarchy. * @kobj: object. */ void kobject_del(struct kobject * kobj) { /* [cgw]: kobj对象不存在 */ if (!kobj) return; /* [cgw]: 从sysfs目录移除kobj对象,清0 kobj->dentry节点 */ sysfs_remove_dir(kobj); /* [cgw]: 删除kobj->dentry节点 */ unlink(kobj); } /** * kobject_unregister - remove object from hierarchy and decrement refcount. * @kobj: object going away. */ void kobject_unregister(struct kobject * kobj) { if (!kobj) return; pr_debug("kobject %s: unregistering\n",kobject_name(kobj)); /* [cgw]: 通知用户空间发生了KOBJ_REMOVE事件 */ kobject_uevent(kobj, KOBJ_REMOVE); /* [cgw]: 从sysfs目录删除kobj对象(删除kobj->entery, kobj->kset->list节点) */ kobject_del(kobj); /* [cgw]: 删除kobj对象,释放内存空间 */ kobject_put(kobj); } /** * kobject_get - increment refcount for object. * @kobj: object. */ struct kobject * kobject_get(struct kobject * kobj) { /* [cgw]: 指针不为空 */ if (kobj) /* [cgw]: 引用计数+1 */ kref_get(&kobj->kref); return kobj; } /** * kobject_cleanup - free kobject resources. * @kobj: object. */ void kobject_cleanup(struct kobject * kobj) { /* [cgw]: 找到kobj->ktype */ struct kobj_type * t = get_ktype(kobj); struct kset * s = kobj->kset; struct kobject * parent = kobj->parent; pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); /* [cgw]: k_name没有指向name */ if (kobj->k_name != kobj->name) /* [cgw]: 释放k_name内存空间 */ kfree(kobj->k_name); /* [cgw]: k_name指针清零 */ kobj->k_name = NULL; /* [cgw]: ktype定义的release函数 */ if (t && t->release) /* [cgw]: 删除kobj,并释放内存 */ t->release(kobj); if (s) /* [cgw]: kobj->kset->kobj引用计数-1 */ kset_put(s); /* [cgw]: kobj父节点引用计数-1 */ kobject_put(parent); } static void kobject_release(struct kref *kref) { /* [cgw]: 删除kobj->k_name, kobj, kobj->parent, kobj->kset, * 调用kobj->ktype->release */ kobject_cleanup(container_of(kref, struct kobject, kref)); } /** * kobject_put - decrement refcount for object. * @kobj: object. * * Decrement the refcount, and if 0, call kobject_cleanup(). */ void kobject_put(struct kobject * kobj) { /* [cgw]: 指针不为空 */ if (kobj) /* [cgw]: 引用计数-1 ,kobj->kref为0时,调用kobject_release */ kref_put(&kobj->kref, kobject_release); } static void dir_release(struct kobject *kobj) { /* [cgw]: 释放kobj内存空间 */ kfree(kobj); } static struct kobj_type dir_ktype = { .release = dir_release, .sysfs_ops = NULL, .default_attrs = NULL, }; /** * kobject_kset_add_dir - add sub directory of object. * @kset: kset the directory is belongs to. * @parent: object in which a directory is created. * @name: directory name. * * Add a plain directory object as child of given object. */ struct kobject *kobject_kset_add_dir(struct kset *kset, struct kobject *parent, const char *name) { struct kobject *k; int ret; /* [cgw]: parent指针为空 */ if (!parent) return NULL; /* [cgw]: 分配一个struct kobject的内存空间 */ k = kzalloc(sizeof(*k), GFP_KERNEL); /* [cgw]: 分配失败 */ if (!k) return NULL; /* [cgw]: 分配kobj->kset */ k->kset = kset; /* [cgw]: 分配kobj->parent */ k->parent = parent; /* [cgw]: 分配kobj->ktype */ k->ktype = &dir_ktype; /* [cgw]: 给kobj->k_name分配名字 */ kobject_set_name(k, name); /* [cgw]: 注册kobj */ ret = kobject_register(k); /* [cgw]: 注册失败 */ if (ret < 0) { printk(KERN_WARNING "%s: kobject_register error: %d\n", __func__, ret); /* [cgw]: 删除kobj */ kobject_del(k); return NULL; } return k; } /** * kobject_add_dir - add sub directory of object. * @parent: object in which a directory is created. * @name: directory name. * * Add a plain directory object as child of given object. */ struct kobject *kobject_add_dir(struct kobject *parent, const char *name) { /* [cgw]: 添加一个kobj对象子目录 * kobj->kset为空 */ return kobject_kset_add_dir(NULL, parent, name); } /** * kset_init - initialize a kset for use * @k: kset */ void kset_init(struct kset * k) { /* [cgw]: 初始化kset成员kobj对象 */ kobject_init(&k->kobj); /* [cgw]: 初始化kset成员list链表 */ INIT_LIST_HEAD(&k->list); /* [cgw]: 初始化保护kset成员list链表的自旋锁 */ spin_lock_init(&k->list_lock); } /** * kset_add - add a kset object to the hierarchy. * @k: kset. */ int kset_add(struct kset * k) { /* [cgw]: 添加kset成员kobj对象 * */ return kobject_add(&k->kobj); } /** * kset_register - initialize and add a kset. * @k: kset. */ int kset_register(struct kset * k) { /* [cgw]: 指针为空 */ if (!k) return -EINVAL; /* [cgw]: 初始化并添加kset */ kset_init(k); return kset_add(k); } /** * kset_unregister - remove a kset. * @k: kset. */ void kset_unregister(struct kset * k) { /* [cgw]: 指针为空 */ if (!k) return; /* [cgw]: 删除kset,实际是删除kset->kobj */ kobject_unregister(&k->kobj); } /** * kset_find_obj - search for object in kset. * @kset: kset we're looking in. * @name: object's name. * * Lock kset via @kset->subsys, and iterate over @kset->list, * looking for a matching kobject. If matching object is found * take a reference and return the object. */ struct kobject * kset_find_obj(struct kset * kset, const char * name) { struct list_head * entry; struct kobject * ret = NULL; /* [cgw]: 进入临界区 */ spin_lock(&kset->list_lock); /* [cgw]: 以kset->list为头节点开始轮询kset->list链表中 * 每个节点,取出存到entry */ list_for_each(entry,&kset->list) { /* [cgw]: 取出一个entry,根据这个entry,找到对应kobj */ struct kobject * k = to_kobj(entry); /* [cgw]: 找到对应的kobj,这个kobj的名字是否是要找的kobj的名字匹配 */ if (kobject_name(k) && !strcmp(kobject_name(k),name)) { /* [cgw]: 找到了这个kobj,引用计数+1 */ ret = kobject_get(k); break; } } /* [cgw]: 退出临界区 */ spin_unlock(&kset->list_lock); return ret; } void subsystem_init(struct kset *s) { /* [cgw]: 初始化kset,每一个 kset都属于一个子系统 * Every kset must belong to a subsystem. The subsystem * membership helps establish the kset's position in the hierarchy * <Linux Device Drivers> */ kset_init(s); } int subsystem_register(struct kset *s) { /* [cgw]: 注册kset */ return kset_register(s); } void subsystem_unregister(struct kset *s) { /* [cgw]: 注销kset */ kset_unregister(s); } /** * subsystem_create_file - export sysfs attribute file. * @s: subsystem. * @a: subsystem attribute descriptor. */ int subsys_create_file(struct kset *s, struct subsys_attribute *a) { int error = 0; if (!s || !a) return -EINVAL; /* [cgw]: 这个操作最终导致,kset->kobj引用计数+1 * 并返回ket指针 */ if (subsys_get(s)) { /* [cgw]: 为kset->kobj这个对象创建一个属性文件 */ error = sysfs_create_file(&s->kobj, &a->attr); /* [cgw]: 这个操作最终导致,kset->kobj引用计数-1 * 并返回kset指针 */ subsys_put(s); } return error; } EXPORT_SYMBOL(kobject_init); EXPORT_SYMBOL(kobject_register); EXPORT_SYMBOL(kobject_unregister); EXPORT_SYMBOL(kobject_get); EXPORT_SYMBOL(kobject_put); EXPORT_SYMBOL(kobject_add); EXPORT_SYMBOL(kobject_del); EXPORT_SYMBOL(kset_register); EXPORT_SYMBOL(kset_unregister); EXPORT_SYMBOL(subsystem_register); EXPORT_SYMBOL(subsystem_unregister); EXPORT_SYMBOL(subsys_create_file);
转载请注明原文地址: https://www.6miu.com/read-46329.html

最新回复(0)