]> Devi Nivas Git - cs3210-lab0.git/commitdiff
protect ip->valid and ip->nlink with sleep lock in iput()
authorRobert Morris <rtm@csail.mit.edu>
Tue, 8 Aug 2017 17:48:48 +0000 (13:48 -0400)
committerRobert Morris <rtm@csail.mit.edu>
Tue, 8 Aug 2017 17:48:48 +0000 (13:48 -0400)
file.h
fs.c

diff --git a/file.h b/file.h
index 73a8e6791b1d36fe5cf71fb09b41c7021f4fee55..497a58a5fc62e4d290bd1cbd4b2e405f6b0e5a82 100644 (file)
--- a/file.h
+++ b/file.h
@@ -14,8 +14,8 @@ struct inode {
   uint dev;           // Device number
   uint inum;          // Inode number
   int ref;            // Reference count
-  struct sleeplock lock;
-  int valid;          // remainder has been read from disk?
+  struct sleeplock lock; // protects everything below here
+  int valid;          // inode has been read from disk?
 
   short type;         // copy of disk inode
   short major;
diff --git a/fs.c b/fs.c
index b67db5df4d5a4006279742e8601191c06ae71c9f..c73c5307d75b3911a66d1f71c9aab4f68d4078a3 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -320,15 +320,17 @@ void
 iput(struct inode *ip)
 {
   acquire(&icache.lock);
-  if(ip->ref == 1 && ip->valid && ip->nlink == 0){
-    // inode has no links and no other references: truncate and free.
+  if(ip->ref == 1){
     acquiresleep(&ip->lock);
-    release(&icache.lock);
-    itrunc(ip);
-    ip->type = 0;
-    iupdate(ip);
-    acquire(&icache.lock);
-    ip->valid = 0;
+    if(ip->valid && ip->nlink == 0){
+      // inode has no links and no other references: truncate and free.
+      release(&icache.lock);
+      itrunc(ip);
+      ip->type = 0;
+      iupdate(ip);
+      ip->valid = 0;
+      acquire(&icache.lock);
+    }
     releasesleep(&ip->lock);
   }
   ip->ref--;