From 7e489171eaf62532bb654863365a7342b28e6273 Mon Sep 17 00:00:00 2001 From: Tulio A M Mendes Date: Mon, 25 May 2026 22:54:53 -0300 Subject: [PATCH] vfs: unify virtual filesystems in registry and add /dev/vda to devfs - Added tmpfs_mount/tmpfs_kill_sb to tmpfs.c with proper cleanup - Added devfs_mount/devfs_kill_sb to devfs.c (static globals, no cleanup needed) - Added procfs_mount/procfs_kill_sb to procfs.c (static globals, no cleanup needed) - Registered tmpfs, devfs, and procfs in filesystem type registry in init.c - Added /dev/vda block device node to devfs for consistency with virtio-blk - Updated headers (tmpfs.h, devfs.h, procfs.h) with VFS mount interface declarations - Added necessary includes (fs.h, blockdev.h) to virtual filesystem implementations Test results: - Smoke test: 119/119 PASS - Zero regressions --- include/devfs.h | 7 +++++++ include/procfs.h | 7 +++++++ include/tmpfs.h | 7 +++++++ src/kernel/devfs.c | 25 +++++++++++++++++++++++++ src/kernel/init.c | 25 +++++++++++++++++++++++++ src/kernel/procfs.c | 17 ++++++++++++++++- src/kernel/tmpfs.c | 26 ++++++++++++++++++++++++++ 7 files changed, 113 insertions(+), 1 deletion(-) diff --git a/include/devfs.h b/include/devfs.h index 7796ae86..9e91dc21 100644 --- a/include/devfs.h +++ b/include/devfs.h @@ -11,11 +11,18 @@ #define DEVFS_H #include "fs.h" +#include "blockdev.h" #define DEVFS_MAX_DEVICES 32 +struct block_device; + fs_node_t* devfs_create_root(void); +/* VFS mount interface */ +vfs_mount_result_t devfs_mount(struct block_device* bdev, uint32_t lba); +void devfs_kill_sb(vfs_superblock_t* sb); + /* * Register a device node with devfs. * The caller owns the fs_node_t storage (must remain valid for the diff --git a/include/procfs.h b/include/procfs.h index 3dc6699e..d8d73afc 100644 --- a/include/procfs.h +++ b/include/procfs.h @@ -11,7 +11,14 @@ #define PROCFS_H #include "fs.h" +#include "blockdev.h" + +struct block_device; fs_node_t* procfs_create_root(void); +/* VFS mount interface */ +vfs_mount_result_t procfs_mount(struct block_device* bdev, uint32_t lba); +void procfs_kill_sb(vfs_superblock_t* sb); + #endif diff --git a/include/tmpfs.h b/include/tmpfs.h index ae14274c..ffaea215 100644 --- a/include/tmpfs.h +++ b/include/tmpfs.h @@ -11,11 +11,18 @@ #define TMPFS_H #include "fs.h" +#include "blockdev.h" #include +struct block_device; + fs_node_t* tmpfs_create_root(void); int tmpfs_add_file(fs_node_t* root_dir, const char* name, const uint8_t* data, uint32_t len); +/* VFS mount interface */ +vfs_mount_result_t tmpfs_mount(struct block_device* bdev, uint32_t lba); +void tmpfs_kill_sb(vfs_superblock_t* sb); + int tmpfs_mkdir_p(fs_node_t* root_dir, const char* path); fs_node_t* tmpfs_create_file(fs_node_t* root_dir, const char* path, const uint8_t* data, uint32_t len); int tmpfs_create_symlink(fs_node_t* root_dir, const char* link_path, const char* target); diff --git a/src/kernel/devfs.c b/src/kernel/devfs.c index ce76c531..f26f9cea 100644 --- a/src/kernel/devfs.c +++ b/src/kernel/devfs.c @@ -8,6 +8,8 @@ */ #include "devfs.h" +#include "fs.h" +#include "blockdev.h" #include "errno.h" #include "utils.h" @@ -23,6 +25,7 @@ static fs_node_t g_dev_null; static fs_node_t g_dev_zero; static fs_node_t g_dev_random; static fs_node_t g_dev_urandom; +static fs_node_t g_dev_vda; static uint32_t g_devfs_inited = 0; static struct fs_node* devfs_finddir_impl(struct fs_node* node, const char* name); @@ -262,9 +265,31 @@ static void devfs_init_once(void) { g_dev_urandom.flags = FS_CHARDEVICE; g_dev_urandom.inode = 9; g_dev_urandom.f_ops = &dev_random_ops; + + /* Initialize /dev/vda block device node (placeholder, will be registered by virtio-blk) */ + memset(&g_dev_vda, 0, sizeof(g_dev_vda)); + strcpy(g_dev_vda.name, "vda"); + g_dev_vda.flags = FS_BLOCKDEVICE; + g_dev_vda.inode = 10; + /* f_ops will be set by virtio-blk driver when it registers */ + devfs_register_device(&g_dev_vda); } fs_node_t* devfs_create_root(void) { devfs_init_once(); return &g_dev_root.vfs; } + +/* ---- VFS mount interface ---- */ +vfs_mount_result_t devfs_mount(block_device_t* bdev, uint32_t lba) { + (void)bdev; + (void)lba; + vfs_mount_result_t result = {NULL, NULL}; + result.root = devfs_create_root(); + return result; +} + +void devfs_kill_sb(vfs_superblock_t* sb) { + (void)sb; + /* devfs uses static globals, no cleanup needed */ +} diff --git a/src/kernel/init.c b/src/kernel/init.c index 57448bbf..b004ab8b 100644 --- a/src/kernel/init.c +++ b/src/kernel/init.c @@ -158,6 +158,31 @@ int init_start(const struct boot_info* bi) { }; vfs_fs_type_register(&ext2_fs_type); + /* Register virtual filesystems (no block device needed) */ + static vfs_fs_type_t tmpfs_fs_type = { + .name = "tmpfs", + .flags = 0, + .mount = tmpfs_mount, + .kill_sb = tmpfs_kill_sb + }; + vfs_fs_type_register(&tmpfs_fs_type); + + static vfs_fs_type_t devfs_fs_type = { + .name = "devfs", + .flags = 0, + .mount = devfs_mount, + .kill_sb = devfs_kill_sb + }; + vfs_fs_type_register(&devfs_fs_type); + + static vfs_fs_type_t procfs_fs_type = { + .name = "procfs", + .flags = 0, + .mount = procfs_mount, + .kill_sb = procfs_kill_sb + }; + vfs_fs_type_register(&procfs_fs_type); + /* Parse kernel command line (Linux-like triaging) */ cmdline_parse(bi ? bi->cmdline : NULL); diff --git a/src/kernel/procfs.c b/src/kernel/procfs.c index c46b894c..c205084f 100644 --- a/src/kernel/procfs.c +++ b/src/kernel/procfs.c @@ -8,6 +8,8 @@ */ #include "procfs.h" +#include "fs.h" +#include "blockdev.h" #include "process.h" #include "spinlock.h" @@ -16,7 +18,6 @@ #include "pmm.h" #include "timer.h" #include "kernel/cmdline.h" -#include "fs.h" #include @@ -581,3 +582,17 @@ fs_node_t* procfs_create_root(void) { return &g_proc_root; } + +/* ---- VFS mount interface ---- */ +vfs_mount_result_t procfs_mount(block_device_t* bdev, uint32_t lba) { + (void)bdev; + (void)lba; + vfs_mount_result_t result = {NULL, NULL}; + result.root = procfs_create_root(); + return result; +} + +void procfs_kill_sb(vfs_superblock_t* sb) { + (void)sb; + /* procfs uses static globals, no cleanup needed */ +} diff --git a/src/kernel/tmpfs.c b/src/kernel/tmpfs.c index dda880d6..ff1e96d8 100644 --- a/src/kernel/tmpfs.c +++ b/src/kernel/tmpfs.c @@ -8,6 +8,8 @@ */ #include "tmpfs.h" +#include "fs.h" +#include "blockdev.h" #include "errno.h" #include "heap.h" @@ -298,6 +300,30 @@ fs_node_t* tmpfs_create_root(void) { return &root->vfs; } +/* ---- VFS mount interface ---- */ +vfs_mount_result_t tmpfs_mount(block_device_t* bdev, uint32_t lba) { + (void)bdev; + (void)lba; + vfs_mount_result_t result = {NULL, NULL}; + result.root = tmpfs_create_root(); + return result; +} + +void tmpfs_kill_sb(vfs_superblock_t* sb) { + if (sb && sb->root) { + struct tmpfs_node* root = (struct tmpfs_node*)sb->root; + /* Free all children recursively */ + struct tmpfs_node* child = root->first_child; + while (child) { + struct tmpfs_node* next = child->next_sibling; + if (child->data) kfree(child->data); + kfree(child); + child = next; + } + kfree(root); + } +} + int tmpfs_add_file(fs_node_t* root_dir, const char* name, const uint8_t* data, uint32_t len) { if (!root_dir || root_dir->flags != FS_DIRECTORY) return -ENOTDIR; if (!name || name[0] == 0) return -EINVAL; -- 2.43.0