Skip to content

Commit

Permalink
protect ip->valid and ip->nlink with sleep lock in iput()
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Morris committed Aug 8, 2017
1 parent 3375df5 commit 70d912b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 10 additions & 8 deletions fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
Expand Down

0 comments on commit 70d912b

Please sign in to comment.