1 Introduction to the Inode
Understand the inode, from the file storage.
The file is stored on the hard disk and the smallest storage unit of the hard disk is called a "sector" (sector). Each sector stores 512 bytes (equivalent to 0.5KB).
When the operating system reads the hard drive, it does not read one sector at a time, so it is too inefficient to read multiple sectors at once, that is, to read a "block" once in a row. This "block" consisting of multiple sectors is the smallest unit of file access. The size of the "block", the most common is 4KB, that is, eight consecutive sector to form a chunk.
File data is stored in "block", so obviously we also have to find a place to store the metadata of the file, such as the creator of the file, the date of creation of the file, the size of the file, and so on. This area of storing file meta information is called an inode, and the Chinese translation is "index node".
2 contents of the Inode
The inode contains the meta information for the file, specifically the following:
* Number of bytes in file
* File owner's user ID
* The group ID of the file
* Read, Write, execute permissions on files
* File timestamp, there are three: CTime refers to the inode last change time, mtime refers to the file content of the last change in time, atime refers to the file last opened time.
* Number of links, that is, how many file names point to this inode
* Location of File data block
You can use the Stat command to view the Inode information for a file:
[Root@localhost/]# Stat Example.txt
In short, all file information except the file name exists in the inode. As for why there is no filename, the following will be explained in detail.
3 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 areas. One is the data area, which holds the file data, and the other is the Inode table, which holds the information that the inode contains.
The size of each inode node, typically 128 bytes or 256 bytes. The total number of inode nodes, given when formatted, is typically set to an inode every 1KB or 2KB. Assuming that in a 1GB hard disk, each inode node size is 128 bytes, set an inode per 1KB, then the Inode table size will reach 128MB, accounting for 12.8% of the entire hard drive.
You can use the DF command to view the total number of inode and the quantity already used for each hard disk partition.
[Root@localhost/]# Df-i
To see the size of each inode node, you can use the following command:
[root@localhost/]# dumpe2fs-h/dev/sda1 | grep "Inode Size"
Because each file must have an inode, it is possible that the inode has been used up, but the hard drive is not full. At this point, you cannot create a new file on your hard disk.
4 inode Number
Each inode has a number, and the operating system uses the Inode number to identify the different files.
It is worth repeating that the Unix/linux system does not use the filename, and the inode number is used to identify the file. For systems, filenames are just nicknames or nicknames that are easily identifiable by inode numbers. On the surface, the user opens the file by file name. In fact, the 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, locate the block where the file data resides and read the data.
Using the Ls-i command, you can see the inode number corresponding to the file name:
[Root@localhost/]# ls-i test.txt
6970 test.txt
5 Directory Files
In a unix/linux system, directory (directory) is also a file. Opening the directory is actually opening the catalog file.
The structure of a catalog file is very simple, a list of a series of directory entries (dirent). Each directory entry consists of two parts: the file name of the containing file and the Inode number for the file name.
The LS command lists all file names in the directory file only:
[root@localhost/]# ls/
backup boot etc lib lost+found misc net proc sbin SRV tmp var
bin Dev home lib64 media mnt opt root SELinux sys usr
The ls-i command lists the entire directory file, which is the filename and inode number:
[Root@localhost/]# ls-i/
2359297 backup 2 home 14372 misc 131073 root 262145 tmp
393217 bin 2621441 lib 1703937 mnt 1835009 sbin 3014657 usr
2 boot 2490369 lib64 14376 Net 1 selinux 1048577 var
4 Dev One lost+found 2 opt 917505 srv
2097153 etc 524289 Media 1 proc 1 sys
If you want to see the details of a file, you must access the Inode node and read the information based on the inode number. The ls-l command lists the details of the file.
[Root@localhost/]# ls-l/
6 Hard Links
In general, the filename and inode number are "one by one correspondence", and each inode number corresponds to a filename. However, the Unix/linux system allows multiple filenames to point to the same inode number. This means that you can access the same content with different file names, and modifying the contents of the file affects all file names, but deleting a file name does not affect access to another file name. This situation is referred to as a "hard link" (hard link).
ln command to create a hard link:
[root@localhost/]# ln source file target file
After running this command, the source file and the inode number of the target file are the same, pointing to the same inode. An inode message is called a "number of links", which records the total number of file names that point to the inode, and then increases by 1. Conversely, deleting a filename 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 area.
By the way, here are the number of links to the directory file. When you create a directory, two directory 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" for any directory is always equal to 2 plus the total number of subdirectories (including hidden directories), where 2 is the "hard link" of the parent directory and the ". Hard link" under the current directory.
7 Soft Links
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, whichever file is opened, the final read is file B. At this point, file A is referred to as the "soft link" (soft link) or symbolic link (symbolic link) of file B.
This means that file a depends on file B, and if you delete file B, 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 filename, not the inode number of file B, and the Inode "link number" of File B does not change.
The ln-s command can create soft links.
[root@localhost/]# ln-s source file or directory destination file or directory
8 Special effects of the Inode
Because the inode number is separated from the filename, this mechanism leads to some phenomena peculiar to the unix/linux system.
1. Sometimes, the file name contains special characters and cannot be deleted normally. At this point, directly delete the Inode node, 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 identifies the file with the Inode number and no longer considers the filename. Therefore, generally speaking, the system cannot know the filename from the inode number.
The 3rd makes the software update simple, can be updated without shutting down the software, do not need to restart. Because the system through the Inode number, identify the running files, not through the filename. Update, the new version of the file with the same file name, to generate an inode, will not affect the running of the file. Wait until the next time the software is run, the file name will automatically point to the new version of the file, the old version of the file's inode is recycled.
9 Practical Problems
In a lower-configured Linux server (memory, hard disk is relatively small) in the/data partition to create a file, the system prompts the disk space is not enough, with the df-h command to view the disk usage, found that/data partition only used 66%, and 12G of the remaining space, it is not supposed to appear this problem. Later, Df-i looked at the/data partition's index node (inode) and found that it was full (iused=100%), causing the system to create new directories and files.
Find out why:
There are a very large number of small byte cache files in the/data/cache directory, and the block is not much occupied, but it takes up a lot of inode.
Solution:
1. Delete some of the files in the/data/cache directory, releasing part of the inode for the/data partition.
2. Connect the Newcache directory in the free partition/opt to/data/cache using a soft connection, and use the inode of the/OPT partition to alleviate the problem of the insufficient inode of the/data partition:
****************************************************************************************
Original address: http://blog.csdn.net/jesseyoung/article/details/42524813
Blog home: Http://blog.csdn.net/jesseyoung
****************************************************************************************