4.2.35Operating Systems

ext4 structure — superblock, block groups, inodes

2,249 words10 min readdifficulty · medium1 backlinks

WHY does ext4 need structure at all?

WHY block groups? On a spinning disk, jumping the head far away is slow. If a file's metadata (inode) and its data blocks live in the same region, the head barely moves. ext4 keeps that locality idea even on SSDs because it also reduces fragmentation and keeps allocation bitmaps small.


The three layers

1. Superblock — "the filesystem's ID card"

The first 1024 bytes are reserved (boot sector), so the primary superblock lives at byte offset 1024.

2. Block group — "a self-contained mini-filesystem"

Figure — ext4 structure — superblock, block groups, inodes

3. Inode — "everything about a file except its name"


HOW a path resolves to data (the full chain)

To read /home/ada/x.txt:

  1. Inode 2 is always the root /. Read its data block → directory listing → find home → inode #.
  2. Read that inode → its data → find ada → inode #.
  3. Read that inode → find x.txt → inode #.
  4. Read that inode → its extents → physical data blocks → bytes.

Worked examples


Common mistakes


Flashcards

What does the superblock store?
Global filesystem info: total blocks/inodes, block size, blocks-per-group, free counts, UUID, mount count, magic number 0xEF53.
Why are there backup superblocks?
The superblock is critical metadata; if the primary (offset 1024) is corrupted, fsck restores from backups stored in selected block groups.
Why does ext4 split the disk into block groups?
To keep metadata near its data (locality/less seek), bound bitmap size, and reduce fragmentation.
What does the block bitmap track and how big is it?
One bit per block in the group, used/free; it is exactly one block, so a group holds 8B blocks (B = block size in bytes).
Blocks per group for 4 KiB blocks?
8 × 4096 = 32768 blocks = 128 MiB per group.
What is an inode and what does it NOT contain?
A fixed record (256 B) with a file's metadata + data-block locations; it does NOT contain the filename.
Where is the filename stored?
In the directory entry, which maps name → inode number.
What makes hard links possible?
Multiple directory entries pointing to the same inode number; the inode has a link count, data freed only at count 0.
What are ext4 extents and why are they better?
A record mapping a contiguous range of logical blocks to physical blocks; far more compact/faster than ext2/3 indirect pointers for large files.
Which inode is the root directory?
Inode 2 (inodes 1–10 reserved).
Group of inode N with I inodes/group?
floor((N-1)/I); subtract 1 because inode numbers are 1-based.
What does df -i show?
Inode usage (a separate fixed pool from disk space), set at mkfs time.

Recall Feynman: explain to a 12-year-old

Imagine a huge parking lot (the disk) split into identical chunks of spaces (block groups). At the entrance there's a sign (superblock) saying how big the lot is and the rules. Each chunk has a little checklist (bitmap) showing which spaces are taken. Every car (file) has an ID card (inode) that says who owns it, when it parked, and exactly which spaces it uses — but the card doesn't have the car's nickname. The nicknames are in a phone book (directory) that says "Ada's car → ID card #4732." That's why one car can have two nicknames: two phone-book lines, one ID card.

Connections

  • Operating Systems — filesystems sit between the Virtual File System layer and the block device.
  • ext2 vs ext3 vs ext4 — extents and journaling are the big ext4 upgrades.
  • Hard links vs Symbolic links — explained by name/inode separation.
  • Disk Scheduling — block-group locality reduces seeks.
  • Bitmaps for allocation — same trick used in Memory Management free-frame tracking.
  • Journaling — ext4's crash consistency layer above this structure.

Concept Map

structured by

split into

described by

holds block size, counts, magic 0xEF53

replicated as

reduces seek via locality

contains

contains

contains

caps size to 8B blocks

stores

points to

Raw block device N blocks

ext4 filesystem

Block groups

Superblock

Block bitmap 1 block

Inode bitmap

Inode table

Data blocks

Inode per file

Superblock backups

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek disk basically blocks ka ek bada array hai — bas numbered boxes, 0 se N-1 tak. Inme khud se koi "file" ya "free space" ka concept nahi hota. ext4 ka kaam hai is flat array pe structure dalna. Sabse pehle wo poori disk ko barabar block groups me kaat deta hai. Kyun? Taaki kisi file ka metadata aur uska data paas paas rahe — disk head ko zyada ghoomna na pade (locality), aur fragmentation kam ho. Har group ek mini-filesystem jaisa hota hai: ek block bitmap (kaun se block free hain), ek inode bitmap, inode table, aur phir actual data blocks.

Superblock filesystem ka ID card hai — total blocks, total inodes, block size, blocks-per-group, UUID, aur magic number 0xEF53. Ye itna important hai ki agar corrupt ho gaya toh kuch nahi milega, isliye ext4 iski backup copies rakhta hai different groups me. Block group ki size ek hi cheez se decide hoti hai: block bitmap exactly ek block ka hota hai, aur ek block (4KiB) me 8×4096 = 32768 bits hote hain, toh ek group me 32768 blocks = 128 MiB.

Ab inode — ye file ke baare me sab kuch rakhta hai: permissions, owner, size, timestamps, aur sabse important, data blocks kahan hain (ext4 me extents se, jo contiguous range ko ek hi record me map karte hain — fast aur compact). Lekin ek twist: inode me filename nahi hota! Naam directory me hota hai jo name ko inode number se map karta hai. Isi wajah se hard links possible hain — do naam, ek hi inode, inode pe ek link count.

Yaad rakhne ki baat: space aur inodes alag-alag pools hain. Lakhon chhoti files banao toh inodes khatam ho sakte hain bhale space bacha ho — df -i se check karo. Aur math karte waqt inode number 1-based hota hai, toh group nikaalte waqt hamesha (N-1) use karo, warna boundary pe galti ho jaayegi.

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections