kobject.c 添加注释
最近结合《Linux Device Drivers》对kobject的理解,对kobject.c文件添加注释,仅供参考!
1 /**
2 * populate_dir - populate directory with attributes.
3 * @kobj: object we're working on.
4 *
5 * Most subsystems have a set of default attributes that
6 * are associated with an object that registers with them.
7 * This is a helper called during object registration that
8 * loops through the default attributes of the subsystem
9 * and creates attributes files for them in sysfs.
10 *
11 */
12
13 static int populate_dir(struct kobject * kobj)
14 {
15 /* [cgw]: 从kobject取出kobj_type成员指针 */
16 struct kobj_type * t = get_ktype(kobj);
17 struct attribute * attr;
18 int error = 0;
19 int i;
20 /* [cgw]: kobj_type中默认属性default_attrs不为空,即已经添加一个或以上
21 * 属性项
22 */
23 if (t && t->default_attrs) {
24 /* [cgw]: 轮询default_attrs中的指针不为空,即已经添加属性 */
25 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
26 /* [cgw]: 在sysfs中为这个kobj创建一个属性文件 */
27 if ((error = sysfs_create_file(kobj,attr)))
28 break;
29 }
30 }
31 return error;
32 }
3 * @kobj: object we're working on.
4 *
5 * Most subsystems have a set of default attributes that
6 * are associated with an object that registers with them.
7 * This is a helper called during object registration that
8 * loops through the default attributes of the subsystem
9 * and creates attributes files for them in sysfs.
10 *
11 */
12
13 static int populate_dir(struct kobject * kobj)
14 {
15 /* [cgw]: 从kobject取出kobj_type成员指针 */
16 struct kobj_type * t = get_ktype(kobj);
17 struct attribute * attr;
18 int error = 0;
19 int i;
20 /* [cgw]: kobj_type中默认属性default_attrs不为空,即已经添加一个或以上
21 * 属性项
22 */
23 if (t && t->default_attrs) {
24 /* [cgw]: 轮询default_attrs中的指针不为空,即已经添加属性 */
25 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
26 /* [cgw]: 在sysfs中为这个kobj创建一个属性文件 */
27 if ((error = sysfs_create_file(kobj,attr)))
28 break;
29 }
30 }
31 return error;
32 }
1 static int create_dir(struct kobject * kobj, struct dentry *shadow_parent)
2 {
3 int error = 0;
4 /* [cgw]: 获得这个kobj的名字,名字不为空 */
5 if (kobject_name(kobj)) {
6 /* [cgw]: 为kobj创建一个目录
7 * @shadow_parent: parent parent object. (父对象)
8 *
9 */
10 error = sysfs_create_dir(kobj, shadow_parent);
11 if (!error) {
12 /* [cgw]: 添加kobj属性*/
13 if ((error = populate_dir(kobj)))
14 /* [cgw]: 添加失败,删除kobj对象目录 */
15 sysfs_remove_dir(kobj);
16 }
17 }
18 return error;
19 }
3 int error = 0;
4 /* [cgw]: 获得这个kobj的名字,名字不为空 */
5 if (kobject_name(kobj)) {
6 /* [cgw]: 为kobj创建一个目录
7 * @shadow_parent: parent parent object. (父对象)
8 *
9 */
10 error = sysfs_create_dir(kobj, shadow_parent);
11 if (!error) {
12 /* [cgw]: 添加kobj属性*/
13 if ((error = populate_dir(kobj)))
14 /* [cgw]: 添加失败,删除kobj对象目录 */
15 sysfs_remove_dir(kobj);
16 }
17 }
18 return error;
19 }
1 static inline struct kobject * to_kobj(struct list_head * entry)
2 {
3 /* [cgw]: 从链表list_head取出一个entry
4 * 根据这个entry,找到包含这个entry的
5 * kobject结构体地址
6 */
7 return container_of(entry,struct kobject,entry);
8 }
3 /* [cgw]: 从链表list_head取出一个entry
4 * 根据这个entry,找到包含这个entry的
5 * kobject结构体地址
6 */
7 return container_of(entry,struct kobject,entry);
8 }
1 static int get_kobj_path_length(struct kobject *kobj)
2 {
3 int length = 1;
4 struct kobject * parent = kobj;
5
6 /* walk up the ancestors until we hit the one pointing to the
7 * root.
8 * Add 1 to strlen for leading '/' of each level.
9 */
10 /* [cgw]: 逐个节点查找对象的名字,计算每节点对象的名字长度,
11 * 并加1,加1是每个名字前有个'/',并累计所有名字长度
12 */
13 do {
14 /* [cgw]: 名字不为空 */
15 if (kobject_name(parent) == NULL)
16 return 0;
17 /* [cgw]: 累计名字长度 */
18 length += strlen(kobject_name(parent)) + 1;
19 /* [cgw]: 指向上一节点(当前对象的父辈) */
20 parent = parent->parent;
21 } while (parent);
22 return length;
23 }
3 int length = 1;
4 struct kobject * parent = kobj;
5
6 /* walk up the ancestors until we hit the one pointing to the
7 * root.
8 * Add 1 to strlen for leading '/' of each level.
9 */
10 /* [cgw]: 逐个节点查找对象的名字,计算每节点对象的名字长度,
11 * 并加1,加1是每个名字前有个'/',并累计所有名字长度
12 */
13 do {
14 /* [cgw]: 名字不为空 */
15 if (kobject_name(parent) == NULL)
16 return 0;
17 /* [cgw]: 累计名字长度 */
18 length += strlen(kobject_name(parent)) + 1;
19 /* [cgw]: 指向上一节点(当前对象的父辈) */
20 parent = parent->parent;
21 } while (parent);
22 return length;
23 }
1 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
2 {
3 struct kobject * parent;
4
5 /* [cgw]: 逐个节点查找对象的名字,并填装到path中 */
6 --length;
7 for (parent = kobj; parent; parent = parent->parent) {
8 /* [cgw]: 计算当前节点对象名字长度 */
9 int cur = strlen(kobject_name(parent));
10 /* back up enough to print this name with '/' */
11 /* [cgw]: 总长度减去一个名字的长度 */
12 length -= cur;
13 /* [cgw]: 把这个对象名字填装到path的path + length位置 */
14 strncpy (path + length, kobject_name(parent), cur);
15 /* [cgw]: 在path的path + length -1的位置插入'/' */
16 *(path + --length) = '/';
17 }
18
19 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
20 }
3 struct kobject * parent;
4
5 /* [cgw]: 逐个节点查找对象的名字,并填装到path中 */
6 --length;
7 for (parent = kobj; parent; parent = parent->parent) {
8 /* [cgw]: 计算当前节点对象名字长度 */
9 int cur = strlen(kobject_name(parent));
10 /* back up enough to print this name with '/' */
11 /* [cgw]: 总长度减去一个名字的长度 */
12 length -= cur;
13 /* [cgw]: 把这个对象名字填装到path的path + length位置 */
14 strncpy (path + length, kobject_name(parent), cur);
15 /* [cgw]: 在path的path + length -1的位置插入'/' */
16 *(path + --length) = '/';
17 }
18
19 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
20 }
1 /**
2 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
3 *
4 * @kobj: kobject in question, with which to build the path
5 * @gfp_mask: the allocation type used to allocate the path
6 *
7 * The result must be freed by the caller with kfree().
8 */
9 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
10 {
11 char *path;
12 int len;
13
14 /* [cgw]: 根据当前节点kobj,统计从当前节点开始到所有父辈节点的名字
15 * 总长度
16 */
17 len = get_kobj_path_length(kobj);
18 /* [cgw]: 长度为0,错误 */
19 if (len == 0)
20 return NULL;
21 /* [cgw]: 分配长度len个字节的内存空间,并清0内存 */
22 path = kzalloc(len, gfp_mask);
23 if (!path)
24 return NULL;
25 /* [cgw]: 把所有节点对象名字填装到path中 */
26 fill_kobj_path(kobj, path, len);
27
28 return path;
29 }
30 EXPORT_SYMBOL_GPL(kobject_get_path);
3 *
4 * @kobj: kobject in question, with which to build the path
5 * @gfp_mask: the allocation type used to allocate the path
6 *
7 * The result must be freed by the caller with kfree().
8 */
9 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
10 {
11 char *path;
12 int len;
13
14 /* [cgw]: 根据当前节点kobj,统计从当前节点开始到所有父辈节点的名字
15 * 总长度
16 */
17 len = get_kobj_path_length(kobj);
18 /* [cgw]: 长度为0,错误 */
19 if (len == 0)
20 return NULL;
21 /* [cgw]: 分配长度len个字节的内存空间,并清0内存 */
22 path = kzalloc(len, gfp_mask);
23 if (!path)
24 return NULL;
25 /* [cgw]: 把所有节点对象名字填装到path中 */
26 fill_kobj_path(kobj, path, len);
27
28 return path;
29 }
30 EXPORT_SYMBOL_GPL(kobject_get_path);
1 /**
2 * kobject_init - initialize object.
3 * @kobj: object in question.
4 */
5 void kobject_init(struct kobject * kobj)
6 {
7 /* [cgw]: kobj为空,错误 */
8 if (!kobj)
9 return;
10 /* [cgw]: 初始化kobj->kref引用计数为1 */
11 kref_init(&kobj->kref);
12 /* [cgw]: 初始化kobj->entry链表 */
13 INIT_LIST_HEAD(&kobj->entry);
14 /* [cgw]: 初始化kobj->poll */
15 init_waitqueue_head(&kobj->poll);
16 /* [cgw]: 获得kset成员的指针 */
17 kobj->kset = kset_get(kobj->kset);
18 }
2 * kobject_init - initialize object.
3 * @kobj: object in question.
4 */
5 void kobject_init(struct kobject * kobj)
6 {
7 /* [cgw]: kobj为空,错误 */
8 if (!kobj)
9 return;
10 /* [cgw]: 初始化kobj->kref引用计数为1 */
11 kref_init(&kobj->kref);
12 /* [cgw]: 初始化kobj->entry链表 */
13 INIT_LIST_HEAD(&kobj->entry);
14 /* [cgw]: 初始化kobj->poll */
15 init_waitqueue_head(&kobj->poll);
16 /* [cgw]: 获得kset成员的指针 */
17 kobj->kset = kset_get(kobj->kset);
18 }
/**
* 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);
}
* @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_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;
}
1 /**
2 * kobject_add - add an object to the hierarchy.
3 * @kobj: object.
4 */
5 int kobject_add(struct kobject * kobj)
6 {
7 /* [cgw]: 添加kobj到层,实际上是把kobj添加到kset集合
8 * entry插入到kobj->kset->list链表中, 为这个kobj创建目录,
9 * 即加入到sysfs目录,并为这个kobj添加属性
10 */
11 return kobject_shadow_add(kobj, NULL);
12 }
3 * @kobj: object.
4 */
5 int kobject_add(struct kobject * kobj)
6 {
7 /* [cgw]: 添加kobj到层,实际上是把kobj添加到kset集合
8 * entry插入到kobj->kset->list链表中, 为这个kobj创建目录,
9 * 即加入到sysfs目录,并为这个kobj添加属性
10 */
11 return kobject_shadow_add(kobj, NULL);
12 }
1 /**
2 * kobject_register - initialize and add an object.
3 * @kobj: object in question.
4 */
5
6 int kobject_register(struct kobject * kobj)
7 {
8 int error = -EINVAL;
9 /* [cgw]: 已经定义了一个kobj对象 */
10 if (kobj) {
11 /* [cgw]: 初始化这个kobj对象 */
12 kobject_init(kobj);
13 /* [cgw]: 添加这个kobj到sysfs目录,kset集合 */
14 error = kobject_add(kobj);
15 /* [cgw]: 添加成功 */
16 if (!error)
17 /* [cgw]: 通知用户空间发生了KOBJ_ADD事件 */
18 kobject_uevent(kobj, KOBJ_ADD);
19 }
20 return error;
21 }
3 * @kobj: object in question.
4 */
5
6 int kobject_register(struct kobject * kobj)
7 {
8 int error = -EINVAL;
9 /* [cgw]: 已经定义了一个kobj对象 */
10 if (kobj) {
11 /* [cgw]: 初始化这个kobj对象 */
12 kobject_init(kobj);
13 /* [cgw]: 添加这个kobj到sysfs目录,kset集合 */
14 error = kobject_add(kobj);
15 /* [cgw]: 添加成功 */
16 if (!error)
17 /* [cgw]: 通知用户空间发生了KOBJ_ADD事件 */
18 kobject_uevent(kobj, KOBJ_ADD);
19 }
20 return error;
21 }
1 /**
2 * kobject_set_name - Set the name of an object
3 * @kobj: object.
4 * @fmt: format string used to build the name
5 *
6 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
7 * string that @kobj->k_name points to. Otherwise, use the static
8 * @kobj->name array.
9 */
10 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
11 {
12 int error = 0;、
13 /* [cgw]: kobj名字长度限制 */
14 int limit = KOBJ_NAME_LEN;
15 int need;
16 va_list args;
17 char * name;
18
19 /*
20 * First, try the static array
21 */
22 /* [cgw]: 格式化参数中的字符到kobj->name数组 */
23 va_start(args,fmt);
24 need = vsnprintf(kobj->name,limit,fmt,args);
25 va_end(args);
26 /* [cgw]: 长度在限制以内 */
27 if (need < limit)
28 /* [cgw]: name指向kobj->name */
29 name = kobj->name;
30 else {
31 /*
32 * Need more space? Allocate it and try again
33 */
34 /* [cgw]: 长度超出限制以外 */
35 limit = need + 1;
36 /* [cgw]: 分配长度为need + 1个字节的内存空间 */
37 name = kmalloc(limit,GFP_KERNEL);
38 /* [cgw]: 分配失败 */
39 if (!name) {
40 error = -ENOMEM;
41 goto Done;
42 }
43 /* [cgw]: 重新格式化到name */
44 va_start(args,fmt);
45 need = vsnprintf(name,limit,fmt,args);
46 va_end(args);
47
48 /* Still? Give up. */
49 /* [cgw]: 还是不够? 算了,有心无力了 */
50 if (need >= limit) {
51 /* [cgw]: 释放分配给name的内存空间 */
52 kfree(name);
53 error = -EFAULT;
54 goto Done;
55 }
56 }
57
58 /* Free the old name, if necessary. */
59 /* [cgw]: 旧的名字不要了 */
60 if (kobj->k_name && kobj->k_name != kobj->name)
61 /* [cgw]: 释放旧名字的内存空间 */
62 kfree(kobj->k_name);
63
64 /* Now, set the new name */
65 /* [cgw]: 重置新名字 */
66 kobj->k_name = name;
67 Done:
68 return error;
69 }
70
71 EXPORT_SYMBOL(kobject_set_name);
3 * @kobj: object.
4 * @fmt: format string used to build the name
5 *
6 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
7 * string that @kobj->k_name points to. Otherwise, use the static
8 * @kobj->name array.
9 */
10 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
11 {
12 int error = 0;、
13 /* [cgw]: kobj名字长度限制 */
14 int limit = KOBJ_NAME_LEN;
15 int need;
16 va_list args;
17 char * name;
18
19 /*
20 * First, try the static array
21 */
22 /* [cgw]: 格式化参数中的字符到kobj->name数组 */
23 va_start(args,fmt);
24 need = vsnprintf(kobj->name,limit,fmt,args);
25 va_end(args);
26 /* [cgw]: 长度在限制以内 */
27 if (need < limit)
28 /* [cgw]: name指向kobj->name */
29 name = kobj->name;
30 else {
31 /*
32 * Need more space? Allocate it and try again
33 */
34 /* [cgw]: 长度超出限制以外 */
35 limit = need + 1;
36 /* [cgw]: 分配长度为need + 1个字节的内存空间 */
37 name = kmalloc(limit,GFP_KERNEL);
38 /* [cgw]: 分配失败 */
39 if (!name) {
40 error = -ENOMEM;
41 goto Done;
42 }
43 /* [cgw]: 重新格式化到name */
44 va_start(args,fmt);
45 need = vsnprintf(name,limit,fmt,args);
46 va_end(args);
47
48 /* Still? Give up. */
49 /* [cgw]: 还是不够? 算了,有心无力了 */
50 if (need >= limit) {
51 /* [cgw]: 释放分配给name的内存空间 */
52 kfree(name);
53 error = -EFAULT;
54 goto Done;
55 }
56 }
57
58 /* Free the old name, if necessary. */
59 /* [cgw]: 旧的名字不要了 */
60 if (kobj->k_name && kobj->k_name != kobj->name)
61 /* [cgw]: 释放旧名字的内存空间 */
62 kfree(kobj->k_name);
63
64 /* Now, set the new name */
65 /* [cgw]: 重置新名字 */
66 kobj->k_name = name;
67 Done:
68 return error;
69 }
70
71 EXPORT_SYMBOL(kobject_set_name);
1 /**
2 * kobject_rename - change the name of an object
3 * @kobj: object in question.
4 * @new_name: object's new name
5 */
6
7 int kobject_rename(struct kobject * kobj, const char *new_name)
8 {
9 int error = 0;
10 const char *devpath = NULL;
11 char *devpath_string = NULL;
12 char *envp[2];
13
14 /* [cgw]: kobj引用计数+1 */
15 kobj = kobject_get(kobj);
16 /* [cgw]: kobj为空 */
17 if (!kobj)
18 return -EINVAL;
19 /* [cgw]: kobj的父节点为空 */
20 if (!kobj->parent)
21 return -EINVAL;
22 /* [cgw]: 获得当前节点kobj和所有父节点的名字 */
23 devpath = kobject_get_path(kobj, GFP_KERNEL);
24 /* [cgw]: 获取失败 */
25 if (!devpath) {
26 error = -ENOMEM;
27 goto out;
28 }
29 /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */
30 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
31 /* [cgw]: 分配失败 */
32 if (!devpath_string) {
33 error = -ENOMEM;
34 goto out;
35 }
36 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
37 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
38 /* [cgw]: 存入环境变量 */
39 envp[0] = devpath_string;
40 envp[1] = NULL;
41 /* Note : if we want to send the new name alone, not the full path,
42 * we could probably use kobject_name(kobj); */
43 /* [cgw]: 重命名kobj对象 */
44 error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
45
46 /* This function is mostly/only used for network interface.
47 * Some hotplug package track interfaces by their name and
48 * therefore want to know when the name is changed by the user. */
49 /* [cgw]: 重命名成功 */
50 if (!error)
51 /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */
52 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
53
54 out:
55 /* [cgw]: 释放devpath_string和devpath内存 */
56 kfree(devpath_string);
57 kfree(devpath);
58 /* [cgw]: kobj引用计数-1,并删除kobj */
59 kobject_put(kobj);
60
61 return error;
62 }
3 * @kobj: object in question.
4 * @new_name: object's new name
5 */
6
7 int kobject_rename(struct kobject * kobj, const char *new_name)
8 {
9 int error = 0;
10 const char *devpath = NULL;
11 char *devpath_string = NULL;
12 char *envp[2];
13
14 /* [cgw]: kobj引用计数+1 */
15 kobj = kobject_get(kobj);
16 /* [cgw]: kobj为空 */
17 if (!kobj)
18 return -EINVAL;
19 /* [cgw]: kobj的父节点为空 */
20 if (!kobj->parent)
21 return -EINVAL;
22 /* [cgw]: 获得当前节点kobj和所有父节点的名字 */
23 devpath = kobject_get_path(kobj, GFP_KERNEL);
24 /* [cgw]: 获取失败 */
25 if (!devpath) {
26 error = -ENOMEM;
27 goto out;
28 }
29 /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */
30 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
31 /* [cgw]: 分配失败 */
32 if (!devpath_string) {
33 error = -ENOMEM;
34 goto out;
35 }
36 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
37 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
38 /* [cgw]: 存入环境变量 */
39 envp[0] = devpath_string;
40 envp[1] = NULL;
41 /* Note : if we want to send the new name alone, not the full path,
42 * we could probably use kobject_name(kobj); */
43 /* [cgw]: 重命名kobj对象 */
44 error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
45
46 /* This function is mostly/only used for network interface.
47 * Some hotplug package track interfaces by their name and
48 * therefore want to know when the name is changed by the user. */
49 /* [cgw]: 重命名成功 */
50 if (!error)
51 /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */
52 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
53
54 out:
55 /* [cgw]: 释放devpath_string和devpath内存 */
56 kfree(devpath_string);
57 kfree(devpath);
58 /* [cgw]: kobj引用计数-1,并删除kobj */
59 kobject_put(kobj);
60
61 return error;
62 }
1 /**
2 * kobject_rename - change the name of an object
3 * @kobj: object in question.
4 * @new_parent: object's new parent
5 * @new_name: object's new name
6 */
7
8 int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
9 const char *new_name)
10 {
11 int error = 0;
12 /* [cgw]: kobj引用计数+1*/
13 kobj = kobject_get(kobj);
14 if (!kobj)
15 return -EINVAL;
16 /* [cgw]: 重命名kobj对象 */
17 error = sysfs_rename_dir(kobj, new_parent, new_name);
18 /* [cgw]: kobj引用计数-1,并删除kobj */
19 kobject_put(kobj);
20
21 return error;
22 }
3 * @kobj: object in question.
4 * @new_parent: object's new parent
5 * @new_name: object's new name
6 */
7
8 int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
9 const char *new_name)
10 {
11 int error = 0;
12 /* [cgw]: kobj引用计数+1*/
13 kobj = kobject_get(kobj);
14 if (!kobj)
15 return -EINVAL;
16 /* [cgw]: 重命名kobj对象 */
17 error = sysfs_rename_dir(kobj, new_parent, new_name);
18 /* [cgw]: kobj引用计数-1,并删除kobj */
19 kobject_put(kobj);
20
21 return error;
22 }
1 /**
2 * kobject_move - move object to another parent
3 * @kobj: object in question.
4 * @new_parent: object's new parent (can be NULL)
5 */
6
7 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
8 {
9 int error;
10 struct kobject *old_parent;
11 const char *devpath = NULL;
12 char *devpath_string = NULL;
13 char *envp[2];
14
15 /* [cgw]: kobj引用计数+1*/
16 kobj = kobject_get(kobj);
17 /* [cgw]: kobj指针为空*/
18 if (!kobj)
19 return -EINVAL;
20 /* [cgw]: kobj父节点引用计数+1*/
21 new_parent = kobject_get(new_parent);
22 /* [cgw]: kobj指针为空*/
23 if (!new_parent) {
24 /* [cgw]: kobj所属kset存在 */
25 if (kobj->kset)
26 /* [cgw]: kobj->kset->kobj引用计数+1
27 */
28 new_parent = kobject_get(&kobj->kset->kobj);
29 }
30 /* old object path */
31 /* [cgw]: 获得kobj 和所有父节点对象名字,即路径名*/
32 devpath = kobject_get_path(kobj, GFP_KERNEL);
33 /* [cgw]: 获取失败 */
34 if (!devpath) {
35 error = -ENOMEM;
36 goto out;
37 }
38 /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */
39 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
40 /* [cgw]: 分配失败 */
41 if (!devpath_string) {
42 error = -ENOMEM;
43 goto out;
44 }
45 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
46 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
47 /* [cgw]: 存入环境变量 */
48 envp[0] = devpath_string;
49 envp[1] = NULL;
50 /* [cgw]: 移动kobj到新的父节点对象 */
51 error = sysfs_move_dir(kobj, new_parent);
52 /* [cgw]: 失败 */
53 if (error)
54 goto out;
55 /* [cgw]: 记录kobj旧的的父节点对象 */
56 old_parent = kobj->parent;
57 /* [cgw]: 用新的父节点对象代替旧的 */
58 kobj->parent = new_parent;
59 new_parent = NULL;
60 /* [cgw]: kobj旧的父节点对象引用计数-1, 并删除旧的父节点对象 */
61 kobject_put(old_parent);
62 /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */
63 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
64 out:
65 /* [cgw]: kobj新的父节点对象引用计数-1, 并删除新的父节点对象 */
66 kobject_put(new_parent);
67 /* [cgw]: kobj引用计数-1, 并删除kobj */
68 kobject_put(kobj);
69 /* [cgw]: 释放devpath_string和devpath内存 */
70 kfree(devpath_string);
71 kfree(devpath);
72 return error;
73 }
3 * @kobj: object in question.
4 * @new_parent: object's new parent (can be NULL)
5 */
6
7 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
8 {
9 int error;
10 struct kobject *old_parent;
11 const char *devpath = NULL;
12 char *devpath_string = NULL;
13 char *envp[2];
14
15 /* [cgw]: kobj引用计数+1*/
16 kobj = kobject_get(kobj);
17 /* [cgw]: kobj指针为空*/
18 if (!kobj)
19 return -EINVAL;
20 /* [cgw]: kobj父节点引用计数+1*/
21 new_parent = kobject_get(new_parent);
22 /* [cgw]: kobj指针为空*/
23 if (!new_parent) {
24 /* [cgw]: kobj所属kset存在 */
25 if (kobj->kset)
26 /* [cgw]: kobj->kset->kobj引用计数+1
27 */
28 new_parent = kobject_get(&kobj->kset->kobj);
29 }
30 /* old object path */
31 /* [cgw]: 获得kobj 和所有父节点对象名字,即路径名*/
32 devpath = kobject_get_path(kobj, GFP_KERNEL);
33 /* [cgw]: 获取失败 */
34 if (!devpath) {
35 error = -ENOMEM;
36 goto out;
37 }
38 /* [cgw]: 分配strlen(devpath) + 15字节内存空间 */
39 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
40 /* [cgw]: 分配失败 */
41 if (!devpath_string) {
42 error = -ENOMEM;
43 goto out;
44 }
45 /* [cgw]: 格式化"DEVPATH_OLD=%s" + devpath 存到devpath_string */
46 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
47 /* [cgw]: 存入环境变量 */
48 envp[0] = devpath_string;
49 envp[1] = NULL;
50 /* [cgw]: 移动kobj到新的父节点对象 */
51 error = sysfs_move_dir(kobj, new_parent);
52 /* [cgw]: 失败 */
53 if (error)
54 goto out;
55 /* [cgw]: 记录kobj旧的的父节点对象 */
56 old_parent = kobj->parent;
57 /* [cgw]: 用新的父节点对象代替旧的 */
58 kobj->parent = new_parent;
59 new_parent = NULL;
60 /* [cgw]: kobj旧的父节点对象引用计数-1, 并删除旧的父节点对象 */
61 kobject_put(old_parent);
62 /* [cgw]: 发送一个用户事件KOBJ_MOVE,和环境变量 */
63 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
64 out:
65 /* [cgw]: kobj新的父节点对象引用计数-1, 并删除新的父节点对象 */
66 kobject_put(new_parent);
67 /* [cgw]: kobj引用计数-1, 并删除kobj */
68 kobject_put(kobj);
69 /* [cgw]: 释放devpath_string和devpath内存 */
70 kfree(devpath_string);
71 kfree(devpath);
72 return error;
73 }
1 /**
2 * kobject_del - unlink kobject from hierarchy.
3 * @kobj: object.
4 */
5
6 void kobject_del(struct kobject * kobj)
7 {
8 /* [cgw]: kobj对象不存在 */
9 if (!kobj)
10 return;
11 /* [cgw]: 从sysfs目录移除kobj对象,清0 kobj->dentry节点 */
12 sysfs_remove_dir(kobj);
13 /* [cgw]: 删除kobj->dentry节点 */
14 unlink(kobj);
15 }
3 * @kobj: object.
4 */
5
6 void kobject_del(struct kobject * kobj)
7 {
8 /* [cgw]: kobj对象不存在 */
9 if (!kobj)
10 return;
11 /* [cgw]: 从sysfs目录移除kobj对象,清0 kobj->dentry节点 */
12 sysfs_remove_dir(kobj);
13 /* [cgw]: 删除kobj->dentry节点 */
14 unlink(kobj);
15 }
1 /**
2 * kobject_unregister - remove object from hierarchy and decrement refcount.
3 * @kobj: object going away.
4 */
5
6 void kobject_unregister(struct kobject * kobj)
7 {
8 if (!kobj)
9 return;
10 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
11 /* [cgw]: 通知用户空间发生了KOBJ_REMOVE事件 */
12 kobject_uevent(kobj, KOBJ_REMOVE);
13 /* [cgw]: 从sysfs目录删除kobj对象(删除kobj->entery, kobj->kset->list节点) */
14 kobject_del(kobj);
15 /* [cgw]: 删除kobj对象,释放内存空间 */
16 kobject_put(kobj);
17 }
3 * @kobj: object going away.
4 */
5
6 void kobject_unregister(struct kobject * kobj)
7 {
8 if (!kobj)
9 return;
10 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
11 /* [cgw]: 通知用户空间发生了KOBJ_REMOVE事件 */
12 kobject_uevent(kobj, KOBJ_REMOVE);
13 /* [cgw]: 从sysfs目录删除kobj对象(删除kobj->entery, kobj->kset->list节点) */
14 kobject_del(kobj);
15 /* [cgw]: 删除kobj对象,释放内存空间 */
16 kobject_put(kobj);
17 }
1 /**
2 * kobject_get - increment refcount for object.
3 * @kobj: object.
4 */
5
6 struct kobject * kobject_get(struct kobject * kobj)
7 {
8 /* [cgw]: 指针不为空 */
9 if (kobj)
10 /* [cgw]: 引用计数+1 */
11 kref_get(&kobj->kref);
12 return kobj;
13 }
3 * @kobj: object.
4 */
5
6 struct kobject * kobject_get(struct kobject * kobj)
7 {
8 /* [cgw]: 指针不为空 */
9 if (kobj)
10 /* [cgw]: 引用计数+1 */
11 kref_get(&kobj->kref);
12 return kobj;
13 }
1 /**
2 * kobject_cleanup - free kobject resources.
3 * @kobj: object.
4 */
5
6 void kobject_cleanup(struct kobject * kobj)
7 {
8 /* [cgw]: 找到kobj->ktype */
9 struct kobj_type * t = get_ktype(kobj);
10 struct kset * s = kobj->kset;
11 struct kobject * parent = kobj->parent;
12
13 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
14 /* [cgw]: k_name没有指向name */
15 if (kobj->k_name != kobj->name)
16 /* [cgw]: 释放k_name内存空间 */
17 kfree(kobj->k_name);
18 /* [cgw]: k_name指针清零 */
19 kobj->k_name = NULL;
20 /* [cgw]: ktype定义的release函数 */
21 if (t && t->release)
22 /* [cgw]: 删除kobj,并释放内存 */
23 t->release(kobj);
24 if (s)
25 /* [cgw]: kobj->kset->kobj引用计数-1 */
26 kset_put(s);
27 /* [cgw]: kobj父节点引用计数-1 */
28 kobject_put(parent);
29 }
3 * @kobj: object.
4 */
5
6 void kobject_cleanup(struct kobject * kobj)
7 {
8 /* [cgw]: 找到kobj->ktype */
9 struct kobj_type * t = get_ktype(kobj);
10 struct kset * s = kobj->kset;
11 struct kobject * parent = kobj->parent;
12
13 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
14 /* [cgw]: k_name没有指向name */
15 if (kobj->k_name != kobj->name)
16 /* [cgw]: 释放k_name内存空间 */
17 kfree(kobj->k_name);
18 /* [cgw]: k_name指针清零 */
19 kobj->k_name = NULL;
20 /* [cgw]: ktype定义的release函数 */
21 if (t && t->release)
22 /* [cgw]: 删除kobj,并释放内存 */
23 t->release(kobj);
24 if (s)
25 /* [cgw]: kobj->kset->kobj引用计数-1 */
26 kset_put(s);
27 /* [cgw]: kobj父节点引用计数-1 */
28 kobject_put(parent);
29 }
1 static void kobject_release(struct kref *kref)
2 {
3 /* [cgw]: 删除kobj->k_name, kobj, kobj->parent, kobj->kset,
4 * 调用kobj->ktype->release
5 */
6 kobject_cleanup(container_of(kref, struct kobject, kref));
7 }
3 /* [cgw]: 删除kobj->k_name, kobj, kobj->parent, kobj->kset,
4 * 调用kobj->ktype->release
5 */
6 kobject_cleanup(container_of(kref, struct kobject, kref));
7 }
1 /**
2 * kobject_put - decrement refcount for object.
3 * @kobj: object.
4 *
5 * Decrement the refcount, and if 0, call kobject_cleanup().
6 */
7 void kobject_put(struct kobject * kobj)
8 {
9 /* [cgw]: 指针不为空 */
10 if (kobj)
11 /* [cgw]: 引用计数-1 ,kobj->kref为0时,调用kobject_release */
12 kref_put(&kobj->kref, kobject_release);
13 }
3 * @kobj: object.
4 *
5 * Decrement the refcount, and if 0, call kobject_cleanup().
6 */
7 void kobject_put(struct kobject * kobj)
8 {
9 /* [cgw]: 指针不为空 */
10 if (kobj)
11 /* [cgw]: 引用计数-1 ,kobj->kref为0时,调用kobject_release */
12 kref_put(&kobj->kref, kobject_release);
13 }
1 static void dir_release(struct kobject *kobj)
2 {
3 /* [cgw]: 释放kobj内存空间 */
4 kfree(kobj);
5 }
3 /* [cgw]: 释放kobj内存空间 */
4 kfree(kobj);
5 }
1 static struct kobj_type dir_ktype = {
2 .release = dir_release,
3 .sysfs_ops = NULL,
4 .default_attrs = NULL,
5 };
3 .sysfs_ops = NULL,
4 .default_attrs = NULL,
5 };
1 /**
2 * kobject_kset_add_dir - add sub directory of object.
3 * @kset: kset the directory is belongs to.
4 * @parent: object in which a directory is created.
5 * @name: directory name.
6 *
7 * Add a plain directory object as child of given object.
8 */
9 struct kobject *kobject_kset_add_dir(struct kset *kset,
10 struct kobject *parent, const char *name)
11 {
12 struct kobject *k;
13 int ret;
14
15 /* [cgw]: parent指针为空 */
16 if (!parent)
17 return NULL;
18
19 /* [cgw]: 分配一个struct kobject的内存空间 */
20 k = kzalloc(sizeof(*k), GFP_KERNEL);
21 /* [cgw]: 分配失败 */
22 if (!k)
23 return NULL;
24
25 /* [cgw]: 分配kobj->kset */
26 k->kset = kset;
27 /* [cgw]: 分配kobj->parent */
28 k->parent = parent;
29 /* [cgw]: 分配kobj->ktype */
30 k->ktype = &dir_ktype;
31 /* [cgw]: 给kobj->k_name分配名字 */
32 kobject_set_name(k, name);
33 /* [cgw]: 注册kobj */
34 ret = kobject_register(k);
35 /* [cgw]: 注册失败 */
36 if (ret < 0) {
37 printk(KERN_WARNING "%s: kobject_register error: %d\n",
38 __func__, ret);
39 /* [cgw]: 删除kobj */
40 kobject_del(k);
41 return NULL;
42 }
43
44 return k;
45 }
3 * @kset: kset the directory is belongs to.
4 * @parent: object in which a directory is created.
5 * @name: directory name.
6 *
7 * Add a plain directory object as child of given object.
8 */
9 struct kobject *kobject_kset_add_dir(struct kset *kset,
10 struct kobject *parent, const char *name)
11 {
12 struct kobject *k;
13 int ret;
14
15 /* [cgw]: parent指针为空 */
16 if (!parent)
17 return NULL;
18
19 /* [cgw]: 分配一个struct kobject的内存空间 */
20 k = kzalloc(sizeof(*k), GFP_KERNEL);
21 /* [cgw]: 分配失败 */
22 if (!k)
23 return NULL;
24
25 /* [cgw]: 分配kobj->kset */
26 k->kset = kset;
27 /* [cgw]: 分配kobj->parent */
28 k->parent = parent;
29 /* [cgw]: 分配kobj->ktype */
30 k->ktype = &dir_ktype;
31 /* [cgw]: 给kobj->k_name分配名字 */
32 kobject_set_name(k, name);
33 /* [cgw]: 注册kobj */
34 ret = kobject_register(k);
35 /* [cgw]: 注册失败 */
36 if (ret < 0) {
37 printk(KERN_WARNING "%s: kobject_register error: %d\n",
38 __func__, ret);
39 /* [cgw]: 删除kobj */
40 kobject_del(k);
41 return NULL;
42 }
43
44 return k;
45 }
1 /**
2 * kobject_add_dir - add sub directory of object.
3 * @parent: object in which a directory is created.
4 * @name: directory name.
5 *
6 * Add a plain directory object as child of given object.
7 */
8 struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
9 {
10 /* [cgw]: 添加一个kobj对象子目录
11 * kobj->kset为空
12 */
13 return kobject_kset_add_dir(NULL, parent, name);
14 }
3 * @parent: object in which a directory is created.
4 * @name: directory name.
5 *
6 * Add a plain directory object as child of given object.
7 */
8 struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
9 {
10 /* [cgw]: 添加一个kobj对象子目录
11 * kobj->kset为空
12 */
13 return kobject_kset_add_dir(NULL, parent, name);
14 }
1 /**
2 * kset_init - initialize a kset for use
3 * @k: kset
4 */
5
6 void kset_init(struct kset * k)
7 {
8 /* [cgw]: 初始化kset成员kobj对象 */
9 kobject_init(&k->kobj);
10 /* [cgw]: 初始化kset成员list链表 */
11 INIT_LIST_HEAD(&k->list);
12 /* [cgw]: 初始化保护kset成员list链表的自旋锁 */
13 spin_lock_init(&k->list_lock);
14 }
3 * @k: kset
4 */
5
6 void kset_init(struct kset * k)
7 {
8 /* [cgw]: 初始化kset成员kobj对象 */
9 kobject_init(&k->kobj);
10 /* [cgw]: 初始化kset成员list链表 */
11 INIT_LIST_HEAD(&k->list);
12 /* [cgw]: 初始化保护kset成员list链表的自旋锁 */
13 spin_lock_init(&k->list_lock);
14 }
1 /**
2 * kset_add - add a kset object to the hierarchy.
3 * @k: kset.
4 */
5
6 int kset_add(struct kset * k)
7 {
8 /* [cgw]: 添加kset成员kobj对象
9 *
10 */
11 return kobject_add(&k->kobj);
12 }
3 * @k: kset.
4 */
5
6 int kset_add(struct kset * k)
7 {
8 /* [cgw]: 添加kset成员kobj对象
9 *
10 */
11 return kobject_add(&k->kobj);
12 }
1 /**
2 * kset_register - initialize and add a kset.
3 * @k: kset.
4 */
5
6 int kset_register(struct kset * k)
7 {
8 /* [cgw]: 指针为空 */
9 if (!k)
10 return -EINVAL;
11 /* [cgw]: 初始化并添加kset */
12 kset_init(k);
13 return kset_add(k);
14 }
3 * @k: kset.
4 */
5
6 int kset_register(struct kset * k)
7 {
8 /* [cgw]: 指针为空 */
9 if (!k)
10 return -EINVAL;
11 /* [cgw]: 初始化并添加kset */
12 kset_init(k);
13 return kset_add(k);
14 }
1 /**
2 * kset_unregister - remove a kset.
3 * @k: kset.
4 */
5
6 void kset_unregister(struct kset * k)
7 {
8 /* [cgw]: 指针为空 */
9 if (!k)
10 return;
11 /* [cgw]: 删除kset,实际是删除kset->kobj */
12 kobject_unregister(&k->kobj);
13 }
3 * @k: kset.
4 */
5
6 void kset_unregister(struct kset * k)
7 {
8 /* [cgw]: 指针为空 */
9 if (!k)
10 return;
11 /* [cgw]: 删除kset,实际是删除kset->kobj */
12 kobject_unregister(&k->kobj);
13 }
1 /**
2 * kset_find_obj - search for object in kset.
3 * @kset: kset we're looking in.
4 * @name: object's name.
5 *
6 * Lock kset via @kset->subsys, and iterate over @kset->list,
7 * looking for a matching kobject. If matching object is found
8 * take a reference and return the object.
9 */
10
11 struct kobject * kset_find_obj(struct kset * kset, const char * name)
12 {
13 struct list_head * entry;
14 struct kobject * ret = NULL;
15
16 /* [cgw]: 进入临界区 */
17 spin_lock(&kset->list_lock);
18 /* [cgw]: 以kset->list为头节点开始轮询kset->list链表中
19 * 每个节点,取出存到entry
20 */
21 list_for_each(entry,&kset->list) {
22 /* [cgw]: 取出一个entry,根据这个entry,找到对应kobj
23 */
24 struct kobject * k = to_kobj(entry);
25 /* [cgw]: 找到对应的kobj,这个kobj的名字是否是要找的kobj的名字匹配 */
26 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
27 /* [cgw]: 找到了这个kobj,引用计数+1 */
28 ret = kobject_get(k);
29 break;
30 }
31 }
32 /* [cgw]: 退出临界区 */
33 spin_unlock(&kset->list_lock);
34 return ret;
35 }
3 * @kset: kset we're looking in.
4 * @name: object's name.
5 *
6 * Lock kset via @kset->subsys, and iterate over @kset->list,
7 * looking for a matching kobject. If matching object is found
8 * take a reference and return the object.
9 */
10
11 struct kobject * kset_find_obj(struct kset * kset, const char * name)
12 {
13 struct list_head * entry;
14 struct kobject * ret = NULL;
15
16 /* [cgw]: 进入临界区 */
17 spin_lock(&kset->list_lock);
18 /* [cgw]: 以kset->list为头节点开始轮询kset->list链表中
19 * 每个节点,取出存到entry
20 */
21 list_for_each(entry,&kset->list) {
22 /* [cgw]: 取出一个entry,根据这个entry,找到对应kobj
23 */
24 struct kobject * k = to_kobj(entry);
25 /* [cgw]: 找到对应的kobj,这个kobj的名字是否是要找的kobj的名字匹配 */
26 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
27 /* [cgw]: 找到了这个kobj,引用计数+1 */
28 ret = kobject_get(k);
29 break;
30 }
31 }
32 /* [cgw]: 退出临界区 */
33 spin_unlock(&kset->list_lock);
34 return ret;
35 }
1 void subsystem_init(struct kset *s)
2 {
3 /* [cgw]: 初始化kset,每一个 kset都属于一个子系统
4 * Every kset must belong to a subsystem. The subsystem
5 * membership helps establish the kset's position in the hierarchy
6 * <Linux Device Drivers>
7 */
8 kset_init(s);
9 }
10
11 int subsystem_register(struct kset *s)
12 {
13 /* [cgw]: 注册kset */
14 return kset_register(s);
15 }
16
17 void subsystem_unregister(struct kset *s)
18 {
19 /* [cgw]: 注销kset */
20 kset_unregister(s);
21 }
22
23 /**
24 * subsystem_create_file - export sysfs attribute file.
25 * @s: subsystem.
26 * @a: subsystem attribute descriptor.
27 */
28
29 int subsys_create_file(struct kset *s, struct subsys_attribute *a)
30 {
31 int error = 0;
32
33 if (!s || !a)
34 return -EINVAL;
35 /* [cgw]: 这个操作最终导致,kset->kobj引用计数+1
36 * 并返回ket指针
37 */
38 if (subsys_get(s)) {
39 /* [cgw]: 为kset->kobj这个对象创建一个属性文件 */
40 error = sysfs_create_file(&s->kobj, &a->attr);
41 /* [cgw]: 这个操作最终导致,kset->kobj引用计数-1
42 * 并返回kset指针
43 */
44 subsys_put(s);
45 }
46 return error;
47 }
48
49 EXPORT_SYMBOL(kobject_init);
50 EXPORT_SYMBOL(kobject_register);
51 EXPORT_SYMBOL(kobject_unregister);
52 EXPORT_SYMBOL(kobject_get);
53 EXPORT_SYMBOL(kobject_put);
54 EXPORT_SYMBOL(kobject_add);
55 EXPORT_SYMBOL(kobject_del);
56
57 EXPORT_SYMBOL(kset_register);
58 EXPORT_SYMBOL(kset_unregister);
59
60 EXPORT_SYMBOL(subsystem_register);
61 EXPORT_SYMBOL(subsystem_unregister);
62 EXPORT_SYMBOL(subsys_create_file);
3 /* [cgw]: 初始化kset,每一个 kset都属于一个子系统
4 * Every kset must belong to a subsystem. The subsystem
5 * membership helps establish the kset's position in the hierarchy
6 * <Linux Device Drivers>
7 */
8 kset_init(s);
9 }
10
11 int subsystem_register(struct kset *s)
12 {
13 /* [cgw]: 注册kset */
14 return kset_register(s);
15 }
16
17 void subsystem_unregister(struct kset *s)
18 {
19 /* [cgw]: 注销kset */
20 kset_unregister(s);
21 }
22
23 /**
24 * subsystem_create_file - export sysfs attribute file.
25 * @s: subsystem.
26 * @a: subsystem attribute descriptor.
27 */
28
29 int subsys_create_file(struct kset *s, struct subsys_attribute *a)
30 {
31 int error = 0;
32
33 if (!s || !a)
34 return -EINVAL;
35 /* [cgw]: 这个操作最终导致,kset->kobj引用计数+1
36 * 并返回ket指针
37 */
38 if (subsys_get(s)) {
39 /* [cgw]: 为kset->kobj这个对象创建一个属性文件 */
40 error = sysfs_create_file(&s->kobj, &a->attr);
41 /* [cgw]: 这个操作最终导致,kset->kobj引用计数-1
42 * 并返回kset指针
43 */
44 subsys_put(s);
45 }
46 return error;
47 }
48
49 EXPORT_SYMBOL(kobject_init);
50 EXPORT_SYMBOL(kobject_register);
51 EXPORT_SYMBOL(kobject_unregister);
52 EXPORT_SYMBOL(kobject_get);
53 EXPORT_SYMBOL(kobject_put);
54 EXPORT_SYMBOL(kobject_add);
55 EXPORT_SYMBOL(kobject_del);
56
57 EXPORT_SYMBOL(kset_register);
58 EXPORT_SYMBOL(kset_unregister);
59
60 EXPORT_SYMBOL(subsystem_register);
61 EXPORT_SYMBOL(subsystem_unregister);
62 EXPORT_SYMBOL(subsys_create_file);