Linux inode && Fast Directory Travel Method (undone)

Source: Internet
Author: User

Directory

1 . Introduction to Linux inode 2. Fast Directory Travel Method

1. Introduction to Linux Inode

0x1: Disk Partitioning principle

Bytes-and sector (sector) (512 bytes per sector) block (the most common is 4KB, that is, 8 contiguous sector form a block)

1. The minimum storage unit for the  disk is sector 2. The smallest unit of file storage is " block "

0x2:linux EX2 FileSystem

When a partition (partition) is formatted as a ext2 file system, there are two areas of inode table and block area

1The . Inode table holds the inode, where the area that stores the file meta information resembles the concept of MBR in Windows (main boot sector, master boot Recoder), which is on the No. 0 track of a hard disk. The computer will read the area as soon as it is switched on. By the meaning of the MBR, if a hard disk MBR hangs, this hard disk is equal to the inode is recorded"the relevant attributes of the file, and the file contents are placed within a Block"the information. Simply put, the inode in addition to record the attributes of the file, but also must have a point (pointer) function, that is, to the file content placed in the block, so that the operating system can correctly get the contents of the file we know that all the devices under Linux are abstracted to the unified concept of the document, Each file has a corresponding inode, which contains some information about the file/*the structure in which the inode metadata is stored is: struct stat, which can be used to view inode information for a file using the stat command: Stat example.txtstruct stat {dev_t St_dev; /* ID of device containing file*/ino_t St_ino; /*inode Number*/mode_t St_mode; /*Protection*/nlink_t St_nlink; /*Number of hard links*/uid_t St_uid; /*User ID of owner*/gid_t St_gid; /*group ID of owner*/dev_t St_rdev; /*Device ID (if special file)*/off_t st_size; /*Total size, in bytes*/blksize_t st_blksize; /*BlockSize for filesystem I/O*/blkcnt_t st_blocks; /*Number of 512B blocks allocated*/    /*Since Linux 2.6, the kernel supports nanosecond precision for the following timestamp fields. For the details before Linux 2.6, see NOTES. */    structTimespec St_atim;/*Time of last access*/    structTimespec St_mtim;/*Time of last modification*/    structTimespec St_ctim;/*Time of last status change*/    #defineSt_atime st_atim.tv_sec/* Backward compatibility */#defineSt_mtime st_mtim.tv_sec#defineSt_ctime st_ctim.tv_sec};*/2The . Block Areablock is the smallest unit in the file system that holds the actual data, and the data itself is stored in this area

Size of the 0x3:inode

The inode also consumes hard disk space, and we know that the inode information is stored in the Inode table, and 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 an inode is set per 1KB, the Inode table will be up to 128MB in size, representing 12.8% of the entire hard drive
To see the total number of inode and number of uses for each hard disk partition, you can use the DF command

Df-i

0x4:inode number

Each inode has a number, and the operating system uses Inode numbers to identify different files. 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. In fact, this process within the system is divided into three steps

1 The system finds the inode number corresponding to this file name. 2 . Get inode information via inode number 3. According to the Inode information, locate the block where the file data resides, read the data

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

Ls-i ip.txt.win03

0x5: Reading directory files

In a unix/linux system, directory is also a file. Open Directory, actually open directory file
The structure of a directory file is very simple, a list of catalog items (dirent), that is, the traversal of directory entries (dirent) in nature. Each catalog item, consisting of two parts

1 file names for the included files 2. Inode number corresponding to the file name

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

Ls-i/etc

The Read permission (r) and Write permission (W) for the directory file are for the directory file itself. Because only the file name and inode number in the directory file, if only read permission, can only get the file name, unable to obtain additional information, because the other information is stored in the Inode node, and read the information within the Inode node requires the directory file execution permissions (x)
0x6: 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, the Unix/linux system allows multiple filenames to point to the same inode number. In this architecture design of Linux, the following things can be achieved

1 . Access the same content with a different file name 2 Changes to the contents of a file can affect all filenames 3. Delete a filename without affecting access to another file name

This is referred to as a "hard link"

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

It is worth noting that:

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 of the latter is the inode number of the parent directory of the current directory, equivalent to the "  Hard link . " So, the total number of " hard links " in any directory is always equal to 2 plus the total number of subdirectories (with hidden directories)

0x7: 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 number" does not change
Special effects of 0x8:inode

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

123

Relevant Link:

http://man7.org/linux/man-pages/man2/stat.2.htmlhttp://www.cnblogs.com/ kkgreen/archive/2012/03/02/2377794.htmlhttp://www.ruanyifeng.com/blog/2011/12/inode.html 

2. Fast Directory Travel Method

Relevant Link:

http://guojing.me/linux-kernel-architecture/posts/dentry-cache/http://  Oss.org.cn/kernel-book/ch08/8.3.3.htmhttp://blog.csdn.net/cywosp/article/details/21126161

Copyright (c) Littlehann All rights reserved

Linux inode && Fast Directory Travel Method (undone)

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.