Understanding of Inode and block under Linux

Source: Internet
Author: User

Basic concepts

First, the basic concepts of the inode and the block are spoken. In a Linux system, files consist of metadata and data blocks . A data block is a multiple contiguous sector (sector), which is the smallest unit of file storage (512 bytes each). Block size, the most common is 4KB, that is, a continuous 8 sector composition, storage file data and directory data . Metadata is used to record the creator of the file, the date of creation, the size, and so on, which is called the Inode, the index node, which stores the file metadata information.

Because the inode is also used to store file-related attribute information, it also consumes hard disk space. The information contains the inode number, the number of bytes of the file, User ID, Group ID, read, write, execute permission, timestamp (total three: CTime refers to the time of the last change of the inode, mtime refers to the time of the last change in the contents of the file, Atime refers to the time the file was last opened), the number of links (soft and hard link), the location of the data block, Note that there is no file name.

When the hard drive is formatted, the operating system divides the hard disk into two zones, the data area and the Inode area. The size of each inode node is typically 128B or the total number of 256b,inode is determined when the file system is formatted.

There are also several concepts to understand:

inode Bitmap: The inode bitmap, using binary to record the use of the inode, such as whether the inode is idle and so on.

Block Bitmap: Blocks bitmap, with Inode Bitmap, using binary mode to record the use of the block. When a file is found or created, this bitmap is scanned to find the block corresponding to the free inode number.

Super BLOCK: The superuser contains the overall information about the file system on the hard disk or partition, such as the size of the file system.

Dentry: It plays the role of connecting different file object Inode in the kernel, and then it plays the role of maintaining the file system directory tree. Dentry is a purely memory structure that is created directly in memory by the file system during the process of providing file access. The dentry contains information such as file name, inode number of the file, and so on.

To the file system Inode area block plot:

How to find, create, and delete files

Next to the file search, creation, deletion principle three aspects to the inode and block understanding.

File lookup: For example, to find/var/log/message

File creation: For example, to create a/backup/test.txt (if the backup directory exists)

Deletion of files: deleting/backup/test.txt

Find

    1. First root directory (directory is also a file, not a container!) Can also be understood as a mapping table for a path) is self-referencing. Locate the corresponding entry in the Inode table. (For the moment, the inode number in the Inode table and its corresponding block information becomes an entry)
    2. The corresponding information in the Inode table points to the block corresponding to the root directory, with the Var directory corresponding to the information and inode number.
    3. The system finds its corresponding entry for the Var directory in the Inode table based on the Var directory inode number.
    4. The corresponding block is found according to the block information of the Var directory, which has the information of the log directory and the inode number.
    5. The system also finds its corresponding entry in the Inode table based on the log directory inode number.
    6. According to the log directory corresponding block information to find the corresponding block, which has messages file.
    7. Finds its corresponding entry in the Inode table based on the inode number of the messages file.
    8. Finally, the system tells us how many blocks the messages file corresponds to according to the entry information of messages, and finally shows us the data we see.

Create

    1. First, scan the inode bitmap, find the free inode number, and then take it.
    2. The root self-reference finds the corresponding entry in the Inode table and finds the corresponding block based on the entry.
    3. The entries in the Inode table are found back by the inode number corresponding to the backup directory in the block corresponding to the root directory.
    4. The entries in the Inode table find the corresponding block, and a new file Test.txt is created in the dentry of the block.
    5. This test.txt file is then given to the inode number that was occupied at the beginning of the scan.

When you want to save the data to the Test.txt file, the following occurs:

The system scans the block bitmap (accelerates the lookup of disk free blocks, records whether idle, etc.), finds free blocks, allocates blocks larger than the data size, and then recycles (avoids disk fragmentation). For example, to create a 10k size Test.txt file, a block of 4k, only need 3 blocks, the system is allocated 4 or more, and finally to the extra block allocated to the free block waiting for use.

Delete

When you want to delete a file, it is to use the block bitmap is marked as idle, the relevant location of the Inode bitmap is empty, the equivalent is not occupied, the system considers this file to be deleted. But the data is still there, and the next time you create the file, you can overwrite the data for the file that was emptied of the block bitmap and the inode bitmap.

The file powder also only uses some random data to populate the original file data block, which makes the file difficult to recover.

Above

Understanding of Inode and block under Linux

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.