fs: add ramfs
This commit is contained in:
parent
a7c526d2a6
commit
750ffa2e16
13 changed files with 355 additions and 55 deletions
|
|
@ -9,35 +9,35 @@
|
|||
static mem_cache_t *inode_slab = NULL;
|
||||
static mem_cache_t *dirent_slab = NULL;
|
||||
|
||||
dirent_t *inode_find_dirent(inode_t *inode, char *name)
|
||||
dirent_t *inode_find_dirent(inode_t *inode, const char *name)
|
||||
{
|
||||
list_item_t *p = inode->dentries.head;
|
||||
|
||||
while (p) {
|
||||
dirent_t *dirent = (void *)p->ptr;
|
||||
if (!strncmp(name, dirent->name, dirent->name_len)) {
|
||||
if (!strncmp(name, dirent->name, dirent->name_len))
|
||||
return dirent;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dirent_t *dirent_create(const char *name, inode_t *inode, u64 flags)
|
||||
dirent_t *dirent_create(const char *name)
|
||||
{
|
||||
dirent_t *ret = slab_alloc(dirent_slab);
|
||||
|
||||
if (ret) {
|
||||
ret->inode = inode;
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
ret->flags = flags;
|
||||
ret->rec_len = sizeof(dirent_t);
|
||||
ret->rec_len = sizeof(dirent_t);
|
||||
|
||||
ret->name_len = strlen(name);
|
||||
ret->name = kalloc(ret->name_len + 1, ALLOC_KERN);
|
||||
ret->name[ret->name_len] = 0;
|
||||
memcpy(ret->name, name, ret->name_len);
|
||||
ret->name_len = strlen(name);
|
||||
ret->name = strdup(name);
|
||||
|
||||
if (!ret->name) {
|
||||
slab_free(dirent_slab, ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
@ -69,7 +69,9 @@ void inode_free(inode_t *inode)
|
|||
void inode_init()
|
||||
{
|
||||
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
||||
if(!inode_slab)panic("failed to create inode_slab\n");
|
||||
if (!inode_slab)
|
||||
panic("failed to create inode_slab\n");
|
||||
dirent_slab = slab_create("dentry_slab", sizeof(dirent_t), 64 * KB, 0x8);
|
||||
if(!dirent_slab)panic("failed to create dirent_slab\n");
|
||||
if (!dirent_slab)
|
||||
panic("failed to create dirent_slab\n");
|
||||
}
|
||||
|
|
|
|||
161
fs/vfs/ramfs.c
Normal file
161
fs/vfs/ramfs.c
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
/* 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);
|
||||
87
fs/vfs/vfs.c
87
fs/vfs/vfs.c
|
|
@ -1,44 +1,87 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include "lib/rbtree.h"
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
|
||||
#include <lib/list.h>
|
||||
#include <lib/string.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
static fs_t root_fs = {0};
|
||||
#include <lib/errno.h>
|
||||
|
||||
void vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
|
||||
static fs_t *root_fs = NULL;
|
||||
|
||||
static list_t fs_type_list = {0};
|
||||
|
||||
int vfs_mount(inode_t *mount_point, fs_t *fs_to_mount)
|
||||
{
|
||||
if (!mount_point) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void vfs_close_inode(inode_t *inode)
|
||||
{
|
||||
list_item_t *entry = inode->dentries.head;
|
||||
while (entry) {
|
||||
dirent_t *dirent = entry->ptr;
|
||||
|
||||
if (dirent->inode) {
|
||||
vfs_close_inode(dirent->inode);
|
||||
if (root_fs) {
|
||||
return -EBUSY;
|
||||
} else {
|
||||
root_fs = fs_to_mount;
|
||||
}
|
||||
|
||||
inode_free(inode);
|
||||
|
||||
entry = entry->next;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void vfs_open_inode(const char *path)
|
||||
static int iter_inode_dirent(inode_t *ino, const char *name, inode_t **out)
|
||||
{
|
||||
fs_t *fs = ino->fs;
|
||||
dirent_t *d = inode_find_dirent(ino, name);
|
||||
if (d) {
|
||||
if (d->inode)
|
||||
*out = d->inode;
|
||||
else
|
||||
return fs->fs_ops.open_inode(fs, d->ino_num, out);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vfs_get_inode(const char *path, inode_t *ret)
|
||||
{
|
||||
inode_t *ino = root_fs->root_inode;
|
||||
|
||||
char name[512] = {0};
|
||||
|
||||
char c;
|
||||
int i = 0;
|
||||
while (i < 256 && ino) {
|
||||
c = *path++;
|
||||
if (c == '/' || c == '\0') {
|
||||
name[i] = 0;
|
||||
i = 0;
|
||||
|
||||
int res = iter_inode_dirent(ino, name, &ino);
|
||||
if (res)
|
||||
return res;
|
||||
|
||||
if (c == '\0')
|
||||
break;
|
||||
} else {
|
||||
name[i] = c;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
void vfs_register(struct fs_type *fs)
|
||||
{
|
||||
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||
memcpy(ins, fs, sizeof(*fs));
|
||||
list_insert(&fs_type_list, ins);
|
||||
}
|
||||
|
||||
void vfs_init()
|
||||
{
|
||||
void inode_init();
|
||||
inode_init();
|
||||
memset(&root_fs, 0, sizeof(root_fs));
|
||||
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
|
||||
memset(root_fs, 0, sizeof(*root_fs));
|
||||
|
||||
list_create(&fs_type_list);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue