vbos/fs/vfs/ramfs.c
Michael Shaw 3ca769014c ramfs: write: update file size when size increased
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-05 14:16:02 +02:00

229 lines
4 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/slab.h>
#include <kernel/atomic.h>
#include <fs/vfs.h>
#include <fs/ramfs.h>
#include <lib/string.h>
static struct fs_ops *ramfs_ops = NULL;
static struct inode_ops *ramfs_inode_ops = NULL;
static u64 inode_ticker = 1;
int ramfs_create(u64 flags, fs_t **out)
{
int ret;
fs_t *fs = kalloc(sizeof(fs_t), ALLOC_KERN);
if (!fs) {
ret = -ENOMEM;
goto out_fs;
}
dirent_t *root = dirent_create("/");
if (!root) {
ret = -ENOMEM;
goto out_root;
}
inode_t *root_inode = inode_create();
if (!root_inode) {
ret = -ENOMEM;
goto out_root_inode;
}
fs->type = FS_TYPE_RAMFS;
fs->flags = flags;
fs->block_size = 1;
fs->n_blocks = 0;
memcpy(&fs->fs_ops, ramfs_ops, sizeof(struct fs_ops));
root_inode->mode = S_IFDIR;
fs->root_dirent = root;
root->type = S_IFDIR;
root->fs = fs;
root->inode = root_inode;
*out = fs;
return 0;
out_root_inode:
dirent_free(root);
out_root:
kfree(fs);
out_fs:
return ret;
}
static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
{
if (!out || !fs)
return -EINVAL;
inode_t *ret = inode_create();
if (!ret)
return -ENOMEM;
ret->mode = mode;
ret->ino_num = atomic_xadd(&inode_ticker, 1);
*out = ret;
return 0;
}
static int free_inode(fs_t *fs, inode_t *inode)
{
kfree(inode->data);
inode_free(inode);
return 0;
}
static int open_inode(fs_t *fs, u64 id, dirent_t *dirent, inode_t **out)
{
return -ENOENT;
}
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
{
ssize_t ret = 0;
if (!inode->data)
return 0;
if (inode->file_size == 0)
goto out;
if (offset >= inode->file_size)
return -EINVAL;
if (size + offset >= inode->file_size)
size = inode->file_size - offset - 1;
memcpy(buf, inode->data + offset, size);
ret = size;
out:
return ret;
}
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
{
ssize_t ret = 0;
if (!inode->data) {
inode->data = kalloc(size + offset, ALLOC_KERN);
inode->file_size = size + offset;
}
if (!inode->data) {
ret = -ENOMEM;
goto out;
}
if (inode->file_size < size + offset) {
inode->data = krealloc(inode->data, size + offset, ALLOC_KERN);
inode->file_size = size + offset;
if (!inode->data) {
ret = -ENOMEM;
goto out;
}
}
memcpy(inode->data + offset, buf, size);
ret = size;
out:
return ret;
}
static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
{
if (!in || !to || !name || !out)
return -EINVAL;
dirent_t *new = dirent_create(name);
if (!new) {
return -ENOMEM;
}
inode_inc_ref(to);
new->inode = to;
new->fs_ptr = in->fs_ptr;
list_insert(&in->inode->dirents, new);
*out = new;
return 0;
}
static int unlink(dirent_t *dent)
{
return 0;
}
static ssize_t get_dents(inode_t *parent, dirent_t **dents)
{
/* count first */
list_item_t *item = parent->dirents.head;
ssize_t n = 0;
while (item) {
n++;
item = item->next;
}
/* now copy */
dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN);
item = parent->dirents.head;
n = 0;
while (item) {
memcpy(arr + n, item->ptr, sizeof(dirent_t));
n++;
item = item->next;
}
return n;
}
static dirent_t *mount(int flags, struct block_device *bdev)
{
fs_t *new_fs;
ssize_t res = ramfs_create(flags, &new_fs);
if (res < 0)
return NULL;
return new_fs->root_dirent;
}
static int umount(fs_t *fs)
{
return -EINVAL;
}
void ramfs_init()
{
void *ops = kalloc(sizeof(struct fs_ops) + sizeof(struct inode_ops), ALLOC_KERN);
if (!ops) {
kerr("failed to init ramfs\n");
return;
}
ramfs_ops = ops;
ramfs_inode_ops = ops + sizeof(struct fs_ops);
ramfs_ops->alloc_inode = alloc_inode;
ramfs_ops->free_inode = free_inode;
ramfs_ops->open_inode = open_inode;
ramfs_inode_ops->read = read;
ramfs_inode_ops->write = write;
ramfs_inode_ops->link = link;
ramfs_inode_ops->unlink = unlink;
ramfs_inode_ops->get_dents = get_dents;
struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = mount, .fs_umount = umount};
vfs_register(&ramfs_type);
}
DRIVER_ENTRY(ramfs_init);