]> Devi Nivas Git - cs3210-lab0.git/commitdiff
open()
authorrtm <rtm>
Sat, 29 Jul 2006 09:35:02 +0000 (09:35 +0000)
committerrtm <rtm>
Sat, 29 Jul 2006 09:35:02 +0000 (09:35 +0000)
17 files changed:
bio.c
console.c
defs.h
fd.c
fd.h
fs.c
ide.c
kalloc.c
main.c
proc.c
proc.h
spinlock.c
spinlock.h
syscall.c
syscall.h
userfs.c
usys.S

diff --git a/bio.c b/bio.c
index 9be11df492afed2d71ad3cd0e3f937b8d2dead95..c1a4bd63cf49d7437724d646129b53791bbb554f 100644 (file)
--- a/bio.c
+++ b/bio.c
@@ -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()
index 9756c84df8e7d4cac0ae9e016e0436058aa3c709..9a7d7254607364bf32bbc3b2a5a09a64e2204cc6 100644 (file)
--- a/console.c
+++ b/console.c
@@ -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;
 
diff --git a/defs.h b/defs.h
index 84616c99df4ae1834fbf29eb0c95f34e47f1445a..c6c333bd58939b250bb2120d9bc69672d41747fa 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -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);
diff --git a/fd.c b/fd.c
index 6ce4aec00d68cdb9cb99ace7ffb18f56e68b95c7..9ce6bae5a8a49ce4e6770dcbac3ac4e8916d7630 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -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");
     }
diff --git a/fd.h b/fd.h
index aa03af9a233dd869336131d63e65f37952ed8c2c..442ee343d83bf45db12b34b3639f870a10c12781 100644 (file)
--- a/fd.h
+++ b/fd.h
@@ -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];
diff --git a/fs.c b/fs.c
index 3b06bc710ef0d60e702be8997a72c5b5dfbb937d..0e987c26dd446f5b10efeb8a19a2e58e5e34ac34 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -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);
 }
diff --git a/ide.c b/ide.c
index 88a1d4d1468c10a36aa81df9c7f3f15306036f03..27a76fb255d3d02eec4cde4566cabd8d2f1c3bcc 100644 (file)
--- a/ide.c
+++ b/ide.c
@@ -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;
 
index 0b22c9118ec88b43ad8d41ba98ee205b0302f617..c13a639dc360cf51f51c5af40b774b1471979629 100644 (file)
--- a/kalloc.c
+++ b/kalloc.c
@@ -15,7 +15,7 @@
 #include "proc.h"
 #include "spinlock.h"
 
-struct spinlock kalloc_lock;
+struct spinlock kalloc_lock = { "kalloc" };
 
 struct run {
   struct run *next;
diff --git a/main.c b/main.c
index 1b8bdbe7ad19fc0c11e89514a4e2d151a7b36665..3db7eab7b981f9bd1852de44f31dace61d346e5d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -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();
diff --git a/proc.c b/proc.c
index 4e44a8ef6009538df1043dc703a5210fffa27d3d..b67810e9221e1706abf542977236886d7656877e 100644 (file)
--- a/proc.c
+++ b/proc.c
@@ -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.
diff --git a/proc.h b/proc.h
index 1b86eb2e43e1b872c4e27e74b7a8aded250ce4b1..a27314113c7320dc96e0935c0b8bb01fa03e8ac6 100644 (file)
--- a/proc.h
+++ b/proc.h
@@ -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];
index 171afaf01c2737524442e8cd39f8c705601ac7be..bde6e461815ef63f998d0f4c6631c5331c57bc92 100644 (file)
@@ -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)
index 0572124bfb0592922bd0c1d6cda01d41998628a8..ee48a7e2620ba7787cad917d148dc655ccbd3df9 100644 (file)
@@ -1,4 +1,5 @@
 struct spinlock {
+  char *name;
   uint locked;
   uint pc;
   int cpu;
index ab9dd170cfcd98adf937e87bef632626e2dbe181..43be5342b3b9ba13007701744ce6d66dbeb9a035 100644 (file)
--- a/syscall.c
+++ b/syscall.c
@@ -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.
@@ -243,6 +244,41 @@ sys_cons_puts(void)
   return 0;
 }
 
+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)
 {
@@ -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
index 799d8ed291796ffe0cb95cb6a6238fd6675cbec0..e894200c9103781143c03a1a3264a92811dec22b 100644 (file)
--- a/syscall.h
+++ b/syscall.h
@@ -11,3 +11,4 @@
 #define SYS_panic 11
 #define SYS_cons_puts 12
 #define SYS_exec 13
+#define SYS_open 14
index d3a4923658545c51546338cdc08f61affa5ca51e..c263868e8c087191837988592d548c12662389c7 100644 (file)
--- a/userfs.c
+++ b/userfs.c
@@ -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;
 }
diff --git a/usys.S b/usys.S
index 09c753ee82fe7e42f9bc49f969aeb0f95b0c5f6c..2c3c855d954321af6832c2e5ae8b9920480dfc04 100644 (file)
--- a/usys.S
+++ b/usys.S
@@ -21,3 +21,4 @@ STUB(kill)
 STUB(panic)
 STUB(cons_puts)
 STUB(exec)
+STUB(open)