Disclaimer: This article is limited to cnblogs release, other third-party websites are pirated, original address: Linux file system Model
In the Linux environment have experienced some students may encounter a problem, the problem is to insert a USB flash drive or other external devices, often do not respond to the Internet to find after others will teach you how to use a few commands and then you can operate the same as normal file operation of these devices (of course, Many popular desktop Linux environments, such as Ubuntu/centos, are now supported for auto-mount.
However, perhaps we have also thought about why Linux is so troublesome, many times I encountered this problem is that Linux is not a normal OS ah, need to be so troublesome and normal (??). ), but it's a little bit of an obsession with something, and I suddenly get my mind on it, so I want to know why it's so troublesome, or why it needs to be. Of course, in this article, you do not have the answer to why so troublesome, I guess is nothing more than the development of the corresponding OS people do not want to do this Feature, essentially can do, like mentioned Ubuntu, but you can learn in this article how to mount a USB stick and other operations is how a principle.
File system Abstraction model for Linux
In Linux, in order to adapt to different formats of the file system (EXT4/NFS), that can support the simultaneous use of different file system files, made a layer of abstraction, that is, the so-called VFS (Virtual File systems), the entire Linux The file hierarchy can be summed up so abstractly:
VFS hides the underlying complex file system details for upper-level applications and processes, which means that the process does not need to know whether the file system is a local hard disk or a USB flash drive or a network file system, while the VFS provides a series of abstract interfaces to the lower layer. This allows upper-layer applications to use the same interface to access devices from different sources. It is important to note that because VFS is an abstract concept, so many of the elements inside are the same as the specific file system name, in this article, if not the specific file system, then the noun (for example, Inode/superblock) is to describe the VFS, rather than the specific file system.
In the VFS, there are four important data structures, each of which are:
- Superblock: A collection of file system advanced metadata containing various data from the file system, stored on disk
- Dentry: Hierarchical management of file systems, dynamically created at run time, only in memory
- I-node: The basic unit of a file system, which can correspond to a file, a directory, or a link, stored on disk
- Each open file in the File:linux corresponds to the file object, which defines the common Files operation in Linux
So when we open some files in the process, the object model in memory should look like this:
- The first layer of blue is our open File object, which is placed in our process table , which is associated with the process.
- The second layer is the directory structure/vfsmount structure, this layer is a tree hierarchy, the previous said is to facilitate us to find the real I-node
- The third layer is the real I-node object, here is the open is this file, but this file is located in the file system what is not known here
- The fourth layer is the last superblock, where we can determine the real type of file system where the file is actually accessed (Ext4/ext3 ... )
Examples of specific file systems: ext2
The so-called specific file system is in our usual use of the process, when the new installation of an OS or a newly added a piece of storage, under normal circumstances are not directly mounted, because your disk does not set the file system, even if mounted, our OS is not recognized. So, what's the special case, and that's what you're imagining, you have two sets of identical machines A and B, one at home, one in other places, you want to use the same environment when using two machines (Os/soft, etc.), then you install the environment on any one machine, And then on the two machines are generic this installed you need the environment of the disk, so that you regularly in two machines to carry the disk, and machine A is working on a good disk directly pulled down to Machine B is also working, do not need you on machine B and reset the file system, Although you might think this example is a little silly, this is a really handy implementation in the cloud environment.
OK, don't say much, take a look at a classic ext2 file system in Linux. In the ext2 file system, this file system manages the disk (which can not be a full disk, such as a disk size of 1 T, here can only give 40G) is divided into a block of equal size, and the size of the block is variable, popular point /info/liuliqiang/da and /info/liuliqiang/db are used ext2 FileSystem, but the block size of da is 4096 byte, while the block size of DB is.
In the Ext2 file system, the first bytes in the disk location is superblock, which says Superblock is the size of the 1024x768 bytes. there is a property that requires our attention, that is, the Magic,ext2 file system and Ext3 file system magic are 0xef53, which shows that ext2 and ext3 compatibility is very good! , there is also a property called Block_size, which is used to represent the size of the block in this ext2 filesystem, which is mutable, as stated earlier.
Ext2 file systems are divided into blocks, So how do we store the files? Then look down, in the Ext2 file system, the different number of blocks are clustered into the so-called block group, each block group will correspond to a block group Descri Ption, these group description will be put together, and the position is immediately behind Superblock .
Each block group contains inode table and block bitmap, and through this inode table, we can get a Inode , and then the inode stores The pointer information of the block , so we get the real disk data. Information such as inode table and block bitmap is placed at the beginning of block group , followed by a piece, forming the so-called Ext2 file system.
The combination of VFS and ext2
After reading the abstract virtual file system and the real file system, it is time to combine the two to look at the unity.
When our system starts to load, it will build a Dentry directory tree, which is not the same as the specific OS tree, the directory tree is created by default only a real Rootfs file system, The other directories in the directory tree are then the mount points that are provided for later mounting of other real file systems. For example, our Ext2 file system is attached to one of the directories and is mounted.
Then open the file, open the file, we established this file fd,fd will correspond to Dentry and VFS I-node, through Dentry we can find the corresponding file system (because it is mounted in Dentry), and then through the VFS I-node we can Obtain the specific contents of the file, thus completing the entire VFS to ext2 real file system conversion.
Summarize
This article from a general point of view describes the Linux, the file operation of the abstract and specific combination, but for the Linux IO, this is only the tip of the iceberg, and I will follow the article in combination with their own thinking and understanding, more to dig some details. Finally, thanks for the help of the following Reference articles, let me know more clearly.
Reference
- Advanced Programming for UNIX environments
- Modern operating System (3rd edition)
- Anatomy of a Linux virtual system file exchanger
- View Linux virtual file system from file I/O
- Parsing the VFS file system mechanism in Linux
- Hard disk layout for EXT2 file system
Linux File System Model