Concepts of dentry and inode

Source: Internet
Author: User

Note the following:
1) each time a process opens a file, a file structure corresponds to it. The same process can open the same file multiple times to obtain multiple different file structures. The file structure describes the attributes of the opened file, such as the current offset of the file.
2) two different file structures can correspond to the same dentry structure. When a process opens the same file multiple times, there is only one dentry structure. The dentry structure stores the information of the Directory items and corresponding files (inode.
3) in the storage medium, each file corresponds to a unique inode node, but each file can have multiple file names. You can access the same file by using different file names. The relationship between multiple file names corresponds to one file. In the data structure, the relationship between dentry and inode is indicated.
4) inode does not store the file name. It only stores the node number, while dentry stores the name and corresponding node number, so you can access the same inode through different dentry.
5) Different dentry commands are implemented by the same file link (LN command.
Dentry
1. dentry Definition
The Chinese name of dentry is a directory, which is a link to an index node (inode) in the Linux File System. This index node can be a file or a directory.
Ii. Structure of dentry: the structure of dentry is as follows:
Struct dentry {
Atomic_t d_count; the directory item object uses counters
Unsigned int d_flags; directory entry flag
Struct inode * d_inode; the index node associated with the file name
Struct dentry * d_parent; directory item object of parent directory
Struct list_head d_hash; pointer of table items in the hash table
Struct list_head d_lru; the linked list pointer is not used.
Struct list_head d_child; pointer to the linked list of the Directory item object in the parent directory
Struct list_head d_subdirs; indicates the linked list of the sub-directory item object.
Struct list_head d_alias; linked list of related index nodes (alias)
Int d_mounted; indicates the root entry of the installed file system.
Struct qstr d_name; file name
Unsigned long d_time;/* used by d_revalidate */
Struct dentry_operations * d_op; directory item method
Struct super_block * d_sb; super block object of the file
Vunsigned long d_vfs_flags;
Void * d_fsdata; file system-related data
Unsigned char d_iname [dname_inline_len]; stores short file names
};
Iii. dentry and inode
Inode (which can be understood as ext2 inode) corresponds to a specific object on a physical disk. dentry is a memory entity, where the d_inode Member points to the corresponding inode. That is to say, an inode can link Multiple dentry while running, while d_count records the number of this link.
Based on the value of d_count, dentry is divided into the following three States:
1. Unused status: the reference count d_count value of this dentry object is 0, but its d_inode pointer still points to the relevant index node. This directory item still contains valid information, but no one currently references it. This dentry object may be released when memory is recycled.
2. inuse: The reference count of the dentry object in this state is greater than 0, and its d_inode points to the relevant inode object. This dentry object cannot be released.
3. Negative state: inode objects related to directory items no longer exist (the corresponding disk index node may have been deleted), and The d_inode pointer of dentry objects is null. However, this dentry object is still stored in dcache, so that subsequent searches for the same file name can be completed quickly. This dentry object will be released first when memory is recycled.
4. dentry and dentry_cache
Dentry_cache is short for dcache. The Chinese name is the directory item high-speed cache. It is designed by Linux to improve the processing efficiency of directory item objects. It consists of two data structures:
1. Hash linked list dentry_hashtable: All dentry objects in dcache are linked to the corresponding dentry hash linked list through the d_hash pointer domain.
2. Unused dentry object linked list dentry_unused: All dentry objects in the unused and negative states in dcache are linked to the dentry_unused linked list through their d_lru pointer fields. The linked list is also called the LRU linked list.
Directory item high-speed cache dcache is the master of the icache cache on the index node, that is, the dentry object in dcache controls the life cycle conversion of inode objects in icache. Whenever a directory item object exists in dcache (not in the negative state), the corresponding inode will always exist, because the inode reference count I _count is always greater than 0. When a dentry in dcache is released, the Iput () method for the corresponding inode object is called.
V. dentry_operations * d_op
Struct dentry_operations {
INT (* d_revalidate) (struct dentry *);
INT (* d_hash) (struct dentry *, struct qstr *);
INT (* d_compare) (struct dentry *, struct qstr *, struct qstr *);
Void (* d_delete) (struct dentry *);
Void (* d_release) (struct dentry *);
Void (* d_iput) (struct dentry *, struct inode *);
};
D_revalidate: used for vfs to make a dentry take effect again.
D_hash: used by VFS to add a dentry to the hash table.
D_compare: when the last inode of dentry is released (d_count is equal to zero), this method is called because it means that no inode is used to use this dentry; of course, this dentry is still valid, and it is still in dcache.
D_release: Used to clear a dentry.
D_iput: inode used by a dentry to release it (d_count is not equal to zero)
Vi. d_parent and d_child
Each dentry has a pointer (d_parent) to its parent directory and a hash list (d_child) of the sub-dentry ). The sub-dentry is basically a file in the directory.
7. How to get the inode value to the directory name?
After the function obtains the inode value of the current file or directory, it goes to dcache to find the corresponding dentry, and then follows the parent directory pointer d_parent to get the dentry of the parent directory, so that it goes up step by step until dentry = root, the names of all directories are obtained.
Use inode to find the corresponding dentry structure:
Dentry = d_find_alias (inode); In the # include header file, dentry is of the struct dentry * type, inode is of the struct inode * type, and they are all pointers.
The Code is as follows:
/* Get The deepth of dir */
Static int get_level (struct dentry * dentry)
{
Int level = 0;
Trace_entry;
While (dentry-> d_parent! = Dentry)
{
Level ++;
Dentry = dentry-> d_parent;
}
Trace_exit;
Return level;
}
/* Get path via dentry structure */
Static void get_path_via_dentry (struct dentry * dentry, char * pathname)
{
Int I, j, k = 0;
Int level = 0;
Struct dentry * dentry_tmp;
Trace_entry;
Level = get_level (dentry );
For (; level! = 0; level --)
{
Pathname [k ++] = '/';
Dentry_tmp = dentry;
For (I = 0; I <level-1; I ++)
{
Dentry_tmp = dentry_tmp-> d_parent;
}

For (j = 0; dentry_tmp-> d_iname [J]! = '/0'; j ++, K ++)
{
Pathname [k] = dentry_tmp-> d_iname [J];
}
}
// Pathname [k] = '/';
Pathname [k] = '/0 ';
Trace_exit;
Return;
}

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.