vfs: add initial ramfs driver
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
dec9b2c715
commit
e4122d1c65
5 changed files with 244 additions and 8 deletions
|
|
@ -1,3 +1,4 @@
|
|||
# SPDX-License-Identifier: GPL-2.0-only
|
||||
obj-y += vfs.o
|
||||
obj-y += inode.o
|
||||
obj-y += ramfs.o
|
||||
|
|
|
|||
235
fs/vfs/ramfs.c
Normal file
235
fs/vfs/ramfs.c
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/slab.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;
|
||||
|
||||
int ramfs_create(u64 flags, fs_t **out)
|
||||
{
|
||||
int ret = 0;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
*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;
|
||||
read_lock(&inode->rwlock);
|
||||
if (!inode->data)
|
||||
goto out;
|
||||
|
||||
if (offset >= inode->file_size)
|
||||
goto out;
|
||||
|
||||
if (inode->file_size == 0)
|
||||
goto out;
|
||||
|
||||
if (size + offset >= inode->file_size)
|
||||
size = inode->file_size - offset - 1;
|
||||
|
||||
memcpy(buf, inode->data + offset, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
read_unlock(&inode->rwlock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
write_lock(&inode->rwlock);
|
||||
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);
|
||||
if (!inode->data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(inode->data + offset, buf, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
write_unlock(&inode->rwlock);
|
||||
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;
|
||||
}
|
||||
|
||||
new->inode = to;
|
||||
new->fs_ptr = in->fs_ptr;
|
||||
|
||||
spinlock_acquire(&in->lock);
|
||||
write_lock(&in->inode->rwlock);
|
||||
|
||||
list_insert(&in->inode->dirents, new);
|
||||
|
||||
write_unlock(&in->inode->rwlock);
|
||||
spinlock_release(&in->lock);
|
||||
|
||||
*out = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int unlink(dirent_t *dent)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
||||
{
|
||||
read_lock(&parent->rwlock);
|
||||
/* 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;
|
||||
}
|
||||
|
||||
read_unlock(&parent->rwlock);
|
||||
|
||||
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);
|
||||
10
fs/vfs/vfs.c
10
fs/vfs/vfs.c
|
|
@ -44,9 +44,9 @@ static char *path_trim(const char *name)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static int next_level(const char *path, inode_t *dir, dirent_t **out_dirent)
|
||||
static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
||||
{
|
||||
if (!dir || !out_dirent)
|
||||
if (!dent || !out_dirent)
|
||||
return -EINVAL;
|
||||
|
||||
int res;
|
||||
|
|
@ -57,14 +57,14 @@ static int next_level(const char *path, inode_t *dir, dirent_t **out_dirent)
|
|||
|
||||
if (c == '/' || c == '\0') {
|
||||
dirent_t *dirent;
|
||||
res = inode_find_dirent(dir, build, &dirent);
|
||||
res = inode_find_dirent(dent->inode, build, &dirent);
|
||||
if (res < 0)
|
||||
return -ENOENT;
|
||||
|
||||
spinlock_acquire(&dirent->lock);
|
||||
|
||||
if (!dirent->inode) {
|
||||
fs_t *fs = dir->dentry->fs;
|
||||
fs_t *fs = dent->fs;
|
||||
u64 inum = dirent->ino_num;
|
||||
res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode);
|
||||
if (res < 0) {
|
||||
|
|
@ -116,7 +116,7 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
while (1) {
|
||||
dirent_t *next_dir;
|
||||
|
||||
int res = next_level(cur_path, current_dir->inode, &next_dir);
|
||||
int res = next_level(cur_path, current_dir, &next_dir);
|
||||
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(current_dir);
|
||||
|
|
|
|||
|
|
@ -49,7 +49,6 @@ struct _inode {
|
|||
u64 mode;
|
||||
u64 ino_num;
|
||||
u64 file_size;
|
||||
dirent_t *dentry;
|
||||
|
||||
u64 flags;
|
||||
|
||||
|
|
@ -60,7 +59,6 @@ struct _inode {
|
|||
|
||||
list_t dirents;
|
||||
|
||||
struct _inode *parent_inode;
|
||||
struct inode_ops i_ops;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@
|
|||
|
||||
#include <fs/vfs.h>
|
||||
|
||||
dirent_t *ramfs_mount(int flags, struct block_device *bdev);
|
||||
#define FS_TYPE_RAMFS 0x1234
|
||||
|
||||
int ramfs_create(u64 flags, fs_t **out);
|
||||
int ramfs_umount(fs_t *fs);
|
||||
|
||||
#endif /* _RAMFS_H_ */
|
||||
|
|
|
|||
Loading…
Reference in a new issue