246 lines
4.7 KiB
C
246 lines
4.7 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;
|
|
memcpy(&root_inode->i_ops, ramfs_inode_ops, sizeof(struct inode_ops));
|
|
|
|
fs->root_dirent = root;
|
|
root->type = S_IFDIR;
|
|
root->fs = fs;
|
|
root->inode = root_inode;
|
|
root->parent = NULL;
|
|
|
|
*out = fs;
|
|
return 0;
|
|
|
|
out_root_inode:
|
|
inode_dec_ref(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);
|
|
|
|
memcpy(&ret->i_ops, ramfs_inode_ops, sizeof(struct inode_ops));
|
|
|
|
*out = ret;
|
|
return 0;
|
|
}
|
|
|
|
static int free_inode(fs_t *fs, inode_t *inode)
|
|
{
|
|
kfree(inode->data);
|
|
inode_dec_ref(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)
|
|
{
|
|
if (!inode->data || inode->file_size == 0)
|
|
return 0;
|
|
|
|
if (offset >= inode->file_size)
|
|
return -EINVAL;
|
|
|
|
if (size + offset > inode->file_size)
|
|
size = inode->file_size - offset;
|
|
|
|
memcpy(buf, inode->data + offset, size);
|
|
return size;
|
|
}
|
|
|
|
static ssize_t write(inode_t *inode, const char *buf, size_t size, size_t offset)
|
|
{
|
|
size_t new_size = size + offset;
|
|
|
|
if (!inode->data) {
|
|
inode->data = kzalloc(new_size, ALLOC_KERN);
|
|
if (!inode->data)
|
|
return -ENOMEM;
|
|
inode->file_size = new_size;
|
|
} else if (inode->file_size < new_size) {
|
|
u8 *new_data = krealloc(inode->data, new_size, ALLOC_KERN);
|
|
if (!new_data)
|
|
return -ENOMEM;
|
|
if (offset > inode->file_size)
|
|
memset(new_data + inode->file_size, 0, offset - inode->file_size);
|
|
inode->data = new_data;
|
|
inode->file_size = new_size;
|
|
}
|
|
|
|
memcpy(inode->data + offset, buf, size);
|
|
return size;
|
|
}
|
|
|
|
static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out)
|
|
{
|
|
if (!parent_dir || !child || !name || !out)
|
|
return -EINVAL;
|
|
|
|
dirent_t *new = dirent_create(name);
|
|
if (!new)
|
|
return -ENOMEM;
|
|
|
|
new->inode = child;
|
|
|
|
if (!list_insert(&parent_dir->dirents, new)) {
|
|
dirent_free(new);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
*out = new;
|
|
return 0;
|
|
}
|
|
|
|
static int unlink(inode_t *parent_dir, const char *name)
|
|
{
|
|
if (!parent_dir || !name)
|
|
return -EINVAL;
|
|
|
|
list_item_t *item = parent_dir->dirents.head;
|
|
while (item) {
|
|
dirent_t *dent = item->ptr;
|
|
if (!strcmp(dent->name, name)) {
|
|
list_remove(item);
|
|
dirent_dec_ref(dent);
|
|
return 0;
|
|
}
|
|
item = item->next;
|
|
}
|
|
|
|
return -ENOENT;
|
|
}
|
|
|
|
static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
|
{
|
|
list_item_t *item = parent->dirents.head;
|
|
ssize_t n = 0;
|
|
|
|
while (item) {
|
|
n++;
|
|
item = item->next;
|
|
}
|
|
|
|
if (n == 0) {
|
|
*dents = NULL;
|
|
return 0;
|
|
}
|
|
|
|
dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN);
|
|
if (!arr)
|
|
return -ENOMEM;
|
|
|
|
item = parent->dirents.head;
|
|
n = 0;
|
|
while (item) {
|
|
dirent_t *copy = dirent_dup(item->ptr);
|
|
if (!copy) {
|
|
/* free already-copied entries before returning */
|
|
for (ssize_t i = 0; i < n; i++) {
|
|
if (arr[i].inode)
|
|
inode_dec_ref(arr[i].inode);
|
|
kfree(arr[i].name);
|
|
}
|
|
kfree(arr);
|
|
return -ENOMEM;
|
|
}
|
|
arr[n] = *copy;
|
|
kfree(copy);
|
|
n++;
|
|
item = item->next;
|
|
}
|
|
|
|
*dents = arr;
|
|
return n;
|
|
}
|
|
|
|
static int init(int flags, struct block_device *to_mount, fs_t **out)
|
|
{
|
|
return ramfs_create(flags, out);
|
|
}
|
|
|
|
static int deinit(fs_t *fs)
|
|
{
|
|
return -EINVAL;
|
|
}
|
|
|
|
void ramfs_init(void)
|
|
{
|
|
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_driver ramfs_type = {.name = "ramfs", .init = init, .deinit = deinit};
|
|
|
|
vfs_register(&ramfs_type);
|
|
}
|