KVFS improvements

This commit is contained in:
2025-08-15 19:30:42 +02:00
parent d91330ba73
commit c6c12d93a0
5 changed files with 76 additions and 28 deletions

View File

@ -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;

View File

@ -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_