Course contentsShow
← Linux
Lesson 277 of 1,8193. The Filesystem Hierarchy and POSIX StandardsFree lesson

Directory Entries and Path Traversal

Trace the lookup process of file paths by navigating through dentry objects in the kernel cache.

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:

  1. Start at Root: The kernel locates the dentry for the root directory /.
  2. Component Lookup: It looks inside the / dentry to find the entry for home.
  3. Traversal: It moves to the home dentry, then searches that entry for user.
  4. Final Resolution: Finally, it finds the docs entry within user, which provides the inode number 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.