From 06e6e427c76bdb88a7f72dd04411d95a4bda3270 Mon Sep 17 00:00:00 2001 From: Miguel Date: Sun, 16 Sep 2018 23:46:30 +0200 Subject: starting to create sysfs --- fs/ext2.c | 25 ++++++++++++++ fs/ext2.h | 3 ++ fs/fd.h | 5 +-- fs/mount.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/mount.h | 53 +++++++++++++++++++++++++++++ kernel/exceptions.c | 1 - kernel/kernel.c | 2 ++ kernel/kernel.h | 6 ++-- kernel/kmalloc.c | 16 +++++++++ kernel/kmalloc.h | 7 ++-- kernel/mem.c | 7 ++++ kernel/mem.h | 1 + kernel/syscalls.c | 7 ++-- kernel/vmem.c | 1 - testing/testing.c | 51 ++++++++++++++++++++++++++++ testing/testing.h | 1 + xxx/mount.c | 12 ------- xxx/mount.h | 23 ------------- 18 files changed, 270 insertions(+), 47 deletions(-) create mode 100644 fs/mount.c create mode 100644 fs/mount.h delete mode 100644 xxx/mount.c delete mode 100644 xxx/mount.h diff --git a/fs/ext2.c b/fs/ext2.c index 659edda..b518bd9 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -1,6 +1,7 @@ #include #include +#include "mount.h" #include "kernel.h" #include "ext2.h" @@ -303,3 +304,27 @@ int ext2_read_dir(uint32_t ext2_start_addr, int inode_nr, fs_dirent *dirs, uint3 } return 0; } + +/* mount interface */ + +fd ext2_mount_file_open(mount *m,char *path) +{ + return fd_from_path(path); +} + +int ext2_mount_read_dir(mount *m,char *path, fs_dirent *dirs, uint32_t *pos) +{ + uint32_t inode= ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,path); + return ext2_read_dir(m->data, inode, dirs, pos); +} + +void ext2_mount(char *path) +{ + mount m; + m.type=MOUNT_TYPE_EXT2; + memcpy(m.path,path,strlen(path)+1); + m.mount_file_open=ext2_mount_file_open; + m.mount_read_dir=ext2_mount_read_dir; + m.data=VMEM_EXT2_RAMIMAGE; + mount_add(m); +} diff --git a/fs/ext2.h b/fs/ext2.h index efa045f..3c53350 100644 --- a/fs/ext2.h +++ b/fs/ext2.h @@ -33,3 +33,6 @@ uint32_t ext2_filename_to_inode(uint32_t ext2_start_addr, char *path); /** get address of first byte of given block for given inode number */ uint32_t ext2_inode_blockstart(uint32_t ext2_start_addr,uint32_t inode_nr,uint32_t block); + +/** mount */ +void ext2_mount(char *path); diff --git a/fs/fd.h b/fs/fd.h index 429387b..a51a0f3 100644 --- a/fs/fd.h +++ b/fs/fd.h @@ -26,7 +26,8 @@ typedef struct enum FD_TYPE{ FD_TYPE_FIFO_BUFFERED=1, - FD_TYPE_EXT2_FILE=2 + FD_TYPE_EXT2_FILE=2, + FD_TYPE_EXT2_DIR=3 }; typedef struct fd_struct @@ -47,5 +48,5 @@ bool fd_close(fd*); fd fd_from_fifo(fifo* f); fd fd_from_path(char *path); - +//fd fd_from_sysfs(void(*g)(void (*f)(char *fmt, ...))); #endif diff --git a/fs/mount.c b/fs/mount.c new file mode 100644 index 0000000..a7cd949 --- /dev/null +++ b/fs/mount.c @@ -0,0 +1,96 @@ +#include + +#include "kernel.h" +#include "mount.h" + +#include "log.h" + +#include "lib/string/string.h" +// temporary +#include "fd.h" + +static mount mounts[MAX_MOUNTS]; +static uint32_t mounts_count=0; + +char *mount_type_to_str(uint32_t t) +{ + switch(t) + { + case MOUNT_TYPE_EXT2: return "EXT2"; + case MOUNT_TYPE_PIPES: return "PIPES"; + case MOUNT_TYPE_SYS: return "SYS"; + } + return "UNKNOWN"; +} + +void mount_add(mount mnt) +{ + if(mounts_count==MAX_MOUNTS)kpanic("maxium number of mounts exceeded. increase MAX_MOUNTS in kernel.h and recomplie kernel."); + mounts[mounts_count++]=mnt; + klog("mounted %s",mnt.path); +} + +void mount_dump() +{ + for(int i=0;itype),m->path); + } +} + +static uint32_t check_match(char *p1, char *p2) +{ + uint32_t c=0; + while(1) + { + if(*p1==0||*p2==0||*p1!=*p2)return c; + c++; + p1++; + p2++; + } +} + +/** + * Find the mountpoint correspoding with the given path and return in _mnt_ parameter. + * The return value points to the input path with chopped of prefix indicating the mount itself. + * returns 0 if mount not found. + */ +static char* get_mount_for_path(char *path,mount *mnt) +{ + if(path[0]!='/')kpanic("this works only for absolute paths!"); + + uint32_t best=0; + uint32_t best_len=0; + + for(int i=0;ipath); + + if(len>best_len&&len==strlen(m->path)) + { + best=i; + best_len=len; + } + + } + if(best_len==0)return 0; + *mnt=mounts[best]; + return path+best_len-1; +} + +fd mount_file_open(char *path) +{ + mount m; + char *p=get_mount_for_path(path,&m); + klog("%s in %s",p,m.path); + return m.mount_file_open(&m,p); +} + +int mount_read_dir (char *path, fs_dirent *dirs, uint32_t *pos) +{ + mount m; + char *p=get_mount_for_path(path,&m); + return m.mount_read_dir(&m,p,dirs,pos); +} diff --git a/fs/mount.h b/fs/mount.h new file mode 100644 index 0000000..4e61c28 --- /dev/null +++ b/fs/mount.h @@ -0,0 +1,53 @@ +/** + * @file + * Simple mount point manager + * ========================== + * + * Add up to MAX_MOUNTS (as defined in kernel.h) mounts and use + * mount_file_open() and mount_read_dir() to transparently + * be dispatch the underlying commands to them based on the supplied paths. + * + * Mount directories should exist on root direcotry '/' + */ + +#ifndef MOUNT_H +#define MOUNT_H + +#include +#include "interface/fs.h" // fs_dirent for read_dir +#include "fd.h" // file descriptor returned by open + +/** the possible values for mount_struct.type */ +enum MOUNT_TYPE{ + MOUNT_TYPE_EXT2 = 1, + MOUNT_TYPE_PIPES = 2, + MOUNT_TYPE_SYS = 3 +}; + +/** struct telling all we need about a single mountpoint */ +typedef struct mount_struct +{ + uint32_t type; // MOUNT_TYPE + + char path[256]; // where are we mounted (provide leading and trailing slash!) + + fd (*mount_file_open)(struct mount_struct*, char *path); + int (*mount_read_dir) (struct mount_struct*, char *path, fs_dirent *dirs, uint32_t *pos); + + void *data; //opaque data + +}mount; + +/** dumps mount info to klog */ +void mount_dump(); + +/** adds a new mountpoints */ +void mount_add(mount mnt); + +/** dispatchers to according mount points */ +fd mount_file_open(char *path); + +/** TODO: should use fd number instead of PATH on each call*/ +int mount_read_dir (char *path, fs_dirent *dirs, uint32_t *pos); + +#endif diff --git a/kernel/exceptions.c b/kernel/exceptions.c index a092055..4ad87e8 100644 --- a/kernel/exceptions.c +++ b/kernel/exceptions.c @@ -126,7 +126,6 @@ uint32_t exception_handle(uint32_t esp, uint32_t irq) klog("killing task in question!"); task_syscall(SYSCALL_EXIT,task_get_current_pid(),0,0); - scheduler_wake_all(); return scheduler_run(esp,0); } diff --git a/kernel/kernel.c b/kernel/kernel.c index ca82a5d..0239489 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -51,6 +51,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // -- UNIT TESTING -- // testing_kmalloc(); + testing_mount(); // -- DISABLE LEGACY PIC -- // klog("Remapping & Disabling Programmable Interrupt Controller (PIC) ..."); @@ -97,6 +98,7 @@ void kernel_main(uint32_t eax,uint32_t ebx) // -- EXT2 RAM IMAGE -- // klog("Check ext2 ram image ... "); ext2_dump_info(VMEM_EXT2_RAMIMAGE); + ext2_mount("/"); // -- APIC -- // klog("Advanced Programmable Interrupt Controller (APIC) config ..."); diff --git a/kernel/kernel.h b/kernel/kernel.h index 83d89b8..e3d7590 100644 --- a/kernel/kernel.h +++ b/kernel/kernel.h @@ -22,12 +22,14 @@ REFERENCES #ifndef FOOLOS_CONFIG_H #define FOOLOS_CONFIG_H -#define BIN_INIT "/bin/init" - //#define FOOLOS_UNIT_TESTING // Run Unit Tests //#define FOOLOS_LOG_OFF // Turn off logging //#define FOOLOS_COLORLESS // Turn off colors in log +#define MAX_MOUNTS 10 + +#define BIN_INIT "/bin/init" + #define FIFO_MAX_RINGBUFFERS 20 #define MAX_FIFOS 20 #define MAX_FD 20 diff --git a/kernel/kmalloc.c b/kernel/kmalloc.c index 356aab0..6a7520a 100644 --- a/kernel/kmalloc.c +++ b/kernel/kmalloc.c @@ -116,3 +116,19 @@ void kbfree(uint32_t pos) mark_free(blk,map[blk]); spinlock_release(SPINLOCK_ALLOC); } + +void kmalloc_sysfs(void (*f)(char *fmt, ...)) +{ + uint32_t free=0; + uint32_t used=0; + for(int i=0;imod_start; mod_end=mod->mod_end; } - diff --git a/testing/testing.c b/testing/testing.c index fc95054..7646b95 100644 --- a/testing/testing.c +++ b/testing/testing.c @@ -1,7 +1,58 @@ #include #include "log.h" #include "kmalloc.h" +#include "mount.h" +#include "lib/string/string.h" + +void testing_mount_add(char *path, uint32_t t) +{ + mount m; + memcpy(m.path,path,strlen(path)+1); + m.type=t; + mount_add(m); +} + +void testing_get_mount(char *path) +{ + /* + mount m; + char *subpath=get_mount_for_path(path,&m); + if(subpath==0) + { + testlog ("mount %s not found!",path); + } + else + { + testlog("mount [%s] is [%s] at [%s]",path, subpath, m.path); + } + */ +} + +void testing_mount() +{ + #ifndef FOOLOS_UNIT_TESTING + return; + #endif + testlog ("= UNIT TESTING MOUNTS ="); + testing_mount_add("/",MOUNT_TYPE_EXT2); + testing_mount_add("/pipes/",MOUNT_TYPE_PIPES); + testing_mount_add("/pipes/input/",MOUNT_TYPE_PIPES); + testing_mount_add("/sys/",MOUNT_TYPE_SYS); + mount_dump(); +// testing_get_mount("pipes/input"); + testing_get_mount("/dupa"); + testing_get_mount("/pipe"); + testing_get_mount("/pipes/input"); + testing_get_mount("/pipes/in"); + testing_get_mount("/pipes/input/"); + testing_get_mount("/pipes/input/dups"); + testing_get_mount("/sys"); + testing_get_mount("/sys/ass"); + testing_get_mount("/sys/ass/bass"); + testing_get_mount("/foo/sys/ass/bass"); + while(1); +} void testing_kmalloc_alloc(uint32_t size) { uint32_t addr=kballoc(size); diff --git a/testing/testing.h b/testing/testing.h index bde0453..df20770 100644 --- a/testing/testing.h +++ b/testing/testing.h @@ -1 +1,2 @@ void testing_kmalloc(); +void testing_mount(); diff --git a/xxx/mount.c b/xxx/mount.c deleted file mode 100644 index cb741f9..0000000 --- a/xxx/mount.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "mount.h" - -void mount_add(char *path, void *data, - file (*open) (struct mount_struct*,char *path), - int (*getdents)(struct mount_struct*, uint32_t file_desciptor, fs_dirent *entries, uint32_t max_count)) -{ -} - -mount *mounts_get() -{ - return 0; -} diff --git a/xxx/mount.h b/xxx/mount.h deleted file mode 100644 index a327e2e..0000000 --- a/xxx/mount.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MOUNT_H -#define MOUNT_H - -#define MOUNT_MAX_MOUNTS 10 - -#include -#include "file.h" -#include "interface/fs.h" - -typedef struct mount_struct -{ - char path[256]; // where are we mounted - int (*getdents) (struct mount_struct*, uint32_t file_desciptor, fs_dirent *entries, uint32_t max_count); - file (*open) (struct mount_struct*,char *path); - void *data; //opaque - -}mount; - -mount *mounts_get(); -void mount_add(char *path, void *data,file (*open)(struct mount_struct*,char *path),int (*getdents)(struct mount_struct*, uint32_t file_desciptor, fs_dirent *entries, uint32_t max_count)); - -// -#endif -- cgit v1.2.3