tags: Little chat [Linux kernel] linux linux kernel C language sysfs Pseudo file system
sysfsIt is a pseudo file system. Does not represent real physical devices, in the Linux kernel,sysfsThe file system will exist for a long time in memory.sysfsUsed to model specific kernel objects (e.g., physical devices) and provide a method associated with device and device driver. use
ls -l /sys
Commands can be viewedsysfsWhich kernel objects have been exported in the file system. As shown below:

It can be seen from the above figure,sysfsBlock, Bus, Class, Dev, Devices, Firmware, FS, Kernel, Module, and Power kernel objects (different kernel configurations are different).
From the kernel source code. HerebusThe kernel object is an example, the kernel source will bebuses_init()Call in the functionkset_create_and_add()The kernel function creates a BUS object collection and adds it to the SYSFS file system.
Developers can usesysfsTo determine useful information about the running kernel, such as: The kernel finds which devices are found on each bus (BUS), and which drivers that are bound each device can be known from the SYSFS file system. In addition, SYSFS can also be used for debugging, optimizing devices, and other kernel subsystems.
Current block (block) subsystem usessysfsTo mount the root partition. If SYSFS is disabled, you must specify the boot device in the kernel boot command line. For example, for / dev / hda1 devices, the command can beroot=03:01。
For designers of embedded systems, according to actual use scenarios, can be disabledsysfsFile system to save system resources.
First, the Linux kernelsysfsThe source code of the file system is located in the (/ fs / sysfs) directory. As shown below (the source code has been compiled):

fromMakefileThe file can be seen that 5 source files will be compiled during the compilation process:file.o dir.o symlink.o mount.o group.o
This section does not analyzesysfsThe internal implementation details of the file system, mainly analyzes the following two questions:
(1)sysfsHow does the file system mounted in the Linux kernel?
(2) How to add the kernel object set to how the Linux kernelsysfsFile system?
sysfsHow to mount the file system in the Linux kernelexiststart_kernel()The function will be calledvfs_caches_init(totalram_pages);The function is initialized by the virtual file system, and this function will be called.mnt_init()Function, inmnt_init()The function will be calledsysfs_init()rightsysfsThe file system is mounted. The following picture:

code segment:
void __init mnt_init(void)
{
//...
kernfs_init();
err = sysfs_init();
if (err)
printk(KERN_WARNING "%s: sysfs_init error: %d\n",
__func__, err);
//....
}
(/fs/sysfs/mount.c)
static struct file_system_type sysfs_fs_type = {
.name = "sysfs",
.mount = sysfs_mount,
.kill_sb = sysfs_kill_sb,
.fs_flags = FS_USERNS_VISIBLE | FS_USERNS_MOUNT,
};
int __init sysfs_init(void)
{
int err;
sysfs_root = kernfs_create_root(NULL, KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK,
NULL);
if (IS_ERR(sysfs_root))
return PTR_ERR(sysfs_root);
sysfs_root_kn = sysfs_root->kn;
err = register_filesystem(&sysfs_fs_type);
if (err) {
kernfs_destroy_root(sysfs_root);
return err;
}
return 0;
}
The above code first-line code, defines the type of SYSFS file system description, specified.name、.mount、.kill_sb、.fs_flags。
existsysfs_init()Call in the functionkernfs_create_root()(kernfsIt is a universal kernel virtual file system, which is more complex this article not to do excessive analysis) created Kernfs_Root. Then callregister_filesystem()Register to kernelsysfsFile system.
sysfsFile system?The above writes, the Linux kernel is called.kset_create_and_add()The kernel function creates a kernel object set to add the kernel object set to the SYSFS file system, this section will look at the function.
This function has the following important function call relationships:

It can be seen from the above figure,kset_create_and_add()The function will last callsysfs_create_dir_ns()This function. Thereby create a SYSFS file system directory. The code snippet is as follows:
(/fs/sysfs/dir.c)
int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
{
struct kernfs_node *parent, *kn;
BUG_ON(!kobj);
if (kobj->parent)
parent = kobj->parent->sd;
else
parent = sysfs_root_kn;
if (!parent)
return -ENOENT;
kn = kernfs_create_dir_ns(parent, kobject_name(kobj),
S_IRWXU | S_IRUGO | S_IXUGO, kobj, ns);
if (IS_ERR(kn)) {
if (PTR_ERR(kn) == -EEXIST)
sysfs_warn_dup(parent, kobject_name(kobj));
return PTR_ERR(kn);
}
kobj->sd = kn;
return 0;
}
This article describes the starting point from Linux kernel code and the actual system operation.sysfsSome functions of the file system, and analyze how the Linux kernel initializes and mountsysfsFile system, and how the INUX kernel adds the kernel object set tosysfsDocument system.
Due to the limited number of small knowledge and energy, if there is any place to share the article, please see a lot of criticism, Haha!
Foreword IOCTL: Register a virtual character device file, use the Read/WRITE/IOCTL and other interfaces on this virtual device to interact with the user But this method has several obvious disadvantag...
Linux kernel file system Overview The term file system has different meanings in different contexts: Refers to a specific file format. For example, the file system of Linux isExt2, MSDOS file system i...
Article Directory File system abstraction layer The main data structure of VFS Superblock object inode object Directory entry object file object File system related data structures File system type Mo...
Nuclear start -> Initialization -> Establish process 0 -> Start file descriptor 0, 1, 2 (standard input file \ standard output file \ standard error output file) -> Function process start_...
Copyright statement: This article is an original article of bloggers. Following the CC 4.0 BY-SA copyright agreement, please attach the original link and this statement. Links in this article: https:/...
The Linux kernel design is basically based on the idea of "everything is a file". Therefore, all the system calls or module functions we can see need to use the file system. If there is no...
I don’t write the previous ones, start the analysis from the kernel source code do_mount Set the parameters of mnt_flag through the flags parameter passed by this function, and then clear some f...
DevTMPFS file system analysis Articles directory DevTMPFS file system analysis 1. Open Chapter Introduction to DEVTMPFS Third, DevTMPFS kernel source analysis (2-1) DEVTMPFS initialization (2-2) The c...
Table of contents 1. PROC file system 1. PROC content 2. advantages and disadvantages 2. Data from specific processes Third, general system information Fourth, network information 5. System control in...
How to use the area on a block device, and Kernel managing a free area on the block device Entity file How to name and manage files How user program manipulating files and directories 1....