Add support for "keyboard" input over the serial port.
[pintos-anon] / solutions / p4.patch
index 9e11e251463505459c8bb2df0a4dbd5d0b7f83b5..fff8ca1cba1625519d1611ec845392aa75f42a41 100644 (file)
@@ -1,12 +1,12 @@
 Index: src/Makefile.build
 diff -u src/Makefile.build~ src/Makefile.build
---- src/Makefile.build~ 2005-06-18 20:20:47.000000000 -0700
-+++ src/Makefile.build 2006-04-08 19:57:07.000000000 -0700
+--- src/Makefile.build~
++++ src/Makefile.build
 @@ -53,7 +53,9 @@ userprog_SRC += userprog/gdt.c               # GDT in
  userprog_SRC += userprog/tss.c                # TSS management.
  
  # No virtual memory code yet.
--#vm_SRC = vm/filename.c                       # Some file.
+-#vm_SRC = vm/file.c                   # Some file.
 +vm_SRC = vm/page.c
 +vm_SRC += vm/frame.c
 +vm_SRC += vm/swap.c
@@ -23,8 +23,8 @@ diff -u src/Makefile.build~ src/Makefile.build
  OBJECTS = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES)))
 Index: src/devices/timer.c
 diff -u src/devices/timer.c~ src/devices/timer.c
---- src/devices/timer.c~ 2005-07-06 13:45:36.000000000 -0700
-+++ src/devices/timer.c 2006-04-08 19:57:07.000000000 -0700
+--- src/devices/timer.c~
++++ src/devices/timer.c
 @@ -23,6 +23,9 @@ static volatile int64_t ticks;
     Initialized by timer_calibrate(). */
  static unsigned loops_per_tick;
@@ -103,8 +103,8 @@ diff -u src/devices/timer.c~ src/devices/timer.c
  /* Returns true if LOOPS iterations waits for more than one timer
 Index: src/filesys/Make.vars
 diff -u src/filesys/Make.vars~ src/filesys/Make.vars
---- src/filesys/Make.vars~ 2005-06-20 13:24:21.000000000 -0700
-+++ src/filesys/Make.vars 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/Make.vars~
++++ src/filesys/Make.vars
 @@ -6,7 +6,7 @@ TEST_SUBDIRS = tests/userprog tests/file
  GRADING_FILE = $(SRCDIR)/tests/filesys/Grading.no-vm
  
@@ -119,8 +119,8 @@ diff -u src/filesys/Make.vars~ src/filesys/Make.vars
 +GRADING_FILE = $(SRCDIR)/tests/filesys/Grading.with-vm
 Index: src/filesys/cache.c
 diff -u src/filesys/cache.c~ src/filesys/cache.c
---- src/filesys/cache.c~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/filesys/cache.c 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/cache.c~
++++ src/filesys/cache.c
 @@ -0,0 +1,473 @@
 +#include "filesys/cache.h"
 +#include <debug.h>
@@ -387,7 +387,7 @@ diff -u src/filesys/cache.c~ src/filesys/cache.c
 +
 +  /* Wait for cache contention to die down. */
 +  lock_release (&cache_sync);
-+  timer_sleep (1000);
++  timer_msleep (1000);
 +  goto try_again;
 +}
 +
@@ -597,8 +597,8 @@ diff -u src/filesys/cache.c~ src/filesys/cache.c
 +}
 Index: src/filesys/cache.h
 diff -u src/filesys/cache.h~ src/filesys/cache.h
---- src/filesys/cache.h~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/filesys/cache.h 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/cache.h~
++++ src/filesys/cache.h
 @@ -0,0 +1,23 @@
 +#ifndef FILESYS_CACHE_H
 +#define FILESYS_CACHE_H
@@ -625,135 +625,69 @@ diff -u src/filesys/cache.h~ src/filesys/cache.h
 +#endif /* filesys/cache.h */
 Index: src/filesys/directory.c
 diff -u src/filesys/directory.c~ src/filesys/directory.c
---- src/filesys/directory.c~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/directory.c 2006-04-08 19:57:07.000000000 -0700
-@@ -3,12 +3,17 @@
+--- src/filesys/directory.c~
++++ src/filesys/directory.c
+@@ -1,4 +1,5 @@
  #include <string.h>
  #include <list.h>
++#include "filesys/free-map.h"
  #include "filesys/filesys.h"
-+#include "filesys/fsutil.h"
  #include "filesys/inode.h"
- #include "threads/malloc.h"
-+#include "threads/synch.h"
- /* A directory. */
- struct dir 
-   {
-+    struct list_elem list_elem;         /* open_dirs list element. */
-+    struct lock dir_lock;               /* Protects inode. */
-+    int open_cnt;                       /* Number of openers. */
-     struct inode *inode;                /* Backing store. */
-   };
-@@ -20,15 +25,21 @@ struct dir_entry 
+@@ -21,12 +21,39 @@ struct dir_entry 
      bool in_use;                        /* In use or free? */
    };
  
 -/* Creates a directory with space for ENTRY_CNT entries in the
 -   given SECTOR.  Returns true if successful, false on failure. */
++/* Creates a directory in the given SECTOR.
++   The directory's parent is in PARENT_SECTOR.
++   Returns inode of created directory if successful,
++   null pointer on faiilure.
++   On failure, SECTOR is released in the free map. */
 -bool
++struct inode *
 -dir_create (disk_sector_t sector, size_t entry_cnt) 
-+/* List of all open directories. */
-+static struct list open_dirs;
-+
-+/* Protects open_dirs additions or removals. */
-+static struct lock open_dirs_lock;
-+
-+/* Initializes directory modules. */
-+void
-+dir_init (void) 
++dir_create (disk_sector_t sector, disk_sector_t parent_sector) 
  {
 -  return inode_create (sector, entry_cnt * sizeof (struct dir_entry));
-+  list_init (&open_dirs);
-+  lock_init (&open_dirs_lock);
- }
--/* Opens the directory in the given INODE, of which it takes
-+/* Opens the directory for the given INODE, of which it takes
-    ownership, and sets *DIRP to the new directory or a null
-    pointer on failure.  Return true if successful, false on
-    failure. */
-@@ -36,19 +47,46 @@ bool
- dir_open (struct inode *inode, struct dir **dirp) 
- {
-   struct dir *dir = NULL;
-+  struct list_elem *e;
-   
-   ASSERT (dirp != NULL);
--  if (inode != NULL) 
-+  lock_acquire (&open_dirs_lock);
++  struct inode *inode = inode_create (sector, DIR_INODE);
++  if (inode != NULL) 
++    {
++      struct dir_entry entries[2];
 +
-+  if (inode == NULL)
-+    goto done;
++      memset (entries, 0, sizeof entries);
 +
-+  /* Inode must refer to directory. */
-+  if (inode_get_type (inode) != DIR_INODE)
-+    goto done;
++      /* "." entry. */
++      entries[0].inode_sector = sector;
++      strlcpy (entries[0].name, ".", sizeof entries[0].name);
++      entries[0].in_use = true;
 +
-+  /* Check whether this directory is already open. */
-+  for (e = list_begin (&open_dirs); e != list_end (&open_dirs);
-+       e = list_next (e)) 
-     {
--      dir = malloc (sizeof *dir);
--      if (dir != NULL) 
--        dir->inode = inode;
-+      dir = list_entry (e, struct dir, list_elem);
-+      if (dir->inode == inode) 
++      /* ".." entry. */
++      entries[1].inode_sector = parent_sector;
++      strlcpy (entries[1].name, "..", sizeof entries[1].name);
++      entries[1].in_use = true;
++      
++      if (inode_write_at (inode, entries, sizeof entries, 0) != sizeof entries)
 +        {
-+          dir->open_cnt++;
-+          goto done;
-+        }
++          inode_remove (inode);
++          inode_close (inode); 
++          inode = NULL;
++        } 
 +    }
-+
-+  /* Create new directory. */
-+  dir = calloc (1, sizeof *dir);
-+  if (dir != NULL)
-+    {
-+      list_push_front (&open_dirs, &dir->list_elem);
-+      lock_init (&dir->dir_lock);
-+      dir->open_cnt = 1;
-+      dir->inode = inode;
-+      inode_reopen (dir->inode);
-     }
-   
-+ done:
-   *dirp = dir;
--  if (dir == NULL)
--    inode_close (inode);
-+  inode_close (inode);
-+  lock_release (&open_dirs_lock);
-   return dir != NULL;
- }
-@@ -61,22 +99,34 @@ dir_open_root (struct dir **dirp)
-   return dir_open (inode_open (ROOT_DIR_SECTOR), dirp);
++  return inode;
  }
  
-+/* Re-opens DIR and returns true. */
-+bool
-+dir_reopen (struct dir *dir)
-+{
-+  dir->open_cnt++;
-+  return true;
-+}
-+
- /* Destroys DIR and frees associated resources. */
- void
- dir_close (struct dir *dir) 
+ /* Opens and returns the directory for the given INODE, of which
+@@ -35,7 +59,7 @@ struct dir *
+ dir_open (struct inode *inode) 
  {
--  if (dir != NULL)
-+  if (dir == NULL)
-+    return;
-+
-+  lock_acquire (&open_dirs_lock);
-+  if (--dir->open_cnt == 0) 
+   struct dir *dir = calloc (1, sizeof *dir);
+-  if (inode != NULL && dir != NULL)
++  if (inode != NULL && dir != NULL && inode_get_type (inode) == DIR_INODE)
      {
-+      list_remove (&dir->list_elem);
-       inode_close (dir->inode);
-       free (dir);
-     }
-+  lock_release (&open_dirs_lock);
+       dir->inode = inode;
+       dir->pos = 0;
+@@ -84,10 +108,8 @@ dir_get_inode (struct dir *dir) 
  }
  
  /* Searches DIR for a file with the given NAME.
@@ -766,20 +700,28 @@ diff -u src/filesys/directory.c~ src/filesys/directory.c
  static bool
  lookup (const struct dir *dir, const char *name,
          struct dir_entry *ep, off_t *ofsp) 
-@@ -113,10 +163,12 @@ dir_lookup (const struct dir *dir, const
+@@ -120,15 +142,16 @@ dir_lookup (const struct dir *dir, const
+             struct inode **inode) 
+ {
+   struct dir_entry e;
++  bool ok;
    ASSERT (dir != NULL);
    ASSERT (name != NULL);
  
-+  lock_acquire ((struct lock *) &dir->dir_lock);
-   if (lookup (dir, name, &e, NULL))
-     *inode = inode_open (e.inode_sector);
-   else
-     *inode = NULL;
-+  lock_release ((struct lock *)&dir->dir_lock);
+-  if (lookup (dir, name, &e, NULL))
+-    *inode = inode_open (e.inode_sector);
+-  else
+-    *inode = NULL;
++  inode_lock (dir->inode);
++  ok = lookup (dir, name, &e, NULL);
++  inode_unlock (dir->inode);
  
++  *inode = ok ? inode_open (e.inode_sector) : NULL;
    return *inode != NULL;
  }
-@@ -138,10 +190,11 @@ dir_add (struct dir *dir, const char *na
+@@ -149,10 +172,11 @@ dir_add (struct dir *dir, const char *na
    ASSERT (name != NULL);
  
    /* Check NAME for validity. */
@@ -788,84 +730,132 @@ diff -u src/filesys/directory.c~ src/filesys/directory.c
      return false;
  
    /* Check that NAME is not in use. */
-+  lock_acquire (&dir->dir_lock);
++  inode_lock (dir->inode);
    if (lookup (dir, name, NULL, NULL))
      goto done;
  
-@@ -164,6 +217,7 @@ dir_add (struct dir *dir, const char *na
+@@ -175,6 +199,7 @@ dir_add (struct dir *dir, const char *na
    success = inode_write_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
  
   done:
-+  lock_release (&dir->dir_lock);
++  inode_unlock (dir->inode);
    return success;
  }
  
-@@ -182,12 +236,14 @@ dir_remove (struct dir *dir, const char 
+@@ -192,13 +217,37 @@ dir_remove (struct dir *dir, const char 
+   ASSERT (dir != NULL);
    ASSERT (name != NULL);
  
++  if (!strcmp (name, ".") || !strcmp (name, ".."))
++    return false;
++
    /* Find directory entry. */
-+  lock_acquire (&dir->dir_lock);
++  inode_lock (dir->inode);
    if (!lookup (dir, name, &e, &ofs))
      goto done;
  
--  /* Open inode. */
-+  /* Open inode and verify that it is not an in-use directory. */
+   /* Open inode. */
    inode = inode_open (e.inode_sector);
--  if (inode == NULL)
-+  if (inode == NULL
-+      || (inode_get_type (inode) == DIR_INODE && inode_open_cnt (inode) != 1))
+   if (inode == NULL)
      goto done;
  
++  /* Verify that it is not an in-use or non-empty directory. */
++  if (inode_get_type (inode) == DIR_INODE)
++    {
++      struct dir_entry e2;
++      off_t pos;
++
++      if (inode_open_cnt (inode) != 1)
++        goto done;
++
++      inode_lock (inode);
++      for (pos = 0; inode_read_at (inode, &e2, sizeof e2, pos) == sizeof e2;
++           pos += sizeof e2)
++        if (e2.in_use && strcmp (e2.name, ".") && strcmp (e2.name, ".."))
++          {
++            inode_unlock (inode);
++            goto done;
++          }
++      inode_unlock (inode);
++    }
++ 
    /* Erase directory entry. */
-@@ -201,6 +257,7 @@ dir_remove (struct dir *dir, const char 
+@@ -211,6 +241,7 @@ dir_remove (struct dir *dir, const char 
+   success = true;
  
   done:
++  inode_unlock (dir->inode);
    inode_close (inode);
-+  lock_release (&dir->dir_lock);
    return success;
  }
-@@ -211,8 +268,10 @@ dir_list (const struct dir *dir)
+@@ -223,14 +254,17 @@ dir_readdir (struct dir *dir, char name[
+ {
    struct dir_entry e;
-   size_t ofs;
-   
-+  lock_acquire ((struct lock *) &dir->dir_lock);
-   for (ofs = 0; inode_read_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
-        ofs += sizeof e) 
-     if (e.in_use)
-       printf ("%s\n", e.name);
-+  lock_release ((struct lock *) &dir->dir_lock);
++  inode_lock (dir->inode);
+   while (inode_read_at (dir->inode, &e, sizeof e, dir->pos) == sizeof e) 
+     {
+       dir->pos += sizeof e;
+-      if (e.in_use)
++      if (e.in_use && strcmp (e.name, ".") && strcmp (e.name, ".."))
+         {
++          inode_unlock (dir->inode);
+           strlcpy (name, e.name, NAME_MAX + 1);
+           return true;
+         } 
+     }
++  inode_unlock (dir->inode);
+   return false;
  }
 Index: src/filesys/directory.h
 diff -u src/filesys/directory.h~ src/filesys/directory.h
---- src/filesys/directory.h~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/directory.h 2006-04-08 19:57:07.000000000 -0700
-@@ -13,9 +13,10 @@
+--- src/filesys/directory.h~
++++ src/filesys/directory.h
+@@ -14,7 +14,7 @@
  struct inode;
- struct dir;
+ /* Opening and closing directories. */
 -bool dir_create (disk_sector_t sector, size_t entry_cnt);
-+void dir_init (void);
- bool dir_open (struct inode *, struct dir **);
- bool dir_open_root (struct dir **);
-+bool dir_reopen (struct dir *);
- void dir_close (struct dir *);
- bool dir_lookup (const struct dir *, const char *name, struct inode **);
- bool dir_add (struct dir *, const char *name, disk_sector_t);
++struct inode *dir_create (disk_sector_t sector, disk_sector_t parent_sector);
+ struct dir *dir_open (struct inode *);
+ struct dir *dir_open_root (void);
+ struct dir *dir_reopen (struct dir *);
 Index: src/filesys/file.c
 diff -u src/filesys/file.c~ src/filesys/file.c
---- src/filesys/file.c~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/file.c 2006-04-08 19:57:07.000000000 -0700
-@@ -1,6 +1,8 @@
+--- src/filesys/file.c~
++++ src/filesys/file.c
+@@ -1,4 +1,5 @@
  #include "filesys/file.h"
  #include <debug.h>
-+#include "filesys/directory.h"
++#include "filesys/free-map.h"
  #include "filesys/inode.h"
-+#include "filesys/filesys.h"
  #include "threads/malloc.h"
+@@ -11,6 +11,24 @@ struct file 
+     bool deny_write;            /* Has file_deny_write() been called? */
+   };
  
- /* An open file. */
-@@ -18,7 +20,7 @@ struct file *
++/* Creates a file in the given SECTOR,
++   initially LENGTH bytes long. 
++   Returns inode for the file on success, null pointer on failure.
++   On failure, SECTOR is released in the free map. */
++struct inode *
++file_create (disk_sector_t sector, off_t length) 
++{
++  struct inode *inode = inode_create (sector, FILE_INODE);
++  if (inode != NULL && length > 0
++      && inode_write_at (inode, "", 1, length - 1) != 1)
++    {
++      inode_remove (inode); 
++      inode_close (inode);
++      inode = NULL;
++    }
++  return inode;
++}
++
+ /* Opens a file for the given INODE, of which it takes ownership,
+    and returns the new file.  Returns a null pointer if an
+    allocation fails or if INODE is null. */
+@@ -18,7 +34,7 @@ struct file *
  file_open (struct inode *inode) 
  {
    struct file *file = calloc (1, sizeof *file);
@@ -874,10 +864,29 @@ diff -u src/filesys/file.c~ src/filesys/file.c
      {
        file->inode = inode;
        file->pos = 0;
+Index: src/filesys/file.h
+diff -u src/filesys/file.h~ src/filesys/file.h
+--- src/filesys/file.h~
++++ src/filesys/file.h
+@@ -1,11 +1,14 @@
+ #ifndef FILESYS_FILE_H
+ #define FILESYS_FILE_H
++#include <stdbool.h>
++#include "devices/disk.h"
+ #include "filesys/off_t.h"
+ struct inode;
+ /* Opening and closing files. */
++struct inode *file_create (disk_sector_t sector, off_t length);
+ struct file *file_open (struct inode *);
+ struct file *file_reopen (struct file *);
+ void file_close (struct file *);
 Index: src/filesys/filesys.c
 diff -u src/filesys/filesys.c~ src/filesys/filesys.c
---- src/filesys/filesys.c~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/filesys.c 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/filesys.c~
++++ src/filesys/filesys.c
 @@ -2,11 +2,13 @@
  #include <debug.h>
  #include <stdio.h>
@@ -890,18 +899,17 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
  #include "devices/disk.h"
 +#include "threads/thread.h"
  
- /* The disk that contains the filesystem. */
+ /* The disk that contains the file system. */
  struct disk *filesys_disk;
-@@ -23,6 +25,8 @@ filesys_init (bool format) 
-     PANIC ("hd0:1 (hdb) not present, filesystem initialization failed");
+@@ -23,6 +25,7 @@ filesys_init (bool format) 
+     PANIC ("hd0:1 (hdb) not present, file system initialization failed");
  
    inode_init ();
-+  dir_init ();
 +  cache_init ();
    free_map_init ();
  
    if (format) 
-@@ -37,6 +41,103 @@ void
+@@ -37,6 +40,130 @@ void
  filesys_done (void) 
  {
    free_map_close ();
@@ -946,10 +954,10 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
 +/* Resolves relative or absolute file NAME.
 +   Returns true if successful, false on failure.
 +   Stores the directory corresponding to the name into *DIRP,
-+   and the file name part into BASENAME. */
++   and the file name part into BASE_NAME. */
 +static bool
-+resolve_name (const char *name,
-+              struct dir **dirp, char basename[NAME_MAX + 1]) 
++resolve_name_to_entry (const char *name,
++                       struct dir **dirp, char base_name[NAME_MAX + 1]) 
 +{
 +  struct dir *dir = NULL;
 +  struct inode *inode;
@@ -958,17 +966,12 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
 +  int ok;
 +
 +  /* Find starting directory. */
-+  if (name[0] == '/' || thread_current ()->wd == NULL) 
-+    {
-+      if (!dir_open_root (&dir))
-+        goto error;
-+    }
-+  else 
-+    {
-+      if (!dir_reopen (thread_current ()->wd))
-+        goto error;
-+      dir = thread_current ()->wd;
-+    }
++  if (name[0] == '/' || thread_current ()->wd == NULL)
++    dir = dir_open_root ();
++  else
++    dir = dir_reopen (thread_current ()->wd);
++  if (dir == NULL)
++    goto error;
 +
 +  /* Get first name part. */
 +  cp = name;
@@ -983,7 +986,8 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
 +        goto error;
 +
 +      dir_close (dir);
-+      if (!dir_open (inode, &dir))
++      dir = dir_open (inode);
++      if (dir == NULL)
 +        goto error;
 +
 +      strlcpy (part, next_part, NAME_MAX + 1);
@@ -993,44 +997,92 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
 +
 +  /* Return our results. */
 +  *dirp = dir;
-+  strlcpy (basename, part, NAME_MAX + 1);
++  strlcpy (base_name, part, NAME_MAX + 1);
 +  return true;
 +
 + error:
 +  /* Return failure. */
 +  dir_close (dir);
 +  *dirp = NULL;
-+  basename[0] = '\0';
++  base_name[0] = '\0';
 +  return false;
++}
++
++/* Resolves relative or absolute file NAME to an inode.
++   Returns an inode if successful, or a null pointer on failure.
++   The caller is responsible for closing the returned inode. */
++static struct inode *
++resolve_name_to_inode (const char *name)
++{
++  if (name[0] == '/' && name[strspn (name, "/")] == '\0') 
++    {
++      /* The name represents the root directory.
++         There's no name part at all, so resolve_name_to_entry()
++         would reject it entirely.
++         Special case it. */
++      return inode_open (ROOT_DIR_SECTOR);
++    }
++  else 
++    {
++      struct dir *dir;
++      char base_name[NAME_MAX + 1];
++
++      if (resolve_name_to_entry (name, &dir, base_name)) 
++        {
++          struct inode *inode;
++          dir_lookup (dir, base_name, &inode);
++          dir_close (dir);
++          return inode; 
++        }
++      else
++        return NULL;
++    }
  }
  \f
  /* Creates a file named NAME with the given INITIAL_SIZE.
-@@ -44,16 +145,17 @@ filesys_done (void) 
+@@ -44,16 +171,32 @@ filesys_done (void) 
     Fails if a file named NAME already exists,
     or if internal memory allocation fails. */
  bool
 -filesys_create (const char *name, off_t initial_size) 
 +filesys_create (const char *name, off_t initial_size, enum inode_type type) 
  {
-   struct dir *dir;
-+  char basename[NAME_MAX + 1];
-   disk_sector_t inode_sector = 0;
--  bool success = (dir_open_root (&dir)
+-  disk_sector_t inode_sector = 0;
+-  struct dir *dir = dir_open_root ();
+-  bool success = (dir != NULL
 -                  && free_map_allocate (1, &inode_sector)
 -                  && inode_create (inode_sector, initial_size)
 -                  && dir_add (dir, name, inode_sector));
 -  if (!success && inode_sector != 0) 
 -    free_map_release (inode_sector, 1);
-+  bool success = (resolve_name (name, &dir, basename)
-+                  && free_map_allocate (&inode_sector)
-+                  && inode_create (inode_sector, initial_size, type)
-+                  && dir_add (dir, basename, inode_sector));
-+  if (!success && inode_sector) 
-+    free_map_release (inode_sector);
++  struct dir *dir;
++  char base_name[NAME_MAX + 1];
++  disk_sector_t inode_sector;
++
++  bool success = (resolve_name_to_entry (name, &dir, base_name)
++                  && free_map_allocate (&inode_sector));
++  if (success) 
++    {
++      struct inode *inode;
++      if (type == FILE_INODE)
++        inode = file_create (inode_sector, initial_size);
++      else
++        inode = dir_create (inode_sector,
++                            inode_get_inumber (dir_get_inode (dir))); 
++      if (inode != NULL)
++        {
++          success = dir_add (dir, base_name, inode_sector);
++          if (!success)
++            inode_remove (inode);
++          inode_close (inode);
++        }
++      else
++        success = false;
++    }
    dir_close (dir);
  
    return success;
-@@ -64,17 +166,18 @@ filesys_create (const char *name, off_t 
+@@ -64,17 +199,10 @@ filesys_create (const char *name, off_t 
     otherwise.
     Fails if no file named NAME exists,
     or if an internal memory allocation fails. */
@@ -1038,143 +1090,81 @@ diff -u src/filesys/filesys.c~ src/filesys/filesys.c
 +struct inode *
  filesys_open (const char *name)
  {
-   struct dir *dir;
-+  char basename[NAME_MAX + 1];
-   struct inode *inode = NULL;
--  if (dir_open_root (&dir))
+-  struct dir *dir = dir_open_root ();
+-  struct inode *inode = NULL;
+-
+-  if (dir != NULL)
 -    dir_lookup (dir, name, &inode);
-+  if (resolve_name (name, &dir, basename))
-+    dir_lookup (dir, basename, &inode);
-   dir_close (dir);
+-  dir_close (dir);
+-
 -  return file_open (inode);
-+  return inode;
++  return resolve_name_to_inode (name);
  }
  
  /* Deletes the file named NAME.
-@@ -85,13 +188,54 @@ bool
+@@ -84,12 +212,35 @@ filesys_open (const char *name)
+ bool
  filesys_remove (const char *name) 
  {
-   struct dir *dir = NULL;
--  bool success = (dir_open_root (&dir)
--                  && dir_remove (dir, name));
-+  char basename[NAME_MAX + 1];
-+  bool success = false;
-+
-+  if (resolve_name (name, &dir, basename)) 
-+    success = dir_remove (dir, basename);
-   dir_close (dir); 
+-  struct dir *dir = dir_open_root ();
+-  bool success = dir != NULL && dir_remove (dir, name);
+-  dir_close (dir); 
++  struct dir *dir;
++  char base_name[NAME_MAX + 1];
++  bool success;
  
++  if (resolve_name_to_entry (name, &dir, base_name)) 
++    {
++      success = dir_remove (dir, base_name);
++      dir_close (dir);
++    }
++  else
++    success = false;
++  
    return success;
  }
 +/* Change current directory to NAME.
 +   Return true if successful, false on failure. */
 +bool
 +filesys_chdir (const char *name) 
 +{
-+  struct dir *dir;
-+
-+  /* Find new directory. */
-+  if (*name == '\0')
-+    return false;
-+  else if (name[strspn (name, "/")] == '\0')
++  struct dir *dir = dir_open (resolve_name_to_inode (name));
++  if (dir != NULL) 
 +    {
-+      if (!dir_open_root (&dir))
-+        return false; 
++      dir_close (thread_current ()->wd);
++      thread_current ()->wd = dir;
++      return true;
 +    }
-+  else 
-+    {
-+      char basename[NAME_MAX + 1];
-+      struct inode *base_inode;
-+      struct dir *base_dir;
-+      if (!resolve_name (name, &dir, basename)
-+          || !dir_lookup (dir, basename, &base_inode)
-+          || !dir_open (base_inode, &base_dir))
-+        {
-+          dir_close (dir);
-+          return false;
-+        }
-+      dir_close (dir);
-+      dir = base_dir;
-+    }
-+
-+  /* Change current directory. */
-+  dir_close (thread_current ()->wd);
-+  thread_current ()->wd = dir;
-+  
-+  return true;
++  else
++    return false;
 +}
-+
- /* Prints a list of files in the filesystem to the system
-    console.
-    Returns true if successful, false on failure,
-@@ -99,13 +243,9 @@ filesys_remove (const char *name) 
- bool
- filesys_list (void) 
- {
--  struct dir *dir = NULL;
--  bool success = dir_open_root (&dir);
--  if (success)
--    dir_list (dir);
--  dir_close (dir);
-+  dir_list (thread_current ()->wd);
--  return success;
-+  return true;
- }
  \f
  static void must_succeed_function (int, bool) NO_INLINE;
-@@ -128,8 +268,8 @@ filesys_self_test (void)
-     {
-       /* Create file and check that it contains zeros
-          throughout the created length. */
--      MUST_SUCCEED (filesys_create ("foo", sizeof s));
--      MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
-+      MUST_SUCCEED (filesys_create ("foo", sizeof s, FILE_INODE));
-+      MUST_SUCCEED ((file = file_open (filesys_open ("foo"))) != NULL);
-       MUST_SUCCEED (file_read (file, s2, sizeof s2) == sizeof s2);
-       MUST_SUCCEED (memcmp (s2, zeros, sizeof s) == 0);
-       MUST_SUCCEED (file_tell (file) == sizeof s);
-@@ -137,7 +277,7 @@ filesys_self_test (void)
-       file_close (file);
-       /* Reopen file and write to it. */
--      MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
-+      MUST_SUCCEED ((file = file_open (filesys_open ("foo"))) != NULL);
-       MUST_SUCCEED (file_write (file, s, sizeof s) == sizeof s);
-       MUST_SUCCEED (file_tell (file) == sizeof s);
-       MUST_SUCCEED (file_length (file) == sizeof s);
-@@ -145,7 +285,7 @@ filesys_self_test (void)
-       /* Reopen file and verify that it reads back correctly.
-          Delete file while open to check proper semantics. */
--      MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
-+      MUST_SUCCEED ((file = file_open (filesys_open ("foo"))) != NULL);
-       MUST_SUCCEED (filesys_remove ("foo"));
-       MUST_SUCCEED (filesys_open ("foo") == NULL);
-       MUST_SUCCEED (file_read (file, s2, sizeof s) == sizeof s);
-@@ -172,9 +312,13 @@ static void
+ #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
+@@ -155,9 +306,18 @@ static void
  do_format (void)
  {
-   printf ("Formatting filesystem...");
++  struct inode *inode;
+   printf ("Formatting file system...");
 +
 +  /* Set up free map. */
    free_map_create ();
 -  if (!dir_create (ROOT_DIR_SECTOR, 16))
 +
 +  /* Set up root directory. */
-+  if (!inode_create (ROOT_DIR_SECTOR, 0, DIR_INODE))
++  inode = dir_create (ROOT_DIR_SECTOR, ROOT_DIR_SECTOR);
++  if (inode == NULL)
      PANIC ("root directory creation failed");
--  free_map_close ();
++  inode_close (inode);  
++
+   free_map_close ();
 +
    printf ("done.\n");
  }
 Index: src/filesys/filesys.h
 diff -u src/filesys/filesys.h~ src/filesys/filesys.h
---- src/filesys/filesys.h~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/filesys.h 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/filesys.h~
++++ src/filesys/filesys.h
 @@ -3,6 +3,7 @@
  
  #include <stdbool.h>
@@ -1183,7 +1173,7 @@ diff -u src/filesys/filesys.h~ src/filesys/filesys.h
  
  /* Sectors of system file inodes. */
  #define FREE_MAP_SECTOR 0       /* Free map file inode sector. */
-@@ -13,8 +14,8 @@ extern struct disk *filesys_disk;
+@@ -13,9 +14,10 @@ extern struct disk *filesys_disk;
  
  void filesys_init (bool format);
  void filesys_done (void);
@@ -1192,12 +1182,14 @@ diff -u src/filesys/filesys.h~ src/filesys/filesys.h
 +bool filesys_create (const char *name, off_t initial_size, enum inode_type);
 +struct inode *filesys_open (const char *name);
  bool filesys_remove (const char *name);
- bool filesys_chdir (const char *name);
- bool filesys_list (void);
++bool filesys_chdir (const char *name);
+ void filesys_self_test (void);
 Index: src/filesys/free-map.c
 diff -u src/filesys/free-map.c~ src/filesys/free-map.c
---- src/filesys/free-map.c~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/free-map.c 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/free-map.c~
++++ src/filesys/free-map.c
 @@ -3,15 +3,18 @@
  #include <debug.h>
  #include "filesys/file.h"
@@ -1278,25 +1270,23 @@ diff -u src/filesys/free-map.c~ src/filesys/free-map.c
    file_close (free_map_file);
  }
  
-@@ -72,7 +76,7 @@ void
+@@ -72,5 +76,9 @@ void
  free_map_create (void) 
  {
++  struct inode *inode;
++
    /* Create inode. */
 -  if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map)))
-+  if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map), FILE_INODE))
++  inode = file_create (FREE_MAP_SECTOR, 0);
++  if (inode == NULL)   
      PANIC ("free map creation failed");
++  inode_close (inode);
+
    /* Write bitmap to file. */
-@@ -81,4 +85,5 @@ free_map_create (void) 
-     PANIC ("can't open free map");
-   if (!bitmap_write (free_map, free_map_file))
-     PANIC ("can't write free map");
-+  file_close (free_map_file);
- }
 Index: src/filesys/free-map.h
 diff -u src/filesys/free-map.h~ src/filesys/free-map.h
---- src/filesys/free-map.h~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/free-map.h 2006-04-08 19:57:07.000000000 -0700
+--- src/filesys/free-map.h~
++++ src/filesys/free-map.h
 @@ -11,7 +11,7 @@ void free_map_create (void);
  void free_map_open (void);
  void free_map_close (void);
@@ -1309,43 +1299,43 @@ diff -u src/filesys/free-map.h~ src/filesys/free-map.h
  #endif /* filesys/free-map.h */
 Index: src/filesys/fsutil.c
 diff -u src/filesys/fsutil.c~ src/filesys/fsutil.c
---- src/filesys/fsutil.c~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/fsutil.c 2006-04-08 19:57:07.000000000 -0700
-@@ -30,7 +30,7 @@ fsutil_cat (char **argv)
+--- src/filesys/fsutil.c~
++++ src/filesys/fsutil.c
+@@ -38,7 +38,7 @@ fsutil_cat (char **argv)
    char *buffer;
  
-   printf ("Printing '%s' to the console...\n", filename);
--  file = filesys_open (filename);
-+  file = file_open (filesys_open (filename));
+   printf ("Printing '%s' to the console...\n", file_name);
+-  file = filesys_open (file_name);
++  file = file_open (filesys_open (file_name));
    if (file == NULL)
-     PANIC ("%s: open failed", filename);
+     PANIC ("%s: open failed", file_name);
    buffer = palloc_get_page (PAL_ASSERT);
-@@ -102,9 +102,9 @@ fsutil_put (char **argv) 
-     PANIC ("%s: invalid file size %d", filename, size);
+@@ -110,9 +110,9 @@ fsutil_put (char **argv) 
+     PANIC ("%s: invalid file size %d", file_name, size);
    
    /* Create destination file. */
--  if (!filesys_create (filename, size))
-+  if (!filesys_create (filename, size, FILE_INODE))
-     PANIC ("%s: create failed", filename);
--  dst = filesys_open (filename);
-+  dst = file_open (filesys_open (filename));
+-  if (!filesys_create (file_name, size))
++  if (!filesys_create (file_name, size, FILE_INODE))
+     PANIC ("%s: create failed", file_name);
+-  dst = filesys_open (file_name);
++  dst = file_open (filesys_open (file_name));
    if (dst == NULL)
-     PANIC ("%s: open failed", filename);
+     PANIC ("%s: open failed", file_name);
  
-@@ -154,7 +154,7 @@ fsutil_get (char **argv)
+@@ -162,7 +162,7 @@ fsutil_get (char **argv)
      PANIC ("couldn't allocate buffer");
  
    /* Open source file. */
--  src = filesys_open (filename);
-+  src = file_open (filesys_open (filename));
+-  src = filesys_open (file_name);
++  src = file_open (filesys_open (file_name));
    if (src == NULL)
-     PANIC ("%s: open failed", filename);
+     PANIC ("%s: open failed", file_name);
    size = file_length (src);
 Index: src/filesys/inode.c
 diff -u src/filesys/inode.c~ src/filesys/inode.c
---- src/filesys/inode.c~ 2006-04-08 12:09:33.000000000 -0700
-+++ src/filesys/inode.c 2006-04-08 20:03:00.000000000 -0700
-@@ -1,26 +1,41 @@
+--- src/filesys/inode.c~
++++ src/filesys/inode.c
+@@ -1,23 +1,38 @@
  #include "filesys/inode.h"
 +#include <bitmap.h>
  #include <list.h>
@@ -1385,15 +1375,12 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 -    uint32_t unused[125];               /* Not used. */
    };
  
--/* Returns the number of sectors to allocate for an inode SIZE
-+/* Returns the number of sectors to allocate for an indoe SIZE
-    bytes long. */
- static inline size_t
- bytes_to_sectors (off_t size)
-@@ -35,33 +50,29 @@ struct inode 
+ /* Returns the number of sectors to allocate for an inode SIZE
+@@ -35,74 +50,59 @@ struct inode 
      disk_sector_t sector;               /* Sector number of disk location. */
      int open_cnt;                       /* Number of openers. */
      bool removed;                       /* True if deleted, false otherwise. */
++    struct lock lock;                   /* Protects the inode. */
 +
 +    /* Denying writes. */
 +    struct lock deny_write_lock;        /* Protects members below. */
@@ -1434,24 +1421,29 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 +  lock_init (&open_inodes_lock);
  }
  
- /* Initializes an inode with LENGTH bytes of data and
-@@ -70,38 +81,35 @@ inode_init (void) 
-    Returns true if successful.
-    Returns false if memory or disk allocation fails. */
- bool
+-/* Initializes an inode with LENGTH bytes of data and
+-   writes the new inode to sector SECTOR on the file system
+-   disk.
+-   Returns true if successful.
+-   Returns false if memory or disk allocation fails. */
+-bool
 -inode_create (disk_sector_t sector, off_t length)
-+inode_create (disk_sector_t sector, off_t length, enum inode_type type) 
++/* Initializes an inode of the given TYPE, writes the new inode
++   to sector SECTOR on the file system disk, and returns the
++   inode thus created.  Returns a null pointer if unsuccessful,
++   in which case SECTOR is released in the free map. */  
++struct inode *
++inode_create (disk_sector_t sector, enum inode_type type) 
  {
 -  struct inode_disk *disk_inode = NULL;
 -  bool success = false;
 +  struct cache_block *block;
 +  struct inode_disk *disk_inode;
-+  bool success;
-   ASSERT (length >= 0);
++  struct inode *inode;
  
+-  ASSERT (length >= 0);
 +  block = cache_lock (sector, EXCLUSIVE);
-+
    /* If this assertion fails, the inode structure is not exactly
       one sector in size, and you should fix that. */
    ASSERT (sizeof *disk_inode == DISK_SECTOR_SIZE);
@@ -1464,8 +1456,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  
 -  disk_inode = calloc (1, sizeof *disk_inode);
 -  if (disk_inode != NULL)
-+  if (length > 0) 
-     {
+-    {
 -      size_t sectors = bytes_to_sectors (length);
 -      disk_inode->length = length;
 -      disk_inode->magic = INODE_MAGIC;
@@ -1483,17 +1474,16 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 -          success = true; 
 -        } 
 -      free (disk_inode);
-+      struct inode *inode = inode_open (sector);
-+      success = inode != NULL && inode_write_at (inode, "", 1, length - 1);
-+      inode_close (inode);
-     }
-+  else
-+    success = true;
-+
-   return success;
+-    }
+-  return success;
++  inode = inode_open (sector);
++  if (inode == NULL)
++    free_map_release (sector);
++  return inode;
  }
  
-@@ -115,6 +123,7 @@ inode_open (disk_sector_t sector) 
+ /* Reads an inode from SECTOR
+@@ -115,29 +110,35 @@ inode_open (disk_sector_t sector) 
    struct inode *inode;
  
    /* Check whether this inode is already open. */
@@ -1501,11 +1491,12 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
    for (e = list_begin (&open_inodes); e != list_end (&open_inodes);
         e = list_next (e)) 
      {
-@@ -122,22 +131,26 @@ inode_open (disk_sector_t sector) 
+       inode = list_entry (e, struct inode, elem);
        if (inode->sector == sector) 
          {
-           inode_reopen (inode);
+-          inode_reopen (inode);
 -          return inode; 
++          inode->open_cnt++;
 +          goto done; 
          }
      }
@@ -1520,6 +1511,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
    list_push_front (&open_inodes, &inode->elem);
    inode->sector = sector;
    inode->open_cnt = 1;
++  lock_init (&inode->lock);
    inode->deny_write_cnt = 0;
 +  lock_init (&inode->deny_write_lock);
 +  cond_init (&inode->no_writers_cond);
@@ -1531,7 +1523,16 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
    return inode;
  }
  
-@@ -150,6 +163,17 @@ inode_reopen (struct inode *inode) 
+@@ -146,9 +147,24 @@ struct inode *
+ inode_reopen (struct inode *inode)
+ {
+   if (inode != NULL)
+-    inode->open_cnt++;
++    {
++      lock_acquire (&open_inodes_lock);
++      inode->open_cnt++;
++      lock_release (&open_inodes_lock);
++    }
    return inode;
  }
  
@@ -1546,10 +1547,9 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 +  return type;
 +}
 +
- /* Closes INODE and writes it to disk.
-    If this was the last reference to INODE, frees its memory.
-    If INODE was also a removed inode, frees its blocks. */
-@@ -161,21 +185,60 @@ inode_close (struct inode *inode) 
+ /* Returns INODE's inode number. */
+ disk_sector_t
+@@ -161,21 +183,60 @@ inode_close (struct inode *inode) 
      return;
  
    /* Release resources if this was the last opener. */
@@ -1615,7 +1615,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  }
  
  /* Marks INODE to be deleted when it is closed by the last caller who
-@@ -187,6 +250,156 @@ inode_remove (struct inode *inode) 
+@@ -187,6 +248,156 @@ inode_remove (struct inode *inode) 
    inode->removed = true;
  }
  
@@ -1772,7 +1772,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  /* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
     Returns the number of bytes actually read, which may be less
     than SIZE if an error occurs or end of file is reached. */
-@@ -195,13 +408,12 @@ inode_read_at (struct inode *inode, void
+@@ -195,13 +406,12 @@ inode_read_at (struct inode *inode, void
  {
    uint8_t *buffer = buffer_;
    off_t bytes_read = 0;
@@ -1788,7 +1788,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  
        /* Bytes left in inode, bytes left in sector, lesser of the two. */
        off_t inode_left = inode_length (inode) - offset;
-@@ -210,26 +422,16 @@ inode_read_at (struct inode *inode, void
+@@ -210,26 +420,16 @@ inode_read_at (struct inode *inode, void
  
        /* Number of bytes to actually copy out of this sector. */
        int chunk_size = size < min_left ? size : min_left;
@@ -1821,7 +1821,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
          }
        
        /* Advance. */
-@@ -237,75 +439,84 @@ inode_read_at (struct inode *inode, void
+@@ -237,75 +437,82 @@ inode_read_at (struct inode *inode, void
        offset += chunk_size;
        bytes_read += chunk_size;
      }
@@ -1849,11 +1849,10 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 +
  /* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
     Returns the number of bytes actually written, which may be
-    less than SIZE if end of file is reached or an error occurs.
+-   less than SIZE if end of file is reached or an error occurs.
 -   (Normally a write at end of file would extend the inode, but
 -   growth is not yet implemented.) */
-+   (Normally a write at end of file would extend the file, but
-+   file growth is not yet implemented.) */
++   less than SIZE if an error occurs. */
  off_t
  inode_write_at (struct inode *inode, const void *buffer_, off_t size,
                  off_t offset) 
@@ -1943,7 +1942,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  
    return bytes_written;
  }
-@@ -315,8 +526,12 @@ inode_write_at (struct inode *inode, con
+@@ -315,8 +522,12 @@ inode_write_at (struct inode *inode, con
  void
  inode_deny_write (struct inode *inode) 
  {
@@ -1957,7 +1956,7 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
  }
  
  /* Re-enables writes to INODE.
-@@ -325,14 +540,33 @@ inode_deny_write (struct inode *inode) 
+@@ -325,14 +536,47 @@ inode_deny_write (struct inode *inode) 
  void
  inode_allow_write (struct inode *inode) 
  {
@@ -1991,42 +1990,59 @@ diff -u src/filesys/inode.c~ src/filesys/inode.c
 +  lock_release (&open_inodes_lock);
 +
 +  return open_cnt;
++}
++
++/* Locks INODE. */
++void
++inode_lock (struct inode *inode) 
++{
++  lock_acquire (&inode->lock);
++}
++
++/* Releases INODE's lock. */
++void
++inode_unlock (struct inode *inode) 
++{
++  lock_release (&inode->lock);
  }
 Index: src/filesys/inode.h
 diff -u src/filesys/inode.h~ src/filesys/inode.h
---- src/filesys/inode.h~ 2005-06-18 20:20:48.000000000 -0700
-+++ src/filesys/inode.h 2006-04-08 19:57:07.000000000 -0700
-@@ -7,10 +7,18 @@
+--- src/filesys/inode.h~
++++ src/filesys/inode.h
+@@ -7,11 +7,19 @@
  
  struct bitmap;
  
 +/* Type of an inode. */
 +enum inode_type 
 +  {
-+    FILE_INODE,       /* Ordinary file. */
-+    DIR_INODE         /* Directory. */
++    FILE_INODE,         /* Ordinary file. */
++    DIR_INODE           /* Directory. */
 +  };
 +
  void inode_init (void);
 -bool inode_create (disk_sector_t, off_t);
-+bool inode_create (disk_sector_t, off_t, enum inode_type);
++struct inode *inode_create (disk_sector_t, enum inode_type);
  struct inode *inode_open (disk_sector_t);
  struct inode *inode_reopen (struct inode *);
 +enum inode_type inode_get_type (const struct inode *);
+ disk_sector_t inode_get_inumber (const struct inode *);
  void inode_close (struct inode *);
  void inode_remove (struct inode *);
  off_t inode_read_at (struct inode *, void *, off_t size, off_t offset);
-@@ -18,5 +26,6 @@ off_t inode_write_at (struct inode *, co
+@@ -18,5 +27,8 @@ off_t inode_write_at (struct inode *, co
  void inode_deny_write (struct inode *);
  void inode_allow_write (struct inode *);
  off_t inode_length (const struct inode *);
 +int inode_open_cnt (const struct inode *);
++void inode_lock (struct inode *);
++void inode_unlock (struct inode *);
  
  #endif /* filesys/inode.h */
 Index: src/threads/init.c
 diff -u src/threads/init.c~ src/threads/init.c
---- src/threads/init.c~ 2006-01-29 13:32:55.000000000 -0800
-+++ src/threads/init.c 2006-04-08 19:57:07.000000000 -0700
+--- src/threads/init.c~
++++ src/threads/init.c
 @@ -33,6 +33,8 @@
  #include "filesys/filesys.h"
  #include "filesys/fsutil.h"
@@ -2048,8 +2064,8 @@ diff -u src/threads/init.c~ src/threads/init.c
    /* Run actions specified on kernel command line. */
 Index: src/threads/interrupt.c
 diff -u src/threads/interrupt.c~ src/threads/interrupt.c
---- src/threads/interrupt.c~ 2006-01-29 13:32:55.000000000 -0800
-+++ src/threads/interrupt.c 2006-04-08 19:57:07.000000000 -0700
+--- src/threads/interrupt.c~
++++ src/threads/interrupt.c
 @@ -354,6 +354,8 @@ intr_handler (struct intr_frame *frame) 
        in_external_intr = true;
        yield_on_return = false;
@@ -2061,10 +2077,10 @@ diff -u src/threads/interrupt.c~ src/threads/interrupt.c
       If there is no handler, invoke the unexpected interrupt
 Index: src/threads/thread.c
 diff -u src/threads/thread.c~ src/threads/thread.c
---- src/threads/thread.c~ 2006-04-08 12:09:39.000000000 -0700
-+++ src/threads/thread.c 2006-04-08 19:57:07.000000000 -0700
+--- src/threads/thread.c~
++++ src/threads/thread.c
 @@ -13,6 +13,7 @@
- #include "threads/synch.h"
+ #include "threads/vaddr.h"
  #ifdef USERPROG
  #include "userprog/process.h"
 +#include "userprog/syscall.h"
@@ -2115,8 +2131,7 @@ diff -u src/threads/thread.c~ src/threads/thread.c
  #ifdef USERPROG
    process_exit ();
  #endif
--
-+  
    /* Just set our status to dying and schedule another process.
       We will be destroyed during the call to schedule_tail(). */
    intr_disable ();
@@ -2158,8 +2173,8 @@ diff -u src/threads/thread.c~ src/threads/thread.c
  
 Index: src/threads/thread.h
 diff -u src/threads/thread.h~ src/threads/thread.h
---- src/threads/thread.h~ 2005-06-18 20:21:21.000000000 -0700
-+++ src/threads/thread.h 2006-04-08 19:57:07.000000000 -0700
+--- src/threads/thread.h~
++++ src/threads/thread.h
 @@ -2,8 +2,10 @@
  #define THREADS_THREAD_H
  
@@ -2226,8 +2241,8 @@ diff -u src/threads/thread.h~ src/threads/thread.h
  
 Index: src/userprog/exception.c
 diff -u src/userprog/exception.c~ src/userprog/exception.c
---- src/userprog/exception.c~ 2006-01-29 13:32:56.000000000 -0800
-+++ src/userprog/exception.c 2006-04-08 19:57:07.000000000 -0700
+--- src/userprog/exception.c~
++++ src/userprog/exception.c
 @@ -4,6 +4,7 @@
  #include "userprog/gdt.h"
  #include "threads/interrupt.h"
@@ -2256,8 +2271,8 @@ diff -u src/userprog/exception.c~ src/userprog/exception.c
            not_present ? "not present" : "rights violation",
 Index: src/userprog/pagedir.c
 diff -u src/userprog/pagedir.c~ src/userprog/pagedir.c
---- src/userprog/pagedir.c~ 2006-01-29 13:32:56.000000000 -0800
-+++ src/userprog/pagedir.c 2006-04-08 19:57:07.000000000 -0700
+--- src/userprog/pagedir.c~
++++ src/userprog/pagedir.c
 @@ -35,15 +35,7 @@ pagedir_destroy (uint32_t *pd) 
    ASSERT (pd != base_page_dir);
    for (pde = pd; pde < pd + pd_no (PHYS_BASE); pde++)
@@ -2277,9 +2292,10 @@ diff -u src/userprog/pagedir.c~ src/userprog/pagedir.c
  
 Index: src/userprog/process.c
 diff -u src/userprog/process.c~ src/userprog/process.c
---- src/userprog/process.c~ 2006-04-08 19:37:00.000000000 -0700
-+++ src/userprog/process.c 2006-04-08 20:00:47.000000000 -0700
-@@ -15,11 +15,26 @@
+--- src/userprog/process.c~
++++ src/userprog/process.c
+@@ -14,12 +14,27 @@
+ #include "threads/flags.h"
  #include "threads/init.h"
  #include "threads/interrupt.h"
 +#include "threads/malloc.h"
@@ -2298,7 +2314,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +   thread. */
 +struct exec_info 
 +  {
-+    const char *filename;               /* Program to load. */
++    const char *file_name;              /* Program to load. */
 +    struct semaphore load_done;         /* "Up"ed when loading complete. */
 +    struct wait_status *wait_status;    /* Child process. */
 +    struct dir *wd;                     /* Working directory. */
@@ -2307,9 +2323,9 @@ diff -u src/userprog/process.c~ src/userprog/process.c
  
  /* Starts a new thread running a user program loaded from
     FILENAME.  The new thread may be scheduled (and may even exit)
-@@ -27,41 +42,82 @@ static bool load (const char *cmdline, v
+@@ -28,41 +43,78 @@ static bool load (const char *cmdline, v
  tid_t
- process_execute (const char *filename) 
+ process_execute (const char *file_name) 
  {
 -  char *fn_copy;
 +  struct dir *wd = thread_current ()->wd;
@@ -2318,27 +2334,23 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +  char *save_ptr;
    tid_t tid;
  
--  /* Make a copy of FILENAME.
+-  /* Make a copy of FILE_NAME.
 -     Otherwise there's a race between the caller and load(). */
 -  fn_copy = palloc_get_page (0);
 -  if (fn_copy == NULL)
 +  /* Initialize exec_info. */
-+  exec.filename = filename;
-+  if (wd) 
-+    {
-+      dir_reopen (wd);
-+      exec.wd = wd; 
-+    }
-+  else if (!dir_open_root (&exec.wd))
++  exec.file_name = file_name;
++  exec.wd = wd != NULL ? dir_reopen (wd) : dir_open_root ();
++  if (exec.wd == NULL)
      return TID_ERROR;
--  strlcpy (fn_copy, filename, PGSIZE);
+-  strlcpy (fn_copy, file_name, PGSIZE);
 +  sema_init (&exec.load_done, 0);
  
-   /* Create a new thread to execute FILENAME. */
--  tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy);
+   /* Create a new thread to execute FILE_NAME. */
+-  tid = thread_create (file_name, PRI_DEFAULT, execute_thread, fn_copy);
 -  if (tid == TID_ERROR)
 -    palloc_free_page (fn_copy); 
-+  strlcpy (thread_name, filename, sizeof thread_name);
++  strlcpy (thread_name, file_name, sizeof thread_name);
 +  strtok_r (thread_name, " ", &save_ptr);
 +  tid = thread_create (thread_name, PRI_DEFAULT, execute_thread, &exec);
 +  if (tid != TID_ERROR)
@@ -2361,10 +2373,10 @@ diff -u src/userprog/process.c~ src/userprog/process.c
  /* A thread function that loads a user process and starts it
     running. */
  static void
--execute_thread (void *filename_)
+-execute_thread (void *file_name_)
 +execute_thread (void *exec_)
  {
--  char *filename = filename_;
+-  char *file_name = file_name_;
 +  struct exec_info *exec = exec_;
    struct intr_frame if_;
    bool success;
@@ -2376,9 +2388,11 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
    if_.cs = SEL_UCSEG;
    if_.eflags = FLAG_IF | FLAG_MBS;
--  success = load (filename, &if_.eip, &if_.esp);
-+  success = load (exec->filename, &if_.eip, &if_.esp);
-+
+-  success = load (file_name, &if_.eip, &if_.esp);
++  success = load (exec->file_name, &if_.eip, &if_.esp);
+-  /* If load failed, quit. */
+-  palloc_free_page (file_name);
 +  /* Allocate wait_status. */
 +  if (success)
 +    {
@@ -2386,9 +2400,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +        = malloc (sizeof *exec->wait_status);
 +      success = exec->wait_status != NULL; 
 +    }
--  /* If load failed, quit. */
--  palloc_free_page (filename);
++
 +  /* Initialize wait_status. */
 +  if (success) 
 +    {
@@ -2404,7 +2416,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    if (!success) 
      thread_exit ();
  
-@@ -76,18 +116,47 @@ execute_thread (void *filename_)
+@@ -76,18 +128,47 @@ execute_thread (void *file_name_)
    NOT_REACHED ();
  }
  
@@ -2457,7 +2469,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    return -1;
  }
  
-@@ -96,8 +165,35 @@ void
+@@ -96,8 +177,35 @@ void
  process_exit (void)
  {
    struct thread *cur = thread_current ();
@@ -2493,7 +2505,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    /* Destroy the current process's page directory and switch back
       to the kernel-only page directory. */
    pd = cur->pagedir;
-@@ -194,7 +290,7 @@ struct Elf32_Phdr
+@@ -194,7 +302,7 @@ struct Elf32_Phdr
  #define PF_W 2          /* Writable. */
  #define PF_R 4          /* Readable. */
  
@@ -2502,15 +2514,15 @@ diff -u src/userprog/process.c~ src/userprog/process.c
  static bool validate_segment (const struct Elf32_Phdr *, struct file *);
  static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
                            uint32_t read_bytes, uint32_t zero_bytes,
-@@ -205,13 +301,15 @@ static bool load_segment (struct file *f
+@@ -205,13 +313,15 @@ static bool load_segment (struct file *f
     and its initial stack pointer into *ESP.
     Returns true if successful, false otherwise. */
  bool
--load (const char *filename, void (**eip) (void), void **esp) 
+-load (const char *file_name, void (**eip) (void), void **esp) 
 +load (const char *cmd_line, void (**eip) (void), void **esp) 
  {
    struct thread *t = thread_current ();
-+  char filename[NAME_MAX + 2];
++  char file_name[NAME_MAX + 2];
    struct Elf32_Ehdr ehdr;
    struct file *file = NULL;
    off_t file_ofs;
@@ -2519,7 +2531,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
    int i;
  
    /* Allocate and activate page directory. */
-@@ -220,13 +318,28 @@ load (const char *filename, void (**eip)
+@@ -220,13 +330,28 @@ load (const char *file_name, void (**eip
      goto done;
    process_activate ();
  
@@ -2529,27 +2541,27 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +    goto done;
 +  hash_init (t->pages, page_hash, page_less, NULL);
 +
-+  /* Extract filename from command line. */
++  /* Extract file_name from command line. */
 +  while (*cmd_line == ' ')
 +    cmd_line++;
-+  strlcpy (filename, cmd_line, sizeof filename);
-+  cp = strchr (filename, ' ');
++  strlcpy (file_name, cmd_line, sizeof file_name);
++  cp = strchr (file_name, ' ');
 +  if (cp != NULL)
 +    *cp = '\0';
 +
    /* Open executable file. */
--  file = filesys_open (filename);
-+  t->bin_file = file = file_open (filesys_open (filename));
+-  file = filesys_open (file_name);
++  t->bin_file = file = file_open (filesys_open (file_name));
    if (file == NULL) 
      {
-       printf ("load: %s: open failed\n", filename);
+       printf ("load: %s: open failed\n", file_name);
        goto done; 
      }
 +  file_deny_write (file);
  
    /* Read and verify executable header. */
    if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
-@@ -271,7 +400,7 @@ load (const char *filename, void (**eip)
+@@ -301,7 +426,7 @@ load (const char *file_name, void (**eip
      }
  
    /* Set up stack. */
@@ -2558,7 +2570,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
      goto done;
  
    /* Start address. */
-@@ -311,14 +424,11 @@ load (const char *filename, void (**eip)
+@@ -311,14 +436,11 @@ load (const char *file_name, void (**eip
  
   done:
    /* We arrive here whether the load is successful or not. */
@@ -2573,7 +2585,7 @@ diff -u src/userprog/process.c~ src/userprog/process.c
  /* Checks whether PHDR describes a valid, loadable segment in
     FILE and returns true if so, false otherwise. */
  static bool
-@@ -387,79 +497,127 @@ load_segment (struct file *file, off_t o
+@@ -386,79 +508,127 @@ load_segment (struct file *file, off_t o
    ASSERT (pg_ofs (upage) == 0);
    ASSERT (ofs % PGSIZE == 0);
  
@@ -2676,12 +2688,12 @@ diff -u src/userprog/process.c~ src/userprog/process.c
 +  cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
 +  if (cmd_line_copy == NULL)
 +    return false;
-+
-+  if (push (kpage, &ofs, &null, sizeof null) == NULL)
-+    return false;
  
 -  kpage = palloc_get_page (PAL_USER | PAL_ZERO);
 -  if (kpage != NULL) 
++  if (push (kpage, &ofs, &null, sizeof null) == NULL)
++    return false;
++
 +  /* Parse command line into arguments
 +     and push them in reverse order. */
 +  argc = 0;
@@ -2755,16 +2767,16 @@ diff -u src/userprog/process.c~ src/userprog/process.c
  }
 Index: src/userprog/syscall.c
 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
---- src/userprog/syscall.c~ 2005-06-18 20:21:21.000000000 -0700
-+++ src/userprog/syscall.c 2006-04-08 20:01:17.000000000 -0700
-@@ -1,20 +1,594 @@
+--- src/userprog/syscall.c~
++++ src/userprog/syscall.c
+@@ -1,20 +1,684 @@
  #include "userprog/syscall.h"
  #include <stdio.h>
 +#include <string.h>
  #include <syscall-nr.h>
 +#include "userprog/process.h"
 +#include "userprog/pagedir.h"
-+#include "devices/kbd.h"
++#include "devices/input.h"
 +#include "filesys/directory.h"
 +#include "filesys/filesys.h"
 +#include "filesys/file.h"
@@ -2773,8 +2785,8 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +#include "threads/malloc.h"
 +#include "threads/palloc.h"
  #include "threads/thread.h"
-+#include "threads/vaddr.h"
 -
++#include "threads/vaddr.h"
 +#include "vm/page.h"
 + 
 + 
@@ -2795,7 +2807,9 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +static int sys_munmap (int mapping);
 +static int sys_chdir (const char *udir);
 +static int sys_mkdir (const char *udir);
-+static int sys_lsdir (void);
++static int sys_readdir (int handle, char *name);
++static int sys_isdir (int handle);
++static int sys_inumber (int handle);
 + 
  static void syscall_handler (struct intr_frame *);
 -
@@ -2840,13 +2854,15 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +      {1, (syscall_function *) sys_munmap},
 +      {1, (syscall_function *) sys_chdir},
 +      {1, (syscall_function *) sys_mkdir},
-+      {0, (syscall_function *) sys_lsdir},
++      {2, (syscall_function *) sys_readdir},
++      {1, (syscall_function *) sys_isdir},
++      {1, (syscall_function *) sys_inumber},
 +    };
++
 +  const struct syscall *sc;
 +  unsigned call_nr;
 +  int args[3];
-+
 +  /* Get the system call. */
 +  copy_in (&call_nr, f->esp, sizeof call_nr);
 +  if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
@@ -2869,7 +2885,8 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
  static void
 -syscall_handler (struct intr_frame *f UNUSED) 
 +copy_in (void *dst_, const void *usrc_, size_t size) 
-+{
+ {
+-  printf ("system call!\n");
 +  uint8_t *dst = dst_;
 +  const uint8_t *usrc = usrc_;
 +
@@ -2890,6 +2907,32 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +    }
 +}
 + 
++/* Copies SIZE bytes from kernel address SRC to user address
++   UDST.
++   Call thread_exit() if any of the user accesses are invalid. */
++static void
++copy_out (void *udst_, const void *src_, size_t size) 
++{
++  uint8_t *udst = udst_;
++  const uint8_t *src = src_;
++
++  while (size > 0) 
++    {
++      size_t chunk_size = PGSIZE - pg_ofs (udst);
++      if (chunk_size > size)
++        chunk_size = size;
++      
++      if (!page_lock (udst, false))
++        thread_exit ();
++      memcpy (udst, src, chunk_size);
++      page_unlock (udst);
++
++      udst += chunk_size;
++      src += chunk_size;
++      size -= chunk_size;
++    }
++}
++ 
 +/* Creates a copy of user string US in kernel memory
 +   and returns it as a page that must be freed with
 +   palloc_free_page().
@@ -2932,8 +2975,8 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  page_unlock (upage);
 + lock_error:
 +  palloc_free_page (ks);
-+  thread_exit ();
-+}
+   thread_exit ();
+ }
 + 
 +/* Halt system call. */
 +static int
@@ -2999,6 +3042,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  {
 +    struct list_elem elem;      /* List element. */
 +    struct file *file;          /* File. */
++    struct dir *dir;            /* Directory. */
 +    int handle;                 /* File handle. */
 +  };
 + 
@@ -3010,18 +3054,28 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  struct file_descriptor *fd;
 +  int handle = -1;
 + 
-+  fd = malloc (sizeof *fd);
++  fd = calloc (1, sizeof *fd);
 +  if (fd != NULL)
 +    {
-+      fd->file = file_open (filesys_open (kfile));
-+      if (fd->file != NULL)
++      struct inode *inode = filesys_open (kfile);
++      if (inode != NULL)
 +        {
-+          struct thread *cur = thread_current ();
-+          handle = fd->handle = cur->next_handle++;
-+          list_push_front (&cur->fds, &fd->elem);
++          if (inode_get_type (inode) == FILE_INODE)
++            fd->file = file_open (inode);
++          else
++            fd->dir = dir_open (inode);
++          if (fd->file != NULL || fd->dir != NULL)
++            {
++              struct thread *cur = thread_current ();
++              handle = fd->handle = cur->next_handle++;
++              list_push_front (&cur->fds, &fd->elem);
++            }
++          else 
++            {
++              free (fd);
++              inode_close (inode);
++            }
 +        }
-+      else 
-+        free (fd);
 +    }
 +  
 +  palloc_free_page (kfile);
@@ -3049,11 +3103,35 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  thread_exit ();
 +}
 + 
++/* Returns the file descriptor associated with the given handle.
++   Terminates the process if HANDLE is not associated with an
++   open ordinary file. */
++static struct file_descriptor *
++lookup_file_fd (int handle) 
++{
++  struct file_descriptor *fd = lookup_fd (handle);
++  if (fd->file == NULL)
++    thread_exit ();
++  return fd;
++}
++ 
++/* Returns the file descriptor associated with the given handle.
++   Terminates the process if HANDLE is not associated with an
++   open directory. */
++static struct file_descriptor *
++lookup_dir_fd (int handle) 
++{
++  struct file_descriptor *fd = lookup_fd (handle);
++  if (fd->dir == NULL)
++    thread_exit ();
++  return fd;
++}
++
 +/* Filesize system call. */
 +static int
 +sys_filesize (int handle) 
 +{
-+  struct file_descriptor *fd = lookup_fd (handle);
++  struct file_descriptor *fd = lookup_file_fd (handle);
 +  int size;
 + 
 +  size = file_length (fd->file);
@@ -3071,7 +3149,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +
 +  /* Look up file descriptor. */
 +  if (handle != STDIN_FILENO)
-+    fd = lookup_fd (handle);
++    fd = lookup_file_fd (handle);
 +
 +  while (size > 0) 
 +    {
@@ -3101,7 +3179,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +          size_t i;
 +          
 +          for (i = 0; i < read_amt; i++) 
-+            udst[i] = kbd_getc ();
++            udst[i] = input_getc ();
 +          bytes_read = read_amt;
 +        }
 +
@@ -3123,15 +3201,14 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +/* Write system call. */
 +static int
 +sys_write (int handle, void *usrc_, unsigned size) 
- {
--  printf ("system call!\n");
++{
 +  uint8_t *usrc = usrc_;
 +  struct file_descriptor *fd = NULL;
 +  int bytes_written = 0;
 +
 +  /* Lookup up file descriptor. */
 +  if (handle != STDOUT_FILENO)
-+    fd = lookup_fd (handle);
++    fd = lookup_file_fd (handle);
 +
 +  while (size > 0) 
 +    {
@@ -3182,7 +3259,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +sys_seek (int handle, unsigned position) 
 +{
 +  if ((off_t) position >= 0)
-+    file_seek (lookup_fd (handle)->file, position);
++    file_seek (lookup_file_fd (handle)->file, position);
 +  return 0;
 +}
 + 
@@ -3190,7 +3267,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +static int
 +sys_tell (int handle) 
 +{
-+  return file_tell (lookup_fd (handle)->file);
++  return file_tell (lookup_file_fd (handle)->file);
 +}
 + 
 +/* Close system call. */
@@ -3199,6 +3276,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +{
 +  struct file_descriptor *fd = lookup_fd (handle);
 +  file_close (fd->file);
++  dir_close (fd->dir);
 +  list_remove (&fd->elem);
 +  free (fd);
 +  return 0;
@@ -3231,8 +3309,8 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +        return m;
 +    }
 + 
-   thread_exit ();
- }
++  thread_exit ();
++}
 +
 +/* Remove mapping M from the virtual address space,
 +   writing back any pages that have changed. */
@@ -3253,7 +3331,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +static int
 +sys_mmap (int handle, void *addr)
 +{
-+  struct file_descriptor *fd = lookup_fd (handle);
++  struct file_descriptor *fd = lookup_file_fd (handle);
 +  struct mapping *m = malloc (sizeof *m);
 +  size_t offset;
 +  off_t length;
@@ -3323,12 +3401,35 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +  return ok;
 +}
 +
-+/* Lsdir system call. */
++/* Readdir system call. */
 +static int
-+sys_lsdir (void) 
++sys_readdir (int handle, char *uname)
 +{
-+  dir_list (thread_current ()->wd);
-+  return 0;
++  struct file_descriptor *fd = lookup_dir_fd (handle);
++  char name[NAME_MAX + 1];
++  bool ok = dir_readdir (fd->dir, name);
++  if (ok)
++    copy_out (uname, name, strlen (name) + 1);
++  return ok;
++}
++
++/* Isdir system call. */
++static int
++sys_isdir (int handle)
++{
++  struct file_descriptor *fd = lookup_fd (handle);
++  return fd->dir != NULL;
++}
++
++/* Inumber system call. */
++static int
++sys_inumber (int handle)
++{
++  struct file_descriptor *fd = lookup_fd (handle);
++  struct inode *inode = (fd->file 
++                         ? file_get_inode (fd->file)
++                         : dir_get_inode (fd->dir));
++  return inode_get_inumber (inode);
 +}
 +\f 
 +/* On thread exit, close all open files and unmap all mappings. */
@@ -3343,6 +3444,7 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +      struct file_descriptor *fd = list_entry (e, struct file_descriptor, elem);
 +      next = list_next (e);
 +      file_close (fd->file);
++      dir_close (fd->dir);
 +      free (fd);
 +    }
 +   
@@ -3358,8 +3460,8 @@ diff -u src/userprog/syscall.c~ src/userprog/syscall.c
 +}
 Index: src/userprog/syscall.h
 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
---- src/userprog/syscall.h~ 2004-09-05 22:38:45.000000000 -0700
-+++ src/userprog/syscall.h 2006-04-08 19:57:08.000000000 -0700
+--- src/userprog/syscall.h~
++++ src/userprog/syscall.h
 @@ -2,5 +2,6 @@
  #define USERPROG_SYSCALL_H
  
@@ -3369,8 +3471,8 @@ diff -u src/userprog/syscall.h~ src/userprog/syscall.h
  #endif /* userprog/syscall.h */
 Index: src/vm/frame.c
 diff -u src/vm/frame.c~ src/vm/frame.c
---- src/vm/frame.c~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/frame.c 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/frame.c~
++++ src/vm/frame.c
 @@ -0,0 +1,161 @@
 +#include "vm/frame.h"
 +#include <stdio.h>
@@ -3535,8 +3637,8 @@ diff -u src/vm/frame.c~ src/vm/frame.c
 +}
 Index: src/vm/frame.h
 diff -u src/vm/frame.h~ src/vm/frame.h
---- src/vm/frame.h~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/frame.h 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/frame.h~
++++ src/vm/frame.h
 @@ -0,0 +1,23 @@
 +#ifndef VM_FRAME_H
 +#define VM_FRAME_H
@@ -3563,8 +3665,8 @@ diff -u src/vm/frame.h~ src/vm/frame.h
 +#endif /* vm/frame.h */
 Index: src/vm/page.c
 diff -u src/vm/page.c~ src/vm/page.c
---- src/vm/page.c~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/page.c 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/page.c~
++++ src/vm/page.c
 @@ -0,0 +1,294 @@
 +#include "vm/page.h"
 +#include <stdio.h>
@@ -3862,8 +3964,8 @@ diff -u src/vm/page.c~ src/vm/page.c
 +}
 Index: src/vm/page.h
 diff -u src/vm/page.h~ src/vm/page.h
---- src/vm/page.h~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/page.h 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/page.h~
++++ src/vm/page.h
 @@ -0,0 +1,50 @@
 +#ifndef VM_PAGE_H
 +#define VM_PAGE_H
@@ -3917,8 +4019,8 @@ diff -u src/vm/page.h~ src/vm/page.h
 +#endif /* vm/page.h */
 Index: src/vm/swap.c
 diff -u src/vm/swap.c~ src/vm/swap.c
---- src/vm/swap.c~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/swap.c 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/swap.c~
++++ src/vm/swap.c
 @@ -0,0 +1,85 @@
 +#include "vm/swap.h"
 +#include <bitmap.h>
@@ -4007,8 +4109,8 @@ diff -u src/vm/swap.c~ src/vm/swap.c
 +}
 Index: src/vm/swap.h
 diff -u src/vm/swap.h~ src/vm/swap.h
---- src/vm/swap.h~ 1969-12-31 16:00:00.000000000 -0800
-+++ src/vm/swap.h 2006-04-08 19:57:08.000000000 -0700
+--- src/vm/swap.h~
++++ src/vm/swap.h
 @@ -0,0 +1,11 @@
 +#ifndef VM_SWAP_H
 +#define VM_SWAP_H 1