summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fs/ext2.c25
-rw-r--r--fs/ext2.h3
-rw-r--r--fs/fd.h5
-rw-r--r--fs/mount.c96
-rw-r--r--fs/mount.h53
-rw-r--r--kernel/exceptions.c1
-rw-r--r--kernel/kernel.c2
-rw-r--r--kernel/kernel.h6
-rw-r--r--kernel/kmalloc.c16
-rw-r--r--kernel/kmalloc.h7
-rw-r--r--kernel/mem.c7
-rw-r--r--kernel/mem.h1
-rw-r--r--kernel/syscalls.c7
-rw-r--r--kernel/vmem.c1
-rw-r--r--testing/testing.c51
-rw-r--r--testing/testing.h1
-rw-r--r--xxx/mount.c12
-rw-r--r--xxx/mount.h23
18 files changed, 270 insertions, 47 deletions
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 <stdbool.h>
#include <stdint.h>
+#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 <stddef.h>
+
+#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;i<mounts_count;i++)
+ {
+ mount *m=&mounts[i];
+ klog("%s mounted at: %s ",mount_type_to_str(m->type),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;i<mounts_count;i++)
+ {
+ mount *m=&mounts[i];
+ uint32_t len=check_match(path,m->path);
+
+ 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 <stdint.h>
+#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;i<BLOCKS;i++)
+ {
+ if(map[i]) used++;
+ else free++;
+ }
+
+ f("kernel blocks allocation/deallocation");
+ f("total 4096kb blocks: %d (%d bytes)",BLOCKS,BLOCKS*4096);
+ f("used 4096kb blocks: %d (%d bytes)",used,used*4096);
+ f("free 4096kb blocks: %d (%d bytes)",free,free*4096);
+}
diff --git a/kernel/kmalloc.h b/kernel/kmalloc.h
index c7900c5..5ca4c8c 100644
--- a/kernel/kmalloc.h
+++ b/kernel/kmalloc.h
@@ -23,7 +23,10 @@
/** Allocate size*4096 bytes and returns a 32-bit address
* and 0 if fails.
* */
-uint32_t kballoc (uint32_t size);
+uint32_t kballoc (uint32_t size);
/** Free memory allocated before by supplying the address returned by kballoc */
-void kbfree (uint32_t addr);
+void kbfree (uint32_t addr);
+
+/** Get current status for sysfs */
+void kmalloc_sysfs(void (*f)(char *fmt, ...));
diff --git a/kernel/mem.c b/kernel/mem.c
index a77dfa4..52e08b6 100644
--- a/kernel/mem.c
+++ b/kernel/mem.c
@@ -227,3 +227,10 @@ uint32_t mem_init(multiboot_information *info)
return 0;
}
+
+void mem_sysfs(void (*f)(char *fmt, ...))
+{
+ f("physical memory manager");
+ f("free 4096kb blocks : %d",mem_free_blocks);
+ f("free bytes : %d",mem_free_blocks*4096);
+}
diff --git a/kernel/mem.h b/kernel/mem.h
index 6871e11..85fc688 100644
--- a/kernel/mem.h
+++ b/kernel/mem.h
@@ -29,3 +29,4 @@ uint32_t mem_init(multiboot_information *info);
void* mem_alloc_block ();
void mem_free_block(void* p);
uint32_t mem_get_free_blocks_count();
+void mem_sysfs(void (*f)(char *fmt, ...));
diff --git a/kernel/syscalls.c b/kernel/syscalls.c
index 5205bf1..8b5040a 100644
--- a/kernel/syscalls.c
+++ b/kernel/syscalls.c
@@ -16,6 +16,7 @@
#include "scheduler.h"
#include "log.h"
#include "timer.h"
+#include "mount.h"
static fd fds[MAX_FD];
static uint32_t next_fd=0;
@@ -155,9 +156,7 @@ int syscall_read(int file, char *buf, int len)
//TODO: replace with dirent!
int syscall_readdir(const char *name,fs_dirent *dirs,int *pos)
{
- uint32_t inode = ext2_filename_to_inode(VMEM_EXT2_RAMIMAGE,name);
- if(inode==0)return 0;
- return ext2_read_dir(VMEM_EXT2_RAMIMAGE, inode, dirs, pos);
+ return mount_read_dir(name, dirs, pos);
}
// for non blocking io?
@@ -330,7 +329,7 @@ int syscall_open(char *name, int flags, int mode)
}
else
{
- fds[next_fd]=fd_from_path(name);
+ fds[next_fd]=mount_file_open(name);
if(*(uint32_t *)fds[next_fd].data==0)return -1;
}
diff --git a/kernel/vmem.c b/kernel/vmem.c
index 129ddb5..590722d 100644
--- a/kernel/vmem.c
+++ b/kernel/vmem.c
@@ -427,4 +427,3 @@ void vmem_init(multiboot_information *cfg_multiboot, acpi_information *cfg_acpi)
mod_start=mod->mod_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 <stdint.h>
#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 <stdint.h>
-#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