Highlights

Contents

Part IX
09

Filesystems

A file name is not the file bytes. Directories resolve names to inode metadata, allocation maps logical offsets to blocks, and caches delay storage work. We will follow descriptors, links, allocation, page cache and journaling before returning to the server trace.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1OS fundamentals2Processes3Fork, exec and threads4Synchronization5Deadlocks6CPU scheduling7Paging and memory8Copy-on-write9Filesystems10I/O and IPC11Protection and Linux12Complete server trace

#Section 36Filesystems and path lookup

A path lookup starts with a directory entry that maps a name to an inode number. The lookup reaches filesystem metadata that identifies the object and its data blocks. Directories associate names with object identities, so the same object can have several names.

The distinction lets unlink remove a name while open descriptors still refer to the underlying object. The data and inode can be reclaimed only when no directory link and no open reference remains. File types include regular files, directories, sockets, pipes and symbolic links, each with different operations.

A DIRECTORY NAME LEADS TO AN INODE, THEN TO DATA BLOCKSA DIRECTORY NAME LEADS TO AN INODE, THEN TO DATA BLOCKSdirectorynotesinode Aserver.loginode Bconfiginode Cinode Bowner / modesize / timesblock pointersbytesbytesbytesthe name and the file contents live in different structures
Figure 3.48Illustrative lookup path moves from filename through directory entry and inode to data blocks. Orange marks the inode because metadata and data references meet there.

Directory traversal checks each component under the filesystem’s lookup and permission rules. The final inode describes a regular file, directory or another supported object; its block references identify the stored data for that object. Renaming changes a directory relationship rather than copying every data byte to a new inode. Keeping names separate from identity is what makes open references meaningful across a rename.

Descriptors provide a common handle interface, not common persistence semantics. A log involves filesystem storage; a socket involves transport queues; a pipe has a kernel buffer.

#Section 37File descriptors

open returns a small integer index in the process’s descriptor table. Descriptor 0 is standard input, 1 standard output and 2 standard error by convention. A new file or socket can occupy descriptor 3. The descriptor refers to kernel open-file state, which includes access mode and current offset where relevant.

Sockets, pipes, terminals and regular files therefore share a handle shape even though their readiness and storage behavior differ. A forked child inherits descriptor references, and a shell uses that fact to connect pipeline endpoints. A close-on-exec flag prevents accidental inheritance into a replaced program.

LOCAL DESCRIPTORS CAN REFER TO ONE SHARED OPEN FILE DESCRIPTIONLOCAL DESCRIPTORS CAN REFER TO ONE SHARED OPEN FILE DESCRIPTIONprocess Aprocess B01230123open fileoffsetflagsinodefork / dup can share the offset
Figure 3.49Illustrative shared open-file state. Process-local descriptors can refer to one open file description after fork or dup, sharing its offset and flags while the inode identifies the file.

The integer is local to a process, so descriptor 3 in another process can name a different resource. Duplicating a descriptor can create another reference to the same open-file description, and inherited references after fork can share its current offset. Closing one reference does not automatically close the object while other references remain. This distinction matters for shell pipes and concurrent file access.

Listeners, logs, and event handles consume the descriptor budget too. A recycled integer can name a different object, so delayed callbacks must preserve connection identity.

#Section 38Inodes, hard links and symbolic links

Inodes

An inode stores file type, permissions, owner, size, timestamps, link count and references to data blocks. It usually does not store the filename. The directory owns the name-to-inode mapping, which is why one inode can have several names.

A hard link is another directory entry for the same inode. Removing the original name does not remove the data while another hard link or open reference remains. A symbolic link is a separate file containing a path, so it can become dangling if the target name disappears or moves.

Hard links normally cannot cross filesystem boundaries because the inode belongs to one filesystem. Symbolic links can point across such boundaries because they store a path. Relative symbolic links resolve relative to the link’s directory, which makes moving the containing tree part of the correctness analysis.

A hard link names the same object. A symbolic link resolves another path, which can later refer to a replacement. Identical initial reads do not imply identical rename or deletion behavior.

Unlinking the last name does not reclaim bytes while an open reference remains. Deleted logs can therefore occupy storage until closure. Diagnose path identity and open lifetime separately.

#Section 39Allocation, cache and journaling

Filesystems allocate blocks with contiguous, linked or indexed strategies. Inode-based allocation uses metadata references to locate file ranges, while free-space structures track available blocks. Contiguous extents help sequential access but can fragment as files grow.

The page cache keeps file data in RAM and marks changed pages dirty. A write returning to an application does not automatically mean the storage device has durable bytes. A filesystem journal records metadata intent and ordering so crash recovery can restore a consistent filesystem state. Database WAL solves a related durability problem at another layer.

A FILE READ: INODE POINTERS FIND BLOCKS, THE PAGE CACHE KEEPS A RAM COPY, DIRTY PAGES FLUSH LATERA FILE READ: INODE POINTERS FIND BLOCKS, THE PAGE CACHE KEEPS A RAM COPY, DIRTY PAGES FLUSH LATERinodeptr 0 → 17ptr 1 → 42ptr 2 → 43disk blocks (bitmap marks free)page cache in RAMpage 0page 1page 2page 1 dirtyread()repeat reads come from RAM; fsync() forces the flush
Figure 3.51Illustrative logical file blocks pass through inode pointers, page cache and dirty writeback. Orange marks cached pages; dirty writeback still needs the stated persistence contract.
THE JOURNAL WRITES THE PLAN FIRST, SO A CRASH CAN REPLAY OR DISCARD ITTHE JOURNAL WRITES THE PLAN FIRST, SO A CRASH CAN REPLAY OR DISCARD ITjournal (append-only)begininode 1207bitmapdir entrycommithome locationsinode tablefree bitmapdirectory1write intent to journal2flush journal, then commit record3update metadata in place4checkpoint: journal space freedcrash before commit: discard the partial entrycrash after commit: replay it into the home locations
Figure 3.52Illustrative journal records intent before metadata and later checkpoints it. Orange marks the initial intent; persistence and metadata changes follow in order.

Allocation and page cache

Linked allocation follows each block to the next and makes random lookup expensive. Indexed allocation uses a lookup structure so an offset can identify its block more directly. Contiguous allocation and extents improve sequential placement but need a policy for growth and free-space fragmentation. The physical layout is independent of whether application bytes currently reside in the page cache.

Journaling and persistence

Buffered writes may only dirty cache. Persistence needs the promised synchronization boundary; journaling preserves its declared scope, not every application byte or the database’s transaction invariant.