4.2.35 · D2 · HinglishOperating Systems

Visual walkthroughext4 structure — superblock, block groups, inodes

2,319 words11 min read↑ Read in English

4.2.35 · D2 · Coding › Operating Systems › ext4 structure — superblock, block groups, inodes

Hum sab kuch zero se banate hain. Agar aapne kabhi bit, byte, ya floor bracket nahi dekha, Step 1 se shuru karo aur skip mat karo.


Step 1 — Ek disk blocks ka flat array hai

KYA. Poori disk ko ek lambi row of identical boxes ki tarah socho. Har box ek block hai — storage ka ek fixed chunk, commonly bytes (yaani KiB). Hum inhe number karte hain.

KYO. Hardware humein koi meaning nahi deta — na "files", na "free space", bas numbered boxes. ext4 jo bhi clever kaam karta hai wo in boxes ke baare mein notes likhne ka tarika hai — kuch boxes ke andar hi. Toh pehli picture bas ye naked row hai.

PICTURE. Neeche di gayi row dekho. Box ke neeche label uska block number hai. Abhi kuch nahi keh raha ki kaunse boxes use ho rahe hain — yahi wo problem hai jo hume solve karni hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 2 — Sawaal: "kaunse boxes free hain?"

KYA. ext4 koi file store karne se pehle, use jaanna hai ki kaunse blocks already liye hue hain. Hume har block ke liye ek compact yes/no answer chahiye: used () ya free ().

KYO bitmap, list nahi? Hum free block numbers ki ek list rakh sakte the, lekin numbers ki list bulky aur search karne mein slow hoti hai. Iske badle hum ek bitmap use karte hain: ek bit per block. Bit ek chhota sawaal answer karta hai — "kya block use ho raha hai?" Ye sabse sasta possible per-block record hai: exactly ek switch har ek ke liye. (General technique ke liye Bitmaps for allocation dekho.)

PICTURE. Neeche, Step 1 ke har block ke upar ek bit baitha hua hai. Ek magenta bit () = used, ek pale bit () = free. Bits ki ye strip hi bitmap hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 3 — Bitmap ko kahan store karein? Ek block ke andar.

KYA. Bitmap bhi data hai, toh use blocks mein rehna hoga. ext4 ek clean choice karta hai: block bitmap exactly EK block hai.

KYO exactly ek? Do reasons, dono simplicity aur speed ke baare mein:

  1. Ek block ek read hai — OS ek hi disk access mein ek region ka poora "free map" grab kar leta hai.
  2. Fixed-size bitmap ka matlab hai geometry predictable hai: har region identical dikhta hai, toh mkfs cheezein search karne ki bajay arithmetic se place kar sakta hai.

PICTURE. Hum ek block (orange) lete hain, use blow up karte hain, aur dikhate hain ki ye bits ka ek grid hai — kyunki ye bytes ka hai aur har byte bits ka hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 4 — Killer constraint → blocks per group

KYA. Ab Step 2 aur Step 3 combine karo. Bitmap blocks track karta hai ( bits chahiye). Bitmap hai ek block ( bits hold karta hai). Ek bit kisi aisi block ko describe nahi kar sakta jiske liye jagah nahi, toh:

Hum sabse bada region lete hain jo ek bitmap fully cover kar sakta hai, toh hum set karte hain . Wo region ek block group hai.

KYO maximum set karo? Bade groups = kam groups = kam repeated metadata aur file ki apni map ke saath better locality. Lekin hum exceed nahi kar sakte, kyunki bit number block mein simply exist nahi karta. Toh maximum woh sweet spot hai jo constraint hume free mein deta hai.

PICTURE. Left mein, bits wala ek bitmap block. Usme se arrows fan out hote hain, har bit exactly ek data block ko point karta hai right mein blocks ke ek region mein. Har arrow land karta hai; koi leftover nahi. Wo region ek block group hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 5 — Real number plug in karo

KYA. Common bytes substitute karo.

KYO. Formula abstract hai; ek smart 12-saal ke bacche ko apni khud ki disk par actual megabytes dikhne chahiye.

Ab utne blocks ko bytes mein convert karo:

PICTURE. Ek single block group scale ke saath ek labelled bar ki tarah draw kiya gaya: front mein ek patla orange bitmap slice, phir ek patla inode-bitmap slice, phir inode table, phir data blocks ka wide band — poore bar par "128 MiB" tag laga hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 6 — Disk ko groups se tile karna (aur leftover case)

KYA. Poori disk bas yahi -MiB groups end to end laaye hue hai. Ek GiB disk ke liye:

KYO division. Total capacity ÷ group size = kitne groups fit hote hain. Har numerator/denominator MiB unit carry karta hai, jo cancel ho jaati hai, ek pure count rehta hai.

EDGE CASE — disk poora multiple nahi hai. Real disks rarely evenly divide hoti hain. Agar last group partial hogi, toh ext4 use ek short final group ki tarah rakhta hai: uski bitmap mein phir bhi bits hote hain lekin extra bits (un blocks ke liye jo exist nahi karte) permanently used () mark ho jaate hain toh allocator unhe kabhi nahi deta.

PICTURE. Groups G0…G3 equal orange bars ki tarah dikhaye gaye hain, phir ek chhota final bar jiska non-existent tail hatched hai aur "bits forced to 1 — never allocated" mark kiya gaya hai.

Figure — ext4 structure — superblock, block groups, inodes

Step 7 — Inodes ke liye same idea → group-of-an-inode formula

KYA. Inodes pre-numbered records hain (). Ye bhi groups mein evenly split hote hain: har group mein inodes ka ek fixed count. Ye jaanne ke liye ki inode kis group mein rehta hai:

KYO 1 subtract karo? Inode numbers 1-based hain (wo se start hote hain), lekin ek group ki table ke andar positions 0-based hain (wo se start hote hain). counting-from-one se counting-from-zero mein shift karta hai. Floor bracket ka matlab hai "fractional part phenko" — ye answer karta hai "inode tak pahunchne se pehle kitne poore groups fit hote hain?"

KYO floor aur round nahi? Rounding ek boundary inode ko agale group mein dhakkel sakta hai. Floor kabhi overshoot nahi karta: ye aapko us group mein land karata hai jo actually contain karta hai.

PICTURE. Inodes ki ek number line ke groups mein cut ki gayi hai. Inode ek segment ke andar baitha hai; ek labelled bracket start se count kiya hua dikhata hai, aur floor arrow group index par snap down karta hai.

Figure — ext4 structure — superblock, block groups, inodes

Ek-picture summary

Upar sab kuch ek hi chain hai: ek byte 8 bits hai → ek block bits hold karta hai → ek bitmap block sirf blocks track kar sakta hai → toh ek block group hai blocks → unhe disk par tile karo → inodes ko same tarah number karo aur kisi bhi ek ko floor-divide se locate karo. Neeche ki figure us poore logical waterfall ko ek single flow mein compress karti hai.

Figure — ext4 structure — superblock, block groups, inodes

1 byte = 8 bits

1 block = B bytes = 8B bits

block bitmap = 1 block

can track only 8B blocks

block group = 8B blocks

4KiB gives 32768 blocks = 128 MiB

tile groups across disk

inodes split I per group

group of N = floor of N minus 1 over I

Recall Feynman retelling — apne plain words mein bol ke dikhao

Ek disk bas ek bahut badi shelf hai identical boxes ki, har box ek bytes ka block. Inhe use karne ke liye, ext4 ko ek checklist chahiye ki kaunse boxes full hain. Wo checklist har box ke liye ek bit ki tarah likhti hai — full ya empty — aur poori checklist ek single box mein thuns deti hai. Lekin ek box sirf bits hold karta hai, toh ek checklist sirf boxes track kar sakti hai. Isliye exactly ek block group blocks = MiB hai: checklist itne se zyada nahi dekh sakti. Poori disk ko inhi -MiB chunks mein kaato; ek leftover partial chunk bas apne imaginary boxes ko "full" mark kar deta hai taki koi unhe use na kare. Inodes (chhote record cards, ek per file) se number kiye gaye hain aur same even tarike se split hote hain, toh karta number kis chunk mein rehta hai ye jaanne ke liye subtract karo (kyunki cards se count karte hain lekin slots se) aur cards-per-chunk se divide karo, fraction phenko. Bas yahi poori geometry hai — ek bitmap box hi har cheez ka size decide karta hai.

Recall Quick self-test

Blocks per group formula, words mein? ::: Ek bitmap block bits hold karta hai, har bit ek block track karta hai, toh ek group blocks ka hai. 4 KiB blocks ke liye 128 MiB kyun? ::: blocks, times bytes MiB. Inode-group formula mein kyun? ::: Inodes se count karte hain, table slots se; unke beech convert karta hai. Partial final group ka kya hota hai? ::: Non-existent blocks ke uske bitmap bits (used) force ho jaate hain toh allocator unhe skip kar deta hai.

Ye bhi dekho: Operating Systems · ext2 vs ext3 vs ext4 · Hard links vs Symbolic links · Virtual File System · Journaling · Disk Scheduling · Memory Management.