Working port of Little FS

This commit is contained in:
2025-08-16 20:35:00 +02:00
parent 2b0566c56f
commit 8da890e388
20 changed files with 332 additions and 19 deletions

View File

@ -9,7 +9,10 @@
#include "assert.h"
#include "errors.h"
#include "fs/kvfs/kvfs.h"
#include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h"
#include "baseimg/baseimg.h"
#include "dlmalloc/malloc.h"
VfsTable VFS_TABLE;
@ -19,6 +22,41 @@ void vfs_init_kvfs(VfsMountPoint *mp) {
mp->remove = &kvfs_remove;
mp->check = &kvfs_check;
mp->cleanup = &kvfs_cleanup;
mp->create = &kvfs_create;
}
void vfs_init_littlefs(VfsMountPoint *mp) {
struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
hal_memset(cfg, 0, sizeof(*cfg));
cfg->context = mp;
cfg->read = &portlfs_read;
cfg->prog = &portlfs_prog;
cfg->erase = &portlfs_erase;
cfg->sync = &portlfs_sync;
cfg->block_size = LITTLEFS_BLOCK_SIZE;
cfg->block_count = mp->backingsd->capacity(mp->backingsd) / LITTLEFS_BLOCK_SIZE;
cfg->read_size = 64;
cfg->prog_size = 64;
cfg->block_cycles = 16;
cfg->cache_size = 64;
cfg->lookahead_size = 64;
cfg->read_buffer = NULL;
cfg->prog_buffer = NULL;
cfg->lookahead_buffer = NULL;
cfg->name_max = 0;
cfg->file_max = 0;
cfg->attr_max = 0;
int err = lfs_mount(&mp->fs.littlefs.instance, cfg);
if (err < 0) {
ERR("vfs", "Little FS mount failed\n");
}
mp->read = &littlefs_read;
mp->write = &littlefs_write;
mp->remove = &littlefs_remove;
mp->check = &littlefs_check;
mp->cleanup = &littlefs_cleanup;
mp->create = &littlefs_create;
}
int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd) {
@ -39,6 +77,9 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd) {
case VFS_KVFS:
vfs_init_kvfs(mp);
break;
case VFS_LITTLEFS:
vfs_init_littlefs(mp);
break;
default:
return E_UNKNOWN_FSTYPE;
}
@ -82,6 +123,20 @@ int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size
return mp->read(mp, path, buffer, n, off);
}
int32_t vfs_create(char *mountpoint, const char *path, int32_t type) {
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->create(mp, path, type);
}
int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n, size_t off) {
VfsMountPoint *mp = NULL;
@ -119,11 +174,21 @@ int32_t tmpvars_init(void) {
return vfs_mount("tmpvars", VFS_KVFS, backingsd);
}
int32_t base_init(void) {
RamSdInitExtra extra = { .capacity = baseimg_getsize(), .preallocbuffer = (uint8_t*)baseimg_getaddr() };
StoreDev *backingsd = storedev_create(STOREDEV_RAMSD, &extra);
if (backingsd == NULL) {
return E_NOMEMORY;
}
return vfs_mount("base", VFS_LITTLEFS, backingsd);
}
void vfs_init(void) {
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
spinlock_init(&VFS_TABLE.spinlock);
tmpvars_init();
base_init();
LOG("vfs", "init done\n");
@ -133,7 +198,12 @@ void vfs_init(void) {
LOG("vfs", "mount point %s: %s, backing device: %s\n",
vmp->label, vfs_strings[vmp->fstype], storedev_strings[vmp->backingsd->sdtype]);
if (vmp->check != NULL) {
LOG("vfs", "check = %s\n", vmp->check() ? "OK" : "FAIL");
bool ok = vmp->check();
if (ok) {
LOG("vfs", "check = %s\n", "OK");
} else {
ERR("vfs", "check = %s\n", "FAIL");
}
} else {
LOG("vfs", "check skipped\n");
}

View File

@ -6,6 +6,7 @@
#include <stdbool.h>
#include "spinlock/spinlock.h"
#include "fs/kvfs/kvfs.h"
#include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h"
#define VFS_MOUNTPOINT_LABEL_MAX 128
@ -13,10 +14,17 @@
enum {
VFS_KVFS,
VFS_LITTLEFS,
};
static const char *vfs_strings[] = {
"KVFS"
"KVFS",
"Little FS",
};
enum {
VFS_CREATE_DIR,
VFS_CREATE_FILE,
};
typedef struct VfsMountPoint {
@ -28,11 +36,13 @@ typedef struct VfsMountPoint {
int32_t (*read)(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n, size_t off);
int32_t (*write)(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n, size_t off);
int32_t (*remove)(struct VfsMountPoint *vmp, const char *path);
int32_t (*create)(struct VfsMountPoint *vmp, const char *path, int32_t type);
int32_t (*cleanup)(struct VfsMountPoint *vmp);
bool (*check)(void);
union {
Kvfs kvfs;
LittleFs littlefs;
} fs;
SpinLock spinlock;
} VfsMountPoint;
@ -48,6 +58,7 @@ void vfs_init(void);
int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n, size_t off);
int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n, size_t off);
int32_t vfs_remove(char *mountpoint, const char *path);
int32_t vfs_create(char *mountpoint, const char *path, int32_t type);
int32_t vfs_unmount(char *mountpoint);
#endif // VFS_VFS_H_