protect ip->valid and ip->nlink with sleep lock in iput()

This commit is contained in:
Robert Morris 2017-08-08 13:48:48 -04:00
parent 3375df5061
commit 70d912b332
2 changed files with 12 additions and 10 deletions

4
file.h
View file

@ -14,8 +14,8 @@ struct inode {
uint dev; // Device number uint dev; // Device number
uint inum; // Inode number uint inum; // Inode number
int ref; // Reference count int ref; // Reference count
struct sleeplock lock; struct sleeplock lock; // protects everything below here
int valid; // remainder has been read from disk? int valid; // inode has been read from disk?
short type; // copy of disk inode short type; // copy of disk inode
short major; short major;

18
fs.c
View file

@ -320,15 +320,17 @@ void
iput(struct inode *ip) iput(struct inode *ip)
{ {
acquire(&icache.lock); acquire(&icache.lock);
if(ip->ref == 1 && ip->valid && ip->nlink == 0){ if(ip->ref == 1){
// inode has no links and no other references: truncate and free.
acquiresleep(&ip->lock); acquiresleep(&ip->lock);
release(&icache.lock); if(ip->valid && ip->nlink == 0){
itrunc(ip); // inode has no links and no other references: truncate and free.
ip->type = 0; release(&icache.lock);
iupdate(ip); itrunc(ip);
acquire(&icache.lock); ip->type = 0;
ip->valid = 0; iupdate(ip);
ip->valid = 0;
acquire(&icache.lock);
}
releasesleep(&ip->lock); releasesleep(&ip->lock);
} }
ip->ref--; ip->ref--;