a start at concurrent FS system calls

This commit is contained in:
Robert Morris 2014-08-27 17:15:30 -04:00
parent 2c56547272
commit 71453f72f2
6 changed files with 95 additions and 61 deletions

4
defs.h
View file

@ -81,8 +81,8 @@ void microdelay(int);
// log.c // log.c
void initlog(void); void initlog(void);
void log_write(struct buf*); void log_write(struct buf*);
void begin_trans(); void begin_op();
void commit_trans(); void end_op();
// mp.c // mp.c
extern int ismp; extern int ismp;

8
exec.c
View file

@ -18,9 +18,9 @@ exec(char *path, char **argv)
struct proghdr ph; struct proghdr ph;
pde_t *pgdir, *oldpgdir; pde_t *pgdir, *oldpgdir;
begin_trans(); begin_op();
if((ip = namei(path)) == 0){ if((ip = namei(path)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
ilock(ip); ilock(ip);
@ -50,7 +50,7 @@ exec(char *path, char **argv)
goto bad; goto bad;
} }
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
ip = 0; ip = 0;
// Allocate two pages at the next page boundary. // Allocate two pages at the next page boundary.
@ -101,7 +101,7 @@ exec(char *path, char **argv)
freevm(pgdir); freevm(pgdir);
if(ip){ if(ip){
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
} }
return -1; return -1;
} }

8
file.c
View file

@ -72,9 +72,9 @@ fileclose(struct file *f)
if(ff.type == FD_PIPE) if(ff.type == FD_PIPE)
pipeclose(ff.pipe, ff.writable); pipeclose(ff.pipe, ff.writable);
else if(ff.type == FD_INODE){ else if(ff.type == FD_INODE){
begin_trans(); begin_op();
iput(ff.ip); iput(ff.ip);
commit_trans(); end_op();
} }
} }
@ -136,12 +136,12 @@ filewrite(struct file *f, char *addr, int n)
if(n1 > max) if(n1 > max)
n1 = max; n1 = max;
begin_trans(); begin_op();
ilock(f->ip); ilock(f->ip);
if ((r = writei(f->ip, addr + i, f->off, n1)) > 0) if ((r = writei(f->ip, addr + i, f->off, n1)) > 0)
f->off += r; f->off += r;
iunlock(f->ip); iunlock(f->ip);
commit_trans(); end_op();
if(r < 0) if(r < 0)
break; break;

80
log.c
View file

@ -5,18 +5,22 @@
#include "fs.h" #include "fs.h"
#include "buf.h" #include "buf.h"
// Simple logging. Each file system system call // Simple logging that allows concurrent FS system calls.
// should be surrounded with begin_trans() and commit_trans() calls.
// //
// The log holds at most one transaction at a time. Commit forces // A log transaction contains the updates of *multiple* FS system
// the log (with commit record) to disk, then installs the affected // calls. The logging systems only commits when there are
// blocks to disk, then erases the log. begin_trans() ensures that // no FS system calls active. Thus there is never
// only one system call can be in a transaction; others must wait. // any reasoning required about whether a commit might
// write an uncommitted system call's updates to disk.
// //
// Allowing only one transaction at a time means that the file // A system call should call begin_op()/end_op() to mark
// system code doesn't have to worry about the possibility of // its start and end. Usually begin_op() just increments
// one transaction reading a block that another one has modified, // the count of in-progress FS system calls and returns.
// for example an i-node block. // But if it thinks the log is close to running out, it
// blocks this system call, and causes the system to wait
// until end_op() indicates there are no executing FS
// system calls, at which point the last end_op() commits
// all the system calls' writes.
// //
// The log is a physical re-do log containing disk blocks. // The log is a physical re-do log containing disk blocks.
// The on-disk log format: // The on-disk log format:
@ -38,13 +42,15 @@ struct log {
struct spinlock lock; struct spinlock lock;
int start; int start;
int size; 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; int dev;
struct logheader lh; struct logheader lh;
}; };
struct log log; struct log log;
static void recover_from_log(void); static void recover_from_log(void);
static void commit();
void void
initlog(void) initlog(void)
@ -117,19 +123,52 @@ recover_from_log(void)
write_head(); // clear the log write_head(); // clear the log
} }
// an FS system call should call begin_op() when it starts.
void void
begin_trans(void) begin_op(void)
{ {
acquire(&log.lock); acquire(&log.lock);
while (log.busy) { while(1){
sleep(&log, &log.lock); if(log.committing){
sleep(&log, &log.lock);
} else {
// XXX wait (for a commit) if log is longish.
// need to reserve to avoid over-commit of log space.
log.outstanding += 1;
release(&log.lock);
break;
}
} }
log.busy = 1;
release(&log.lock);
} }
// an FS system call should call end_op() after it finishes.
// can't write the disk &c while holding locks, thus do_commit.
void 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;
}
release(&log.lock);
if(do_commit){
commit();
acquire(&log.lock);
log.committing = 0;
wakeup(&log);
release(&log.lock);
}
}
static void
commit()
{ {
if (log.lh.n > 0) { if (log.lh.n > 0) {
write_head(); // Write header to disk -- the real commit write_head(); // Write header to disk -- the real commit
@ -137,11 +176,6 @@ commit_trans(void)
log.lh.n = 0; log.lh.n = 0;
write_head(); // Erase the transaction from the log 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. // Caller has modified b->data and is done with the buffer.
@ -159,7 +193,7 @@ log_write(struct buf *b)
if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1) if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1)
panic("too big a transaction"); panic("too big a transaction");
if (!log.busy) if (log.outstanding < 1)
panic("write outside of trans"); panic("write outside of trans");
for (i = 0; i < log.lh.n; i++) { for (i = 0; i < log.lh.n; i++) {

4
proc.c
View file

@ -186,9 +186,9 @@ exit(void)
} }
} }
begin_trans(); begin_op();
iput(proc->cwd); iput(proc->cwd);
commit_trans(); end_op();
proc->cwd = 0; proc->cwd = 0;
acquire(&ptable.lock); acquire(&ptable.lock);

View file

@ -121,16 +121,16 @@ sys_link(void)
if(argstr(0, &old) < 0 || argstr(1, &new) < 0) if(argstr(0, &old) < 0 || argstr(1, &new) < 0)
return -1; return -1;
begin_trans(); begin_op();
if((ip = namei(old)) == 0){ if((ip = namei(old)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
ilock(ip); ilock(ip);
if(ip->type == T_DIR){ if(ip->type == T_DIR){
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return -1; return -1;
} }
@ -148,7 +148,7 @@ sys_link(void)
iunlockput(dp); iunlockput(dp);
iput(ip); iput(ip);
commit_trans(); end_op();
return 0; return 0;
@ -157,7 +157,7 @@ bad:
ip->nlink--; ip->nlink--;
iupdate(ip); iupdate(ip);
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return -1; return -1;
} }
@ -189,9 +189,9 @@ sys_unlink(void)
if(argstr(0, &path) < 0) if(argstr(0, &path) < 0)
return -1; return -1;
begin_trans(); begin_op();
if((dp = nameiparent(path, name)) == 0){ if((dp = nameiparent(path, name)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
@ -225,13 +225,13 @@ sys_unlink(void)
iupdate(ip); iupdate(ip);
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return 0; return 0;
bad: bad:
iunlockput(dp); iunlockput(dp);
commit_trans(); end_op();
return -1; return -1;
} }
@ -291,23 +291,23 @@ sys_open(void)
if(argstr(0, &path) < 0 || argint(1, &omode) < 0) if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
return -1; return -1;
begin_trans(); begin_op();
if(omode & O_CREATE){ if(omode & O_CREATE){
ip = create(path, T_FILE, 0, 0); ip = create(path, T_FILE, 0, 0);
if(ip == 0){ if(ip == 0){
commit_trans(); end_op();
return -1; return -1;
} }
} else { } else {
if((ip = namei(path)) == 0){ if((ip = namei(path)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
ilock(ip); ilock(ip);
if(ip->type == T_DIR && omode != O_RDONLY){ if(ip->type == T_DIR && omode != O_RDONLY){
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return -1; return -1;
} }
} }
@ -316,11 +316,11 @@ sys_open(void)
if(f) if(f)
fileclose(f); fileclose(f);
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return -1; return -1;
} }
iunlock(ip); iunlock(ip);
commit_trans(); end_op();
f->type = FD_INODE; f->type = FD_INODE;
f->ip = ip; f->ip = ip;
@ -336,13 +336,13 @@ sys_mkdir(void)
char *path; char *path;
struct inode *ip; struct inode *ip;
begin_trans(); begin_op();
if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){ if(argstr(0, &path) < 0 || (ip = create(path, T_DIR, 0, 0)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return 0; return 0;
} }
@ -354,16 +354,16 @@ sys_mknod(void)
int len; int len;
int major, minor; int major, minor;
begin_trans(); begin_op();
if((len=argstr(0, &path)) < 0 || if((len=argstr(0, &path)) < 0 ||
argint(1, &major) < 0 || argint(1, &major) < 0 ||
argint(2, &minor) < 0 || argint(2, &minor) < 0 ||
(ip = create(path, T_DEV, major, minor)) == 0){ (ip = create(path, T_DEV, major, minor)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return 0; return 0;
} }
@ -373,20 +373,20 @@ sys_chdir(void)
char *path; char *path;
struct inode *ip; struct inode *ip;
begin_trans(); begin_op();
if(argstr(0, &path) < 0 || (ip = namei(path)) == 0){ if(argstr(0, &path) < 0 || (ip = namei(path)) == 0){
commit_trans(); end_op();
return -1; return -1;
} }
ilock(ip); ilock(ip);
if(ip->type != T_DIR){ if(ip->type != T_DIR){
iunlockput(ip); iunlockput(ip);
commit_trans(); end_op();
return -1; return -1;
} }
iunlock(ip); iunlock(ip);
iput(proc->cwd); iput(proc->cwd);
commit_trans(); end_op();
proc->cwd = ip; proc->cwd = ip;
return 0; return 0;
} }