Skip to content

p12-log: Add write-ahead logging with file layer transaction support#22

Closed
Yash-Rawat-IIT-D wants to merge 1 commit into
codenet:p12-logfrom
Yash-Rawat-IIT-D:p12-log
Closed

p12-log: Add write-ahead logging with file layer transaction support#22
Yash-Rawat-IIT-D wants to merge 1 commit into
codenet:p12-logfrom
Yash-Rawat-IIT-D:p12-log

Conversation

@Yash-Rawat-IIT-D

Copy link
Copy Markdown

Overview


Integrated p12-log write-ahead logging functionality from PR #8 with comprehensive transaction wrappers and file layer improvements. Built on p11-file-layer (PR #21).

Credit: Original p12-log implementation by Amber Agarwal (Amber-Agarwal, PR #8)
This PR: Integration work including mkdir() implementation, begin_op/end_op wrappers in file.rs, log_write() integration, and welcome() rewrite to match C version


Changes Made (Integration Work)

1. File Layer Transaction Wrappers

File: src/file.rs

  • Added begin_op()/end_op() wrappers to all filesystem-modifying functions:
    • fileclose() - Wraps iput with transaction (lines 111-113)
    • filewrite() - Wraps write loop with transaction (lines 191-198)
    • unlink() - Begin at start, end_op before all 4 return points (lines 242-302)
    • open() - Begin at start, end_op before all 5 return points (lines 348-390)
    • mkdir() - Complete begin_op/end_op wrapper (lines 399-411)
  • Changed create() visibility from pub fn to fn (private, line 306)

Ensures all multi-step filesystem operations are atomic and crash-consistent. Matches C file.c transaction patterns exactly.

2. Logging Integration in Filesystem Layer

File: src/fs.rs

  • Replaced all bwrite() calls with log::log_write() in 7 locations:
    • bzero() - Zero disk blocks (line 208)
    • balloc() - Block allocation (line 234)
    • bfree() - Block deallocation (line 264)
    • ialloc() - Inode allocation (line 287)
    • iupdate() - Inode updates (line 385)
    • bmap() - Block mapping with allocation (line 488)
    • writei() - File writing (line 581)

Write-ahead logging requires all disk modifications go through log_write() for crash recovery. Direct bwrite() bypasses logging system.

3. mkdir() Implementation

File: src/file.rs

  • Implemented mkdir() function (lines 399-411)
  • Creates directory with . and .. entries
  • Proper transaction wrapping with begin_op/end_op
  • Matches C sys_mkdir() semantics

Missing from original p11 implementation, required for complete file layer functionality.

4. welcome() Function Rewrite

File: src/lib.rs

  • Completely rewrote welcome() to match C main.c version exactly (lines 55-94)
  • Uses file layer primitives: mkdir(), open(), filewrite(), fileread(), unlink()
  • Replaces low-level filesystem operations with high-level file descriptor API
  • Tests complete file lifecycle: create → write → read → delete

Demonstrates proper usage of file layer with transaction system. Matches C testing approach.

5. Log System Initialization

File: src/lib.rs

  • Added log::initlog(param::ROOTDEV) call during kernel initialization (line 114)
  • Recovers from log on startup for crash consistency

Critical for write-ahead logging - must recover incomplete transactions before filesystem use.

File: src/param.rs

  • Added LOGSIZE = 30 (MAXOPBLOCKS * 3)
  • Added MAXOPBLOCKS = 10
  • Added NFILE = 100

Expected Behavior

On boot:

  • Initializes logging system and recovers from any incomplete transactions
  • Creates /foo directory
  • Creates /foo/greet.txt with content "Hello from file layer!"
  • Reads and prints file contents
  • Deletes /foo/greet.txt
  • Verifies /foo is empty
  • All operations are crash-consistent via write-ahead logging

Testing

Testing Environment: WSL (Windows Subsystem for Linux)

[PASS] Build: make kernel completes with 0 warnings, 0 errors
[PASS] Build: make xv6.img generates filesystem image with LOGSIZE=30
[PASS] Runtime: make qemu shows expected behavior:
       - Log initialized: Reads superblock, recovers from log
       - Directory creation: /foo created successfully
       - File operations: /foo/greet.txt create → write → read → delete cycle
       - Transaction atomicity: All multi-step operations complete or rollback
       - Crash consistency: Log replay works correctly on restart

Files Modified

  • src/log.rs - Complete write-ahead logging implementation
  • src/file.rs - Transaction wrappers, mkdir() implementation
  • src/fs.rs - All bwrite() → log_write() conversions
  • src/lib.rs - welcome() rewrite, initlog() call
  • src/param.rs - Log system constants

Constraints Met

  • Semantic equivalence with C implementation
  • Zero compilation warnings/errors
  • Crash-consistent filesystem operations
  • Transaction boundaries match C exactly

@KoRaghav KoRaghav changed the base branch from p4-traps to p12-log May 6, 2026 08:57
Comment thread src/file.rs
}

if ff.type_ == FileType::Inode {
crate::log::begin_op();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import the crate at the top instead

@KoRaghav

KoRaghav commented May 6, 2026

Copy link
Copy Markdown
Collaborator

Merged manually

@KoRaghav KoRaghav closed this May 6, 2026
@KoRaghav KoRaghav mentioned this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants