Learn Linux Inode in one day

Source: Internet
Author: User

 

In the Linux file system, many people do not understand Inode. Today I will share with you my understanding of Inode. If there is something wrong with understanding Inode, please give me more criticism.

One day in the previous article, I learned about Linux. In the last article, I gave an EXT3 file system structure, which involves Inode-the lower-left part of the figure. Today, we will discuss this part in detail. We hope that the content of today will help you better understand Inode.

 


First, let's look at the Inode structure.

 

 

 

Next, let's take a look at how the file system accesses files.

1. Locate the Inode number corresponding to the file through the correspondence in Directory based on the file name

2. Read the Inode table of the file based on the Inode number.

3. Read the corresponding Blocks according to the Pointer in the Inode table.

 

Here is an important thing: Directory, which is not a common Directory, but a list that records the Inode number corresponding to a file/Directory name. For example

 

 

Directory:

A directory is a mapping between the human name for the file and the computer's inode number.

Therefore, this Directory is not a file. We can think of it as an attribute in the file system. It is only used for key file names and Inode numbers. This must be understood. Otherwise, it is not easy to understand the hard link content.

 

I learned about the basic concepts of file and directory permissions in Linux one day.

The second column shows how many files are connected to inode.

For a file, this field indicates the number of hard links of the file,

If it is a directory, this field indicates the number of subdirectories contained in the directory.

 

Is it easy to understand now? If you still don't understand it, let's get to know it through the example below.

 

Take the RHEL6 system as an example.

Create a test directory under the root directory. Go to this directory and perform operations.

[Root @ yufei test] # pwd

/Test

[Root @ yufei test] # touch testfile

[Root @ yufei test] # mkdir testdir

Create lab files and directories

[Root @ yufei test] # ls-li

Total 4

977 drwxr-xr-x. 2 root 4096 Apr 5 testdir

976-rw-r-. 1 root 0 Apr 5 16:47 testfile

Inode and inode count of the file and directory are

977 <--> 2 <--> testdir

976 <--> 1 <--> testfile

The number of links in the directory is 2 and the number of links to the file is 1. Why? In fact, it is easy to understand. For a directory, there must be two special directories in each directory, that is. and .. these two directories are also described in the previous course ,. indicates the current directory, and .. indicates the upper-level directory. We also know that in a Linux system, It is found from the root. To find a directory, you must first find its upper-level directory. Therefore, empty directories (strictly speaking, cannot be empty.) There are two links to the corresponding Inode number. As a file, it is obvious that there is only one link to the corresponding Inode number. Needless to say,

 

Next let's take a look at how the number of links has changed.

Continue the above operations

[Root @ yufei test] # ln testfile. hard

[Root @ yufei test] # ln-s testfile. soft

Create a hard link and a soft link for testfile

[Root @ yufei test] # ls-il

Total 4

977 drwxr-xr-x. 2 root 4096 Apr 5 testdir

976-rw-r-. 2 root 0 Apr 5 16:47 testfile

976-rw-r-. 2 root 0 Apr 5 16:47 testfile. hard

978 lrwxrwxrwx. 1 root 8 Apr 5 testfile. soft-> testfile

After checking the properties of the file and directory, we can find that after creating a hard link, the inode count of testfile is added. In addition, the Inode numbers testfile and testfile. hard are the same. This hard link re-creates a file name corresponding to the Inode of the original file. In essence, a new ing is added to Directory. In this example, do you know more about the meaning of Inode count. It refers to the number of file names corresponding to an Inode.

 

Next let's take a look at other features of hard links.

[Root @ yufei ~] # Watch-n 1 "df-I; df"

Every 1.0 s: df-I; df Tue Apr 5 21:52:53 2011

 

Filesystem Inodes IUsed IFree IUse % Mounted on

/Dev/sda1 960992 105415 855577 11%/

Tmpfs 63946 1 63945 1%/dev/shm

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 2747612 11603116 20%/

Tmpfs 255784 0 255784 0%/dev/shm

The preceding command allows you to view the number of blocks and inode changes in the system in real time.

We recommend that you do not use the deumpe2fs and tune2fs commands. If you use them to view them, it will be very depressing-you will find that no matter how you create a file or write content to the file, the values of Inode and block will not change unless you restart the system once every operation, and use the above command to monitor their changes in the second. You can view and learn about the df command. Of course, there is also the du command, which is related to the file system.

 

Let's create a hard link.

[Root @ yufei test] # ls-li

Total 4

977 drwxr-xr-x. 2 root 4096 Apr 5 testdir

976-rw-r-. 2 root 0 Apr 5 16:47 testfile

976-rw-r-. 2 root 0 Apr 5 16:47 testfile. hard

978 lrwxrwxrwx. 1 root 8 Apr 5 testfile. soft-> testfile

[Root @ yufei test] # ln testfile. hard1

[Root @ yufei test] # ls-li

Total 4

977 drwxr-xr-x. 2 root 4096 Apr 5 testdir

976-rw-r-. 3 root 0 Apr 5 16:47 testfile

976-rw-r-. 3 root 0 Apr 5 16:47 testfile. hard

976-rw-r-. 3 root 0 Apr 5 16:47 testfile. hard1

978 lrwxrwxrwx. 1 root 8 Apr 5 testfile. soft-> testfile

Observe the relationship between Inode count and Inode number.

Next let's take a look at the inodes and blocks changes.

[Root @ yufei ~] # Watch-n 1 "df-I; df"

Every 1.0 s: df-I; df Tue Apr 5 21:53:38 2011

 

Filesystem Inodes IUsed IFree IUse % Mounted on

/Dev/sda1 960992 105415 855577 11%/

Tmpfs 63946 1 63945 1%/dev/shm

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 2747612 11603116 20%/

Tmpfs 255784 0 255784 0%/dev/shm

We found that inodes and blocks are not reduced, so hard links do not occupy disk space.

If you delete a hard link, it will change the number of Inode count. Hard links also have two other features: they cannot cross-Filesystem or link directories.

 

Next let's take a look at this soft link.

[Root @ yufei test] # ls-il testfile. soft testfile

976-rw-r-. 3 root 0 Apr 5 21:50 testfile

978 lrwxrwxrwx. 1 root 8 Apr 5 testfile. soft-> testfile

The Inode number is different from the original file. The size also changes. It can be seen that this soft link re-creates a file, and the file points to the original file, rather than to the original Inode. Of course, it will occupy inode and block. After the source file is deleted, the linked file cannot exist independently. Although the file name is retained, we cannot view the content of the soft link file. However, soft links can be cross-file systems and can be linked to directories. It is equivalent to a shortcut in windows. With this feature, we can solve the problem of inode conut deficiency in a partition through soft link (soft link to another inode count enough partitions ).

 

Next, let's analyze the effects of copying, moving, and deleting files on inode.

[Root @ yufei ~] # Watch-n 1 "df-I; df"

Every 1.0 s: df-I; df Tue Apr 5 21:57:38 2011

 

Filesystem Inodes IUsed IFree IUse % Mounted on

/Dev/sda1 960992 105415 855577 11%/

Tmpfs 63946 1 63945 1%/dev/shm

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 2747612 11603116 20%/

Tmpfs 255784 0 255784 0%/dev/shm

 

[Root @ yufei test] # ls-li

Total 4

977 drwxr-xr-x. 2 root 4096 Apr 5 testdir

976-rw-r-. 3 root 0 Apr 5 18:54 testfile

976-rw-r-. 3 root 0 Apr 5 18:54 testfile. hard

976-rw-r-. 3 root 0 Apr 5 :54 testfile. hard1

978 lrwxrwxrwx. 1 root 8 Apr 5 testfile. soft-> testfile

Record the above information first

 

Check the file copy status first.

[Root @ yufei test] # cp testfile. cp

[Root @ yufei test] # ls-li

976-rw-r-. 3 root 0 Apr 5 21:50 testfile

979-rw-r-. 1 root 0 Apr 5 21:58 testfile. cp

We only compare the two files and find that the Inode number is different. Let's take a look at the remaining conditions of inodes and blocks.

Every 1.0 s: df-I; df Tue Apr 5 22:02:49 2011

 

Filesystem Inodes IUsed IFree IUse % Mounted on

/Dev/sda1 960992 105416 855576 11%/

Tmpfs 63946 1 63945 1%/dev/shm

Filesystem 1K-blocks Used Available Use % Mounted on

/Dev/sda1 15118728 2747620 11603108 20%/

Tmpfs 255784 0 255784 0%/dev/shm

Inodes is reduced and blocks is reduced. This means that copying a file creates a file and occupies Inode and Block.

The file creation process is: first look for an empty Inode, write a new Inode table, create Directory, corresponding file name, Write File Content to the block

 

You can use this experiment to move and delete files. I will give a description directly.

There are two ways to move a file:

When Moving Files in the same file system

Create a new inoing between the file name and Inode (that is, write information in Directory), delete the old information in Directory, and update CTIME, other information, such as Inode, has no effect.

 

When Moving Files in different file systems

First, find an empty Inode, write a new Inode table, create a Directory ing in Directory, Write File Content to the block, and change the CTIME.

 

Delete an object

In essence, link count is reduced. When link count is 0, it indicates that this Inode can be used and the Block is marked as Writable, but the data in the Block is not cleared, unless there is new data that requires this block.

 

Finally, let's make a summary:

1. An Inode corresponds to a file, and a file occupies multiple blocks Based on its size.

2. More accurately, a file corresponds to only one Inode. Because hard links are not actually creating new files, they only write new mappings in Directory.

3. When we delete a file, we only mark Inode as available. The content of the file in the block is not cleared. Only when a new file needs to occupy blocks, will be overwritten'

 

From yufei blog

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.