This article describes the file system in Linux. The source code is from the IA32-based 2.4.20 kernel. In general, the file system in Linux can be divided into three main parts: one is the system call of the Upper-layer file system, and the other is the Virtual File System VFS (Virtual Filesystem Switch ), third, the actual file systems attached to VFS, such as ext2 and jffs. This article focuses on explaining the internal mechanism of VFS in Linux kernel through code analysis. In this process, it will involve upper-level file system calls and how to mount the lower-level actual file system. This article attempts to explain the VFS file system mechanism in Linux from a relatively high point of view.
1. Summary
This article describes the file system in Linux. The source code is from the IA32-based 2.4.20 kernel. In general, the file system in Linux can be divided into three main parts: one is the system call of the Upper-layer file system, and the other is the Virtual File System VFS (Virtual Filesystem Switch ), third, the actual file systems attached to VFS, such as ext2 and jffs. This article focuses on explaining the internal mechanism of VFS in Linux kernel through code analysis. In this process, it will involve upper-level file system calls and how to mount the lower-level actual file system. This article attempts to explain the VFS file system mechanism in Linux from a relatively high point of view. Therefore, the description focuses more on the main context of the entire module, rather than the details, there are also several illustrations to help readers understand.
The VFS code is relatively cumbersome and complex. I hope that you will have a clear understanding of the overall VFS Operating Mechanism in Linux after reading this article. Before reading this article, we recommend that you read the source code of the file system to create the most basic concepts of the file system in Linux. For example, you should be familiar with super block, dentry, and inode at least, the meaning of data structures such as vfsmount, so that you can read this article for better understanding.
2. Overview of VFS
VFS is a software mechanism. It may be called a Linux File System Administrator. The data structure associated with it only exists in the physical memory. Therefore, during each system initialization, Linux must first construct a VFS directory tree in the memory (namespace in the Linux source code ), in fact, it is to establish the corresponding data structure in the memory. The VFS directory tree is an important concept in the file system module of Linux. I hope that you do not confuse it with the actual file system directory tree. In my opinion, the directories in VFS are mainly used to provide mount points of the actual file system. Of course, file-level operations are also involved in VFS. This article does not describe this situation. The directory tree or directory is mentioned below. Unless otherwise specified, it refers to the directory tree or directory of VFS. Figure 1 is a possible image of the directory tree in memory:
Figure 1: VFS directory tree structure
3. File System Registration
The file system here refers to the actual file systems that may be mounted to the directory tree. The so-called actual file system means that the actual operations in VFS will eventually be completed through them, it does not mean that they must exist on a specific storage device. For example, I have registered more than a dozen file systems under my Linux machine, including "rootfs", "proc", "ext2", and "sockfs.
3.1 Data Structure
In Linux source code, each actual file system is represented by the following data structure: struct file_system_type {const char * name; int fs_flags; struct super_block * (* read_super) (struct super_block *, void *, int); struct module * owner; struct file_system_type * next; struct list_head fs_supers ;}; |
The registration process will actually instantiate the struct file_system_type data structure of each actual file system, and then form a linked list. In the kernel, a global variable named file_systems is used to point to the table header of the linked list.
3.2 register the rootfs File System
In many actual file systems, the reason for introducing the registration process of the rootfs File System separately is that the VFS of the file system is too closely related, if ext2/ext3 is a local Linux file system, then the rootfs file system is the basis for the existence of VFS. Generally, the registration of file systems is completed through the module_init macro and the do_initcils () function (readers can read the module_init macro statement and arch \ i386 \ vmlinux. the lds file to understand this process), but the registration of rootfs is completed through the init_rootfs () initialization function, this means that the registration process of rootfs is an integral part of the Linux kernel initialization phase.
Init_rootfs () registers the rootfs File System by calling the register_filesystem (& rootfs_fs_type) function. rootfs_fs_type is defined as follows:
struct file_system_type rootfs_fs_type = { name: "rootfs", read_super: ramfs_read_super, fs_flags: FS_NOMOUNT|FS_LITTER, owner: THIS_MODULE, } |
The structure of the registered file_systems linked list is shown in Figure 2:
Figure 2: file_systems linked list Structure