2006-09-07 14:28:12 +00:00
|
|
|
// Buffer cache.
|
|
|
|
//
|
2007-08-27 14:09:30 +00: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.
|
2016-08-25 13:13:00 +00:00
|
|
|
//
|
2007-08-27 14:09:30 +00:00
|
|
|
// Interface:
|
|
|
|
// * To get a buffer for a particular disk block, call bread.
|
2011-10-11 10:41:37 +00:00
|
|
|
// * After changing buffer data, call bwrite to write it to disk.
|
2007-08-27 14:09:30 +00:00
|
|
|
// * 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.
|
2016-08-25 13:13:00 +00:00
|
|
|
//
|
2016-09-11 21:24:04 +00:00
|
|
|
// The implementation uses two state flags internally:
|
2011-10-11 10:41:37 +00:00
|
|
|
// * B_VALID: the buffer data has been read from the disk.
|
2007-08-27 14:09:30 +00:00
|
|
|
// * B_DIRTY: the buffer data has been modified
|
|
|
|
// and needs to be written to disk.
|
2006-09-07 14:28:12 +00:00
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "defs.h"
|
2007-08-27 23:26:33 +00:00
|
|
|
#include "param.h"
|
2006-07-21 13:18:04 +00:00
|
|
|
#include "spinlock.h"
|
2016-09-11 21:24:04 +00:00
|
|
|
#include "sleeplock.h"
|
2015-04-03 12:22:02 +00:00
|
|
|
#include "fs.h"
|
2006-07-21 13:18:04 +00:00
|
|
|
#include "buf.h"
|
|
|
|
|
2009-05-31 01:29:17 +00:00
|
|
|
struct {
|
|
|
|
struct spinlock lock;
|
|
|
|
struct buf buf[NBUF];
|
2006-08-10 22:08:14 +00:00
|
|
|
|
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
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
void
|
|
|
|
binit(void)
|
|
|
|
{
|
2006-08-12 22:34:13 +00:00
|
|
|
struct buf *b;
|
|
|
|
|
2009-05-31 05:12:21 +00:00
|
|
|
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;
|
2016-09-11 21:24:04 +00:00
|
|
|
initsleeplock(&b->lock, "buffer");
|
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
|
|
|
}
|
2006-08-10 22:08:14 +00:00
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
// Look through buffer cache for block on device dev.
|
2014-08-27 18:14:52 +00:00
|
|
|
// If not found, allocate a buffer.
|
2016-09-11 21:24:04 +00:00
|
|
|
// In either case, return locked buffer.
|
2006-09-07 14:28:12 +00:00
|
|
|
static struct buf*
|
2015-04-03 12:22:02 +00:00
|
|
|
bget(uint dev, uint blockno)
|
2006-07-21 13:18:04 +00:00
|
|
|
{
|
2006-08-12 11:38:57 +00:00
|
|
|
struct buf *b;
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2009-05-31 01:29:17 +00:00
|
|
|
acquire(&bcache.lock);
|
2011-08-29 21:18:40 +00:00
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
// Is the block already cached?
|
2009-05-31 01:29:17 +00:00
|
|
|
for(b = bcache.head.next; b != &bcache.head; b = b->next){
|
2015-04-03 12:22:02 +00:00
|
|
|
if(b->dev == dev && b->blockno == blockno){
|
2016-09-12 00:17:22 +00:00
|
|
|
b->refcnt++;
|
|
|
|
release(&bcache.lock);
|
|
|
|
acquiresleep(&b->lock);
|
|
|
|
return b;
|
2007-08-08 09:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:17:22 +00:00
|
|
|
// Not cached; recycle some unused buffer and clean buffer
|
2016-09-11 21:24:04 +00:00
|
|
|
// "clean" because B_DIRTY and not locked means log.c
|
2014-08-28 09:57:47 +00:00
|
|
|
// hasn't yet committed the changes to the buffer.
|
2009-05-31 01:29:17 +00:00
|
|
|
for(b = bcache.head.prev; b != &bcache.head; b = b->prev){
|
2016-09-12 00:17:22 +00:00
|
|
|
if(b->refcnt == 0 && (b->flags & B_DIRTY) == 0) {
|
2007-08-08 09:50:26 +00:00
|
|
|
b->dev = dev;
|
2015-04-03 12:22:02 +00:00
|
|
|
b->blockno = blockno;
|
2016-09-12 00:17:22 +00:00
|
|
|
b->flags = 0;
|
|
|
|
b->refcnt = 1;
|
2011-08-29 21:18:40 +00:00
|
|
|
release(&bcache.lock);
|
2016-09-12 00:17:22 +00:00
|
|
|
acquiresleep(&b->lock);
|
2007-08-08 09:50:26 +00:00
|
|
|
return b;
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-08 09:50:26 +00:00
|
|
|
panic("bget: no buffers");
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:24:04 +00:00
|
|
|
// Return a locked buf with the contents of the indicated block.
|
2006-09-06 17:27:19 +00:00
|
|
|
struct buf*
|
2015-04-03 12:22:02 +00:00
|
|
|
bread(uint dev, uint blockno)
|
2006-07-21 13:18:04 +00:00
|
|
|
{
|
|
|
|
struct buf *b;
|
|
|
|
|
2015-04-03 12:22:02 +00:00
|
|
|
b = bget(dev, blockno);
|
|
|
|
if(!(b->flags & B_VALID)) {
|
2009-03-08 22:07:13 +00:00
|
|
|
iderw(b);
|
2015-04-03 12:22:02 +00:00
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2016-09-11 21:24:04 +00:00
|
|
|
// Write b's contents to disk. Must be locked.
|
2006-08-07 01:38:46 +00:00
|
|
|
void
|
2007-08-22 06:01:32 +00:00
|
|
|
bwrite(struct buf *b)
|
2006-08-07 01:38:46 +00:00
|
|
|
{
|
2016-09-12 00:17:22 +00:00
|
|
|
if(!holdingsleep(&b->lock))
|
2006-09-07 15:29:54 +00:00
|
|
|
panic("bwrite");
|
2007-08-27 14:09:30 +00:00
|
|
|
b->flags |= B_DIRTY;
|
2009-03-08 22:07:13 +00:00
|
|
|
iderw(b);
|
2006-08-07 01:38:46 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:24:04 +00:00
|
|
|
// Release a locked buffer.
|
2011-10-11 10:41:37 +00:00
|
|
|
// Move to the head of the MRU list.
|
2006-07-21 13:18:04 +00:00
|
|
|
void
|
|
|
|
brelse(struct buf *b)
|
|
|
|
{
|
2016-09-12 00:17:22 +00:00
|
|
|
if(!holdingsleep(&b->lock))
|
2006-07-21 13:18:04 +00:00
|
|
|
panic("brelse");
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2016-09-11 21:24:04 +00:00
|
|
|
releasesleep(&b->lock);
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2016-09-12 00:17:22 +00:00
|
|
|
acquire(&bcache.lock);
|
|
|
|
b->refcnt--;
|
|
|
|
if (b->refcnt == 0) {
|
|
|
|
// no one is waiting for it.
|
|
|
|
b->next->prev = b->prev;
|
|
|
|
b->prev->next = b->next;
|
|
|
|
b->next = bcache.head.next;
|
|
|
|
b->prev = &bcache.head;
|
|
|
|
bcache.head.next->prev = b;
|
|
|
|
bcache.head.next = b;
|
|
|
|
}
|
|
|
|
|
2009-05-31 01:29:17 +00:00
|
|
|
release(&bcache.lock);
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
2014-08-29 21:06:49 +00:00
|
|
|
//PAGEBREAK!
|
|
|
|
// Blank page.
|
2006-07-21 13:18:04 +00:00
|
|
|
|