Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

311552046 lab7 #123

Open
wants to merge 4 commits into
base: 311552046
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lab7/include/ramdisk.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef RAMDISK_H

#include "vfs.h"
#define RAMDISK_H

void init_rd(char *buffer);
Expand All @@ -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
13 changes: 13 additions & 0 deletions lab7/include/uartfs.h
Original file line number Diff line number Diff line change
@@ -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
Binary file modified lab7/initramfs.cpio
Binary file not shown.
10 changes: 10 additions & 0 deletions lab7/src/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 20 additions & 0 deletions lab7/src/ramdisk.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "mini_uart.h"
#include "vfs.h"

#define O_CREAT 0b100
#define null 0

typedef struct
{
Expand Down Expand Up @@ -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;i<index;i++)
{
struct vnode* target;
int op_status = mount_node->v_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;
}
17 changes: 16 additions & 1 deletion lab7/src/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand Down
97 changes: 42 additions & 55 deletions lab7/src/tmpfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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;i<len;i++)
{
buffer[i] = internal[file->f_pos];
file->f_pos++;
}
return len;
}

for(int i=0;i<len;i++)
{
buffer[i] = internal[file->f_pos];
Expand All @@ -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;
}
Expand All @@ -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;i<dir_node->child_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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;i<dir_node->child_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;i<dir_node->child_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;
}

Expand Down
70 changes: 70 additions & 0 deletions lab7/src/uartfs.c
Original file line number Diff line number Diff line change
@@ -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;i<len;i++)
{
uart_send(*buffer++);
}
return len;
}

int uartfs_read(struct file* file,void* buf,size_t len)
{
char* buffer = buf;
for(int i=0;i<len;i++)
{
*buffer++ = uart_recv();
}
return len;
}

int uartfs_open(struct vnode* file_node,struct file** target)
{
struct file* open_file = d_alloc(sizeof(struct file));
open_file->vnode = 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;
}
Loading