Skip to content
Snippets Groups Projects
bio.c 3.34 KiB
Newer Older
rsc's avatar
rsc committed
// 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
// editing buf, so other code is not allowed to look at it.
// To wait for a buffer that is B_BUSY, sleep on buf.
// (See bget below.)
// 
// If B_VALID is set, it means that the memory contents
// have been initialized by reading them off the disk.
// (Conversely, if B_VALID is not set, the memory contents
// of buf must be initialized, often by calling bread,
// before being used.)
// 
// 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.

rtm's avatar
rtm committed
#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;

rsc's avatar
rsc committed
// Linked list of all buffers, through prev/next.
rtm's avatar
rtm committed
// bufhead->next is most recently used.
// bufhead->tail is least recently used.
struct buf bufhead;

rtm's avatar
rtm committed
  struct buf *b;

  initlock(&buf_table_lock, "buf_table");
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  // Create linked list of buffers
rtm's avatar
rtm committed
  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;
  }
rtm's avatar
rtm committed

// Look through buffer cache for sector on device dev.
rsc's avatar
rsc committed
// If not found, allocate fresh block.
// In either case, return locked buffer.
static struct buf*
rsc's avatar
rsc committed
bget(uint dev, uint sector)
rtm's avatar
rtm committed
{
  struct buf *b;
rtm's avatar
rtm committed

  acquire(&buf_table_lock);

 loop:
  // Try for cached block.
rsc's avatar
rsc committed
  for(b = bufhead.next; b != &bufhead; b = b->next){
    if((b->flags & (B_BUSY|B_VALID)) &&
rsc's avatar
rsc committed
       b->dev == dev && b->sector == sector){
      if(b->flags & B_BUSY){
        sleep(buf, &buf_table_lock);
        goto loop;
      }
      b->flags |= B_BUSY;
      // b->flags &= ~B_VALID; // Force reread from disk
      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;
rtm's avatar
rtm committed
    }
  }
  panic("bget: no buffers");
rtm's avatar
rtm committed
}

rsc's avatar
rsc committed
// Read buf's contents from disk.
rsc's avatar
rsc committed
struct buf*
rtm's avatar
rtm committed
bread(uint dev, uint sector)
{
  struct buf *b;

rsc's avatar
rsc committed
  b = bget(dev, sector);
rtm's avatar
rtm committed
  if(b->flags & B_VALID)
rtm's avatar
rtm committed
    return b;
rtm's avatar
rtm committed

rsc's avatar
rsc committed
  ide_rw(dev & 0xff, sector, b->data, 1, 1);
rtm's avatar
rtm committed
  b->flags |= B_VALID;
rtm's avatar
rtm committed

  return b;
}

rsc's avatar
rsc committed
// Write buf's contents to disk.
rsc's avatar
rsc committed
// Must be locked.
kaashoek's avatar
kaashoek committed
void
rsc's avatar
 
rsc committed
bwrite(struct buf *b)
kaashoek's avatar
kaashoek committed
{
rsc's avatar
rsc committed
  if((b->flags & B_BUSY) == 0)
    panic("bwrite");
rsc's avatar
 
rsc committed
  ide_rw(b->dev & 0xff, b->sector, b->data, 1, 0);
rtm's avatar
rtm committed
  b->flags |= B_VALID;
kaashoek's avatar
kaashoek committed
}

rsc's avatar
rsc committed
// Release the buffer buf.
rtm's avatar
rtm committed
void
brelse(struct buf *b)
{
  if((b->flags & B_BUSY) == 0)
    panic("brelse");
rtm's avatar
rtm committed
  acquire(&buf_table_lock);

rtm's avatar
rtm committed
  b->next->prev = b->prev;
  b->prev->next = b->next;
  b->next = bufhead.next;
  b->prev = &bufhead;
  bufhead.next->prev = b;
  bufhead.next = b;

rtm's avatar
rtm committed
  b->flags &= ~B_BUSY;
  wakeup(buf);

  release(&buf_table_lock);
}