Analysis of Linux file System and index node Inode

Source: Internet
Author: User

We know that the disk needs to be formatted after partitioning, before the operating system can use this partition. The reason for formatting is that the file attributes/permissions set by each operating system are different. In Linux, the traditional disk file system is ext, following ext as an example of Linux file System and index node.

We know that the file's data, in addition to the actual content of the file, often contains very many attributes. The file system typically places data such as permissions and attributes into the Inode (index node), while the actual data is placed in the data block block, and a super block records the overall information for the entire file system.

In the file system, each inode is numbered with a block, and each inode corresponds to a file, and the inode contains the block number where the file data is placed. Then when looking for a file, you can find the file corresponding Inode number, to find the file data placed block number, and then read out the actual contents of the file. The process can be simplified as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/3C/wKioL1edt92xTpKBAABafnqaKg4354.png "title=" Inode file access. PNG "alt=" Wkiol1edt92xtpkbaabafnqakg4354.png "/>

The inode also consumes hard disk space, and the file system begins by planning the inode with block, which is the data area where the file data is stored, the inode is the inode, and the information contained in the inode is stored, unless the file system is reformatted or changed by command. Otherwise, the inode and block are fixed and no longer changes; the size of each inode node is typically the total number of 128byte or 256byte,inode nodes, given when formatting. But if our file system is larger, then the number of inode and block will be very large, if placed together will not be easy to manage. Therefore, the Ext file system is differentiated into multiple block groups when it is formatted, each block group has its own inode/block/superblock system (note: Block groups are divided within a single file system, so the inode within multiple block groups, Block numbers are not the same). The structure of ext system is roughly as follows

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/85/3C/wKiom1eduhChMMOzAABC3WfpJ5Q566.png "title=" capture. PNG "alt=" Wkiom1eduhchmmozaabc3wfpj5q566.png "/>

Data block

Data block is used to place the contents of the file, in the Ext file system supported block size 1KB,2KB and 4KB Three, in the format of the block size is fixed, and each block is numbered to facilitate the inode record.

    • In principle, the size and number of blocks cannot be changed after formatting (unless reformatted)

    • Only one file per block can be placed in the data

    • If the file is larger than the size of the block, a file can occupy multiple block numbers

    • If the file is smaller than block, the block's space cannot be reused (disk space is wasted)

Inodetable

Inodetable is used to place all files within a block group and their corresponding inode. The file data for the Inode record has at least the following

    • Access mode for this file (Read/write/excute)

    • The owner and group of the file (Owner/group)

    • The size of the file

    • Number of hard links pointing to the contents of this file

    • The time the file was created or changed (CTime)

    • Last read time (atime)

    • Last modified time (mtime)

    • Flags (flag) defining file attributes, such as setuid, etc.

    • Pointing to the true content of the file (point)

The number and size of the inode is fixed at the time of formatting, and the inode has the following characteristics

    • The size of each inode is fixed at 128byte or 256byte

    • Each file occupies only one inode, so the number of files that the file system can create (except hard-linked files) 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 in compliance with the user, and can begin to actually read the contents of the block.

      The approximate structure of the inode is as follows

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/85/3C/wKioL1edvLfhO79sAADw9vwTO8g676.png "title=" Capture 1. PNG "alt=" Wkiol1edvlfho79saadw9vwto8g676.png "/>

Simply analyze the relationship between Inode/block and file size:

We can see that there are 12 direct block pointers, an indirect block pointer, a double indirect pointer block, and a triple indirect pointer block in the inode. Add a block of 1KB, then 12 direct block pointers can point to the data size: 12x1k=12k; When greater than 12KB, an indirect pointer block is used, which points to a block to record the additional block number, a pointer block occupies 4byte, So a block can store 256 pointer blocks, can point to the size of the data is 256x1k=256k, when the file is larger, will use a double indirect pointer block, can point to the size of the data: 256x256x1k=64m; When the file is larger, a triple indirect pointer block is used, The data size can be pointed to: 256x256x256x1k=16g;

Superblock

Superblock is the place to record information about the entire file system, without superblock, there is no such file system. The information they record is mainly:

    • Block vs. Inode Total

    • Unused and used Inode/block quantity

    • Block and Inode size (block is 1k,2k,4k;inode 128byte)

    • File system Information about the file system's hang-up time, the last time the data was written, the last time the disk was checked, and so on

    • A validbit value that Validbit is 0 if this filesystem is already mounted, otherwise 1

Note: A file system should have only one superblock (on blockgroup1), but if the subsequent blockgroup contains superblock it should be primarily a backup of blockgroup within the first superblock, This could be a superblock rescue.

File System Description--file system Description

This section can describe the start and end of each blockgroup block number, as well as the description of each segment Superblock/bitmap/inodetable/datablock between the block number

Block Correspondence Table--block bitmap

Log on disk with unused block numbers

Inode Correspondence Table--inode Bitmap

Record the use and unused inode numbers

Above, we describe the structure of the inode and the access to the common file through the inode. In Linux, everything is file, then how is the directory accessed through the Inode, and how to access files in the directory through the directory?

For directories, the file system will also allocate 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 recorded in the directory and used by the file name.

Here's how to access the files in the directory by accessing the/testdir/rootdir/f1.txt file:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/3C/wKiom1edwk-j7UdbAABWLkbAf5I462.png "title=" Inode Directory Access. PNG "alt=" Wkiom1edwk-j7udbaabwlkbaf5i462.png "/>

    • The system first reads the file name of the Access file (including the path), according to the Mount information, determine the file name corresponding to the partition of the file, and then go to the root directory of the partition to start reading

    • Locate the inode of the/DEV/SDA5 inode number 128 by using the mount point information, and the Inode has the permissions that allow us to read the contents of the block (with R and X)

    • Obtain the block number in the inode of the/testdir and find the inode number of the/testdir/rootdir directory from the Block (174)

    • Read 174th Inode to determine if you have permission (r and X) to get the block number in the Inode

    • Based on the block number obtained, locate the inode number of the/testdir/rootdir/f1.txt file from the contents of the block (349)

    • Read No. 349 inode to determine if you have permission (r) to get the block number in the Inode

    • Reads the contents of the/testdir/rootdir/f1.txt from the block based on the block number obtained


This article is from "Oscar winner" blog, please make sure to keep this source http://luoliumeng.blog.51cto.com/11882524/1832470

Analysis of Linux file System and index node Inode

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.