After the meeting today, I Need To summarize the show and store of sysfs. By the way, I will make a comprehensive summary.
Kobject sysfs
2.6 The kernel adds a compelling new feature-the unified device model. The device model provides an independent mechanism to represent a device and describe its Extension Structure in the system.
1, kobject
The core component of the device model is kobject, which is represented by the struct kobject struct and defined in <Linux/kobject. h>. Kobject provides fields such as reference count, name, and parent pointer. The specific structure is as follows:
Struct kobject {
Const char * Name; // name
Struct list_head entry;
Struct kobject * parent; // parent pointer
Struct kset * kset; // collection of kobject objects
Struct kobj_type * ktype; // a special type associated with a kobject object
Struct sysfs_dirent * SD;
Struct kref; // reference count implemented through this struct
Unsigned int state_initialized: 1;
Unsigned int state_in_sysfs: 1;
Unsigned int state_add_uevent_sent: 1;
Unsigned int state_remove_uevent_sent: 1;
};
The following describes the ktype struct:
Struct kobj_type {
Void (* release) (struct kobject * kobj); // destructor to be called when the reference count of kobject is reduced to 0
Struct sysfs_ops * sysfs_ops; // describes the features of sysfs file read/write.
Struct attribute ** default_attrs; // defines the default attributes related to this kobject.
};
Next let's look at the kset struct:
Struct kset {
Struct list_head list; // connects all the kobject objects in the Set (kset ).
Spinlock_t list_lock;
Struct kobject kobj; // The base class of the Set
Struct kset_uevent_ops * uevent_ops; // uevent-related operations
};
After reading the relevant structures of the main kobject, the following describes the management and operations of kobject:
Void kobject_init (struct kobject * kobj, struct kobj_type * ktype); this function is used to initialize kobject. After initializing and setting the name, you also need to set the kset and ktype fields for it.
Reference count of kobject:
There are two main functions:
Struct kobject * kobject_get (struct kobject * kobj); // Add a reference count
Void kobject_put (struct kobject * kobj);//Reduce reference count
2. sysfs
Sysfs file system is a Virtual File System in memory, which provides us with a kobject object hierarchy view. It helps users observe the extension structure of various devices in the system in a simple file system. With the help of attribute objects, kobject can provide kernel variables for users to read or write data in the form of a bootstrap file.
The trick of sysfs is to closely associate the kobject object with the directory entry, which is implemented through the dentry field in the kobject object. The dentry struct indicates a directory item. By connecting kobject to a specified directory item, you can easily map kobject to this directory. From then on, exporting kobject into a file system is as simple as building directory items in the memory. Well, kobject has actually formed a tree-our beloved object model system. Because kobject is mapped to a directory item, and the object hierarchy has already formed a tree in the memory, sysfs is easy to generate.
Add and delete kobject in sysfs
Int kobject_register ();
Int kobject_unregister ();
In <Linux/sysfs. h>
Struct attribute {
Const char * Name; // attribute name
Struct module * owner; // Module
Mode_t mode; // permission
};
Struct sysfs_ops {
Ssize_t (* Show) (struct kobject *, struct attribute *, char *); // this method is called when reading sysfs files
Ssize_t (* store) (struct kobject *, struct attribute *, const char *, size_t); // this method is called when writing sysfs files.
};
The show () method is called during read operations. It copies the attribute values provided by ATTR to the specified buffer. The buffer size is page_size bytes.
The store () method is called during write operations. It reads the size bytes from the buffer and stores them in the attribute struct variable represented by ATTR. The buffer size is always page_size or smaller.