diff options
| author | rtm <rtm> | 2006-07-29 09:35:02 +0000 | 
|---|---|---|
| committer | rtm <rtm> | 2006-07-29 09:35:02 +0000 | 
| commit | 32630628a996e29018641af262272339ed6fef88 (patch) | |
| tree | 73c9a7dee75f96c0ce0e9c804d379dd60bf254b4 | |
| parent | e46fb46fcf4302bf5ed913101c5c7b510fe03ad4 (diff) | |
| download | xv6-labs-32630628a996e29018641af262272339ed6fef88.tar.gz xv6-labs-32630628a996e29018641af262272339ed6fef88.tar.bz2 xv6-labs-32630628a996e29018641af262272339ed6fef88.zip  | |
open()
| -rw-r--r-- | bio.c | 2 | ||||
| -rw-r--r-- | console.c | 2 | ||||
| -rw-r--r-- | defs.h | 2 | ||||
| -rw-r--r-- | fd.c | 2 | ||||
| -rw-r--r-- | fd.h | 3 | ||||
| -rw-r--r-- | fs.c | 9 | ||||
| -rw-r--r-- | ide.c | 2 | ||||
| -rw-r--r-- | kalloc.c | 2 | ||||
| -rw-r--r-- | main.c | 2 | ||||
| -rw-r--r-- | proc.c | 6 | ||||
| -rw-r--r-- | proc.h | 2 | ||||
| -rw-r--r-- | spinlock.c | 10 | ||||
| -rw-r--r-- | spinlock.h | 1 | ||||
| -rw-r--r-- | syscall.c | 39 | ||||
| -rw-r--r-- | syscall.h | 1 | ||||
| -rw-r--r-- | userfs.c | 16 | ||||
| -rw-r--r-- | usys.S | 1 | 
17 files changed, 89 insertions, 13 deletions
@@ -8,7 +8,7 @@  #include "buf.h"  struct buf buf[NBUF]; -struct spinlock buf_table_lock; +struct spinlock buf_table_lock = { "buf_table" };  struct buf *  getblk() @@ -3,7 +3,7 @@  #include "defs.h"  #include "spinlock.h" -struct spinlock console_lock; +struct spinlock console_lock = { "console" };  int panicked = 0;  int use_console_lock = 0; @@ -99,7 +99,7 @@ void brelse(struct buf *);  struct inode * iget(uint dev, uint inum);  void ilock(struct inode *ip);  void iunlock(struct inode *ip); -void iincref(struct inode *ip); +void idecref(struct inode *ip);  void iput(struct inode *ip);  struct inode * namei(char *path);  int readi(struct inode *ip, void *xdst, uint off, uint n); @@ -86,6 +86,8 @@ fd_close(struct fd *fd)    if(--fd->ref == 0){      if(fd->type == FD_PIPE){        pipe_close(fd->pipe, fd->writeable); +    } else if(fd->type == FD_FILE){ +      idecref(fd->ip);      } else {        panic("fd_close");      } @@ -1,9 +1,10 @@  struct fd { -  enum { FD_CLOSED, FD_NONE, FD_PIPE } type; +  enum { FD_CLOSED, FD_NONE, FD_PIPE, FD_FILE } type;    int ref; // reference count    char readable;    char writeable;    struct pipe *pipe; +  struct inode *ip;  };  extern struct fd fds[NFD]; @@ -12,7 +12,7 @@  // these are inodes currently in use  // an entry is free if count == 0  struct inode inode[NINODE]; -struct spinlock inode_table_lock; +struct spinlock inode_table_lock = { "inode_table" };  uint rootdev = 1; @@ -111,11 +111,14 @@ iput(struct inode *ip)  }  void -iincref(struct inode *ip) +idecref(struct inode *ip)  {    acquire(&inode_table_lock); -  ip->count += 1; +  if(ip->count < 1) +    panic("idecref"); + +  ip->count -= 1;    release(&inode_table_lock);  } @@ -25,7 +25,7 @@ struct ide_request {  };  struct ide_request request[NREQUEST];  int head, tail; -struct spinlock ide_lock; +struct spinlock ide_lock = { "ide" };  int disk_channel; @@ -15,7 +15,7 @@  #include "proc.h"  #include "spinlock.h" -struct spinlock kalloc_lock; +struct spinlock kalloc_lock = { "kalloc" };  struct run {    struct run *next; @@ -95,6 +95,8 @@ mpmain(void)  {    cprintf("an application processor\n");    idtinit(); // CPU's idt +  if(cpu() == 0) +    panic("mpmain on cpu 0");    lapic_init(cpu());    lapic_timerinit();    lapic_enableintr(); @@ -7,7 +7,7 @@  #include "defs.h"  #include "spinlock.h" -struct spinlock proc_table_lock; +struct spinlock proc_table_lock = { "proc_table" };  struct proc proc[NPROC];  struct proc *curproc[NCPU]; @@ -137,8 +137,10 @@ scheduler(void)    cprintf("start scheduler on cpu %d jmpbuf %p\n", cpu(), &cpus[cpu()].jmpbuf);    cpus[cpu()].lastproc = &proc[0]; -  if(cpus[cpu()].nlock != 0) +  if(cpus[cpu()].nlock != 0){ +    cprintf("la %x lr %x\n", cpus[cpu()].lastacquire, cpus[cpu()].lastrelease   );      panic("holding locks at first entry to scheduler"); +  }    for(;;){      // Loop over process table looking for process to run. @@ -70,6 +70,8 @@ struct cpu {    char mpstack[MPSTACK]; // per-cpu start-up stack, only used to get into main()    struct proc *lastproc;  // last proc scheduled on this cpu (never NULL)    int nlock; // # of locks currently held +  struct spinlock *lastacquire; // xxx debug +  struct spinlock *lastrelease; // xxx debug  };  extern struct cpu cpus[NCPU]; @@ -8,7 +8,7 @@  // Can't call cprintf from inside these routines,  // because cprintf uses them itself. -#define cprintf dont_use_cprintf +//#define cprintf dont_use_cprintf  extern int use_console_lock; @@ -21,8 +21,12 @@ getcallerpc(void *v)  void  acquire(struct spinlock * lock)  { -	if(holding(lock)) +  if(holding(lock)){ +    extern use_console_lock; +    use_console_lock = 0; +    cprintf("lock %s pc %x\n", lock->name ? lock->name : "", lock->pc);  		panic("acquire"); +  }  	if(cpus[cpu()].nlock++ == 0)  		cli(); @@ -31,6 +35,7 @@ acquire(struct spinlock * lock)  	cpuid(0, 0, 0, 0, 0);	// memory barrier  	lock->pc = getcallerpc(&lock);  	lock->cpu = cpu(); +        cpus[cpu()].lastacquire = lock;  }  void @@ -39,6 +44,7 @@ release(struct spinlock * lock)  	if(!holding(lock))  		panic("release"); +        cpus[cpu()].lastrelease = lock;  	cpuid(0, 0, 0, 0, 0);	// memory barrier  	lock->locked = 0;  	if(--cpus[cpu()].nlock == 0) @@ -1,4 +1,5 @@  struct spinlock { +  char *name;    uint locked;    uint pc;    int cpu; @@ -11,6 +11,7 @@  #include "fs.h"  #include "fsvar.h"  #include "elf.h" +#include "fd.h"  /*   * User code makes a system call with INT T_SYSCALL. @@ -244,6 +245,41 @@ sys_cons_puts(void)  }  int +sys_open(void) +{ +  struct proc *cp = curproc[cpu()]; +  struct inode *ip; +  uint arg0, arg1; +  int ufd; +  struct fd *fd; + +  if(fetcharg(0, &arg0) < 0 || fetcharg(1, &arg1) < 0) +    return -1; +  if(checkstring(arg0) < 0) +    return -1; +  if((ip = namei(cp->mem + arg0)) == 0) +    return -1; +  if((fd = fd_alloc()) == 0){ +    iput(ip); +    return -1; +  } +  if((ufd = fd_ualloc()) < 0){ +    iput(ip); +    fd_close(fd); +    return -1; +  } + +  iunlock(ip); +  fd->type = FD_FILE; +  fd->readable = 1; +  fd->writeable = 0; +  fd->ip = ip; +  cp->fds[ufd] = fd; + +  return ufd; +} + +int  sys_exec(void)  {    struct proc *cp = curproc[cpu()]; @@ -467,6 +503,9 @@ syscall(void)    case SYS_exec:      ret = sys_exec();      break; +  case SYS_open: +    ret = sys_open(); +    break;    default:      cprintf("unknown sys call %d\n", num);      // XXX fault @@ -11,3 +11,4 @@  #define SYS_panic 11  #define SYS_cons_puts 12  #define SYS_exec 13 +#define SYS_open 14 @@ -8,8 +8,24 @@ char *args[] = { "echo", "hello", "goodbye", 0 };  int  main(void)  { +  int fd; +    puts("userfs running\n");    block(); +  fd = open("echo", 0); +  if(fd >= 0){ +    puts("open echo ok\n"); +    close(fd); +  } else { +    puts("open echo failed!\n"); +  } +  fd = open("doesnotexist", 0); +  if(fd >= 0){ +    puts("open doesnotexist succeeded!\n"); +    close(fd); +  } else { +    puts("open doesnotexist failed\n"); +  }    exec("echo", args);    return 0;  } @@ -21,3 +21,4 @@ STUB(kill)  STUB(panic)  STUB(cons_puts)  STUB(exec) +STUB(open)  | 
