2006-09-07 16:28:12 +02:00
|
|
|
// Buffer cache.
|
|
|
|
//
|
2007-08-27 16:09:30 +02:00
|
|
|
// 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 16:28:12 +02:00
|
|
|
//
|
2007-08-27 16:09:30 +02: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 16:28:12 +02:00
|
|
|
//
|
2007-08-27 16:09:30 +02:00
|
|
|
// The implementation uses three state flags internally:
|
|
|
|
// * B_BUSY: the block has been returned from bread
|
|
|
|
// and has not been passed back to brelse.
|
|
|
|
// * 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 16:28:12 +02:00
|
|
|
|
2006-07-21 15:18:04 +02:00
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
2007-08-28 01:26:33 +02:00
|
|
|
#include "param.h"
|
2006-07-21 15:18:04 +02:00
|
|
|
#include "spinlock.h"
|
|
|
|
#include "buf.h"
|
|
|
|
|
|
|
|
struct buf buf[NBUF];
|
2006-08-11 00:08:14 +02:00
|
|
|
struct spinlock buf_table_lock;
|
|
|
|
|
2006-09-07 16:28:12 +02:00
|
|
|
// Linked list of all buffers, through prev/next.
|
2006-08-13 00:34:13 +02:00
|
|
|
// bufhead->next is most recently used.
|
|
|
|
// bufhead->tail is least recently used.
|
|
|
|
struct buf bufhead;
|
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
void
|
|
|
|
binit(void)
|
|
|
|
{
|
2006-08-13 00:34:13 +02:00
|
|
|
struct buf *b;
|
|
|
|
|
2006-08-11 00:08:14 +02:00
|
|
|
initlock(&buf_table_lock, "buf_table");
|
2006-08-13 00:34:13 +02:00
|
|
|
|
2007-08-28 07:00:53 +02:00
|
|
|
//PAGEBREAK!
|
2006-09-07 16:28:12 +02:00
|
|
|
// Create linked list of buffers
|
2006-08-13 00:34:13 +02:00
|
|
|
bufhead.prev = &bufhead;
|
|
|
|
bufhead.next = &bufhead;
|
|
|
|
for(b = buf; b < buf+NBUF; b++){
|
|
|
|
b->next = bufhead.next;
|
|
|
|
b->prev = &bufhead;
|
|
|
|
bufhead.next->prev = b;
|
|
|
|
bufhead.next = b;
|
|
|
|
}
|
2006-08-11 00:08:14 +02:00
|
|
|
}
|
2006-07-21 15:18:04 +02:00
|
|
|
|
2007-08-08 11:50:26 +02:00
|
|
|
// Look through buffer cache for sector on device dev.
|
2006-09-07 16:28:12 +02:00
|
|
|
// If not found, allocate fresh block.
|
|
|
|
// In either case, return locked buffer.
|
|
|
|
static struct buf*
|
2006-09-07 16:39:05 +02:00
|
|
|
bget(uint dev, uint sector)
|
2006-07-21 15:18:04 +02:00
|
|
|
{
|
2006-08-12 13:38:57 +02:00
|
|
|
struct buf *b;
|
2006-07-21 15:18:04 +02:00
|
|
|
|
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
2007-08-08 11:50:26 +02:00
|
|
|
loop:
|
|
|
|
// Try for cached block.
|
2007-08-14 21:42:14 +02:00
|
|
|
for(b = bufhead.next; b != &bufhead; b = b->next){
|
2007-08-08 11:50:26 +02:00
|
|
|
if((b->flags & (B_BUSY|B_VALID)) &&
|
2007-08-14 21:42:14 +02:00
|
|
|
b->dev == dev && b->sector == sector){
|
2009-05-31 02:23:00 +02:00
|
|
|
if(!(b->flags & B_BUSY)){
|
|
|
|
b->flags |= B_BUSY;
|
|
|
|
release(&buf_table_lock);
|
|
|
|
return b;
|
2007-08-14 21:42:14 +02:00
|
|
|
}
|
2009-05-31 02:23:00 +02:00
|
|
|
sleep(b, &buf_table_lock);
|
|
|
|
goto loop;
|
2007-08-08 11:50:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate fresh block.
|
|
|
|
for(b = bufhead.prev; b != &bufhead; b = b->prev){
|
|
|
|
if((b->flags & B_BUSY) == 0){
|
|
|
|
b->dev = dev;
|
|
|
|
b->sector = sector;
|
2009-05-31 02:23:00 +02:00
|
|
|
b->flags = B_BUSY;
|
2007-08-08 11:50:26 +02:00
|
|
|
release(&buf_table_lock);
|
|
|
|
return b;
|
2006-07-21 15:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
2007-08-08 11:50:26 +02:00
|
|
|
panic("bget: no buffers");
|
2006-07-21 15:18:04 +02:00
|
|
|
}
|
|
|
|
|
2007-08-24 23:14:58 +02:00
|
|
|
// Return a B_BUSY buf with the contents of the indicated disk sector.
|
2006-09-06 19:27:19 +02:00
|
|
|
struct buf*
|
2006-07-21 15:18:04 +02:00
|
|
|
bread(uint dev, uint sector)
|
|
|
|
{
|
|
|
|
struct buf *b;
|
|
|
|
|
2006-09-07 16:28:12 +02:00
|
|
|
b = bget(dev, sector);
|
2007-08-27 16:09:30 +02:00
|
|
|
if(!(b->flags & B_VALID))
|
2009-03-08 23:07:13 +01:00
|
|
|
iderw(b);
|
2006-07-21 15:18:04 +02:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2007-08-24 23:14:58 +02:00
|
|
|
// Write buf's contents to disk. Must be locked.
|
2006-08-07 03:38:46 +02:00
|
|
|
void
|
2007-08-22 08:01:32 +02:00
|
|
|
bwrite(struct buf *b)
|
2006-08-07 03:38:46 +02:00
|
|
|
{
|
2006-09-07 17:29:54 +02:00
|
|
|
if((b->flags & B_BUSY) == 0)
|
|
|
|
panic("bwrite");
|
2007-08-27 16:09:30 +02:00
|
|
|
b->flags |= B_DIRTY;
|
2009-03-08 23:07:13 +01:00
|
|
|
iderw(b);
|
2006-08-07 03:38:46 +02:00
|
|
|
}
|
|
|
|
|
2006-09-07 16:28:12 +02:00
|
|
|
// Release the buffer buf.
|
2006-07-21 15:18:04 +02:00
|
|
|
void
|
|
|
|
brelse(struct buf *b)
|
|
|
|
{
|
|
|
|
if((b->flags & B_BUSY) == 0)
|
|
|
|
panic("brelse");
|
2006-09-06 19:27:19 +02:00
|
|
|
|
2006-07-21 15:18:04 +02:00
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
2006-08-13 00:34:13 +02:00
|
|
|
b->next->prev = b->prev;
|
|
|
|
b->prev->next = b->next;
|
|
|
|
b->next = bufhead.next;
|
|
|
|
b->prev = &bufhead;
|
|
|
|
bufhead.next->prev = b;
|
|
|
|
bufhead.next = b;
|
|
|
|
|
2006-07-21 15:18:04 +02:00
|
|
|
b->flags &= ~B_BUSY;
|
2009-05-31 02:23:00 +02:00
|
|
|
wakeup(b);
|
2006-07-21 15:18:04 +02:00
|
|
|
|
|
|
|
release(&buf_table_lock);
|
|
|
|
}
|
|
|
|
|