Simple VFS layer
This commit is contained in:
92
kernel/fs/kvfs/kvfs.c
Normal file
92
kernel/fs/kvfs/kvfs.c
Normal file
@ -0,0 +1,92 @@
|
||||
#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) {
|
||||
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_memcpy(buffer, node->buffer, MIN(n, KVFS_BUFFER_SIZE));
|
||||
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) {
|
||||
KvfsNode *node = NULL;
|
||||
|
||||
spinlock_acquire(&vmp->spinlock);
|
||||
HSHTB_GET(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
if (node == NULL) {
|
||||
HSHTB_ALLOC(&vmp->fs.kvfs, nodes, (char *)key, key_, node);
|
||||
if (node == NULL) {
|
||||
spinlock_release(&vmp->spinlock);
|
||||
return E_NOMEMORY;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
hal_memcpy(node->buffer, buffer, MIN(n, KVFS_BUFFER_SIZE));
|
||||
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;
|
||||
}
|
||||
|
||||
bool kvfs_check(void) {
|
||||
int32_t ret;
|
||||
|
||||
char *hello = "WAWAWAWA!!!";
|
||||
ret = vfs_write("+tmpvars", "hello", hello, hal_strlen(hello)+1);
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
char buf[20];
|
||||
ret = vfs_read("+tmpvars", "hello", buf, sizeof(buf));
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
vfs_remove("+tmpvars", "hello");
|
||||
if (ret != E_OK) return false;
|
||||
|
||||
ret = vfs_read("+tmpvars", "hello", buf, sizeof(buf));
|
||||
if (ret != E_NOENTRY) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
28
kernel/fs/kvfs/kvfs.h
Normal file
28
kernel/fs/kvfs/kvfs.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef FS_KVFS_KVFS_H_
|
||||
#define FS_KVFS_KVFS_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
struct VfsMountPoint;
|
||||
|
||||
#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];
|
||||
uint8_t *buffer;
|
||||
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);
|
||||
int32_t kvfs_write(struct VfsMountPoint *vmp, const char *key, const uint8_t *const buffer, size_t n);
|
||||
int32_t kvfs_remove(struct VfsMountPoint *vmp, const char *key);
|
||||
bool kvfs_check(void);
|
||||
|
||||
#endif // FS_KVFS_KVFS_H_
|
Reference in New Issue
Block a user