Understanding of the Linux file system (two)

In the previous section, we have basically learned the general attributes of files. This section mainly introduces the structure of VFS: The structure of VFS mainly includes the representation of files and file system
files: inode is the kernel choice to represent file content and related metadata The method abstracts the access to the underlying file system:
1: Inode operation, creating links, renaming files, generating new files in the directory, and deleting files
2: File operations, which mainly affect the data content of files. File read and write operations and create memory map.
The operation of the corresponding inode can provide information about the file system characteristics and current status. Open files are always allocated to a specific process, so the kernel must store the association between the file and the process in the data structure, and use the file descriptor as an index when accessing.
File system and super block information:
Super block structure: store file system related information, for disk file systems, this object usually corresponds to a file system control block on the disk, including all modified inodes in the related file system .
The inode data structure contains the attributes of each file operation, such as: i_size represents the length of the file; i_block represents the length of the file according to the block, each VFS inode has a unique identifier stored in i_ino, i_count is a record process accessing the structure The number, i_nlink is the number of hardware links.
i_rdev is needed when inode represents a device file. It represents communication with that device process.
Inode represents a block device. i_pipe contains the inode information used to implement the pipeline. Inode
operation: the kernel provides a large number of functions to operate on the inode. There are mainly two pointers. (I_op and i_fop) point to functions that implement the above abstraction.
i_op is related to a specific inode operation. i_fop provides file operation
inodes that can be stored in a linked list, so there are three states of inodes:
The inode is stored in the memory and is not associated with any file, and there is no action state. The
inode is in the memory and is being used by a file or multiple processes, usually a file. The file content and inode information is consistent with the information on the block device. The file has been mounted .
The inode structure is stored in memory and used by one or more processes, and its data content is inconsistent with the storage medium. At this time, it is dirty data
process-specific information: the file descriptor is used to open a file with a unique identifier in a process. The kernel can establish an association between the descriptors in the user process and the structures used inside the kernel. The file data of the process is stored in fs, and the number of files that can be opened is defined by NR_OPEN_DEFAULT. The
file operation mainly includes the following points:
file_operatuons contains function pointers to all possible file operations. The pointer structure mainly contains the following operations:
1: read and write are respectively responsible for read and write operations; its parameters involve file descriptors, buffers and offsets. Another parameter is the number of reads and writes.
2: The content of the file is mapped to the virtual address space of the process, and the file can be easily accessed through mmap.
3: ioctl is used to communicate with hardware devices, so it can only be used for device files.
The VFS namespace is a collection of all file systems that have been mounted to form a container's directory tree. At the same time, this member of nsproxy is responsible for the processing of the namespace.
Directory item cache: Linux uses the directory item cache (dentry) to quickly access the results of previous lookup operations. Dentry not only makes it easy to handle the file system, but is also critical to improving system performance. It speeds up the communication with the underlying file system through minimal words. VFS processing. The organization of dentry objects in memory mainly involves two parts:
1: a hash table contains all dentry objects
2: an LRU linked list, in which objects that are no longer used will be granted a final grace period,
The kernel has several standard functions for assisting dentry and simplifying the operation of dentry objects. Its realization is mainly an exercise in linked list management and data structure processing.
The various data structures described above are the basis for the work of the VFS layer. Now we will deal with VFS objects (file system operations). Let's start with the system calls used by the standard library to communicate with the kernel. When the kernel is compiled, you can set whether to support a certain file system, so all of a file system needs to be registered in the kernel in advance.
1: Register the file system register_filesystem is used to register the file system
2: Loading and unloading need to add objects to the linked list, initiated by the mount system call, each mounted file system corresponds to an instance of the vfsmount structure
3: Super block management loading new File system vfsmount is not the only structure that needs to be created in the kernel, but needs to start from the super block read. There is an s_root in the data structure of the super block to detect whether the file system has been mounted. If it is NULL, the file is a pseudo file system.
delete_inode deletes the inode from the memory and the underlying media, and removes the pointer to the relevant data block, but the file data is not affected. When an inode is no longer used, vfs internally calls clear_inode, which releases all relevant memory pages containing data.
The process of the mount system call is as follows:
sys_mount->copy mount option->do_mount->path_lookup->call the corresponding load function according to the flag .
do_new_mount (do_kernel_mount and do_add_mount)
The task of do_kernel_mount is to use get_fs_type to find the matching file instance. This function scans the linked list to find whether the linked list of the registered file system returns the correct value, and automatically loads the corresponding module if it is not found.
do_add_mount handles some necessary lock operations.
Pseudo file system: The
file system does not necessarily need the support of the underlying block device. Ramfs and tmpfs are stored in memory; procfs and sysfs do not require backup storage. The pseudo file system is a file system that cannot be mounted, so it cannot be directly used in the user layer. see. It mainly includes (bdev responsible for representing the inode of the block device, pipefs for processing pipes, and socket for processing sockets). The
MS_NOUSER flag is a flag that mount allows files to be mounted.
File operation:
1: Find inode (nameidata) Through name and flags, the kernel uses the path_lookup function to find the path or file name.
2: Open the file
3: Read and write
three parameters: file descriptor, buffer area for saving data, length of the number of characters read
1: Asynchronous read, if the O_DIRECT flag is set, read directly, not used Page cache.
2: Read the file from the mapping. Through the mapping mechanism, the part of the file that needs to be read is mapped to the memory page. Continue writing to the memory through the big loop until all files are transferred to the memory.
In the next section, we will focus on the content related to EXT2 and EXT3, so stay tuned! !

Guess you like

Origin blog.51cto.com/12768454/2547409