Linux file system - inode notes

What is the inode

inode definition: a data structure in a Unix file system, a file for storing meta information data
 
file stored in the hard disk is "block" (Block) units, the common block size is 4k
a slightly larger the files will be stored in multiple blocks, then how fast access to the data it? The answer is inode
 
in the file system, each file object corresponds to an inode, which is commonly used to store some information (owner, creation time, modification time, location, file permissions, file the corresponding objects in the system memory block and so on )
is divided into three steps when the operating system accesses a file:

  1. Find the corresponding inode number for the file name
  2. No access to the corresponding file object meta-information by inode
  3. Locate the file block corresponding to the meta information, the read data

As it can be seen from the above description, an index that is actually the inode in the file system, to facilitate fast access to data and file management

Some details about the inode

inode content

POSIX standard defines the information contained in the inode:

  • Expressed in bytes of the file size
  • The device ID, which identifies the file receiving device
  • User ID of the file owner
  • Group ID documents
  • Schema files (mode), to determine the type of file and its owner, its group, other users access to this file
  • Additional system and user flags (flag), used to protect the file
  • 3 timestamp, records the inode itself is modified (ctime, inode change time), the file content is modified (mtime, modification time), last accessed (atime, access time) time
  • A link number that represents the number of hard links pointing to this inode
  • Pointer to the file system storage location

Use stat command to query a file inode numbers and their corresponding meta information file:

inode storage

meta information stored inode file objects will take up part of the disk space
when the file system is created (formatted), a storage area will be divided into two continuous areas, one of which is used to save the inode, called inode Table, each the default inode 128 or 256 bytes of
the other one data block area is used to save the file

As can be seen here, the inode number of a file system determines, at the time of initialization and the inode number proportional to the size of disk

Check each hard disk partition and the total number of inode number has been used, you can use the df command:

Since each file will correspond to a inode, when there are a large number of small files to disk, disk space may appear idle but have run out of inode situation
such as a mail server there may be a large number of eml file the disk inode is exhausted, unable to enter the letter the new e-mail
is generally large-scale mail system will design their own file system to store data messages, such as letterhead barrel (mbox) file structure to store all the data for a single user's messages

Directory files, hard links and soft links

One kind of Linux, everything is a file, the file is a directory, each directory entry consists of two parts: the directory file names of all files, all file names corresponding to the inode number
using the ls -i command to view the contents of the directory:

If you want to view the details of the file directory, then you need the inode number of each file, find the corresponding inode access meta-information:

 
Inode information included in the above, this information can be found in the file name does not exist, in fact, in the Unix environment, a file object may correspond to a plurality of file names, i.e. hard link (hard link)
 
using the command to create a hard link ln :

ln 源文件 目标文件


F1.txt f2.txt figure above all point to the same inode, modify the contents of one file to another, will affect
the inode contents can be found in a number of links, when we create a hard link, the corresponding file the inode number of links will be +1, which indicates the corresponding file name (reference count) +1
Similarly, when deletion of a file, the link number will be -1, the link number is 0 file data is completely cleared

 
And hard links as well as corresponding soft link, using the ln -s command to create soft links:

ln -s 源文文件或目录 目标文件或目录


Difference between soft links and hard links that soft links to their links to the file name, rather than the inode, a soft link has its own inode
delete the file it points later, the soft link is not accessible (No such file or directory)
 
here the design is similar to C ++ pointers and references, hard links can be compared to a reference, while the soft link is a pointer that holds the file name (address to access file data)
and inode number of links is and smart pointer references similar count design ideas , have to say that the computer system has a lot of design commonality

Other effects of the inode

Due to the presence of hard links, file name and inode is many relationships, the operating system to open a file after the file name abandoned, leaving only the inode number to access the contents of the file
library function getcwd () implementation, that is, from the current working directory parent directory inode inode lookup step by step, and finally spell out the full absolute path
 
inode design makes installation / replacement of the library files in the operating system is very convenient
when some processes are still using the library, other processes can replace the library file inode inode number points to a newly created, followed by access to the library have been directed to the contents of the new library file, reducing the time necessary to restart the system to replace the library
as the old of the inode number of links already zero, using the old library after the end of the process, the old inode and the old library file will be automatically recovered file system


references

理解inode -- 阮一峰的网络日志
inode -- wikipedia

Guess you like

Origin www.cnblogs.com/chenxinshuo/p/12150645.html