aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-04-21 14:18:20 +0200
committerMarvin Borner2020-04-21 14:18:20 +0200
commit40b98b3eb3f897d3d12199c79b30e2fd9151baf9 (patch)
tree0f10c78c95bd24a4b5cef2a3948dd3abbc1a4eb7
parentc83104329214bd618444d6e0242bb0317f52dbbc (diff)
Added very basic vfs
-rw-r--r--src/kernel/fs/ext2.c96
-rw-r--r--src/kernel/fs/ext2.h5
-rw-r--r--src/kernel/fs/vfs.c48
-rw-r--r--src/kernel/fs/vfs.h66
-rw-r--r--src/kernel/kernel.c4
-rw-r--r--src/kernel/lib/data/generic_tree.c102
-rw-r--r--src/kernel/lib/data/generic_tree.h36
-rw-r--r--src/kernel/lib/data/list.c220
-rw-r--r--src/kernel/lib/data/list.h60
9 files changed, 216 insertions, 421 deletions
diff --git a/src/kernel/fs/ext2.c b/src/kernel/fs/ext2.c
index c95c134..ce8338a 100644
--- a/src/kernel/fs/ext2.c
+++ b/src/kernel/fs/ext2.c
@@ -7,6 +7,7 @@
#include <kernel/memory/alloc.h>
#include <kernel/lib/lib.h>
#include <kernel/lib/stdlib.h>
+#include <kernel/fs/vfs.h>
static struct ext2_superblock superblock;
static struct bgd *bgdt;
@@ -231,4 +232,97 @@ uint32_t ext2_look_up_path(char *path)
return 0;
return inode;
-} \ No newline at end of file
+}
+
+// VFS Implementation
+
+void ext2_vfs_open(struct fs_node *node)
+{
+ node->inode = ext2_look_up_path(node->name);
+
+ if (node->inode != 0) {
+ struct ext2_file *file = kmalloc(sizeof *file);
+ ext2_open_inode(node->inode, file);
+
+ node->impl = file;
+
+ // TODO: More file metadata
+ }
+}
+
+static void ext2_vfs_close(struct fs_node *node)
+{
+ kfree(node->impl);
+}
+
+static uint32_t ext2_vfs_read(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ if (offset != ((struct ext2_file *)node->impl)->pos) {
+ panic("Seeking is currently unsupported for Ext2 files");
+ return 0;
+ }
+
+ return (uint32_t)ext2_read(node->impl, (uint8_t *)buf, size);
+}
+
+static uint32_t ext2_vfs_write(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ panic("Writing to Ext2 is currently unsupported");
+
+ return 0;
+}
+
+static struct dir_entry *ext2_vfs_read_dir(struct fs_node *node, size_t index)
+{
+ struct ext2_dirent ext2_dir;
+
+ if (ext2_next_dirent(node->impl, &ext2_dir)) {
+ struct dir_entry *dir = kmalloc(sizeof *dir);
+
+ dir->inode = ext2_dir.inode_num;
+ strcpy(dir->name, (char *)ext2_dir.name);
+
+ return dir;
+ } else {
+ return NULL;
+ }
+}
+
+static struct fs_node *ext2_vfs_find_dir(struct fs_node *node, char *name)
+{
+ uint32_t inode = ext2_find_in_dir(node->inode, name);
+ if (inode == 0) {
+ return NULL;
+ } else {
+ struct fs_node *found = kmalloc(sizeof *found);
+ found->inode = inode;
+
+ return found;
+ }
+}
+
+void ext2_mount(struct fs_node *mountpoint)
+{
+ assert(mountpoint->node_ptr == NULL && (mountpoint->type & MOUNTPOINT_NODE) == 0);
+ assert((mountpoint->type & DIR_NODE) != 0);
+
+ struct fs_node *ext2_root = (struct fs_node *)kmalloc(sizeof(struct fs_node));
+ ext2_root->name[0] = '\0';
+ ext2_root->permissions = 0;
+ ext2_root->uid = 0;
+ ext2_root->gid = 0;
+ ext2_root->inode = ROOT_INODE;
+ ext2_root->length = 0;
+ ext2_root->type = DIR_NODE;
+ ext2_root->read = ext2_vfs_read;
+ ext2_root->write = ext2_vfs_write;
+ ext2_root->open = ext2_vfs_open;
+ ext2_root->close = ext2_vfs_close;
+ ext2_root->read_dir = ext2_vfs_read_dir;
+ ext2_root->find_dir = ext2_vfs_find_dir;
+ ext2_root->node_ptr = NULL;
+ ext2_root->impl = NULL;
+
+ mountpoint->type |= MOUNTPOINT_NODE;
+ mountpoint->node_ptr = ext2_root;
+}
diff --git a/src/kernel/fs/ext2.h b/src/kernel/fs/ext2.h
index e5e0f1c..f3ea4a0 100644
--- a/src/kernel/fs/ext2.h
+++ b/src/kernel/fs/ext2.h
@@ -4,6 +4,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
+#include <kernel/fs/vfs.h>
#define ROOT_INODE 2
@@ -137,4 +138,6 @@ bool ext2_next_dirent(struct ext2_file *file, struct ext2_dirent *dir);
uint32_t ext2_find_in_dir(uint32_t dir_inode, const char *name);
uint32_t ext2_look_up_path(char *path);
-#endif \ No newline at end of file
+void ext2_mount(struct fs_node *mountpoint);
+
+#endif
diff --git a/src/kernel/fs/vfs.c b/src/kernel/fs/vfs.c
new file mode 100644
index 0000000..075117e
--- /dev/null
+++ b/src/kernel/fs/vfs.c
@@ -0,0 +1,48 @@
+#include <stddef.h>
+#include <kernel/fs/vfs.h>
+
+struct fs_node *fs_root = NULL;
+
+uint32_t read_fs_node(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ if (node->read != NULL)
+ return node->read(node, offset, size, buf);
+ else
+ return 0;
+}
+
+uint32_t write_fs_node(struct fs_node *node, size_t offset, size_t size, char *buf)
+{
+ if (node->write != NULL)
+ return node->write(node, offset, size, buf);
+ else
+ return 0;
+}
+
+void open_fs_node(struct fs_node *node, bool read, bool write)
+{
+ if (node->open != NULL)
+ node->open(node);
+}
+
+void close_fs_node(struct fs_node *node)
+{
+ if (node->close != NULL)
+ node->close(node);
+}
+
+struct dir_entry *read_dir_node(struct fs_node *node, size_t index)
+{
+ if ((node->type & DIR_NODE) != 0 && node->read_dir != NULL)
+ return node->read_dir(node, index);
+ else
+ return NULL;
+}
+
+struct fs_node *find_dir_node(struct fs_node *node, char *name)
+{
+ if ((node->type & DIR_NODE) != 0 && node->find_dir != NULL)
+ return node->find_dir(node, name);
+ else
+ return NULL;
+}
diff --git a/src/kernel/fs/vfs.h b/src/kernel/fs/vfs.h
new file mode 100644
index 0000000..41d80eb
--- /dev/null
+++ b/src/kernel/fs/vfs.h
@@ -0,0 +1,66 @@
+#ifndef MELVIX_VFS_H
+#define MELVIX_VFS_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#define MAX_NAME_LENGTH 128
+
+enum node_type {
+ FILE_NODE = 1,
+ DIR_NODE,
+ CHAR_DEV_NODE,
+ BLOCK_DEV_NODE,
+ PIPE_NODE,
+ SYMLINK_NODE,
+ MOUNTPOINT_NODE = 8
+};
+
+struct fs_node;
+struct dir_entry;
+
+typedef uint32_t (*read)(struct fs_node *, size_t, size_t, char *);
+typedef uint32_t (*write)(struct fs_node *, size_t, size_t, char *);
+typedef void (*open)(struct fs_node *);
+typedef void (*close)(struct fs_node *);
+
+typedef struct dir_entry *(*read_dir)(struct fs_node *, size_t);
+typedef struct fs_node *(*find_dir)(struct fs_node *, char *);
+
+struct fs_node {
+ char name[MAX_NAME_LENGTH];
+ uint32_t length;
+ uint32_t inode;
+ uint32_t permissions;
+ uint32_t uid;
+ uint32_t gid;
+ enum node_type type;
+
+ struct fs_node *node_ptr;
+
+ void *impl;
+
+ read read;
+ write write;
+ open open;
+ close close;
+ read_dir read_dir;
+ find_dir find_dir;
+};
+
+struct dir_entry {
+ char name[MAX_NAME_LENGTH];
+ uint32_t inode;
+};
+
+struct fs_node *fs_root;
+
+uint32_t read_fs_node(struct fs_node *node, size_t offset, size_t size, char *buf);
+uint32_t write_fs_node(struct fs_node *node, size_t offset, size_t size, char *buf);
+void open_fs_node(struct fs_node *node, bool read, bool write);
+void close_fs_node(struct fs_node *node);
+struct dir_entry *read_dir_node(struct fs_node *node, size_t index);
+struct fs_node *find_dir_node(struct fs_node *node, char *name);
+
+#endif
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index 57b853f..89c5c4c 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -18,6 +18,7 @@
#include <kernel/fs/ata.h>
#include <kernel/fs/ext2.h>
#include <kernel/cmos/rtc.h>
+#include <kernel/memory/alloc.h>
void kernel_main(uint32_t magic, uint32_t multiboot_address)
{
@@ -57,6 +58,7 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address)
ata_init();
ext2_init_fs();
+ ext2_mount(fs_root);
load_binaries();
set_optimal_resolution();
@@ -75,4 +77,4 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address)
// panic("This should NOT happen!");
// asm ("div %0" :: "r"(0)); // Exception testing x/0
-} \ No newline at end of file
+}
diff --git a/src/kernel/lib/data/generic_tree.c b/src/kernel/lib/data/generic_tree.c
deleted file mode 100644
index 38caccc..0000000
--- a/src/kernel/lib/data/generic_tree.c
+++ /dev/null
@@ -1,102 +0,0 @@
-#include <kernel/lib/data/generic_tree.h>
-#include <kernel/lib/data/list.h>
-#include <kernel/memory/alloc.h>
-
-gtree_t *tree_create()
-{
- return (gtree_t *)kcalloc(sizeof(gtree_t), 1);
-}
-
-gtreenode_t *treenode_create(void *value)
-{
- gtreenode_t *n = kcalloc(sizeof(gtreenode_t), 1);
- n->value = value;
- n->children = list_create();
- return n;
-}
-
-gtreenode_t *tree_insert(gtree_t *tree, gtreenode_t *subroot, void *value)
-{
- gtreenode_t *treenode = kcalloc(sizeof(gtreenode_t), 1);
- treenode->children = list_create();
- treenode->value = value;
-
- if (!tree->root) {
- tree->root = treenode;
- return treenode;
- }
- list_insert_front(subroot->children, treenode);
- return treenode;
-}
-
-gtreenode_t *tree_find_parent(gtree_t *tree, gtreenode_t *remove_node, int *child_index)
-{
- if (remove_node == tree->root)
- return NULL;
- return tree_find_parent_recur(tree, remove_node, tree->root, child_index);
-}
-
-gtreenode_t *tree_find_parent_recur(gtree_t *tree, gtreenode_t *remove_node, gtreenode_t *subroot,
- int *child_index)
-{
- int idx;
- if ((idx = list_contain(subroot->children, remove_node)) != -1) {
- *child_index = idx;
- return subroot;
- }
- foreach(child, subroot->children)
- {
- gtreenode_t *ret =
- tree_find_parent_recur(tree, remove_node, child->val, child_index);
- if (ret != NULL) {
- return ret;
- }
- }
- return NULL;
-}
-
-void tree_remove(gtree_t *tree, gtreenode_t *remove_node)
-{
- int child_index = -1;
- gtreenode_t *parent = tree_find_parent(tree, remove_node, &child_index);
- if (parent != NULL) {
- gtreenode_t *freethis = list_remove_by_index(parent->children, child_index);
- kfree(freethis);
- }
-}
-
-void tree2list_recur(gtreenode_t *subroot, list_t *list)
-{
- if (subroot == NULL)
- return;
- foreach(child, subroot->children)
- {
- gtreenode_t *curr_treenode = (gtreenode_t *)child->val;
- void *curr_val = curr_treenode->value;
- list_insert_back(list, curr_val);
- tree2list_recur(child->val, list);
- }
-}
-
-void tree2list(gtree_t *tree, list_t *list)
-{
- tree2list_recur(tree->root, list);
-}
-
-void tree2array(gtree_t *tree, void **array, int *size)
-{
- tree2array_recur(tree->root, array, size);
-}
-
-void tree2array_recur(gtreenode_t *subroot, void **array, int *size)
-{
- if (subroot == NULL)
- return;
- void *curr_val = (void *)subroot->value;
- array[*size] = curr_val;
- *size = *size + 1;
- foreach(child, subroot->children)
- {
- tree2array_recur(child->val, array, size);
- }
-} \ No newline at end of file
diff --git a/src/kernel/lib/data/generic_tree.h b/src/kernel/lib/data/generic_tree.h
deleted file mode 100644
index 3a630bc..0000000
--- a/src/kernel/lib/data/generic_tree.h
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef MELVIX_GENERIC_TREE_H
-#define MELVIX_GENERIC_TREE_H
-
-#include <kernel/lib/data/list.h>
-
-typedef struct gtreenode {
- list_t *children;
- void *value;
-} gtreenode_t;
-
-typedef struct gtree {
- gtreenode_t *root;
-} gtree_t;
-
-gtree_t *tree_create();
-
-gtreenode_t *treenode_create(void *value);
-
-gtreenode_t *tree_insert(gtree_t *tree, gtreenode_t *subroot, void *value);
-
-gtreenode_t *tree_find_parent(gtree_t *tree, gtreenode_t *remove_node, int *child_index);
-
-gtreenode_t *tree_find_parent_recur(gtree_t *tree, gtreenode_t *remove_node, gtreenode_t *subroot,
- int *child_index);
-
-void tree_remove(gtree_t *tree, gtreenode_t *remove_node);
-
-void tree2list_recur(gtreenode_t *subroot, list_t *list);
-
-void tree2list(gtree_t *tree, list_t *list);
-
-void tree2array(gtree_t *tree, void **array, int *size);
-
-void tree2array_recur(gtreenode_t *subroot, void **array, int *size);
-
-#endif \ No newline at end of file
diff --git a/src/kernel/lib/data/list.c b/src/kernel/lib/data/list.c
deleted file mode 100644
index 7a0cff2..0000000
--- a/src/kernel/lib/data/list.c
+++ /dev/null
@@ -1,220 +0,0 @@
-#include <kernel/lib/data/list.h>
-#include <kernel/memory/alloc.h>
-#include <kernel/lib/stdlib.h>
-#include <kernel/lib/lib.h>
-
-list_t *list_create()
-{
- list_t *list = kcalloc(sizeof(list_t), 1);
- return list;
-}
-
-uint32_t list_size(list_t *list)
-{
- if (!list)
- return 0;
- return list->size;
-}
-
-void *list_remove_node(list_t *list, listnode_t *node)
-{
- void *val = node->val;
- if (list->head == node)
- return list_remove_front(list);
- else if (list->tail == node)
- return list_remove_back(list);
- else {
- node->next->prev = node->prev;
- node->prev->next = node->next;
- list->size--;
- kfree(node);
- }
- return val;
-}
-
-listnode_t *list_insert_front(list_t *list, void *val)
-{
- listnode_t *t = kcalloc(sizeof(listnode_t), 1);
- list->head->prev = t;
- t->next = list->head;
- t->val = val;
-
- if (!list->head)
- list->tail = t;
-
- list->head = t;
- list->size++;
- return t;
-}
-
-void list_insert_back(list_t *list, void *val)
-{
- listnode_t *t = kcalloc(sizeof(listnode_t), 1);
- t->prev = list->tail;
- if (list->tail)
- list->tail->next = t;
- t->val = val;
-
- if (!list->head)
- list->head = t;
-
- list->tail = t;
- list->size++;
-}
-
-void *list_remove_front(list_t *list)
-{
- if (!list->head)
- return 0;
- listnode_t *t = list->head;
- void *val = t->val;
- list->head = t->next;
- if (list->head)
- list->head->prev = NULL;
- kfree(t);
- list->size--;
- return val;
-}
-
-void *list_remove_back(list_t *list)
-{
- if (!list->head)
- return NULL;
- listnode_t *t = list->tail;
- void *val = t->val;
- list->tail = t->prev;
- if (list->tail)
- list->tail->next = NULL;
- kfree(t);
- list->size--;
- return val;
-}
-
-void list_push(list_t *list, void *val)
-{
- list_insert_back(list, val);
-}
-
-listnode_t *list_pop(list_t *list)
-{
- if (!list->head)
- return NULL;
- listnode_t *t = list->tail;
- list->tail = t->prev;
- if (list->tail)
- list->tail->next = NULL;
- list->size--;
- return t;
-}
-
-void list_enqueue(list_t *list, void *val)
-{
- list_insert_front(list, val);
-}
-
-listnode_t *list_dequeue(list_t *list)
-{
- return list_pop(list);
-}
-
-void *list_peek_front(list_t *list)
-{
- if (!list->head)
- return NULL;
- return list->head->val;
-}
-
-void *list_peek_back(list_t *list)
-{
- if (!list->tail)
- return NULL;
- return list->tail->val;
-}
-
-int list_contain(list_t *list, void *val)
-{
- int idx = 0;
- foreach(listnode, list)
- {
- if (listnode->val == val)
- return idx;
- idx++;
- }
- return -1;
-}
-
-listnode_t *list_get_node_by_index(list_t *list, int index)
-{
- if (index < 0 || (uint32_t)index >= list_size(list))
- return NULL;
- int curr = 0;
- foreach(listnode, list)
- {
- if (index == curr)
- return listnode;
- curr++;
- }
- return NULL;
-}
-
-void *list_remove_by_index(list_t *list, int index)
-{
- listnode_t *node = list_get_node_by_index(list, index);
- return list_remove_node(list, node);
-}
-
-void list_destroy(list_t *list)
-{
- listnode_t *node = list->head;
- while (node != NULL) {
- listnode_t *save = node;
- node = node->next;
- kfree(save);
- }
- kfree(list);
-}
-
-void listnode_destroy(listnode_t *node)
-{
- kfree(node);
-}
-
-char *list2str(list_t *list, const char *delim)
-{
- char *ret = kmalloc(256);
- memset(ret, 0, 256);
- int len = 0, ret_len = 256;
- while (list_size(list) > 0) {
- char *temp = list_pop(list)->val;
- int len_temp = strlen(temp);
- if (len + len_temp + 1 + 1 > ret_len) {
- ret_len = ret_len * 2;
- ret = krealloc(ret, ret_len);
- len = len + len_temp + 1;
- }
- strcat(ret, delim);
- strcat(ret, temp);
- }
- return ret;
-}
-
-list_t *str_split(const char *str, const char *delim, uint32_t *numtokens)
-{
- list_t *ret_list = list_create();
- char *s = strdup(str);
- char *token, *rest = s;
- while ((token = strsep(&rest, delim)) != NULL) {
- if (!strcmp(token, "."))
- continue;
- if (!strcmp(token, "..")) {
- if (list_size(ret_list) > 0)
- list_pop(ret_list);
- continue;
- }
- list_push(ret_list, strdup(token));
- if (numtokens)
- (*numtokens)++;
- }
- kfree(s);
- return ret_list;
-} \ No newline at end of file
diff --git a/src/kernel/lib/data/list.h b/src/kernel/lib/data/list.h
deleted file mode 100644
index eeda9b3..0000000
--- a/src/kernel/lib/data/list.h
+++ /dev/null
@@ -1,60 +0,0 @@
-#ifndef MELVIX_LIST_H
-#define MELVIX_LIST_H
-
-#include <stdint.h>
-
-typedef struct listnode {
- struct listnode *prev;
- struct listnode *next;
- void *val;
-} listnode_t;
-
-typedef struct list {
- listnode_t *head;
- listnode_t *tail;
- uint32_t size;
-} list_t;
-
-#define foreach(t, list) for (listnode_t *t = list->head; t != NULL; t = t->next)
-
-list_t *list_create();
-
-uint32_t list_size(list_t *list);
-
-listnode_t *list_insert_front(list_t *list, void *val);
-
-void list_insert_back(list_t *list, void *val);
-
-void *list_remove_node(list_t *list, listnode_t *node);
-
-void *list_remove_front(list_t *list);
-
-void *list_remove_back(list_t *list);
-
-void list_push(list_t *list, void *val);
-
-listnode_t *list_pop(list_t *list);
-
-void list_enqueue(list_t *list, void *val);
-
-listnode_t *list_dequeue(list_t *list);
-
-void *list_peek_front(list_t *list);
-
-void *list_peek_back(list_t *list);
-
-void list_destroy(list_t *list);
-
-void listnode_destroy(listnode_t *node);
-
-int list_contain(list_t *list, void *val);
-
-listnode_t *list_get_node_by_index(list_t *list, int index);
-
-void *list_remove_by_index(list_t *list, int index);
-
-char *list2str(list_t *list, const char *delim);
-
-list_t *str_split(const char *str, const char *delim, uint32_t *numtokens);
-
-#endif \ No newline at end of file