7996c254601a9899294721238d475767d7daaf0d
[pintos-anon] / solutions / p4.patch
1 Index: src/Makefile.build
2 diff -u src/Makefile.build~ src/Makefile.build
3 --- src/Makefile.build~
4 +++ src/Makefile.build
5 @@ -53,7 +53,9 @@ userprog_SRC += userprog/gdt.c                # GDT in
6  userprog_SRC += userprog/tss.c         # TSS management.
7  
8  # No virtual memory code yet.
9 -#vm_SRC = vm/file.c                    # Some file.
10 +vm_SRC = vm/page.c
11 +vm_SRC += vm/frame.c
12 +vm_SRC += vm/swap.c
13  
14  # Filesystem code.
15  filesys_SRC  = filesys/filesys.c       # Filesystem core.
16 @@ -62,6 +64,7 @@ filesys_SRC += filesys/file.c         # Files.
17  filesys_SRC += filesys/directory.c     # Directories.
18  filesys_SRC += filesys/inode.c         # File headers.
19  filesys_SRC += filesys/fsutil.c                # Utilities.
20 +filesys_SRC += filesys/cache.c         # Cache.
21  
22  SOURCES = $(foreach dir,$(KERNEL_SUBDIRS),$($(dir)_SRC))
23  OBJECTS = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(SOURCES)))
24 Index: src/devices/timer.c
25 diff -u src/devices/timer.c~ src/devices/timer.c
26 --- src/devices/timer.c~
27 +++ src/devices/timer.c
28 @@ -23,6 +23,9 @@ static volatile int64_t ticks;
29     Initialized by timer_calibrate(). */
30  static unsigned loops_per_tick;
31  
32 +/* Threads waiting in timer_sleep(). */
33 +static struct list wait_list;
34 +
35  static intr_handler_func timer_interrupt;
36  static bool too_many_loops (unsigned loops);
37  static void busy_wait (int64_t loops);
38 @@ -43,6 +46,8 @@ timer_init (void) 
39    outb (0x40, count >> 8);
40  
41    intr_register_ext (0x20, timer_interrupt, "8254 Timer");
42 +
43 +  list_init (&wait_list);
44  }
45  
46  /* Calibrates loops_per_tick, used to implement brief delays. */
47 @@ -93,16 +93,37 @@
48    return timer_ticks () - then;
49  }
50  
51 +/* Compares two threads based on their wake-up times. */
52 +static bool
53 +compare_threads_by_wakeup_time (const struct list_elem *a_,
54 +                                const struct list_elem *b_,
55 +                                void *aux UNUSED) 
56 +{
57 +  const struct thread *a = list_entry (a_, struct thread, timer_elem);
58 +  const struct thread *b = list_entry (b_, struct thread, timer_elem);
59 +
60 +  return a->wakeup_time < b->wakeup_time;
61 +}
62 +
63  /* Sleeps for approximately TICKS timer ticks.  Interrupts must
64     be turned on. */
65  void
66  timer_sleep (int64_t ticks) 
67  {
68 -  int64_t start = timer_ticks ();
69 +  struct thread *t = thread_current ();
70 +
71 +  /* Schedule our wake-up time. */
72 +  t->wakeup_time = timer_ticks () + ticks;
73  
74 +  /* Atomically insert the current thread into the wait list. */
75    ASSERT (intr_get_level () == INTR_ON);
76 -  while (timer_elapsed (start) < ticks) 
77 -    thread_yield ();
78 +  intr_disable ();
79 +  list_insert_ordered (&wait_list, &t->timer_elem,
80 +                       compare_threads_by_wakeup_time, NULL);
81 +  intr_enable ();
82 +
83 +  /* Wait. */
84 +  sema_down (&t->timer_sema);
85  }
86  
87  /* Sleeps for approximately MS milliseconds.  Interrupts must be
88 @@ -132,6 +158,16 @@ timer_interrupt (struct intr_frame *args
89  {
90    ticks++;
91    thread_tick ();
92 +
93 +  while (!list_empty (&wait_list))
94 +    {
95 +      struct thread *t = list_entry (list_front (&wait_list),
96 +                                     struct thread, timer_elem);
97 +      if (ticks < t->wakeup_time) 
98 +        break;
99 +      sema_up (&t->timer_sema);
100 +      list_pop_front (&wait_list);
101 +    }
102  }
103  
104  /* Returns true if LOOPS iterations waits for more than one timer
105 Index: src/filesys/Make.vars
106 diff -u src/filesys/Make.vars~ src/filesys/Make.vars
107 --- src/filesys/Make.vars~
108 +++ src/filesys/Make.vars
109 @@ -6,7 +6,7 @@ TEST_SUBDIRS = tests/userprog tests/file
110  GRADING_FILE = $(SRCDIR)/tests/filesys/Grading.no-vm
111  
112  # Uncomment the lines below to enable VM.
113 -#os.dsk: DEFINES += -DVM
114 -#KERNEL_SUBDIRS += vm
115 -#TEST_SUBDIRS += tests/vm
116 -#GRADING_FILE = $(SRCDIR)/tests/filesys/Grading.with-vm
117 +os.dsk: DEFINES += -DVM
118 +KERNEL_SUBDIRS += vm
119 +TEST_SUBDIRS += tests/vm
120 +GRADING_FILE = $(SRCDIR)/tests/filesys/Grading.with-vm
121 Index: src/filesys/cache.c
122 diff -u src/filesys/cache.c~ src/filesys/cache.c
123 --- src/filesys/cache.c~
124 +++ src/filesys/cache.c
125 @@ -0,0 +1,473 @@
126 +#include "filesys/cache.h"
127 +#include <debug.h>
128 +#include <string.h>
129 +#include "filesys/filesys.h"
130 +#include "devices/disk.h"
131 +#include "devices/timer.h"
132 +#include "threads/malloc.h"
133 +#include "threads/synch.h"
134 +#include "threads/thread.h"
135 +
136 +#define INVALID_SECTOR ((disk_sector_t) -1)
137 +
138 +/* A cached block. */
139 +struct cache_block 
140 +  {
141 +    /* Locking to prevent eviction. */
142 +    struct lock block_lock;                    /* Protects fields in group. */
143 +    struct condition no_readers_or_writers; /* readers == 0 && writers == 0 */
144 +    struct condition no_writers;                            /* writers == 0 */
145 +    int readers, read_waiters;          /* # of readers, # waiting to read. */
146 +    int writers, write_waiters; /* # of writers (<= 1), # waiting to write. */
147 +
148 +    /* Sector number.  INVALID_SECTOR indicates a free cache block.
149 +
150 +       Changing from free to allocated requires cache_sync.
151 +
152 +       Changing from allocated to free requires block_lock, block
153 +       must be up-to-date and not dirty, and no one may be
154 +       waiting on it. */
155 +    disk_sector_t sector;
156 +
157 +    /* Is data[] correct?
158 +       Requires write lock or data_lock. */
159 +    bool up_to_date;
160 +
161 +    /* Does data[] need to be written back to disk?
162 +       Valid only when up-to-date.
163 +       Requires read lock or write lock or data_lock. */
164 +    bool dirty;
165 +
166 +    /* Sector data.
167 +       Access to data[] requires up-to-date and read or write lock.
168 +       Bringing up-to-date requires write lock or data_lock. */
169 +    struct lock data_lock;              /* Protects fields in group. */
170 +    uint8_t data[DISK_SECTOR_SIZE];     /* Disk data. */
171 +  };
172 +
173 +/* Cache. */
174 +#define CACHE_CNT 64
175 +struct cache_block cache[CACHE_CNT];
176 +
177 +/* Cache lock.
178 +
179 +   Required to allocate a cache block to a sector, to prevent a
180 +   single sector being allocated two different cache blocks.
181 +
182 +   Required to search the cache for a sector, to prevent the
183 +   sector from being added while the search is ongoing.
184 +
185 +   Protects hand. */
186 +struct lock cache_sync;
187 +
188 +/* Cache eviction hand.
189 +   Protected by cache_sync. */
190 +static int hand = 0;
191 +
192 +static void flushd_init (void);
193 +static void readaheadd_init (void);
194 +static void readaheadd_submit (disk_sector_t sector);
195 +\f
196 +/* Initializes cache. */
197 +void
198 +cache_init (void) 
199 +{
200 +  int i;
201 +  
202 +  lock_init (&cache_sync);
203 +  for (i = 0; i < CACHE_CNT; i++) 
204 +    {
205 +      struct cache_block *b = &cache[i];
206 +      lock_init (&b->block_lock);
207 +      cond_init (&b->no_readers_or_writers);
208 +      cond_init (&b->no_writers);
209 +      b->readers = b->read_waiters = 0;
210 +      b->writers = b->write_waiters = 0;
211 +      b->sector = INVALID_SECTOR;
212 +      lock_init (&b->data_lock);
213 +    }
214 +
215 +  flushd_init ();
216 +  readaheadd_init ();
217 +}
218 +
219 +/* Flushes cache to disk. */
220 +void
221 +cache_flush (void) 
222 +{
223 +  int i;
224 +  
225 +  for (i = 0; i < CACHE_CNT; i++)
226 +    {
227 +      struct cache_block *b = &cache[i];
228 +      disk_sector_t sector;
229 +      
230 +      lock_acquire (&b->block_lock);
231 +      sector = b->sector;
232 +      lock_release (&b->block_lock);
233 +
234 +      if (sector == INVALID_SECTOR)
235 +        continue;
236 +
237 +      b = cache_lock (sector, EXCLUSIVE);
238 +      if (b->up_to_date && b->dirty) 
239 +        {
240 +          disk_write (filesys_disk, b->sector, b->data);
241 +          b->dirty = false; 
242 +        }
243 +      cache_unlock (b);
244 +    }
245 +}
246 +
247 +/* Locks the given SECTOR into the cache and returns the cache
248 +   block.
249 +   If TYPE is EXCLUSIVE, then the block returned will be locked
250 +   only by the caller.  The calling thread must not already
251 +   have any lock on the block.
252 +   If TYPE is NON_EXCLUSIVE, then block returned may be locked by
253 +   any number of other callers.  The calling thread may already
254 +   have any number of non-exclusive locks on the block. */
255 +struct cache_block *
256 +cache_lock (disk_sector_t sector, enum lock_type type) 
257 +{
258 +  int i;
259 +
260 + try_again:
261 +  lock_acquire (&cache_sync);
262 +
263 +  /* Is the block already in-cache? */
264 +  for (i = 0; i < CACHE_CNT; i++)
265 +    {
266 +      /* Skip any blocks that don't hold SECTOR. */
267 +      struct cache_block *b = &cache[i];
268 +      lock_acquire (&b->block_lock);
269 +      if (b->sector != sector) 
270 +        {
271 +          lock_release (&b->block_lock);
272 +          continue;
273 +        }
274 +      lock_release (&cache_sync);
275 +
276 +      /* Get read or write lock. */
277 +      if (type == NON_EXCLUSIVE) 
278 +        {
279 +          /* Lock for read. */
280 +          b->read_waiters++;
281 +          if (b->writers || b->write_waiters)
282 +            do {
283 +              cond_wait (&b->no_writers, &b->block_lock);
284 +            } while (b->writers);
285 +          b->readers++;
286 +          b->read_waiters--;
287 +        }
288 +      else 
289 +        {
290 +          /* Lock for write. */
291 +          b->write_waiters++;
292 +          if (b->readers || b->read_waiters || b->writers)
293 +            do {
294 +              cond_wait (&b->no_readers_or_writers, &b->block_lock);
295 +            } while (b->readers || b->writers);
296 +          b->writers++;
297 +          b->write_waiters--;
298 +        }
299 +      lock_release (&b->block_lock);
300 +
301 +      /* Our sector should have been pinned in the cache while we
302 +         were waiting.  Make sure. */
303 +      ASSERT (b->sector == sector);
304 +
305 +      return b;
306 +    }
307 +
308 +  /* Not in cache.  Find empty slot.
309 +     We hold cache_sync. */
310 +  for (i = 0; i < CACHE_CNT; i++)
311 +    {
312 +      struct cache_block *b = &cache[i];
313 +      lock_acquire (&b->block_lock);
314 +      if (b->sector == INVALID_SECTOR) 
315 +        {
316 +          /* Drop block_lock, which is no longer needed because
317 +             this is the only code that allocates free blocks,
318 +             and we still have cache_sync.
319 +
320 +             We can't drop cache_sync yet because someone else
321 +             might try to allocate this same block (or read from
322 +             it) while we're still initializing the block. */
323 +          lock_release (&b->block_lock);
324 +
325 +          b->sector = sector;
326 +          b->up_to_date = false;
327 +          ASSERT (b->readers == 0);
328 +          ASSERT (b->writers == 0);
329 +          if (type == NON_EXCLUSIVE)
330 +            b->readers = 1;
331 +          else
332 +            b->writers = 1;
333 +          lock_release (&cache_sync);
334 +          return b;
335 +        }
336 +      lock_release (&b->block_lock); 
337 +    }
338 +
339 +  /* No empty slots.  Evict something.
340 +     We hold cache_sync. */
341 +  for (i = 0; i < CACHE_CNT; i++)
342 +    {
343 +      struct cache_block *b = &cache[hand];
344 +      if (++hand >= CACHE_CNT)
345 +        hand = 0;
346 +
347 +      /* Try to grab exclusive write access to block. */
348 +      lock_acquire (&b->block_lock);
349 +      if (b->readers || b->writers || b->read_waiters || b->write_waiters) 
350 +        {
351 +          lock_release (&b->block_lock);
352 +          continue;
353 +        }
354 +      b->writers = 1;
355 +      lock_release (&b->block_lock);
356 +
357 +      lock_release (&cache_sync);
358 +
359 +      /* Write block to disk if dirty. */
360 +      if (b->up_to_date && b->dirty) 
361 +        {
362 +          disk_write (filesys_disk, b->sector, b->data);
363 +          b->dirty = false;
364 +        }
365 +
366 +      /* Remove block from cache, if possible: someone might have
367 +         started waiting on it while the lock was released. */
368 +      lock_acquire (&b->block_lock);
369 +      b->writers = 0;
370 +      if (!b->read_waiters && !b->write_waiters) 
371 +        {
372 +          /* No one is waiting for it, so we can free it. */
373 +          b->sector = INVALID_SECTOR; 
374 +        }
375 +      else 
376 +        {
377 +          /* There is a waiter.  Give it the block. */
378 +          if (b->read_waiters)
379 +            cond_broadcast (&b->no_writers, &b->block_lock);
380 +          else
381 +            cond_signal (&b->no_readers_or_writers, &b->block_lock);
382 +        }
383 +      lock_release (&b->block_lock);
384 +
385 +      /* Try again. */
386 +      goto try_again;
387 +    }
388 +
389 +  /* Wait for cache contention to die down. */
390 +  lock_release (&cache_sync);
391 +  timer_msleep (1000);
392 +  goto try_again;
393 +}
394 +
395 +/* Bring block B up-to-date, by reading it from disk if
396 +   necessary, and return a pointer to its data.
397 +   The caller must have an exclusive or non-exclusive lock on
398 +   B. */
399 +void *
400 +cache_read (struct cache_block *b) 
401 +{
402 +  lock_acquire (&b->data_lock);
403 +  if (!b->up_to_date) 
404 +    {
405 +      disk_read (filesys_disk, b->sector, b->data);
406 +      b->up_to_date = true;
407 +      b->dirty = false; 
408 +    }
409 +  lock_release (&b->data_lock);
410 +
411 +  return b->data;
412 +}
413 +
414 +/* Zero out block B, without reading it from disk, and return a
415 +   pointer to the zeroed data.
416 +   The caller must have an exclusive lock on B. */
417 +void *
418 +cache_zero (struct cache_block *b) 
419 +{
420 +  ASSERT (b->writers);
421 +  memset (b->data, 0, DISK_SECTOR_SIZE);
422 +  b->up_to_date = true;
423 +  b->dirty = true;
424 +
425 +  return b->data;
426 +}
427 +
428 +/* Marks block B as dirty, so that it will be written back to
429 +   disk before eviction.
430 +   The caller must have a read or write lock on B,
431 +   and B must be up-to-date. */
432 +void
433 +cache_dirty (struct cache_block *b) 
434 +{
435 +  ASSERT (b->up_to_date);
436 +  b->dirty = true;
437 +}
438 +
439 +/* Unlocks block B.
440 +   If B is no longer locked by any thread, then it becomes a
441 +   candidate for immediate eviction. */
442 +void
443 +cache_unlock (struct cache_block *b) 
444 +{
445 +  lock_acquire (&b->block_lock);
446 +  if (b->readers) 
447 +    {
448 +      ASSERT (b->writers == 0);
449 +      if (--b->readers == 0)
450 +        cond_signal (&b->no_readers_or_writers, &b->block_lock);
451 +    }
452 +  else if (b->writers)
453 +    {
454 +      ASSERT (b->readers == 0);
455 +      ASSERT (b->writers == 1);
456 +      b->writers--;
457 +      if (b->read_waiters)
458 +        cond_broadcast (&b->no_writers, &b->block_lock);
459 +      else
460 +        cond_signal (&b->no_readers_or_writers, &b->block_lock);
461 +    }
462 +  else
463 +    NOT_REACHED ();
464 +  lock_release (&b->block_lock);
465 +}
466 +
467 +/* If SECTOR is in the cache, evicts it immediately without
468 +   writing it back to disk (even if dirty).
469 +   The block must be entirely unused. */
470 +void
471 +cache_free (disk_sector_t sector) 
472 +{
473 +  int i;
474 +  
475 +  lock_acquire (&cache_sync);
476 +  for (i = 0; i < CACHE_CNT; i++)
477 +    {
478 +      struct cache_block *b = &cache[i];
479 +
480 +      lock_acquire (&b->block_lock);
481 +      if (b->sector == sector) 
482 +        {
483 +          lock_release (&cache_sync);
484 +
485 +          /* Only invalidate the block if it's unused.  That
486 +             should be the normal case, but it could be part of a
487 +             read-ahead (in readaheadd()) or write-behind (in
488 +             cache_flush()). */
489 +          if (b->readers == 0 && b->read_waiters == 0
490 +              && b->writers == 0 && b->write_waiters == 0) 
491 +            b->sector = INVALID_SECTOR; 
492 +
493 +          lock_release (&b->block_lock);
494 +          return;
495 +        }
496 +      lock_release (&b->block_lock);
497 +    }
498 +  lock_release (&cache_sync);
499 +}
500 +
501 +void
502 +cache_readahead (disk_sector_t sector) 
503 +{
504 +  readaheadd_submit (sector);
505 +}
506 +\f
507 +/* Flush daemon. */
508 +
509 +static void flushd (void *aux);
510 +
511 +/* Initializes flush daemon. */
512 +static void
513 +flushd_init (void) 
514 +{
515 +  thread_create ("flushd", PRI_MIN, flushd, NULL);
516 +}
517 +
518 +/* Flush daemon thread. */
519 +static void
520 +flushd (void *aux UNUSED) 
521 +{
522 +  for (;;) 
523 +    {
524 +      timer_msleep (30 * 1000);
525 +      cache_flush ();
526 +    }
527 +}
528 +\f
529 +/* A block to read ahead. */
530 +struct readahead_block 
531 +  {
532 +    struct list_elem list_elem;         /* readahead_list element. */
533 +    disk_sector_t sector;               /* Sector to read. */
534 +  };
535 +
536 +/* Protects readahead_list.
537 +   Monitor lock for readahead_list_nonempty. */
538 +static struct lock readahead_lock;
539 +
540 +/* Signaled when a block is added to readahead_list. */
541 +static struct condition readahead_list_nonempty;
542 +
543 +/* List of blocks for read-ahead. */
544 +static struct list readahead_list;
545 +
546 +static void readaheadd (void *aux);
547 +
548 +/* Initialize read-ahead daemon. */
549 +static void
550 +readaheadd_init (void) 
551 +{
552 +  lock_init (&readahead_lock);
553 +  cond_init (&readahead_list_nonempty);
554 +  list_init (&readahead_list);
555 +  thread_create ("readaheadd", PRI_MIN, readaheadd, NULL);
556 +}
557 +
558 +/* Adds SECTOR to the read-ahead queue. */
559 +static void
560 +readaheadd_submit (disk_sector_t sector) 
561 +{
562 +  /* Allocate readahead block. */
563 +  struct readahead_block *block = malloc (sizeof *block);
564 +  if (block == NULL)
565 +    return;
566 +  block->sector = sector;
567 +
568 +  /* Add block to list. */
569 +  lock_acquire (&readahead_lock);
570 +  list_push_back (&readahead_list, &block->list_elem);
571 +  cond_signal (&readahead_list_nonempty, &readahead_lock);
572 +  lock_release (&readahead_lock);
573 +}
574 +
575 +/* Read-ahead daemon. */
576 +static void
577 +readaheadd (void *aux UNUSED) 
578 +{
579 +  for (;;) 
580 +    {
581 +      struct readahead_block *ra_block;
582 +      struct cache_block *cache_block;
583 +
584 +      /* Get readahead block from list. */
585 +      lock_acquire (&readahead_lock);
586 +      while (list_empty (&readahead_list)) 
587 +        cond_wait (&readahead_list_nonempty, &readahead_lock);
588 +      ra_block = list_entry (list_pop_front (&readahead_list),
589 +                             struct readahead_block, list_elem);
590 +      lock_release (&readahead_lock);
591 +
592 +      /* Read block into cache. */
593 +      cache_block = cache_lock (ra_block->sector, NON_EXCLUSIVE);
594 +      cache_read (cache_block);
595 +      cache_unlock (cache_block);
596 +      free (ra_block);
597 +    }
598 +}
599 Index: src/filesys/cache.h
600 diff -u src/filesys/cache.h~ src/filesys/cache.h
601 --- src/filesys/cache.h~
602 +++ src/filesys/cache.h
603 @@ -0,0 +1,23 @@
604 +#ifndef FILESYS_CACHE_H
605 +#define FILESYS_CACHE_H
606 +
607 +#include "devices/disk.h"
608 +
609 +/* Type of block lock. */
610 +enum lock_type 
611 +  {
612 +    NON_EXCLUSIVE,     /* Any number of lockers. */
613 +    EXCLUSIVE          /* Only one locker. */
614 +  };
615 +
616 +void cache_init (void);
617 +void cache_flush (void);
618 +struct cache_block *cache_lock (disk_sector_t, enum lock_type);
619 +void *cache_read (struct cache_block *);
620 +void *cache_zero (struct cache_block *);
621 +void cache_dirty (struct cache_block *);
622 +void cache_unlock (struct cache_block *);
623 +void cache_free (disk_sector_t);
624 +void cache_readahead (disk_sector_t);
625 +
626 +#endif /* filesys/cache.h */
627 Index: src/filesys/directory.c
628 diff -u src/filesys/directory.c~ src/filesys/directory.c
629 --- src/filesys/directory.c~
630 +++ src/filesys/directory.c
631 @@ -1,4 +1,5 @@
632  #include <string.h>
633  #include <list.h>
634 +#include "filesys/free-map.h"
635  #include "filesys/filesys.h"
636  #include "filesys/inode.h"
637 @@ -21,12 +21,39 @@ struct dir_entry 
638      bool in_use;                        /* In use or free? */
639    };
640  
641 -/* Creates a directory with space for ENTRY_CNT entries in the
642 -   given SECTOR.  Returns true if successful, false on failure. */
643 +/* Creates a directory in the given SECTOR.
644 +   The directory's parent is in PARENT_SECTOR.
645 +   Returns inode of created directory if successful,
646 +   null pointer on faiilure.
647 +   On failure, SECTOR is released in the free map. */
648 -bool
649 +struct inode *
650 -dir_create (disk_sector_t sector, size_t entry_cnt) 
651 +dir_create (disk_sector_t sector, disk_sector_t parent_sector) 
652  {
653 -  return inode_create (sector, entry_cnt * sizeof (struct dir_entry));
654 +  struct inode *inode = inode_create (sector, DIR_INODE);
655 +  if (inode != NULL) 
656 +    {
657 +      struct dir_entry entries[2];
658 +
659 +      memset (entries, 0, sizeof entries);
660 +
661 +      /* "." entry. */
662 +      entries[0].inode_sector = sector;
663 +      strlcpy (entries[0].name, ".", sizeof entries[0].name);
664 +      entries[0].in_use = true;
665 +
666 +      /* ".." entry. */
667 +      entries[1].inode_sector = parent_sector;
668 +      strlcpy (entries[1].name, "..", sizeof entries[1].name);
669 +      entries[1].in_use = true;
670 +      
671 +      if (inode_write_at (inode, entries, sizeof entries, 0) != sizeof entries)
672 +        {
673 +          inode_remove (inode);
674 +          inode_close (inode); 
675 +          inode = NULL;
676 +        } 
677 +    }
678 +  return inode;
679  }
680  
681  /* Opens and returns the directory for the given INODE, of which
682 @@ -35,7 +59,7 @@ struct dir *
683  dir_open (struct inode *inode) 
684  {
685    struct dir *dir = calloc (1, sizeof *dir);
686 -  if (inode != NULL && dir != NULL)
687 +  if (inode != NULL && dir != NULL && inode_get_type (inode) == DIR_INODE)
688      {
689        dir->inode = inode;
690        dir->pos = 0;
691 @@ -84,10 +108,8 @@ dir_get_inode (struct dir *dir) 
692  }
693  
694  /* Searches DIR for a file with the given NAME.
695 -   If successful, returns true, sets *EP to the directory entry
696 -   if EP is non-null, and sets *OFSP to the byte offset of the
697 -   directory entry if OFSP is non-null.
698 -   otherwise, returns false and ignores EP and OFSP. */
699 +   If successful, returns the file's entry;
700 +   otherwise, returns a null pointer. */
701  static bool
702  lookup (const struct dir *dir, const char *name,
703          struct dir_entry *ep, off_t *ofsp) 
704 @@ -120,15 +142,16 @@ dir_lookup (const struct dir *dir, const
705              struct inode **inode) 
706  {
707    struct dir_entry e;
708 +  bool ok;
709  
710    ASSERT (dir != NULL);
711    ASSERT (name != NULL);
712  
713 -  if (lookup (dir, name, &e, NULL))
714 -    *inode = inode_open (e.inode_sector);
715 -  else
716 -    *inode = NULL;
717 +  inode_lock (dir->inode);
718 +  ok = lookup (dir, name, &e, NULL);
719 +  inode_unlock (dir->inode);
720  
721 +  *inode = ok ? inode_open (e.inode_sector) : NULL;
722    return *inode != NULL;
723  }
724  
725 @@ -149,10 +172,11 @@ dir_add (struct dir *dir, const char *na
726    ASSERT (name != NULL);
727  
728    /* Check NAME for validity. */
729 -  if (*name == '\0' || strlen (name) > NAME_MAX)
730 +  if (*name == '\0' || strchr (name, '/') || strlen (name) > NAME_MAX)
731      return false;
732  
733    /* Check that NAME is not in use. */
734 +  inode_lock (dir->inode);
735    if (lookup (dir, name, NULL, NULL))
736      goto done;
737  
738 @@ -175,6 +199,7 @@ dir_add (struct dir *dir, const char *na
739    success = inode_write_at (dir->inode, &e, sizeof e, ofs) == sizeof e;
740  
741   done:
742 +  inode_unlock (dir->inode);
743    return success;
744  }
745  
746 @@ -192,13 +217,37 @@ dir_remove (struct dir *dir, const char 
747    ASSERT (dir != NULL);
748    ASSERT (name != NULL);
749  
750 +  if (!strcmp (name, ".") || !strcmp (name, ".."))
751 +    return false;
752 +
753    /* Find directory entry. */
754 +  inode_lock (dir->inode);
755    if (!lookup (dir, name, &e, &ofs))
756      goto done;
757  
758    /* Open inode. */
759    inode = inode_open (e.inode_sector);
760    if (inode == NULL)
761      goto done;
762  
763 +  /* Verify that it is not an in-use or non-empty directory. */
764 +  if (inode_get_type (inode) == DIR_INODE)
765 +    {
766 +      struct dir_entry e2;
767 +      off_t pos;
768 +
769 +      if (inode_open_cnt (inode) != 1)
770 +        goto done;
771 +
772 +      inode_lock (inode);
773 +      for (pos = 0; inode_read_at (inode, &e2, sizeof e2, pos) == sizeof e2;
774 +           pos += sizeof e2)
775 +        if (e2.in_use && strcmp (e2.name, ".") && strcmp (e2.name, ".."))
776 +          {
777 +            inode_unlock (inode);
778 +            goto done;
779 +          }
780 +      inode_unlock (inode);
781 +    }
782
783    /* Erase directory entry. */
784 @@ -211,6 +241,7 @@ dir_remove (struct dir *dir, const char 
785    success = true;
786  
787   done:
788 +  inode_unlock (dir->inode);
789    inode_close (inode);
790    return success;
791  }
792 @@ -223,14 +254,17 @@ dir_readdir (struct dir *dir, char name[
793  {
794    struct dir_entry e;
795  
796 +  inode_lock (dir->inode);
797    while (inode_read_at (dir->inode, &e, sizeof e, dir->pos) == sizeof e) 
798      {
799        dir->pos += sizeof e;
800 -      if (e.in_use)
801 +      if (e.in_use && strcmp (e.name, ".") && strcmp (e.name, ".."))
802          {
803 +          inode_unlock (dir->inode);
804            strlcpy (name, e.name, NAME_MAX + 1);
805            return true;
806          } 
807      }
808 +  inode_unlock (dir->inode);
809    return false;
810  }
811 Index: src/filesys/directory.h
812 diff -u src/filesys/directory.h~ src/filesys/directory.h
813 --- src/filesys/directory.h~
814 +++ src/filesys/directory.h
815 @@ -14,7 +14,7 @@
816  struct inode;
817  
818  /* Opening and closing directories. */
819 -bool dir_create (disk_sector_t sector, size_t entry_cnt);
820 +struct inode *dir_create (disk_sector_t sector, disk_sector_t parent_sector);
821  struct dir *dir_open (struct inode *);
822  struct dir *dir_open_root (void);
823  struct dir *dir_reopen (struct dir *);
824 Index: src/filesys/file.c
825 diff -u src/filesys/file.c~ src/filesys/file.c
826 --- src/filesys/file.c~
827 +++ src/filesys/file.c
828 @@ -1,4 +1,5 @@
829  #include "filesys/file.h"
830  #include <debug.h>
831 +#include "filesys/free-map.h"
832  #include "filesys/inode.h"
833  #include "threads/malloc.h"
834 @@ -11,6 +11,24 @@ struct file 
835      bool deny_write;            /* Has file_deny_write() been called? */
836    };
837  
838 +/* Creates a file in the given SECTOR,
839 +   initially LENGTH bytes long. 
840 +   Returns inode for the file on success, null pointer on failure.
841 +   On failure, SECTOR is released in the free map. */
842 +struct inode *
843 +file_create (disk_sector_t sector, off_t length) 
844 +{
845 +  struct inode *inode = inode_create (sector, FILE_INODE);
846 +  if (inode != NULL && length > 0
847 +      && inode_write_at (inode, "", 1, length - 1) != 1)
848 +    {
849 +      inode_remove (inode); 
850 +      inode_close (inode);
851 +      inode = NULL;
852 +    }
853 +  return inode;
854 +}
855 +
856  /* Opens a file for the given INODE, of which it takes ownership,
857     and returns the new file.  Returns a null pointer if an
858     allocation fails or if INODE is null. */
859 @@ -18,7 +34,7 @@ struct file *
860  file_open (struct inode *inode) 
861  {
862    struct file *file = calloc (1, sizeof *file);
863 -  if (inode != NULL && file != NULL)
864 +  if (inode != NULL && file != NULL && inode_get_type (inode) == FILE_INODE)
865      {
866        file->inode = inode;
867        file->pos = 0;
868 Index: src/filesys/file.h
869 diff -u src/filesys/file.h~ src/filesys/file.h
870 --- src/filesys/file.h~
871 +++ src/filesys/file.h
872 @@ -1,11 +1,14 @@
873  #ifndef FILESYS_FILE_H
874  #define FILESYS_FILE_H
875  
876 +#include <stdbool.h>
877 +#include "devices/disk.h"
878  #include "filesys/off_t.h"
879  
880  struct inode;
881  
882  /* Opening and closing files. */
883 +struct inode *file_create (disk_sector_t sector, off_t length);
884  struct file *file_open (struct inode *);
885  struct file *file_reopen (struct file *);
886  void file_close (struct file *);
887 Index: src/filesys/filesys.c
888 diff -u src/filesys/filesys.c~ src/filesys/filesys.c
889 --- src/filesys/filesys.c~
890 +++ src/filesys/filesys.c
891 @@ -2,11 +2,13 @@
892  #include <debug.h>
893  #include <stdio.h>
894  #include <string.h>
895 +#include "filesys/cache.h"
896  #include "filesys/file.h"
897  #include "filesys/free-map.h"
898  #include "filesys/inode.h"
899  #include "filesys/directory.h"
900  #include "devices/disk.h"
901 +#include "threads/thread.h"
902  
903  /* The disk that contains the file system. */
904  struct disk *filesys_disk;
905 @@ -23,6 +25,7 @@ filesys_init (bool format) 
906      PANIC ("hd0:1 (hdb) not present, file system initialization failed");
907  
908    inode_init ();
909 +  cache_init ();
910    free_map_init ();
911  
912    if (format) 
913 @@ -37,6 +40,130 @@ void
914  filesys_done (void) 
915  {
916    free_map_close ();
917 +  cache_flush ();
918 +}
919 +\f
920 +/* Extracts a file name part from *SRCP into PART,
921 +   and updates *SRCP so that the next call will return the next
922 +   file name part.
923 +   Returns 1 if successful, 0 at end of string, -1 for a too-long
924 +   file name part. */
925 +static int
926 +get_next_part (char part[NAME_MAX], const char **srcp)
927 +{
928 +  const char *src = *srcp;
929 +  char *dst = part;
930 +
931 +  /* Skip leading slashes.
932 +     If it's all slashes, we're done. */
933 +  while (*src == '/')
934 +    src++;
935 +  if (*src == '\0')
936 +    return 0;
937 +
938 +  /* Copy up to NAME_MAX character from SRC to DST.
939 +     Add null terminator. */
940 +  while (*src != '/' && *src != '\0') 
941 +    {
942 +      if (dst < part + NAME_MAX)
943 +        *dst++ = *src;
944 +      else
945 +        return -1;
946 +      src++; 
947 +    }
948 +  *dst = '\0';
949 +
950 +  /* Advance source pointer. */
951 +  *srcp = src;
952 +  return 1;
953 +}
954 +
955 +/* Resolves relative or absolute file NAME.
956 +   Returns true if successful, false on failure.
957 +   Stores the directory corresponding to the name into *DIRP,
958 +   and the file name part into BASE_NAME. */
959 +static bool
960 +resolve_name_to_entry (const char *name,
961 +                       struct dir **dirp, char base_name[NAME_MAX + 1]) 
962 +{
963 +  struct dir *dir = NULL;
964 +  struct inode *inode;
965 +  const char *cp;
966 +  char part[NAME_MAX + 1], next_part[NAME_MAX + 1];
967 +  int ok;
968 +
969 +  /* Find starting directory. */
970 +  if (name[0] == '/' || thread_current ()->wd == NULL)
971 +    dir = dir_open_root ();
972 +  else
973 +    dir = dir_reopen (thread_current ()->wd);
974 +  if (dir == NULL)
975 +    goto error;
976 +
977 +  /* Get first name part. */
978 +  cp = name;
979 +  if (get_next_part (part, &cp) <= 0)
980 +    goto error;
981 +
982 +  /* As long as another part follows the current one,
983 +     traverse down another directory. */
984 +  while ((ok = get_next_part (next_part, &cp)) > 0)
985 +    {
986 +      if (!dir_lookup (dir, part, &inode))
987 +        goto error;
988 +
989 +      dir_close (dir);
990 +      dir = dir_open (inode);
991 +      if (dir == NULL)
992 +        goto error;
993 +
994 +      strlcpy (part, next_part, NAME_MAX + 1);
995 +    }
996 +  if (ok < 0)
997 +    goto error;
998 +
999 +  /* Return our results. */
1000 +  *dirp = dir;
1001 +  strlcpy (base_name, part, NAME_MAX + 1);
1002 +  return true;
1003 +
1004 + error:
1005 +  /* Return failure. */
1006 +  dir_close (dir);
1007 +  *dirp = NULL;
1008 +  base_name[0] = '\0';
1009 +  return false;
1010 +}
1011 +
1012 +/* Resolves relative or absolute file NAME to an inode.
1013 +   Returns an inode if successful, or a null pointer on failure.
1014 +   The caller is responsible for closing the returned inode. */
1015 +static struct inode *
1016 +resolve_name_to_inode (const char *name)
1017 +{
1018 +  if (name[0] == '/' && name[strspn (name, "/")] == '\0') 
1019 +    {
1020 +      /* The name represents the root directory.
1021 +         There's no name part at all, so resolve_name_to_entry()
1022 +         would reject it entirely.
1023 +         Special case it. */
1024 +      return inode_open (ROOT_DIR_SECTOR);
1025 +    }
1026 +  else 
1027 +    {
1028 +      struct dir *dir;
1029 +      char base_name[NAME_MAX + 1];
1030 +
1031 +      if (resolve_name_to_entry (name, &dir, base_name)) 
1032 +        {
1033 +          struct inode *inode;
1034 +          dir_lookup (dir, base_name, &inode);
1035 +          dir_close (dir);
1036 +          return inode; 
1037 +        }
1038 +      else
1039 +        return NULL;
1040 +    }
1041  }
1042  \f
1043  /* Creates a file named NAME with the given INITIAL_SIZE.
1044 @@ -44,16 +171,32 @@ filesys_done (void) 
1045     Fails if a file named NAME already exists,
1046     or if internal memory allocation fails. */
1047  bool
1048 -filesys_create (const char *name, off_t initial_size) 
1049 +filesys_create (const char *name, off_t initial_size, enum inode_type type) 
1050  {
1051 -  disk_sector_t inode_sector = 0;
1052 -  struct dir *dir = dir_open_root ();
1053 -  bool success = (dir != NULL
1054 -                  && free_map_allocate (1, &inode_sector)
1055 -                  && inode_create (inode_sector, initial_size)
1056 -                  && dir_add (dir, name, inode_sector));
1057 -  if (!success && inode_sector != 0) 
1058 -    free_map_release (inode_sector, 1);
1059 +  struct dir *dir;
1060 +  char base_name[NAME_MAX + 1];
1061 +  disk_sector_t inode_sector;
1062 +
1063 +  bool success = (resolve_name_to_entry (name, &dir, base_name)
1064 +                  && free_map_allocate (&inode_sector));
1065 +  if (success) 
1066 +    {
1067 +      struct inode *inode;
1068 +      if (type == FILE_INODE)
1069 +        inode = file_create (inode_sector, initial_size);
1070 +      else
1071 +        inode = dir_create (inode_sector,
1072 +                            inode_get_inumber (dir_get_inode (dir))); 
1073 +      if (inode != NULL)
1074 +        {
1075 +          success = dir_add (dir, base_name, inode_sector);
1076 +          if (!success)
1077 +            inode_remove (inode);
1078 +          inode_close (inode);
1079 +        }
1080 +      else
1081 +        success = false;
1082 +    }
1083    dir_close (dir);
1084  
1085    return success;
1086 @@ -64,17 +199,10 @@ filesys_create (const char *name, off_t 
1087     otherwise.
1088     Fails if no file named NAME exists,
1089     or if an internal memory allocation fails. */
1090 -struct file *
1091 +struct inode *
1092  filesys_open (const char *name)
1093  {
1094 -  struct dir *dir = dir_open_root ();
1095 -  struct inode *inode = NULL;
1096 -
1097 -  if (dir != NULL)
1098 -    dir_lookup (dir, name, &inode);
1099 -  dir_close (dir);
1100 -
1101 -  return file_open (inode);
1102 +  return resolve_name_to_inode (name);
1103  }
1104  
1105  /* Deletes the file named NAME.
1106 @@ -84,12 +212,35 @@ filesys_open (const char *name)
1107  bool
1108  filesys_remove (const char *name) 
1109  {
1110 -  struct dir *dir = dir_open_root ();
1111 -  bool success = dir != NULL && dir_remove (dir, name);
1112 -  dir_close (dir); 
1113 +  struct dir *dir;
1114 +  char base_name[NAME_MAX + 1];
1115 +  bool success;
1116  
1117 +  if (resolve_name_to_entry (name, &dir, base_name)) 
1118 +    {
1119 +      success = dir_remove (dir, base_name);
1120 +      dir_close (dir);
1121 +    }
1122 +  else
1123 +    success = false;
1124 +  
1125    return success;
1126  }
1127 +/* Change current directory to NAME.
1128 +   Return true if successful, false on failure. */
1129 +bool
1130 +filesys_chdir (const char *name) 
1131 +{
1132 +  struct dir *dir = dir_open (resolve_name_to_inode (name));
1133 +  if (dir != NULL) 
1134 +    {
1135 +      dir_close (thread_current ()->wd);
1136 +      thread_current ()->wd = dir;
1137 +      return true;
1138 +    }
1139 +  else
1140 +    return false;
1141 +}
1142  \f
1143  static void must_succeed_function (int, bool) NO_INLINE;
1144  #define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
1145 @@ -155,9 +306,18 @@ static void
1146  do_format (void)
1147  {
1148 +  struct inode *inode;
1149    printf ("Formatting file system...");
1150 +
1151 +  /* Set up free map. */
1152    free_map_create ();
1153 -  if (!dir_create (ROOT_DIR_SECTOR, 16))
1154 +
1155 +  /* Set up root directory. */
1156 +  inode = dir_create (ROOT_DIR_SECTOR, ROOT_DIR_SECTOR);
1157 +  if (inode == NULL)
1158      PANIC ("root directory creation failed");
1159 +  inode_close (inode);  
1160 +
1161    free_map_close ();
1162 +
1163    printf ("done.\n");
1164  }
1165 Index: src/filesys/filesys.h
1166 diff -u src/filesys/filesys.h~ src/filesys/filesys.h
1167 --- src/filesys/filesys.h~
1168 +++ src/filesys/filesys.h
1169 @@ -3,6 +3,7 @@
1170  
1171  #include <stdbool.h>
1172  #include "filesys/off_t.h"
1173 +#include "filesys/inode.h"
1174  
1175  /* Sectors of system file inodes. */
1176  #define FREE_MAP_SECTOR 0       /* Free map file inode sector. */
1177 @@ -13,9 +14,10 @@ extern struct disk *filesys_disk;
1178  
1179  void filesys_init (bool format);
1180  void filesys_done (void);
1181 -bool filesys_create (const char *name, off_t initial_size);
1182 -struct file *filesys_open (const char *name);
1183 +bool filesys_create (const char *name, off_t initial_size, enum inode_type);
1184 +struct inode *filesys_open (const char *name);
1185  bool filesys_remove (const char *name);
1186 +bool filesys_chdir (const char *name);
1187  
1188  void filesys_self_test (void);
1189  
1190 Index: src/filesys/free-map.c
1191 diff -u src/filesys/free-map.c~ src/filesys/free-map.c
1192 --- src/filesys/free-map.c~
1193 +++ src/filesys/free-map.c
1194 @@ -3,15 +3,18 @@
1195  #include <debug.h>
1196  #include "filesys/file.h"
1197  #include "filesys/filesys.h"
1198 -#include "filesys/inode.h"
1199 +#include "threads/synch.h"
1200  
1201  static struct file *free_map_file;   /* Free map file. */
1202  static struct bitmap *free_map;      /* Free map, one bit per disk sector. */
1203 +static struct lock free_map_lock;    /* Mutual exclusion. */
1204  
1205  /* Initializes the free map. */
1206  void
1207  free_map_init (void) 
1208  {
1209 +  lock_init (&free_map_lock);
1210 +
1211    free_map = bitmap_create (disk_size (filesys_disk));
1212    if (free_map == NULL)
1213      PANIC ("bitmap creation failed--disk is too large");
1214 @@ -19,33 +22,32 @@ free_map_init (void) 
1215    bitmap_mark (free_map, ROOT_DIR_SECTOR);
1216  }
1217  
1218 -/* Allocates CNT consecutive sectors from the free map and stores
1219 -   the first into *SECTORP.
1220 -   Returns true if successful, false if all sectors were
1221 +/* Allocates a sector from the free map and stores it into
1222 +   *SECTORP.
1223 +   Return true if successful, false if all sectors were
1224     available. */
1225  bool
1226 -free_map_allocate (size_t cnt, disk_sector_t *sectorp) 
1227 +free_map_allocate (disk_sector_t *sectorp) 
1228  {
1229 -  disk_sector_t sector = bitmap_scan_and_flip (free_map, 0, cnt, false);
1230 -  if (sector != BITMAP_ERROR
1231 -      && free_map_file != NULL
1232 -      && !bitmap_write (free_map, free_map_file))
1233 -    {
1234 -      bitmap_set_multiple (free_map, sector, cnt, false); 
1235 -      sector = BITMAP_ERROR;
1236 -    }
1237 -  if (sector != BITMAP_ERROR)
1238 +  size_t sector;
1239 +  
1240 +  lock_acquire (&free_map_lock);
1241 +  sector = bitmap_scan_and_flip (free_map, 0, 1, false);
1242 +  lock_release (&free_map_lock);
1243 +
1244 +  if (sector != BITMAP_ERROR) 
1245      *sectorp = sector;
1246    return sector != BITMAP_ERROR;
1247  }
1248  
1249 -/* Makes CNT sectors starting at SECTOR available for use. */
1250 +/* Makes SECTOR available for use. */
1251  void
1252 -free_map_release (disk_sector_t sector, size_t cnt)
1253 +free_map_release (disk_sector_t sector)
1254  {
1255 -  ASSERT (bitmap_all (free_map, sector, cnt));
1256 -  bitmap_set_multiple (free_map, sector, cnt, false);
1257 -  bitmap_write (free_map, free_map_file);
1258 +  lock_acquire (&free_map_lock);
1259 +  ASSERT (bitmap_test (free_map, sector));
1260 +  bitmap_reset (free_map, sector);
1261 +  lock_release (&free_map_lock);
1262  }
1263  
1264  /* Opens the free map file and reads it from disk. */
1265 @@ -63,6 +65,8 @@ free_map_open (void) 
1266  void
1267  free_map_close (void) 
1268  {
1269 +  if (!bitmap_write (free_map, free_map_file))
1270 +    PANIC ("can't write free map");
1271    file_close (free_map_file);
1272  }
1273  
1274 @@ -72,5 +76,9 @@ void
1275  free_map_create (void) 
1276  {
1277 +  struct inode *inode;
1278 +
1279    /* Create inode. */
1280 -  if (!inode_create (FREE_MAP_SECTOR, bitmap_file_size (free_map)))
1281 +  inode = file_create (FREE_MAP_SECTOR, 0);
1282 +  if (inode == NULL)   
1283      PANIC ("free map creation failed");
1284 +  inode_close (inode);
1285
1286    /* Write bitmap to file. */
1287 Index: src/filesys/free-map.h
1288 diff -u src/filesys/free-map.h~ src/filesys/free-map.h
1289 --- src/filesys/free-map.h~
1290 +++ src/filesys/free-map.h
1291 @@ -11,7 +11,7 @@ void free_map_create (void);
1292  void free_map_open (void);
1293  void free_map_close (void);
1294  
1295 -bool free_map_allocate (size_t, disk_sector_t *);
1296 -void free_map_release (disk_sector_t, size_t);
1297 +bool free_map_allocate (disk_sector_t *);
1298 +void free_map_release (disk_sector_t);
1299  
1300  #endif /* filesys/free-map.h */
1301 Index: src/filesys/fsutil.c
1302 diff -u src/filesys/fsutil.c~ src/filesys/fsutil.c
1303 --- src/filesys/fsutil.c~
1304 +++ src/filesys/fsutil.c
1305 @@ -38,7 +38,7 @@ fsutil_cat (char **argv)
1306    char *buffer;
1307  
1308    printf ("Printing '%s' to the console...\n", file_name);
1309 -  file = filesys_open (file_name);
1310 +  file = file_open (filesys_open (file_name));
1311    if (file == NULL)
1312      PANIC ("%s: open failed", file_name);
1313    buffer = palloc_get_page (PAL_ASSERT);
1314 @@ -117,9 +117,9 @@
1315            printf ("Putting '%s' into the file system...\n", file_name);
1316  
1317            /* Create destination file. */
1318 -          if (!filesys_create (file_name, size))
1319 +          if (!filesys_create (file_name, size, FILE_INODE))
1320              PANIC ("%s: create failed", file_name);
1321 -          dst = filesys_open (file_name);
1322 +          dst = file_open (filesys_open (file_name));
1323            if (dst == NULL)
1324              PANIC ("%s: open failed", file_name);
1325  
1326 @@ -162,7 +162,7 @@ fsutil_get (char **argv)
1327      PANIC ("couldn't allocate buffer");
1328  
1329    /* Open source file. */
1330 -  src = filesys_open (file_name);
1331 +  src = file_open (filesys_open (file_name));
1332    if (src == NULL)
1333      PANIC ("%s: open failed", file_name);
1334    size = file_length (src);
1335 Index: src/filesys/inode.c
1336 diff -u src/filesys/inode.c~ src/filesys/inode.c
1337 --- src/filesys/inode.c~
1338 +++ src/filesys/inode.c
1339 @@ -1,23 +1,38 @@
1340  #include "filesys/inode.h"
1341 +#include <bitmap.h>
1342  #include <list.h>
1343  #include <debug.h>
1344  #include <round.h>
1345 +#include <stdio.h>
1346  #include <string.h>
1347 +#include "filesys/cache.h"
1348  #include "filesys/filesys.h"
1349  #include "filesys/free-map.h"
1350  #include "threads/malloc.h"
1351 +#include "threads/synch.h"
1352  
1353  /* Identifies an inode. */
1354  #define INODE_MAGIC 0x494e4f44
1355  
1356 +#define DIRECT_CNT 123
1357 +#define INDIRECT_CNT 1
1358 +#define DBL_INDIRECT_CNT 1
1359 +#define SECTOR_CNT (DIRECT_CNT + INDIRECT_CNT + DBL_INDIRECT_CNT)
1360 +
1361 +#define PTRS_PER_SECTOR ((off_t) (DISK_SECTOR_SIZE / sizeof (disk_sector_t))) 
1362 +#define INODE_SPAN ((DIRECT_CNT                                              \
1363 +                     + PTRS_PER_SECTOR * INDIRECT_CNT                        \
1364 +                     + PTRS_PER_SECTOR * PTRS_PER_SECTOR * DBL_INDIRECT_CNT) \
1365 +                    * DISK_SECTOR_SIZE)
1366 +
1367  /* On-disk inode.
1368     Must be exactly DISK_SECTOR_SIZE bytes long. */
1369  struct inode_disk
1370    {
1371 -    disk_sector_t start;                /* First data sector. */
1372 +    disk_sector_t sectors[SECTOR_CNT];  /* Sectors. */
1373 +    enum inode_type type;               /* FILE_INODE or DIR_INODE. */
1374      off_t length;                       /* File size in bytes. */
1375      unsigned magic;                     /* Magic number. */
1376 -    uint32_t unused[125];               /* Not used. */
1377    };
1378  
1379  /* Returns the number of sectors to allocate for an inode SIZE
1380 @@ -35,74 +50,59 @@ struct inode 
1381      disk_sector_t sector;               /* Sector number of disk location. */
1382      int open_cnt;                       /* Number of openers. */
1383      bool removed;                       /* True if deleted, false otherwise. */
1384 +    struct lock lock;                   /* Protects the inode. */
1385 +
1386 +    /* Denying writes. */
1387 +    struct lock deny_write_lock;        /* Protects members below. */
1388 +    struct condition no_writers_cond;   /* Signaled when no writers. */ 
1389      int deny_write_cnt;                 /* 0: writes ok, >0: deny writes. */
1390 -    struct inode_disk data;             /* Inode content. */
1391 +    int writer_cnt;                     /* Number of writers. */
1392    };
1393  
1394 -/* Returns the disk sector that contains byte offset POS within
1395 -   INODE.
1396 -   Returns -1 if INODE does not contain data for a byte at offset
1397 -   POS. */
1398 -static disk_sector_t
1399 -byte_to_sector (const struct inode *inode, off_t pos) 
1400 -{
1401 -  ASSERT (inode != NULL);
1402 -  if (pos < inode->data.length)
1403 -    return inode->data.start + pos / DISK_SECTOR_SIZE;
1404 -  else
1405 -    return -1;
1406 -}
1407 -
1408  /* List of open inodes, so that opening a single inode twice
1409     returns the same `struct inode'. */
1410  static struct list open_inodes;
1411  
1412 +/* Controls access to open_inodes list. */
1413 +static struct lock open_inodes_lock;
1414 +
1415 +static void deallocate_inode (const struct inode *);
1416 +
1417  /* Initializes the inode module. */
1418  void
1419  inode_init (void) 
1420  {
1421    list_init (&open_inodes);
1422 +  lock_init (&open_inodes_lock);
1423  }
1424  
1425 -/* Initializes an inode with LENGTH bytes of data and
1426 -   writes the new inode to sector SECTOR on the file system
1427 -   disk.
1428 -   Returns true if successful.
1429 -   Returns false if memory or disk allocation fails. */
1430 -bool
1431 -inode_create (disk_sector_t sector, off_t length)
1432 +/* Initializes an inode of the given TYPE, writes the new inode
1433 +   to sector SECTOR on the file system disk, and returns the
1434 +   inode thus created.  Returns a null pointer if unsuccessful,
1435 +   in which case SECTOR is released in the free map. */  
1436 +struct inode *
1437 +inode_create (disk_sector_t sector, enum inode_type type) 
1438  {
1439 -  struct inode_disk *disk_inode = NULL;
1440 -  bool success = false;
1441 +  struct cache_block *block;
1442 +  struct inode_disk *disk_inode;
1443 +  struct inode *inode;
1444  
1445 -  ASSERT (length >= 0);
1446 +  block = cache_lock (sector, EXCLUSIVE);
1447  
1448    /* If this assertion fails, the inode structure is not exactly
1449       one sector in size, and you should fix that. */
1450    ASSERT (sizeof *disk_inode == DISK_SECTOR_SIZE);
1451 +  disk_inode = cache_zero (block);
1452 +  disk_inode->type = type;
1453 +  disk_inode->length = 0;
1454 +  disk_inode->magic = INODE_MAGIC;
1455 +  cache_dirty (block);
1456 +  cache_unlock (block);
1457  
1458 -  disk_inode = calloc (1, sizeof *disk_inode);
1459 -  if (disk_inode != NULL)
1460 -    {
1461 -      size_t sectors = bytes_to_sectors (length);
1462 -      disk_inode->length = length;
1463 -      disk_inode->magic = INODE_MAGIC;
1464 -      if (free_map_allocate (sectors, &disk_inode->start))
1465 -        {
1466 -          disk_write (filesys_disk, sector, disk_inode);
1467 -          if (sectors > 0) 
1468 -            {
1469 -              static char zeros[DISK_SECTOR_SIZE];
1470 -              size_t i;
1471 -              
1472 -              for (i = 0; i < sectors; i++) 
1473 -                disk_write (filesys_disk, disk_inode->start + i, zeros); 
1474 -            }
1475 -          success = true; 
1476 -        } 
1477 -      free (disk_inode);
1478 -    }
1479 -  return success;
1480 +  inode = inode_open (sector);
1481 +  if (inode == NULL)
1482 +    free_map_release (sector);
1483 +  return inode;
1484  }
1485  
1486  /* Reads an inode from SECTOR
1487 @@ -115,29 +110,35 @@ inode_open (disk_sector_t sector) 
1488    struct inode *inode;
1489  
1490    /* Check whether this inode is already open. */
1491 +  lock_acquire (&open_inodes_lock);
1492    for (e = list_begin (&open_inodes); e != list_end (&open_inodes);
1493         e = list_next (e)) 
1494      {
1495        inode = list_entry (e, struct inode, elem);
1496        if (inode->sector == sector) 
1497          {
1498 -          inode_reopen (inode);
1499 -          return inode; 
1500 +          inode->open_cnt++;
1501 +          goto done; 
1502          }
1503      }
1504  
1505    /* Allocate memory. */
1506    inode = malloc (sizeof *inode);
1507    if (inode == NULL)
1508 -    return NULL;
1509 +    goto done;
1510  
1511    /* Initialize. */
1512    list_push_front (&open_inodes, &inode->elem);
1513    inode->sector = sector;
1514    inode->open_cnt = 1;
1515 +  lock_init (&inode->lock);
1516    inode->deny_write_cnt = 0;
1517 +  lock_init (&inode->deny_write_lock);
1518 +  cond_init (&inode->no_writers_cond);
1519    inode->removed = false;
1520 -  disk_read (filesys_disk, inode->sector, &inode->data);
1521 +  
1522 + done:
1523 +  lock_release (&open_inodes_lock);
1524    return inode;
1525  }
1526  
1527 @@ -146,9 +147,24 @@ struct inode *
1528  inode_reopen (struct inode *inode)
1529  {
1530    if (inode != NULL)
1531 -    inode->open_cnt++;
1532 +    {
1533 +      lock_acquire (&open_inodes_lock);
1534 +      inode->open_cnt++;
1535 +      lock_release (&open_inodes_lock);
1536 +    }
1537    return inode;
1538  }
1539  
1540 +/* Returns the type of INODE. */
1541 +enum inode_type
1542 +inode_get_type (const struct inode *inode) 
1543 +{
1544 +  struct cache_block *inode_block = cache_lock (inode->sector, NON_EXCLUSIVE);
1545 +  struct inode_disk *disk_inode = cache_read (inode_block);
1546 +  enum inode_type type = disk_inode->type;
1547 +  cache_unlock (inode_block);
1548 +  return type;
1549 +}
1550 +
1551  /* Returns INODE's inode number. */
1552  disk_sector_t
1553 @@ -161,21 +183,60 @@ inode_close (struct inode *inode) 
1554      return;
1555  
1556    /* Release resources if this was the last opener. */
1557 +  lock_acquire (&open_inodes_lock);
1558    if (--inode->open_cnt == 0)
1559      {
1560        /* Remove from inode list and release lock. */
1561        list_remove (&inode->elem);
1562 +      lock_release (&open_inodes_lock);
1563   
1564        /* Deallocate blocks if removed. */
1565        if (inode->removed) 
1566 -        {
1567 -          free_map_release (inode->sector, 1);
1568 -          free_map_release (inode->data.start,
1569 -                            bytes_to_sectors (inode->data.length)); 
1570 -        }
1571 +        deallocate_inode (inode);
1572  
1573        free (inode); 
1574      }
1575 +  else
1576 +    lock_release (&open_inodes_lock);
1577 +}
1578 +
1579 +/* Deallocates SECTOR and anything it points to recursively.
1580 +   LEVEL is 2 if SECTOR is doubly indirect,
1581 +   or 1 if SECTOR is indirect,
1582 +   or 0 if SECTOR is a data sector. */
1583 +static void
1584 +deallocate_recursive (disk_sector_t sector, int level) 
1585 +{
1586 +  if (level > 0) 
1587 +    {
1588 +      struct cache_block *block = cache_lock (sector, EXCLUSIVE);
1589 +      disk_sector_t *pointers = cache_read (block);
1590 +      int i;
1591 +      for (i = 0; i < PTRS_PER_SECTOR; i++)
1592 +        if (pointers[i])
1593 +          deallocate_recursive (sector, level - 1);
1594 +      cache_unlock (block);
1595 +    }
1596 +  
1597 +  cache_free (sector);
1598 +  free_map_release (sector);
1599 +}
1600 +
1601 +/* Deallocates the blocks allocated for INODE. */
1602 +static void
1603 +deallocate_inode (const struct inode *inode)
1604 +{
1605 +  struct cache_block *block = cache_lock (inode->sector, EXCLUSIVE);
1606 +  struct inode_disk *disk_inode = cache_read (block);
1607 +  int i;
1608 +  for (i = 0; i < SECTOR_CNT; i++)
1609 +    if (disk_inode->sectors[i]) 
1610 +      {
1611 +        int level = (i >= DIRECT_CNT) + (i >= DIRECT_CNT + INDIRECT_CNT);
1612 +        deallocate_recursive (disk_inode->sectors[i], level); 
1613 +      }
1614 +  cache_unlock (block);
1615 +  deallocate_recursive (inode->sector, 0);
1616  }
1617  
1618  /* Marks INODE to be deleted when it is closed by the last caller who
1619 @@ -187,6 +248,156 @@ inode_remove (struct inode *inode) 
1620    inode->removed = true;
1621  }
1622  
1623 +/* Translates SECTOR_IDX into a sequence of block indexes in
1624 +   OFFSETS and sets *OFFSET_CNT to the number of offsets. */
1625 +static void
1626 +calculate_indices (off_t sector_idx, size_t offsets[], size_t *offset_cnt)
1627 +{
1628 +  /* Handle direct blocks. */
1629 +  if (sector_idx < DIRECT_CNT) 
1630 +    {
1631 +      offsets[0] = sector_idx;
1632 +      *offset_cnt = 1;
1633 +      return;
1634 +    }
1635 +  sector_idx -= DIRECT_CNT;
1636 +
1637 +  /* Handle indirect blocks. */
1638 +  if (sector_idx < PTRS_PER_SECTOR * INDIRECT_CNT)
1639 +    {
1640 +      offsets[0] = DIRECT_CNT + sector_idx / PTRS_PER_SECTOR;
1641 +      offsets[1] = sector_idx % PTRS_PER_SECTOR;
1642 +      *offset_cnt = 2;
1643 +      return;
1644 +    }
1645 +  sector_idx -= PTRS_PER_SECTOR * INDIRECT_CNT;
1646 +
1647 +  /* Handle doubly indirect blocks. */
1648 +  if (sector_idx < DBL_INDIRECT_CNT * PTRS_PER_SECTOR * PTRS_PER_SECTOR)
1649 +    {
1650 +      offsets[0] = (DIRECT_CNT + INDIRECT_CNT
1651 +                    + sector_idx / (PTRS_PER_SECTOR * PTRS_PER_SECTOR));
1652 +      offsets[1] = sector_idx / PTRS_PER_SECTOR;
1653 +      offsets[2] = sector_idx % PTRS_PER_SECTOR;
1654 +      *offset_cnt = 3;
1655 +      return;
1656 +    }
1657 +  NOT_REACHED ();
1658 +}
1659 +
1660 +/* Retrieves the data block for the given byte OFFSET in INODE,
1661 +   setting *DATA_BLOCK to the block.
1662 +   Returns true if successful, false on failure.
1663 +   If ALLOCATE is false, then missing blocks will be successful
1664 +   with *DATA_BLOCk set to a null pointer.
1665 +   If ALLOCATE is true, then missing blocks will be allocated.
1666 +   The block returned will be locked, normally non-exclusively,
1667 +   but a newly allocated block will have an exclusive lock. */
1668 +static bool
1669 +get_data_block (struct inode *inode, off_t offset, bool allocate,
1670 +                struct cache_block **data_block) 
1671 +{
1672 +  disk_sector_t this_level_sector;
1673 +  size_t offsets[3];
1674 +  size_t offset_cnt;
1675 +  size_t level;
1676 +
1677 +  ASSERT (offset >= 0);
1678 +
1679 +  calculate_indices (offset / DISK_SECTOR_SIZE, offsets, &offset_cnt);
1680 +  level = 0;
1681 +  this_level_sector = inode->sector;
1682 +  for (;;) 
1683 +    {
1684 +      struct cache_block *this_level_block;
1685 +      uint32_t *this_level_data;
1686 +
1687 +      struct cache_block *next_level_block;
1688 +
1689 +      /* Check whether the block for the next level is allocated. */
1690 +      this_level_block = cache_lock (this_level_sector, NON_EXCLUSIVE);
1691 +      this_level_data = cache_read (this_level_block);
1692 +      if (this_level_data[offsets[level]] != 0)
1693 +        {
1694 +          /* Yes, it's allocated.  Advance to next level. */
1695 +          this_level_sector = this_level_data[offsets[level]];
1696 +
1697 +          if (++level == offset_cnt) 
1698 +            {
1699 +              /* We hit the data block.
1700 +                 Do read-ahead. */
1701 +              if ((level == 0 && offsets[level] + 1 < DIRECT_CNT)
1702 +                  || (level > 0 && offsets[level] + 1 < PTRS_PER_SECTOR)) 
1703 +                {
1704 +                  uint32_t next_sector = this_level_data[offsets[level] + 1];
1705 +                  if (next_sector && next_sector < disk_size (filesys_disk))
1706 +                    cache_readahead (next_sector); 
1707 +                }
1708 +              cache_unlock (this_level_block);
1709 +
1710 +              /* Return block. */
1711 +              *data_block = cache_lock (this_level_sector, NON_EXCLUSIVE);
1712 +              return true;
1713 +            }
1714 +          cache_unlock (this_level_block);
1715 +          continue;
1716 +        }
1717 +      cache_unlock (this_level_block);
1718 +
1719 +      /* No block is allocated.  Nothing is locked.
1720 +         If we're not allocating new blocks, then this is
1721 +         "success" (with all-zero data). */
1722 +      if (!allocate) 
1723 +        {
1724 +          *data_block = NULL;
1725 +          return true;
1726 +        }
1727 +
1728 +      /* We need to allocate a new block.
1729 +         Grab an exclusive lock on this level's block so we can
1730 +         insert the new block. */
1731 +      this_level_block = cache_lock (this_level_sector, EXCLUSIVE);
1732 +      this_level_data = cache_read (this_level_block);
1733 +
1734 +      /* Since we released this level's block, someone else might
1735 +         have allocated the block in the meantime.  Recheck. */
1736 +      if (this_level_data[offsets[level]] != 0)
1737 +        {
1738 +          cache_unlock (this_level_block);
1739 +          continue;
1740 +        }
1741 +
1742 +      /* Allocate the new block. */
1743 +      if (!free_map_allocate (&this_level_data[offsets[level]]))
1744 +        {
1745 +          cache_unlock (this_level_block);
1746 +          *data_block = NULL;
1747 +          return false;
1748 +        }
1749 +      cache_dirty (this_level_block);
1750 +
1751 +      /* Lock and clear the new block. */
1752 +      next_level_block = cache_lock (this_level_data[offsets[level]],
1753 +                                     EXCLUSIVE);
1754 +      cache_zero (next_level_block);
1755 +
1756 +      /* Release this level's block.  No one else can access the
1757 +         new block yet, because we have an exclusive lock on it. */
1758 +      cache_unlock (this_level_block);
1759 +
1760 +      /* If this is the final level, then return the new block. */
1761 +      if (level == offset_cnt - 1) 
1762 +        {
1763 +          *data_block = next_level_block;
1764 +          return true;
1765 +        }
1766 +
1767 +      /* Otherwise, release the new block and go around again to
1768 +         follow the new pointer. */
1769 +      cache_unlock (next_level_block);
1770 +    }
1771 +}
1772 +
1773  /* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET.
1774     Returns the number of bytes actually read, which may be less
1775     than SIZE if an error occurs or end of file is reached. */
1776 @@ -195,13 +406,12 @@ inode_read_at (struct inode *inode, void
1777  {
1778    uint8_t *buffer = buffer_;
1779    off_t bytes_read = 0;
1780 -  uint8_t *bounce = NULL;
1781  
1782    while (size > 0) 
1783      {
1784 -      /* Disk sector to read, starting byte offset within sector. */
1785 -      disk_sector_t sector_idx = byte_to_sector (inode, offset);
1786 +      /* Sector to read, starting byte offset within sector, sector data. */
1787        int sector_ofs = offset % DISK_SECTOR_SIZE;
1788 +      struct cache_block *block;
1789  
1790        /* Bytes left in inode, bytes left in sector, lesser of the two. */
1791        off_t inode_left = inode_length (inode) - offset;
1792 @@ -210,26 +420,16 @@ inode_read_at (struct inode *inode, void
1793  
1794        /* Number of bytes to actually copy out of this sector. */
1795        int chunk_size = size < min_left ? size : min_left;
1796 -      if (chunk_size <= 0)
1797 +      if (chunk_size <= 0 || !get_data_block (inode, offset, false, &block))
1798          break;
1799  
1800 -      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
1801 -        {
1802 -          /* Read full sector directly into caller's buffer. */
1803 -          disk_read (filesys_disk, sector_idx, buffer + bytes_read); 
1804 -        }
1805 +      if (block == NULL) 
1806 +        memset (buffer + bytes_read, 0, chunk_size);
1807        else 
1808          {
1809 -          /* Read sector into bounce buffer, then partially copy
1810 -             into caller's buffer. */
1811 -          if (bounce == NULL) 
1812 -            {
1813 -              bounce = malloc (DISK_SECTOR_SIZE);
1814 -              if (bounce == NULL)
1815 -                break;
1816 -            }
1817 -          disk_read (filesys_disk, sector_idx, bounce);
1818 -          memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size);
1819 +          const uint8_t *sector_data = cache_read (block);
1820 +          memcpy (buffer + bytes_read, sector_data + sector_ofs, chunk_size);
1821 +          cache_unlock (block);
1822          }
1823        
1824        /* Advance. */
1825 @@ -237,75 +437,82 @@ inode_read_at (struct inode *inode, void
1826        offset += chunk_size;
1827        bytes_read += chunk_size;
1828      }
1829 -  free (bounce);
1830  
1831    return bytes_read;
1832  }
1833  
1834 +/* Extends INODE to be at least LENGTH bytes long. */
1835 +static void
1836 +extend_file (struct inode *inode, off_t length) 
1837 +{
1838 +  if (length > inode_length (inode)) 
1839 +    {
1840 +      struct cache_block *inode_block = cache_lock (inode->sector, EXCLUSIVE);
1841 +      struct inode_disk *disk_inode = cache_read (inode_block);
1842 +      if (length > disk_inode->length) 
1843 +        {
1844 +          disk_inode->length = length;
1845 +          cache_dirty (inode_block);
1846 +        }
1847 +      cache_unlock (inode_block);
1848 +    }
1849 +}
1850 +
1851  /* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET.
1852     Returns the number of bytes actually written, which may be
1853 -   less than SIZE if end of file is reached or an error occurs.
1854 -   (Normally a write at end of file would extend the inode, but
1855 -   growth is not yet implemented.) */
1856 +   less than SIZE if an error occurs. */
1857  off_t
1858  inode_write_at (struct inode *inode, const void *buffer_, off_t size,
1859                  off_t offset) 
1860  {
1861    const uint8_t *buffer = buffer_;
1862    off_t bytes_written = 0;
1863 -  uint8_t *bounce = NULL;
1864  
1865 -  if (inode->deny_write_cnt)
1866 -    return 0;
1867 +  /* Don't write if writes are denied. */
1868 +  lock_acquire (&inode->deny_write_lock);
1869 +  if (inode->deny_write_cnt) 
1870 +    {
1871 +      lock_release (&inode->deny_write_lock);
1872 +      return 0;
1873 +    }
1874 +  inode->writer_cnt++;
1875 +  lock_release (&inode->deny_write_lock);
1876  
1877    while (size > 0) 
1878      {
1879 -      /* Sector to write, starting byte offset within sector. */
1880 -      disk_sector_t sector_idx = byte_to_sector (inode, offset);
1881 +      /* Sector to write, starting byte offset within sector, sector data. */
1882        int sector_ofs = offset % DISK_SECTOR_SIZE;
1883 +      struct cache_block *block;
1884 +      uint8_t *sector_data;
1885  
1886 -      /* Bytes left in inode, bytes left in sector, lesser of the two. */
1887 -      off_t inode_left = inode_length (inode) - offset;
1888 +      /* Bytes to max inode size, bytes left in sector, lesser of the two. */
1889 +      off_t inode_left = INODE_SPAN - offset;
1890        int sector_left = DISK_SECTOR_SIZE - sector_ofs;
1891        int min_left = inode_left < sector_left ? inode_left : sector_left;
1892  
1893        /* Number of bytes to actually write into this sector. */
1894        int chunk_size = size < min_left ? size : min_left;
1895 -      if (chunk_size <= 0)
1896 -        break;
1897  
1898 -      if (sector_ofs == 0 && chunk_size == DISK_SECTOR_SIZE) 
1899 -        {
1900 -          /* Write full sector directly to disk. */
1901 -          disk_write (filesys_disk, sector_idx, buffer + bytes_written); 
1902 -        }
1903 -      else 
1904 -        {
1905 -          /* We need a bounce buffer. */
1906 -          if (bounce == NULL) 
1907 -            {
1908 -              bounce = malloc (DISK_SECTOR_SIZE);
1909 -              if (bounce == NULL)
1910 -                break;
1911 -            }
1912 +      if (chunk_size <= 0 || !get_data_block (inode, offset, true, &block))
1913 +        break;
1914  
1915 -          /* If the sector contains data before or after the chunk
1916 -             we're writing, then we need to read in the sector
1917 -             first.  Otherwise we start with a sector of all zeros. */
1918 -          if (sector_ofs > 0 || chunk_size < sector_left) 
1919 -            disk_read (filesys_disk, sector_idx, bounce);
1920 -          else
1921 -            memset (bounce, 0, DISK_SECTOR_SIZE);
1922 -          memcpy (bounce + sector_ofs, buffer + bytes_written, chunk_size);
1923 -          disk_write (filesys_disk, sector_idx, bounce); 
1924 -        }
1925 +      sector_data = cache_read (block);
1926 +      memcpy (sector_data + sector_ofs, buffer + bytes_written, chunk_size);
1927 +      cache_dirty (block);
1928 +      cache_unlock (block);
1929  
1930        /* Advance. */
1931        size -= chunk_size;
1932        offset += chunk_size;
1933        bytes_written += chunk_size;
1934      }
1935 -  free (bounce);
1936 +
1937 +  extend_file (inode, offset);
1938 +
1939 +  lock_acquire (&inode->deny_write_lock);
1940 +  if (--inode->writer_cnt == 0)
1941 +    cond_signal (&inode->no_writers_cond, &inode->deny_write_lock);
1942 +  lock_release (&inode->deny_write_lock);
1943  
1944    return bytes_written;
1945  }
1946 @@ -315,8 +522,12 @@ inode_write_at (struct inode *inode, con
1947  void
1948  inode_deny_write (struct inode *inode) 
1949  {
1950 +  lock_acquire (&inode->deny_write_lock);
1951 +  while (inode->writer_cnt > 0)
1952 +    cond_wait (&inode->no_writers_cond, &inode->deny_write_lock);
1953 +  ASSERT (inode->deny_write_cnt < inode->open_cnt);
1954    inode->deny_write_cnt++;
1955 -  ASSERT (inode->deny_write_cnt <= inode->open_cnt);
1956 +  lock_release (&inode->deny_write_lock);
1957  }
1958  
1959  /* Re-enables writes to INODE.
1960 @@ -325,14 +536,47 @@ inode_deny_write (struct inode *inode) 
1961  void
1962  inode_allow_write (struct inode *inode) 
1963  {
1964 +  lock_acquire (&inode->deny_write_lock);
1965    ASSERT (inode->deny_write_cnt > 0);
1966    ASSERT (inode->deny_write_cnt <= inode->open_cnt);
1967    inode->deny_write_cnt--;
1968 +  lock_release (&inode->deny_write_lock);
1969  }
1970  
1971  /* Returns the length, in bytes, of INODE's data. */
1972  off_t
1973  inode_length (const struct inode *inode)
1974  {
1975 -  return inode->data.length;
1976 +  struct cache_block *inode_block = cache_lock (inode->sector, NON_EXCLUSIVE);
1977 +  struct inode_disk *disk_inode = cache_read (inode_block);
1978 +  off_t length = disk_inode->length;
1979 +  cache_unlock (inode_block);
1980 +  return length;
1981 +}
1982 +
1983 +/* Returns the number of openers. */
1984 +int
1985 +inode_open_cnt (const struct inode *inode) 
1986 +{
1987 +  int open_cnt;
1988 +  
1989 +  lock_acquire (&open_inodes_lock);
1990 +  open_cnt = inode->open_cnt;
1991 +  lock_release (&open_inodes_lock);
1992 +
1993 +  return open_cnt;
1994 +}
1995 +
1996 +/* Locks INODE. */
1997 +void
1998 +inode_lock (struct inode *inode) 
1999 +{
2000 +  lock_acquire (&inode->lock);
2001 +}
2002 +
2003 +/* Releases INODE's lock. */
2004 +void
2005 +inode_unlock (struct inode *inode) 
2006 +{
2007 +  lock_release (&inode->lock);
2008  }
2009 Index: src/filesys/inode.h
2010 diff -u src/filesys/inode.h~ src/filesys/inode.h
2011 --- src/filesys/inode.h~
2012 +++ src/filesys/inode.h
2013 @@ -7,11 +7,19 @@
2014  
2015  struct bitmap;
2016  
2017 +/* Type of an inode. */
2018 +enum inode_type 
2019 +  {
2020 +    FILE_INODE,         /* Ordinary file. */
2021 +    DIR_INODE           /* Directory. */
2022 +  };
2023 +
2024  void inode_init (void);
2025 -bool inode_create (disk_sector_t, off_t);
2026 +struct inode *inode_create (disk_sector_t, enum inode_type);
2027  struct inode *inode_open (disk_sector_t);
2028  struct inode *inode_reopen (struct inode *);
2029 +enum inode_type inode_get_type (const struct inode *);
2030  disk_sector_t inode_get_inumber (const struct inode *);
2031  void inode_close (struct inode *);
2032  void inode_remove (struct inode *);
2033  off_t inode_read_at (struct inode *, void *, off_t size, off_t offset);
2034 @@ -18,5 +27,8 @@ off_t inode_write_at (struct inode *, co
2035  void inode_deny_write (struct inode *);
2036  void inode_allow_write (struct inode *);
2037  off_t inode_length (const struct inode *);
2038 +int inode_open_cnt (const struct inode *);
2039 +void inode_lock (struct inode *);
2040 +void inode_unlock (struct inode *);
2041  
2042  #endif /* filesys/inode.h */
2043 Index: src/threads/init.c
2044 diff -u src/threads/init.c~ src/threads/init.c
2045 --- src/threads/init.c~
2046 +++ src/threads/init.c
2047 @@ -33,6 +33,8 @@
2048  #include "filesys/filesys.h"
2049  #include "filesys/fsutil.h"
2050  #endif
2051 +#include "vm/frame.h"
2052 +#include "vm/swap.h"
2053  
2054  /* Amount of physical memory, in 4 kB pages. */
2055  size_t ram_pages;
2056 @@ -124,6 +126,9 @@ main (void)
2057    filesys_init (format_filesys);
2058  #endif
2059  
2060 +  frame_init ();
2061 +  swap_init ();
2062 +
2063    printf ("Boot complete.\n");
2064    
2065    /* Run actions specified on kernel command line. */
2066 Index: src/threads/interrupt.c
2067 diff -u src/threads/interrupt.c~ src/threads/interrupt.c
2068 --- src/threads/interrupt.c~
2069 +++ src/threads/interrupt.c
2070 @@ -354,6 +354,8 @@ intr_handler (struct intr_frame *frame) 
2071        in_external_intr = true;
2072        yield_on_return = false;
2073      }
2074 +  else 
2075 +    thread_current ()->user_esp = frame->esp;
2076  
2077    /* Invoke the interrupt's handler.
2078       If there is no handler, invoke the unexpected interrupt
2079 Index: src/threads/thread.c
2080 diff -u src/threads/thread.c~ src/threads/thread.c
2081 --- src/threads/thread.c~
2082 +++ src/threads/thread.c
2083 @@ -13,6 +13,7 @@
2084  #include "threads/vaddr.h"
2085  #ifdef USERPROG
2086  #include "userprog/process.h"
2087 +#include "userprog/syscall.h"
2088  #endif
2089  
2090  /* Random value for struct thread's `magic' member.
2091 @@ -55,7 +56,8 @@ static void kernel_thread (thread_func *
2092  static void idle (void *aux UNUSED);
2093  static struct thread *running_thread (void);
2094  static struct thread *next_thread_to_run (void);
2095 -static void init_thread (struct thread *, const char *name, int priority);
2096 +static void init_thread (struct thread *, const char *name, int priority,
2097 +                         tid_t);
2098  static bool is_thread (struct thread *) UNUSED;
2099  static void *alloc_frame (struct thread *, size_t size);
2100  static void schedule (void);
2101 @@ -82,9 +84,8 @@ thread_init (void) 
2102  
2103    /* Set up a thread structure for the running thread. */
2104    initial_thread = running_thread ();
2105 -  init_thread (initial_thread, "main", PRI_DEFAULT);
2106 +  init_thread (initial_thread, "main", PRI_DEFAULT, 0);
2107    initial_thread->status = THREAD_RUNNING;
2108 -  initial_thread->tid = allocate_tid ();
2109  }
2110  
2111  /* Starts preemptive thread scheduling by enabling interrupts.
2112 @@ -159,8 +160,8 @@ thread_create (const char *name, int pri
2113      return TID_ERROR;
2114  
2115    /* Initialize thread. */
2116 -  init_thread (t, name, priority);
2117 -  tid = t->tid = allocate_tid ();
2118 +  init_thread (t, name, priority, allocate_tid ());
2119 +  tid = t->tid;
2120  
2121    /* Stack frame for kernel_thread(). */
2122    kf = alloc_frame (t, sizeof *kf);
2123 @@ -288,10 +289,11 @@ thread_tid (void) 
2124  void
2125  thread_exit (void) 
2126  {
2127    ASSERT (!intr_context ());
2128  
2129 +  syscall_exit ();
2130  #ifdef USERPROG
2131    process_exit ();
2132  #endif
2133  
2134    /* Remove thread from all threads list, set our status to dying,
2135 @@ -406,17 +410,29 @@ is_thread (struct thread *t)
2136  /* Does basic initialization of T as a blocked thread named
2137     NAME. */
2138  static void
2139 -init_thread (struct thread *t, const char *name, int priority)
2140 +init_thread (struct thread *t, const char *name, int priority, tid_t tid)
2141  {
2142    ASSERT (t != NULL);
2143    ASSERT (PRI_MIN <= priority && priority <= PRI_MAX);
2144    ASSERT (name != NULL);
2145  
2146    memset (t, 0, sizeof *t);
2147 +  t->tid = tid;
2148    t->status = THREAD_BLOCKED;
2149    strlcpy (t->name, name, sizeof t->name);
2150    t->stack = (uint8_t *) t + PGSIZE;
2151    t->priority = priority;
2152 +  t->exit_code = -1;
2153 +  t->wait_status = NULL;
2154 +  list_init (&t->children);
2155 +  sema_init (&t->timer_sema, 0);
2156 +  t->pagedir = NULL;
2157 +  t->pages = NULL;
2158 +  t->bin_file = NULL;
2159 +  list_init (&t->fds);
2160 +  list_init (&t->mappings);
2161 +  t->next_handle = 2;
2162 +  t->wd = NULL;
2163    t->magic = THREAD_MAGIC;
2164  }
2165  
2166 Index: src/threads/thread.h
2167 diff -u src/threads/thread.h~ src/threads/thread.h
2168 --- src/threads/thread.h~
2169 +++ src/threads/thread.h
2170 @@ -2,8 +2,10 @@
2171  #define THREADS_THREAD_H
2172  
2173  #include <debug.h>
2174 +#include <hash.h>
2175  #include <list.h>
2176  #include <stdint.h>
2177 +#include "threads/synch.h"
2178  
2179  /* States in a thread's life cycle. */
2180  enum thread_status
2181 @@ -89,18 +91,50 @@ struct thread
2182      uint8_t *stack;                     /* Saved stack pointer. */
2183      int priority;                       /* Priority. */
2184  
2185 +    /* Owned by process.c. */
2186 +    int exit_code;                      /* Exit code. */
2187 +    struct wait_status *wait_status;    /* This process's completion status. */
2188 +    struct list children;               /* Completion status of children. */
2189 +
2190      /* Shared between thread.c and synch.c. */
2191      struct list_elem elem;              /* List element. */
2192  
2193 -#ifdef USERPROG
2194 +    /* Alarm clock. */
2195 +    int64_t wakeup_time;                /* Time to wake this thread up. */
2196 +    struct list_elem timer_elem;        /* Element in timer_wait_list. */
2197 +    struct semaphore timer_sema;        /* Semaphore. */
2198 +
2199      /* Owned by userprog/process.c. */
2200      uint32_t *pagedir;                  /* Page directory. */
2201 -#endif
2202 +    struct hash *pages;                 /* Page table. */
2203 +    struct file *bin_file;              /* The binary executable. */
2204 +
2205 +    /* Owned by syscall.c. */
2206 +    struct list fds;                    /* List of file descriptors. */
2207 +    struct list mappings;               /* Memory-mapped files. */
2208 +    int next_handle;                    /* Next handle value. */
2209 +    void *user_esp;                     /* User's stack pointer. */
2210 +    struct dir *wd;                     /* Working directory. */
2211  
2212      /* Owned by thread.c. */
2213      unsigned magic;                     /* Detects stack overflow. */
2214    };
2215  
2216 +/* Tracks the completion of a process.
2217 +   Reference held by both the parent, in its `children' list,
2218 +   and by the child, in its `wait_status' pointer. */
2219 +struct wait_status
2220 +  {
2221 +    struct list_elem elem;              /* `children' list element. */
2222 +    struct lock lock;                   /* Protects ref_cnt. */
2223 +    int ref_cnt;                        /* 2=child and parent both alive,
2224 +                                           1=either child or parent alive,
2225 +                                           0=child and parent both dead. */
2226 +    tid_t tid;                          /* Child thread id. */
2227 +    int exit_code;                      /* Child exit code, if dead. */
2228 +    struct semaphore dead;              /* 1=child alive, 0=child dead. */
2229 +  };
2230 +
2231  /* If false (default), use round-robin scheduler.
2232     If true, use multi-level feedback queue scheduler.
2233     Controlled by kernel command-line options "-o mlfqs".
2234 Index: src/userprog/exception.c
2235 diff -u src/userprog/exception.c~ src/userprog/exception.c
2236 --- src/userprog/exception.c~
2237 +++ src/userprog/exception.c
2238 @@ -4,6 +4,7 @@
2239  #include "userprog/gdt.h"
2240  #include "threads/interrupt.h"
2241  #include "threads/thread.h"
2242 +#include "vm/page.h"
2243  
2244  /* Number of page faults processed. */
2245  static long long page_fault_cnt;
2246 @@ -148,9 +149,14 @@ page_fault (struct intr_frame *f) 
2247    write = (f->error_code & PF_W) != 0;
2248    user = (f->error_code & PF_U) != 0;
2249  
2250 -  /* To implement virtual memory, delete the rest of the function
2251 -     body, and replace it with code that brings in the page to
2252 -     which fault_addr refers. */
2253 +  /* Allow the pager to try to handle it. */
2254 +  if (user && not_present)
2255 +    {
2256 +      if (!page_in (fault_addr))
2257 +        thread_exit ();
2258 +      return;
2259 +    }
2260 +
2261    printf ("Page fault at %p: %s error %s page in %s context.\n",
2262            fault_addr,
2263            not_present ? "not present" : "rights violation",
2264 Index: src/userprog/pagedir.c
2265 diff -u src/userprog/pagedir.c~ src/userprog/pagedir.c
2266 --- src/userprog/pagedir.c~
2267 +++ src/userprog/pagedir.c
2268 @@ -35,15 +35,7 @@ pagedir_destroy (uint32_t *pd) 
2269    ASSERT (pd != init_page_dir);
2270    for (pde = pd; pde < pd + pd_no (PHYS_BASE); pde++)
2271      if (*pde & PTE_P) 
2272 -      {
2273 -        uint32_t *pt = pde_get_pt (*pde);
2274 -        uint32_t *pte;
2275 -        
2276 -        for (pte = pt; pte < pt + PGSIZE / sizeof *pte; pte++)
2277 -          if (*pte & PTE_P) 
2278 -            palloc_free_page (pte_get_page (*pte));
2279 -        palloc_free_page (pt);
2280 -      }
2281 +      palloc_free_page (pde_get_pt (*pde));
2282    palloc_free_page (pd);
2283  }
2284  
2285 Index: src/userprog/process.c
2286 diff -u src/userprog/process.c~ src/userprog/process.c
2287 --- src/userprog/process.c~
2288 +++ src/userprog/process.c
2289 @@ -14,12 +14,27 @@
2290  #include "threads/flags.h"
2291  #include "threads/init.h"
2292  #include "threads/interrupt.h"
2293 +#include "threads/malloc.h"
2294  #include "threads/palloc.h"
2295  #include "threads/thread.h"
2296  #include "threads/vaddr.h"
2297 +#include "vm/page.h"
2298 +#include "vm/frame.h"
2299  
2300  static thread_func start_process NO_RETURN;
2301 -static bool load (const char *cmdline, void (**eip) (void), void **esp);
2302 +static bool load (const char *cmd_line, void (**eip) (void), void **esp);
2303 +
2304 +/* Data structure shared between process_execute() in the
2305 +   invoking thread and start_process() in the newly invoked
2306 +   thread. */
2307 +struct exec_info 
2308 +  {
2309 +    const char *file_name;              /* Program to load. */
2310 +    struct semaphore load_done;         /* "Up"ed when loading complete. */
2311 +    struct wait_status *wait_status;    /* Child process. */
2312 +    struct dir *wd;                     /* Working directory. */
2313 +    bool success;                       /* Program successfully loaded? */
2314 +  };
2315  
2316  /* Starts a new thread running a user program loaded from
2317     FILENAME.  The new thread may be scheduled (and may even exit)
2318 @@ -28,41 +43,78 @@ static bool load (const char *cmdline, v
2319  tid_t
2320  process_execute (const char *file_name) 
2321  {
2322 -  char *fn_copy;
2323 +  struct dir *wd = thread_current ()->wd;
2324 +  struct exec_info exec;
2325 +  char thread_name[16];
2326 +  char *save_ptr;
2327    tid_t tid;
2328  
2329 -  /* Make a copy of FILE_NAME.
2330 -     Otherwise there's a race between the caller and load(). */
2331 -  fn_copy = palloc_get_page (0);
2332 -  if (fn_copy == NULL)
2333 +  /* Initialize exec_info. */
2334 +  exec.file_name = file_name;
2335 +  exec.wd = wd != NULL ? dir_reopen (wd) : dir_open_root ();
2336 +  if (exec.wd == NULL)
2337      return TID_ERROR;
2338 -  strlcpy (fn_copy, file_name, PGSIZE);
2339 +  sema_init (&exec.load_done, 0);
2340  
2341    /* Create a new thread to execute FILE_NAME. */
2342 -  tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy);
2343 -  if (tid == TID_ERROR)
2344 -    palloc_free_page (fn_copy); 
2345 +  strlcpy (thread_name, file_name, sizeof thread_name);
2346 +  strtok_r (thread_name, " ", &save_ptr);
2347 +  tid = thread_create (thread_name, PRI_DEFAULT, start_process, &exec);
2348 +  if (tid != TID_ERROR)
2349 +    {
2350 +      sema_down (&exec.load_done);
2351 +      if (exec.success)
2352 +        list_push_back (&thread_current ()->children, &exec.wait_status->elem);
2353 +      else 
2354 +        {
2355 +          tid = TID_ERROR;
2356 +          /* Don't close exec.wd; child process will have done so. */
2357 +        }
2358 +    }
2359 +  else
2360 +    dir_close (exec.wd);
2361 +
2362    return tid;
2363  }
2364  
2365  /* A thread function that loads a user process and starts it
2366     running. */
2367  static void
2368 -start_process (void *file_name_)
2369 +start_process (void *exec_)
2370  {
2371 -  char *file_name = file_name_;
2372 +  struct exec_info *exec = exec_;
2373    struct intr_frame if_;
2374    bool success;
2375  
2376 +  thread_current ()->wd = exec->wd;
2377 +
2378    /* Initialize interrupt frame and load executable. */
2379    memset (&if_, 0, sizeof if_);
2380    if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
2381    if_.cs = SEL_UCSEG;
2382    if_.eflags = FLAG_IF | FLAG_MBS;
2383 -  success = load (file_name, &if_.eip, &if_.esp);
2384 +  success = load (exec->file_name, &if_.eip, &if_.esp);
2385  
2386 -  /* If load failed, quit. */
2387 -  palloc_free_page (file_name);
2388 +  /* Allocate wait_status. */
2389 +  if (success)
2390 +    {
2391 +      exec->wait_status = thread_current ()->wait_status
2392 +        = malloc (sizeof *exec->wait_status);
2393 +      success = exec->wait_status != NULL; 
2394 +    }
2395 +
2396 +  /* Initialize wait_status. */
2397 +  if (success) 
2398 +    {
2399 +      lock_init (&exec->wait_status->lock);
2400 +      exec->wait_status->ref_cnt = 2;
2401 +      exec->wait_status->tid = thread_current ()->tid;
2402 +      sema_init (&exec->wait_status->dead, 0);
2403 +    }
2404 +  
2405 +  /* Notify parent thread and clean up. */
2406 +  exec->success = success;
2407 +  sema_up (&exec->load_done);
2408    if (!success) 
2409      thread_exit ();
2410  
2411 @@ -76,18 +128,47 @@ start_process (void *file_name_)
2412    NOT_REACHED ();
2413  }
2414  
2415 +/* Releases one reference to CS and, if it is now unreferenced,
2416 +   frees it. */
2417 +static void
2418 +release_child (struct wait_status *cs) 
2419 +{
2420 +  int new_ref_cnt;
2421 +  
2422 +  lock_acquire (&cs->lock);
2423 +  new_ref_cnt = --cs->ref_cnt;
2424 +  lock_release (&cs->lock);
2425 +
2426 +  if (new_ref_cnt == 0)
2427 +    free (cs);
2428 +}
2429 +
2430  /* Waits for thread TID to die and returns its exit status.  If
2431     it was terminated by the kernel (i.e. killed due to an
2432     exception), returns -1.  If TID is invalid or if it was not a
2433     child of the calling process, or if process_wait() has already
2434     been successfully called for the given TID, returns -1
2435 -   immediately, without waiting.
2436 -
2437 -   This function will be implemented in problem 2-2.  For now, it
2438 -   does nothing. */
2439 +   immediately, without waiting. */
2440  int
2441 -process_wait (tid_t child_tid UNUSED) 
2442 +process_wait (tid_t child_tid) 
2443  {
2444 +  struct thread *cur = thread_current ();
2445 +  struct list_elem *e;
2446 +
2447 +  for (e = list_begin (&cur->children); e != list_end (&cur->children);
2448 +       e = list_next (e)) 
2449 +    {
2450 +      struct wait_status *cs = list_entry (e, struct wait_status, elem);
2451 +      if (cs->tid == child_tid) 
2452 +        {
2453 +          int exit_code;
2454 +          list_remove (e);
2455 +          sema_down (&cs->dead);
2456 +          exit_code = cs->exit_code;
2457 +          release_child (cs);
2458 +          return exit_code;
2459 +        }
2460 +    }
2461    return -1;
2462  }
2463  
2464 @@ -96,8 +177,35 @@ void
2465  process_exit (void)
2466  {
2467    struct thread *cur = thread_current ();
2468 +  struct list_elem *e, *next;
2469    uint32_t *pd;
2470  
2471 +  printf ("%s: exit(%d)\n", cur->name, cur->exit_code);
2472 +
2473 +  /* Notify parent that we're dead. */
2474 +  if (cur->wait_status != NULL) 
2475 +    {
2476 +      struct wait_status *cs = cur->wait_status;
2477 +      cs->exit_code = cur->exit_code;
2478 +      sema_up (&cs->dead);
2479 +      release_child (cs);
2480 +    }
2481 +
2482 +  /* Free entries of children list. */
2483 +  for (e = list_begin (&cur->children); e != list_end (&cur->children);
2484 +       e = next) 
2485 +    {
2486 +      struct wait_status *cs = list_entry (e, struct wait_status, elem);
2487 +      next = list_remove (e);
2488 +      release_child (cs);
2489 +    }
2490 +
2491 +  /* Destroy the page hash table. */
2492 +  page_exit ();
2493 +  
2494 +  /* Close executable (and allow writes). */
2495 +  file_close (cur->bin_file);
2496 +
2497    /* Destroy the current process's page directory and switch back
2498       to the kernel-only page directory. */
2499    pd = cur->pagedir;
2500 @@ -194,7 +302,7 @@ struct Elf32_Phdr
2501  #define PF_W 2          /* Writable. */
2502  #define PF_R 4          /* Readable. */
2503  
2504 -static bool setup_stack (void **esp);
2505 +static bool setup_stack (const char *cmd_line, void **esp);
2506  static bool validate_segment (const struct Elf32_Phdr *, struct file *);
2507  static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
2508                            uint32_t read_bytes, uint32_t zero_bytes,
2509 @@ -205,13 +313,15 @@ static bool load_segment (struct file *f
2510     and its initial stack pointer into *ESP.
2511     Returns true if successful, false otherwise. */
2512  bool
2513 -load (const char *file_name, void (**eip) (void), void **esp) 
2514 +load (const char *cmd_line, void (**eip) (void), void **esp) 
2515  {
2516    struct thread *t = thread_current ();
2517 +  char file_name[NAME_MAX + 2];
2518    struct Elf32_Ehdr ehdr;
2519    struct file *file = NULL;
2520    off_t file_ofs;
2521    bool success = false;
2522 +  char *cp;
2523    int i;
2524  
2525    /* Allocate and activate page directory. */
2526 @@ -220,13 +330,28 @@ load (const char *file_name, void (**eip
2527      goto done;
2528    process_activate ();
2529  
2530 +  /* Create page hash table. */
2531 +  t->pages = malloc (sizeof *t->pages);
2532 +  if (t->pages == NULL)
2533 +    goto done;
2534 +  hash_init (t->pages, page_hash, page_less, NULL);
2535 +
2536 +  /* Extract file_name from command line. */
2537 +  while (*cmd_line == ' ')
2538 +    cmd_line++;
2539 +  strlcpy (file_name, cmd_line, sizeof file_name);
2540 +  cp = strchr (file_name, ' ');
2541 +  if (cp != NULL)
2542 +    *cp = '\0';
2543 +
2544    /* Open executable file. */
2545 -  file = filesys_open (file_name);
2546 +  t->bin_file = file = file_open (filesys_open (file_name));
2547    if (file == NULL) 
2548      {
2549        printf ("load: %s: open failed\n", file_name);
2550        goto done; 
2551      }
2552 +  file_deny_write (file);
2553  
2554    /* Read and verify executable header. */
2555    if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr
2556 @@ -301,7 +426,7 @@ load (const char *file_name, void (**eip
2557      }
2558  
2559    /* Set up stack. */
2560 -  if (!setup_stack (esp))
2561 +  if (!setup_stack (cmd_line, esp))
2562      goto done;
2563  
2564    /* Start address. */
2565 @@ -311,14 +436,11 @@ load (const char *file_name, void (**eip
2566  
2567   done:
2568    /* We arrive here whether the load is successful or not. */
2569 -  file_close (file);
2570    return success;
2571  }
2572  \f
2573  /* load() helpers. */
2574  
2575 -static bool install_page (void *upage, void *kpage, bool writable);
2576 -
2577  /* Checks whether PHDR describes a valid, loadable segment in
2578     FILE and returns true if so, false otherwise. */
2579  static bool
2580 @@ -386,79 +508,127 @@ load_segment (struct file *file, off_t o
2581    ASSERT (pg_ofs (upage) == 0);
2582    ASSERT (ofs % PGSIZE == 0);
2583  
2584 -  file_seek (file, ofs);
2585    while (read_bytes > 0 || zero_bytes > 0) 
2586      {
2587 -      /* Calculate how to fill this page.
2588 -         We will read PAGE_READ_BYTES bytes from FILE
2589 -         and zero the final PAGE_ZERO_BYTES bytes. */
2590        size_t page_read_bytes = read_bytes < PGSIZE ? read_bytes : PGSIZE;
2591        size_t page_zero_bytes = PGSIZE - page_read_bytes;
2592 -
2593 -      /* Get a page of memory. */
2594 -      uint8_t *kpage = palloc_get_page (PAL_USER);
2595 -      if (kpage == NULL)
2596 +      struct page *p = page_allocate (upage, !writable);
2597 +      if (p == NULL)
2598          return false;
2599 -
2600 -      /* Load this page. */
2601 -      if (file_read (file, kpage, page_read_bytes) != (int) page_read_bytes)
2602 -        {
2603 -          palloc_free_page (kpage);
2604 -          return false; 
2605 -        }
2606 -      memset (kpage + page_read_bytes, 0, page_zero_bytes);
2607 -
2608 -      /* Add the page to the process's address space. */
2609 -      if (!install_page (upage, kpage, writable)) 
2610 +      if (page_read_bytes > 0) 
2611          {
2612 -          palloc_free_page (kpage);
2613 -          return false; 
2614 +          p->file = file;
2615 +          p->file_offset = ofs;
2616 +          p->file_bytes = page_read_bytes;
2617          }
2618 -
2619 -      /* Advance. */
2620        read_bytes -= page_read_bytes;
2621        zero_bytes -= page_zero_bytes;
2622 +      ofs += page_read_bytes;
2623        upage += PGSIZE;
2624      }
2625    return true;
2626  }
2627  
2628 -/* Create a minimal stack by mapping a zeroed page at the top of
2629 -   user virtual memory. */
2630 +/* Reverse the order of the ARGC pointers to char in ARGV. */
2631 +static void
2632 +reverse (int argc, char **argv) 
2633 +{
2634 +  for (; argc > 1; argc -= 2, argv++) 
2635 +    {
2636 +      char *tmp = argv[0];
2637 +      argv[0] = argv[argc - 1];
2638 +      argv[argc - 1] = tmp;
2639 +    }
2640 +}
2641
2642 +/* Pushes the SIZE bytes in BUF onto the stack in KPAGE, whose
2643 +   page-relative stack pointer is *OFS, and then adjusts *OFS
2644 +   appropriately.  The bytes pushed are rounded to a 32-bit
2645 +   boundary.
2646 +
2647 +   If successful, returns a pointer to the newly pushed object.
2648 +   On failure, returns a null pointer. */
2649 +static void *
2650 +push (uint8_t *kpage, size_t *ofs, const void *buf, size_t size) 
2651 +{
2652 +  size_t padsize = ROUND_UP (size, sizeof (uint32_t));
2653 +  if (*ofs < padsize)
2654 +    return NULL;
2655 +
2656 +  *ofs -= padsize;
2657 +  memcpy (kpage + *ofs + (padsize - size), buf, size);
2658 +  return kpage + *ofs + (padsize - size);
2659 +}
2660 +
2661 +/* Sets up command line arguments in KPAGE, which will be mapped
2662 +   to UPAGE in user space.  The command line arguments are taken
2663 +   from CMD_LINE, separated by spaces.  Sets *ESP to the initial
2664 +   stack pointer for the process. */
2665  static bool
2666 -setup_stack (void **esp) 
2667 +init_cmd_line (uint8_t *kpage, uint8_t *upage, const char *cmd_line,
2668 +               void **esp) 
2669  {
2670 -  uint8_t *kpage;
2671 -  bool success = false;
2672 +  size_t ofs = PGSIZE;
2673 +  char *const null = NULL;
2674 +  char *cmd_line_copy;
2675 +  char *karg, *saveptr;
2676 +  int argc;
2677 +  char **argv;
2678 +
2679 +  /* Push command line string. */
2680 +  cmd_line_copy = push (kpage, &ofs, cmd_line, strlen (cmd_line) + 1);
2681 +  if (cmd_line_copy == NULL)
2682 +    return false;
2683  
2684 -  kpage = palloc_get_page (PAL_USER | PAL_ZERO);
2685 -  if (kpage != NULL) 
2686 +  if (push (kpage, &ofs, &null, sizeof null) == NULL)
2687 +    return false;
2688 +
2689 +  /* Parse command line into arguments
2690 +     and push them in reverse order. */
2691 +  argc = 0;
2692 +  for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL;
2693 +       karg = strtok_r (NULL, " ", &saveptr))
2694      {
2695 -      success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
2696 -      if (success)
2697 -        *esp = PHYS_BASE;
2698 -      else
2699 -        palloc_free_page (kpage);
2700 +      void *uarg = upage + (karg - (char *) kpage);
2701 +      if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL)
2702 +        return false;
2703 +      argc++;
2704      }
2705 -  return success;
2706 +
2707 +  /* Reverse the order of the command line arguments. */
2708 +  argv = (char **) (upage + ofs);
2709 +  reverse (argc, (char **) (kpage + ofs));
2710 +
2711 +  /* Push argv, argc, "return address". */
2712 +  if (push (kpage, &ofs, &argv, sizeof argv) == NULL
2713 +      || push (kpage, &ofs, &argc, sizeof argc) == NULL
2714 +      || push (kpage, &ofs, &null, sizeof null) == NULL)
2715 +    return false;
2716 +
2717 +  /* Set initial stack pointer. */
2718 +  *esp = upage + ofs;
2719 +  return true;
2720  }
2721  
2722 -/* Adds a mapping from user virtual address UPAGE to kernel
2723 -   virtual address KPAGE to the page table.
2724 -   If WRITABLE is true, the user process may modify the page;
2725 -   otherwise, it is read-only.
2726 -   UPAGE must not already be mapped.
2727 -   KPAGE should probably be a page obtained from the user pool
2728 -   with palloc_get_page().
2729 -   Returns true on success, false if UPAGE is already mapped or
2730 -   if memory allocation fails. */
2731 +/* Create a minimal stack for T by mapping a page at the
2732 +   top of user virtual memory.  Fills in the page using CMD_LINE
2733 +   and sets *ESP to the stack pointer. */
2734  static bool
2735 -install_page (void *upage, void *kpage, bool writable)
2736 +setup_stack (const char *cmd_line, void **esp) 
2737  {
2738 -  struct thread *t = thread_current ();
2739 -
2740 -  /* Verify that there's not already a page at that virtual
2741 -     address, then map our page there. */
2742 -  return (pagedir_get_page (t->pagedir, upage) == NULL
2743 -          && pagedir_set_page (t->pagedir, upage, kpage, writable));
2744 +  struct page *page = page_allocate (((uint8_t *) PHYS_BASE) - PGSIZE, false);
2745 +  if (page != NULL) 
2746 +    {
2747 +      page->frame = frame_alloc_and_lock (page);
2748 +      if (page->frame != NULL)
2749 +        {
2750 +          bool ok;
2751 +          page->read_only = false;
2752 +          page->private = false;
2753 +          ok = init_cmd_line (page->frame->base, page->addr, cmd_line, esp);
2754 +          frame_unlock (page->frame);
2755 +          return ok;
2756 +        }
2757 +    }
2758 +  return false;
2759  }
2760 Index: src/userprog/syscall.c
2761 diff -u src/userprog/syscall.c~ src/userprog/syscall.c
2762 --- src/userprog/syscall.c~
2763 +++ src/userprog/syscall.c
2764 @@ -1,20 +1,685 @@
2765  #include "userprog/syscall.h"
2766  #include <stdio.h>
2767 +#include <string.h>
2768  #include <syscall-nr.h>
2769 +#include "userprog/process.h"
2770 +#include "userprog/pagedir.h"
2771 +#include "devices/input.h"
2772 +#include "devices/shutdown.h"
2773 +#include "filesys/directory.h"
2774 +#include "filesys/filesys.h"
2775 +#include "filesys/file.h"
2776 +#include "threads/init.h"
2777  #include "threads/interrupt.h"
2778 +#include "threads/malloc.h"
2779 +#include "threads/palloc.h"
2780  #include "threads/thread.h"
2781 -
2782 +#include "threads/vaddr.h"
2783 +#include "vm/page.h"
2784
2785
2786 +static int sys_halt (void);
2787 +static int sys_exit (int status);
2788 +static int sys_exec (const char *ufile);
2789 +static int sys_wait (tid_t);
2790 +static int sys_create (const char *ufile, unsigned initial_size);
2791 +static int sys_remove (const char *ufile);
2792 +static int sys_open (const char *ufile);
2793 +static int sys_filesize (int handle);
2794 +static int sys_read (int handle, void *udst_, unsigned size);
2795 +static int sys_write (int handle, void *usrc_, unsigned size);
2796 +static int sys_seek (int handle, unsigned position);
2797 +static int sys_tell (int handle);
2798 +static int sys_close (int handle);
2799 +static int sys_mmap (int handle, void *addr);
2800 +static int sys_munmap (int mapping);
2801 +static int sys_chdir (const char *udir);
2802 +static int sys_mkdir (const char *udir);
2803 +static int sys_readdir (int handle, char *name);
2804 +static int sys_isdir (int handle);
2805 +static int sys_inumber (int handle);
2806
2807  static void syscall_handler (struct intr_frame *);
2808 -
2809 +static void copy_in (void *, const void *, size_t);
2810
2811  void
2812  syscall_init (void) 
2813  {
2814    intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall");
2815  }
2816
2817 +/* System call handler. */
2818 +static void
2819 +syscall_handler (struct intr_frame *f) 
2820 +{
2821 +  typedef int syscall_function (int, int, int);
2822 +
2823 +  /* A system call. */
2824 +  struct syscall 
2825 +    {
2826 +      size_t arg_cnt;           /* Number of arguments. */
2827 +      syscall_function *func;   /* Implementation. */
2828 +    };
2829 +
2830 +  /* Table of system calls. */
2831 +  static const struct syscall syscall_table[] =
2832 +    {
2833 +      {0, (syscall_function *) sys_halt},
2834 +      {1, (syscall_function *) sys_exit},
2835 +      {1, (syscall_function *) sys_exec},
2836 +      {1, (syscall_function *) sys_wait},
2837 +      {2, (syscall_function *) sys_create},
2838 +      {1, (syscall_function *) sys_remove},
2839 +      {1, (syscall_function *) sys_open},
2840 +      {1, (syscall_function *) sys_filesize},
2841 +      {3, (syscall_function *) sys_read},
2842 +      {3, (syscall_function *) sys_write},
2843 +      {2, (syscall_function *) sys_seek},
2844 +      {1, (syscall_function *) sys_tell},
2845 +      {1, (syscall_function *) sys_close},
2846 +      {2, (syscall_function *) sys_mmap},
2847 +      {1, (syscall_function *) sys_munmap},
2848 +      {1, (syscall_function *) sys_chdir},
2849 +      {1, (syscall_function *) sys_mkdir},
2850 +      {2, (syscall_function *) sys_readdir},
2851 +      {1, (syscall_function *) sys_isdir},
2852 +      {1, (syscall_function *) sys_inumber},
2853 +    };
2854 +
2855 +  const struct syscall *sc;
2856 +  unsigned call_nr;
2857 +  int args[3];
2858  
2859 +  /* Get the system call. */
2860 +  copy_in (&call_nr, f->esp, sizeof call_nr);
2861 +  if (call_nr >= sizeof syscall_table / sizeof *syscall_table)
2862 +    thread_exit ();
2863 +  sc = syscall_table + call_nr;
2864 +
2865 +  /* Get the system call arguments. */
2866 +  ASSERT (sc->arg_cnt <= sizeof args / sizeof *args);
2867 +  memset (args, 0, sizeof args);
2868 +  copy_in (args, (uint32_t *) f->esp + 1, sizeof *args * sc->arg_cnt);
2869 +
2870 +  /* Execute the system call,
2871 +     and set the return value. */
2872 +  f->eax = sc->func (args[0], args[1], args[2]);
2873 +}
2874
2875 +/* Copies SIZE bytes from user address USRC to kernel address
2876 +   DST.
2877 +   Call thread_exit() if any of the user accesses are invalid. */
2878  static void
2879 -syscall_handler (struct intr_frame *f UNUSED) 
2880 +copy_in (void *dst_, const void *usrc_, size_t size) 
2881  {
2882 -  printf ("system call!\n");
2883 +  uint8_t *dst = dst_;
2884 +  const uint8_t *usrc = usrc_;
2885 +
2886 +  while (size > 0) 
2887 +    {
2888 +      size_t chunk_size = PGSIZE - pg_ofs (usrc);
2889 +      if (chunk_size > size)
2890 +        chunk_size = size;
2891 +      
2892 +      if (!page_lock (usrc, false))
2893 +        thread_exit ();
2894 +      memcpy (dst, usrc, chunk_size);
2895 +      page_unlock (usrc);
2896 +
2897 +      dst += chunk_size;
2898 +      usrc += chunk_size;
2899 +      size -= chunk_size;
2900 +    }
2901 +}
2902
2903 +/* Copies SIZE bytes from kernel address SRC to user address
2904 +   UDST.
2905 +   Call thread_exit() if any of the user accesses are invalid. */
2906 +static void
2907 +copy_out (void *udst_, const void *src_, size_t size) 
2908 +{
2909 +  uint8_t *udst = udst_;
2910 +  const uint8_t *src = src_;
2911 +
2912 +  while (size > 0) 
2913 +    {
2914 +      size_t chunk_size = PGSIZE - pg_ofs (udst);
2915 +      if (chunk_size > size)
2916 +        chunk_size = size;
2917 +      
2918 +      if (!page_lock (udst, false))
2919 +        thread_exit ();
2920 +      memcpy (udst, src, chunk_size);
2921 +      page_unlock (udst);
2922 +
2923 +      udst += chunk_size;
2924 +      src += chunk_size;
2925 +      size -= chunk_size;
2926 +    }
2927 +}
2928
2929 +/* Creates a copy of user string US in kernel memory
2930 +   and returns it as a page that must be freed with
2931 +   palloc_free_page().
2932 +   Truncates the string at PGSIZE bytes in size.
2933 +   Call thread_exit() if any of the user accesses are invalid. */
2934 +static char *
2935 +copy_in_string (const char *us) 
2936 +{
2937 +  char *ks;
2938 +  char *upage;
2939 +  size_t length;
2940
2941 +  ks = palloc_get_page (0);
2942 +  if (ks == NULL) 
2943 +    thread_exit ();
2944 +
2945 +  length = 0;
2946 +  for (;;) 
2947 +    {
2948 +      upage = pg_round_down (us);
2949 +      if (!page_lock (upage, false))
2950 +        goto lock_error;
2951 +
2952 +      for (; us < upage + PGSIZE; us++) 
2953 +        {
2954 +          ks[length++] = *us;
2955 +          if (*us == '\0') 
2956 +            {
2957 +              page_unlock (upage);
2958 +              return ks; 
2959 +            }
2960 +          else if (length >= PGSIZE) 
2961 +            goto too_long_error;
2962 +        }
2963 +
2964 +      page_unlock (upage);
2965 +    }
2966 +
2967 + too_long_error:
2968 +  page_unlock (upage);
2969 + lock_error:
2970 +  palloc_free_page (ks);
2971    thread_exit ();
2972  }
2973
2974 +/* Halt system call. */
2975 +static int
2976 +sys_halt (void)
2977 +{
2978 +  shutdown_power_off ();
2979 +}
2980
2981 +/* Exit system call. */
2982 +static int
2983 +sys_exit (int exit_code) 
2984 +{
2985 +  thread_current ()->exit_code = exit_code;
2986 +  thread_exit ();
2987 +  NOT_REACHED ();
2988 +}
2989
2990 +/* Exec system call. */
2991 +static int
2992 +sys_exec (const char *ufile) 
2993 +{
2994 +  tid_t tid;
2995 +  char *kfile = copy_in_string (ufile);
2996
2997 +  tid = process_execute (kfile);
2998
2999 +  palloc_free_page (kfile);
3000
3001 +  return tid;
3002 +}
3003
3004 +/* Wait system call. */
3005 +static int
3006 +sys_wait (tid_t child) 
3007 +{
3008 +  return process_wait (child);
3009 +}
3010
3011 +/* Create system call. */
3012 +static int
3013 +sys_create (const char *ufile, unsigned initial_size) 
3014 +{
3015 +  char *kfile = copy_in_string (ufile);
3016 +  bool ok = filesys_create (kfile, initial_size, FILE_INODE);
3017 +  palloc_free_page (kfile);
3018
3019 +  return ok;
3020 +}
3021
3022 +/* Remove system call. */
3023 +static int
3024 +sys_remove (const char *ufile) 
3025 +{
3026 +  char *kfile = copy_in_string (ufile);
3027 +  bool ok = filesys_remove (kfile);
3028 +  palloc_free_page (kfile);
3029
3030 +  return ok;
3031 +}
3032 +\f
3033 +/* A file descriptor, for binding a file handle to a file. */
3034 +struct file_descriptor
3035 +  {
3036 +    struct list_elem elem;      /* List element. */
3037 +    struct file *file;          /* File. */
3038 +    struct dir *dir;            /* Directory. */
3039 +    int handle;                 /* File handle. */
3040 +  };
3041
3042 +/* Open system call. */
3043 +static int
3044 +sys_open (const char *ufile) 
3045 +{
3046 +  char *kfile = copy_in_string (ufile);
3047 +  struct file_descriptor *fd;
3048 +  int handle = -1;
3049
3050 +  fd = calloc (1, sizeof *fd);
3051 +  if (fd != NULL)
3052 +    {
3053 +      struct inode *inode = filesys_open (kfile);
3054 +      if (inode != NULL)
3055 +        {
3056 +          if (inode_get_type (inode) == FILE_INODE)
3057 +            fd->file = file_open (inode);
3058 +          else
3059 +            fd->dir = dir_open (inode);
3060 +          if (fd->file != NULL || fd->dir != NULL)
3061 +            {
3062 +              struct thread *cur = thread_current ();
3063 +              handle = fd->handle = cur->next_handle++;
3064 +              list_push_front (&cur->fds, &fd->elem);
3065 +            }
3066 +          else 
3067 +            {
3068 +              free (fd);
3069 +              inode_close (inode);
3070 +            }
3071 +        }
3072 +    }
3073 +  
3074 +  palloc_free_page (kfile);
3075 +  return handle;
3076 +}
3077
3078 +/* Returns the file descriptor associated with the given handle.
3079 +   Terminates the process if HANDLE is not associated with an
3080 +   open file. */
3081 +static struct file_descriptor *
3082 +lookup_fd (int handle) 
3083 +{
3084 +  struct thread *cur = thread_current ();
3085 +  struct list_elem *e;
3086 +   
3087 +  for (e = list_begin (&cur->fds); e != list_end (&cur->fds);
3088 +       e = list_next (e))
3089 +    {
3090 +      struct file_descriptor *fd;
3091 +      fd = list_entry (e, struct file_descriptor, elem);
3092 +      if (fd->handle == handle)
3093 +        return fd;
3094 +    }
3095
3096 +  thread_exit ();
3097 +}
3098
3099 +/* Returns the file descriptor associated with the given handle.
3100 +   Terminates the process if HANDLE is not associated with an
3101 +   open ordinary file. */
3102 +static struct file_descriptor *
3103 +lookup_file_fd (int handle) 
3104 +{
3105 +  struct file_descriptor *fd = lookup_fd (handle);
3106 +  if (fd->file == NULL)
3107 +    thread_exit ();
3108 +  return fd;
3109 +}
3110
3111 +/* Returns the file descriptor associated with the given handle.
3112 +   Terminates the process if HANDLE is not associated with an
3113 +   open directory. */
3114 +static struct file_descriptor *
3115 +lookup_dir_fd (int handle) 
3116 +{
3117 +  struct file_descriptor *fd = lookup_fd (handle);
3118 +  if (fd->dir == NULL)
3119 +    thread_exit ();
3120 +  return fd;
3121 +}
3122 +
3123 +/* Filesize system call. */
3124 +static int
3125 +sys_filesize (int handle) 
3126 +{
3127 +  struct file_descriptor *fd = lookup_file_fd (handle);
3128 +  int size;
3129
3130 +  size = file_length (fd->file);
3131
3132 +  return size;
3133 +}
3134
3135 +/* Read system call. */
3136 +static int
3137 +sys_read (int handle, void *udst_, unsigned size) 
3138 +{
3139 +  uint8_t *udst = udst_;
3140 +  struct file_descriptor *fd;
3141 +  int bytes_read = 0;
3142 +
3143 +  /* Look up file descriptor. */
3144 +  if (handle != STDIN_FILENO)
3145 +    fd = lookup_file_fd (handle);
3146 +
3147 +  while (size > 0) 
3148 +    {
3149 +      /* How much to read into this page? */
3150 +      size_t page_left = PGSIZE - pg_ofs (udst);
3151 +      size_t read_amt = size < page_left ? size : page_left;
3152 +      off_t retval;
3153 +
3154 +      /* Check that touching this page is okay. */
3155 +      if (!page_lock (udst, true)) 
3156 +        thread_exit ();
3157 +
3158 +      /* Read from file into page. */
3159 +      if (handle != STDIN_FILENO) 
3160 +        {
3161 +          retval = file_read (fd->file, udst, read_amt);
3162 +          if (retval < 0)
3163 +            {
3164 +              if (bytes_read == 0)
3165 +                bytes_read = -1; 
3166 +              break;
3167 +            }
3168 +          bytes_read += retval; 
3169 +        }
3170 +      else 
3171 +        {
3172 +          size_t i;
3173 +          
3174 +          for (i = 0; i < read_amt; i++) 
3175 +            udst[i] = input_getc ();
3176 +          bytes_read = read_amt;
3177 +        }
3178 +
3179 +      /* Release page. */
3180 +      page_unlock (udst);
3181 +
3182 +      /* If it was a short read we're done. */
3183 +      if (retval != (off_t) read_amt)
3184 +        break;
3185 +
3186 +      /* Advance. */
3187 +      udst += retval;
3188 +      size -= retval;
3189 +    }
3190 +   
3191 +  return bytes_read;
3192 +}
3193
3194 +/* Write system call. */
3195 +static int
3196 +sys_write (int handle, void *usrc_, unsigned size) 
3197 +{
3198 +  uint8_t *usrc = usrc_;
3199 +  struct file_descriptor *fd = NULL;
3200 +  int bytes_written = 0;
3201 +
3202 +  /* Lookup up file descriptor. */
3203 +  if (handle != STDOUT_FILENO)
3204 +    fd = lookup_file_fd (handle);
3205 +
3206 +  while (size > 0) 
3207 +    {
3208 +      /* How much bytes to write to this page? */
3209 +      size_t page_left = PGSIZE - pg_ofs (usrc);
3210 +      size_t write_amt = size < page_left ? size : page_left;
3211 +      off_t retval;
3212 +
3213 +      /* Check that we can touch this user page. */
3214 +      if (!page_lock (usrc, false)) 
3215 +        thread_exit ();
3216 +
3217 +      /* Do the write. */
3218 +      if (handle == STDOUT_FILENO)
3219 +        {
3220 +          putbuf ((char *) usrc, write_amt);
3221 +          retval = write_amt;
3222 +        }
3223 +      else
3224 +        retval = file_write (fd->file, usrc, write_amt);
3225 +
3226 +      /* Release user page. */
3227 +      page_unlock (usrc);
3228 +
3229 +      /* Handle return value. */
3230 +      if (retval < 0) 
3231 +        {
3232 +          if (bytes_written == 0)
3233 +            bytes_written = -1;
3234 +          break;
3235 +        }
3236 +      bytes_written += retval;
3237 +
3238 +      /* If it was a short write we're done. */
3239 +      if (retval != (off_t) write_amt)
3240 +        break;
3241 +
3242 +      /* Advance. */
3243 +      usrc += retval;
3244 +      size -= retval;
3245 +    }
3246
3247 +  return bytes_written;
3248 +}
3249
3250 +/* Seek system call. */
3251 +static int
3252 +sys_seek (int handle, unsigned position) 
3253 +{
3254 +  if ((off_t) position >= 0)
3255 +    file_seek (lookup_file_fd (handle)->file, position);
3256 +  return 0;
3257 +}
3258
3259 +/* Tell system call. */
3260 +static int
3261 +sys_tell (int handle) 
3262 +{
3263 +  return file_tell (lookup_file_fd (handle)->file);
3264 +}
3265
3266 +/* Close system call. */
3267 +static int
3268 +sys_close (int handle) 
3269 +{
3270 +  struct file_descriptor *fd = lookup_fd (handle);
3271 +  file_close (fd->file);
3272 +  dir_close (fd->dir);
3273 +  list_remove (&fd->elem);
3274 +  free (fd);
3275 +  return 0;
3276 +}
3277 +\f
3278 +/* Binds a mapping id to a region of memory and a file. */
3279 +struct mapping
3280 +  {
3281 +    struct list_elem elem;      /* List element. */
3282 +    int handle;                 /* Mapping id. */
3283 +    struct file *file;          /* File. */
3284 +    uint8_t *base;              /* Start of memory mapping. */
3285 +    size_t page_cnt;            /* Number of pages mapped. */
3286 +  };
3287 +
3288 +/* Returns the file descriptor associated with the given handle.
3289 +   Terminates the process if HANDLE is not associated with a
3290 +   memory mapping. */
3291 +static struct mapping *
3292 +lookup_mapping (int handle) 
3293 +{
3294 +  struct thread *cur = thread_current ();
3295 +  struct list_elem *e;
3296 +   
3297 +  for (e = list_begin (&cur->mappings); e != list_end (&cur->mappings);
3298 +       e = list_next (e))
3299 +    {
3300 +      struct mapping *m = list_entry (e, struct mapping, elem);
3301 +      if (m->handle == handle)
3302 +        return m;
3303 +    }
3304
3305 +  thread_exit ();
3306 +}
3307 +
3308 +/* Remove mapping M from the virtual address space,
3309 +   writing back any pages that have changed. */
3310 +static void
3311 +unmap (struct mapping *m) 
3312 +{
3313 +  list_remove (&m->elem);
3314 +  while (m->page_cnt-- > 0) 
3315 +    {
3316 +      page_deallocate (m->base);
3317 +      m->base += PGSIZE;
3318 +    }
3319 +  file_close (m->file);
3320 +  free (m);
3321 +}
3322
3323 +/* Mmap system call. */
3324 +static int
3325 +sys_mmap (int handle, void *addr)
3326 +{
3327 +  struct file_descriptor *fd = lookup_file_fd (handle);
3328 +  struct mapping *m = malloc (sizeof *m);
3329 +  size_t offset;
3330 +  off_t length;
3331 +
3332 +  if (m == NULL || addr == NULL || pg_ofs (addr) != 0)
3333 +    return -1;
3334 +
3335 +  m->handle = thread_current ()->next_handle++;
3336 +  m->file = file_reopen (fd->file);
3337 +  if (m->file == NULL) 
3338 +    {
3339 +      free (m);
3340 +      return -1;
3341 +    }
3342 +  m->base = addr;
3343 +  m->page_cnt = 0;
3344 +  list_push_front (&thread_current ()->mappings, &m->elem);
3345 +
3346 +  offset = 0;
3347 +  length = file_length (m->file);
3348 +  while (length > 0)
3349 +    {
3350 +      struct page *p = page_allocate ((uint8_t *) addr + offset, false);
3351 +      if (p == NULL)
3352 +        {
3353 +          unmap (m);
3354 +          return -1;
3355 +        }
3356 +      p->private = false;
3357 +      p->file = m->file;
3358 +      p->file_offset = offset;
3359 +      p->file_bytes = length >= PGSIZE ? PGSIZE : length;
3360 +      offset += p->file_bytes;
3361 +      length -= p->file_bytes;
3362 +      m->page_cnt++;
3363 +    }
3364 +  
3365 +  return m->handle;
3366 +}
3367 +
3368 +/* Munmap system call. */
3369 +static int
3370 +sys_munmap (int mapping) 
3371 +{
3372 +  unmap (lookup_mapping (mapping));
3373 +  return 0;
3374 +}
3375 +
3376 +/* Chdir system call. */
3377 +static int
3378 +sys_chdir (const char *udir) 
3379 +{
3380 +  char *kdir = copy_in_string (udir);
3381 +  bool ok = filesys_chdir (kdir);
3382 +  palloc_free_page (kdir);
3383 +  return ok;
3384 +}
3385 +
3386 +/* Mkdir system call. */
3387 +static int
3388 +sys_mkdir (const char *udir)
3389 +{
3390 +  char *kdir = copy_in_string (udir);
3391 +  bool ok = filesys_create (kdir, 0, DIR_INODE);
3392 +  palloc_free_page (kdir);
3393
3394 +  return ok;
3395 +}
3396 +
3397 +/* Readdir system call. */
3398 +static int
3399 +sys_readdir (int handle, char *uname)
3400 +{
3401 +  struct file_descriptor *fd = lookup_dir_fd (handle);
3402 +  char name[NAME_MAX + 1];
3403 +  bool ok = dir_readdir (fd->dir, name);
3404 +  if (ok)
3405 +    copy_out (uname, name, strlen (name) + 1);
3406 +  return ok;
3407 +}
3408 +
3409 +/* Isdir system call. */
3410 +static int
3411 +sys_isdir (int handle)
3412 +{
3413 +  struct file_descriptor *fd = lookup_fd (handle);
3414 +  return fd->dir != NULL;
3415 +}
3416 +
3417 +/* Inumber system call. */
3418 +static int
3419 +sys_inumber (int handle)
3420 +{
3421 +  struct file_descriptor *fd = lookup_fd (handle);
3422 +  struct inode *inode = (fd->file 
3423 +                         ? file_get_inode (fd->file)
3424 +                         : dir_get_inode (fd->dir));
3425 +  return inode_get_inumber (inode);
3426 +}
3427 +\f 
3428 +/* On thread exit, close all open files and unmap all mappings. */
3429 +void
3430 +syscall_exit (void) 
3431 +{
3432 +  struct thread *cur = thread_current ();
3433 +  struct list_elem *e, *next;
3434 +   
3435 +  for (e = list_begin (&cur->fds); e != list_end (&cur->fds); e = next)
3436 +    {
3437 +      struct file_descriptor *fd = list_entry (e, struct file_descriptor, elem);
3438 +      next = list_next (e);
3439 +      file_close (fd->file);
3440 +      dir_close (fd->dir);
3441 +      free (fd);
3442 +    }
3443 +   
3444 +  for (e = list_begin (&cur->mappings); e != list_end (&cur->mappings);
3445 +       e = next)
3446 +    {
3447 +      struct mapping *m = list_entry (e, struct mapping, elem);
3448 +      next = list_next (e);
3449 +      unmap (m);
3450 +    }
3451 +
3452 +  dir_close (cur->wd);
3453 +}
3454 Index: src/userprog/syscall.h
3455 diff -u src/userprog/syscall.h~ src/userprog/syscall.h
3456 --- src/userprog/syscall.h~
3457 +++ src/userprog/syscall.h
3458 @@ -2,5 +2,6 @@
3459  #define USERPROG_SYSCALL_H
3460  
3461  void syscall_init (void);
3462 +void syscall_exit (void);
3463  
3464  #endif /* userprog/syscall.h */
3465 Index: src/vm/frame.c
3466 diff -u src/vm/frame.c~ src/vm/frame.c
3467 --- src/vm/frame.c~
3468 +++ src/vm/frame.c
3469 @@ -0,0 +1,161 @@
3470 +#include "vm/frame.h"
3471 +#include <stdio.h>
3472 +#include "vm/page.h"
3473 +#include "devices/timer.h"
3474 +#include "threads/init.h"
3475 +#include "threads/malloc.h"
3476 +#include "threads/palloc.h"
3477 +#include "threads/synch.h"
3478 +#include "threads/vaddr.h"
3479 +
3480 +static struct frame *frames;
3481 +static size_t frame_cnt;
3482 +
3483 +static struct lock scan_lock;
3484 +static size_t hand;
3485 +
3486 +/* Initialize the frame manager. */
3487 +void
3488 +frame_init (void) 
3489 +{
3490 +  void *base;
3491 +
3492 +  lock_init (&scan_lock);
3493 +  
3494 +  frames = malloc (sizeof *frames * ram_pages);
3495 +  if (frames == NULL)
3496 +    PANIC ("out of memory allocating page frames");
3497 +
3498 +  while ((base = palloc_get_page (PAL_USER)) != NULL) 
3499 +    {
3500 +      struct frame *f = &frames[frame_cnt++];
3501 +      lock_init (&f->lock);
3502 +      f->base = base;
3503 +      f->page = NULL;
3504 +    }
3505 +}
3506 +
3507 +/* Tries to allocate and lock a frame for PAGE.
3508 +   Returns the frame if successful, false on failure. */
3509 +static struct frame *
3510 +try_frame_alloc_and_lock (struct page *page) 
3511 +{
3512 +  size_t i;
3513 +
3514 +  lock_acquire (&scan_lock);
3515 +
3516 +  /* Find a free frame. */
3517 +  for (i = 0; i < frame_cnt; i++)
3518 +    {
3519 +      struct frame *f = &frames[i];
3520 +      if (!lock_try_acquire (&f->lock))
3521 +        continue;
3522 +      if (f->page == NULL) 
3523 +        {
3524 +          f->page = page;
3525 +          lock_release (&scan_lock);
3526 +          return f;
3527 +        } 
3528 +      lock_release (&f->lock);
3529 +    }
3530 +
3531 +  /* No free frame.  Find a frame to evict. */
3532 +  for (i = 0; i < frame_cnt * 2; i++) 
3533 +    {
3534 +      /* Get a frame. */
3535 +      struct frame *f = &frames[hand];
3536 +      if (++hand >= frame_cnt)
3537 +        hand = 0;
3538 +
3539 +      if (!lock_try_acquire (&f->lock))
3540 +        continue;
3541 +
3542 +      if (f->page == NULL) 
3543 +        {
3544 +          f->page = page;
3545 +          lock_release (&scan_lock);
3546 +          return f;
3547 +        } 
3548 +
3549 +      if (page_accessed_recently (f->page)) 
3550 +        {
3551 +          lock_release (&f->lock);
3552 +          continue;
3553 +        }
3554 +          
3555 +      lock_release (&scan_lock);
3556 +      
3557 +      /* Evict this frame. */
3558 +      if (!page_out (f->page))
3559 +        {
3560 +          lock_release (&f->lock);
3561 +          return NULL;
3562 +        }
3563 +
3564 +      f->page = page;
3565 +      return f;
3566 +    }
3567 +
3568 +  lock_release (&scan_lock);
3569 +  return NULL;
3570 +}
3571 +
3572 +/* Tries really hard to allocate and lock a frame for PAGE.
3573 +   Returns the frame if successful, false on failure. */
3574 +struct frame *
3575 +frame_alloc_and_lock (struct page *page) 
3576 +{
3577 +  size_t try;
3578 +
3579 +  for (try = 0; try < 3; try++) 
3580 +    {
3581 +      struct frame *f = try_frame_alloc_and_lock (page);
3582 +      if (f != NULL) 
3583 +        {
3584 +          ASSERT (lock_held_by_current_thread (&f->lock));
3585 +          return f; 
3586 +        }
3587 +      timer_msleep (1000);
3588 +    }
3589 +
3590 +  return NULL;
3591 +}
3592 +
3593 +/* Locks P's frame into memory, if it has one.
3594 +   Upon return, p->frame will not change until P is unlocked. */
3595 +void
3596 +frame_lock (struct page *p) 
3597 +{
3598 +  /* A frame can be asynchronously removed, but never inserted. */
3599 +  struct frame *f = p->frame;
3600 +  if (f != NULL) 
3601 +    {
3602 +      lock_acquire (&f->lock);
3603 +      if (f != p->frame)
3604 +        {
3605 +          lock_release (&f->lock);
3606 +          ASSERT (p->frame == NULL); 
3607 +        } 
3608 +    }
3609 +}
3610 +
3611 +/* Releases frame F for use by another page.
3612 +   F must be locked for use by the current process.
3613 +   Any data in F is lost. */
3614 +void
3615 +frame_free (struct frame *f)
3616 +{
3617 +  ASSERT (lock_held_by_current_thread (&f->lock));
3618 +          
3619 +  f->page = NULL;
3620 +  lock_release (&f->lock);
3621 +}
3622 +
3623 +/* Unlocks frame F, allowing it to be evicted.
3624 +   F must be locked for use by the current process. */
3625 +void
3626 +frame_unlock (struct frame *f) 
3627 +{
3628 +  ASSERT (lock_held_by_current_thread (&f->lock));
3629 +  lock_release (&f->lock);
3630 +}
3631 Index: src/vm/frame.h
3632 diff -u src/vm/frame.h~ src/vm/frame.h
3633 --- src/vm/frame.h~
3634 +++ src/vm/frame.h
3635 @@ -0,0 +1,23 @@
3636 +#ifndef VM_FRAME_H
3637 +#define VM_FRAME_H
3638 +
3639 +#include <stdbool.h>
3640 +#include "threads/synch.h"
3641 +
3642 +/* A physical frame. */
3643 +struct frame 
3644 +  {
3645 +    struct lock lock;           /* Prevent simultaneous access. */
3646 +    void *base;                 /* Kernel virtual base address. */
3647 +    struct page *page;          /* Mapped process page, if any. */
3648 +  };
3649 +
3650 +void frame_init (void);
3651 +
3652 +struct frame *frame_alloc_and_lock (struct page *);
3653 +void frame_lock (struct page *);
3654 +
3655 +void frame_free (struct frame *);
3656 +void frame_unlock (struct frame *);
3657 +
3658 +#endif /* vm/frame.h */
3659 Index: src/vm/page.c
3660 diff -u src/vm/page.c~ src/vm/page.c
3661 --- src/vm/page.c~
3662 +++ src/vm/page.c
3663 @@ -0,0 +1,294 @@
3664 +#include "vm/page.h"
3665 +#include <stdio.h>
3666 +#include <string.h>
3667 +#include "vm/frame.h"
3668 +#include "vm/swap.h"
3669 +#include "filesys/file.h"
3670 +#include "threads/malloc.h"
3671 +#include "threads/thread.h"
3672 +#include "userprog/pagedir.h"
3673 +#include "threads/vaddr.h"
3674 +
3675 +/* Maximum size of process stack, in bytes. */
3676 +#define STACK_MAX (1024 * 1024)
3677 +
3678 +/* Destroys a page, which must be in the current process's
3679 +   page table.  Used as a callback for hash_destroy(). */
3680 +static void
3681 +destroy_page (struct hash_elem *p_, void *aux UNUSED)
3682 +{
3683 +  struct page *p = hash_entry (p_, struct page, hash_elem);
3684 +  frame_lock (p);
3685 +  if (p->frame)
3686 +    frame_free (p->frame);
3687 +  free (p);
3688 +}
3689 +
3690 +
3691 +/* Destroys the current process's page table. */
3692 +void
3693 +page_exit (void) 
3694 +{
3695 +  struct hash *h = thread_current ()->pages;
3696 +  if (h != NULL)
3697 +    hash_destroy (h, destroy_page);
3698 +}
3699 +
3700 +/* Returns the page containing the given virtual ADDRESS,
3701 +   or a null pointer if no such page exists.
3702 +   Allocates stack pages as necessary. */
3703 +static struct page *
3704 +page_for_addr (const void *address) 
3705 +{
3706 +  if (address < PHYS_BASE) 
3707 +    {
3708 +      struct page p;
3709 +      struct hash_elem *e;
3710 +
3711 +      /* Find existing page. */
3712 +      p.addr = (void *) pg_round_down (address);
3713 +      e = hash_find (thread_current ()->pages, &p.hash_elem);
3714 +      if (e != NULL)
3715 +        return hash_entry (e, struct page, hash_elem);
3716 +
3717 +      /* No page.  Expand stack? */
3718 +      if (address >= PHYS_BASE - STACK_MAX
3719 +          && address >= thread_current ()->user_esp - 32)
3720 +        return page_allocate ((void *) address, false);
3721 +    }
3722 +  return NULL;
3723 +}
3724 +
3725 +/* Locks a frame for page P and pages it in.
3726 +   Returns true if successful, false on failure. */
3727 +static bool
3728 +do_page_in (struct page *p)
3729 +{
3730 +  /* Get a frame for the page. */
3731 +  p->frame = frame_alloc_and_lock (p);
3732 +  if (p->frame == NULL)
3733 +    return false;
3734 +
3735 +  /* Copy data into the frame. */
3736 +  if (p->sector != (disk_sector_t) -1) 
3737 +    {
3738 +      /* Get data from swap. */
3739 +      swap_in (p); 
3740 +    }
3741 +  else if (p->file != NULL) 
3742 +    {
3743 +      /* Get data from file. */
3744 +      off_t read_bytes = file_read_at (p->file, p->frame->base,
3745 +                                        p->file_bytes, p->file_offset);
3746 +      off_t zero_bytes = PGSIZE - read_bytes;
3747 +      memset (p->frame->base + read_bytes, 0, zero_bytes);
3748 +      if (read_bytes != p->file_bytes)
3749 +        printf ("bytes read (%"PROTd") != bytes requested (%"PROTd")\n",
3750 +                read_bytes, p->file_bytes);
3751 +    }
3752 +  else 
3753 +    {
3754 +      /* Provide all-zero page. */
3755 +      memset (p->frame->base, 0, PGSIZE);
3756 +    }
3757 +
3758 +  return true;
3759 +}
3760 +
3761 +/* Faults in the page containing FAULT_ADDR.
3762 +   Returns true if successful, false on failure. */
3763 +bool
3764 +page_in (void *fault_addr) 
3765 +{
3766 +  struct page *p;
3767 +  bool success;
3768 +
3769 +  /* Can't handle page faults without a hash table. */
3770 +  if (thread_current ()->pages == NULL) 
3771 +    return false;
3772 +
3773 +  p = page_for_addr (fault_addr);
3774 +  if (p == NULL) 
3775 +    return false; 
3776 +
3777 +  frame_lock (p);
3778 +  if (p->frame == NULL)
3779 +    {
3780 +      if (!do_page_in (p))
3781 +        return false;
3782 +    }
3783 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
3784 +    
3785 +  /* Install frame into page table. */
3786 +  success = pagedir_set_page (thread_current ()->pagedir, p->addr,
3787 +                              p->frame->base, !p->read_only);
3788 +
3789 +  /* Release frame. */
3790 +  frame_unlock (p->frame);
3791 +
3792 +  return success;
3793 +}
3794 +
3795 +/* Evicts page P.
3796 +   P must have a locked frame.
3797 +   Return true if successful, false on failure. */
3798 +bool
3799 +page_out (struct page *p) 
3800 +{
3801 +  bool dirty;
3802 +  bool ok;
3803 +
3804 +  ASSERT (p->frame != NULL);
3805 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
3806 +
3807 +  /* Mark page not present in page table, forcing accesses by the
3808 +     process to fault.  This must happen before checking the
3809 +     dirty bit, to prevent a race with the process dirtying the
3810 +     page. */
3811 +  pagedir_clear_page (p->thread->pagedir, p->addr);
3812 +
3813 +  /* Has the frame been modified? */
3814 +  dirty = pagedir_is_dirty (p->thread->pagedir, p->addr);
3815 +
3816 +  /* Write frame contents to disk if necessary. */
3817 +  if (p->file != NULL) 
3818 +    {
3819 +      if (dirty) 
3820 +        {
3821 +          if (p->private)
3822 +            ok = swap_out (p);
3823 +          else 
3824 +            ok = file_write_at (p->file, p->frame->base, p->file_bytes,
3825 +                                p->file_offset) == p->file_bytes;
3826 +        }
3827 +      else
3828 +        ok = true;
3829 +    }
3830 +  else
3831 +    ok = swap_out (p);
3832 +  if (ok) 
3833 +    {
3834 +      //memset (p->frame->base, 0xcc, PGSIZE);
3835 +      p->frame = NULL; 
3836 +    }
3837 +  return ok;
3838 +}
3839 +
3840 +/* Returns true if page P's data has been accessed recently,
3841 +   false otherwise.
3842 +   P must have a frame locked into memory. */
3843 +bool
3844 +page_accessed_recently (struct page *p) 
3845 +{
3846 +  bool was_accessed;
3847 +
3848 +  ASSERT (p->frame != NULL);
3849 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
3850 +
3851 +  was_accessed = pagedir_is_accessed (p->thread->pagedir, p->addr);
3852 +  if (was_accessed)
3853 +    pagedir_set_accessed (p->thread->pagedir, p->addr, false);
3854 +  return was_accessed;
3855 +}
3856 +
3857 +/* Adds a mapping for user virtual address VADDR to the page hash
3858 +   table.  Fails if VADDR is already mapped or if memory
3859 +   allocation fails. */
3860 +struct page *
3861 +page_allocate (void *vaddr, bool read_only)
3862 +{
3863 +  struct thread *t = thread_current ();
3864 +  struct page *p = malloc (sizeof *p);
3865 +  if (p != NULL) 
3866 +    {
3867 +      p->addr = pg_round_down (vaddr);
3868 +
3869 +      p->read_only = read_only;
3870 +      p->private = !read_only;
3871 +
3872 +      p->frame = NULL;
3873 +
3874 +      p->sector = (disk_sector_t) -1;
3875 +
3876 +      p->file = NULL;
3877 +      p->file_offset = 0;
3878 +      p->file_bytes = 0;
3879 +
3880 +      p->thread = thread_current ();
3881 +
3882 +      if (hash_insert (t->pages, &p->hash_elem) != NULL) 
3883 +        {
3884 +          /* Already mapped. */
3885 +          free (p);
3886 +          p = NULL;
3887 +        }
3888 +    }
3889 +  return p;
3890 +}
3891 +
3892 +/* Evicts the page containing address VADDR
3893 +   and removes it from the page table. */
3894 +void
3895 +page_deallocate (void *vaddr) 
3896 +{
3897 +  struct page *p = page_for_addr (vaddr);
3898 +  ASSERT (p != NULL);
3899 +  frame_lock (p);
3900 +  if (p->frame)
3901 +    {
3902 +      struct frame *f = p->frame;
3903 +      if (p->file && !p->private) 
3904 +        page_out (p); 
3905 +      frame_free (f);
3906 +    }
3907 +  hash_delete (thread_current ()->pages, &p->hash_elem);
3908 +  free (p);
3909 +}
3910 +
3911 +/* Returns a hash value for the page that E refers to. */
3912 +unsigned
3913 +page_hash (const struct hash_elem *e, void *aux UNUSED) 
3914 +{
3915 +  const struct page *p = hash_entry (e, struct page, hash_elem);
3916 +  return ((uintptr_t) p->addr) >> PGBITS;
3917 +}
3918 +
3919 +/* Returns true if page A precedes page B. */
3920 +bool
3921 +page_less (const struct hash_elem *a_, const struct hash_elem *b_,
3922 +           void *aux UNUSED) 
3923 +{
3924 +  const struct page *a = hash_entry (a_, struct page, hash_elem);
3925 +  const struct page *b = hash_entry (b_, struct page, hash_elem);
3926 +  
3927 +  return a->addr < b->addr;
3928 +}
3929 +
3930 +/* Tries to lock the page containing ADDR into physical memory.
3931 +   If WILL_WRITE is true, the page must be writeable;
3932 +   otherwise it may be read-only.
3933 +   Returns true if successful, false on failure. */
3934 +bool
3935 +page_lock (const void *addr, bool will_write) 
3936 +{
3937 +  struct page *p = page_for_addr (addr);
3938 +  if (p == NULL || (p->read_only && will_write))
3939 +    return false;
3940 +  
3941 +  frame_lock (p);
3942 +  if (p->frame == NULL)
3943 +    return (do_page_in (p)
3944 +            && pagedir_set_page (thread_current ()->pagedir, p->addr,
3945 +                                 p->frame->base, !p->read_only)); 
3946 +  else
3947 +    return true;
3948 +}
3949 +
3950 +/* Unlocks a page locked with page_lock(). */
3951 +void
3952 +page_unlock (const void *addr) 
3953 +{
3954 +  struct page *p = page_for_addr (addr);
3955 +  ASSERT (p != NULL);
3956 +  frame_unlock (p->frame);
3957 +}
3958 Index: src/vm/page.h
3959 diff -u src/vm/page.h~ src/vm/page.h
3960 --- src/vm/page.h~
3961 +++ src/vm/page.h
3962 @@ -0,0 +1,50 @@
3963 +#ifndef VM_PAGE_H
3964 +#define VM_PAGE_H
3965 +
3966 +#include <hash.h>
3967 +#include "devices/disk.h"
3968 +#include "filesys/off_t.h"
3969 +#include "threads/synch.h"
3970 +
3971 +/* Virtual page. */
3972 +struct page 
3973 +  {
3974 +    /* Immutable members. */
3975 +    void *addr;                 /* User virtual address. */
3976 +    bool read_only;             /* Read-only page? */
3977 +    struct thread *thread;      /* Owning thread. */
3978 +
3979 +    /* Accessed only in owning process context. */
3980 +    struct hash_elem hash_elem; /* struct thread `pages' hash element. */
3981 +
3982 +    /* Set only in owning process context with frame->frame_lock held.
3983 +       Cleared only with scan_lock and frame->frame_lock held. */
3984 +    struct frame *frame;        /* Page frame. */
3985 +
3986 +    /* Swap information, protected by frame->frame_lock. */
3987 +    disk_sector_t sector;       /* Starting sector of swap area, or -1. */
3988 +    
3989 +    /* Memory-mapped file information, protected by frame->frame_lock. */
3990 +    bool private;               /* False to write back to file,
3991 +                                   true to write back to swap. */
3992 +    struct file *file;          /* File. */
3993 +    off_t file_offset;          /* Offset in file. */
3994 +    off_t file_bytes;           /* Bytes to read/write, 1...PGSIZE. */
3995 +  };
3996 +
3997 +void page_exit (void);
3998 +
3999 +struct page *page_allocate (void *, bool read_only);
4000 +void page_deallocate (void *vaddr);
4001 +
4002 +bool page_in (void *fault_addr);
4003 +bool page_out (struct page *);
4004 +bool page_accessed_recently (struct page *);
4005 +
4006 +bool page_lock (const void *, bool will_write);
4007 +void page_unlock (const void *);
4008 +
4009 +hash_hash_func page_hash;
4010 +hash_less_func page_less;
4011 +
4012 +#endif /* vm/page.h */
4013 Index: src/vm/swap.c
4014 diff -u src/vm/swap.c~ src/vm/swap.c
4015 --- src/vm/swap.c~
4016 +++ src/vm/swap.c
4017 @@ -0,0 +1,85 @@
4018 +#include "vm/swap.h"
4019 +#include <bitmap.h>
4020 +#include <debug.h>
4021 +#include <stdio.h>
4022 +#include "vm/frame.h"
4023 +#include "vm/page.h"
4024 +#include "devices/disk.h"
4025 +#include "threads/synch.h"
4026 +#include "threads/vaddr.h"
4027 +
4028 +/* The swap disk. */
4029 +static struct disk *swap_disk;
4030 +
4031 +/* Used swap pages. */
4032 +static struct bitmap *swap_bitmap;
4033 +
4034 +/* Protects swap_bitmap. */
4035 +static struct lock swap_lock;
4036 +
4037 +/* Number of sectors per page. */
4038 +#define PAGE_SECTORS (PGSIZE / DISK_SECTOR_SIZE)
4039 +
4040 +/* Sets up swap. */
4041 +void
4042 +swap_init (void) 
4043 +{
4044 +  swap_disk = disk_get (1, 1);
4045 +  if (swap_disk == NULL) 
4046 +    {
4047 +      printf ("no swap disk--swap disabled\n");
4048 +      swap_bitmap = bitmap_create (0);
4049 +    }
4050 +  else
4051 +    swap_bitmap = bitmap_create (disk_size (swap_disk) / PAGE_SECTORS);
4052 +  if (swap_bitmap == NULL)
4053 +    PANIC ("couldn't create swap bitmap");
4054 +  lock_init (&swap_lock);
4055 +}
4056 +
4057 +/* Swaps in page P, which must have a locked frame
4058 +   (and be swapped out). */
4059 +void
4060 +swap_in (struct page *p) 
4061 +{
4062 +  size_t i;
4063 +  
4064 +  ASSERT (p->frame != NULL);
4065 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
4066 +  ASSERT (p->sector != (disk_sector_t) -1);
4067 +
4068 +  for (i = 0; i < PAGE_SECTORS; i++)
4069 +    disk_read (swap_disk, p->sector + i,
4070 +               p->frame->base + i * DISK_SECTOR_SIZE);
4071 +  bitmap_reset (swap_bitmap, p->sector / PAGE_SECTORS);
4072 +  p->sector = (disk_sector_t) -1;
4073 +}
4074 +
4075 +/* Swaps out page P, which must have a locked frame. */
4076 +bool
4077 +swap_out (struct page *p) 
4078 +{
4079 +  size_t slot;
4080 +  size_t i;
4081 +
4082 +  ASSERT (p->frame != NULL);
4083 +  ASSERT (lock_held_by_current_thread (&p->frame->lock));
4084 +
4085 +  lock_acquire (&swap_lock);
4086 +  slot = bitmap_scan_and_flip (swap_bitmap, 0, 1, false);
4087 +  lock_release (&swap_lock);
4088 +  if (slot == BITMAP_ERROR) 
4089 +    return false; 
4090 +
4091 +  p->sector = slot * PAGE_SECTORS;
4092 +  for (i = 0; i < PAGE_SECTORS; i++)
4093 +    disk_write (swap_disk, p->sector + i,
4094 +                p->frame->base + i * DISK_SECTOR_SIZE);
4095 +  
4096 +  p->private = false;
4097 +  p->file = NULL;
4098 +  p->file_offset = 0;
4099 +  p->file_bytes = 0;
4100 +
4101 +  return true;
4102 +}
4103 Index: src/vm/swap.h
4104 diff -u src/vm/swap.h~ src/vm/swap.h
4105 --- src/vm/swap.h~
4106 +++ src/vm/swap.h
4107 @@ -0,0 +1,11 @@
4108 +#ifndef VM_SWAP_H
4109 +#define VM_SWAP_H 1
4110 +
4111 +#include <stdbool.h>
4112 +
4113 +struct page;
4114 +void swap_init (void);
4115 +void swap_in (struct page *);
4116 +bool swap_out (struct page *);
4117 +
4118 +#endif /* vm/swap.h */