kobject.c 添加注释
2016-07-23 12:36
369 查看
最近结合《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.
*
*/
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;
}
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 */
error = sysfs_create_dir(kobj, shadow_parent);
if (!error) {
/* [cgw]: 添加kobj属性*/
if ((error = populate_dir(kobj)))
/* [cgw]: 添加失败,删除kobj对象目录 */
sysfs_remove_dir(kobj);
}
}
return error;
}
1 static inline struct kobject * to_kobj(struct list_head * entry)
{
/* [cgw]: 从链表list_head取出一个entry
* 根据这个entry,找到包含这个entry的
* kobject结构体地址
*/
return container_of(entry,struct kobject,entry);
}
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 */
/* [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;
}
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));
/* 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);
}
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)
{
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);
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;
/* [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;
}
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添加属性
*/
return kobject_shadow_add(kobj, NULL);
}
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对象 */
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;
}
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 */
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);
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;
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;
}
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)
{
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;
}
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;
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;
}
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)
return;
/* [cgw]: 从sysfs目录移除kobj对象,清0 kobj->dentry节点 */
sysfs_remove_dir(kobj);
/* [cgw]: 删除kobj->dentry节点 */
unlink(kobj);
}
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;
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);
}
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)
/* [cgw]: 引用计数+1 */
kref_get(&kobj->kref);
return kobj;
}
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);
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);
}
1 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));
}
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]: 指针不为空 */
if (kobj)
/* [cgw]: 引用计数-1 ,kobj->kref为0时,调用kobject_release */
kref_put(&kobj->kref, kobject_release);
}
1 static void dir_release(struct kobject *kobj)
{
/* [cgw]: 释放kobj内存空间 */
kfree(kobj);
}
1 static struct kobj_type dir_ktype = {
.release = dir_release,
.sysfs_ops = NULL,
.default_attrs = NULL,
};
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,
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;
}
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 {
/* [cgw]: 添加一个kobj对象子目录
* kobj->kset为空
*/
return kobject_kset_add_dir(NULL, parent, name);
}
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);
/* [cgw]: 初始化kset成员list链表 */
INIT_LIST_HEAD(&k->list);
/* [cgw]: 初始化保护kset成员list链表的自旋锁 */
spin_lock_init(&k->list_lock);
}
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 *
*/
return kobject_add(&k->kobj);
}
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)
return -EINVAL;
/* [cgw]: 初始化并添加kset */
kset_init(k);
return kset_add(k);
}
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)
return;
/* [cgw]: 删除kset,实际是删除kset->kobj */
kobject_unregister(&k->kobj);
}
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 */
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;
}
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 }
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);
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.
*
*/
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;
}
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 */
error = sysfs_create_dir(kobj, shadow_parent);
if (!error) {
/* [cgw]: 添加kobj属性*/
if ((error = populate_dir(kobj)))
/* [cgw]: 添加失败,删除kobj对象目录 */
sysfs_remove_dir(kobj);
}
}
return error;
}
1 static inline struct kobject * to_kobj(struct list_head * entry)
{
/* [cgw]: 从链表list_head取出一个entry
* 根据这个entry,找到包含这个entry的
* kobject结构体地址
*/
return container_of(entry,struct kobject,entry);
}
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 */
/* [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;
}
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));
/* 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);
}
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)
{
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);
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;
/* [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;
}
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添加属性
*/
return kobject_shadow_add(kobj, NULL);
}
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对象 */
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;
}
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 */
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);
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;
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;
}
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)
{
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;
}
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;
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;
}
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)
return;
/* [cgw]: 从sysfs目录移除kobj对象,清0 kobj->dentry节点 */
sysfs_remove_dir(kobj);
/* [cgw]: 删除kobj->dentry节点 */
unlink(kobj);
}
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;
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);
}
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)
/* [cgw]: 引用计数+1 */
kref_get(&kobj->kref);
return kobj;
}
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);
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);
}
1 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));
}
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]: 指针不为空 */
if (kobj)
/* [cgw]: 引用计数-1 ,kobj->kref为0时,调用kobject_release */
kref_put(&kobj->kref, kobject_release);
}
1 static void dir_release(struct kobject *kobj)
{
/* [cgw]: 释放kobj内存空间 */
kfree(kobj);
}
1 static struct kobj_type dir_ktype = {
.release = dir_release,
.sysfs_ops = NULL,
.default_attrs = NULL,
};
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,
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;
}
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 {
/* [cgw]: 添加一个kobj对象子目录
* kobj->kset为空
*/
return kobject_kset_add_dir(NULL, parent, name);
}
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);
/* [cgw]: 初始化kset成员list链表 */
INIT_LIST_HEAD(&k->list);
/* [cgw]: 初始化保护kset成员list链表的自旋锁 */
spin_lock_init(&k->list_lock);
}
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 *
*/
return kobject_add(&k->kobj);
}
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)
return -EINVAL;
/* [cgw]: 初始化并添加kset */
kset_init(k);
return kset_add(k);
}
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)
return;
/* [cgw]: 删除kset,实际是删除kset->kobj */
kobject_unregister(&k->kobj);
}
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 */
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;
}
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 }
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);
相关文章推荐
- Manage Space for schema object
- Java知识点系列:java.lang.Object
- java JSON学习—JSONObject与JSONArray的使用
- ObjectAnimator里面的动画属性值汇总
- 48.The job t gather optimizer statistics for objects runs as part of the automatic maintenance windo
- index( [object] ) object为可选的 string/element/jquery类型
- JSONObject和XML和java等之间的转换
- iOS开发之Objective-c的SHA224/SHA256/SHA384/SHA512加密算法的实现
- 【CV论文阅读】:Rich feature hierarchies for accurate object detection and semantic segmentation
- java.io.EOFException: No content to map to Object due to end of input
- nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
- objective-c 中随机数的用法 (3种:arc4random() 、random()、CCRANDOM_0_1() )
- java.lang.IllegalStateException: attempt to re-open an already-closed object
- Tobject 类解析
- 在MVC控制器里面使用dynamic和ExpandoObject,实现数据转义的输出
- Spark算子:RDD行动Action操作(5)–saveAsTextFile、saveAsSequenceFile、saveAsObjectFile
- Objective-C-UI控件学习之UICollectionViewFlowLayout
- Objections vs. excuses
- Objective-C学习之判断字符串是qq,银行卡,手机号等等公用的方法
- java实现map和object互转的三种方法(未测试)