Understanding of the Inode in Linux

Source: Internet
Author: User
Tags parent directory

First, Inode What is it?

To understand the inode, start with the file storage.

The file is stored on the hard disk, and the minimum storage unit for the hard disk is called "Sector" (Sector). Each sector is stored 512 bytes (equivalent to 0.5KB).

When the operating system reads the hard disk, it does not read each sector, so the efficiency is too low, but the one-time continuous reading of multiple sectors, that is, one time to read a block. This "block", composed of multiple sectors, is the smallest unit of file access. "Block" size, the most common is 4KB, that is, eight consecutive sector to form a block.

File data is stored in "blocks", then obviously, we must also find a place to store the meta-information of the file, such as the creator of the file, the date the file was created, the size of the file, and so on. This area of stored file meta information is called Inode, and the Chinese name is "Index node".

Each file has a corresponding inode, which contains some information about the file.

Second, Inode of the content

The inode contains meta information for the file, specifically the following:

* Number of bytes in the file

* User ID of the owner of the file

* The group ID of the file

* file read, write, execute permissions

* File timestamp, total three: CTime refers to the time when the inode was last changed, mtime refers to the time when the file content was last changed, atime refers to the time when the file was last opened.

* Number of links, that is, how many filenames point to this inode

* Location of File data block

You can use the Stat command to view inode information for a file:

Stat Example.txt


In summary, all file information except the filename is present in the inode. As for why there is no file name, the following is explained in detail.

Third, Inode the size

The inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two zones. One is the data area, the file data is stored, and the other is the Inode area (inode table), which holds the information contained in the Inode.

The size of each inode node is typically 128 bytes or 256 bytes. The total number of inode nodes, given at the time of formatting, is usually set to one inode per 1KB or 2KB each. Assuming that the size of each inode node in a 1GB hard disk is 128 bytes, and one inode is set per 1KB, the Inode table will be about 128MB in size, accounting for 12.8% of the entire drive.

You can use the DF command to view the total number of inode per hard disk partition and the quantity that is already in use.

Df-i


To see the size of each inode node, you can use the following command:

sudo dumpe2fs-h/dev/hda | grep "Inode Size"


Since each file must have an inode, it is possible that the inode has been exhausted, but the hard disk is not yet full. At this point, you cannot create a new file on your hard disk.

Four, Inode number

Each inode has a number, and the operating system uses Inode numbers to identify different files.

It is worth repeating here that the unix/Linux system does not use file names, and inode numbers are used to identify files. For the system, the file name is just an alias or nickname for the inode number to easily identify.

On the surface, the user opens the file by file name. In fact, this process inside the system is divided into three steps: First, the system finds the inode number corresponding to the file name, and secondly, obtains the inode information through the inode number, and finally, according to the Inode information, finds the block of the file data and reads the data.

With the Ls-i command, you can see the inode number corresponding to the file name:

Ls-i Example.txt


V. Catalogue files

In a unix/linux system, directory is also a file. Opening the directory is essentially opening the directory file.

The structure of the catalog file is very simple, which is a list of a series of catalog items (dirent). Each directory entry consists of two parts: the file name of the included file, and the file name corresponding to the Inode number.

The LS command lists only all the file names in the directory file:

Ls/etc


The ls-i command lists the entire directory file, the file name and inode number:

Ls-i/etc


If you want to view the details of a file, you must access the Inode node based on the inode number and read the information. The ls-l command lists the details of the file.

Ls-l/etc


Understanding the above knowledge, you can understand the permissions of the directory. The Read permission (r) and Write permission (W) for the directory file are for the directory file itself (that is, what permissions different users can access to the directory file, for example here different users to the TMP directory file (d can detect that TMP is a directory file, D represents directory, That is, the directory is rwxr-xr-x, the first group of three characters, that is, rwx, the file owner of the user's read and write permissions to the file, the second group of three characters, that is, R-x, the file owner of the user group of users who read and write access to the file, the third group of three characters, namely R-x, Represents the read-write permission of a user other than the user group in which the file owner user resides. A process access operation that runs under a user can only operate on the directory file that the user has access to. Because only the file name and inode number are in the directory file, if you have Read permission, you can only get the file name and cannot get additional information because the other information is stored in the Inode node, and the information inside the Inode node requires execute permission (x) of the directory file.

Six, hard links

In general, the file name and inode number are the "one by one correspondence" relationship, and each inode number corresponds to a file name.

However, Unix/linux system allows multiple filenames to point to the same Inode number.

This means that the same content can be accessed with different file names, and changes to the contents of the file affect all file names, but deleting a file name does not affect access to another file name. This is referred to as a " hard link ".

The ln command can create a hard link:

ln source file Destination file


After running the above command, the source file is the same as the inode number of the destination file, pointing to the same inode. Inode One of the messages is called " Number of links " , the record points to the Inode the total number of filenames, this will increase by 1.

in turn, deleting a file name will make Inode in the node " Number of links " minus 1 . When this value is reduced to 0, indicating that no file name points to the inode, the system reclaims the inode number and its corresponding block area.

Here, by the way, the "number of links" to the catalog file. When you create a catalog, two catalog entries are generated by default: "." and ".". The inode number of the former is the inode number of the current directory, which is equivalent to the "hard link" of the current directory, and the inode number is the inode number of the parent directory of the current directory, which is equivalent to the "hard link" of the parent directory. So, the total number of "hard links" in any directory is always equal to 2

Seven, soft link

In addition to hard links, there is a special case.

The inode number for file A and file B is different, but the content of file A is the path to file B. When you read file A, the system automatically directs the visitor to file B. Therefore, regardless of which file you open, the final read is file B. At this point, file A is referred to as the "soft link" of File B (soft link) or "Symbolic link" (symbolic).

This means that file a depends on file B and if file B is deleted, opening file A will cause an error: "No such file or directory". This is the biggest difference between soft links and hard links: file a points to file B's file name, not file B's inode number, and file B's Inode "link count" does not change.

The ln-s command can create a soft link.

Ln-s source file or directory destination file or directory


Eight, Inode the special role

Because the inode number is separated from the file name, this mechanism causes some unix/linux system-specific phenomena.

1. Sometimes the file name contains special characters and cannot be deleted properly. At this point, delete the Inode node directly, you can play the role of deleting files.

2. Move or rename the file, just change the file name without affecting the inode number.

3. After opening a file, the system will identify the file with the Inode number and no longer consider the filename. Therefore, it is generally not possible for the system to know the file name from the inode number.

The 3rd makes software updates simple and can be updated without shutting down the software, without requiring a restart. Because the system through the Inode number, identify the running files, not through the file name. Update, the new version of the file with the same file name, the generation of an inode, will not affect the running files. The next time you run this software, the file name will automatically point to the new version of the file, the old file's inode is recycled.



Additional:

Second, hard links and soft links

each of these Dentry all have a unique Inode , and each Inode you may have more than one Dentry , this situation is caused by LN hard links are generated.

Hard link: In fact, the same file has multiple aliases, with the same inode, and Dentry is different.

1. The file has the same inode and data block;

2. Only files that already exist can be created;

3. Creating hard links for different cross-file systems

4. Cannot create a directory, can only create hard links to files

5. Deleting a hard link does not affect other files that have the same inode number;

Soft Link: The soft link has its own inode, which has its own file, but the contents of this file is the path name of the other file. Therefore, the soft link has its own inode number and user data block.

1. Soft links have their own file attributes and permissions, etc.;

2. Soft links can be created for non-existent files or directories;

3. Soft link can cross file system;

4. Soft links can be created on files or directories;

5. When creating a soft link, the link count I_nlink will not increase;

6. Deleting a soft link does not affect the file being pointed to, but if the original file is deleted, it becomes a dead link, but the path to re-create it back to normal soft link, but the content of the source file may be changed.


The file allocation method is the file system structure when the index is allocated (roughly speaking, it is the partition structure):

A file system file is divided into directory files and ordinary files of the two categories.

If the file allocation method is index allocation, then there is the concept of the index node appears.


The inode also consumes hard disk space, so when the hard disk is formatted, the operating system automatically divides the hard disk into two zones. One is the data area, the file data is stored , and the other is the inode area (inode table), which holds the information contained in the Inode.

The size of each inode node is typically 128 bytes or 256 bytes. The total number of inode nodes, given at the time of formatting, is usually set to one inode per 1KB or 2KB each. Assuming that the size of each inode node in a 1GB hard disk is 128 bytes, and one inode is set per 1KB, the Inode table will be about 128MB in size, accounting for 12.8% of the entire drive.

You can use the DF command to view the total number of inode per hard disk partition and the quantity already used: Df-i


2. Partitioning

(1) partition structure

Partitions ( Partition ) in being Linux the file system (such as ext2 ) When formatted, it is divided into inode Table and the Block Table two parts, and the size is fixed. all the inode in the partition is in the Inode table, and all blocks are in block table.


Files, directories, catalog items, index nodes, super blocks


Some of the concepts above are shown in position relationship 4 on disk.

Figure 4. disk and file system


The directory blocks are stored in one FCB (file control block, a general - bytes) " FCB is the business data that is stored in the directory file, and the data block is the business data of the ordinary file. A common file consists of one FCB in the directory block plus multiple blocks of data, and the catalog file consists of one FCB in the directory block plus multiple other directory blocks. An index node can only be used by one file (whether it is a directory file or a normal file) and cannot be used by other files at the same time. A directory block can only be located in the directory tree in the same sibling files (whether it is a directory file, or ordinary files), so a root file of the FCB directory block can only hold the root directory file FCB , the root file is the only one that is sibling to the root directory. The FCB of a file points to his index node, his index node points to the block owned by the file (if the file is a directory file, the block that the file owns is the directory block, and if the file is a normal file, the block that the file owns is the data block;)


Superblock is the most basic metadata for a file system, which defines the file system's similarity, size, state, and other metadata data structures (metadata). Superblock is critical for file systems, so it is redundant to store multiple copies for each file system. Superblock is a very "high-level" metadata structure for file systems. For example, if the superblock of the/var partition is corrupted, the/var partition will not mount. At this point, fsck is typically executed to automatically select a copy of the Superblock backup to replace the damaged superblock and attempt to repair the file system. The primary superblock is stored in block 0 or block 1 of the partition, while the Superblock backup is distributed across multiple blocks of the file system. When manual recovery is required, we can use DUMPE2FS/DEV/SDA1 | Grep-i Superblock to see which copy of the Superblock backup of the SDA1 partition is available. Let's assume that dumpe2fs output a line like this: Backup Superblock at 163840, Group descriptors at 163841-163841, and with this information we can try to use this superblock backup:/S Bin/fsck.ext3-b 163840-b 1024/dev/sda1. Please note that we assume that the block size is 1024 bytes.


Understanding of the Inode in 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.