vfs: add dirent parent pointer

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-12 10:47:58 -04:00
commit d2c698ac0a
7 changed files with 25 additions and 13 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -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;