Linux Ext2 File System (Inode&block) detailed

Source: Internet
Author: User

mentioned above: one of the most important tasks of Linux system administrators is to manage their own disk file systems, each partition is not too large or too small, too much to cause the disk capacity wasted, too small will cause the resulting file can not be stored problems. In Linux, the files are made up of two pieces of data, one part is metadata and the other part is data. So where does this data reside in the file system? This makes it necessary to understand the basic principles of the file system's Inode and block, and Linux's most traditional disk file system uses EXT2, so we understand its internals.


Part I: composition and partitioning of disks (Basic)

Mechanical part of the disk:

1. Round Platters (part of the main record data)

2. The head on the mechanical arm and the robotic arm (data on the disc can be read and written)

3, spindle motor, you can rotate the disc, so that the mechanical arm of the head on the disk to read and write data

Basic concepts of disk

1, the sector is the smallest physical storage unit (512bytes)

2, the sector to form a garden, that is the cylinder, the cylinder is the smallest unit of partition

3, the first sector is the most important, there are hard disk main boot program (MBR) accounted for 446bytes and partition table (partision tables) accounted for 64bytes.

4, the current popular domestic hard disk interface SATA and server hard disk interface SAS.

Disk partition section:

1, primary partition and extended partition can have up to 4 (hard disk limit)

2. The extended partition can have only one (operating system limit)

3. The logical partition is a partition differentiated by the extended partition

4. The contents of primary partition and logical partition can be formatted, and the extended partition cannot be formatted.


Part II: Basic features of file systems

We all know that the hard disk partition after the need to format, and then the operating system to use the partition, why? This is because the properties and permissions of the various operating system files are not the same, in order to be able to store these files, the partition is formatted to become the system format that the operating system can use.

The filesystem typically places two pieces of data in separate blocks, with permissions and attributes placed in the inode, the actual data is placed in the block of the DataSet, and a super block (Superblock) that records the entire file system's overall information, including the total inode and block , usage, amount remaining, and file system format and information. The Inode records the properties of the file, a file occupies an inode, and the block number where the file's data resides is recorded, and the block will actually record the contents of the file, and if the file is too large, it will occupy more than one block.


Part III: EXT2 file system for Linux

1. Data block

The data block is where the content of the file is placed, and the block size supported under the Ext2 file system is 1KB, 2KB, and 4KB. At the time of formatting, the block size is determined, and each block is numbered to facilitate inode recording.

In principle, the size and number of blocks in the format can no longer be changed (unless reformatted), each block can only place a single file of data, there are two cases, one is more than 1 blocks of files will occupy more than block, the other is the file is less than block, The remaining space on the block will no longer be used (wasted disk space).

Well, since the big block above can cause serious disk capacity wastage, the block is set to a minimum of 1K? Then for large files will occupy a larger number of blocks, and the inode to record the number of blocks more than the time when the file system will be bad read and write performance.

Therefore, before the system is formatted, in order to think of a good file system of the expected use, the current prevalence of 4K.


2. Inode

The main content of the inode is the attributes of the record file and the actual data of the file that is placed in the block. The Inode record file data has the following contents:

A, the access mode of the file (r/w/x)

B, owner and group of the file (Owner/group)

C, the size of the file

D, timestamp of the file (Atime, CTime, Mtime)

E, defining flags for file attributes

F, pointing to the true content of the file (pointer)

In fact, the number of inode and size in the format of the time has been fixed, in addition to the above other features are as follows:

Each inode has a size of 128bytes, and each file occupies only one inode, so 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 the permissions recorded by the inode against the user, and if so, continue to read the contents of the block.


Part IV:structure of the Inode


First, the inode needs to record a lot of data, but only 128bytes, and Inode record a block number will be spent 4bytes, an inode if you do not record other data, the maximum can only record 32 block numbers. If the size of a file is 4GB and each block is 4KB, then at least 1 million block number, then according to the above-mentioned inode corresponding to 32 block number, you need at least 32,000 inode, this will require more inode, We do not have so many inode, so our system has the Inode record block number of 12 direct, 1 indirect, 1 double indirect, 1 three indirect record area.


Below we analyze the internal principle through the structure diagram of the inode above.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/85/34/wKiom1ecndXiQ5RjAAF2IIfGDzY936.png "title=" 44.png "alt=" Wkiom1ecndxiq5rjaaf2iifgdzy936.png "/>

The left side of the graph is the inode itself (128bytes), which has 12 directly pointing to the block number control, 12 records can be directly obtained block number. As far as we say indirect is to take a block of memory to place as the record block number record area, if the file is large, the system will use indirect. Similarly, if the file is larger, you can use double indirect, and three indirectly. The so-called double indirect first block then points to the next record number of the block where it is actually recorded in the second block. And so on three indirectly is the use of the third block to record numbers, the first two blocks are placed in the record area of the content.

If the size of the 1 block is 1KB, let's see how many blocks can be recorded in each layer.

12 Direct points: 12x1k=12k (direct connection, only 12 blocks can be recorded)

Indirect connection: 256x1k=256k (the record for each block number will take 4bytes, so 1 blocks will record 256 records)

Double indirect: 256X256X1K=64MB (first block will specify 256 second level, each second layer can also specify 256 numbers, will correspond to 256 blocks)

Three indirect: 256X256X256X1K=16GB (the first block will specify 256 second layer, each second layer will also specify 256 third layer, each third layer can specify 256 numbers)

So the above four ways to add together about equal to 16GB, at this time we know the file system block format 1K size, can accommodate the largest file for 16GB, because the Ext2 file system itself limits the size of block, so the size of the block 2K and 4K, this is not introduced, That's the same thing.


Part V: Experimental Operation view current file system

[[email protected] ~]# df  filesystem      1k-blocks    used available use% mounted on/ dev/sda2      103081248 3759536  94078832   4%  /tmpfs             953648      228    953420   1% /dev/shm/dev/sda1          194241   39059    144942   22% /boot/dev/sda3       20027260   44992   18958268   1% /testdir/dev/sr0          3824484 3824484         0 100% /media/centos_6.8_ Final 
[[email protected] ~]# dumpe2fs /dev/sda1dumpe2fs 1.41.12  (17-May-2010) filesystem volume name:   <none>last mounted on:           /boot   (last mount point) filesystem uuid:           439d1457-04f7-4069-b3ac-04e77c29543cFilesystem magic  number:  0xef53filesystem revision #:    1  (dynamic) Filesystem  features:      has_journal ext_attr resize_inode dir_index  filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg  dir_nlink extra_isizeFilesystem flags:          signed_directory_hash default mount options:    user_xattr  Aclfilesystem state:         clean    (file system is not) errors behavior:           Continue Filesystem OS type:        LinuxInode count:               51200   (total number of inode) block count:               204800  (total block) reserved block count:      10240Free blocks:               155182     (how many blocks are available) free inodes:               51161      (number of inode available)   first  block:              1
block size:               1024         (size of each block is 1024 bytes) fragment size:             1024Reserved GDT blocks:       256Blocks per group:         8192Fragments  per group:      8192inodes per group:          2048inode blocks per group:   256flex block  group size:    16Filesystem created:        Mon Jul 25 01:29:21 2016Last mount time:           Fri Jul 29 23:48:48 2016Last write time:           fri jul 29 23:48:48 2016 
Mount count:              7maximum  mount count:      -1Last checked:              Mon Jul 25 01:29:21 2016Check  interval:           0  (<none>) Lifetime  Writes:          48 mbreserved blocks uid:       0  (user root) reserved blocks gid:       0  (group root) first inode:               11Inode size:           128           (128 bytes per inode) journal inode:             8default directory hash:   half_md4directory  Hash seed:      e0fcf3ec-d918-4914-8e8b-34b7fa31b7f6journal backup:            inode blocksjournal features:           (None) journal size:              4096kJournal length:            4096Journal sequence:          0x0000002bjournal start:            1

This article is a simple introduction to the inode and block of the Linux Ext2 file system, including the Super block, the Block table, the Inode table, and the relationship with the directory tree, we first understand its basic principles and help us to learn the next.

This article is from the "Linux [email protected] Xueshuai" blog, be sure to keep this source http://xueshuai.blog.51cto.com/11578473/1832162

Linux Ext2 File System (Inode&block) detailed

Related Article

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.