FullStack AI Hub
Studio
lesson depth
Mastery
not started · 0%

Linux File Systems & Inodes

ext4 file system layout, inodes, directory entries, hard/soft links, and buffer page cache.

Freshness: current•15 min read•Computer Science and Programming

Key Learning Outcomes

  • ✓Understand inode allocations and directory metadata structures
  • ✓Optimize disk I/O with page cache flush settings
Full Technical Reference Document

Mental model

POSIX/Linux cleanly splits a file's human name, metadata, and disk bytes across three separate structures: Directory Entries (dentries) simply map filenames onto integer Inode (Index Node) IDs; the Inode stores permissions (mode), owner (uid), link counts (nlink), timestamps, and physical disk Data Block / Extent pointers (containing zero filename!); and actual file payloads reside inside ext4 / XFS data blocks cached transparently through kernel Page Cache.

Directory dentry maps filename -> Inode #
Kernel loads Inode metadata & nlink
Ext4 extent tree resolves block offsets
Read/write hits kernel Page Cache
unlink clears dentry; blocks free when nlink=0 & open_fds=0
Conceptual teaching model synthesized from:Linux Kernel Kernel.org Official Architecture & Systems DocumentationSite Reliability Engineering

Learning outcomes

  • Trace file lookups and deletion rules cleanly across dentries, inodes (st_nlink + open kernel FD refcounts), and ext4 Extent trees (faster than legacy indirect block maps).
  • Diagnose puzzling "No space left on device (ENOSPC)" failures cleanly when df -h shows 20% used (inode exhaustion df -i 100% or deleted open files held by running workers!).
  • Choose properly cleanly between ext4 (static format-time inode table, ultra-reliable journaling) and XFS (dynamic runtime inode allocation, parallel allocation groups for huge DB arrays).

Theory

Two legendary Linux operations mysteries confuse SREs and ML platform engineers alike until seeing the Inode link reference formula:

Blocks Freed on Disk  ⟺  (inode.nlink==0)  ∧  (kernel_open_fd_count==0)\text{Blocks Freed on Disk} \iff (\text{inode.nlink} == 0) \;\land\; (\text{kernel\_open\_fd\_count} == 0)

  1. "Disk 100% Full (df -h), Yet Large File Delete Freed Zero Bytes (du shows tiny!)": An engineer ran rm /var/log/huge.log cleanly while a background worker still held that log file open! Because rm calls unlink(), deleting solely the directory dentry (dropping st_nlink -> 0), the kernel preserves 100% of disk data blocks cleanly intact until the worker process closes that FD or restarts (lsof +L1 reveals all deleted-yet-held zombie blocks instantaneously!).
  2. "ENOSPC Write Failures With 200 GB Free (df -h at 30%)": Writing millions of tiny cache shards or chunk artifacts exhausted ext4's fixed format-time inode table (df -i = 100%) completely cleanly with plenty of raw disk blocks idle!
Filesystem PrimitiveKey PropertyPractical ImplicationDiagnostic Utility
Hard Link (ln a b)New dentry pointing directly cleanly to identical Inode # (st_nlink++)Cannot cross distinct filesystem mounts or link directories; deleting a preserves b & data cleanlyUsed cleanly in Nix/Docker layered deduplication (content stores)
Symbolic Link (ln -s a b)Independent tiny Inode holding literal pathname target stringWorks across mounts/dirs cleanly; dangles (ENOENT) if a moves; beware symlink traversal attacks!Clean deployment slot swaps (/current -> /releases/v2)
ext4 Journaling (data=ordered)Metadata journal (JBD2) writes data blocks cleanly ahead of metadata commitProtects filesystem tree integrity cleanly after crashes; fixed df -i inode table set at mkfs.ext4Standard root / boot / container overlay volumes
XFS (Allocation Groups)Dynamically allocates fresh inodes anywhere free space exists; parallel AG locksAvoids df -i pre-exhaustion & scales multi-thread concurrent DB writers smoothlyIdeal cleanly for huge petabyte NVMe data lakes & databases
bash
#!/usr/bin/env bash
set -euo pipefail
​
# 1. Diagnose BOTH block bytes (`df -h`) AND fixed inode table exhaustion (`df -i`)
df -h /var/lib/data
df -i /var/lib/data
​
# 2. Find deleted ("unlinked") huge files still pinned silently on disk by open running PIDs!
lsof -nP +L1 | head -n 20
​
# Inspect exact inode number, extent link count (Links: N), and block count
stat /etc/hosts

Trade-offs

Deploying default ext4 (data=ordered) delivers mature crash consistency, instant lightweight checks (e2fsck), and strong mixed-file reliability cleanly across container hosts—provided teams avoid storing millions of tiny un-batched 1 KB files without tuning -i inode ratios upfront at mkfs (or packaging artifacts inside SQLite / Parquet / tar shards cleanly). Switching high-IOPS database volumes cleanly onto XFS gains lock-sharded Allocation Groups and dynamic inode scaling (never running out of df -i while bytes remain free), trading slightly heavier shrink limitations (XFS volumes cannot shrink offline cleanly, only expand!).

Failure modes and misconceptions

  • Truncating vs Deleting Held Open Logs (rm app.log): Removing a live active log without sending SIGUSR1 logrotate reopen (copytruncate or logrotate postrotate) hides the filename cleanly from ls/du while filling root storage straight to 100% crash!
  • Exhausting Fixed ext4 Inodes With Small Un-Batched Vector/Eval Shards: Dumping 4 million tiny JSON chunks directly onto default ext4 (1 inode per 16 KB formatted ratio) hits ENOSPC cleanly at 5% byte occupancy cleanly.
  • Confusing Inode Deletion Guarantees Across Hard Links (Docker layers): Deleting one tag/manifest path leaves shared underlying inode blocks intact cleanly whenever sibling hard links preserve nlink >= 1.
  • Ignoring stat() / Symlink Race Attacks (O_NOFOLLOW): Writing temp files cleanly without resolving symlinks or checking O_NOFOLLOW exposes privileged services cleanly to arbitrary file overwrites via malicious symlink drops cleanly.

Decision scenario

An embeddings staging server fails Kubernetes pod scheduling with No space left on device, yet df -h reads only 44% used! Checking df -i exposes 100% inode usage from 8.4 million tiny orphaned token cache JSONs on ext4, while another volume shows 99% df -h with du reporting just 12 GB due to a deleted-but-open uvicorn log file! Pack small cache fragments cleanly inside SQLite / RocksDB stores (or XFS), clean dangling caches, verify active deleted handles via lsof +L1 + signal reopen (logrotate), and add df -i alongside df -h alerts cleanly!

Reflect before revealing the guide

Prerequisites & Related Concepts (2)

Private notes

0 words
Recommended Free Track
Free · Verifiable Credential

FS-102LLM Systems Foundations

Build a connected mental model from tokens through retrieval evaluation security and operations.

24 concepts~18h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.

Next