From 3b8d3c1f0aa32e519c03db233a483a948a2c937b Mon Sep 17 00:00:00 2001 From: Liu Aleaxander Date: Mon, 24 May 2010 21:34:24 +0800 Subject: [PATCH] Fixes on fs and ram disk Signed-off-by: Liu Aleaxander --- bochsrc | 2 +- fs/dir.c | 13 ++++++------ fs/ext2.c | 65 ++------------------------------------------------------ fs/inode.c | 9 ++++---- fs/ls.c | 2 +- fs/namei.c | 13 ++++++------ fs/ram_mke2fs.c | 13 ++++++------ fs/ramdisk.c | 32 +++++++++++++++++++++------- include/rd.h | 8 ++++--- include/string.h | 10 +++++++++ 10 files changed, 68 insertions(+), 99 deletions(-) diff --git a/bochsrc b/bochsrc index 8bbd324..61d86a4 100755 --- a/bochsrc +++ b/bochsrc @@ -9,7 +9,7 @@ megs: 32 #gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0 # filename of ROM images -romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xf0000 +romimage: file=/usr/share/bochs/BIOS-bochs-latest vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest # what disk images will be used diff --git a/fs/dir.c b/fs/dir.c index 07e1a91..dcbbd3a 100644 --- a/fs/dir.c +++ b/fs/dir.c @@ -9,6 +9,7 @@ * Aleaxander@gmail.com */ +#include #include #include @@ -23,12 +24,12 @@ void ext2_make_empty_dir(struct m_inode *dir, struct m_inode *parent) dot.inode = dir->i_num; dot.name_len = 1; dot.rec_len = EXT2_DIR_REC_LEN(1); - _strcpy(dot.name, "."); + strcpy(dot.name, "."); ddot.inode = parent->i_num; ddot.name_len = 2; ddot.rec_len = EXT2_DIR_REC_LEN(2); - _strcpy(ddot.name, ".."); + strcpy(ddot.name, ".."); ext2_add_entry(dir, &dot); ext2_add_entry(dir, &ddot); @@ -57,7 +58,7 @@ int ext2_dir_empty(struct m_inode *dir) dir_block ++; } - if (_strcmp(de->name, ".") || _strcmp(de->inode, "..") ) + if (strcmp(de->name, ".") || strcmp(de->inode, "..") ) return 0; i ++; if ( i > 2) @@ -83,7 +84,7 @@ void mkdir(char *pathname) */ dir_name = pathname; - file_name = _strchr(pathname,'/'); + file_name = strchr(pathname,'/'); if (! file_name) { printk("error: please input the absolute pathname\n"); return ; @@ -103,9 +104,9 @@ void mkdir(char *pathname) entry.inode = inode->i_num; - entry.name_len = _strlen(file_name); + entry.name_len = strlen(file_name); entry.rec_len = EXT2_DIR_REC_LEN(entry.name_len); - _strcpy(entry.name, file_name); + strcpy(entry.name, file_name); EXT2_DEBUG(printk("the new entry's name is %s\n",entry.name)); diff --git a/fs/ext2.c b/fs/ext2.c index d827952..2fc394d 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -5,79 +5,18 @@ */ #include -//#include +#include #include #include extern int vsprintf (char *buf, const char *fmt, va_list args); - -char * _strcpy (char *dest, const char *src) -{ - char *p = dest; - - while ((*p++ = *src++) != '\0') - ; - - return dest; -} - - -int _strcmp(const char *s1, const char *s2) -{ - while ((*s1 == *s2) && *s1 != '\0') { - s1++; - s2++; - } - - return (*s1 - *s2); -} - -char *_strchr (const char *s, int c) -{ - while (*s) { - if (*s == c) - return (char *) s; - s++; - } - - return (void *)0; -} - -void * _memcpy(void *dest, const void *src, int count) -{ - char *tmp = dest; - const char *s = src; - - while (count--) - *tmp++ = *s++; -} - void * copy_block(void *dest, const void *src) { - _memcpy(dest, src, EXT2_BLOCK_SIZE); -} - - - -int _strlen(const char *s) -{ - const char *sc; - - for (sc = s; *sc; ++sc) - /* nothing */; - return sc - s; + memcpy(dest, src, EXT2_BLOCK_SIZE); } -void * _memset(void *s, int c, int count) -{ - char *xs = s; - - while (count--) - *xs++ = c; - return s; -} int is_print(char c) { return ((c >= '!') && (c <= '~') ); diff --git a/fs/inode.c b/fs/inode.c index 9ab7f46..a19b05d 100644 --- a/fs/inode.c +++ b/fs/inode.c @@ -7,6 +7,7 @@ #include #include +#include static int _bmap(struct m_inode * inode,int block,int create) @@ -123,7 +124,7 @@ static void ext2_read_inode (struct m_inode * inode) { struct ext2_inode *raw_inode = ext2_get_inode (inode->i_num); - _memcpy (&inode->inode, raw_inode, sizeof(struct ext2_inode)); + memcpy (&inode->inode, raw_inode, sizeof(struct ext2_inode)); } @@ -131,7 +132,7 @@ static void ext2_write_inode (struct m_inode * inode) { struct ext2_inode *raw_inode = ext2_get_inode (inode->i_num); - _memcpy (raw_inode, &inode->inode, sizeof(struct ext2_inode)); + memcpy (raw_inode, &inode->inode, sizeof(struct ext2_inode)); } #ifdef RAM_EXT2_FS @@ -160,7 +161,7 @@ struct m_inode * ext2_iget (int inr) space (just for a pointer to inode). so the follwing instruction : - _memset(inode, 0, sizeof (struct m_inode)); + memset(inode, 0, sizeof (struct m_inode)); will cover others data, then break into some unknow conditions . @@ -180,7 +181,7 @@ struct m_inode * ext2_iget (int inr) EXT2_DEBUG(); - _memset (&inode, 0, sizeof (struct m_inode)); + memset (&inode, 0, sizeof (struct m_inode)); inode->i_num = inr; ext2_read_inode (&inode); diff --git a/fs/ls.c b/fs/ls.c index b5d6c99..e64221c 100644 --- a/fs/ls.c +++ b/fs/ls.c @@ -36,7 +36,7 @@ void ls(char *pathname,char *arg) } - if (_strcmp(arg, "-l") == 0) { + if (strcmp(arg, "-l") == 0) { printk("%-16s%d\n",de->name,de->inode); goto next; } diff --git a/fs/namei.c b/fs/namei.c index f452d11..d99c998 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -10,6 +10,7 @@ */ #include #include +#include /* @@ -25,7 +26,7 @@ static inline int ext2_match_entry (int len, const char * const name, return 0; if (!de->inode) return 0; - return !_strcmp(name, de->name, len); + return !strncmp(name, de->name, len); } @@ -105,7 +106,7 @@ void ext2_add_entry (struct m_inode *dir_inode, struct ext2_dir_entry *dir) if (de->name == NULL || de->inode == 0) break; - if ( !_strcmp(de->name, dir->name) ) { + if ( !strcmp(de->name, dir->name) ) { printk("%s: already exist\n", dir->name); return; } @@ -116,9 +117,9 @@ void ext2_add_entry (struct m_inode *dir_inode, struct ext2_dir_entry *dir) } de->inode = dir->inode; - de->name_len = _strlen(dir->name); + de->name_len = strlen(dir->name); de->rec_len = EXT2_DIR_REC_LEN(de->name_len); - _strcpy(de->name, dir->name); + strcpy(de->name, dir->name); EXT2_DEBUG(printk("the new added entry's name is %s\n",de->name)); @@ -151,13 +152,13 @@ struct m_inode * ext2_namei(char *pathname) p = pathname; while (*pathname!='/'&&*pathname) pathname++; - _memcpy (thisname, p, pathname - p); + memcpy (thisname, p, pathname - p); thisname[pathname-p] = '\0'; EXT2_DEBUG(printk("thisname %s\n",thisname)); - if (de = ext2_find_entry(working_inode, thisname, _strlen(thisname))) { + if (de = ext2_find_entry(working_inode, thisname, strlen(thisname))) { inode = ext2_iget(de->inode); ext2_iput(working_inode); working_inode = inode; diff --git a/fs/ram_mke2fs.c b/fs/ram_mke2fs.c index d69c741..b0b41d6 100644 --- a/fs/ram_mke2fs.c +++ b/fs/ram_mke2fs.c @@ -16,9 +16,8 @@ #include #include - -extern unsigned long ram_start; -extern unsigned long ram_end; +#include +#include void ram_ext2_init_super_block() @@ -95,7 +94,7 @@ void ram_ext2_init_root_inode() inode_table = ram_start + EXT2_BLOCK_SIZE * 10; root_inode = (struct ext2_inode *)inode_table + (ROOT_INODE - 1) ; - _memset(root_inode, 0, sizeof (struct ext2_inode)); + memset(root_inode, 0, sizeof (struct ext2_inode)); root_inode->i_size = 0x400; /* 1K */ root_inode->i_links_count = 3; @@ -148,12 +147,12 @@ void ram_mke2fs() block = root_inode->inode.i_block[0]; block_buffer = (void *)(ram_start + EXT2_BLOCK_SIZE * block); - _memcpy(block_buffer, &root, EXT2_DIR_REC_LEN(1)); + memcpy(block_buffer, &root, EXT2_DIR_REC_LEN(1)); block_buffer += EXT2_DIR_REC_LEN(1); - _memcpy(block_buffer, &root_dot_dot, EXT2_DIR_REC_LEN(1)); + memcpy(block_buffer, &root_dot_dot, EXT2_DIR_REC_LEN(1)); block_buffer += EXT2_DIR_REC_LEN(2); - _memcpy(block_buffer, &lost, EXT2_DIR_REC_LEN(10)); + memcpy(block_buffer, &lost, EXT2_DIR_REC_LEN(10)); } diff --git a/fs/ramdisk.c b/fs/ramdisk.c index f807706..041887c 100644 --- a/fs/ramdisk.c +++ b/fs/ramdisk.c @@ -1,19 +1,20 @@ #include #include -//#include +#include +#include unsigned long ram_start; -unsigned long ram_end; +static unsigned long ram_end; void inline rd_read(void *mem, void *ram, int block) { - _memcpy(mem, ram, block << 10); + memcpy(mem, ram, RD_BLOCK_SIZE); } void inline rd_write(void *ram, void *mem, int block) { - _memcpy(ram, mem, block << 10); + memcpy(ram, mem, RD_BLOCK_SIZE); } void inline rd_read_block(void *mem, void *ram) @@ -35,19 +36,34 @@ void * bread (unsigned int block) return (void*)ram; } + +void bwrite(unsigned int block, char *buf) +{ + unsigned long ram; + ram = ram_start + block * EXT2_BLOCK_SIZE; + + memcpy(ram, buf, EXT2_BLOCK_SIZE); +} #else void * bread (unsigned int block) { - void *block_buffer[1024]; + static void block_buffer[1024]; void *ram; - //EXT2_DEBUG(); - - ram = ram_start + block * EXT2_BLOCK_SIZE; + ram = ram_start + block * RD_BLOCK_SIZE; rd_read_block(block_buffer,ram); return block_buffer; } + +void bwrite(unsigned int block, char *buf) +{ + unsigned long ram; + ram = ram_start + block * RD_BLOCK_SIZE; + + rd_write_block((void *)ram, buf); +} + #endif /* bread */ diff --git a/include/rd.h b/include/rd.h index eef76e6..7e7dba0 100644 --- a/include/rd.h +++ b/include/rd.h @@ -2,12 +2,14 @@ #define _RD_H -extern unsigned long ram_start; - -extern void * bread(unsigned int ); +#define RD_BLOCK_SHIFT 10 +#define RD_BLOCK_SIZE ( 1 << RD_BLOCK_SHIFT ) +extern unsigned long ram_start; +extern void * bread(unsigned int); +extern void bwrite(unsigned int, char *); #endif /* rd.h */ diff --git a/include/string.h b/include/string.h index 2454457..a4f6b61 100644 --- a/include/string.h +++ b/include/string.h @@ -17,6 +17,16 @@ static int strcmp(const char *s1, const char *s2) return (*s1 - *s2); } +static int strncmp(const char *s1, const char *s2, int n) +{ + while ((--n) && (*s1 == *s2) && *s1 != '\0') { + s1++; + s2++; + } + + return (*s1 - *s2); +} + static char *strcpy(char *dest, const char *src) { -- 2.11.4.GIT