Visual walkthrough — File allocation — contiguous, linked, indexed (inode)
4.2.34 · D2· Coding › Operating Systems › File allocation — contiguous, linked, indexed (inode)
Hum kuch bhi assume nahi karte sirf itna: ek disk numbered boxes ka array hai. Chalo har symbol ko earn karte hain.
Step 1 — Ek disk bas numbered boxes hai
KYA HAI. Ek hard disk (ya SSD) ko equal-sized chunks mein kaata jaata hai jinhein blocks kehte hain. Socho ek lambi shelf jisme identical boxes hain, har ek pe ek number likha hai: box 0, box 1, box 2, … Har box mein same number of bytes aate hain.
KYUN. Operating system kabhi directly metal se "byte 9000" nahi padhta — woh ek baar mein poora block padhta hai. Toh sabse pehli cheez jo hume chahiye woh hai "ek box kitna bada hai" ka naam. Hum us size ko (bytes per block) kehte hain. Ek common value hai bytes (4 KB).
PICTURE. Pale-yellow boxes ki row dekho. Har ek bytes wide hai; neeche ka number uska physical block address hai — disk pe uska box label.

Step 2 — Ek file box numbers ki list hai
KYA HAI. Ek file ke bytes rarely ek box mein fit hote hain, aur woh boxes jo woh use karti hai shelf pe idhar-udhar bikhari hoti hain. Toh file aslmein bas ek physical block addresses ki ordered list hai: "mera data box 40 mein hai, phir box 7 mein, phir box 91 mein…"
KYUN. Agar woh list kho gayi, file gone — data abhi bhi disk pe hai, lekin hum nahi jaante kaunse boxes hamare hain ya kis order mein. Toh file allocation ka poora game yeh hai: yeh box numbers ki list hum kahan rakhte hain? Indexed allocation ka jawab: ise ek dedicated jagah mein rakho jise index block / inode kehte hain.
PICTURE. Blue arrows woh list hain. List ki position 0 box 40 ki taraf point karti hai, position 1 box 7 ki taraf, aur aise aage. List mein position logical block number hai; wahan stored value physical block number hai.

Step 3 — EK block mein kitne box numbers fit hote hain?
KYA HAI. Ek box number (ek pointer) khud sirf kuch bytes hota hai — uski size ko kaho (pointer size, e.g. bytes). Agar hum ek poore block ko sirf pointers se bhaar dein, kitne fit honge?
KYUN. Yeh ratio poori derivation mein sabse important number hai, kyunki inode ka har level apni reach ko exactly itne se multiply karta hai. Hum ise apna naam dete hain:
Term by term:
- = ek block mein bytes (woh box jise hum pointers se bhaar rahe hain),
- = ek pointer mein bytes (ek box-number ki width),
- = ek block mein kitne pointers fit hote hain.
KYUN divide? Division ka jawab hai "size ki kitni cheezein size ke container mein fit hongi?" — yeh ki definition hai. ke saath: pointers per block.
PICTURE. Ek block chhote slots mein kata hua, har slot mein ek physical block number. Pink brace slots dikhata hai.

Step 4 — Level 0: 12 direct pointers (tiny files, zero extra reads)
KYA HAI. Inode mein khud 12 pointers hote hain jo seedha data blocks ki taraf point karte hain. Yeh direct pointers hain. Koi middle-men nahi.
KYUN. Real-world fact: zyaadatar files choti hoti hain. Agar 12 direct pointers already bytes tak pahunch jaate hain, toh ek choti file ko zero extra disk reads chahiye — inode already memory mein hai, toh direct pointer follow karna kuch extra cost nahi karta. Yahi 80/20 design hai: common case ko free banao.
- = inode mein direct slots ki sankhya,
- = har data block mein bytes.
ke saath: B ≈ 48 KB.
PICTURE. Left pe inode; 12 blue arrows seedhe 12 yellow data boxes ki taraf jaate hain. Inode aur data ke beech kuch nahi.

Step 5 — Level 1: single indirect (ek hop → aur blocks)
KYA HAI. Inode mein ek extra pointer hai, single indirect. Yeh data ki taraf point nahi karta — yeh ek pointers se bhare block ki taraf point karta hai. Woh block pointers hold karta hai, har ek ek data block ki taraf aim karta hua.
KYUN. Direct slots 12 pe khatam ho gaye. Inode mein hazaron pointers thusne ki jagah (jo har chhoti file ke liye inode ko huge bana deta), hum extra pointers ka kharcha tab karte hain jab file ko chahiye — ek block ko pointer-holder ke roop mein kharch karke. Ek extra read aur data blocks kharid deta hai.
- = us ek indirection block mein pointers (Step 3 se),
- = un data blocks mein se har ek ke bytes.
ke saath: MB.
PICTURE. Inode → ek pink "index" block → blue arrows data boxes ki taraf fan out karte hue. Ek hop data tak pahunchne se pehle — notice karo.

Step 6 — Levels 2 aur 3: double & triple indirect (fan-out squared, cubed)
KYA HAI. Wahi trick, stacked.
- Double indirect: inode → pointers ka ek block, har ek ek single-indirect block of pointers ki taraf point karta hai → data blocks.
- Triple indirect: ek aur layer → data blocks.
KYUN. Pointers-pointing-to-pointers ki har nayi layer reach ko ke ek aur factor se multiply karti hai. Yahi wajah hai ki numbers explode karte hain: , toh har level pichle se hazaar guna bada hai. Do hops dete hain, teen hops .
- = first-level slots second-level slots (multiply isliye kyunki top pointers mein se har ek ke neeche aur hain),
- = wahi reasoning teen deep stacked.
PICTURE. Ek branching tree: root (inode ka double-indirect pointer) → branches → har ek leaves mein branch karta hai. Leaves count karo: . Dashed triple-indirect ke liye ek aur level of branching add karta hai.

Step 7 — Levels ko sum karo = 4 TB result
KYA HAI. Total reach = har level jod do. Har block count ko se multiply karke bytes mein convert karo:
KYUN add karte hain? File ke chaar regions disjoint hain — ek logical block exactly ek region mein hota hai — toh total addressable size unka sum hai (no double-counting).
plug in karo:
| Level | Blocks | Bytes | Human |
|---|---|---|---|
| Direct | ≈ 48 KB | ||
| Single | 4 MB | ||
| Double | 4 GB | ||
| Triple | 4 TB |
Triple-indirect term () double se hazaar guna bada hai, jo single se hazaar guna bada hai — toh total ≈ 4 TB hai, aur chhote terms use barely nudge karte hain.
PICTURE. Log scale pe ek stacked bar — har level pichle se hazaar guna oocha. Aankhein dekhti hain kyun "4 TB + 4 GB + 4 MB + 48 KB" basically sirf "4 TB" hai.

Step 8 — Edge & degenerate cases (kuch nahi chhoda)
KYA HAI & KYUN, case by case:
- Empty file ( bytes). Koi data blocks nahi. Inode abhi bhi exist karta hai (woh metadata + all-null pointers hold karta hai). Max reach unchanged; actual reach = 0.
- Tiny file ≤ 48 KB. Poori tarah 12 direct pointers mein rehti hai. Koi bhi byte padhna = in-memory inode ke baad 0 extra reads. Yeh woh fast common case hai jiske liye design optimize kiya gaya hai.
- Ek block exactly boundary vs pe. aakhri direct block hai; pehla single-indirect block hai. Yahan off-by-one ka matlab ek galat disk read — dhyaan se count karo: direct logical blocks se tak cover karta hai.
- Bade pointers (, 64-bit addresses). Tab , toh har level shrink hota hai: triple reach B ≈ 512 GB. Mote pointers → chhotii max file. Yahi wajah hai ki number exact filesystem pe depend karta hai, koi universal constant nahi.
- Bade blocks (). Tab aur khud double ho jaata hai, toh triple reach — bahut zyada grow karta hai. Bade blocks → bahut badi max file (lekin half-empty last blocks mein zyada wasted space — dekho External vs Internal Fragmentation).
PICTURE. Teen cases side by side: empty inode (saare pointers null), tiny file (sirf direct used), aur "boundary" number line jo block 11 vs 12 mark karta hai.

Ek-picture summary
Sab kuch ek board pe: inode ke 12 direct + single + double + triple pointers, har ek pe block count label kiya hua () aur data tak pahunchne ke liye hops ki depth. Left→right padho: fan-out compound karta hai, sizes explode karti hain, total ≈ 4 TB.

Recall Feynman retelling — plain words mein wapas bolo
Ek disk equal boxes ki shelf hai, har ek bytes ka. Ek file yaad rakhne ke liye hum box numbers ki list rakhte hain. Ek box box-numbers hold kar sakta hai, kyunki har number bytes wide hai. Inode seedha data ki taraf 12 numbers store karta hai (choti files ke liye great — koi extra reads nahi). Jab 12 enough nahi, woh ek poore box ko numbers ki list ke roop mein spend karta hai (single indirect: ek hop, blocks). Aur chahiye? numbers ka ek box jahan har ek doosre numbers ke box ki taraf point kare, blocks deta hai (double). Ek aur layer deti hai (triple). Chaar regions ko add karo — yeh overlap nahi karte — blocks ko bytes mein badlo karke, aur ke saath tum paate ho . ya badlo aur number badal jaata hai, kyunki badal jaata hai.
Recall
kya hai aur yeh kyun matter karta hai? ::: , pointers per block; har indirection level reach ko se multiply karta hai. Double indirect kyun blocks deta hai, nahi? ::: Do layers of fan-out multiply hoti hain: top pointers, har ek aur ke liye, toh . Direct pointers choti files ke liye "free" kyun hain? ::: Inode already memory mein hota hai, toh ek direct pointer data tak zero extra disk reads mein pahunchta hai. Agar pointer size 4 se 8 bytes ho jaaye toh max file size ka kya hoga? ::: half ho jaata hai (1024 se 512), toh har level shrink hota hai; max ≈512 GB tak drop ho jaata hai.
Related: Directory Structure name→inode mapping store karta hai; Free Space Management (bitmap, free list) decide karta hai kaunse boxes free hain; File Systems (ext4, FAT, NTFS) in schemes ko practice mein implement karte hain.