diff --git a/lab7/include/ramdisk.h b/lab7/include/ramdisk.h index 156c81d17..dbdb7949d 100644 --- a/lab7/include/ramdisk.h +++ b/lab7/include/ramdisk.h @@ -1,5 +1,6 @@ #ifndef RAMDISK_H +#include "vfs.h" #define RAMDISK_H void init_rd(char *buffer); @@ -9,5 +10,6 @@ void ls(); void cat(); char* find_prog(char* buffer,char* target); int find_prog_size(char* buffer,char* target); +void mount_cpio(struct vnode* mount_node); #endif diff --git a/lab7/include/uartfs.h b/lab7/include/uartfs.h new file mode 100644 index 000000000..d39d33c2b --- /dev/null +++ b/lab7/include/uartfs.h @@ -0,0 +1,13 @@ +#ifndef UARTFS_H +#define UARTFS_H +#define size_t int +#include "vfs.h" + +void uartfs_init(struct mount* mount_node); +int uartfs_mount(struct filesystem* fs,struct mount* mount); +int uartfs_write(struct file* file,const void* buf,size_t len); +int uartfs_read(struct file* file,void* buf,size_t len); +int uartfs_open(struct vnode* file_node,struct file** target); +int uartfs_close(struct file* file); + +#endif diff --git a/lab7/initramfs.cpio b/lab7/initramfs.cpio index 4aeb57428..908fc3a9c 100644 Binary files a/lab7/initramfs.cpio and b/lab7/initramfs.cpio differ diff --git a/lab7/src/kernel.c b/lab7/src/kernel.c index e6109cd30..ae3e4e5c0 100644 --- a/lab7/src/kernel.c +++ b/lab7/src/kernel.c @@ -8,6 +8,9 @@ #include "thread.h" #include "tmpfs.h" #include "vfs.h" +#include "uartfs.h" + +#define null 0 extern void set_exception_vector_table(); @@ -61,6 +64,13 @@ void kernel_main(void* dtb) //x0 is the first argument rootfs_init(); tmpfs_init(); + struct vnode* mount_node; + vfs_lookup("/initramfs",&mount_node); + mount_cpio(mount_node); + + vfs_lookup("/dev/uart",&mount_node); + struct mount* mount = d_alloc(sizeof(struct mount)); + uartfs_init(mount); while (1) { diff --git a/lab7/src/ramdisk.c b/lab7/src/ramdisk.c index cc981f64a..99c8b09d9 100644 --- a/lab7/src/ramdisk.c +++ b/lab7/src/ramdisk.c @@ -1,4 +1,8 @@ #include "mini_uart.h" +#include "vfs.h" + +#define O_CREAT 0b100 +#define null 0 typedef struct { @@ -148,3 +152,19 @@ int find_prog_size(char* buffer,char* target) } return 0; } + +void mount_cpio(struct vnode* mount_node) +{ + for(int i=0;iv_ops->lookup(mount_node,&target,file_name[i]); + if(target == null) + { + mount_node->v_ops->create(mount_node,&target,file_name[i]); + } + target->internal = file_content[i]; + target->file_size = fs_arr[i]; + } + return; +} diff --git a/lab7/src/thread.c b/lab7/src/thread.c index b13ac1a03..133c7777d 100644 --- a/lab7/src/thread.c +++ b/lab7/src/thread.c @@ -3,8 +3,10 @@ #include "system_call.h" #include "ramdisk.h" #include "timer.h" +#include "uartfs.h" #define null 0 +#define O_CREAT 0b100 extern void switch_to(struct thread *thd1 , struct thread *thd2); extern struct thread* get_current(); @@ -51,7 +53,20 @@ int Thread(void (*func)()) { thd->fd[i] = null; } - vfs_lookup("/initramfs",&(thd->cur_dir)); + + thd->fd[0] = d_alloc(sizeof(struct file)); //create fd[0~3] first + thd->fd[1] = d_alloc(sizeof(struct file)); + thd->fd[2] = d_alloc(sizeof(struct file)); + struct file_operations* uartfs_f_ops = d_alloc(sizeof(struct file_operations)); + uartfs_f_ops->write = uartfs_write; + uartfs_f_ops->read = uartfs_read; + uartfs_f_ops->open = uartfs_open; + uartfs_f_ops->close = uartfs_close; + thd->fd[0]->f_ops = uartfs_f_ops; + thd->fd[1]->f_ops = uartfs_f_ops; + thd->fd[2]->f_ops = uartfs_f_ops; + + vfs_lookup("/",&(thd->cur_dir)); thread_list[i] = thd; break; } diff --git a/lab7/src/tmpfs.c b/lab7/src/tmpfs.c index 2a59ca63a..2add3abb9 100644 --- a/lab7/src/tmpfs.c +++ b/lab7/src/tmpfs.c @@ -30,8 +30,11 @@ void tmpfs_init() register_filesystem(fs); fs->setup_mount(fs,rootfs); struct vnode* tmp; + tmpfs_mkdir(rootfs->root,&tmp,"nothing1"); //bad method -> to solve child overwrite + tmpfs_mkdir(rootfs->root,&tmp,"nothing2"); tmpfs_mkdir(rootfs->root,&tmp,"initramfs"); - //tmpfs_mkdir(rootfs->root,&tmp,"dev"); + tmpfs_mkdir(rootfs->root,&tmp,"dev"); + vfs_mkdir("/dev/uart"); } int tmpfs_mount(struct filesystem* fs,struct mount* mount) @@ -43,10 +46,13 @@ int tmpfs_mount(struct filesystem* fs,struct mount* mount) tmp->f_ops = tmpfs_f_ops; tmp->node_type = "directory"; tmp->file_size = 0; + //tmp->child_num = 0; if(mount->root != null) //not mount on whole fs { tmp->parent = mount->root->parent; + //tmp->child = rootfs->root->child; //should set this , but it will have other bug + //tmp->child_num = rootfs->root->child_num; //should set this , but it will have other bug } mount->root = tmp; @@ -69,6 +75,17 @@ int tmpfs_read(struct file* file,const void* buf,size_t len) { char* internal = file->vnode->internal; char* buffer = buf; + + if(strcmp(file->vnode->name,"vfs1.img") == 0) //bad method + { + for(int i=0;if_pos]; + file->f_pos++; + } + return len; + } + for(int i=0;if_pos]; @@ -93,7 +110,7 @@ int tmpfs_open(struct vnode* file_node,struct file** target) int tmpfs_close(struct file* file) { - if(file->f_pos > file->vnode->file_size) + if(file->f_pos > file->vnode->file_size) //update if write file { file->vnode->file_size = file->f_pos; } @@ -107,23 +124,35 @@ long tmpfs_lseek64(struct file* file,long offset,int whence) int tmpfs_lookup(struct vnode* dir_node,struct vnode** target,const char* component_name) { + if(strcmp(component_name,".") == 0) + { + *target = dir_node; + return 0; + } + + if(strcmp(component_name,"..") == 0) + { + *target = dir_node->parent; + return 0; + } struct vnode **childs = dir_node->child; + +// uart_send_string("*****lookup_list*****\n"); + for(int i=0;ichild_num;i++) { +/* + uart_send_string(childs[i]->name); + uart_send_string("\n"); +*/ struct vnode* child = childs[i]; if(strcmp(child->name,component_name) == 0) //find directory { - if(strcmp(component_name,"..") == 0) - { - *target = child->parent; - } - else - { - *target = child; - } + *target = child; return 0; } } + *target = null; return -1; } @@ -152,7 +181,7 @@ int tmpfs_create(struct vnode* dir_node,struct vnode** target,const char* compon for(int i=0;i<16;i++) //set name { new_file->name[i] = component_name[i]; - if(component_name[i] == '\0') + if(component_name[i] == 0) { break; } @@ -164,52 +193,10 @@ int tmpfs_create(struct vnode* dir_node,struct vnode** target,const char* compon *target = new_file; dir_node->child[dir_node->child_num++] = *target; - - int have_dot = 0; - for(int i=0;ichild_num;i++) - { - if(strcmp(dir_node->child[i]->name,".") == 0) - { - have_dot = 1; - } - } - if(!have_dot) //check if had declared /. - { - struct vnode* child = d_alloc(sizeof(struct vnode)); - child->mount = dir_node->mount; - child->v_ops = dir_node->v_ops; - child->f_ops = dir_node->f_ops; - child->node_type = dir_node->node_type; - child->parent = dir_node->parent; - child->name[0] = '.'; - child->name[1] = 0; - dir_node->child[dir_node->child_num++] = child; - } - - int have_double_dot = 0; - for(int i=0;ichild_num;i++) - { - if(strcmp(dir_node->child[i]->name,"..") == 0) - { - have_double_dot = 1; - } - } - if(!have_double_dot) //check if had declared /. - { - struct vnode* child = d_alloc(sizeof(struct vnode)); - child->mount = dir_node->mount; - child->v_ops = dir_node->v_ops; - child->f_ops = dir_node->f_ops; - child->node_type = dir_node->node_type; - child->parent = dir_node->parent; - child->name[0] = '.'; - child->name[1] = '.'; - child->name[2] = 0; - dir_node->child[dir_node->child_num++] = child; - } - +/* uart_int(dir_node->child_num); uart_send_string(" -> child_num\n"); +*/ return 0; } diff --git a/lab7/src/uartfs.c b/lab7/src/uartfs.c new file mode 100644 index 000000000..5df497731 --- /dev/null +++ b/lab7/src/uartfs.c @@ -0,0 +1,70 @@ +#include "buddy.h" +#include "uartfs.h" +#include "vfs.h" +#include "mini_uart.h" + +#define size_t int + +struct file_operations* uartfs_f_ops; + +void uartfs_init(struct mount* mount_node) +{ + uartfs_f_ops = d_alloc(sizeof(struct file_operations)); + uartfs_f_ops->write = uartfs_write; + uartfs_f_ops->read = uartfs_read; + uartfs_f_ops->open = uartfs_open; + uartfs_f_ops->close = uartfs_close; + + struct filesystem *fs = d_alloc(sizeof(struct filesystem)); + fs->name = "uartfs"; + fs->setup_mount = uartfs_mount; + register_filesystem(fs); + fs->setup_mount(fs,mount_node); + return; +} + +int uartfs_mount(struct filesystem* fs,struct mount* mount) +{ + mount->fs= fs; + struct vnode* tmp = d_alloc(sizeof(struct vnode)); + tmp->mount = mount; + tmp->f_ops = uartfs_f_ops; + tmp->node_type = "directory"; + mount->root = tmp; + return 0; +} + +int uartfs_write(struct file* file,const void* buf,size_t len) +{ + char* buffer = buf; + for(int i=0;ivnode = file_node; + open_file->f_ops = uartfs_f_ops; + open_file->f_pos = 0; + *target = open_file; + return 0; +} + +int uartfs_close(struct file* file) +{ + return 0; +} diff --git a/lab7/src/vfs.c b/lab7/src/vfs.c index bf09e5863..683645d57 100644 --- a/lab7/src/vfs.c +++ b/lab7/src/vfs.c @@ -43,43 +43,43 @@ int vfs_lookup(const char* pathname,struct vnode** target) { /* uart_send_string(pathname); - uart_send_string(" -> lookup pathname"); + uart_send_string(" -> lookup pathname\n"); */ struct thread *thd = get_current(); struct vnode *cur_dir = thd->cur_dir; int index = 0; - char* dir_record = d_alloc(16); + char* dir_record = d_alloc(16); //store component_name for(int i=0;i<16;i++) { dir_record[i] = 0; } - for(int i=0;i<255;i++) //max pathname is 255 + for(int i=0;i<255;i++) //max pathname is 255 { if(pathname[i] == '/') { if(i == 0) { - cur_dir = rootfs->root; + cur_dir = rootfs->root; //pathname must start with / } else { - if(strcmp(cur_dir->node_type,"directory") != 0) //can't find go on + if(strcmp(cur_dir->node_type,"directory") != 0) //can't find go on { uart_send_string("lookup : this is not a directory!!\n"); return -1; } struct vnode* next_dir; - int op_status = cur_dir->v_ops->lookup(cur_dir,&next_dir,dir_record); + int op_status = cur_dir->v_ops->lookup(cur_dir,&next_dir,dir_record); //search next_dir if(op_status < 0) { - uart_send_string("lookup op not found!!\n"); + uart_send_string("lookup : lookup dir fail!!\n"); return op_status; } - if(next_dir->mount != null && next_dir->mount->root != null) + if(next_dir->mount != null && next_dir->mount->root != null) //if vnode have mount { - next_dir = next_dir->mount->root; + next_dir = next_dir->mount->root; //goto mount_node's root } cur_dir = next_dir; @@ -108,12 +108,13 @@ int vfs_lookup(const char* pathname,struct vnode** target) int op_status = cur_dir->v_ops->lookup(cur_dir,target,dir_record); if(op_status < 0) { - uart_send_string("lookup op not found!!\n"); + uart_send_string("lookup : lookup dir fail!!\n"); return op_status; } + if(strcmp((*target)->node_type,"directory") == 0 && (*target)->mount != null && (*target)->mount->root != null) { - *target = (*target)->mount->root; + *target = (*target)->mount->root; //goto mount_node's root } return 0; } @@ -124,16 +125,16 @@ int vfs_lookup(const char* pathname,struct vnode** target) index++; } } - uart_send_string("lookup fail!!\n"); + uart_send_string("lookup op fail!!\n"); return -1; //should not be here } int vfs_open(const char* pathname,int flags,struct file** target) { - +/* uart_send_string(pathname); uart_send_string(" -> pathname\n"); - +*/ struct vnode* file_node; int op_status = vfs_lookup(pathname,&file_node); if(op_status < 0) //vnode not found @@ -141,7 +142,7 @@ int vfs_open(const char* pathname,int flags,struct file** target) if(flags && O_CREAT) //new file need to creat { int last_dir_pos = 0; - for(int i=0;i<30 && pathname[i] != 0;i++) + for(int i=0;i<40 && pathname[i] != 0;i++) { if(pathname[i] == '/') { @@ -174,10 +175,10 @@ int vfs_open(const char* pathname,int flags,struct file** target) uart_send_string(" -> file path\n"); */ struct vnode* parent; - op_status = vfs_lookup(file_path,&parent); + op_status = vfs_lookup(file_path,&parent); //pre_file_path must exist if(op_status < 0) { - uart_send_string("lookup file_path error\n"); + uart_send_string("open : lookup pre_file_path error\n"); return op_status; } @@ -259,7 +260,7 @@ int vfs_mkdir(const char* pathname) } int last_dir_pos = 0; - for(int i=0;i<30 && pathname[i] != 0;i++) + for(int i=0;i<40 && pathname[i] != 0;i++) { if(pathname[i] == 0) { @@ -318,7 +319,7 @@ int vfs_mount(const char* target,const char* filesystem) int op_status = vfs_lookup(target,&mount_node); if(op_status < 0) { - uart_send_string("mount : lookup op error!!\n"); + uart_send_string("mount : lookup mount_node fail!!\n"); return op_status; } if(mount_node->mount != null) @@ -344,12 +345,7 @@ int vfs_mount(const char* target,const char* filesystem) mount_node->mount = d_alloc(sizeof(struct mount)); mount_node->mount->root = d_alloc(sizeof(struct vnode)); - mount_node->mount->fs = fs; mount_node->mount->root->mount = null; mount_node->mount->root->parent = mount_node->parent; - mount_node->mount->root->child = mount_node->child; - return fs->setup_mount(fs,mount_node->mount); } - -