2011-07-28 02:35:46 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "mmu.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "x86.h"
|
|
|
|
#include "spinlock.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "buf.h"
|
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
// Simple logging. Each system call that might write the file system
|
|
|
|
// should be surrounded with begin_trans() and commit_trans() calls.
|
|
|
|
//
|
|
|
|
// The log holds at most one transaction at a time. Commit forces
|
|
|
|
// the log (with commit record) to disk, then installs the affected
|
|
|
|
// blocks to disk, then erases the log. begin_trans() ensures that
|
|
|
|
// only one system call can be in a transaction; others must wait.
|
|
|
|
//
|
|
|
|
// Allowing only one transaction at a time means that the file
|
|
|
|
// system code doesn't have to worry about the possibility of
|
|
|
|
// one transaction reading a block that another one has modified,
|
|
|
|
// for example an i-node block.
|
|
|
|
//
|
|
|
|
// Read-only system calls don't need to use transactions, though
|
|
|
|
// this means that they may observe uncommitted data. I-node
|
|
|
|
// and buffer locks prevent read-only calls from seeing inconsistent data.
|
|
|
|
//
|
|
|
|
// The log is a physical re-do log containing disk blocks.
|
|
|
|
// The on-disk log format:
|
|
|
|
// header block, containing sector #s for block A, B, C, ...
|
|
|
|
// block A
|
|
|
|
// block B
|
|
|
|
// block C
|
|
|
|
// ...
|
|
|
|
// Log appends are synchronous.
|
|
|
|
|
|
|
|
// Contents of the header block, used for both the on-disk header block
|
|
|
|
// and to keep track in memory of logged sector #s before commit.
|
2011-07-28 02:35:46 +02:00
|
|
|
struct logheader {
|
2011-08-12 15:25:39 +02:00
|
|
|
int n;
|
2011-07-28 02:35:46 +02:00
|
|
|
int sector[LOGSIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
|
|
|
int start;
|
|
|
|
int size;
|
2011-08-29 23:18:40 +02:00
|
|
|
int intrans;
|
2011-07-28 02:35:46 +02:00
|
|
|
int dev;
|
|
|
|
struct logheader lh;
|
|
|
|
} log;
|
|
|
|
|
|
|
|
static void recover_from_log(void);
|
|
|
|
|
|
|
|
void
|
|
|
|
initlog(void)
|
|
|
|
{
|
|
|
|
if (sizeof(struct logheader) >= BSIZE)
|
|
|
|
panic("initlog: too big logheader");
|
|
|
|
|
|
|
|
struct superblock sb;
|
|
|
|
initlock(&log.lock, "log");
|
|
|
|
readsb(ROOTDEV, &sb);
|
|
|
|
log.start = sb.size - sb.nlog;
|
|
|
|
log.size = sb.nlog;
|
|
|
|
log.dev = ROOTDEV;
|
|
|
|
recover_from_log();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy committed blocks from log to their home location
|
|
|
|
static void
|
|
|
|
install_trans(void)
|
|
|
|
{
|
|
|
|
int tail;
|
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
//if (log.lh.n > 0)
|
|
|
|
// cprintf("install_trans %d\n", log.lh.n);
|
|
|
|
for (tail = 0; tail < log.lh.n; tail++) {
|
2011-09-01 16:25:20 +02:00
|
|
|
struct buf *lbuf = bread(log.dev, log.start+tail+1); // read log block
|
|
|
|
struct buf *dbuf = bread(log.dev, log.lh.sector[tail]); // read dst
|
|
|
|
memmove(dbuf->data, lbuf->data, BSIZE); // copy block to dst
|
|
|
|
bwrite(dbuf); // flush dst to disk
|
|
|
|
brelse(lbuf);
|
2011-07-28 02:35:46 +02:00
|
|
|
brelse(dbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the log header from disk into the in-memory log header
|
|
|
|
static void
|
|
|
|
read_head(void)
|
|
|
|
{
|
|
|
|
struct buf *buf = bread(log.dev, log.start);
|
|
|
|
struct logheader *lh = (struct logheader *) (buf->data);
|
|
|
|
int i;
|
2011-08-12 15:25:39 +02:00
|
|
|
log.lh.n = lh->n;
|
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 02:35:46 +02:00
|
|
|
log.lh.sector[i] = lh->sector[i];
|
|
|
|
}
|
|
|
|
brelse(buf);
|
2011-08-12 15:25:39 +02:00
|
|
|
//if (log.lh.n > 0)
|
|
|
|
// cprintf("read_head: %d\n", log.lh.n);
|
2011-07-28 02:35:46 +02:00
|
|
|
}
|
|
|
|
|
2011-09-01 16:25:20 +02:00
|
|
|
// Write in-memory log header to disk, committing log entries till head
|
2011-07-28 02:35:46 +02:00
|
|
|
static void
|
|
|
|
write_head(void)
|
|
|
|
{
|
2011-08-12 15:25:39 +02:00
|
|
|
// if (log.lh.n > 0)
|
|
|
|
// cprintf("write_head: %d\n", log.lh.n);
|
2011-07-28 02:35:46 +02:00
|
|
|
|
|
|
|
struct buf *buf = bread(log.dev, log.start);
|
|
|
|
struct logheader *hb = (struct logheader *) (buf->data);
|
|
|
|
int i;
|
2011-08-12 15:25:39 +02:00
|
|
|
hb->n = log.lh.n;
|
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 02:35:46 +02:00
|
|
|
hb->sector[i] = log.lh.sector[i];
|
|
|
|
}
|
|
|
|
bwrite(buf);
|
|
|
|
brelse(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recover_from_log(void)
|
|
|
|
{
|
|
|
|
read_head();
|
2011-08-15 18:44:20 +02:00
|
|
|
install_trans(); // if committed, copy from log to disk
|
2011-08-12 15:25:39 +02:00
|
|
|
log.lh.n = 0;
|
2011-08-15 18:44:20 +02:00
|
|
|
write_head(); // clear the log
|
2011-07-28 02:35:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
begin_trans(void)
|
|
|
|
{
|
|
|
|
acquire(&log.lock);
|
2011-08-29 23:18:40 +02:00
|
|
|
while (log.intrans) {
|
|
|
|
sleep(&log, &log.lock);
|
|
|
|
}
|
|
|
|
log.intrans = 1;
|
2011-07-28 02:35:46 +02:00
|
|
|
release(&log.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
commit_trans(void)
|
|
|
|
{
|
2011-08-15 20:11:22 +02:00
|
|
|
if (log.lh.n > 0) {
|
2011-09-01 16:25:20 +02:00
|
|
|
write_head(); // Causes all blocks till log.head to be commited
|
|
|
|
install_trans(); // Install all the transactions till head
|
2011-08-15 20:11:22 +02:00
|
|
|
log.lh.n = 0;
|
2011-09-01 16:25:20 +02:00
|
|
|
write_head(); // Reclaim log
|
2011-08-15 20:11:22 +02:00
|
|
|
}
|
2011-08-29 23:18:40 +02:00
|
|
|
|
2011-07-28 02:35:46 +02:00
|
|
|
acquire(&log.lock);
|
2011-08-29 23:18:40 +02:00
|
|
|
log.intrans = 0;
|
|
|
|
wakeup(&log);
|
2011-07-28 02:35:46 +02:00
|
|
|
release(&log.lock);
|
|
|
|
}
|
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
// Caller has modified b->data and is done with the buffer.
|
|
|
|
// Append the block to the log and record the block number,
|
|
|
|
// but don't write the log header (which would commit the write).
|
|
|
|
// log_write() replaces bwrite(); a typical use is:
|
|
|
|
// bp = bread(...)
|
|
|
|
// modify bp->data[]
|
|
|
|
// log_write(bp)
|
|
|
|
// brelse(bp)
|
2011-07-28 02:35:46 +02:00
|
|
|
void
|
|
|
|
log_write(struct buf *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1)
|
2011-07-28 02:35:46 +02:00
|
|
|
panic("too big a transaction");
|
2011-08-29 23:18:40 +02:00
|
|
|
if (!log.intrans)
|
2011-07-28 02:35:46 +02:00
|
|
|
panic("write outside of trans");
|
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
// cprintf("log_write: %d %d\n", b->sector, log.lh.n);
|
2011-07-28 02:35:46 +02:00
|
|
|
|
2011-08-12 15:25:39 +02:00
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 02:35:46 +02:00
|
|
|
if (log.lh.sector[i] == b->sector) // log absorbtion?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
log.lh.sector[i] = b->sector;
|
|
|
|
struct buf *lbuf = bread(b->dev, log.start+i+1);
|
|
|
|
memmove(lbuf->data, b->data, BSIZE);
|
|
|
|
bwrite(lbuf);
|
|
|
|
brelse(lbuf);
|
2011-08-12 15:25:39 +02:00
|
|
|
if (i == log.lh.n)
|
|
|
|
log.lh.n++;
|
2011-07-28 02:35:46 +02:00
|
|
|
}
|