2011-07-28 00:35:46 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "spinlock.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "buf.h"
|
|
|
|
|
2011-08-12 13:25:39 +00: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
|
2011-09-02 19:08:13 +00:00
|
|
|
// this means that they may observe uncommitted data. I-node and
|
|
|
|
// buffer locks prevent read-only calls from seeing inconsistent data.
|
2011-08-12 13:25:39 +00:00
|
|
|
//
|
|
|
|
// 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 00:35:46 +00:00
|
|
|
struct logheader {
|
2011-08-12 13:25:39 +00:00
|
|
|
int n;
|
2011-07-28 00:35:46 +00:00
|
|
|
int sector[LOGSIZE];
|
|
|
|
};
|
|
|
|
|
2011-09-02 19:08:35 +00:00
|
|
|
struct log {
|
2011-07-28 00:35:46 +00:00
|
|
|
struct spinlock lock;
|
|
|
|
int start;
|
|
|
|
int size;
|
2011-10-11 10:41:37 +00:00
|
|
|
int busy; // a transaction is active
|
2011-07-28 00:35:46 +00:00
|
|
|
int dev;
|
|
|
|
struct logheader lh;
|
2011-09-02 19:08:35 +00:00
|
|
|
};
|
|
|
|
struct log log;
|
2011-07-28 00:35:46 +00:00
|
|
|
|
|
|
|
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 13:25:39 +00:00
|
|
|
for (tail = 0; tail < log.lh.n; tail++) {
|
2011-09-01 14:25:20 +00: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
|
2011-10-11 10:41:37 +00:00
|
|
|
bwrite(dbuf); // write dst to disk
|
2011-09-01 14:25:20 +00:00
|
|
|
brelse(lbuf);
|
2011-07-28 00:35:46 +00: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 13:25:39 +00:00
|
|
|
log.lh.n = lh->n;
|
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 00:35:46 +00:00
|
|
|
log.lh.sector[i] = lh->sector[i];
|
|
|
|
}
|
|
|
|
brelse(buf);
|
|
|
|
}
|
|
|
|
|
2011-10-11 10:41:37 +00:00
|
|
|
// Write in-memory log header to disk.
|
|
|
|
// This is the true point at which the
|
|
|
|
// current transaction commits.
|
2011-07-28 00:35:46 +00:00
|
|
|
static void
|
|
|
|
write_head(void)
|
|
|
|
{
|
|
|
|
struct buf *buf = bread(log.dev, log.start);
|
|
|
|
struct logheader *hb = (struct logheader *) (buf->data);
|
|
|
|
int i;
|
2011-08-12 13:25:39 +00:00
|
|
|
hb->n = log.lh.n;
|
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 00:35:46 +00:00
|
|
|
hb->sector[i] = log.lh.sector[i];
|
|
|
|
}
|
|
|
|
bwrite(buf);
|
|
|
|
brelse(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
recover_from_log(void)
|
|
|
|
{
|
|
|
|
read_head();
|
2011-08-15 16:44:20 +00:00
|
|
|
install_trans(); // if committed, copy from log to disk
|
2011-08-12 13:25:39 +00:00
|
|
|
log.lh.n = 0;
|
2011-08-15 16:44:20 +00:00
|
|
|
write_head(); // clear the log
|
2011-07-28 00:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
begin_trans(void)
|
|
|
|
{
|
|
|
|
acquire(&log.lock);
|
2011-10-11 10:41:37 +00:00
|
|
|
while (log.busy) {
|
2011-08-29 21:18:40 +00:00
|
|
|
sleep(&log, &log.lock);
|
|
|
|
}
|
2011-10-11 10:41:37 +00:00
|
|
|
log.busy = 1;
|
2011-07-28 00:35:46 +00:00
|
|
|
release(&log.lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
commit_trans(void)
|
|
|
|
{
|
2011-08-15 18:11:22 +00:00
|
|
|
if (log.lh.n > 0) {
|
2011-10-11 10:41:37 +00:00
|
|
|
write_head(); // Write header to disk -- the real commit
|
|
|
|
install_trans(); // Now install writes to home locations
|
2011-08-15 18:11:22 +00:00
|
|
|
log.lh.n = 0;
|
2011-10-11 10:41:37 +00:00
|
|
|
write_head(); // Erase the transaction from the log
|
2011-08-15 18:11:22 +00:00
|
|
|
}
|
2011-08-29 21:18:40 +00:00
|
|
|
|
2011-07-28 00:35:46 +00:00
|
|
|
acquire(&log.lock);
|
2011-10-11 10:41:37 +00:00
|
|
|
log.busy = 0;
|
2011-08-29 21:18:40 +00:00
|
|
|
wakeup(&log);
|
2011-07-28 00:35:46 +00:00
|
|
|
release(&log.lock);
|
|
|
|
}
|
|
|
|
|
2011-08-12 13:25:39 +00: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 00:35:46 +00:00
|
|
|
void
|
|
|
|
log_write(struct buf *b)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2011-08-12 13:25:39 +00:00
|
|
|
if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1)
|
2011-07-28 00:35:46 +00:00
|
|
|
panic("too big a transaction");
|
2011-10-11 10:41:37 +00:00
|
|
|
if (!log.busy)
|
2011-07-28 00:35:46 +00:00
|
|
|
panic("write outside of trans");
|
|
|
|
|
2011-08-12 13:25:39 +00:00
|
|
|
for (i = 0; i < log.lh.n; i++) {
|
2011-07-28 00:35:46 +00: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 13:25:39 +00:00
|
|
|
if (i == log.lh.n)
|
|
|
|
log.lh.n++;
|
2011-07-28 00:35:46 +00:00
|
|
|
}
|
2011-09-02 19:14:06 +00:00
|
|
|
|
|
|
|
//PAGEBREAK!
|
|
|
|
// Blank page.
|
|
|
|
|