LINUX-EXT2 File System

Source: Internet
Author: User
Tags what file system

File system Features

For example, the main file system used by Windows 98 's previous Microsoft operating system is FAT (or FAT16), and later versions of Windows 2000 have the so-called NTFS file system, as for Linux's Orthodox file system Ext2 (Linux second E xtended file System, EXT2FS) this one. In addition, the Windows operating system will not recognize Linux Ext2 by default.

So how does the file system work? This is related to the operating system's file data. The newer operating system's file data, in addition to the actual contents of the file, usually contains a lot of attributes, such as the Linux operating system file permissions (RWX) and file attributes (owner, group, time parameters, etc.). The file system typically stores the two parts of the data in separate chunks, with permissions and attributes placed in the inode, and the actual data is placed in the block. In addition, there is a super block (Superblock) that records the overall information of the entire file system, including the total number of inode and block, usage, surplus, etc.

Each inode and block is numbered, and the meaning of these three data can be described briefly as follows:

Superblock: Record The overall information of this filesystem, including the total amount of inode/block, the amount of use, the amount of surplus, and the format and related information of the file system;
Inode: Records the properties of a file, a file occupies an inode, while recording the block number of the file's data;
Block: The actual record of the contents of the file, if the file is too large, will occupy more than block.

We use the Inode and block block plots to illustrate, as shown, the file system first format the Inode and block block, assuming that a file's attributes and permissions data is placed in the inode 4th (smaller squares), and this inode records the actual placement of file data points for 2, 7, 13, 15 of these four block numbers, at this time our operating system can arrange the reading order of the disk, can be a breath of four block content read out! Then the reading of the data is like the arrows specified in the shape.


This method of data access is called an indexed file system (indexed allocation). Is there any other idiomatic file system that can be compared? Yes, that's our usual flash drive (flash), the file system used in the flash drive is generally in FAT format. The FAT file system does not exist in this format, so fat has no way to read all blocks of the file from the beginning. Each block number is recorded in the previous block, and he reads a bit like this:



We assume that the data in the file is written sequentially to the four block numbers of the 1->7->4->15 number, but this filesystem has no way of knowing the number of four blocks in one breath, and he will have to read the block in a single time before he knows where the next block is. If the block that writes the same file data is too fragmented, our disk read head will not be able to read all the data on the disk, so the disk will be more than a few laps to complete the contents of this file!


Data Block (Chunk)

The data block is used to place the contents of the file, the block size supported in the Ext2 file system is 1 K, 2K and 4K three kinds. The size of the block is fixed at the time of formatting, and each block is numbered to facilitate the inode record. However, it is important to note that due to the block size difference, the maximum disk capacity that the file system can support is not the same as the maximum single file capacity. The Ext2 file system limitations resulting from block size are as follows:


What are the limitations of the Ext2 file system block? Some! The basic restrictions are as follows:

In principle, the size and number of blocks cannot be changed after formatting (unless reformatted);
A maximum of one file can be placed within each block;
, if the file is larger than the block size, then a file will occupy multiple block numbers;
If the file is less than block, the remaining capacity of the block cannot be reused (disk space is wasted).


Inode table (inode table)

Let's talk about the inode thing again! As mentioned above, the contents of the inode are in the log file properties and the actual data of the file is placed in which number of blocks inside! Basically, the file data for the Inode record has at least the following:

The file's access mode (Read/write/excute);
The owner and group of the file (Owner/group);
The capacity of the file;
The time the file was created or changed in status (CTime);
Last read time (atime);
Time of Last modification (mtime);
Defines flags (flag) for file attributes, such as SetUID ... ;
The point of the real content of the file (pointer);


The number and size of the inode is also fixed at the time of formatting, in addition to what are the inode features?

Each inode size is fixed to bytes;
Each file will only occupy one inode;
The number of files the file system can create is related to the number of inode;
When the system reads the file, it needs to find the inode and analyze whether the permissions recorded by the inode are consistent with the user, and if so, it can begin to actually read the contents of the block.

Let's take a rough look at the relationship between Inode/block and file size. Inode to record a lot of data, but only 128bytes, and Inode record a block number to spend 4byte, if I have a file with 400MB and each block is 4K, then at least 100,000 block number records It! What's the inode with so much information to record? For this reason, our system is smart. Defines the area of the Inode record block number as 12 direct, one indirect, one double indirect with a three indirect record area. What is this? Let's draw the structure of the inode.



On the left is the Inode itself (bytes), there are 12 directly to the block number of the control, these 12 records can be directly obtained block number! As for the so-called indirect is to take a block to record the block number record area, if the file is too large, you will use the indirect block to record the number. For example, the 1.3.2 indirectly just takes a block to record the extra number. Similarly, if the file continues to grow, then the so-called double-indirect, the first block will only point out where the next record number of blocks, the actual record in the second block. And so on, three indirectly is to use the third block to record the number!

Superblock (Super Block)

Superblock is a place to record information about the entire filesystem, there is no superblock, there is no such filesystem. The main information he has recorded are:

Block and Inode Total;
Number of unused and used inode/block;
Block and Inode size (block is 1, 2, 4k,inode is bytes);
Information about the file system such as the mount time of the filesystem, the last time the data was written, the time of the last Test disk (fsck), and so on;
A valid bit value, if the file system is mounted, the valid bit is 0, and if not mounted, the valid bit is 1.


[Email protected] ~]#DUMPE2FS [-BH] Appliance file nameOptions and Parameters:-B: Lists the parts that are reserved for bad tracks (generally not used!?) )-H: Only superblock data is listed and no other section content is listed! Example: Find my root Disk file name and observe information about the file system[Email protected] ~]#DF   <== This command can call out the device that is currently mounted.Filesystem 1k-blocks used Available use% mounted on/dev/hdc2 9920624 3822848 5585708 41%/        <== is the light! /DEV/HDC3 4956316 141376 4559108 4%/home/dev/hdc1 101086 11126 84741 12%/BOOTTMPFS 371332 0 371332 0%/dev/shm[[email protected] ~]#DUMPE2FS/DEV/HDC2DUMPE2FS 1.39 (29-may-2006)Filesystem Volume Name:/1             <== This is the name of the file system (Label)Filesystem features:has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_fileDefault Mount Options:user_xattr ACL <== parameters that are mounted by defaultFilesystem State:clean          <== This file system is no problem (clean)Errors Behavior:continuefilesystem OS Type:linuxinode count:2560864total number of <==inodeBlock count:2560359total number of <==blockFree blocks:1524760<== How many blocks are available?Free inodes:2411225<== How many more inode are availableFirst block:0Block size:4096           <== the size of each block! Filesystem Created:fri Sep 5 01:49:20 2008Last mount Time:mon Sep 12:09:30 2008Last Write time: Mon Sep 12:09:30 2008Last checked:fri Sep 5 01:49:20 2008First inode:11Inode size:128            <== the size of each inodeJournal Inode:8Under <==, these three are related to the next section .Journal Backup:inode blocksjournal size:128mgroup 0: (Blocks 0-32767)<== First Data group content, containing the start/end number of the blockPrimary superblock at 0, Group descriptors at 1-1<== Super Block at block No. No. 0Reserved GDT blocks at 2-626 Block bitmap @ 627 (+627), Inode bitmap at 628 (+628) inode table at 629-1641 (+629)The block where the <==inode table resides0 Free blocks, 32405 free inodes, 2 directories<== all blocks are exhausted! Free Blocks:free inodes:12-32416<== remaining unused inode numbersGroup 1: (Blocks 32768-65535).... (omitted below) .... # because the volume of data is very large, so some information omitted output! # The first half of the content in the show Supberblock, including the header name (label) and information about Inode/block # is followed by individual information for each block group! You can see the number of each segment data! In other words, basically all the data is related to the block's number! It's important! 

Directory

When we create a directory in the Ext2 file system under Linux, Ext2 allocates an inode with at least one block to the directory. The Inode records the permissions and attributes of the directory, and records the block number assigned to it, while the block is the inode number data that is used to record the file name in this directory and the filename. That is, the block content occupied by the directory is logged as follows:



ls-liTotal654683 -rw-------1 root root  1474 Sep  4 18:27 anaconda-ks.cfg 648322- rw-r--r--1 root root 42304 Sep  4 18:26 install.log648323 -rw-r--r--1 root root  5661 Sep  4 18:25 ins Tall.log.syslog

[Email protected] ~]#ll-d//bin/boot/proc/lost+found/sbinDrwxr-xr-x root root 4096 Sep 22 12:09/<== a 4K blockDrwxr-xr-x 2 root root 4096 Sep 00:07/bin<== a 4K blockDrwxr-xr-x 4 root root 18:06/boot Sep 4<== a 1K blockdrwx------2 root root 16384 Sep 5 01:49/lost+found<== four x 4K blockDr-xr-xr-x-root root 0 Sep 20:07/proc<== This directory does not occupy hard disk spaceDrwxr-xr-x 2 root root 12288 Sep 5 12:33/sbin<== three x 4K block

File:

When we create a generic file in Ext2 under Linux, Ext2 assigns an inode to the file with the number of blocks relative to the file size. For example: Suppose one of my blocks is 4 Kbytes, and I want to create a Kbytes file, then Linux will allocate an inode with 25 block to store the file! Note, however, that since the inode has only 12 direct points, there is one more block to record as the block number.


Directory tree reads:

Since the directory tree starts reading from the root directory, the system can find the inode number of the mount point via the mounted information (usually a filesystem's topmost inode number starts with number 2nd!) ), the inode content of the root directory can be obtained, and the file name data within the block of the root directory is read according to the inode, and then read down to the correct file name one layer at a time.

[Email protected] ~]#Ll-di//etc/etc/passwd2Drwxr-xR-xRoot root 4096 Sep 22 12:09/1912545Drwxr-xR-xRoot root 12288 Oct 04:02/etc1914888-rw-r--r--1 root root 1945 Sep 02:21/etc/passwd


/The Inode:
Through the information of the mount point to find the/DEV/HDC2 inode number 2 of the root inode, and the Inode specification permission allows us to read the contents of the block (with R and X);

/BLOCK:
After the last step, get the block number and find the Inode number that contains the etc/directory (1912545);

The inode of the etc/:
Read number 1,912,545th Inode learned that Vbird has R and X permissions, so you can read the etc/block content;

Block of etc/:
After the last step, obtain the block number and find the inode number (1914888) that contains the passwd file.

The inode of the passwd:
Read number 1,914,888th Inode learned that Vbird has R permission, so can read passwd block content;

Block of passwd:
Finally, the data of the block content is read out.


EXT2/EXT3 file access and logging file system functionality

The last section is only about reading, so what happens to our Ext2 when we create a new file or directory? This time will have to block bitmap and Inode bitmap help! Suppose we want to add a file, the behavior of the file system is this:

1. First determine whether the user wants to add files to the directory has W and x permissions, if any, can be added;
2. Locate the unused inode number according to the Inode bitmap, and write the permissions/attributes of the new file;
3. Locate the block number that is not in use according to block bitmap and write the actual data to block, and the block that upgrades the inode points to the data;
4. Synchronize the inode with the block data just written to upgrade the Inode bitmap with the block bitmap, and upgrade the contents of the Superblock.
In general, we refer to Inode table and data block as the Datastore area, and other segments such as Superblock, Block bitmap, and inode bitmap are called metadata (intermediary data) because SUP Erblock, Inode bitmap and block bitmap data is constantly changing, each new, removed, edited can affect the three parts of the data, it is called the intermediary data.


journaled File System (journaling filesystem)

In order to avoid the inconsistency of the file system mentioned above, our predecessors thought of a way that if a chunk is planned in our filesystem, which is dedicated to the steps of writing or revising a file, wouldn't it be possible to simplify the process of consistency checking? Other words:

1. Preparation: When the system is going to write a file, it will first record a file in the log record block to prepare the information to be written;
2. Actual write: Start writing permissions and data to the file; start upgrading metadata data;
3. End: After completing the upgrade of the data and metadata, complete the record of the file in the log record block.
In such a program, in case of data record process problems, then our system as long as to check the log record block, we can know which file has a problem, for the problem to do a consistent check, instead of the whole block filesystem to check, so that can achieve rapid repair FileSystem's ability! This is the most basic feature of log-in files.


Operation of the Linux file system:

To solve this efficiency problem, the way we use Linux is through a way called asynchronous processing (asynchronously). The so-called asynchronous processing is this:

When the system loads a file into memory, if the file has not been altered, the file data in the memory section is configured to be clean. However, if the in-memory file data has been changed (for example, if you have edited the file with Nano), the data in this memory will be configured as Dirty (Dirty). At this point all the actions are still running in memory and are not written to disk! Data that is configured in memory as "Dirty" is written back to disk at irregular intervals to maintain disk-to-memory data consistency. You can also manually force the disk to be written using the Sync command mentioned in chapter fifth.

We know that the memory is much faster than the hard disk, so if we can put the commonly used files in memory, this will not add system performance? That's right! There is such an idea! Therefore, our Linux system above the file system and memory has a very large relationship:

The system will put the common file data into the main memory buffer to speed up the file system read/write;
, so Linux's physical memory will eventually be exhausted! That's the normal situation! Can accelerate the system performance;
You can manually use sync to force files in memory that are configured as Dirty to be written back to disk;
The shutdown command will actively call sync to write the memory data back to disk if the shutdown is normal;
However, if you do not shut down properly (such as a jump, a crash, or other unexplained reason), the data has not yet been written back to disk, so it may take a lot of time to perform a disk test after restarting, and may even cause file system damage (non-disk damage).


Other Linux supported file systems and VFS while Linux's standard file system is ext2, and there are ext3 that add logging, in fact, Linux also supports many file system formats, especially in recent years, which have introduced several fast-speed journaled file systems , including SGI's XFS file system, Reiserfs file system for smaller files, and the FAT file system of Windows, etc., can be supported by Linux! The common supporting file systems are:

Traditional file system: Ext2/minix/ms-dos/fat (with VFAT module)/iso9660 (CD-ROM) and so on;
journaled file system: Ext3/reiserfs/windows ' NTFS/IBM ' s Jfs/sgi's XFS
Network File system: NFS/SMBFS

Now that we know the file system we're using, we're going to mention, so how does the core of Linux manage these known file systems? In fact, the entire Linux system is read Filesystem through a core function called Virtual Filesystem Switch. In other words, the entire Linux filesystem is actually the VFS in the management, our users do not need to know each partition above the filesystem is what ~ VFS will actively help us do a good job of reading it ~

Suppose your/use is/dev/hda1, with ext3, and/home use/DEV/HDA2, with ReiserFS, then you take/HOME/DMTSAI/.BASHRC, there is specifically to use what file system module to read it? It should be no! This is the function of VFS! Through this VFS function to manage all the filesystem, save us the need to configure the definition of the read file system is very convenient! The entire VFS can be used to illustrate:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

LINUX-EXT2 File System

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.