vbos/fs/vfs/vfs.c
Michael Shaw f9d760993d vfs: switch to dirent tracking over inode tracking
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-02 22:14:54 +02:00

200 lines
3.6 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/slab.h>
#include <fs/vfs.h>
#include <lib/list.h>
#include <lib/string.h>
#include <lib/errno.h>
static fs_t *root_fs = NULL;
static list_t fs_type_list = {0};
static char *path_trim(const char *name)
{
if (!*name)
return NULL;
size_t len = strlen(name);
char *ret = kzalloc(len + 1, ALLOC_KERN);
const char *zeroptr = name + len;
if (!ret)
return NULL;
/* trim trailing slashes */
for (size_t i = len - 1; i >= 1; i--) {
if (name[i] == '/')
zeroptr = name + i;
else
break;
}
/* trim double slashes */
char c;
char last = 0;
size_t i = 0;
const char *cursor = name;
while (cursor < zeroptr && (c = *cursor++)) {
if (last == c && c == '/')
continue;
last = c;
ret[i++] = last;
}
return ret;
}
static int next_level(const char *path, inode_t *dir, dirent_t **out_dirent)
{
if (!dir || !out_dirent)
return -EINVAL;
int res;
char build[256] = {0};
for (int i = 0; i < sizeof(build) - 1; i++) {
char c = path[i];
if (c == '/' || c == '\0') {
dirent_t *dirent;
res = inode_find_dirent(dir, build, &dirent);
if (res < 0)
return -ENOENT;
spinlock_acquire(&dirent->lock);
if (!dirent->inode) {
fs_t *fs = dir->fs;
res = fs->fs_ops.open_inode(fs, dirent->ino_num, &dirent->inode);
if (res < 0) {
spinlock_release(&dirent->lock);
return res;
}
}
dirent_inc_ref(dirent);
spinlock_release(&dirent->lock);
*out_dirent = dirent;
return i;
} else {
build[i] = c;
}
}
return -ENOENT;
}
/* opens a dirent by absolute path only
*
* IMPORTANT: ownership of the out dirent is passed to the caller, and managing
* its refcount is up to the caller (e.g. dirent_dec_ref)
*/
int vfs_open(const char *abs_path, dirent_t **out)
{
if (!abs_path || !out)
return -EINVAL;
char *path_optr = path_trim(abs_path);
if (!path_optr)
return -ENOENT;
char *cur_path = path_optr;
dirent_t *current_dir = root_fs->root_dirent;
int ret = -ENOENT;
dirent_inc_ref(current_dir);
/* Handle root path "/" */
if (strlen(abs_path) == 1 && *abs_path == '/') {
*out = current_dir;
kfree(path_optr);
return 0;
}
while (1) {
dirent_t *next_dir;
int res = next_level(cur_path, current_dir->inode, &next_dir);
if (res < 0) {
dirent_dec_ref(current_dir);
ret = res;
goto out;
}
dirent_inc_ref(next_dir);
dirent_dec_ref(current_dir);
current_dir = next_dir;
cur_path += res;
while (*cur_path == '/')
cur_path++;
if (*cur_path == '\0') {
*out = current_dir;
ret = 0;
goto out;
}
}
out:
kfree(path_optr);
return ret;
}
void vfs_close(dirent_t *dent)
{
dirent_dec_ref(dent);
}
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
{
ssize_t ret;
write_lock(&ino->rwlock);
ret = ino->i_ops.write(ino, buf, size, offset);
write_unlock(&ino->rwlock);
return ret;
}
ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
{
ssize_t ret;
read_lock(&ino->rwlock);
ret = ino->i_ops.read(ino, buf, size, offset);
read_unlock(&ino->rwlock);
return ret;
}
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
{
if (!mount_point || !strcmp(mount_point, "/")) {
root_fs = fs_to_mount;
} else {
}
return 0;
}
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();
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
memset(root_fs, 0, sizeof(*root_fs));
list_create(&fs_type_list);
}