Directory item cache --- dentry
Linux uses the directory item cache (dentry cache) to quickly access the results of previous search operations,
[CPP]View plaincopy
- Struct dentry {
- Atomic_t d_count; // directory item object reference counter
- Unsigned int d_flags;/* protected by d_lock */
- Spinlock_t d_lock;/* per dentry lock */
- Struct inode * d_inode;/* Where the name belongs to-null is
- Struct hlist_node d_hash;/* link to the hash linked list of dentry cache */
- Struct dentry * d_parent;/* pointer to the parent dentry structure */
- Struct qstr d_name; // file name
- Struct list_head d_lru;/* LRU list */
- Union {
- Struct list_head d_child;/* child of parent list */
- Struct rcu_head d_rcu;
- } D_u;
- Struct list_head d_subdirs;/* is the head of the subitem linked list. The subitem may be a directory or a file. All subitems must be linked to this linked list ,*/
- Struct list_head d_alias;/* inode alias list */
- Unsigned long d_time;/* used by d_revalidate */
- Struct dentry_operations * d_op;
- Struct super_block * d_sb;/* the root of the dentry tree */
- Void * d_fsdata;/* FS-specific data */
- Int d_mounted;/* Indicates whether dentry is a mount point. If it is a mount point, this member is not 0.
- Unsigned char d_iname [dname_inline_len_min];/* Small names */
- };
Each directory item object has four states:
Idle status: the directory item object does not contain valid information and is not used by VFS.
Unused Status: Not used by the kernel. The value of d_count is 0, and the value of d_inode is still directed to the relevant index node.
Active Status: in use, the value of d_count is greater than 0, and the value of d_inode is directed to the relevant index node.
Negative status: The index node associated with the directory item does not exist, and the corresponding disk index node has been deleted.
The main purpose of the dentry structure is to establish an association between the file name and the relevant inode node. The three members of the dentry structure are used for this purpose.
D_inode is a pointer to the relevant inode instance.
D_name specifies the file name.
If the file name contains only a small number of characters, it is saved in d_iname to facilitate accelerated access. The short file name generally does not contain 16 characters.
Dentry Cache Organization:
Every request sent by VFS to the underlying implementation will lead to the creation of a new dentry object. These objects are stored in a cache and can be accessed more quickly as needed, the dentery in the memory mainly involves the following two parts:
(1) A hash contains multiple dentry objects, which are implemented by the dentry_hashtable array. Each array element is a pointer to a linked list, this kind of linked list is to form a hash of directory items with the same hash value.
(2) a lru (least recently used) linked list,
Role Analysis of dentry
A. The dentry of each file is linked to the dentry of the parent directory, forming the structure tree of the file system. For example, there is a directory/usr, that is, the d_child member of USR dentry is linked to the d_subdirs Member of the root directory/dentry. The sub-directories under the parent directory are searched through the hash table.
B. All dentry points to a dentry_hashtable. Dentry_hashtable is an array and the array members are the data structure of the hash linked list. If a file has been opened, the dentry structure of the file should exist in the memory, the dentry is linked to a hash linked list header in the dentry_hashtable array.
[File System] File System Study Notes (3) --- directory item cache dentry