Relationship between sysfs file system and kobject

Source: Internet
Author: User

Sysfs File System
 
The Linux kernel introduces the sysfs file system. sysfs can be seen as a file system similar to proc, devfs, and devpty. This file system is a virtual file system that allows you to manage system devices more conveniently. It can generate a hardware hierarchical view of all systems, which is similar to the proc file system that provides process and status information.

Sysfs organizes the devices and bus connected to the system into hierarchical files, which can be accessed by the user space and exported to the user space the kernel data structure and their attributes. One purpose of sysfs is to display the hierarchy of each component in the device driver model. Its top-level directories include block, bus, drivers, class, power, and firmware.

Wugang @ wugang-desktop :~ $ Ls/sys // runtime environment ubuntu 8.04 (2.6.16)
Block bus class devices firmware fs kernel module power slab
 
It organizes devices and bus actually connected to the system into a hierarchical file. user space programs can also use this information to achieve interaction with the kernel, this file system is an intuitive response to the actual device tree in the current system. It establishes this information through the kobject subsystem. When a kobject is created, the corresponding files and directories are created in the corresponding directory under/sys. Since each device has a unique directory in sysfs, it can be read and written by the user space.

 
You may not have been concerned about the mounting process of the sysfs file system. It is mounted like this.
Mount-t sysfs/sys
Sysfs is a special file system that does not have any media to actually store files. The source of sysfs information is the kobject hierarchy. to read a sysfs file, it dynamically extracts information from the kobject structure and generates a file. After the restart, the information is gone.

 
The sysfs file system is closely related to the kobject structure. Each kobject object registered in the kernel corresponds to a directory in the sysfs file system. Kobject is a new device management mechanism introduced in Linux 2.6. It is represented by struct kobject in the kernel. Through this data structure, all devices have a unified interface at the underlying layer. kobject provides basic object management, which forms the core structure of the Linux2.6 device model. Kobject is the basic structure of the device model. Similar to the base class in C ++, it is embedded in a larger object and used to describe the components of the device model. Such as bus, devices, and drivers. All are connected through kobject to form a tree structure. The tree structure corresponds to the/sys direction.

Note: here the kobject structure is not introduced, http://blog.chinaunix.net/u1/55599/showart.php? Id = 1086478.

 
Sysfs is a file system established by using the VFS interface to read and write kobject hierarchies. The register and deregister XX_register () of the kobject hierarchy. A file system is a vague and extensive concept. linux regards all resources as files, allowing users to call the same group of systems through a unified file system operation interface, perform operations on files belonging to different file systems. In this way, the Implementation Details of different file systems can be hidden in user programs, providing a unified, abstract, and virtual file system interface for user programs, this is called "VFS (Virtual Filesystem Switch )". This abstract interface is a set of function operations.

To implement a file system, we need to implement a series of interfaces defined by VFS, such as file_operations, dentry_operations, and inode_operations for upper-layer calls. File_operations describes the operation methods (such as read and write) on each specific file. The dentry_operations struct specifies the operation methods for all VFS directories, while inode_operations provides the operation methods for all nodes.

For example, we write a C program, open ("hello. c", O_RDONLY). The process of calling the program through the system is as follows:
Open ()->/* user space */
-> System Call->/* Through System Call, program process Kernel Status */
Sys_open ()-> filp_open ()-> dentry_open ()-> file_operations-> open ()/* kernel space */

 
Different file systems call different file_operations-> open (), which is sysfs_open_file () under sysfs ().
We use different file systems to abstract their respective file information to dentry and inode. In this way, we can not care about the underlying implementation, we use a series of standard function calls. This is the essence of VFS, and is actually object-oriented.

Note that sysfs is a typical special file. The information it stores is dynamically generated by the system. It dynamically contains the hardware resources of the entire machine. Reading and Writing from sysfs is equivalent to extracting data from the kobject hierarchy.

 
The following is an example:
When we add a new kobject structure, a directory is created under/sys.
Kobject_add ()-> create_dir ()-> sysfs_create_dir ()
 
135 int sysfs_create_dir (struct kobject * kobj)
136 {
137 struct dentry * dentry = NULL;
138 struct dentry * parent;
139 int error = 0;
140
141 bug_on (! Kobj );
142
143 If (kobj-> parent)
144 parent = kobj-> parent-> dentry;
145 else if (sysfs_mount & sysfs_mount-> mnt_sb)
146 parent = sysfs_mount-> mnt_sb-> s_root;
147 else
148 return-EFAULT;
149
150 error = create_dir (kobj, parent, kobject_name (kobj), & dentry );
151 if (! Error)
152 kobj-> dentry = dentry;
153 return error;
154}
143-148 is to find the parent's kobject and then call create_dir ();
 
95 static int create_dir (struct kobject * k, struct dentry * p,
96 const char * n, struct dentry *** d)
97 {
98 int error;
99 umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
100
101 down (& p-> d_inode-> I _sem );
102 * D = sysfs_get_dentry (p, N );
103 If (! Is_err (* D )){
104 error = sysfs_create (* D, mode, init_dir );
105 if (! Error ){
106 error = sysfs_make_dirent (p-> d_fsdata, * D, K, mode,

107 SYSFS_DIR );
108 if (! Error ){
109 p-> d_inode-> I _nlink ++;
110 (* d)-> d_op = & sysfs_dentry_ops;
111 d_rehash (* d );
112}
113}
114 if (error & (error! =-EEXIST ))
115 d_drop (* d );
116 dput (* d );
117} else
118 error = PTR_ERR (* d );
119 up (& p-> d_inode-> I _sem );
120 return error;
121}
* In line 99, set the 'file' attribute to 101 to obtain the semaphore.
 
(1) sysfs_get_dentry ()
Row 3: sysfs_get_dentry (). It obtains the dentry structure based on the parent dentry and file name. First, find it in the cache. If it is found, it will return. If it cannot be found, use d_alloc () to create a new dentry structure.

 
204 static struct dentry * sysfs_lookup (struct inode * dir, struct dentry * dentry,

205 struct nameidata * nd)
206 {
207 struct sysfs_dirent * parent_sd = dentry-> d_parent-> d_fsdata;
208 struct sysfs_dirent * sd;
209 int err = 0;
210
211 list_for_each_entry (sd, & parent_sd-> s_children, s_sibling ){
212 if (sd-> s_type & SYSFS_NOT_PINNED ){
213 const unsigned char * name = sysfs_get_name (sd );
214
215 if (strcmp (name, dentry-> d_name.name ))
216 continue;
217
218 if (sd-> s_type & SYSFS_KOBJ_LINK)
219 err = sysfs_attach_link (sd, dentry );
220 else
221 err = sysfs_attach_attr (SD, dentry );
222 break;
223}
224}
225
226 return ERR_PTR (err );
227}
 
The role of the lookup function has been discussed earlier. It finds out whether there is a file named dentry. d_name.name in the folder represented by inode. If yes, read the corresponding inode structure from the information carrier. If no, null is returned.

# Define SYSFS_NOT_PINNED \
(SYSFS_KOBJ_ATTR | SYSFS_KOBJ_BIN_ATTR | SYSFS_KOBJ_LINK)
However, the lookup of sysfs is different. Other file systems are like inode of common files in ext3 format. They are created at the time of file creation. However, sysfs is different. When creating a common file, it only creates a sysfs_dirent structure. Inode creation is postponed to the lookup function.

Sysfs_attach_attr () and sysfs_attach_link () are used to create an inode Based on dentry and sysfs_dirent.
(2) sysfs_create () analysis (row 104)
Sysfs_create ()-> sysfs_new_inode (mode)-> new_inode (sysfs_sb)
Create a new index node inode. Sysfs_sb is the super block structure of sysfs. Mode is the attribute of inode, which records the following information, such as the file type (folder, link, or common file), inode owner, and creation time.

(3) sysfs make dirent () analysis (Row 1)
Now we have a dirent structure, initialized, and connected it to the s_children linked list of sysfs_dirent in the upper directory. Sysfs_make_dirent () creates a dirent structure for the newly created dentry. And associate dentry with dirent.

 
Note: I have not thoroughly analyzed the case code, so you can analyze it yourself. For specific code, see sysfs. h. Understand the specific call process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.