vfs filesystem mounting

This commit is contained in:
Matheus
2022-08-02 22:14:32 -03:00
parent 8d90e27f15
commit 2419b2e9e0
6 changed files with 155 additions and 29 deletions
+31
View File
@@ -0,0 +1,31 @@
#include <kernel/tmpfs.h>
#include <kernel/vfs.h>
#include <kernel/alloc.h>
static int tmpfs_mount(dirnode_t** rootptr, vnode_t* device, int mountflags, void* fsinfo){
fs_t* desc = alloc(sizeof(fs_t));
if(!desc)
return ENOMEM;
dirnode_t* root = vfs_newdirnode(desc, NULL);
if(!root){
free(desc);
return ENOMEM;
}
*rootptr = root;
return 0;
}
static fscalls_t funcs = {
tmpfs_mount
};
fscalls_t* tmpfs_getfuncs(){
return &funcs;
}
+97 -19
View File
@@ -5,24 +5,88 @@
#include <string.h>
dirnode_t* vfsroot;
hashtable fsfuncs;
vnode_t* vfs_newnode(fs_t* fs, void* fsdata){
static inline dirnode_t* mountpoint(dirnode_t* node){
dirnode_t* ret = node;
while(ret->mount)
ret = ret->mount;
return ret;
}
int vfs_mount(dirnode_t* ref, char* device, char* mountpoint, char* fs, int mountflags, void* fsinfo){
fscalls_t* fscalls = hashtable_get(&fsfuncs, fs);
if(!fscalls)
return ENODEV;
vnode_t* dev = NULL;
dirnode_t* mountdir = ref;
if(device){
dev = ref;
if(*device){
int result = vfs_resolvepath(&dev, ref, device);
if(result)
return result;
}
}
int result = vfs_resolvepath(&mountdir, ref, mountpoint);
if(result)
return result;
if(GETTYPE(mountdir->vnode.st.st_mode) != TYPE_DIR)
return ENOTDIR;
dirnode_t* mount;
result = fscalls->mount(&mount, dev, mountflags, fsinfo);
if(result)
return result;
mountdir->mount = mount;
return 0;
}
vnode_t* vfs_newnode(char* name, fs_t* fs, void* fsdata){
vnode_t* node = alloc(sizeof(vnode_t));
if(!node) return NULL;
node->name = alloc(strlen(name)+1);
if(!name){
free(node);
return NULL;
}
node->fs = fs;
node->fsdata = fsdata;
strcpy(node->name, name);
return node;
}
dirnode_t* vfs_newdirnode(fs_t* fs, void* fsdata){
dirnode_t* vfs_newdirnode(char* name, fs_t* fs, void* fsdata){
dirnode_t* node = alloc(sizeof(dirnode_t));
if(!node) return NULL;
vnode_t* vnode = (vnode_t*)node;
vnode->name = alloc(strlen(name)+1);
if(!vnode->name){
free(node);
return NULL;
}
if(!hashtable_init(&node->children, 10)){ // add more here later
free(vnode->name);
free(node);
return NULL;
}
@@ -30,6 +94,10 @@ dirnode_t* vfs_newdirnode(fs_t* fs, void* fsdata){
vnode->st.st_mode = MAKETYPE(TYPE_DIR);
vnode->fs = fs;
vnode->fsdata = fsdata;
strcpy(vnode->name, name);
return node;
}
int vfs_resolvepath(vnode_t** result, dirnode_t* ref, char *path){
@@ -38,25 +106,33 @@ int vfs_resolvepath(vnode_t** result, dirnode_t* ref, char *path){
char name[512];
off_t nameoffset = 0;
while(nameoffset <= strlen(path)){
memset(name, 0, 512);
iterator = mountpoint(iterator);
while(nameoffset < strlen(path)){
size_t offset = 0;
while(path[nameoffset])
while(path[nameoffset] && path[nameoffset] != '/')
name[offset++] = path[nameoffset++];
if(path[nameoffset] == '/')
++nameoffset;
name[offset] = 0;
// make sure we're a directory
if(!GETTYPE(iterator->vnode.st.st_mode) == TYPE_DIR)
return ENOTDIR;
// find correct node if mountmoint
while(iterator->mountednode)
iterator = iterator->mountednode;
// get the right vnode for this point
iterator = mountpoint(iterator);
// now try to get the right child
vnode_t* child = hashtable_get(&iterator->children, name);
if(!child){
@@ -72,7 +148,7 @@ int vfs_resolvepath(vnode_t** result, dirnode_t* ref, char *path){
iterator = (dirnode_t*)child;
}
*result = (vnode_t*)iterator;
return 0;
@@ -93,21 +169,23 @@ dirnode_t* vfs_root(){
}
void vfs_acquirenode(vnode_t* node){
while(node){
++node->refcount;
node = (vnode_t*)node->parent;
}
++node->refcount;
}
void vfs_releasenode(vnode_t* node){
while(node){
--node->refcount;
node = (vnode_t*)node->parent;
}
--node->refcount;
}
#include <kernel/tmpfs.h>
void vfs_init(){
printf("Creating a fake root for the VFS\n");
vfsroot = vfs_newdirnode(NULL, NULL);
vfsroot = vfs_newdirnode("/", NULL, NULL);
hashtable_init(&fsfuncs, 10);
hashtable_insert(&fsfuncs, "tmpfs", (void*)tmpfs_getfuncs());
}