Redesign the VFS
This commit is contained in:
@ -1,123 +0,0 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "spinlock/spinlock.h"
|
||||
#include "errors.h"
|
||||
#include "hal/hal.h"
|
||||
#include "hshtb.h"
|
||||
#include "kprintf.h"
|
||||
#include "vfs/vfs.h"
|
||||
#include "dlmalloc/malloc.h"
|
||||
#include "util/util.h"
|
||||
|
||||
int32_t kvfs_read(struct VfsMountPoint *vmp, const char *key, uint8_t *const buffer, size_t n, size_t off) {
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
|
||||
if (node == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
spinlock_acquire(&node->spinlock);
|
||||
vmp->backingsd->read(vmp->backingsd, buffer, n, off);
|
||||
spinlock_release(&node->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t kvfs_stat(struct VfsMountPoint *vmp, const char *key, struct VfsStat *stat) {
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
|
||||
if (node == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
stat->type = VFS_TYPE_FILE;
|
||||
stat->size = KVFS_BUFFER_SIZE;
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n, size_t off) {
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
if (node == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
spinlock_acquire(&node->spinlock);
|
||||
vmp->backingsd->write(vmp->backingsd, buffer, n, off);
|
||||
spinlock_release(&node->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t kvfs_remove(struct VfsMountPoint *vmp, const char *key) {
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
|
||||
if (node == NULL) {
|
||||
return E_NOENTRY;
|
||||
}
|
||||
|
||||
spinlock_acquire(&node->spinlock);
|
||||
hal_memset(node, 0, sizeof(*node));
|
||||
spinlock_release(&node->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t kvfs_cleanup(struct VfsMountPoint *vmp) {
|
||||
int32_t err = vmp->backingsd->cleanup(vmp->backingsd);
|
||||
if (err != E_OK) {
|
||||
return err;
|
||||
}
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t kvfs_create(struct VfsMountPoint *vmp, const char *path, int32_t type) {
|
||||
(void)type;
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_ALLOC(&vmp->fs.kvfs, nodes, (char *)path, key_, node);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
|
||||
if (node == NULL) {
|
||||
return E_NOMEMORY;
|
||||
}
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
bool kvfs_check(void) {
|
||||
int32_t ret;
|
||||
|
||||
ret = vfs_create("tmpvars", "hello", VFS_TYPE_FILE);
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
char *hello = "WAWAWAWA!!!";
|
||||
ret = vfs_write("tmpvars", "hello", hello, hal_strlen(hello)+1, 0);
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
char buf[20];
|
||||
ret = vfs_read("tmpvars", "hello", buf, sizeof(buf), 0);
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
vfs_remove("tmpvars", "hello");
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
ret = vfs_read("tmpvars", "hello", buf, sizeof(buf), 0);
|
||||
if (ret != E_NOENTRY) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -1,32 +0,0 @@
|
||||
#ifndef FS_KVFS_KVFS_H_
|
||||
#define FS_KVFS_KVFS_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct VfsMountPoint;
|
||||
struct VfsStat;
|
||||
|
||||
#define KVFS_NODE_KEY_MAX 128
|
||||
#define KVFS_NODES_MAX 256
|
||||
#define KVFS_BUFFER_SIZE (1024 * 2)
|
||||
|
||||
typedef struct {
|
||||
bool taken;
|
||||
uint8_t key_[KVFS_NODE_KEY_MAX];
|
||||
SpinLock spinlock;
|
||||
} KvfsNode;
|
||||
|
||||
typedef struct {
|
||||
KvfsNode nodes[KVFS_NODES_MAX];
|
||||
} Kvfs;
|
||||
|
||||
int32_t kvfs_read(struct VfsMountPoint *vmp, const char *key, uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t kvfs_stat(struct VfsMountPoint *vmp, const char *key, struct VfsStat *stat);
|
||||
int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t kvfs_remove(struct VfsMountPoint *vmp, const char *key);
|
||||
int32_t kvfs_create(struct VfsMountPoint *vmp, const char *path, int32_t type);
|
||||
int32_t kvfs_cleanup(struct VfsMountPoint *vmp);
|
||||
bool kvfs_check(void);
|
||||
|
||||
#endif // FS_KVFS_KVFS_H_
|
@ -5,81 +5,7 @@
|
||||
#include "errors.h"
|
||||
#include "kprintf.h"
|
||||
#include "dlmalloc/malloc.h"
|
||||
|
||||
#define CHECK(err) \
|
||||
do { \
|
||||
int ok = (err); \
|
||||
if (ok < 0) goto bad; \
|
||||
} while(0)
|
||||
|
||||
int32_t littlefs_read(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off) {
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
lfs_file_t file;
|
||||
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_RDONLY));
|
||||
CHECK(lfs_file_seek(&fs->instance, &file, off, LFS_SEEK_SET));
|
||||
CHECK(lfs_file_read(&fs->instance, &file, buffer + off, n));
|
||||
CHECK(lfs_file_close(&fs->instance, &file));
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_OK;
|
||||
bad:
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, struct VfsStat *stat) {
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
struct lfs_info stat1;
|
||||
CHECK(lfs_stat(&fs->instance, path, &stat1));
|
||||
|
||||
if (stat1.type == LFS_TYPE_REG) {
|
||||
stat->type = VFS_TYPE_FILE;
|
||||
} else if (stat1.type == LFS_TYPE_DIR) {
|
||||
stat->type = VFS_TYPE_DIR;
|
||||
}
|
||||
|
||||
stat->size = stat1.size;
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_OK;
|
||||
bad:
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
int32_t littlefs_write(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off) {
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
lfs_file_t file;
|
||||
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_WRONLY));
|
||||
CHECK(lfs_file_seek(&fs->instance, &file, off, LFS_SEEK_SET));
|
||||
CHECK(lfs_file_write(&fs->instance, &file, buffer, n));
|
||||
CHECK(lfs_file_close(&fs->instance, &file));
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_OK;
|
||||
bad:
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
int32_t littlefs_remove(struct VfsMountPoint *vmp, const char *path) {
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
CHECK(lfs_remove(&fs->instance, path));
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_OK;
|
||||
bad:
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
#include "hal/hal.h"
|
||||
|
||||
int32_t littlefs_cleanup(struct VfsMountPoint *vmp) {
|
||||
dlfree(vmp->fs.littlefs.instance.cfg);
|
||||
@ -92,30 +18,106 @@ int32_t littlefs_cleanup(struct VfsMountPoint *vmp) {
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t littlefs_create(struct VfsMountPoint *vmp, const char *path, int32_t type) {
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
switch (type) {
|
||||
case VFS_TYPE_DIR:
|
||||
CHECK(lfs_mkdir(&fs->instance, path));
|
||||
break;
|
||||
case VFS_TYPE_FILE: {
|
||||
lfs_file_t file;
|
||||
CHECK(lfs_file_open(&fs->instance, &file, path, LFS_O_CREAT | LFS_O_WRONLY));
|
||||
CHECK(lfs_file_close(&fs->instance, &file));
|
||||
} break;
|
||||
void littlefs_vobj_cleanup(struct VfsObj *vobj) {
|
||||
if (vobj->extra != NULL) {
|
||||
lfs_file_close(&vobj->vmp->fs.littlefs.instance, (lfs_file_t *)vobj->extra);
|
||||
dlfree(vobj->extra);
|
||||
}
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_OK;
|
||||
bad:
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_BADIO;
|
||||
dlfree(vobj);
|
||||
}
|
||||
|
||||
bool littlefs_check(void) {
|
||||
return true;
|
||||
int32_t littlefs_vobj_read(struct VfsObj *vobj, uint8_t *const buffer, size_t n, size_t off) {
|
||||
spinlock_acquire(&vobj->spinlock);
|
||||
|
||||
int ok = lfs_file_seek(&vobj->vmp->fs.littlefs.instance, (lfs_file_t *)vobj->extra, off, LFS_SEEK_SET);
|
||||
if (ok < 0) {
|
||||
spinlock_release(&vobj->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
ok = lfs_file_read(&vobj->vmp->fs.littlefs.instance, (lfs_file_t *)vobj->extra, buffer, n);
|
||||
if (ok < 0) {
|
||||
spinlock_release(&vobj->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
spinlock_release(&vobj->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
int32_t littlefs_vobj_stat(struct VfsObj *vobj, struct VfsStat *stat) {
|
||||
struct lfs_info statbuf;
|
||||
|
||||
spinlock_acquire(&vobj->spinlock);
|
||||
|
||||
int ok = lfs_stat(&vobj->vmp->fs.littlefs.instance, vobj->path, &statbuf);
|
||||
if (ok < 0) {
|
||||
spinlock_release(&vobj->spinlock);
|
||||
return E_BADIO;
|
||||
}
|
||||
|
||||
if (statbuf.type == LFS_TYPE_REG) {
|
||||
stat->type = VFS_TYPE_FILE;
|
||||
} else if (statbuf.type == LFS_TYPE_DIR) {
|
||||
stat->type = VFS_TYPE_DIR;
|
||||
}
|
||||
stat->size = statbuf.size;
|
||||
|
||||
spinlock_release(&vobj->spinlock);
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32_t flags) {
|
||||
VfsObj *vobj = dlmalloc(sizeof(*vobj));
|
||||
if (vobj == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
hal_memset(vobj, 0, sizeof(*vobj));
|
||||
spinlock_init(&vobj->spinlock);
|
||||
vobj->refs++;
|
||||
|
||||
int lfs_flags = 0;
|
||||
lfs_file_t *file = dlmalloc(sizeof(*file));
|
||||
if (file == NULL) {
|
||||
dlfree(vobj);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
|
||||
LittleFs *fs = &vmp->fs.littlefs;
|
||||
|
||||
if (flags & VFS_FLAG_MAKE) {
|
||||
lfs_flags |= LFS_O_CREAT;
|
||||
}
|
||||
|
||||
if (flags == VFS_FLAG_READ) {
|
||||
lfs_flags |= LFS_O_RDONLY;
|
||||
} else if (flags == VFS_FLAG_WRITE) {
|
||||
lfs_flags |= LFS_O_WRONLY;
|
||||
} else if ((flags & VFS_FLAG_READ) && (flags & VFS_FLAG_WRITE)) {
|
||||
lfs_flags |= LFS_O_RDWR;
|
||||
}
|
||||
|
||||
int ok = lfs_file_open(&fs->instance, file, path, lfs_flags);
|
||||
|
||||
if (ok < 0) {
|
||||
dlfree(vobj);
|
||||
dlfree(file);
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vobj->extra = file;
|
||||
vobj->extrasize = sizeof(*file);
|
||||
vobj->vmp = vmp;
|
||||
vobj->cleanup = &littlefs_vobj_cleanup;
|
||||
vobj->read = &littlefs_vobj_read;
|
||||
vobj->stat = &littlefs_vobj_stat;
|
||||
hal_strcpy(vobj->path, path);
|
||||
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return vobj;
|
||||
}
|
||||
|
||||
int portlfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) {
|
||||
|
@ -9,18 +9,14 @@
|
||||
|
||||
struct VfsMountPoint;
|
||||
struct VfsStat;
|
||||
struct VfsObj;
|
||||
|
||||
typedef struct {
|
||||
lfs_t instance;
|
||||
} LittleFs;
|
||||
|
||||
int32_t littlefs_read(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, struct VfsStat *stat);
|
||||
int32_t littlefs_write(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off);
|
||||
int32_t littlefs_remove(struct VfsMountPoint *vmp, const char *path);
|
||||
int32_t littlefs_create(struct VfsMountPoint *vmp, const char *path, int32_t type);
|
||||
int32_t littlefs_cleanup(struct VfsMountPoint *vmp);
|
||||
bool littlefs_check(void);
|
||||
struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32_t flags);
|
||||
|
||||
int portlfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
|
||||
int portlfs_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size);
|
||||
|
Reference in New Issue
Block a user