From 733b9a7c478349e1bf54429f6c6aa996a92407be Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 12:18:04 -0400 Subject: [PATCH 1/3] vfs: add `vfs_openat` Signed-off-by: Michael Shaw --- fs/vfs/vfs.c | 26 +++++++++++++++++--------- include/fs/vfs.h | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 9ad19c7..f48960d 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -85,19 +85,16 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) return -ENOENT; } -int vfs_open(const char *abs_path, dirent_t **out) +int vfs_openat(dirent_t *in, const char *name, dirent_t **out) { - if (!abs_path || !out) + if (!in || !name || !out) return -EINVAL; - if (!root_fs->root_dirent) - return -ENOENT; - - char *path_optr = path_trim(abs_path); + char *path_optr = path_trim(name); if (!path_optr) return -ENOMEM; - dirent_t *current_dir = root_fs->root_dirent; + dirent_t *current_dir = in; dirent_inc_ref(current_dir); if (*path_optr == '\0') { @@ -138,6 +135,17 @@ out: return ret; } +int vfs_open(const char *abs_path, dirent_t **out) +{ + if (!abs_path || !out) + return -EINVAL; + + if (!root_fs->root_dirent) + return -ENOENT; + + return vfs_openat(root_fs->root_dirent, abs_path, out); +} + void vfs_close(dirent_t *dent) { dirent_dec_ref(dent); @@ -182,7 +190,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t write_lock(&ino->rwlock); - if ((ino->mode &S_IFMT) != S_IFDIR) { + if ((ino->mode & S_IFMT) != S_IFDIR) { write_unlock(&ino->rwlock); return -ENOTDIR; } @@ -231,7 +239,7 @@ int vfs_unlink(dirent_t *parent, const char *name) write_lock(&ino->rwlock); - if ((ino->mode & S_IFMT)!= S_IFDIR) { + if ((ino->mode & S_IFMT) != S_IFDIR) { write_unlock(&ino->rwlock); return -ENOTDIR; } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 1f318dc..9de0e9f 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -83,6 +83,7 @@ void vfs_init(void); void vfs_register(struct fs_driver *fs); int vfs_open(const char *abs_path, dirent_t **out); +int vfs_openat(dirent_t *in, const char *name, dirent_t **out); void vfs_close(dirent_t *dent); int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out); int vfs_unlink(dirent_t *parent, const char *name); From cfda99c7c745653a9bef764da7e86b3838dd718e Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 12:48:55 -0400 Subject: [PATCH 2/3] main: add initramfs read test Signed-off-by: Michael Shaw --- init/main.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/init/main.c b/init/main.c index eeff5ca..336ea01 100644 --- a/init/main.c +++ b/init/main.c @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -42,6 +43,42 @@ static void do_initcalls() (*fn)(); } +static void indent(int indent) +{ + for (int i = 0; i < indent; i++) { + kprintf(" "); + } +} + +void printf_dir_recursive(dirent_t *dir, int ind) +{ + if (!dir || !dir->inode) + return; + + dirent_t *cursor; + struct dir_context ctx = {0}; + int res; + + while (!(res = vfs_readdir(dir, &ctx, &cursor))) { + if (!cursor || !cursor->inode) + continue; + + indent(ind); + kprintf("[inode] %s\n", cursor->name); + + if (cursor->inode->mode & S_IFDIR) { + printf_dir_recursive(cursor, ind + 2); + } else { + indent(ind + 1); + char *buf = kalloc(cursor->inode->file_size + 1, ALLOC_KERN); + buf[cursor->inode->file_size] = 0; + vfs_read(cursor->inode, buf, cursor->inode->file_size, 0); + kprintf("%s", buf); + kfree(buf); + } + } +} + static void kmain() { /* data types */ @@ -77,6 +114,11 @@ static void kmain() if (res < 0) panic("failed to load initramfs: %s\n", strerror(-res)); + krdy("initramfs loaded\n"); + + /* read entire initramfs filesystem */ + printf_dir_recursive(initramfs->root_dirent, 0); + /* do nothing, for now :) */ yield(); } From 898570d0ca0f972889ef6fbb0628ae7787017af5 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 10 Jul 2026 12:58:11 -0400 Subject: [PATCH 3/3] vfs: add `vfs_readdir` Signed-off-by: Michael Shaw --- fs/vfs/inode.c | 2 +- fs/vfs/vfs.c | 34 ++++++++++++++++++++++++++++++++++ include/fs/inode.h | 1 + include/fs/vfs.h | 6 ++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index c7d4e83..878e893 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -11,7 +11,7 @@ static mem_cache_t *inode_slab = NULL; static mem_cache_t *dirent_slab = NULL; -static int inode_populate_dirents(inode_t *inode) +int inode_populate_dirents(inode_t *inode) { write_lock(&inode->rwlock); diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index f48960d..a673cc5 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -151,6 +151,40 @@ void vfs_close(dirent_t *dent) dirent_dec_ref(dent); } +int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out) +{ + if (!dir || !ctx || !out) + return -EINVAL; + + inode_t *ino = dir->inode; + if ((ino->mode & S_IFMT) != S_IFDIR) + return -ENOTDIR; + + int res = inode_populate_dirents(ino); + if (res < 0) + return res; + + read_lock(&ino->rwlock); + + if (!ctx->init) { + ctx->cookie = ino->dirents.head; + ctx->init = true; + } + + list_item_t *item = ctx->cookie; + if (!item) { + read_unlock(&ino->rwlock); + return -ENOENT; + } + + dirent_inc_ref(item->ptr); + *out = item->ptr; + ctx->cookie = item->next; + + read_unlock(&ino->rwlock); + return 0; +} + ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset) { if (!ino) diff --git a/include/fs/inode.h b/include/fs/inode.h index b9aac8a..7117437 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -63,6 +63,7 @@ void dirent_dec_ref(dirent_t *dent); int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out); int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out); +int inode_populate_dirents(inode_t *inode); inode_t *inode_create(void); void inode_free(inode_t *inode); void inode_inc_ref(inode_t *inode); diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 9de0e9f..d6add85 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -79,12 +79,18 @@ struct _fs { struct fs_ops fs_ops; }; +struct dir_context { + void *cookie; + bool init; +}; + void vfs_init(void); void vfs_register(struct fs_driver *fs); int vfs_open(const char *abs_path, dirent_t **out); int vfs_openat(dirent_t *in, const char *name, dirent_t **out); void vfs_close(dirent_t *dent); +int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out); int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out); int vfs_unlink(dirent_t *parent, const char *name); int vfs_mount(const char *mount_point, fs_t *fs_to_mount);