From ce91afe04bd24fe9277a3f7b68ddf0654f1634ac Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Sun, 23 Dec 2018 17:22:14 +1100 Subject: Corrected a bug in the hard-disk driver where calls to hd_read_block() after hd_init() failed (on a system without a disk), would hang. Now, hd_read_block() will fail if no hard disk is present on the system (as indicated by the nblocks count being equal to zero). The same fix also applies to the hd_write_block() function. The hd_init() function now returns a status indicating either successful drive detection and initialization, or failure. This return status won't likely be needed due to the above bug-fix, but may prove useful in the future. Added framework for a block buffer subsystem. This subsystem uses pre-allocated memory to cache blocks that are requested from the block device subsystem. Cached blocks are stored on a linked list sorted in order of usage frequency. Modified the block read/write functions so that they no longer accept a length parameter. The block I/O functions will only read or write a single block at a time. If multiple blocks are required, multiple calls will have to be made. This is to reduce complexity of block device drivers and make integration with the new buffer subsystem easier. Removed all calls to the ATA hard-disk driver. For now, it seems that floppy media will be best as it allows for real-world hardware testing. Furthermore, large portions of the hard-disk driver will need to be re-written anyway once the block buffer subsystem is complete as it only supports PIO transfers, whilst a buffer system will require DMA transfers. As the hard-disk was previously the only supported block device, the block device read/write functions will now always fail, returning -1. --- kernel/fs/block.c | 9 +++--- kernel/fs/buffer.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ kernel/fs/fs.c | 5 ++-- kernel/fs/mount.c | 6 ++-- 4 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 kernel/fs/buffer.c (limited to 'kernel/fs') diff --git a/kernel/fs/block.c b/kernel/fs/block.c index b3f7386..caf97d2 100644 --- a/kernel/fs/block.c +++ b/kernel/fs/block.c @@ -1,10 +1,9 @@ -#include #include -size_t block_read(void *buf, size_t block, size_t len) { - return hd_read_block(buf, block, len); +int block_read(void *buf, size_t block) { + return -1; } -size_t block_write(void *buf, size_t block, size_t len) { - return hd_write_block(buf, block, len); +int block_write(void *buf, size_t block) { + return -1; } diff --git a/kernel/fs/buffer.c b/kernel/fs/buffer.c new file mode 100644 index 0000000..7405738 --- /dev/null +++ b/kernel/fs/buffer.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +/* memory dedicated to the block cache */ +#define BSTART 0x80000 +#define BEND 0xA0000 + +static struct buffer *lru; +static struct buffer *mru; + +struct buffer *buffer_get_block(uint16_t device, uint16_t block); + +void buffer_init(void) { + int c = 0; + struct buffer *last = NULL; + struct buffer *b = (struct buffer*) BSTART; + void *d = (void*) (BEND - BLOCK_SIZE); + + while(d > (void*) b) { + memset(b, 0, sizeof(struct buffer)); + + b->b_data = d; + b->b_prev = last; + + if(last) + last->b_next = b; + + last = b; + + b = (struct buffer*) ((void*) b + sizeof(struct buffer)); + d = (void*) (d - (void*) BLOCK_SIZE); + + c++; + } + + lru = (struct buffer*) BSTART; + mru = last; + + printk("[buf] Buffers initialized (0x%x buffers in pool)\n", c); +} + +struct buffer *buffer_get_block(uint16_t device, uint16_t block) { + int ret; + struct buffer **p = &mru; + struct buffer *b; + + /* check if the requested block is already in the cache */ + while(*p) { + if((*p)->b_device == device && (*p)->b_block == block) { + /* put the buffer back at the MRU end of the list */ + (*p)->b_next = NULL; + (*p)->b_prev = mru; + mru = *p; + return *p; + } + p = &(*p)->b_prev; + } + + /* find a buffer at the end of the LRU list */ + b = lru; + lru = lru->b_next; + lru->b_prev = NULL; + ret = block_read(b->b_data, block); + /* abort if we failed to read in the block */ + if(ret < 0) { + /* release the allocated buffer to the pool */ + lru->b_prev = b; + b->b_next = lru; + b->b_prev = NULL; + lru = b; + return NULL; + } + b->b_device = device; + b->b_block = block; + /* put the buffer back at the MRU end of the list */ + b->b_next = NULL; + b->b_prev = mru; + mru = b; + return b; +} diff --git a/kernel/fs/fs.c b/kernel/fs/fs.c index 792668e..e0d9205 100644 --- a/kernel/fs/fs.c +++ b/kernel/fs/fs.c @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -22,7 +21,9 @@ void fs_init(void) { memset(&sblocks, 0, sizeof(sblocks)); memset(&ftable, 0, sizeof(ftable)); - hd_init(); + buffer_init(); + + /* hd_init(); */ mount_root(); } diff --git a/kernel/fs/mount.c b/kernel/fs/mount.c index 47911fb..9cdf49b 100644 --- a/kernel/fs/mount.c +++ b/kernel/fs/mount.c @@ -6,12 +6,12 @@ struct super_block sblocks[NRSUPER]; void mount_root(void) { - size_t ret; + int ret; char buf[BLOCK_SIZE]; struct super_block *s = (void*) buf; - ret = block_read(buf, 1, 1); - if(ret < 1) { + ret = block_read(buf, 1); + if(ret < 0) { printk("[fs] Failed to read super block\n"); return; } -- cgit v1.3