vbos/fs/vfs/ramfs.c
2026-05-12 07:31:17 -04:00

161 lines
3.3 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
/* ramfs: in-ram filesystem
*
* notes:
* inode->ino_num is a the length of the current kalloc buffer
* inode->data is a pointer to the direct data block of the file
* dirent->ino_num is a pointer to the inode
*/
#include "fs/vfs.h"
#include "lib/errno.h"
#include "lib/list.h"
#include <kernel/common.h>
#include <kernel/slab.h>
#include <lib/string.h>
#include <fs/ramfs.h>
static int alloc_inode(fs_t *fs, inode_t **out)
{
const size_t alloc_len = 0x4000;
inode_t *ret = inode_create();
if (!ret)
return -ENOMEM;
memset(ret, 0, sizeof(*ret));
ret->fs = fs;
ret->ino_num = alloc_len;
ret->data = kalloc(alloc_len, ALLOC_KERN);
*out = ret;
return 0;
}
static int free_inode(fs_t *fs, inode_t *inode)
{
inode_free(inode);
return 0;
}
static int open_inode(fs_t *fs, u64 id, inode_t **out)
{
if (!id)
return -ENOENT;
*out = (void *)id;
return 0;
}
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
{
if (offset < inode->file_size) {
size = MIN(size, inode->file_size - offset);
memcpy(buf, inode->data + offset, size);
return size;
}
return 0;
}
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
{
size_t cur_len = inode->ino_num;
if (offset + size >= cur_len) {
size_t new_size = npow2(size + offset);
void *new_buf = krealloc(inode->data, new_size, ALLOC_KERN);
if (!new_buf)
return -ENOMEM;
inode->ino_num = new_size;
inode->data = new_buf;
}
memcpy(inode->data + offset, buf, size);
return size;
}
static int creat(inode_t *inode, const char *name, u64 mode, dirent_t **out)
{
if (inode_find_dirent(inode, name))
return -EEXIST;
dirent_t *dirent = dirent_create(name);
if (!dirent)
return -ENOMEM;
inode_t *new_ino;
int ret = alloc_inode(inode->fs, &new_ino);
if (ret < 0)
goto out_1;
dirent->inode = new_ino;
inode->mode = mode;
list_insert(&inode->dentries, dirent);
*out = dirent;
return 0;
out_1:
dirent_free(dirent);
return ret;
}
static int mkdir(inode_t *inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_DIRECTORY, &d);
if (ret >= 0)
*out = d;
return ret;
}
int link(inode_t *inode, inode_t *link_inode, const char *name, dirent_t **out)
{
dirent_t *d;
int ret = creat(inode, name, INODE_SYMLINK, &d);
if (ret >= 0)
*out = d;
return ret;
}
static int unlink(inode_t *inode, const char *name)
{
if (inode->mode != INODE_DIRECTORY)
return -ENOTDIR;
list_item_t *item = inode->dentries.head;
size_t name_len = strlen(name);
while (item) {
dirent_t *ent = item->ptr;
if (!strncmp(name, ent->name, name_len)) {
list_remove(item);
dirent_free(ent);
return 0;
}
item = item->next;
}
return -ENOENT;
}
static int readdir(inode_t *parent)
{
if (parent->mode != INODE_DIRECTORY)
return -ENOTDIR;
return 0;
}
static dirent_t *ramfs_mount(int flags, struct block_device *bdev)
{
static struct inode_ops ramfs_inode_ops = {
.read = read, .write = write, .mkdir = mkdir, .creat = creat, .link = link, .unlink = unlink, .readdir = readdir};
static struct fs_ops ramfs_ops = {.alloc_inode = alloc_inode, .free_inode = free_inode, .open_inode = open_inode};
}
void ramfs_init()
{
struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = ramfs_mount};
}
DRIVER_ENTRY_PRIORITY(ramfs_init, 2);