Simple VFS layer
This commit is contained in:
108
kernel/vfs/vfs.c
Normal file
108
kernel/vfs/vfs.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "vfs.h"
|
||||
#include "kprintf.h"
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "hal/hal.h"
|
||||
#include "util/util.h"
|
||||
#include "hshtb.h"
|
||||
#include "assert.h"
|
||||
#include "errors.h"
|
||||
#include "fs/kvfs/kvfs.h"
|
||||
|
||||
VfsTable VFS_TABLE;
|
||||
|
||||
static const char *vfs_strings[] = {
|
||||
[VFS_FATFS] = "FAT",
|
||||
[VFS_KVFS] = "Kvfs",
|
||||
};
|
||||
|
||||
void vfs_init_kvfs(VfsMountPoint *mp) {
|
||||
mp->read = &kvfs_read;
|
||||
mp->write = &kvfs_write;
|
||||
mp->remove = &kvfs_remove;
|
||||
mp->check = &kvfs_check;
|
||||
}
|
||||
|
||||
int32_t vfs_mount(char *mountpoint, int32_t fstype) {
|
||||
VfsMountPoint *mp = NULL;
|
||||
|
||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||
HSHTB_ALLOC(&VFS_TABLE, mountpoints, mountpoint, label, mp);
|
||||
spinlock_release(&VFS_TABLE.spinlock);
|
||||
|
||||
if (mp == NULL) {
|
||||
return E_NOMEMORY;
|
||||
}
|
||||
|
||||
hal_memcpy(mp->label, mountpoint, hal_strlen(mountpoint));
|
||||
mp->fstype = fstype;
|
||||
switch (fstype) {
|
||||
case VFS_KVFS:
|
||||
vfs_init_kvfs(mp);
|
||||
break;
|
||||
default:
|
||||
return E_UNKNOWN_FSTYPE;
|
||||
}
|
||||
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n) {
|
||||
VfsMountPoint *mp = NULL;
|
||||
|
||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||
HSHTB_GET(&VFS_TABLE, mountpoints, mountpoint, label, mp);
|
||||
spinlock_release(&VFS_TABLE.spinlock);
|
||||
|
||||
if (mp == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
return mp->read(mp, path, buffer, n);
|
||||
}
|
||||
|
||||
int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n) {
|
||||
VfsMountPoint *mp = NULL;
|
||||
|
||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||
HSHTB_GET(&VFS_TABLE, mountpoints, mountpoint, label, mp);
|
||||
spinlock_release(&VFS_TABLE.spinlock);
|
||||
|
||||
if (mp == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
return mp->write(mp, path, buffer, n);
|
||||
}
|
||||
|
||||
int32_t vfs_remove(char *mountpoint, const char *path) {
|
||||
VfsMountPoint *mp = NULL;
|
||||
|
||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||
HSHTB_GET(&VFS_TABLE, mountpoints, mountpoint, label, mp);
|
||||
spinlock_release(&VFS_TABLE.spinlock);
|
||||
|
||||
if (mp == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
return mp->remove(mp, path);
|
||||
}
|
||||
|
||||
void vfs_init(void) {
|
||||
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
|
||||
spinlock_init(&VFS_TABLE.spinlock);
|
||||
|
||||
ASSERT("vfs", vfs_mount("+tmpvars", VFS_KVFS) == E_OK, "could not create +tmpvars mount point\n");
|
||||
|
||||
LOG("vfs", "init done\n");
|
||||
|
||||
for (size_t i = 0; i < LEN(VFS_TABLE.mountpoints); i++) {
|
||||
if (!VFS_TABLE.mountpoints[i].taken) continue;
|
||||
VfsMountPoint *vmp = &VFS_TABLE.mountpoints[i];
|
||||
LOG("vfs", "mount point %s: %s\n", vmp->label, vfs_strings[vmp->fstype]);
|
||||
LOG("vfs", "check = %s\n", vmp->check() ? "OK" : "FAIL");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user