xv6/bio.c

141 lines
3.3 KiB
C
Raw Normal View History

2006-09-07 14:28:12 +00:00
// Buffer cache.
//
// The buffer cache is a linked list of buf structures holding
// cached copies of disk block contents. Caching disk blocks
// in memory reduces the number of disk reads and also provides
// a synchronization point for disk blocks used by multiple processes.
2006-09-07 14:28:12 +00:00
//
// Interface:
// * To get a buffer for a particular disk block, call bread.
// * After changing buffer data, call bwrite to flush it to disk.
// * When done with the buffer, call brelse.
// * Do not use the buffer after calling brelse.
// * Only one process at a time can use a buffer,
// so do not keep them longer than necessary.
2006-09-07 14:28:12 +00:00
//
// The implementation uses two state flags internally:
// * B_VALID: the buffer data has been initialized
// with the associated disk block contents.
// * B_DIRTY: the buffer data has been modified
// and needs to be written to disk.
2006-09-07 14:28:12 +00:00
#include "types.h"
#include "defs.h"
2007-08-27 23:26:33 +00:00
#include "param.h"
#include "spinlock.h"
#include "buf.h"
2009-05-31 01:29:17 +00:00
struct {
struct spinlock lock;
struct buf buf[NBUF];
2009-05-31 01:29:17 +00:00
// Linked list of all buffers, through prev/next.
// head.next is most recently used.
struct buf head;
} bcache;
2006-08-12 22:34:13 +00:00
void
binit(void)
{
2006-08-12 22:34:13 +00:00
struct buf *b;
initlock(&bcache.lock, "bcache");
2006-08-12 22:34:13 +00:00
2007-08-28 05:00:53 +00:00
//PAGEBREAK!
2006-09-07 14:28:12 +00:00
// Create linked list of buffers
2009-05-31 01:29:17 +00:00
bcache.head.prev = &bcache.head;
bcache.head.next = &bcache.head;
for(b = bcache.buf; b < bcache.buf+NBUF; b++){
b->next = bcache.head.next;
b->prev = &bcache.head;
b->dev = -1;
initlock(&b->lock, "buf");
initsleeplock(&b->sleeplock);
2009-05-31 01:29:17 +00:00
bcache.head.next->prev = b;
bcache.head.next = b;
2006-08-12 22:34:13 +00:00
}
}
// Look through buffer cache for sector on device dev.
2006-09-07 14:28:12 +00:00
// If not found, allocate fresh block.
// In either case, return sleep-locked buffer.
2006-09-07 14:28:12 +00:00
static struct buf*
2006-09-07 14:39:05 +00:00
bget(uint dev, uint sector)
{
struct buf *b;
2009-05-31 01:29:17 +00:00
acquire(&bcache.lock);
// Try for cached block.
2009-05-31 01:29:17 +00:00
for(b = bcache.head.next; b != &bcache.head; b = b->next){
acquire(&b->lock);
2009-05-31 01:29:17 +00:00
if(b->dev == dev && b->sector == sector){
release(&bcache.lock);
acquire_sleeplock(&b->sleeplock, &b->lock);
release(&b->lock);
return b;
}
release(&b->lock);
}
// Allocate fresh block.
2009-05-31 01:29:17 +00:00
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
acquire(&b->lock);
if (!acquired_sleeplock(&b->sleeplock)) {
release(&bcache.lock);
b->dev = dev;
b->sector = sector;
b->flags = 0;
acquire_sleeplock(&b->sleeplock, &b->lock);
release(&b->lock);
return b;
}
release(&b->lock);
}
panic("bget: no buffers");
}
// Return a locked buf with the contents of the indicated disk sector.
2006-09-06 17:27:19 +00:00
struct buf*
bread(uint dev, uint sector)
{
struct buf *b;
2006-09-07 14:28:12 +00:00
b = bget(dev, sector);
if(!(b->flags & B_VALID))
iderw(b);
return b;
}
2009-05-31 01:29:17 +00:00
// Write b's contents to disk. Must be locked.
2006-08-07 01:38:46 +00:00
void
bwrite(struct buf *b)
2006-08-07 01:38:46 +00:00
{
if(!acquired_sleeplock(&b->sleeplock))
2006-09-07 15:29:54 +00:00
panic("bwrite");
b->flags |= B_DIRTY;
iderw(b);
2006-08-07 01:38:46 +00:00
}
2009-05-31 01:29:17 +00:00
// Release the buffer b.
void
brelse(struct buf *b)
{
if(!acquired_sleeplock(&b->sleeplock))
panic("brelse");
2006-09-06 17:27:19 +00:00
2009-05-31 01:29:17 +00:00
acquire(&bcache.lock);
acquire(&b->lock);
2006-08-12 22:34:13 +00:00
b->next->prev = b->prev;
b->prev->next = b->next;
2009-05-31 01:29:17 +00:00
b->next = bcache.head.next;
b->prev = &bcache.head;
bcache.head.next->prev = b;
bcache.head.next = b;
2006-08-12 22:34:13 +00:00
release_sleeplock(&b->sleeplock);
release(&b->lock);
2009-05-31 01:29:17 +00:00
release(&bcache.lock);
}