From c6c12d93a07d9a53a457d741b5df0ada2d40c6c8 Mon Sep 17 00:00:00 2001 From: kamkow1 Date: Fri, 15 Aug 2025 19:30:42 +0200 Subject: [PATCH] KVFS improvements --- kernel/errors.h | 9 +++++---- kernel/fs/kvfs/kvfs.c | 39 ++++++++++++++++++++++++++++----------- kernel/fs/kvfs/kvfs.h | 5 +++-- kernel/vfs/vfs.c | 41 ++++++++++++++++++++++++++++++++++------- kernel/vfs/vfs.h | 10 ++++++---- 5 files changed, 76 insertions(+), 28 deletions(-) diff --git a/kernel/errors.h b/kernel/errors.h index 6b7e7f7..cc1b994 100644 --- a/kernel/errors.h +++ b/kernel/errors.h @@ -1,9 +1,10 @@ #ifndef ERRORS_H_ #define ERRORS_H_ -#define E_OK 0 -#define E_NOMEMORY -1 -#define E_UNKNOWN_FSTYPE -2 -#define E_NOENTRY -3 +#define E_OK 0 +#define E_NOMEMORY -1 +#define E_UNKNOWN_FSTYPE -2 +#define E_NOENTRY -3 +#define E_OUTOFBOUNDS -4 #endif // ERRORS_H_ diff --git a/kernel/fs/kvfs/kvfs.c b/kernel/fs/kvfs/kvfs.c index 79f0328..3ea0bb1 100644 --- a/kernel/fs/kvfs/kvfs.c +++ b/kernel/fs/kvfs/kvfs.c @@ -10,7 +10,7 @@ #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) { +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); @@ -22,12 +22,12 @@ int32_t kvfs_read(struct VfsMountPoint *vmp, const char *key, uint8_t *const buf } spinlock_acquire(&node->spinlock); - hal_memcpy(buffer, node->buffer, MIN(n, KVFS_BUFFER_SIZE)); + hal_memcpy(buffer + off, node->buffer, MIN(n, KVFS_BUFFER_SIZE - off)); spinlock_release(&node->spinlock); return E_OK; } -int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n) { +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); @@ -42,13 +42,15 @@ int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *co spinlock_release(&vmp->spinlock); spinlock_acquire(&node->spinlock); - node->buffer = dlmalloc(KVFS_BUFFER_SIZE); if (node->buffer == NULL) { - spinlock_release(&node->spinlock); - return E_NOMEMORY; + node->buffer = dlmalloc(KVFS_BUFFER_SIZE); + if (node->buffer == NULL) { + spinlock_release(&node->spinlock); + return E_NOMEMORY; + } } - hal_memcpy(node->buffer, buffer, MIN(n, KVFS_BUFFER_SIZE)); + hal_memcpy(node->buffer + off, buffer, MIN(n, KVFS_BUFFER_SIZE - off)); spinlock_release(&node->spinlock); return E_OK; } @@ -65,26 +67,41 @@ int32_t kvfs_remove(struct VfsMountPoint *vmp, const char *key) { } spinlock_acquire(&node->spinlock); + if (node->buffer != NULL) { + dlfree(node->buffer); + } hal_memset(node, 0, sizeof(*node)); spinlock_release(&node->spinlock); return E_OK; } +int32_t kvfs_cleanup(struct VfsMountPoint *vmp) { + for (size_t i = 0; i < KVFS_NODES_MAX; i++) { + KvfsNode *node = &vmp->fs.kvfs.nodes[i]; + spinlock_acquire(&node->spinlock); + if (node->buffer != NULL) { + dlfree(node->buffer); + } + spinlock_release(&node->spinlock); + } + return E_OK; +} + bool kvfs_check(void) { int32_t ret; char *hello = "WAWAWAWA!!!"; - ret = vfs_write("+tmpvars", "hello", hello, hal_strlen(hello)+1); + 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)); + ret = vfs_read("tmpvars", "hello", buf, sizeof(buf), 0); if (ret != E_OK) return false; - vfs_remove("+tmpvars", "hello"); + vfs_remove("tmpvars", "hello"); if (ret != E_OK) return false; - ret = vfs_read("+tmpvars", "hello", buf, sizeof(buf)); + ret = vfs_read("tmpvars", "hello", buf, sizeof(buf), 0); if (ret != E_NOENTRY) return false; return true; diff --git a/kernel/fs/kvfs/kvfs.h b/kernel/fs/kvfs/kvfs.h index f82ac4f..639a577 100644 --- a/kernel/fs/kvfs/kvfs.h +++ b/kernel/fs/kvfs/kvfs.h @@ -20,9 +20,10 @@ 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); -int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n); +int32_t kvfs_read(struct VfsMountPoint *vmp, const char *key, uint8_t *const buffer, size_t n, size_t off); +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_cleanup(struct VfsMountPoint *vmp); bool kvfs_check(void); #endif // FS_KVFS_KVFS_H_ diff --git a/kernel/vfs/vfs.c b/kernel/vfs/vfs.c index 95c90a1..1462f4e 100644 --- a/kernel/vfs/vfs.c +++ b/kernel/vfs/vfs.c @@ -14,7 +14,7 @@ VfsTable VFS_TABLE; static const char *vfs_strings[] = { [VFS_FATFS] = "FAT", - [VFS_KVFS] = "Kvfs", + [VFS_KVFS] = "KVFS", }; void vfs_init_kvfs(VfsMountPoint *mp) { @@ -22,6 +22,7 @@ void vfs_init_kvfs(VfsMountPoint *mp) { mp->write = &kvfs_write; mp->remove = &kvfs_remove; mp->check = &kvfs_check; + mp->cleanup = &kvfs_cleanup; } int32_t vfs_mount(char *mountpoint, int32_t fstype) { @@ -48,7 +49,7 @@ int32_t vfs_mount(char *mountpoint, int32_t fstype) { return E_OK; } -int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n) { +int32_t vfs_unmount(char *mountpoint) { VfsMountPoint *mp = NULL; spinlock_acquire(&VFS_TABLE.spinlock); @@ -59,10 +60,18 @@ int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size return E_NOENTRY; } - return mp->read(mp, path, buffer, n); + spinlock_acquire(&mp->spinlock); + int32_t err = mp->cleanup(mp); + if (err != E_OK) { + spinlock_release(&mp->spinlock); + return err; + } + hal_memset(mp, 0, sizeof(*mp)); + spinlock_release(&mp->spinlock); + return E_OK; } -int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n) { +int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n, size_t off) { VfsMountPoint *mp = NULL; spinlock_acquire(&VFS_TABLE.spinlock); @@ -73,7 +82,21 @@ int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffe return E_NOENTRY; } - return mp->write(mp, path, buffer, n); + return mp->read(mp, path, buffer, n, off); +} + +int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n, size_t off) { + 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, off); } int32_t vfs_remove(char *mountpoint, const char *path) { @@ -94,7 +117,7 @@ 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"); + ASSERT("vfs", vfs_mount("tmpvars", VFS_KVFS) == E_OK, "could not create tmpvars mount point\n"); LOG("vfs", "init done\n"); @@ -102,7 +125,11 @@ void vfs_init(void) { 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"); + if (vmp->check != NULL) { + LOG("vfs", "check = %s\n", vmp->check() ? "OK" : "FAIL"); + } else { + LOG("vfs", "check skipped\n"); + } } } diff --git a/kernel/vfs/vfs.h b/kernel/vfs/vfs.h index 45e08ca..048c67c 100644 --- a/kernel/vfs/vfs.h +++ b/kernel/vfs/vfs.h @@ -20,9 +20,10 @@ typedef struct VfsMountPoint { uint8_t label[VFS_MOUNTPOINT_LABEL_MAX]; int32_t fstype; - int32_t (*read)(struct VfsMountPoint *vmp, const char *path, uint8_t *const buffer, size_t n); - int32_t (*write)(struct VfsMountPoint *vmp, const char *path, const uint8_t *const buffer, size_t n); + 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 (*cleanup)(struct VfsMountPoint *vmp); bool (*check)(void); union { @@ -39,8 +40,9 @@ typedef struct { extern VfsTable VFS_TABLE; void vfs_init(void); -int32_t vfs_read(char *mountpoint, const char *path, uint8_t *const buffer, size_t n); -int32_t vfs_write(char *mountpoint, const char *path, const uint8_t *const buffer, size_t n); +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_unmount(char *mountpoint); #endif // VFS_VFS_H_