MIT6.S081 Lecture13 Crash Recovery
Crash Recovery
Logging
To achieve a atomic transaction, xv6 use logging to avoid data inconsistence if crash happens while writing. XV6’s syscall won’t write through the inode’s block, instead, it writes ops into log, after the transaction is committed, then xv6 itself writes the log into disk.
Log design
Disk has a continuous space for logging storage, and consists of a header block
foe meta info and a bunch of block copy
. header block
records block index and block number. In xv6, just one transaction is doing at one time.
group commit
, xv6 could wrap many syscall()s and pack them into one transaction to increase parallelism. Also, because of limited log block
, XV6 will split one large write into many little transactions to fit log.
- Write Ahead: Only modified blocks are written into log blocks then system starts to write into home location.
- Freeing: Till all log blocks written into home location and header block is wiped, then we free log block.
Code Analysis:
Start with begin_op(), to tell OS I’m gonna start a safe atomic transaction.
log.outstanding
is the number of syscall that queues at the current transaction.MAXBLOCKS
is the threshold one syscall could use.
1 | void |
log_write(struct buf* b)
. Write the modified block index in the header. This function will reserve a slot for this buf by increasing header->n , thenpin
this buf in the buffer cache (to meet the requirement of Write Ahead Rule).
1 | // Caller has modified b->data and is done with the buffer. |
end_op()
, decrease outstanding, and docommit
if outstanding becomes zero.Note
wakeup(&log)
is to wake up other process blocked on channellog
. Because in end_up(), current process don’t need such reserved space as it claimed at begin_op()
1 | // called at the end of each FS system call. |
commit()
1 | static void |
write_log()
, write all modified buf into log block.
1 | // Copy modified blocks from cache to log. |
write_head()
, write header block
into disk, which is real a commit starts
1 | // Write in-memory log header to disk. |
And left two functions is easy to understand their functionality from their name.
install_trans
, write log block into home data block.
1 | static void |