Skip to content

Commit

Permalink
Merge branch 'master' of git+ssh://amsterdam.csail.mit.edu/home/am0/6…
Browse files Browse the repository at this point in the history
….828/xv6
  • Loading branch information
Robert Morris committed Aug 28, 2014
2 parents 12eeefc + 2b2c197 commit 8d618ca
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 163 deletions.
2 changes: 2 additions & 0 deletions bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ bget(uint dev, uint sector)
}

// Not cached; recycle some non-busy and clean buffer.
// "clean" because B_DIRTY and !B_BUSY means log.c
// hasn't yet committed the changes to the buffer.
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
if((b->flags & B_BUSY) == 0 && (b->flags & B_DIRTY) == 0){
b->dev = dev;
Expand Down
4 changes: 2 additions & 2 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ void microdelay(int);
// log.c
void initlog(void);
void log_write(struct buf*);
void begin_trans();
void commit_trans();
void begin_op();
void end_op();

// mp.c
extern int ismp;
Expand Down
8 changes: 4 additions & 4 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ exec(char *path, char **argv)
struct proghdr ph;
pde_t *pgdir, *oldpgdir;

begin_trans();
begin_op();
if((ip = namei(path)) == 0){
commit_trans();
end_op();
return -1;
}
ilock(ip);
Expand Down Expand Up @@ -50,7 +50,7 @@ exec(char *path, char **argv)
goto bad;
}
iunlockput(ip);
commit_trans();
end_op();
ip = 0;

// Allocate two pages at the next page boundary.
Expand Down Expand Up @@ -101,7 +101,7 @@ exec(char *path, char **argv)
freevm(pgdir);
if(ip){
iunlockput(ip);
commit_trans();
end_op();
}
return -1;
}
8 changes: 4 additions & 4 deletions file.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ fileclose(struct file *f)
if(ff.type == FD_PIPE)
pipeclose(ff.pipe, ff.writable);
else if(ff.type == FD_INODE){
begin_trans();
begin_op();
iput(ff.ip);
commit_trans();
end_op();
}
}

Expand Down Expand Up @@ -136,12 +136,12 @@ filewrite(struct file *f, char *addr, int n)
if(n1 > max)
n1 = max;

begin_trans();
begin_op();
ilock(f->ip);
if ((r = writei(f->ip, addr + i, f->off, n1)) > 0)
f->off += r;
iunlock(f->ip);
commit_trans();
end_op();

if(r < 0)
break;
Expand Down
117 changes: 84 additions & 33 deletions log.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
#include "fs.h"
#include "buf.h"

// Simple logging. Each file system system call
// should be surrounded with begin_trans() and commit_trans() calls.
// Simple logging that allows concurrent FS system 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.
// A log transaction contains the updates of multiple FS system
// calls. The logging system only commits when there are
// no FS system calls active. Thus there is never
// any reasoning required about whether a commit might
// write an uncommitted system call's updates to disk.
//
// A system call should call begin_op()/end_op() to mark
// its start and end. Usually begin_op() just increments
// the count of in-progress FS system calls and returns.
// But if it thinks the log is close to running out, it
// sleeps until the last outstanding end_op() commits.
//
// The log is a physical re-do log containing disk blocks.
// The on-disk log format:
Expand All @@ -38,13 +39,15 @@ struct log {
struct spinlock lock;
int start;
int size;
int busy; // a transaction is active
int outstanding; // how many FS sys calls are executing.
int committing; // in commit(), please wait.
int dev;
struct logheader lh;
};
struct log log;

static void recover_from_log(void);
static void commit();

void
initlog(void)
Expand Down Expand Up @@ -117,36 +120,88 @@ recover_from_log(void)
write_head(); // clear the log
}

// called at the start of each FS system call.
void
begin_trans(void)
begin_op(void)
{
acquire(&log.lock);
while (log.busy) {
sleep(&log, &log.lock);
while(1){
if(log.committing){
sleep(&log, &log.lock);
} else if(log.lh.n + (log.outstanding+1)*MAXOPBLOCKS > LOGSIZE){
// this op might exhaust log space; wait for commit.
sleep(&log, &log.lock);
} else {
log.outstanding += 1;
release(&log.lock);
break;
}
}
log.busy = 1;
release(&log.lock);
}

// called at the end of each FS system call.
// commits if this was the last outstanding operation.
void
commit_trans(void)
end_op(void)
{
int do_commit = 0;

acquire(&log.lock);
log.outstanding -= 1;
if(log.committing)
panic("log.committing");
if(log.outstanding == 0){
do_commit = 1;
log.committing = 1;
} else {
// begin_op() may be waiting for log space.
wakeup(&log);
}
release(&log.lock);

if(do_commit){
// call commit w/o holding locks, since not allowed
// to sleep with locks.
commit();
acquire(&log.lock);
log.committing = 0;
wakeup(&log);
release(&log.lock);
}
}

// Copy modified blocks from cache to log.
static void
write_log(void)
{
int tail;

for (tail = 0; tail < log.lh.n; tail++) {
struct buf *to = bread(log.dev, log.start+tail+1); // log block
struct buf *from = bread(log.dev, log.lh.sector[tail]); // cache block
memmove(to->data, from->data, BSIZE);
bwrite(to); // write the log
brelse(from);
brelse(to);
}
}

static void
commit()
{
if (log.lh.n > 0) {
write_log(); // Write modified blocks from cache to log
write_head(); // Write header to disk -- the real commit
install_trans(); // Now install writes to home locations
log.lh.n = 0;
write_head(); // Erase the transaction from the log
}

acquire(&log.lock);
log.busy = 0;
wakeup(&log);
release(&log.lock);
}

// 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).
// Record the block number and pin in the cache with B_DIRTY.
// commit()/write_log() will do the disk write.
//
// log_write() replaces bwrite(); a typical use is:
// bp = bread(...)
// modify bp->data[]
Expand All @@ -159,21 +214,17 @@ log_write(struct buf *b)

if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1)
panic("too big a transaction");
if (!log.busy)
panic("write outside of trans");
if (log.outstanding < 1)
panic("log_write outside of trans");

for (i = 0; i < log.lh.n; i++) {
if (log.lh.sector[i] == b->sector) // log absorbtion?
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);
if (i == log.lh.n)
log.lh.n++;
b->flags |= B_DIRTY; // XXX prevent eviction
b->flags |= B_DIRTY; // prevent eviction
}

//PAGEBREAK!
Expand Down
2 changes: 1 addition & 1 deletion mkfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)

int nblocks = 985;
int nblocks = (995-LOGSIZE);
int nlog = LOGSIZE;
int ninodes = 200;
int size = 1024;
Expand Down
5 changes: 3 additions & 2 deletions param.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#define NCPU 8 // maximum number of CPUs
#define NOFILE 16 // open files per process
#define NFILE 100 // open files per system
#define NBUF 10 // size of disk block cache
#define NINODE 50 // maximum number of active i-nodes
#define NDEV 10 // maximum major device number
#define ROOTDEV 1 // device number of file system root disk
#define MAXARG 32 // max exec arguments
#define LOGSIZE 10 // max data sectors in on-disk log
#define MAXOPBLOCKS 10 // max # of blocks any FS op writes
#define LOGSIZE (MAXOPBLOCKS*3) // max data sectors in on-disk log
#define NBUF (MAXOPBLOCKS*3) // size of disk block cache (>= LOGSIZE)

4 changes: 2 additions & 2 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ exit(void)
}
}

begin_trans();
begin_op();
iput(proc->cwd);
commit_trans();
end_op();
proc->cwd = 0;

acquire(&ptable.lock);
Expand Down
Loading

0 comments on commit 8d618ca

Please sign in to comment.