Exercises — Journaling — why, how it works
4.2.36 · D4· Coding › Operating Systems › Journaling — why, how it works
Shuru karne se pehle, teen words jo hum baar baar use karenge. Agar koi fuzzy lage, expand karo:
Recall Teen load-bearing words
Consistency ::: filesystem ke structures kabhi ek doosre se contradict nahi karte (koi block do files ka nahi, koi directory kisi khaali jagah point nahi karti). Durability ::: ek write jise tumhe "succeeded" bataya gaya woh power cut mein bhi bachti hai. Consistency se alag! Dekho fsync and Durability. Idempotent ::: do baar karna wahi result deta hai jo ek baar karna deta hai — ek box mein value likhna, phir likhna, hi rehta hai.
Level 1 — Recognition
Exercise 1.1
Transaction lifecycle mein, recovery ke dauran konsi ek write kisi transaction ko "ignore me" se "replay me" mein flip karti hai?
Recall Solution
COMMIT block. Yeh sab se aakhir mein likha jaata hai aur yeh ek chhota sa single sector write hota hai (disks guarantee karti hain ki ek sector atomic hota hai). Recovery rule: commit present ⇒ replay; commit absent ⇒ discard. Isliye parent note isse "the atomic go switch" kehta hai.
Exercise 1.2
Write-Ahead Logging invariant ko apne words mein batao, aur identify karo ki in do orderings mein se kaun si isse obey karti hai:
- (A) write new inode to journal → write new inode in place
- (B) write new inode in place → write new inode to journal
Recall Solution
WAL invariant: ==intent durably journal par hona chahiye pehle koi bhi in-place (real) structure touch ho== — likha jaata hai , jahan ka matlab hai "strictly completes before". (A) isko obey karta hai (journal first). (B) isko violate karta hai — agar (B) ke do steps ke beech crash aa jaaye, toh real inode pehle se hi toot chuka hai aur journal mein koi record nahi hai usse theek karne ke liye. Dekho Write-Ahead Logging.
Level 2 — Application
Exercise 2.1
Tum log.txt mein 1 block append karte ho. Isse data block, inode (size + pointer), aur bitmap — 3 in-place structures touch hote hain. Ordered mode (default) mein, in 3 mein se kitne journal mein jaate hain? Aur jo nahi jaate unke liye kaunsi ordering guarantee apply hoti hai?
Recall Solution
Ordered mode sirf metadata journal karta hai: inode aur bitmap → 2 structures journal mein jaate hain. Data block journal mein nahi jaata; instead isse disk par commit block se pehle force kiya jaata hai. Isse inode garbage point karne se bachta hai. Parent note mein modes table dekho the parent note.
Exercise 2.2
Ek journal transaction mein order se hai: 1 descriptor block, metadata blocks jo log ho rahe hain, aur 1 commit block. Crash ho jaata hai. Recovery ko descriptor aur metadata blocks milte hain, lekin koi commit block nahi. Recovery kya karti hai, aur baad mein real filesystem ki state kya hoti hai?
Recall Solution
Transaction ko discard karo. Kyunki commit missing hai, recovery trust nahi kar sakti ki saare metadata blocks durably pahunche — woh poori cheez ko kabhi-commit-nahi-hua treat karti hai. Khaas baat, checkpoint step (journal → in place copy karna) nahi hua tha, isliye real inode/bitmap kabhi touch nahi hue. Filesystem byte-for-byte apni pre-transaction state mein hai → consistent hi hai. Append simply "hua hi nahi." ✅
Level 3 — Analysis
Exercise 3.1 — recovery time scaling
Ek disk mein hai. Journal ek fixed circular region hai jiska size hai. Purana fsck poori disk scan karta hai read rate par. Journal recovery sirf journal scan karta hai usi rate par.
(a) Full fsck mein kitna time lagta hai? (b) Journal recovery mein kitna time lagta hai? (c) Journaling kitne factor se fast hai?
Recall Solution
Megabytes mein convert karo: .
(a) .
(b) .
(c) Factor faster.
Point: journal recovery kaam jo chal raha tha uske saath scale karta hai (, fixed), jabki fsck total disk size ke saath (). Badi disks fsck ko aur bura banati hain lekin journaling par koi asar nahi.
Exercise 3.2 — ordered-mode counterexample
Writeback mode mein, maano metadata commit ho gayi (inode ab kehta hai "file 8 KB hai, blocks 40–41") lekin crash hota hai pehle ki naye data blocks 40–41 flush ho jaayein. Blocks 40–41 pehle ek deleted user ki password file rakhte the. Newly-extended file ka reader ab kya dekhta hai, aur ordered mode isse kaise rokta hai?
Recall Solution
Reader blocks 40–41 ka stale/garbage content dekhta hai — yahan, kisi aur ka purana password data. Committed metadata legitimately unhi blocks ko point karta hai, lekin intended naya data kabhi aya hi nahi. Yeh ek real security/integrity leak hai. Ordered mode ka fix: yeh data blocks ko disk par force karta hai pehle metadata commit se (data metadata-commit). Toh agar metadata durable hai, toh jis data ko woh point karta hai woh guaranteed pehle se durable hai — stale blocks expose hone ka koi window nahi. Isliye ordered mode default sweet spot hai.
Level 4 — Synthesis
Exercise 4.1 — write-amplification of data mode
Data (full) journal mode mein, metadata aur file data dono journal mein likhe jaate hain aur unke in-place homes par checkpoint bhi hote hain. Chhote descriptor/commit blocks ignore karo, agar tum file data aur metadata append karo, toh disk par kul kitne megabytes jaate hain? "Write amplification" (total bytes written ÷ useful bytes) ko factor ke roop mein express karo.
Recall Solution
Useful payload (kyunki ). Data mode mein har byte do baar likha jaata hai — ek baar journal mein, ek baar in place: Write amplification . Synthesis point: yahi exact 2× cost hai jo full data mode ko "safest, slowest" banati hai, aur isliye ordered mode banaya gaya — woh sirf metadata journal karta hai, toh data ka amplification hota hai. Yahi tension Databases mein Write-Ahead Logging ke saath study ki jaati hai.
Exercise 4.2 — idempotency aur double checkpoint
Ek committed transaction TX#7 set karta hai: inode.size , bitmap.bit[50] . Checkpoint ke dauran, inode disk par copy hota hai, phir crash inode copy hone se pehle bitmap copy hone se pehle aa jaata hai. Reboot par, recovery TX#7 ko committed paata hai aur TX#7 ke saare journal blocks re-copy karta hai — including woh inode jo pehle se likha ja chuka tha. Prove karo ki pehle se sahi inode ko dobara likhna koi nuksan nahi karta, aur woh general property batao jis par yeh rely karta hai.
Recall Solution
Doosri copy se pehle, disk inode.size (pehle checkpoint se). Journal ki inode.size copy bhi hai. Re-copying ke upar likhta hai → value unchanged rehti hai. Bitmap, jo miss hua tha, ab usi replay se bit[50] paata hai. Final state: inode.size , bitmap.bit[50] — exactly committed intent. General property: idempotency — committed transaction ko kitni bhi baar replay karna ek baar replay karne jaisa hi final state deta hai. Yahi crash-during-recovery ko safe banata hai: recovery khud crash ho sakti hai aur dobara run ho sakti hai bina kisi limit ke. Dekho Atomicity.
Level 5 — Mastery
Exercise 5.1 — jhooth bolne wali disk ke around design karna
Kuch saste disks writes ko reorder karti hain aur write "done" report karti hain jabki woh disk ki apni volatile cache mein baith jaata hai. Step by step explain karo yeh WAL invariant ko kaise todta hai jab bhi OS ne writes sahi order mein issue kiye — aur woh single primitive batao jo isse theek karta hai.
Recall Solution
OS issue karta hai: (1) journal metadata, (2) commit block, (3) checkpoint in place — sahi order mein. Lekin agar disk reorder kare aur commit block ko platter par pehle push kare journal metadata se, toh crash ek committed transaction chod sakti hai jiska logged data missing hai. Recovery commit dekhti hai, trust karti hai, replay karti hai — aur garbage/incomplete blocks in place copy karti hai. Corruption, perfect OS ordering ke bawajood.
Root cause: disk ki apni cache ne woh durability boundary tod di jo OS ne assume ki thi. Fix: cache flush / write barrier — OS ko fsync() call karni chahiye (ya FLUSH/FUA barrier issue karna chahiye) journal data likhne ke baad aur fir commit ke baad, disk ko force karte hue ki woh har group ko durably persist kare agle ke shuru hone se pehle. Honest flush primitive ke bina, WAL ka ordering physical media par enforce nahi ho sakta. Dekho fsync and Durability.
Exercise 5.2 — journaling vs copy-on-write
Dono journaling aur copy-on-write (CoW) filesystems crash consistency achieve karte hain, lekin opposite strategies se. Unhe ek ek sentence mein contrast karo, aur batao kaun sa kabhi user data do baar nahi likhta.
Recall Solution
Journaling: in place update karo, lekin pehle intent ek side journal mein likho; recovery replay ya discard karta hai. Full data mode mein yeh data do baar likhta hai (journal + in place). Copy-on-write: kabhi overwrite mat karo; naya data fresh blocks par likho, phir atomically ek single root pointer ko naye tree par flip karo — purana version valid rehta hai flip hone tak. Dekho Copy-on-Write Filesystems. CoW kabhi user data do baar nahi likhta — woh har naye block ko exactly ek baar naye location par likhta hai aur sirf re-point karta hai. Journaling ka data mode inherently double-write karta hai; yahi fundamental trade hai jo dono designs karte hain.
Exercise 5.3 — consistency ≠ durability boundary (capstone)
Ek program file mein 500 KB likhta hai aur write() call success return karti hai. OS ise RAM mein buffer karta hai. Journal mein is data ka koi transaction abhi nahi hai. Power cut ho jaati hai. Reboot ke baad filesystem har consistency check pass karta hai lekin 500 KB gone hai. Kya journaling toot gayi? Precisely justify karo, aur batao program ko kya karna chahiye tha.
Recall Solution
Journaling nahi tooti. Iska promise consistency hai (structures kabhi contradict nahi karte), nahi durability of un-synced buffered writes. Returned write() ka sirf matlab hai "kernel RAM buffer mein copy kiya," "platter par hai" nahi. Kyunki woh data kisi committed journal transaction tak kabhi nahi pahuncha, recovery ke paas correctly kuch replay nahi hai — aur on-disk filesystem perfectly consistent hai, sirf 500 KB ke bina.
Program ko kya karna chahiye tha: write() ke baad fsync(fd) call karo taaki buffered data (aur uska journal transaction) durably disk par ja sake ise saved maanne se pehle. Consistency filesystem ka kaam hai; specific bytes ki durability application ka kaam hai, fsync() ke through. Dekho fsync and Durability.
Recall Self-test summary (cloze)
Woh write jo replay-vs-discard decide karti hai woh hai COMMIT block. WAL journal write ko strictly in-place modification se pehle order karta hai. Recovery safe hai dobara run karne ke liye kyunki replay idempotent hai. Ordered mode sirf metadata journal karta hai lekin data ko metadata commit se pehle force karta hai. Journaling consistency guarantee karta hai, durability nahi; baad wale ke liye fsync use karo.