Introduction file system is the most important part of the operating system. it defines the method and data structure for storing files on disks. File systems are an important means for operating systems to organize, access, and store information. each operating system has its own file system. for example, the file systems used by Windows mainly include FAT16...
Introduction
A file system is the most important part of an operating system. it defines the methods and data structures for storing files on a disk. A file system is an important means for the operating system to organize, access, and store information. each operating system has its own file system. for example, the file systems used by Windows mainly include FAT16, FAT32, and NTFS, linux uses the following file systems: ext2, ext3, Ext4, and ReiserFS.
We know that a disk must be partitioned first and then formatted. Otherwise, it cannot be used. this formatting process is the process of creating a file system, A partition on a disk is a file system. This is just like when we use windows, we can format the disk partition into FAT32 or NTFS, which is completely controlled by ourselves, however, the formatted file system must be recognized by the system you use. This is why the NTFS file system cannot be recognized by the Linux system directly. Similarly, windows cannot understand the same principle as EXT3/Ext4.
There is no standard answer to the definition of a file system. if you are more interested in studying the file system, you may find relevant information. This article only gives you a preliminary understanding of file systems.
Understand some keywords related to file systems
Storage media: hard disks, optical disks, floppy disks, Flash disks, tapes, and network storage devices.
Disk partitioning: this is for large-capacity storage devices, mainly refers to the hard disk. for large hard disks, we need to reasonably plan partitions, so we need to talk about hard disk partitioning.
File system creation: this process is used to create a file system for a storage device. it is also known as formatting or initialization. it is implemented through some initialization tools.
Mount: the file system can only be used for mounting. the Linux operating system uses mount. when mounting a file system, a mount point is required. for example, when installing Linux, sometimes we will prompt you for partitioning, then create a file system, and then ask you what the mount point is. During the use of the Linux system, we will also mount other hard disk partitions and select the mount point. The Mount point is usually a vacant Directory, preferably our self-built vacant directory;
The visible geometric structure of the file system: the file system is used to organize and arrange file access, so it is visible. in Linux, we can use tools such as ls to view its structure, in Linux, we see a tree structure. for example, if the operating system is installed in a file system, it is represented by a/initial tree structure.
Introduction to common file system formats in Linux
EXT1: the first file system supported by Linux is the Minix file system. This file system has serious performance problems, so another file system for Linux has emerged, that is, the extended file system. 1st Extended file systems (ext1) were designed by Remy Card and introduced to Linux in April 1992. The ext1 file system is the first file system to be switched using a virtual file system (VFS. Virtual File System switching is implemented in the 0.96c Kernel. The maximum supported file system is 2 GB.
EXT2: 2nd extended file systems (ext2) were also implemented by Remy Card and introduced to Linux in January 1993. It draws on the advanced ideas of the File System (such as Berkeley Fast File System [FFS. Ext2 supports a maximum file system of 2 TB, but the 2.6 kernel increases the maximum capacity supported by this file system to 32 TB.
EXT3: The 3rd extended file system (ext3) is a major improvement for Linux file systems, although it is inferior to some competitors in terms of performance. The ext3 file system introduces the log concept to improve the reliability of the file system when the system suddenly stops. Although some file systems have better performance (such as the XFS and IBM®Journaled File System [JFS]), but ext3 supports local (in-place) upgrades from systems using ext2. Ext3 was implemented by Stephen Tweedie and introduced in November 2001.
Ext4: 2.6.28 kernel is the first stable Ext4 file system. Significant improvements have been made in terms of performance, scalability, and reliability. Most importantly, ext4 supports 1 EB file systems. Ext4 is implemented by a development team led by Theodore Tso (ext3 maintainer) and introduced to the 2.6.19 kernel. Currently, it has been very stable in the 2.6.28 kernel (until December 2008 ).
Ext4 draws on many useful concepts from competitors. For example, you have implemented the partition (extent) in JFS to manage blocks. Another feature (latency allocation) related to block management has been implemented in ZFS of XFS and Sun Microsystems.
In the Ext4 file system, you can find various improvements and innovations. These improvements include new features (new features), scalability (breaking the limits of the current file system), and reliability (responding to faults). of course, they also include performance improvements.
Swap: a swap file system used to swap partitions in Linux. Linux uses this entire partition as the swap space. Generally, the swap partition in this swap format is twice the primary memory. When the memory is insufficient, Linux will write part of the data to the swap partition.
How the file system works
The work of the file system is related to the file data of the operating system. In addition to the actual file content, file data in the operating system usually contains many attributes, such as file permission (rwx) and file attributes (owner, user group, and time parameter ). The file system usually stores these two parts of data in different blocks, permissions and attributes in inode, and data in block blocks. In addition, a super block records the overall information of the entire file system, including the total amount of inode and block, usage, and remaining amount.
Each inode and block is numbered. the meaning of the three data items can be described as follows:
Using superblock: records the overall information of this filesystem, including the total inode/block quantity, usage, remaining amount, and file system format and related information;
Inode: records the attributes of a file. a file occupies one inode and records the block number of the data in this file;
Volume block: records the content of a file. if the file is too large, multiple blocks are occupied.
Because each inode and block has a number, each file occupies one inode, and the inode contains a block number for storing file data. Therefore, we can know that if we can find the inode of the file, we will naturally know the block number of the data stored in the file, of course, the actual data of the file can be read. This is a more efficient method, because our disk can read and retrieve all the data in a short time, and the read and write performance is better.
The inode and block blocks are illustrated. as shown in, the file system first formats the inode and block blocks, assume that the attribute and permission data of an archive are stored in inode 4 (in a small square), and the inode records the actual locations of archive data: 2, 7, 13, at this time, the operating system can sort the reading order of the disk based on the four block numbers. you can read the four blocks in one breath! Then the data is read as specified by the arrow in.
Figure inode/block data access
This method of data access is called indexed allocation ). Next, let's take a look at the FAT in windows. the file system in this format does not have inode, so FAT cannot read all blocks of this file from the beginning. Each block number is recorded in the previous block. its reading method is a bit like below:
Figure FAT file system data access
Assume that the data in the file is written to the four block numbers 1-> 7-> 4-> 15 in sequence, but the file system cannot know the number of the four blocks in one breath, he needs to read the block one by one to know where the next block is. If the blocks written to the same file are too scattered, the read header of the disk cannot read all the data in a circle, therefore, the disk will be rotated several more times to completely read the content of this file.
This is why fragment is often required in windows systems. the reason for fragment is that the block written to the file is too discrete and the file read efficiency will be poor. In this case, the blocks of the same file can be aggregated by means of fragment, which makes data reading easier.
FAT file systems need to fragment from time to time. do Ext need disk sorting?
Because Ext is an indexed file system, there is basically no need to fragment. However, if the file system is used too long and files are often deleted/edited/added, the file data may be too discrete. at this time, it may need to be reorganized. However, it seems that there are not many people doing Linux disk fragmentation!
As mentioned above, inode is used to record the permissions and related attributes of the file, while block is used to record the actual content of the file. In addition, the file system has planned inode and block at the beginning. unless it is re-formatted (or changed the file system size using commands such as resize2fs), the inode and block will not change after they are fixed. However, it would be unwise to put all inodes and blocks together when my document system reaches hundreds of GB, because inode and block are too large to be easily managed. Therefore, the Ext2 file system is basically divided into multiple block groups during formatting. each block group has an independent inode/block/superblock system.
Ext2 file system structure
In general, the format of Ext2 is a bit like the following:
Ext2 file system
In the overall planning, there is a boot sector at the top of the file system, which can install the boot management program. this is a very important design, in this way, we can install different boot management programs to the frontend of individual file systems without overwriting the unique MBR of the entire hard disk, in this way, we can create an environment with multiple guidance! The six main contents of each block group are described as follows:
Data block)
Data block is used to store data in the file content. in the Ext2 file system, the supported block sizes are 1 K, 2 K, and 4 K. During formatting, the block size is fixed, and each block is numbered to facilitate inode record. However, due to the difference in block size, the maximum disk capacity supported by the file system is different from the maximum single file capacity. Ext2 file system restrictions caused by block size are as follows:
You must note that, although Ext2 already supports a single file capacity larger than 2 GB, some applications still use the old limit, that is, some programs can only catch files smaller than 2 GB, which has nothing to do with the file system!
Note:
In principle, the size and number of blocks cannot be changed after being formatted (unless re-formatted );
Each block can contain up to one file;
If the file size is greater than the block size, one file will occupy multiple blocks;
If the file size is smaller than the block size, the remaining capacity of the block cannot be used again (disk space will be wasted );
If the file size is large, more blocks will be occupied, reducing the read/write efficiency of the file system.
Inode table (inode table)
Let's discuss inode! As mentioned above, inode records the attributes of an archive and the block where the actual data of the archive is stored! Basically, inode records at least the following file data:
Specifies the access mode of the file (read/write/excute );
Owners and groups of the file (owner/group );
Capacity of the file;
Specifies the time when the file is created or the status changes (ctime );
The last read time (atime );
Latest latest modification time (mtime );
The identifier (flag) that defines file features, such as SetUID ...;
Specifies the true point of the file content (pointer );
The number and size of inode are fixed during formatting. In addition, inode has the following features:
The size of each inode is fixed to 128 bytes;
Every file occupies only one inode;
The number of files that can be created by the guest file system is related to the number of inode;
When the guest system reads a file, it must first find the inode and analyze whether the permissions recorded by the inode match the user. If yes, the system can actually read the block content.
Superblock)
Superblock records the information related to the entire filesystem. if there is no Superblock, This filesystem will be unavailable. The following information is recorded:
Total volume of blocks and inode;
The number of inode/blocks that are not used and used by the worker;
The size of the primary block and inode (block is 1, 2, 4 K, inode is 128 bytes );
The Mount time of the specified filesystem, the time when data was last written, and the time when the last disk (fsck) was verified;
If the file system has been mounted, the valid bit value is 0. if the file system is not mounted, the valid bit value is 1.
Superblock is very important, because the basic information of our file system is written here. Generally, the superblock size is 1024 bytes.
Filesystem Description)
This section describes the start and end block numbers of each block group, and the block numbers between each segment (superblock, bitmap, inodemap, data block.
Block bitmap (block table)
If you want to add a new file, block will always be used. which block will be used for recording? Of course, an empty block is selected to record the data of the new file. So how do you know which block is empty? This requires assistance from block bitmap. From the block bitmap, we can know which blocks are empty. Therefore, our system can quickly find available spaces to store files. Similarly, if you delete some files, the block numbers occupied by these files must be released, in this case, the logo corresponding to the block number in the block bitmap must be changed to unused. this is the bitmap function.
Inode bitmap (inode table)
In fact, this function is similar to block bitmap, but block bitmap records the use and unused block numbers. inode bitmap records the use and unused inode numbers.
Finally, we will provide an EXT3 file system structure for you to explain in detail the EXT file system.
Note: In this figure, the MBR is not partitioned, but at the beginning of the disk. thanks to ice360, please refer to the comments section.
From Yufei blog