Linux Ext2 file system details, linuxext2 file details
The reason why I want to write the Ext2 file system is because I have been reading Hadoop recently and found that HDFS and Ext2 file systems are very similar. In addition, I have already learned about Ext2 file systems, I plan to sort out the idea of Ext2 first.
1 Ext2 Summary
In Linux, everything is a file. The most important features of Linux Files: file permissions, file attributes, and file data. The file permissions include rwx, file attributes such as the file size and owner, and file data refers to the data stored in the file.
In the Ext2 file system, they are not stored together, but stored separately. The file permissions and attributes are stored in inode, and the file data is stored in the data block, in addition, the Ext2 file system has a super block used to store the overall information of the file system.
The Ext2 file system has planned inode, data block, and super block at the beginning, and will not change unless formatted. Each inode and data block has a unique ID corresponding to it.
2 super block
The super block is used to record the basic information of the entire file system. It mainly records the following content:
The total number of blocks and inode used. The total number of blocks and inode remaining in the inode file system.
Generally, the super block size is 128 bytes.
3 inode
Inode is used to record the attributes of a file. A file occupies one inode and records the data block number that stores the file data in the inode. Inode stores the following information:
1. File Permission rwx
2. File owner and group
3. File Size
4. last modification time
5. data block number for storing file data
The size of an inode is fixed to 128 bytes. When reading a file, you usually first find the inode corresponding to the file, and then perform a series of operations through inode.
4 data block
The actual storage location of file data. A file occupies at least one data block (one data block can only store one file). If the file is too large, it may occupy multiple data blocks, the size of a data Block is usually 4 kb.
Therefore, when dividing data blocks, their size is critical. If the data block is too small, small files may be used for storage. However, for large files, more data blocks will be occupied. inode requires more regions to record data blocks, reducing the reading and writing efficiency of the file system. If the data block is too large, it may be advantageous to store large files, however, for small files, a data block can store up to one file, which may cause space waste.
Relationship Between Files and inode and data block
Assuming the inode corresponding to the file is inode3, we can see that the actual storage location of the file data is data block1, inode is responsible for recording the numbers of these data blocks, this data access method is called an indexed file system.
File Reading Process:
1. Obtain the inode location of the file, read data in the inode, obtain the File Permission, and check whether the user has the r permission. If there is any step, if not, the process ends, the file content is not displayed;
2. The data block number recorded in inode reads the data on the corresponding data block at a time and reads the file.
5 relationship between directories and inode and data block
The directory is also a file, so it is stored in the same way as the file, without stopping the fact that the directory data block stores the location corresponding to the inode of the subdirectory/file.
In fact, every time a file is read, it is performed from the/root directory. In this way, the inode of the target file is finally found, and the location where the file data is stored, read, and obtain data.