From d2c698ac0a4ee40dc8f215de9606797b36463dbe Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 12 Jul 2026 10:47:58 -0400 Subject: [PATCH] vfs: add dirent `parent` pointer Signed-off-by: Michael Shaw --- fs/vfs/initcpio.c | 1 + fs/vfs/inode.c | 19 +++++++++++++------ fs/vfs/mount.c | 2 +- fs/vfs/ramfs.c | 1 + fs/vfs/vfs.c | 7 ++++--- include/fs/inode.h | 7 ++++--- include/fs/vfs.h | 1 + 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/fs/vfs/initcpio.c b/fs/vfs/initcpio.c index f6ff845..538f876 100644 --- a/fs/vfs/initcpio.c +++ b/fs/vfs/initcpio.c @@ -103,6 +103,7 @@ static ssize_t read_next(fs_t *fs, const char *file, rbtree_t *list, dirent_t ** write_unlock(&parent->inode->rwlock); if (res == 0) { new_dent->fs = parent->fs; + new_dent->parent = parent; *out = new_dent; } else { inode_dec_ref(existing_ino); diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index 878e893..b9458f2 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -11,8 +11,10 @@ static mem_cache_t *inode_slab = NULL; static mem_cache_t *dirent_slab = NULL; -int inode_populate_dirents(inode_t *inode) +int dirent_populate_children(dirent_t *dir) { + inode_t *inode = dir->inode; + write_lock(&inode->rwlock); if (inode->dirents.head != NULL) { @@ -34,6 +36,8 @@ int inode_populate_dirents(inode_t *inode) if (!insert) goto cleanup; + insert->parent = dir; + if (!list_insert(&inode->dirents, insert)) { dirent_free(insert); goto cleanup; @@ -62,9 +66,9 @@ cleanup: return -ENOMEM; } -int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out) +int dirent_lookup_locked(dirent_t *dir, const char *name, dirent_t **out) { - list_item_t *item = inode->dirents.head; + list_item_t *item = dir->inode->dirents.head; while (item) { dirent_t *dent = item->ptr; if (!strcmp(dent->name, name)) { @@ -77,8 +81,10 @@ int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out) return -ENOENT; } -int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) +int dirent_lookup(dirent_t *dir, const char *name, dirent_t **out) { + inode_t *inode = dir->inode; + if ((inode->mode & S_IFMT) != S_IFDIR) return -ENOTDIR; @@ -86,13 +92,13 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) if (inode->dirents.head == NULL) { read_unlock(&inode->rwlock); - int res = inode_populate_dirents(inode); + int res = dirent_populate_children(dir); if (res < 0) return res; read_lock(&inode->rwlock); } - int ret = inode_find_dirent_locked(inode, name, out); + int ret = dirent_lookup_locked(dir, name, out); read_unlock(&inode->rwlock); return ret; } @@ -203,6 +209,7 @@ dirent_t *dirent_dup(dirent_t *src) ret->flags = src->flags; ret->type = src->type; ret->fs = src->fs; + ret->parent = src->parent; if (src->inode) { inode_inc_ref(src->inode); diff --git a/fs/vfs/mount.c b/fs/vfs/mount.c index d39f6d2..dc99c22 100644 --- a/fs/vfs/mount.c +++ b/fs/vfs/mount.c @@ -96,7 +96,7 @@ static int mount_traverse(const char *path, struct mount **out_mount, dirent_t * bool is_last = (*p == '\0'); dirent_t *found = NULL; - int res = inode_find_dirent(cur_dent->inode, name, &found); + int res = dirent_lookup(cur_dent, name, &found); if (res < 0) { dirent_dec_ref(cur_dent); return res; diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index 5811f07..b96ebf3 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -46,6 +46,7 @@ int ramfs_create(u64 flags, fs_t **out) root->type = S_IFDIR; root->fs = fs; root->inode = root_inode; + root->parent = NULL; *out = fs; return 0; diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 8a2faad..d01a853 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -53,7 +53,7 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) if (c == '/' || c == '\0') { dirent_t *dirent; - res = inode_find_dirent(dent->inode, build, &dirent); + res = dirent_lookup(dent, build, &dirent); kfree(build); if (res < 0) return -ENOENT; @@ -174,7 +174,7 @@ int vfs_readdir(dirent_t *dir, struct dir_context *ctx, dirent_t **out) if ((ino->mode & S_IFMT) != S_IFDIR) return -ENOTDIR; - int res = inode_populate_dirents(ino); + int res = dirent_populate_children(dir); if (res < 0) return res; @@ -244,7 +244,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t } dirent_t *test = NULL; - int res = inode_find_dirent_locked(ino, name, &test); + int res = dirent_lookup_locked(parent, name, &test); if (res == 0) { dirent_dec_ref(test); write_unlock(&ino->rwlock); @@ -273,6 +273,7 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t } new_dirent->fs = fs; + new_dirent->parent = parent; *out = new_dirent; write_unlock(&ino->rwlock); return 0; diff --git a/include/fs/inode.h b/include/fs/inode.h index 7117437..abf220d 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -40,6 +40,7 @@ struct _inode { struct _dirent { fs_t *fs; inode_t *inode; + dirent_t *parent; u64 ino_num; @@ -61,9 +62,9 @@ void dirent_free(dirent_t *dirent); void dirent_inc_ref(dirent_t *dent); 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); +int dirent_lookup(dirent_t *dir, const char *name, dirent_t **out); +int dirent_lookup_locked(dirent_t *dir, const char *name, dirent_t **out); +int dirent_populate_children(dirent_t *dir); 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 8003a82..60e9b24 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -70,6 +70,7 @@ struct _fs { u64 flags; size_t block_size; size_t n_blocks; + void *priv; dirent_t *root_dirent;