How file searching works in linux

Source: Internet
Author: User

The file name format in Linux is the same as that in Unix. The directory names are separated by a slash. For example, the file name/home/vmail/test, where/home And/vmail are directory names, and test is the file name. In Linux, file names can be grouped by any printable character or any length. The system analyzes the files in the directory to find the index nodes corresponding to the files.

The first index node required by the system is the index node of the root directory of the file system. Its value is stored in the super block of the file system. To read the index node of an ext2 file system, we must search for it in the index node table of the corresponding data block group. For example, if the index node value of a root directory is 42, We need to read the 42nd index nodes in the index node table of the data block group 0. The root directory index node is an ext2 directory node. That is to say, the mode of the root directory index node is the directory, and its data block contains the ext2/directory entry. From the/index node, we can find the index node corresponding to the home directory. The home directory is only one of the many directory entries, we can find the index node value of the/home/vmail directory from the corresponding node of/home. Next, read the directory (first read its index node, and then read the vmail directory in the data block described by this index node), and find the index node value in the/home/vmail directory. Finally, find the index node value of the test file in the directory entry pointed to by the index node in the description/home/vmail directory, use its index node value to find the data block stored in the file.


Index node inode

In the ext2 file system, index nodes are the foundation of everything. Each file and directory in the file system uses a unique index node. The index nodes in each data block group are stored in the index node table. The data block group also has an index node bitmap, which is used to record allocated and unallocated index nodes in the system. The following are some main fields of the index node of ext2:

1. mode-I _node
Here, we save two pieces of information: What is described by the index node and the permissions of the user. For example, for ext2, an index node can describe files, directories, symbolic connections, Block devices, character devices, and FIFO structures.
2. Owner Information
This is the user and group identifier of the file or directory owner. This allows the file system to correctly authorize an access operation.
3. Size-I _size
The file size in bytes.
4. Timestemps-I _ctime
The time when the index node was created and the time when the index node was last modified.
5. Datablocks-I _block
Pointer to the data block that stores the description file of this index node. The first twelve pointers are pointers to physical data blocks that store data, and the last three pointers include indirect pointers of different levels. For example, two-level pointers point to a pointer block pointing to another pointer block. This means that files with less than or equal to one or two data blocks can be accessed at a speed higher than that of two data blocks.
You should note that the index node of ext2 can describe some special device files. These device files are not real files, but programs in the system can use these device files to access their related devices. All these device files are under the/dev directory. For example, the hook program can use the device file it wants to mount as a parameter.

Super Block

A Superblock volume stores basic information about the size and shape of a file system. The file system administrator can use and maintain the file system. (Generally, when the file system is mounted, the system only reads the super block from data block group 0, but each data block group contains a copy of the super block, in case of system crash)
The super block of EXT2 on the disk is stored in an ext2_super_block structure. The super block contains the following main information:
1. Magic Number (Magic Number) ━ s_magic
Make sure the mounting program is the super block of the ext2 file system. The current value is 0xEF53. (Different types of question systems have different magic numbers. "magic numbers" are used to differentiate different file systems)
2. Revision Level (Revision Level) Revision (version s_rev_level; minor version s_minor_rev_level)
This is the main version number and slave version number of the file system. Depending on this information, the hook program determines whether the file system supports functions of certain file systems.
3. Mount Count (number of connections) s_s_mnt_count and Maximum Mount Count (Maximum number of connections)-s_max_mnt_count
The system is used to determine whether the file system should be fully checked. Each time a file system is attached, the value of mount count is increased by 1. When the value of Mount count is equal to that of Maximum mount count, the system displays Maximal mount count reached and running e2fsck is recommended information, prompting you to check the file system.
4. Block Group Number (Block Group Number) s_s_block_group_nr
The data block number that contains this super block.
5. Block Size (Block Size) s_s_log_block_size
The size of data blocks in the file system, for example, 1 0 2 4 bytes.
6. Blocks per Group (number of Blocks in each Group) s_s_blocks_per_group
The number of data blocks in a data Block group is the same as the Block Size. It is fixed after the file system is created.
7. Free Blocks (idle block) ━ s_free_blocks_count
Number of idle data blocks in the file system.
8. Free Inodes (idle index node) s_s_free_inodes_count
Number of idle index nodes in the file system.
9. First Inode (First index node) s_s_first_ino
The first index node number in the file system. In an ext2 root file system, the first index node is the/directory entry.

Group descriptor of EXT2

Each data block group has a data structure similar to that of a super block. A data block group descriptor must be copied in each data block group.
The Group descriptor of each group is stored in the ext2_group_desc structure. The data block group descriptor contains the following information:
1. Blocks Bitmap (block number of the block Bitmap)-bg_block_bitmap
2. Inode Bitmap (index node Bitmap block number)-bg_inode_bitmap
3. Inode Ta B l e (block number of the table block on the first index node)-bg_inode_table
4. Free blocks count (number of idle blocks in the group)-bg_free_blocks_count
5. Free Inodes count (number of idle index nodes in the group)-bg_free_inodes_count
6. Used directory c o u n t (number of Used Directories)-bg_used_dirs_count
Notice: in fact, the EXT2 file system only uses the data block descriptor table in The 0th blocks of the data block group.

EXT2 directory

Ext2 implements the directory with a special question. The data block of such a file stores the file name and the corresponding index node number together.
This directory structure is stored in the ext2_dir_entry_2 structure.
The length of this structure is variable. The Last name field of this structure is a variable-length array of EXT2_NAME_LEN characters, so this structure is variable.
Fields in the Ext2 directory:
1. inode index node number
2. rec_len directory item length
3. name_len File Name Length
4. file_type file type
5. name file name

 

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.