Introduce and use sleeplocks instead of BUSY flags

Remove I_BUSY, B_BUSY, and intrans defs and usages
One spinlock per buf to avoid ugly loop in bget
fix race in filewrite (don't update f->off after releasing lock)
This commit is contained in:
Frans Kaashoek 2011-08-26 10:08:29 -04:00
parent 8a9b6dbd44
commit 3a5fa7ed90
12 changed files with 106 additions and 63 deletions

32
fs.c
View file

@ -113,11 +113,8 @@ bfree(int dev, uint b)
// It is an error to use an inode without holding a reference to it.
//
// Processes are only allowed to read and write inode
// metadata and contents when holding the inode's lock,
// represented by the I_BUSY flag in the in-memory copy.
// Because inode locks are held during disk accesses,
// they are implemented using a flag rather than with
// spin locks. Callers are responsible for locking
// metadata and contents when holding the inode's sleeplock.
// Callers are responsible for locking
// inodes before passing them to routines in this file; leaving
// this responsibility with the caller makes it possible for them
// to create arbitrarily-sized atomic operations.
@ -216,6 +213,7 @@ iget(uint dev, uint inum)
ip->inum = inum;
ip->ref = 1;
ip->flags = 0;
initsleeplock(&ip->sleeplock);
release(&icache.lock);
return ip;
@ -232,7 +230,7 @@ idup(struct inode *ip)
return ip;
}
// Lock the given inode.
// Acquire the sleeplock for a given inode.
void
ilock(struct inode *ip)
{
@ -243,9 +241,7 @@ ilock(struct inode *ip)
panic("ilock");
acquire(&icache.lock);
while(ip->flags & I_BUSY)
sleep(ip, &icache.lock);
ip->flags |= I_BUSY;
acquire_sleeplock(&ip->sleeplock, &icache.lock);
release(&icache.lock);
if(!(ip->flags & I_VALID)){
@ -268,12 +264,11 @@ ilock(struct inode *ip)
void
iunlock(struct inode *ip)
{
if(ip == 0 || !(ip->flags & I_BUSY) || ip->ref < 1)
if(ip == 0 || !acquired_sleeplock(&ip->sleeplock) || ip->ref < 1)
panic("iunlock");
acquire(&icache.lock);
ip->flags &= ~I_BUSY;
wakeup(ip);
release_sleeplock(&ip->sleeplock);
release(&icache.lock);
}
@ -284,14 +279,15 @@ iput(struct inode *ip)
acquire(&icache.lock);
if(ip->ref == 1 && (ip->flags & I_VALID) && ip->nlink == 0){
// inode is no longer used: truncate and free inode.
if(ip->flags & I_BUSY)
if(acquired_sleeplock(&ip->sleeplock))
panic("iput busy");
ip->flags |= I_BUSY;
acquire_sleeplock(&ip->sleeplock, &icache.lock);
release(&icache.lock);
itrunc(ip);
ip->type = 0;
iupdate(ip);
acquire(&icache.lock);
release_sleeplock(&ip->sleeplock);
ip->flags = 0;
wakeup(ip);
}
@ -433,10 +429,14 @@ writei(struct inode *ip, char *src, uint off, uint n)
return devsw[ip->major].write(ip, src, n);
}
if(off > ip->size || off + n < off)
if(off > ip->size || off + n < off) {
panic("writei1");
return -1;
if(off + n > MAXFILE*BSIZE)
}
if(off + n > MAXFILE*BSIZE) {
panic("writei2");
return -1;
}
for(tot=0; tot<n; tot+=m, off+=m, src+=m){
bp = bread(ip->dev, bmap(ip, off/BSIZE));