xv6-cs450/bio.c

149 lines
3.2 KiB
C
Raw Normal View History

2006-09-07 16:28:12 +02:00
// Buffer cache.
//
// The buffer cache is a linked list of buf structures
// holding cached copies of disk block contents.
// Each buf has two state bits B_BUSY and B_VALID.
// If B_BUSY is set, it means that some code is currently
2007-08-24 21:46:19 +02:00
// using buf, so other code is not allowed to use it.
2006-09-07 16:28:12 +02:00
// To wait for a buffer that is B_BUSY, sleep on buf.
// (See bget below.)
//
2007-08-24 21:32:36 +02:00
// If B_VALID is set, it means that the sector in b->data is
// the same as on the disk. If B_VALID is not set, the contents
2006-09-07 16:28:12 +02:00
// of buf must be initialized, often by calling bread,
2007-08-24 21:32:36 +02:00
// before being used.
2006-09-07 16:28:12 +02:00
//
// After making changes to a buf's memory, call bwrite to flush
// the changes out to disk, to keep the disk and memory copies
// in sync.
//
// When finished with a buffer, call brelse to release the buffer
// (i.e., clear B_BUSY), so that others can access it.
//
// Bufs that are not B_BUSY are fair game for reuse for other
// disk blocks. It is not allowed to use a buf after calling brelse.
#include "types.h"
#include "param.h"
#include "x86.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
#include "spinlock.h"
#include "buf.h"
struct buf buf[NBUF];
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;
void
binit(void)
{
2006-08-13 00:34:13 +02:00
struct buf *b;
initlock(&buf_table_lock, "buf_table");
2006-08-13 00:34:13 +02:00
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;
}
}
// 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)
{
struct buf *b;
acquire(&buf_table_lock);
loop:
// Try for cached block.
2007-08-14 21:42:14 +02:00
for(b = bufhead.next; b != &bufhead; b = b->next){
if((b->flags & (B_BUSY|B_VALID)) &&
2007-08-14 21:42:14 +02:00
b->dev == dev && b->sector == sector){
if(b->flags & B_BUSY){
sleep(buf, &buf_table_lock);
goto loop;
}
b->flags |= B_BUSY;
release(&buf_table_lock);
return b;
}
}
// Allocate fresh block.
for(b = bufhead.prev; b != &bufhead; b = b->prev){
if((b->flags & B_BUSY) == 0){
b->flags = B_BUSY;
b->dev = dev;
b->sector = sector;
release(&buf_table_lock);
return b;
}
}
panic("bget: no buffers");
}
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*
bread(uint dev, uint sector)
{
struct buf *b;
2006-09-07 16:28:12 +02:00
b = bget(dev, sector);
2006-08-30 20:55:06 +02:00
if(b->flags & B_VALID)
2006-08-12 19:17:35 +02:00
return b;
2007-08-24 21:32:36 +02:00
b->flags &= ~B_WRITE;
ide_rw(b);
2006-08-12 19:17:35 +02:00
b->flags |= B_VALID;
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
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-24 21:32:36 +02:00
b->flags |= B_WRITE;
ide_rw(b);
2006-08-12 19:17:35 +02:00
b->flags |= B_VALID;
2006-08-07 03:38:46 +02:00
}
2006-09-07 16:28:12 +02:00
// Release the buffer buf.
void
brelse(struct buf *b)
{
if((b->flags & B_BUSY) == 0)
panic("brelse");
2006-09-06 19:27:19 +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;
b->flags &= ~B_BUSY;
wakeup(buf);
release(&buf_table_lock);
}