Linux-inode Understanding

Source: Internet
Author: User
Tags parent directory

I. What is the inode?

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.

Ii. contents of the Inode

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:

[Email protected] ~]#Stat Install. Log File: 'Install. log'Size:29262Blocks: theIO Block:4096Regularfiledevice:803h/2051D Inode:260614Links:1Access: (0644/-rw-r--r--) Uid: (0/root) Gid: (0/root) Access: -- One- -  to: -:51.700999783+0800Modify: -- One- -  to: -:26.927999310+0800Change : -- One- -  to: -:38.148999307+0800

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.

III. structure of the Inode

Find out how the file system accesses files:
1, according to the filename, through the corresponding relationship in the directory, find the file corresponding Inode number
2. Then read the Inode table of the file according to the inode number
3, and then according to the Inode table in the pointer read to the corresponding blocks

One important thing here is that directory, which is not the directory we usually call, but a list that records the inode number for a file/directory name. Such as:

Iv. size of the Inode

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 -ifilesystem     inodes iused  IFree iuse% mountedon/dev/sda3      447920 84137 363783   %/tmpfs          127543     1127542    1/ dev/shm/dev/sda1       51200      51162    1/ Boot

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

[Email protected] ~]# Dumpe2fs-h/dev/sda3 |grep "Inode Size"DUMPE2FS1.41. A( --may- .) Inode Size: the[[Email protected]~]# dumpe2fs-h/dev/sda3 |grep "Block Size"DUMPE2FS1.41. A( --may- .) Block Size:4096[[Email protected]~]# dumpe2fs-h/dev/sda3 |grep "Block Count"DUMPE2FS1.41. A( --may- .) Block Count:1789696[[Email protected]~]# dumpe2fs-h/dev/sda3 |grep "Inode Count"DUMPE2FS1.41. A( --may- .Inode Count:447920

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.

V. Inode number

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

It is worth repeating that theunix/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 within 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 where the file data resides and reads the data.

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

[Email protected] ~]#ls-Iltotal -276146-RW-------.1Root root1271Nov -  to: -anaconda-ks.cfg260614-rw-r--r--.1Root root29262Nov -  to: - Install. Log260616-rw-r--r--.1Root root7572Nov -  to: - Install. Log.syslog260621lrwxrwxrwx.1Root root OneNov -  One: +Link.logInstall. log

Vi. Catalog 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 inode number that corresponds to the file name.

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

ls /etcconsolekit                 hosts.allow                   printcapdir_colors                 hosts.deny                     profile

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

ls -I/etc273880 consolekit                 141546  maven141113 dir_colors                 155143  mcelog141114 dir_colors.256color        130370 Mime.types

Seven, 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, theunix/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:

Syntax format: ln source file Destination file

[Email protected] test]#ls-litotal0267772-rw-r--r--.1Root root0Nov -  -: +F1.txt[[email protected] test]#LNf1.txt f2.txt[[email protected] test]#ls-litotal0267772-rw-r--r--.2Root root0Nov -  -: +F1.txt267772-rw-r--r--.2Root root0Nov -  -: +F2.txt

After running the above command, the source file is the same as the inode number of the destination file, pointing to the same inode. One of the inode information is called the "number of links," which records the total number of filenames pointing to the inode, which increases by 1.

Conversely, deleting a file name causes the "number of links" in the Inode node to be reduced by 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 region.

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. Therefore, the total number of "hard links" in any directory is always equal to 2 plus the total number of subdirectories (with hidden directories).

Eight, 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 "symlink" (Symbolic Link)

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.

Syntax format: ln-s source file or directory destination file or directory

[Email protected] test]#ls-litotal0267772-rw-r--r--.1Root root0Nov -  -: +F1.txt[[email protected] test]#LN-s f1.txt f2.txt[[email protected] test]#ls-litotal0267772-rw-r--r--.1Root root0Nov -  -: +F1.txt267776lrwxrwxrwx.1Root root6Nov -  -: -F2.txt-F1.txt

IX. special role of the Inode

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 normally. At this point, delete the Inode node directly, you can play the role of deleting files.
  (2) move files or rename files, just change the file name, do not affect the inode number.
  (3) when a file is opened, the system identifies the file with the Inode number and no longer considers 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.

Ten, summary

    1. When the disk partition format file system, it will be divided into inode and block two parts of the content
    2. Inode holds the properties of the file and pointers to the file entity, the filename is not in the Inode
    3. Access the file, through the file name----->inode---->blocks
    4. Inode General default size is 256b,block size can be 1k, 2k, 4k, default is 4k, note the boot partition except
    5. Dumpe2fs/dev/sda3 View the size and number of inode and block by Df-i view the number and usage of inode
    6. A file must occupy at least one inode and one block, and multiple files can occupy the same inode (hard link)
    7. A block can only be used by a file, if the file is very small, block is large, it will cause the block greatly wasted
    8. Block is not the bigger the better, to choose according to the size of the business file, is generally the default 4k
    9. You can specify Inode, block size when formatting partitions (for example: Mkfs.ext4-b 4096-i 1024)

This article was reproduced from: http://www.cnblogs.com/adforce/p/3522433.html

Linux-inode Understanding

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.