4.2.35 · D3 · HinglishOperating Systems

Worked examplesext4 structure — superblock, block groups, inodes

3,949 words18 min read↑ Read in English

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


The scenario matrix

Ext4 geometry ke baare mein har question actually chand shapes mein se ek hota hai. Ye full list hai — baad ke har example mein us cell ka tag lagaa hai jo wo fill karta hai.

# Case class Kya tricky hai Example
A Inode → block group (normal) 1-based offset Ex 1
B Inode → block group (boundary) inode exactly ek group edge pe Ex 2
C Degenerate input: inode 1 sabse chhotaa legal inode, off-by-one trap Ex 3
D Capacity from geometry metadata overhead vs raw size Ex 4
E Zero/limiting: tiny disk disk ek group se chhotaa Ex 5
F Inode table sizing inodes ek alag fixed pool hain Ex 6
G Real-world word problem "millions of tiny files" — space se pehle inodes khatam Ex 7
H Extents vs old pointers fragmented file ke liye kitne extent records Ex 8
I Exam twist: different block size geometry ke saath scale hoti hai; 4096 hardcode mat karo Ex 9
J Usable data blocks per group SAARE metadata blocks subtract karna Ex 10

Do symbols baar baar aate hain, toh inhe ek baar pin kar lete hain, plain words mein:

Recall Woh ek formula jo cells A, B, C solve karta hai

Ek inode jiska number ho (numbering 1 se shuru hoti hai) aur inodes per group ho:

  • Group number :::
  • Us group ki inode table ke andar index :::
  • 1 kyun subtract karte hain? ::: inode numbers 1-based hain, array slots 0-based hain; ye skip karne par tum har boundary pe ek group off ho jaate ho.

Mapping ki ek picture (Case A se pehle ye padho)

Neeche wala figure poora page ek image mein hai: left pe, inode-number line ke equal groups mein kati hui hai; right pe, ek single block group ka physical layout (superblock backup → bitmaps → inode table → data). Har worked example bas "main kaun sa arrow follow kar raha hoon?" hai.

Figure — ext4 structure — superblock, block groups, inodes

Words mein: inode numbers group 0 mein rehte hain, group 1 mein, aur aage bhi aise hi — vertical dashed lines woh group boundaries hain jahan Case B exactly land karta hai. Ek group ke andar (right panel), pehle kuch blocks metadata ke liye hain; end mein badi green stretch hai jahan file data actually jaata hai (woh green stretch hi Case J count karta hai).


Case A — Inode to block group, the ordinary case

Upar wale figure mein blue arrow follow karo: wo inode ko left number-line se kisi group box mein le jaata hai.

Step 1 — 0-based pe shift karo. Ye step kyun? Formula count karta hai ki se pehle kitne inodes aate hain. Inode 1 "zeroth" inode hai, isliye hum 1 subtract karte hain taaki 1-based label ko 0-based position mein badlen.

Step 2 — Group size se divide karo, whole part rakho. Ye step kyun? Har group mein exactly inodes hote hain. Integer division "hamare se pehle kitne full groups fit hote hain" ही group number hai. Floor fractional part phenk deta hai kyunki tum "group 6.1" mein nahi ho sakte.

Step 3 — Remainder slot deta hai. Ye step kyun? Remainder hai "hum group 6 mein kitna andar hain" — inode table ki row (figure ka right panel).

Verify: inode ko (group, index) se reconstruct karo: . ✅ ( 0-based shift ko undo karta hai).


Case B — Boundary: ek inode jo exactly group edge pe baith jaata hai

Figure mein, ye dono inodes pehli dashed boundary line ke dono taraf baithe hain.

Step 1 — Inode 8192 karo. Ye step kyun? , isliye whole part hai. Inode 8192, group 0 ka last inode hai (index ).

Step 2 — Inode 8193 karo. Ye step kyun? Ab numerator exactly group size ke barabar hai, isliye whole part ho jaata hai. Inode 8193, group 1 ka first inode hai (index ).

Verify: group 0 mein inodes hain (exactly inodes), group 1 se shuru hota hai. Count check out hota hai. ✅


Ye figure ke left pe bilkul pehla slot hai.

Step 1 — Formula ko literally apply karo. Ye step kyun? Formula karne ki yahi poori wajah hai: pehla inode group 0 ke slot 0 pe map hona chahiye. Agar hum bhool jaate toh bhi yahan group 0 milta (kyunki ), isliye ye case bug ko chhupaata hai — yahi wajah hai ki tumhe boundaries test karni chahiye (Ex 2), sirf inode 1 nahi.

Verify: reconstruct karo. ✅ Sabse chhotaa inode pehle slot pe map hota hai, jaisa hona chahiye.


Case D — Pure geometry se capacity

Step 1 — Dono sizes ko same unit (MiB) mein daalo. Ye step kyun? Tum size ko group-size se tab tak divide nahi kar sakte jab tak units match na hon. GiB aur MiB mix karna yahan #1 arithmetic error hai.

Step 2 — Group size se divide karo. Ye step kyun? Groups ki number bas total-size ÷ size-per-group hai. Metadata (bitmaps, inode table, superblock backups) har group ke andar se carved out hota hai, isliye ye group count nahi badalta — ye group ke andar usable data blocks mein se khaata hai.

Verify: MiB GiB exactly. ✅ Clean kyunki , ka multiple hai.


Case E — Limiting case: ek group se chhotaa disk

Step 1 — Raw ratio compute karo. Ye step kyun? Disk aadha group hai. Lekin aadha block group nahi ho sakta — group layout ki unit hai.

Step 2 — Ceiling se upar round karo. Ye step kyun? Hum ceiling (upar define ki gayi) use karte hain kyunki group ka ek fraction bhi ek poore group mein rehne ki demand karta hai. ext4 ek group banata hai aur simply usme fewer blocks track karta hai: block bitmap phir bhi ek full block occupies karta hai, lekin uski bahut si bits "beyond end of device" ko used mark karti hain. Isliye ek chhotaa disk proportionally zyada metadata overhead leta hai — woh degenerate case jahan "ek bitmap block" data ke liye oversized hai.

Verify: actually present blocks blocks. Ye ek full bitmap ki bits se kam hai, isliye ek bitmap unhe sab cover kar leta hai room to spare ke saath. ✅ Exactly ek group confirm hota hai.


Case F — Inode table sizing (inodes ek alag pool hain)

Step 1 — Inode table ke total bytes. Ye step kyun? Inode table ek plain array hai: (count) × (ek record ka size).

Step 2 — Bytes ko blocks mein convert karo, upar round karke. Ye step kyun? Disk whole blocks mein allocate hoti hai, isliye generally tumhe upar round karna hota hai (ceiling) — agar array bytes hota toh bhi blocks chahiye honge kyunki leftover bytes ek aur whole block maangti hain. Yahan ye evenly divide ho jaata hai, isliye ceiling aur floor dono par agree karte hain. blocks, bitmap blocks ko dwarf karta hai — inode tables fixed metadata ka bulk hoti hain.

Verify: . ✅ Aur blocks MiB, MiB group mein se — group ka lagbhag inode table hai.


Case G — Real-world word problem: inodes khatam hona

Step 1 — Volume par total inodes. Ye step kyun? Inodes mkfs time pe pre-allocate hote hain — ek fixed pool. Har file ko exactly ek chahiye. Ek baar saare use ho jaayein, koi nayi file nahi ban sakti chahe free bytes kitne bhi hon.

Step 2 — Space side check karo (upper bound). Worst case lo jahan har file phir bhi ek full -byte block occupy kare: Convert karo GiB mein (1 GiB bytes): Ye step kyun? Is worst case mein bhi GiB mein se sirf GiB touch hota hai — ye confirm karta hai ki do resources decouple ho gaye: files (inodes) apni limit hit kar gayi jabki bytes nahi.

Step 3 — Diagnose karo. df -i inodes used dikhaaega; df space free dikhaaega. Fix: files delete karo ya reformat karo zyada inodes ke saath (mkfs.ext4 -N ya -i bytes-per-inode).

Verify: failure count inode pool ke barabar hai: , aur worst-case space bytes GiB exactly. ✅


Case H — Extents vs old block pointers

Figure — ext4 structure — superblock, block groups, inodes

Figure padhna: top blue bar ek -block file hai jo contiguously stored hai — ye ek single extent record mein collapse ho jaati hai (start , length ), right side pe ek arrow se dikhaya gaya hai. Neeche teen bars (green / orange / red) same file hain jo disk par 3 separated runs mein toot gayi hai — ab tumhe 3 extent records chahiye, ek har contiguous run ke liye, phir bhi inode ke 4 inline slots mein fit ho jaate hain. Bottom par gray line yaad dilaati hai ki ext2/3 har block ke liye ek pointer store karta ( of them) plus ek indirect block ke saath. Pedagogical punch: extent count fragments ki sankhya ke saath badhta hai, blocks ki sankhya ke saath nahi.

Step 1 — Contiguous file. blocks ka ek run 1 extent (figure mein top bar). Ye step kyun? Extent ka poora point yehi hai ki contiguity ek (start, length) pair mein collapse ho jaati hai. blocks, ek record.

Step 2 — 3-piece file. Teen runs 3 extents, saare inode ke inline slots mein fit ho jaate hain (B-tree ki zaroorat nahi). Ye step kyun? Ek extent contiguous run ke liye, block ke liye nahi. , isliye koi external tree nahi.

Step 3 — ext2/3 se contrast karo. Purana scheme har data block ke liye ek 4-byte pointer store karta hai: pointers. Pehle direct hain; baaki () ek single indirect block mein rehte hain. Woh indirect block pointers hold karta hai (hum se divide karte hain kyunki har pointer bytes ka hai), jo hai, isliye ek indirect block kaafi hai. Net: ext2/3 ko metadata ka extra indirect block chahiye plus pointer entries — versus ext4 ke extent record ke. Ye step kyun? Dikhata hai ki extents kyun jeetthe hain: metadata fragments ki sankhya ke saath badhti hai, blocks ki sankhya ke saath nahi.

Verify: contiguous ⇒ extent; ext2/3 indirect block pointers hold karta hai needed, isliye exactly ek indirect block kaafi hai. ✅


Case I — Exam twist: block size badal do

Step 1 — Definition se blocks per group recompute karo. Ye step kyun? kabhi bhi ek constant nahi tha — ye tha, par evaluate kiya gaya. Bitmap constraint (ek bit per block, ek bitmap block per group) ke saath scale karti hai. Number yaad karna formula ki jagah trap hai.

Step 2 — Group size in bytes. Ye step kyun? Group size hai (blocks/group) × (bytes/block) . mein quadratic — ko shrink karne se group size shrink hoti hai ().

Verify: . Aur 4 KiB case se ratio: . ✅ Quadratic scaling confirm hua.


Case J — Usable data blocks per group

Ye pehle figure ke right panel mein green data stretch hai — metadata blocks ke baad jo kuch bhi hai.

Step 1 — Har group ke metadata blocks list karo. Ye step kyun? Tum sirf "bitmaps" subtract nahi kar sakte — group carry karta hai wo har fixed structure blocks khaata hai. Ek term miss karna usual exam slip hai. (Superblock backup ke bina groups wo 2 blocks bachate hain, lekin worst case quote karna safe hai.)

Step 2 — Group total se subtract karo. Ye step kyun? Data blocks wo sab hain jo saari fixed metadata carved out hone ke baad bachta hai — ye woh number hai jismein ek group actually files store kar sakta hai.

Step 3 — Overhead as a percentage. Ye step kyun? Loss ko fraction mein badalna metadata "tax" batata hai. Yahan se kam — inode tables ise dominate karti hain.

Verify: (har block ka hisaab), aur bytes MiB usable data per MiB group. ✅


Poori chain, ek nazar mein

Inode number N

subtract 1 for zero-based

divide by inodes per group

floor gives block group

remainder gives table index

block size B

blocks per group equals 8 times B

group size equals 8 times B squared

subtract metadata gives usable data blocks


Self-test

Recall Quick recall

Group of inode 8192 with I=8192 ::: group 0 (group 0 ka last inode, kyunki floor(8191/8192)=0) Blocks per group when B=1024 ::: 8×1024 = 8192 blocks (8 MiB group) Extents for a perfectly contiguous 400-block file ::: exactly 1 extent Why does a 64 MiB disk still get a full bitmap block ::: bitmap ek whole block hoti hai chahe kuch bhi ho; unused bits ko used mark kar diya jaata hai df shows free space but files won't create — cause ::: inode pool exhausted (separate fixed pool); df -i check karo Usable data blocks per group (B=4096, 512-block inode table, 4 other metadata blocks) ::: 32768 − 516 = 32252 data blocks What does equal precisely ::: , hamesha range mein Why ceiling (not floor) for "how many blocks to store X bytes" ::: koi bhi leftover bytes ek aur whole block maangti hain, isliye upar round karte hain

See also: Bitmaps for allocation · Hard links vs Symbolic links · ext2 vs ext3 vs ext4 · Journaling · Virtual File System · Disk Scheduling · Memory Management · back to Operating Systems.