From 7b63bf1d27d31f3b2437ea4da654b85f1fc225d4 Mon Sep 17 00:00:00 2001 From: Marian Ohligs Date: Tue, 5 Jul 2011 14:34:36 +0200 Subject: [PATCH] !!!!! -> increasing the offset has to be done inside the fs/chardriver. - rewrite the initrd_read/write, char_driver r/w. Now all functions use fildes_t instead of vfs_node_t structs. - eleminate some compiler warnings - some changes in open, to reduce compiler warnings. This function isn't working yet! --- arch/x86/include/asm/kb.h | 4 +++- arch/x86/kernel/kb.c | 5 +++- drivers/char/null.c | 5 ++-- drivers/stderr/stderr.c | 6 +++-- drivers/stdin/stdin.c | 6 ++--- drivers/stdout/stdout.c | 6 +++-- fs/fs.c | 6 ++--- fs/initrd.c | 50 +++++++++++++++++++++++++++------------ include/metalsvm/fs.h | 8 +++---- kernel/syscall.c | 22 ++++------------- 10 files changed, 67 insertions(+), 51 deletions(-) diff --git a/arch/x86/include/asm/kb.h b/arch/x86/include/asm/kb.h index 99991d8b..1b2dfb40 100644 --- a/arch/x86/include/asm/kb.h +++ b/arch/x86/include/asm/kb.h @@ -51,7 +51,9 @@ typedef struct tid_t tid; } kb_buffer_t; -kb_buffer_t kb_buffer; +extern kb_buffer_t kb_buffer; + +void kb_flush(); #endif diff --git a/arch/x86/kernel/kb.c b/arch/x86/kernel/kb.c index 70911518..9499d9d8 100644 --- a/arch/x86/kernel/kb.c +++ b/arch/x86/kernel/kb.c @@ -17,8 +17,10 @@ * This file is part of MetalSVM. */ +#include #include #include +#include #include #include @@ -26,12 +28,13 @@ kb_buffer_t kb_buffer = {NULL, 0, 0, 0 }; -kb_flush() { +void kb_flush() { kfree(kb_buffer.buffer, (kb_buffer.maxsize * sizeof(char))); kb_buffer.buffer = NULL; kb_buffer.size = 0; kb_buffer.maxsize = 0; kb_buffer.tid = 0; + return; } /* diff --git a/drivers/char/null.c b/drivers/char/null.c index 4ff7d068..f24fc593 100644 --- a/drivers/char/null.c +++ b/drivers/char/null.c @@ -26,14 +26,15 @@ /* Implementation of a simple null device */ -static ssize_t null_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t null_read(fildes_t* file, uint8_t* buffer, size_t size) { memset(buffer, 0x00, size); + file->offset += size; return size; } -static ssize_t null_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t null_write(fildes_t* file, uint8_t* buffer, size_t size) { return size; } diff --git a/drivers/stderr/stderr.c b/drivers/stderr/stderr.c index 3bcd869e..32a926e8 100644 --- a/drivers/stderr/stderr.c +++ b/drivers/stderr/stderr.c @@ -26,18 +26,20 @@ /* Implementation of a simple stderr device */ -static ssize_t stderr_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t stderr_read(fildes_t* file, uint8_t* buffer, size_t size) { return size; } -static ssize_t stderr_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t stderr_write(fildes_t* file, uint8_t* buffer, size_t size) { kprintf("\nError: "); int i; for (i = 0; ioffset += size; return size; } diff --git a/drivers/stdin/stdin.c b/drivers/stdin/stdin.c index ffdcd964..6a621156 100644 --- a/drivers/stdin/stdin.c +++ b/drivers/stdin/stdin.c @@ -25,10 +25,9 @@ #include #include - /* Implementation of a simple stdin device */ -static ssize_t stdin_read(vfs_node_t* node, const uint8_t* buffer, size_t size, off_t offset) +static ssize_t stdin_read(fildes_t* file, uint8_t* buffer, size_t size) { kb_buffer.buffer = kmalloc(size * sizeof(char)); kb_buffer.maxsize = size; @@ -44,10 +43,11 @@ static ssize_t stdin_read(vfs_node_t* node, const uint8_t* buffer, size_t size, kb_flush(); //kprintf("Size: %i, offset: %i, buffer: %s", size, buffer, offset); + file->offset += size; return size; } -static ssize_t stdin_write(vfs_node_t* node, const uint8_t* buffer, size_t size, off_t offset) +static ssize_t stdin_write(fildes_t* file, uint8_t* buffer, size_t size) { return size; } diff --git a/drivers/stdout/stdout.c b/drivers/stdout/stdout.c index baed88bc..c68eb588 100644 --- a/drivers/stdout/stdout.c +++ b/drivers/stdout/stdout.c @@ -26,17 +26,19 @@ /* Implementation of a simple stdout device */ -static ssize_t stdout_read(vfs_node_t* node, const uint8_t* buffer, size_t size, off_t offset) +static ssize_t stdout_read(fildes_t* file, uint8_t* buffer, size_t size) { return size; } -static ssize_t stdout_write(vfs_node_t* node, const uint8_t* buffer, size_t size, off_t offset) +static ssize_t stdout_write(fildes_t* file, uint8_t* buffer, size_t size) { int i; for (i = 0; ioffset += size; return size; } diff --git a/fs/fs.c b/fs/fs.c index f8994beb..53488e97 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -36,7 +36,7 @@ ssize_t read_fs(fildes_t* file, uint8_t* buffer, size_t size) spinlock_lock(&node->lock); // Has the node got a read callback? if (node->read != 0) - ret = node->read(node, buffer, size, file->offset); + ret = node->read(file, buffer, size); spinlock_unlock(&node->lock); return ret; @@ -53,7 +53,7 @@ ssize_t write_fs(fildes_t* file, uint8_t* buffer, size_t size) spinlock_lock(&node->lock); // Has the node got a write callback? if (node->write != 0) - ret = node->write(node, buffer, size, file->offset); + ret = node->write(file, buffer, size); spinlock_unlock(&node->lock); return ret; @@ -96,7 +96,7 @@ vfs_node_t* open_fs(const char* name, int flags) spinlock_lock(&node->lock); // Has the node got an open callback? if (node->open != 0) - ret = node->open(node, (uint8_t*) fname); + ret = node->open(node); spinlock_unlock(&node->lock); return ret; diff --git a/fs/initrd.c b/fs/initrd.c index 54d0a7c3..0c49cac8 100644 --- a/fs/initrd.c +++ b/fs/initrd.c @@ -55,12 +55,17 @@ typedef struct { char fname[MAX_FNAME]; } initrd_file_desc_t; -static ssize_t initrd_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t initrd_read(fildes_t* file, uint8_t* buffer, size_t size) { uint32_t i, pos = 0, found = 0; + off_t offset = 0; char* data = NULL; + vfs_node_t* node = file->node; block_list_t* blist = &node->block_list; + /* init the tmp offset */ + offset = file->offset; + /* searching for the valid data block */ if (offset) { pos = offset / node->block_size; @@ -87,24 +92,32 @@ static ssize_t initrd_read(vfs_node_t* node, uint8_t* buffer, size_t size, off_t * The user has to restart the read operation * for the next block. */ - if (offset+size >= node->block_size) + if ((offset + size) >= node->block_size) size = node->block_size - offset; memcpy(buffer, data + offset, size); + file->offset += size; return size; } -static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_t offset) +static ssize_t initrd_write(fildes_t* file, uint8_t* buffer, size_t size) { uint32_t i, pos = 0, found = 0; - off_t nodeoffset = offset; + off_t offset = 0; char* data = NULL; + vfs_node_t* node = file->node; block_list_t* blist = &node->block_list; if (BUILTIN_EXPECT(node->type != FS_FILE, 0)) return 0; + if (file->flags & O_APPEND) + file->offset = node->block_size; + + /* init the tmp offset */ + offset = file->offset; + /* searching for the valid data block */ if (offset) { pos = offset / MAX_DATAENTRIES; @@ -116,9 +129,11 @@ static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_ if ((size + offset) >= MAX_DATAENTRIES) size = MAX_DATAENTRIES - offset; if(!blist->data[i]) { - blist->data[i] = (data_block_t*) kmalloc(sizeof(data_block_t)); + blist->data[i] = (data_block_t*) + kmalloc(sizeof(data_block_t)); if (blist->data[i]) - memset(blist->data[i], 0x00, sizeof(data_block_t)); + memset(blist->data[i], 0x00, + sizeof(data_block_t)); } found++; if (found > pos) { @@ -127,16 +142,18 @@ static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_ } if (!blist->next) { - blist->next = (block_list_t*) kmalloc(sizeof(block_list_t)); + blist->next = (block_list_t*) + kmalloc(sizeof(block_list_t)); if (blist->next) - memset(blist->next, 0x00, sizeof(block_list_t)); + memset(blist->next, 0x00, + sizeof(block_list_t)); } blist = blist->next; } while(blist && !data); /* you may have to increase nodesize */ - if (node->block_size < (nodeoffset + size)) - node->block_size = nodeoffset + size; + if (node->block_size < (file->offset + size)) + node->block_size = file->offset + size; /* * If the data block is not large engough, * we copy only the rest of the current block. @@ -144,20 +161,22 @@ static ssize_t initrd_write(vfs_node_t* node, uint8_t* buffer, size_t size, off_ * for the next block. */ memcpy(data + offset, buffer, size); + file->offset += size; return size; } -static vfs_node_t* initrd_open(vfs_node_t* node, uint8_t* name) +static int initrd_open(vfs_node_t* node) { + uint32_t i, j; char* data = NULL; block_list_t* blist = NULL; if (BUILTIN_EXPECT(node->type != FS_DIRECTORY, 0)) { - /*CREATE FILE */ + /* CREATE FILE */ vfs_node_t* new_node = kmalloc(sizeof(vfs_node_t)); if (BUILTIN_EXPECT(!new_node, 0)) - return NULL; + return 0; blist = &node->block_list; dir_block_t* dir_block; @@ -179,7 +198,7 @@ static vfs_node_t* initrd_open(vfs_node_t* node, uint8_t* name) dirent = &dir_block->entries[j]; if (!dirent->vfs_node) { dirent->vfs_node = new_node; - strncpy(dirent->name, (char*) name, MAX_FNAME); + //strncpy(dirent->name, (char*) name, MAX_FNAME); goto exit_for; /* there might be a better Solution *********************************/ } } @@ -217,7 +236,8 @@ exit_for: node->block_size = 0; //&node->block_list = NULL; - return node; + //return node; + return 0; } diff --git a/include/metalsvm/fs.h b/include/metalsvm/fs.h index a515dea0..1584477f 100644 --- a/include/metalsvm/fs.h +++ b/include/metalsvm/fs.h @@ -69,7 +69,7 @@ #endif struct vfs_node; - +struct fildes; /** @defgroup fsprototypes FS access function prototypes * * These typedefs define the type of callbacks - called when read/write/open/close are called.\n @@ -79,11 +79,11 @@ struct vfs_node; */ /** @brief Read function pointer */ -typedef ssize_t (*read_type_t) (struct vfs_node *, uint8_t*, size_t, off_t); +typedef ssize_t (*read_type_t) (struct fildes *, uint8_t*, size_t); /** @brief Write function pointer */ -typedef ssize_t (*write_type_t) (struct vfs_node *, uint8_t*, size_t, off_t); +typedef ssize_t (*write_type_t) (struct fildes *, uint8_t*, size_t); /** @brief Open function pointer */ -typedef struct vfs_node *(*open_type_t) (struct vfs_node *, uint8_t); +typedef int (*open_type_t) (struct vfs_node *); /** @brief Close function pointer */ typedef int (*close_type_t) (struct vfs_node *); /** @brief Read directory function pointer */ diff --git a/kernel/syscall.c b/kernel/syscall.c index d0031e57..ff105ecb 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -29,16 +29,8 @@ static int sys_write(int fd, const char *buf, size_t len) { - unsigned int wrotebytes; - if (per_core(current_task)->fildes_table[fd].flags & O_APPEND) { - per_core(current_task)->fildes_table[fd].offset = per_core(current_task)->fildes_table[fd].node->block_size; - } - - wrotebytes = write_fs(&(per_core(current_task)->fildes_table[fd]), (uint8_t*)buf, len); - //kprintf("writing into filesystem -- fd:%i, Filelength:%i, Writtenbytes: %i, Bufferlength: %s \n", fd, len, wrotebytes, buf); - per_core(current_task)->fildes_table[fd].offset += wrotebytes; - - return wrotebytes; + return write_fs(&(per_core(current_task)->fildes_table[fd]), + (uint8_t*)buf, len); } static int sys_open(const char* file, int flags, int mode) @@ -81,14 +73,8 @@ static int sys_close(int fd) static int sys_read(int fd, const char *buf, size_t len) { - unsigned int readbytes; - readbytes = read_fs( - &(per_core(current_task)->fildes_table[fd]), - (uint8_t*)buf, len); - per_core(current_task)->fildes_table[fd].offset += readbytes; - /*kprintf("fd:%i, Filelength:%i, Bufferlength: %s X: %i\n", fd, len, buf, readbytes); */ - /* Beware: still reading above file limit! */ - return readbytes; + return read_fs(&(per_core(current_task)->fildes_table[fd]), + (uint8_t*)buf, len); } static int sys_lseek(int fd, off_t pos, int origin)