In this lesson, you will learn how the kernel uses dentry objects to map human-readable file paths to internal filesystem locations.
The Role of the Directory Entry (dentry)
When you ask the system to open a file like /etc/passwd, the kernel does not search the entire disk. Instead, it relies on the Virtual Filesystem (VFS) and a specialized structure called a dentry (directory entry).
A dentry acts as a "bridge" between the name of a file and its corresponding inode. Crucially, to make path resolution fast, the kernel maintains a dentry cache in memory. This cache holds the most recently accessed directory entries so the kernel doesn't have to perform slow I/O operations to read the directory content from the disk repeatedly.
Tracing the Path
Path traversal is the process of breaking down a string path into individual components. Imagine you access /home/user/docs. The kernel performs a step-by-step lookup:
- Start at Root: The kernel locates the
dentryfor the root directory/. - Component Lookup: It looks inside the
/dentry to find the entry forhome. - Traversal: It moves to the
homedentry, then searches that entry foruser. - Final Resolution: Finally, it finds the
docsentry withinuser, which provides theinodenumber needed to access the actual data.
If a path component is already in the dentry cache, this process is nearly instantaneous. If it is not, the kernel must use the filesystem driver to read the directory data from the physical disk, create a new dentry object, and store it in the cache for future use.
# Example: When you run 'ls /etc/fstab'
# 1. Kernel checks cache for '/' -> finds dentry
# 2. Kernel checks cache for 'etc' -> finds dentry
# 3. Kernel checks cache for 'fstab' -> finds dentry
# 4. Kernel retrieves the inode from the dentry to display metadata
By caching these associations, the kernel transforms a complex hierarchical path into a simple series of memory lookups.
Key Takeaway: Path traversal is the process of resolving a string path into a specific inode by iteratively matching name components against the kernel's memory-resident dentry cache.