vbos/fs/vfs/vfs.c
Michael Shaw a8540494a9 vfs: add vfs_open
vfs_open parses a path and returns the correct inode. The out inode is a
double pointer argument passed to vfs_open, and the return value is an
error code, or 0

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-01 23:06:39 +02:00

192 lines
3.2 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)
{
size_t len = strlen(name);
char *ret = kzalloc(len, ALLOC_KERN);
const char *zeroptr = 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;
while (name < zeroptr && (c = *name++)) {
if (last == c && c == '/')
continue;
last = c;
ret[i++] = last;
}
return ret;
}
static int next_level(const char *path, inode_t **cur)
{
if (!cur || !*cur)
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(*cur, build, &dirent);
if (res < 0)
return -ENOENT;
spinlock_acquire(&dirent->lock);
if (dirent->inode) {
*cur = dirent->inode;
} else {
fs_t *fs = (*cur)->fs;
res = fs->fs_ops.open_inode(fs, dirent->ino_num, cur);
if (res < 0) {
spinlock_release(&dirent->lock);
return res;
}
}
spinlock_release(&dirent->lock);
inode_inc_ref(*cur);
return i;
} else {
build[i] = c;
}
}
return -ENOENT;
}
int vfs_open(const char *abs_path, inode_t **out)
{
if (!abs_path || !out)
return -EINVAL;
inode_t *c_ino = root_fs->root_inode;
char *path_optr = path_trim(abs_path);
char *cur_path = path_optr;
list_item_t *item;
list_t visited;
list_create(&visited);
/* root case */
if (*cur_path == '\0') {
*out = c_ino;
return 0;
}
while (1) {
while (*cur_path == '/')
cur_path++;
int res = next_level(cur_path, &c_ino);
cur_path += res;
if (!c_ino)
goto out;
list_insert(&visited, c_ino);
if (res < 0)
goto out;
/* found */
if (res == 0) {
*out = c_ino;
return 0;
}
cur_path += res;
}
out:
item = visited.head;
while (item) {
inode_t *inode = item->ptr;
inode_dec_ref(inode);
item = item->next;
}
kfree(path_optr);
return -ENOENT;
}
void vfs_close(inode_t *inode)
{
inode_dec_ref(inode);
}
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);
}