提交 ed179129 创建 作者: Silas Boyd-Wickizer's avatar Silas Boyd-Wickizer

64-bit bio.c

上级 9ef8078d
......@@ -2,10 +2,12 @@
OBJS = \
asm.o \
bio.o \
cga.o \
condvar.o \
console.o \
fs.o \
ide.o \
lapic.o \
kalloc.o \
main.o \
......
......@@ -22,7 +22,7 @@
// and needs to be written to disk.
#include "types.h"
#include "defs.h"
#include "kernel.h"
#include "param.h"
#include "spinlock.h"
#include "condvar.h"
......@@ -32,23 +32,6 @@ static struct ns *bufns;
enum { writeback = 0 };
void
binit(void)
{
bufns = nsalloc(0);
for (uint i = 0; i < NBUF; i++) {
struct buf *b = kmalloc(sizeof(*b));
b->dev = 0xdeadbeef;
b->sector = -i; /* dummy to pre-allocate NBUF spaces for evict */
b->flags = 0;
initlock(&b->lock, "bcache-lock");
initcondvar(&b->cv, "bcache-cv");
if (ns_insert(bufns, KII(b->dev, b->sector), b) < 0)
panic("binit ns_insert");
}
}
static void *
evict(void *vkey, void *bp, void *arg)
{
......@@ -75,7 +58,7 @@ evict_valid(void *vkey, void *bp, void *arg)
// If not found, allocate fresh block.
// In either case, return locked buffer.
static struct buf*
bget(uint dev, uint sector, int *writer)
bget(u32 dev, u64 sector, int *writer)
{
struct buf *b;
......@@ -136,7 +119,7 @@ bget(uint dev, uint sector, int *writer)
// Return a B_BUSY buf with the contents of the indicated disk sector.
struct buf*
bread(uint dev, uint sector, int writer)
bread(u32 dev, u64 sector, int writer)
{
struct buf *b;
......@@ -176,3 +159,19 @@ brelse(struct buf *b, int writer)
rcu_end_read();
}
void
initbio(void)
{
bufns = nsalloc(0);
for (u64 i = 0; i < NBUF; i++) {
struct buf *b = kmalloc(sizeof(*b));
b->dev = 0xdeadbeef;
b->sector = -i; /* dummy to pre-allocate NBUF spaces for evict */
b->flags = 0;
initlock(&b->lock, "bcache-lock");
initcondvar(&b->cv, "bcache-cv");
if (ns_insert(bufns, KII(b->dev, b->sector), b) < 0)
panic("binit ns_insert");
}
}
struct buf {
int flags;
uint dev;
uint sector;
u32 dev;
u64 sector;
struct buf *prev; // LRU cache list
struct buf *next;
struct buf *qnext; // disk queue
char lockname[16];
struct condvar cv;
struct spinlock lock;
uchar data[512];
u8 data[512];
};
#define B_BUSY 0x1 // buffer is locked by some process
#define B_VALID 0x2 // buffer has been read from disk
......
......@@ -106,6 +106,40 @@ vprintfmt(void (*putch) (void*, char), void *putarg,
}
}
// Print to a buffer.
struct bufstate {
char *p;
char *e;
};
static void
writebuf(void *arg, char c)
{
struct bufstate *bs = arg;
if (bs->p < bs->e) {
bs->p[0] = c;
bs->p++;
}
}
void
vsnprintf(char *buf, u32 n, char *fmt, va_list ap)
{
struct bufstate bs = { buf, buf+n-1 };
vprintfmt(writebuf, (void*) &bs, fmt, ap);
bs.p[0] = '\0';
}
void
snprintf(char *buf, u32 n, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, n, fmt, ap);
va_end(ap);
}
void
cprintf(const char *fmt, ...)
{
......
#include "types.h"
#include "kernel.h"
void
iderw(struct buf *b)
{
panic("iderw");
}
#if 0
// Simple PIO-based (non-DMA) IDE driver code.
#include "types.h"
......@@ -159,3 +169,4 @@ iderw(struct buf *b)
release(&idelock);
}
#endif
......@@ -13,6 +13,12 @@ struct spinlock;
struct condvar;
struct proc;
// bio.c
void binit(void);
struct buf* bread(u32, u64, int writer);
void brelse(struct buf*, int writer);
void bwrite(struct buf*);
// cga.c
void cgaputc(char c);
......@@ -24,10 +30,16 @@ void cv_wakeup(struct condvar *cv);
// console.c
void cprintf(const char*, ...);
void panic(const char*) __attribute__((noreturn));
void snprintf(char *buf, u32 n, char *fmt, ...);
// fs.c
int namecmp(const char*, const char*);
// ide.c
void ideinit(void);
void ideintr(void);
void iderw(struct buf*);
// kalloc.c
char* kalloc(void);
void kfree(void *);
......
......@@ -13,6 +13,7 @@ extern void inittrap(void);
extern void initkalloc(void);
extern void initrcu(void);
extern void initproc(void);
extern void initbio(void);
void
cmain(void)
......@@ -29,7 +30,13 @@ cmain(void)
initkalloc();
initrcu(); // initialize rcu module
initproc();
initproc(); // process table
initbio(); // buffer cache
#if 0
fileinit(); // file table
iinit(); // inode cache
ideinit(); // disk
#endif
cprintf("ncpu %d\n", ncpu);
panic("end");
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论