diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-03-15 12:28:59 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-03-15 12:32:42 +0100 |
| commit | e7ab517c36247ef9dcef234b350939c993bd2c63 (patch) | |
| tree | a028814e4f66c06fb813eaef74a279fa2798bc11 | |
| parent | 467d2e1ecc443e4b287e3fb2e7fa359415dbd389 (diff) | |
| download | pintos-e7ab517c36247ef9dcef234b350939c993bd2c63.tar.gz | |
lock open_inodes
| -rw-r--r-- | src/filesys/inode.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/filesys/inode.c b/src/filesys/inode.c index cfdcb7b..78c1972 100644 --- a/src/filesys/inode.c +++ b/src/filesys/inode.c @@ -6,6 +6,7 @@ #include "filesys/filesys.h" #include "filesys/free-map.h" #include "threads/malloc.h" +#include "threads/synch.h" /* Identifies an inode. */ #define INODE_MAGIC 0x494e4f44 @@ -53,6 +54,9 @@ byte_to_sector (const struct inode *inode, off_t pos) return -1; } +/* Lock the open inode list since so inode_open doesn't race. */ +struct lock open_inodes_lock; + /* List of open inodes, so that opening a single inode twice returns the same `struct inode'. */ static struct list open_inodes; @@ -62,6 +66,7 @@ void inode_init (void) { list_init (&open_inodes); + lock_init (&open_inodes_lock); } /* Initializes an inode with LENGTH bytes of data and @@ -114,6 +119,7 @@ inode_open (disk_sector_t sector) struct list_elem *e; struct inode *inode; + lock_acquire (&open_inodes_lock); /* Check whether this inode is already open. */ for (e = list_begin (&open_inodes); e != list_end (&open_inodes); e = list_next (e)) @@ -122,14 +128,17 @@ inode_open (disk_sector_t sector) if (inode->sector == sector) { inode_reopen (inode); + lock_release (&open_inodes_lock); return inode; } } /* Allocate memory. */ inode = malloc (sizeof *inode); - if (inode == NULL) + if (inode == NULL) { + lock_release (&open_inodes_lock); return NULL; + } /* Initialize. */ list_push_front (&open_inodes, &inode->elem); @@ -137,6 +146,7 @@ inode_open (disk_sector_t sector) inode->open_cnt = 1; inode->deny_write_cnt = 0; inode->removed = false; + lock_release (&open_inodes_lock); disk_read (filesys_disk, inode->sector, &inode->data); return inode; } |
