]> Devi Nivas Git - cs3210-lab1.git/commitdiff
fix iput() to more obviously avoid deadlock
authorRobert Morris <rtm@csail.mit.edu>
Tue, 8 Aug 2017 18:19:54 +0000 (14:19 -0400)
committerRobert Morris <rtm@csail.mit.edu>
Tue, 8 Aug 2017 18:19:54 +0000 (14:19 -0400)
fs.c

diff --git a/fs.c b/fs.c
index 67616a46ddb00a8dbbf867598bdf345d9f416209..8bec0019663846a763a9823b60f8ac855a7c91b0 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -329,20 +329,22 @@ iunlock(struct inode *ip)
 void
 iput(struct inode *ip)
 {
-  acquire(&icache.lock);
-  if(ip->ref == 1){
-    acquiresleep(&ip->lock);
-    if(ip->valid && ip->nlink == 0){
+  acquiresleep(&ip->lock);
+  if(ip->valid && ip->nlink == 0){
+    acquire(&icache.lock);
+    int r = ip->ref;
+    release(&icache.lock);
+    if(r == 1){
       // 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);
   }
+  releasesleep(&ip->lock);
+
+  acquire(&icache.lock);
   ip->ref--;
   release(&icache.lock);
 }