2006-07-21 13:18:04 +00:00
|
|
|
#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];
|
2006-08-10 22:08:14 +00:00
|
|
|
struct spinlock buf_table_lock;
|
|
|
|
|
2006-08-12 22:34:13 +00:00
|
|
|
// linked list of all buffers, through prev/next.
|
|
|
|
// bufhead->next is most recently used.
|
|
|
|
// bufhead->tail is least recently used.
|
|
|
|
struct buf bufhead;
|
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
void
|
|
|
|
binit(void)
|
|
|
|
{
|
2006-08-12 22:34:13 +00:00
|
|
|
struct buf *b;
|
|
|
|
|
2006-08-10 22:08:14 +00:00
|
|
|
initlock(&buf_table_lock, "buf_table");
|
2006-08-12 22:34:13 +00: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-10 22:08:14 +00:00
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
struct buf*
|
2006-08-12 11:38:57 +00:00
|
|
|
getblk(uint dev, uint sector)
|
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
|
|
|
|
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
2006-09-06 18:47:51 +00:00
|
|
|
for(;;){
|
2006-08-12 22:34:13 +00:00
|
|
|
for(b = bufhead.next; b != &bufhead; b = b->next)
|
2006-09-06 19:08:14 +00:00
|
|
|
if((b->flags & (B_BUSY|B_VALID)) &&
|
|
|
|
b->dev == dev && b->sector == sector)
|
2006-08-12 11:38:57 +00:00
|
|
|
break;
|
|
|
|
|
2006-08-12 22:34:13 +00:00
|
|
|
if(b != &bufhead){
|
2006-08-12 17:17:35 +00:00
|
|
|
if(b->flags & B_BUSY){
|
|
|
|
sleep(buf, &buf_table_lock);
|
|
|
|
} else {
|
|
|
|
b->flags |= B_BUSY;
|
|
|
|
release(&buf_table_lock);
|
|
|
|
return b;
|
|
|
|
}
|
2006-08-12 11:38:57 +00:00
|
|
|
} else {
|
2006-08-12 22:34:13 +00:00
|
|
|
for(b = bufhead.prev; b != &bufhead; b = b->prev){
|
2006-08-12 11:38:57 +00:00
|
|
|
if((b->flags & B_BUSY) == 0){
|
2006-08-12 17:17:35 +00:00
|
|
|
b->flags = B_BUSY;
|
2006-08-12 11:38:57 +00:00
|
|
|
b->dev = dev;
|
|
|
|
b->sector = sector;
|
|
|
|
release(&buf_table_lock);
|
|
|
|
return b;
|
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
2006-08-12 11:38:57 +00:00
|
|
|
panic("getblk: no buffers");
|
2006-07-21 13:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-06 17:27:19 +00:00
|
|
|
struct buf*
|
2006-07-21 13:18:04 +00:00
|
|
|
bread(uint dev, uint sector)
|
|
|
|
{
|
|
|
|
void *c;
|
|
|
|
struct buf *b;
|
|
|
|
extern struct spinlock ide_lock;
|
|
|
|
|
2006-08-12 11:38:57 +00:00
|
|
|
b = getblk(dev, sector);
|
2006-08-30 18:55:06 +00:00
|
|
|
if(b->flags & B_VALID)
|
2006-08-12 17:17:35 +00:00
|
|
|
return b;
|
2006-07-21 13:18:04 +00:00
|
|
|
|
|
|
|
acquire(&ide_lock);
|
2006-08-06 20:28:15 +00:00
|
|
|
c = ide_start_rw(dev & 0xff, sector, b->data, 1, 1);
|
2006-09-06 17:27:19 +00:00
|
|
|
sleep(c, &ide_lock);
|
2006-08-06 20:28:15 +00:00
|
|
|
ide_finish(c);
|
2006-08-12 17:17:35 +00:00
|
|
|
b->flags |= B_VALID;
|
2006-07-21 13:18:04 +00:00
|
|
|
release(&ide_lock);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2006-08-07 01:38:46 +00:00
|
|
|
void
|
2006-08-12 22:44:26 +00:00
|
|
|
bwrite(struct buf *b, uint sector)
|
2006-08-07 01:38:46 +00:00
|
|
|
{
|
|
|
|
void *c;
|
|
|
|
extern struct spinlock ide_lock;
|
|
|
|
|
|
|
|
acquire(&ide_lock);
|
2006-08-12 22:44:26 +00:00
|
|
|
c = ide_start_rw(b->dev & 0xff, sector, b->data, 1, 0);
|
2006-09-06 17:27:19 +00:00
|
|
|
sleep(c, &ide_lock);
|
2006-08-07 01:38:46 +00:00
|
|
|
ide_finish(c);
|
2006-08-12 17:17:35 +00:00
|
|
|
b->flags |= B_VALID;
|
2006-08-07 01:38:46 +00:00
|
|
|
release(&ide_lock);
|
|
|
|
}
|
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
void
|
|
|
|
brelse(struct buf *b)
|
|
|
|
{
|
|
|
|
if((b->flags & B_BUSY) == 0)
|
|
|
|
panic("brelse");
|
2006-09-06 17:27:19 +00:00
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
2006-08-12 22:34:13 +00: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 13:18:04 +00:00
|
|
|
b->flags &= ~B_BUSY;
|
|
|
|
wakeup(buf);
|
|
|
|
|
|
|
|
release(&buf_table_lock);
|
|
|
|
}
|
|
|
|
|