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;
|
|
|
|
|
|
|
|
void
|
|
|
|
binit(void)
|
|
|
|
{
|
|
|
|
initlock(&buf_table_lock, "buf_table");
|
|
|
|
}
|
2006-07-21 13:18:04 +00:00
|
|
|
|
|
|
|
struct buf *
|
|
|
|
getblk()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
|
|
|
// XXX need to lock the block even if not caching, to
|
|
|
|
// avoid read modify write problems.
|
|
|
|
|
|
|
|
while(1){
|
|
|
|
for(i = 0; i < NBUF; i++){
|
|
|
|
if((buf[i].flags & B_BUSY) == 0){
|
|
|
|
buf[i].flags |= B_BUSY;
|
|
|
|
release(&buf_table_lock);
|
|
|
|
return buf + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep(buf, &buf_table_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct buf *
|
|
|
|
bread(uint dev, uint sector)
|
|
|
|
{
|
|
|
|
void *c;
|
|
|
|
struct buf *b;
|
|
|
|
extern struct spinlock ide_lock;
|
|
|
|
|
|
|
|
b = getblk();
|
|
|
|
|
|
|
|
acquire(&ide_lock);
|
2006-08-06 20:28:15 +00:00
|
|
|
c = ide_start_rw(dev & 0xff, sector, b->data, 1, 1);
|
2006-08-04 18:12:31 +00:00
|
|
|
sleep (c, &ide_lock);
|
2006-08-06 20:28:15 +00:00
|
|
|
ide_finish(c);
|
2006-07-21 13:18:04 +00:00
|
|
|
release(&ide_lock);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2006-08-07 01:38:46 +00:00
|
|
|
void
|
|
|
|
bwrite(uint dev, struct buf *b, uint sector)
|
|
|
|
{
|
|
|
|
void *c;
|
|
|
|
extern struct spinlock ide_lock;
|
|
|
|
|
|
|
|
acquire(&ide_lock);
|
|
|
|
c = ide_start_rw(dev & 0xff, sector, b->data, 1, 0);
|
|
|
|
sleep (c, &ide_lock);
|
|
|
|
ide_finish(c);
|
|
|
|
release(&ide_lock);
|
|
|
|
}
|
|
|
|
|
2006-07-21 13:18:04 +00:00
|
|
|
void
|
|
|
|
brelse(struct buf *b)
|
|
|
|
{
|
|
|
|
if((b->flags & B_BUSY) == 0)
|
|
|
|
panic("brelse");
|
|
|
|
|
|
|
|
acquire(&buf_table_lock);
|
|
|
|
|
|
|
|
b->flags &= ~B_BUSY;
|
|
|
|
wakeup(buf);
|
|
|
|
|
|
|
|
release(&buf_table_lock);
|
|
|
|
}
|
|
|
|
|