[Linux] sysfs file system

[Linux] sysfs file system

foreword

 1. sysfs file system

1.1 What is the sysfs system

1.2 The operating mechanism of sysfs

Two, important structures in sysfs

2.1 The relationship between kobject and kset 

2.2 kobject structure

2.3 kset structure 


 foreword

The important link: keep moving forward with problems

  1. What is the sysfs file system
  2. The principle of the sysf file system
  3. An important structure in the sysfs file system.

In the course of learning Linux notes teacher, the teacher's lectures are concise and in-depth, but some knowledge background is needed to get started. Here I have expanded and summarized. ---Study Notes

Reference articles and videos: https://xuesong.blog.csdn.net/article/details/109522945

https://xuesong.blog.csdn.net/article/details/107724682

https://live.csdn.net/v/279734?spm=1001.2014.3001.5501

 1. sysfs file system

1.1 What is the sysfs system

sysfs is a virtual file system that provides a way to access kernel data structures allowing user space programs to view and control system devices and resources.

The sysfs file system is mounted under the sys file directory. We can clearly understand the system status of the embedded device of the Linux system through the directories and files under sys. sysfs organizes the devices and buses connected to the system into a hierarchical file, which can be accessed by user space, and exports the kernel data structure Q and their attributes to user space.

One of the purposes of sysfs is to display the hierarchical relationship of components in the device driver model, as shown in the following figure:

In the sys directory, the functions of each file are as follows:

1.2 The operating mechanism of sysfs

sysfs provides a mechanism that makes it possible to explicitly describe kernel objects, object attributes, and relationships between objects.

  • A set of interfaces to the kernel for mapping devices into the file system.
  • A set of interfaces for user programs to read or manipulate these devices. Describes the sysfs elements in the kernel and their performance in user space, as shown in the following figure:

The directories and files under sys reflect the system status of the entire machine. These catalogs represent completely different device types, and these catalogs just give us a different perspective on how to look at the entire device model. The real device information is placed in the devices subdirectory, and all devices in the Linux system can be found in this directory. As shown in the figure above, the bus corresponds to drivers and devices, and there are different classifications of devices under classes, which also correspond to various devices. In fact, they are all symbolic links of device files in the devices directory.

Two, important structures in sysfs

2.1 The relationship between kobject and kset 

From the figure below, it is very clear to know the relationship and connection between kset and kobject.

2.2 kobject structure

The kobject is the core of the device model and runs in the background. It brings an object-oriented style of programming to the kernel.

The kobject structure is defined in include/linux/kobject.h in the kernel, and some member variables are included in the kobject structure, among which name, parent, sd, kref and ktype are more important. The specific structure code is as follows:

/*
name:	指向这个kobject的名称。使用kobject_set_name(struct kobject* kobj,const char * name)函数可以修改它。
entry: 简单讲就是要挂载入kset的链表。
parent:	指向此kobject父项的指针。它用于构建描述对象之间关系的层次结构。
sd:		指向struct sysfs dirent结构,它表示该结构内sysfs节点中的这个kobject。
kref:	提供kobject上的引用计数。
ktype: 	描述该对象,kset说明这个对象属于哪套(组)对象。
*/
struct kobject {
	const char		*name;
	struct list_head	entry;
	struct kobject		*parent;
	struct kset		*kset;
	struct kobj_type	*ktype;
	struct kernfs_node	*sd; /* sysfs directory entry */
	struct kref		kref;
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
	struct delayed_work	release;
#endif
	unsigned int state_initialized:1;   //初始化状态
	unsigned int state_in_sysfs:1;      //是否在sys中  
	unsigned int state_add_uevent_sent:1;
	unsigned int state_remove_uevent_sent:1;
	unsigned int uevent_suppress:1;
};

Often use kobject_create_and_add to create and add directories. Before adding to the system, a kobject must be allocated using the kobject_create() function, with the allocated but not yet initialized kobject pointer and its kobject_type pointer as parameters. The kobject_add() function is used to add a kobject and link it to the system, while creating a directory with its default attributes according to its hierarchy. The function with the opposite function is kobject_del(), which deletes the link of kobject.

struct kobject *kobject_create(void)
{
	struct kobject *kobj;

	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
	if (!kobj)
		return NULL;

	kobject_init(kobj, &dynamic_kobj_ktype);
	return kobj;
}

struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
{
	struct kobject *kobj;
	int retval;

	kobj = kobject_create();
	if (!kobj)
		return NULL;

	retval = kobject_add(kobj, parent, "%s", name);
	if (retval) {
		printk(KERN_WARNING "%s: kobject_add error: %d\n",
		       __func__, retval);
		kobject_put(kobj);
		kobj = NULL;
	}
	return kobj;
}

2.3 kset structure 

The kernel object set (kset) mainly combines related kernel objects together, and kset is a collection of objects. 

The kset structure also has one more linked list header than the kobject, and the kobject structure instance corresponding to the subdirectory item can be added to the linked list, so that kset can traverse the kobjects of these subdirectory items. 

struct kset {
	struct list_head list;    //挂载kobject结构的链表
	spinlock_t list_lock;     //保护链表访问的自旋锁
	struct kobject kobj;      //自身包含一个kobject结构  
	const struct kset_uevent_ops *uevent_ops;
} __randomize_layout;

Guess you like

Origin blog.csdn.net/weixin_42373086/article/details/130780463