# File Descriptors: Part 2 In [[File Descriptors Part 1]], we looked at file descriptors as integer handles used by a process. This note goes one level deeper into what those handles point to. ## Prerequisites - Every process has a kernel task structure. - That task structure points to file-related state. - The process file descriptor table maps integers to open file descriptions. - A dentry maps a name inside a directory to an inode. ## Process File Descriptor Table Each process has a file descriptor table. The entries are indexed by integers such as `0`, `1`, `2`, and so on. Each entry points to a `struct file`, which represents an open file description. That distinction matters: the integer file descriptor is process-local, but the open file description contains shared state such as the current file offset and flags. ## Open File Descriptions A `struct file` stores important state for an open file: - Current offset, often represented as `f_pos`. - Status flags such as `O_APPEND`, `O_NONBLOCK`, `O_SYNC`, and `O_DIRECT`. - Access mode, represented in fields such as `f_mode`. Multiple file descriptors can point to the same open file description. This is why operations like `dup` can create two descriptors that share the same file offset. ## Dentries And Inodes The open file description references path state. That path state includes a mount and a dentry. The dentry references the inode. The inode does not store the path. Instead, it stores file metadata such as: - Permissions. - Owner. - Size. - Timestamps. - Device or superblock links. Paths are names. Inodes are file objects. Dentries connect names to inodes. ## Why This Matters This model explains behavior that otherwise feels surprising: - Why two file descriptors can share an offset. - Why deleting a file path does not always remove the underlying data immediately. - Why sockets, pipes, and files can all be addressed through file descriptors. - Why descriptor leaks are production reliability bugs. ## Next A natural next step is understanding `dup`, `dup2`, file offsets, and descriptor inheritance across process boundaries. ## Related - [[File Descriptors Part 1]] - [[Field Notes/File Descriptor]] - [[Topics/Linux & Systems]]