kobjects
At the heart of the device model is the kobject, which is represented by struct kobject and defined in <linux/kobject.h>. The kobject is similar to the Object class in object-oriented languages such as C# or Java. It provides basic facilities, such as reference counting, a name, and a parent pointer, allowing the creation of a hierarchy of objects.
Without further ado:
struct kobject {
char *k_name;
char name[KOBJ_NAME_LEN];
struct kref kref;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct dentry *dentry;
};
The k_name pointer points to the name of this kobject. If the name is less than KOBJ_NAME_LENcurrently 20bytes, the name is stored in name and k_name points there. If the name is larger than KOBJ_NAME_LEN bytes, a buffer sufficient in size to hold the name is dynamically allocated, the name is stored in the buffer, and k_name points there.
The parent pointer points to this kobject's parent. In this manner, kobjects build an object hierarchy in the kernel and allow the expression of the relationship between multiple objects. As you shall see, this is actually all that sysfs is: a user-space filesystem representation of the kobject object hierarchy inside the kernel.
The dentry pointer points to the dentry structure that represents this kobject in sysfs, assuming that this kobject is represented in sysfs.
The kref, ktype, and kset members point to structures used in support of kobjects. The enTRy member is used in conjunction with kset. Those structures and their use are discussed shortly.
kobjects are usually embedded in other structures and are generally not interesting on their own. Instead, a more important structure, such as struct cdev, has a kobj member:
/* cdev structure - object representing a character device */
struct cdev {
struct kobject kobj;
struct module *owner;
struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
When kobjects are embedded inside other structures, the structures receive the standardized functions that a kobject provides. Most importantly, the structure's embedded kobject now allows the structure to become part of an object hierarchy. For example, the cdev structure is presentable in an object hierarchy via the parent pointer cdev->kobj->parent and the list cdev->kobj->entry.
|