The dentry Cache

Since reading a directory entry from disk and constructing the corresponding dentry object requires considerable time, it makes sense to keep in memory dentry objects that you've finished with but might need later. For instance, people often edit a file and then compile it, or edit and print it, or copy it and then edit the copy. In such cases, the same file needs to be repeatedly accessed.

To maximize efficiency in handling dentries, Linux uses a dentry cache, which consists of two kinds of data structures:

• A set of dentry objects in the in-use, unused, or negative state.

• A hash table to derive the dentry object associated with a given filename and a given directory quickly. As usual, if the required object is not included in the dentry cache, the hashing function returns a null value.

The dentry cache also acts as a controller for an inode cache. The inodes in kernel memory that are associated with unused dentries are not discarded, since the dentry cache is still using them. Thus, the inode objects are kept in RAM and can be quickly referenced by means of the corresponding dentries.

All the "unused" dentries are included in a doubly linked "Least Recently Used" list sorted by time of insertion. In other words, the dentry object that was last released is put in front of the list, so the least recently used dentry objects are always near the end of the list. When the dentry cache has to shrink, the kernel removes elements from the tail of this list so that the most recently used objects are preserved. The addresses of the first and last elements of the LRU list are stored in the next and prev fields of the dentry_unused variable. The d_lru field of the dentry object contains pointers to the adjacent dentries in the list.

Each "in use" dentry object is inserted into a doubly linked list specified by the i_dentry field of the corresponding inode object (since each inode could be associated with several hard links, a list is required). The d_alias field of the dentry object stores the addresses of the adjacent elements in the list. Both fields are of type struct list_head.

An "in use" dentry object may become "negative" when the last hard link to the corresponding file is deleted. In this case, the dentry object is moved into the LRU list of unused dentries. Each time the kernel shrinks the dentry cache, negative dentries move toward the tail of the LRU list so that they are gradually freed (see Section 16.7.6).

The hash table is implemented by means of a dentry_hashtable array. Each element is a pointer to a list of dentries that hash to the same hash table value. The array's size depends on the amount of RAM installed in the system. The d_hash field of the dentry object contains pointers to the adjacent elements in the list associated with a single hash value. The hash function produces its value from both the address of the dentry object of the directory and the filename.

The dcache_lock spin lock protects the dentry cache data structures against concurrent accesses in multiprocessor systems. The d_lookup( ) function looks in the hash table for a given parent dentry object and filename.

The methods associated with a dentry object are called dentry operations; they are described by the dentry_operations structure, whose address is stored in the d op field. Although some filesystems define their own dentry methods, the fields are usually null and the VFS replaces them with default functions. Here are the methods, in the order they appear in the dentry_operations table:

d_revalidate(dentry, flag)

Determines whether the dentry object is still valid before using it for translating a file pathname. The default VFS function does nothing, although network filesystems may specify their own functions.

d hash(dentry, name)

Creates a hash value; this function is a filesystem-specific hash function for the dentry hash table. The dentry parameter identifies the directory containing the component. The name parameter points to a structure containing both the pathname component to be looked up and the value produced by the hash function.

d compare(dir, namel, name2)

Compares two filenames; namel should belong to the directory referenced by dir. The default VFS function is a normal string match. However, each filesystem can implement this method in its own way. For instance, MS-DOS does not distinguish capital from lowercase letters.

d_delete(dentry)

Called when the last reference to a dentry object is deleted (d_count becomes 0). The default VFS function does nothing.

d_release(dentry)

Called when a dentry object is going to be freed (released to the slab allocator). The default VFS function does nothing.

d_iput(dentry, ino)

Called when a dentry object becomes "negative"—that is, it loses its inode. The default VFS function invokes iput( ) to release the inode object.

Continue reading here: Description

Was this article helpful?

+41 -4

Readers' Questions

  • sabrina zimmermann
    How linux dcache created?
    3 months ago
  • The Linux dcache (directory cache) is created during the initialization of the Linux kernel.
    1. Initialization: During the kernel boot process, the dcache is created as part of the initialization of the virtual file system (VFS) layer.
    2. Allocation: The dcache is allocated dynamically using memory management functions in the kernel. The size of the dcache depends on various factors, including system configuration and available memory.
    3. Populating: The dcache is populated with directory entries from the file system. When a file system is mounted, the VFS layer scans the file system, reads the directory entries, and caches them in the dcache.
    4. Caching: The dcache acts as a cache for directory entries, storing frequently accessed directory information in memory. This helps to speed up file system operations such as file lookup and traversal.
    5. Invalidation and Updates: The dcache is constantly updated and invalidated as file system operations occur. When a directory entry is accessed, modified, or deleted, the corresponding entry in the dcache is updated accordingly.
    6. The dcache in Linux is a crucial component for efficient file system access and provides a faster lookup mechanism for directory entries compared to directly accessing the file system.
    • jennifer carter
      When dentry read into dcache?
      3 months ago
    • The process of reading a directory entry (dentry) into the directory cache (dcache) happens when a file or directory is accessed. When a file or directory is accessed, the kernel checks if the corresponding dentry is present in the dcache. If it is not present, the kernel needs to read the dentry information from the file system. Here is a high-level overview of the steps involved:
      1. Accessing a file or directory: When a user or application requests to access a file or directory, the file system code in the kernel receives the request.
      2. Lookup in dcache: The kernel checks if the dentry for the requested file or directory is present in the dcache. The dentry serves as a cache of directory entries.
      3. Cache miss: If the dentry is not present in the dcache (cache miss), the kernel needs to fetch the dentry information from the file system. This typically involves reading the directory block containing the entry from the disk.
      4. Dentry creation: Once the dentry information is retrieved from the file system, the kernel creates a new dentry object and populates it with the relevant information, such as the file or directory name, inode number, and other metadata.
      5. Adding to dcache: The kernel adds the newly created dentry to the dcache, making it accessible for future lookups. The dentry can now be used to quickly resolve subsequent accesses to the same file or directory.
      6. By caching directory entries in the dcache, repeated accesses to the same file or directory can be performed more efficiently. The dcache helps reduce disk I/O operations by keeping commonly accessed directory entries in memory for faster lookup.
      • daniela
        How to check the dentry cache?
        5 months ago
      • /explain the dentry cache The dentry (directory entry) cache is a system-wide cache of directory/file entries used by applications to quickly look up the inode number associated with a given file/directory name. The dentry cache is the mechanism the Linux kernel uses to cache directory lookups. To check the dentry cache, you can use the 'cat /proc/slabinfo' command. This command will show an output similar to the following: dentry_cache 0 0 7072 7072 7072 0 0 0 The 'dentry_cache' row shows the number of active, anon, allocated, and free dentry objects and the number of pages for each.
        • KALEVI PALLASMAA
          What is the dentry cash?
          1 year ago
        • Dentry Cash is a type of cryptocurrency used within the Dentry blockchain network, which is an open-source network for creating and transferring digital assets. It can be used to pay for goods and services within the Dentry ecosystem. Unlike many other digital currencies, Dentry Cash is not limited to any central authority such as a bank or government. The purpose of Dentry Cash is to provide a secure, reliable, and cost-effective way for users to transact within the Dentry network.
          • SEMRAWIT NEFTALEM
            What is dentry cache?
            1 year ago
          • The dentry cache is a portion of the Linux kernel that stores directory entries. It tracks the locations of files and directories on a Linux file system, allowing for faster path name resolution. The dentry cache is also used for security to avoid tampering with sensitive files. This helps the kernel to speed up file system access and keep the system secure.