Linux Virtual file System VFS

Source: Internet
Author: User

Linux can mount different file systems (EXT2,FAT,NTFS), with the same style presented to the user, read and write operations are the same, how is this done?

The Linux kernel makes an abstraction layer in a variety of file system formats, making the concepts of files, directories, read-write access, and so on, an abstraction layer, so that the various file systems look the same, and this abstraction layer is called the virtual file system (VFS).

Each process in the PCB (process Control Block) has a File descriptor table, the file descriptor is the index of the tables, each table item has a pointer to the open file, and now we are clear: open files in the kernel with the file structure, The pointer in the file descriptor table points to the file structure body.

Maintains the file Status Flag (member f_flags of the file struct) and the current read and write location in the file structure (member F_pos of the file struct). In, Process 1 and Process 2 both open the same file, but correspond to different file structure bodies, so you can have different file Status flags and read and write locations. The more important members of the file struct are the F_count, which represents the reference count (Reference count), which we'll talk about, which will cause multiple file descriptors to point to the same document struct, such as DUP, fork, and so on. For example, FD1 and FD2 both refer to the same file struct, then its reference count is 2, when close (FD1) does not release the file structure, but only the reference count to 1, if close (FD2), the reference count will be reduced to 0 while releasing the file structure, This really closed the file.

Each file struct points to a file_operations struct, the members of which are function pointers to kernel functions that implement various file operations. For example, in the user program read a file descriptor, read through the system call into the kernel, and then find the file descriptor pointed to the document structure, to find the structure of the file_operations, which is pointed to the body, Invokes the kernel function pointed to by its read member to complete the user request. In the user program calls Lseek, read, write, IOCTL, open and other functions, and ultimately by the kernel calls file_operations the members of the kernel function point to complete the user request. The release member in the File_operations struct is used to complete the close request of the user program, which is called release instead of close because it does not necessarily close the file, but reduces the reference count, and only the reference count is reduced to 0 to close the file. For regular files that are open on the same file system, the steps and methods for file operations such as read, write, and so on, should be the same, and the calling function should be the same, so the file structure of the three open files in the diagram points to the same file_operations struct. If you open a character device file, then its read, write operation must be different from the regular file, not read and write disk data block, but read and write hardware devices, so the file structure should point to the various file_operations structure body, The various file manipulation functions are implemented by the driver of the device.

Each file struct has a pointer to the dentry struct, and "Dentry" is an abbreviation for the directory entry (directory entry). The arguments we pass to the open, stat, and other functions are a path, such as/home/akaedu/a, where the inode of the file needs to be found based on the path. to reduce the number of reads, the kernel caches the tree structure of the directory, called the Dentry cache, where each point is a dentry struct, as long as the dentry is searched along the path parts, from the root directory/to the home directory, and then to the akaedu directory , and then locate file a. Dentry cache saves only recently accessed directory entries, and if the directory entry you are looking for is not in the cache, it will be read from disk to memory.

Each dentry struct has a pointer pointing to the INODE structure. The INODE structure holds the information read from the disk inode. In the example, there are two dentry, representing/home/akaedu/a and/home/akaedu/b, each pointing to the same inode, stating that the two files are mutually hard links. The inode structure holds information that is read from the inode in the partition, such as owner, file size, file type, and permission bit. Each inode struct has a pointer to the inode_operations struct, which is a set of function pointers that point to some kernel functions that complete the file directory operation. Unlike File_operations, inode_operations points to a function that does not operate on a file, but functions that affect the layout of files and directories, such as adding deleted files and directories, tracking symbolic links, and so on. Each inode structure that belongs to the same file system can point to the same inode_operations struct.

The inode struct has a pointer to the Super_block struct. The Super_block structure holds information that is read from a super block on a disk partition, such as file system type, block size, and so on. The S_root member of the Super_block struct is a pointer to Dentry, indicating where the root directory of the filesystem is being mount, in which case the partition is mount to the/home directory.

file , Dentry, inode, super_block These structures constitute the core concept of VFS. for ext2 file systems, there is also the concept of inode and Super block on disk storage layouts, so it is easy to establish correspondence with the concepts in VFS.

Other file system formats from non-UNIX systems (such as Windows FAT32, NTFS), may not have inode or Super block concept, but in order to mount to the Linux system, but also in the driver hard to gather, Looking at FAT32 and NTFS partitions under Linux will find that the permission bits are wrong, and all files are rwxrwxrwx, because they do not have the concept of inode and permission bit, which is hard to come out of.

Transfer from: akaedu textbook

Linux Virtual file System VFS

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.