bio.c: use struct like icache does

This commit is contained in:
rsc 2009-05-31 01:29:17 +00:00
parent c47bc4fd54
commit 0badeaa29f

61
bio.c
View file

@ -27,30 +27,32 @@
#include "spinlock.h" #include "spinlock.h"
#include "buf.h" #include "buf.h"
struct buf buf[NBUF]; struct {
struct spinlock buf_table_lock; struct spinlock lock;
struct buf buf[NBUF];
// Linked list of all buffers, through prev/next. // Linked list of all buffers, through prev/next.
// bufhead->next is most recently used. // head.next is most recently used.
// bufhead->tail is least recently used. struct buf head;
struct buf bufhead; } bcache;
void void
binit(void) binit(void)
{ {
struct buf *b; struct buf *b;
initlock(&buf_table_lock, "buf_table"); initlock(&bcache.lock, "buf_table");
//PAGEBREAK! //PAGEBREAK!
// Create linked list of buffers // Create linked list of buffers
bufhead.prev = &bufhead; bcache.head.prev = &bcache.head;
bufhead.next = &bufhead; bcache.head.next = &bcache.head;
for(b = buf; b < buf+NBUF; b++){ for(b = bcache.buf; b < bcache.buf+NBUF; b++){
b->next = bufhead.next; b->next = bcache.head.next;
b->prev = &bufhead; b->prev = &bcache.head;
bufhead.next->prev = b; b->dev = -1;
bufhead.next = b; bcache.head.next->prev = b;
bcache.head.next = b;
} }
} }
@ -62,30 +64,29 @@ bget(uint dev, uint sector)
{ {
struct buf *b; struct buf *b;
acquire(&buf_table_lock); acquire(&bcache.lock);
loop: loop:
// Try for cached block. // Try for cached block.
for(b = bufhead.next; b != &bufhead; b = b->next){ for(b = bcache.head.next; b != &bcache.head; b = b->next){
if((b->flags & (B_BUSY|B_VALID)) && if(b->dev == dev && b->sector == sector){
b->dev == dev && b->sector == sector){
if(!(b->flags & B_BUSY)){ if(!(b->flags & B_BUSY)){
b->flags |= B_BUSY; b->flags |= B_BUSY;
release(&buf_table_lock); release(&bcache.lock);
return b; return b;
} }
sleep(b, &buf_table_lock); sleep(b, &bcache.lock);
goto loop; goto loop;
} }
} }
// Allocate fresh block. // Allocate fresh block.
for(b = bufhead.prev; b != &bufhead; b = b->prev){ for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
if((b->flags & B_BUSY) == 0){ if((b->flags & B_BUSY) == 0){
b->dev = dev; b->dev = dev;
b->sector = sector; b->sector = sector;
b->flags = B_BUSY; b->flags = B_BUSY;
release(&buf_table_lock); release(&bcache.lock);
return b; return b;
} }
} }
@ -104,7 +105,7 @@ bread(uint dev, uint sector)
return b; return b;
} }
// Write buf's contents to disk. Must be locked. // Write b's contents to disk. Must be locked.
void void
bwrite(struct buf *b) bwrite(struct buf *b)
{ {
@ -114,25 +115,25 @@ bwrite(struct buf *b)
iderw(b); iderw(b);
} }
// Release the buffer buf. // Release the buffer b.
void void
brelse(struct buf *b) brelse(struct buf *b)
{ {
if((b->flags & B_BUSY) == 0) if((b->flags & B_BUSY) == 0)
panic("brelse"); panic("brelse");
acquire(&buf_table_lock); acquire(&bcache.lock);
b->next->prev = b->prev; b->next->prev = b->prev;
b->prev->next = b->next; b->prev->next = b->next;
b->next = bufhead.next; b->next = bcache.head.next;
b->prev = &bufhead; b->prev = &bcache.head;
bufhead.next->prev = b; bcache.head.next->prev = b;
bufhead.next = b; bcache.head.next = b;
b->flags &= ~B_BUSY; b->flags &= ~B_BUSY;
wakeup(b); wakeup(b);
release(&buf_table_lock); release(&bcache.lock);
} }