Implement VFS syscalls
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m29s

This commit is contained in:
2026-02-15 21:34:07 +01:00
parent 0f5bd48328
commit 7726fd2f00
19 changed files with 307 additions and 31 deletions

View File

@@ -87,7 +87,7 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
atomic_fetch_sub (&cpu_counter, 1);
struct proc* spin_proc = proc_from_file ("ramdisk", "/spin");
struct proc* spin_proc = proc_from_file (NULL, "ramdisk", "/spin");
struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu);

View File

@@ -4,7 +4,7 @@
bool path_validate_char (char ch) {
return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch == '_') || (ch == '-') || (ch == '/'));
(ch == '_') || (ch == '-') || (ch == '/') || (ch == '.'));
}
bool path_validate (const char* path) {

View File

@@ -1,6 +1,7 @@
#include <fs/path.h>
#include <fs/ramdiskfs.h>
#include <fs/vfs.h>
#include <libk/minmax.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
@@ -129,10 +130,7 @@ int ramdiskfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t
if (file == NULL)
return -ST_NOT_FOUND;
if (off + size > file->size)
return -ST_OOB_ERROR;
memcpy (buffer, (void*)((uintptr_t)file->content + off), size);
memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size));
return ST_OK;
}

View File

@@ -7,6 +7,7 @@
#include <libk/string.h>
#include <m/status.h>
#include <mm/liballoc.h>
#include <proc/procgroup.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
@@ -75,7 +76,9 @@ struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
return mountpoint;
}
int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffer* desc) {
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path) {
(void)path;
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
@@ -83,6 +86,51 @@ int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffe
spin_lock (&vmp->lock);
vmp->ownerpg = procgroup;
vmp->locked = true;
spin_unlock (&vmp->lock);
return ST_OK;
}
int vfs_close (struct procgroup* procgroup, const char* mountpoint, const char* path) {
(void)path;
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
return -ST_NOT_FOUND;
spin_lock (&vmp->lock);
if (procgroup != NULL && vmp->ownerpg != procgroup) {
spin_unlock (&vmp->lock);
return -ST_PERMISSION_ERROR;
}
vmp->locked = false;
vmp->ownerpg = NULL;
spin_unlock (&vmp->lock);
return ST_OK;
}
int vfs_describe (struct procgroup* procgroup, const char* mountpoint, const char* path,
struct fs_desc_buffer* desc) {
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
return -ST_NOT_FOUND;
spin_lock (&vmp->lock);
if ((procgroup != NULL && vmp->ownerpg != procgroup) && vmp->locked) {
spin_unlock (&vmp->lock);
return -ST_PERMISSION_ERROR;
}
int ret = vmp->driver_ops.describe (vmp, path, desc);
spin_unlock (&vmp->lock);
@@ -90,7 +138,8 @@ int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffe
return ret;
}
int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off, size_t size) {
int vfs_read (struct procgroup* procgroup, const char* mountpoint, const char* path,
uint8_t* buffer, size_t off, size_t size) {
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
@@ -98,6 +147,11 @@ int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t
spin_lock (&vmp->lock);
if ((procgroup != NULL && vmp->ownerpg != procgroup) && vmp->locked) {
spin_unlock (&vmp->lock);
return -ST_PERMISSION_ERROR;
}
int ret = vmp->driver_ops.read (vmp, path, buffer, off, size);
spin_unlock (&vmp->lock);
@@ -105,6 +159,26 @@ int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t
return ret;
}
void vfs_procgroup_cleanup (struct procgroup* procgroup) {
spin_lock (&mount_table.lock);
for (size_t i = 0; i < lengthof (mount_table.mountpoint_buckets); i++) {
struct hash_node_link* link = mount_table.mountpoint_buckets[i];
struct vfs_mountpoint* vmp = hash_entry (link, struct vfs_mountpoint, mount_table_link);
spin_lock (&vmp->lock);
if (vmp->ownerpg == procgroup) {
vmp->locked = false;
vmp->ownerpg = NULL;
}
spin_unlock (&vmp->lock);
}
spin_unlock (&mount_table.lock);
}
void vfs_init (void) {
memset (&mount_table, 0, sizeof (mount_table));

View File

@@ -5,6 +5,7 @@
#include <libk/list.h>
#include <libk/std.h>
#include <m/fs_desc_buffer.h>
#include <proc/procgroup.h>
#include <sync/spin_lock.h>
#define VFS_RAMDISKFS 0
@@ -14,6 +15,8 @@ struct vfs_mountpoint {
struct hash_node_link mount_table_link;
int fs_type;
spin_lock_t lock;
bool locked;
struct procgroup* ownerpg;
struct {
bool (*mount) (struct vfs_mountpoint* mountpoint);
int (*describe) (struct vfs_mountpoint* mountpoint, const char* path,
@@ -29,8 +32,13 @@ struct vfs_mount_table {
};
struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type);
int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffer* desc);
int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off, size_t size);
int vfs_describe (struct procgroup* procgroup, const char* mountpoint, const char* path,
struct fs_desc_buffer* desc);
int vfs_read (struct procgroup* procgroup, const char* mountpoint, const char* path,
uint8_t* buffer, size_t off, size_t size);
int vfs_close (struct procgroup* procgroup, const char* mountpoint, const char* path);
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path);
void vfs_procgroup_cleanup (struct procgroup* procgroup);
void vfs_init (void);
#endif // _KERNEL_FS_VFS_H

7
kernel/libk/minmax.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef _KERNEL_LIBK_MINMAX_H
#define _KERNEL_LIBK_MINMAX_H
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#endif // _KERNEL_LIBK_MINMAX_H

View File

@@ -103,25 +103,38 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
return aux;
}
struct proc* proc_from_file (const char* mountpoint, const char* path) {
struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint,
const char* path) {
struct fs_desc_buffer desc;
if (vfs_describe (mountpoint, path, &desc) != ST_OK)
if (vfs_open (procgroup, mountpoint, path) != ST_OK)
return NULL;
if (desc.type != FS_FILE)
if (vfs_describe (procgroup, mountpoint, path, &desc) != ST_OK) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
if (desc.type != FS_FILE) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL)
return NULL;
if (vfs_read (mountpoint, path, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
if (temp_buffer == NULL) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
if (vfs_read (procgroup, mountpoint, path, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
vfs_close (procgroup, mountpoint, path);
return NULL;
}
vfs_close (procgroup, mountpoint, path);
bool ok = proc_check_elf (temp_buffer);
DEBUG ("Spawning %s:%s, elf header %s\n", mountpoint, path, ok ? "ok" : "bad");
@@ -300,11 +313,11 @@ void proc_init (void) {
irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
#endif
struct proc* spin_proc = proc_from_file ("ramdisk", "/spin");
struct proc* spin_proc = proc_from_file (NULL, "ramdisk", "/spin");
struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu);
struct proc* init = proc_from_file ("ramdisk", "/init");
struct proc* init = proc_from_file (NULL, "ramdisk", "/init");
init->procgroup->capabilities |= (PROC_CAP_TERMINAL | PROC_CAP_KB);
struct cpu* init_cpu = thiscpu;
proc_register (init, &init_cpu);

View File

@@ -51,7 +51,7 @@ bool proc_kill (struct proc* proc, struct cpu** reschedule_cpu);
struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
bool proc_register (struct proc* proc, struct cpu** reschedule_cpu);
struct proc* proc_find_pid (int pid);
struct proc* proc_from_file (const char* mountpoint, const char* path);
struct proc* proc_from_file (struct procgroup* procgroup, const char* mountpoint, const char* path);
void proc_init (void);
#endif // _KERNEL_PROC_PROC_H

View File

@@ -1,3 +1,4 @@
#include <fs/vfs.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <libk/string.h>
@@ -195,6 +196,10 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
proc_delete_resource (resource, &reschedule_cpu);
}
/* unlock VFS owned mountpoints */
vfs_procgroup_cleanup (procgroup);
/* delete mappings */
struct list_node_link *mapping_link, *mapping_link_tmp;
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
struct proc_mapping* mapping =

View File

@@ -248,7 +248,7 @@ DEFINE_SYSCALL (sys_exec) {
if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH);
struct proc* new = proc_from_file (mountpoint, subpath);
struct proc* new = proc_from_file (proc->procgroup, mountpoint, subpath);
if (new == NULL)
return SYSRESULT (-ST_EXEC_ERROR);
@@ -261,6 +261,125 @@ DEFINE_SYSCALL (sys_exec) {
return SYSRESULT (pid);
}
/* int open (char* path) */
DEFINE_SYSCALL (sys_open) {
uintptr_t uvaddr_path = a1;
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
uintptr_t out_paddr;
spin_lock (&proc->procgroup->lock);
out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path);
spin_unlock (&proc->procgroup->lock);
if (out_paddr == 0)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
char mountpoint[fieldsizeof (struct vfs_mountpoint, key)];
const char* subpath = NULL;
if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH);
return SYSRESULT (vfs_open (proc->procgroup, mountpoint, subpath));
}
/* int close (char* path) */
DEFINE_SYSCALL (sys_close) {
uintptr_t uvaddr_path = a1;
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
uintptr_t out_paddr;
spin_lock (&proc->procgroup->lock);
out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path);
spin_unlock (&proc->procgroup->lock);
if (out_paddr == 0)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
char mountpoint[fieldsizeof (struct vfs_mountpoint, key)];
const char* subpath = NULL;
if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH);
return SYSRESULT (vfs_close (proc->procgroup, mountpoint, subpath));
}
/* int read (char* path, size_t off, uint8_t* buffer, size_t size) */
DEFINE_SYSCALL (sys_read) {
uintptr_t uvaddr_path = a1;
size_t off = (size_t)a2;
uintptr_t uvaddr_buffer = a3;
size_t size = (size_t)a4;
uint8_t* buffer = sys_get_user_buffer (proc, uvaddr_buffer, size);
if (buffer == NULL)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
uintptr_t out_paddr;
spin_lock (&proc->procgroup->lock);
out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path);
spin_unlock (&proc->procgroup->lock);
if (out_paddr == 0)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
char mountpoint[fieldsizeof (struct vfs_mountpoint, key)];
const char* subpath = NULL;
if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH);
return SYSRESULT (vfs_read (proc->procgroup, mountpoint, subpath, buffer, off, size));
}
/* int describe (char* path, struct fs_desc_buffer* desc) */
DEFINE_SYSCALL (sys_describe) {
uintptr_t uvaddr_path = a1;
uintptr_t uvaddr_desc = a2;
struct fs_desc_buffer* desc =
sys_get_user_buffer (proc, uvaddr_desc, sizeof (struct fs_desc_buffer));
if (desc == NULL)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
struct limine_hhdm_response* hhdm = limine_hhdm_request.response;
uintptr_t out_paddr;
spin_lock (&proc->procgroup->lock);
out_paddr = mm_v2p (&proc->procgroup->pd, uvaddr_path);
spin_unlock (&proc->procgroup->lock);
if (out_paddr == 0)
return SYSRESULT (-ST_BAD_ADDRESS_SPACE);
const char* path = (const char*)((uintptr_t)hhdm->offset + out_paddr);
char mountpoint[fieldsizeof (struct vfs_mountpoint, key)];
const char* subpath = NULL;
if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH);
return SYSRESULT (vfs_describe (proc->procgroup, mountpoint, subpath, desc));
}
static syscall_handler_func_t handler_table[] = {
[SYS_QUIT] = &sys_quit,
[SYS_TEST] = &sys_test,
@@ -275,6 +394,10 @@ static syscall_handler_func_t handler_table[] = {
[SYS_MUTEX_UNLOCK] = &sys_mutex_unlock,
[SYS_DEVICE_DO] = &sys_device_do,
[SYS_EXEC] = &sys_exec,
[SYS_OPEN] = &sys_open,
[SYS_CLOSE] = &sys_close,
[SYS_READ] = &sys_read,
[SYS_DESCRIBE] = &sys_describe,
};
syscall_handler_func_t syscall_find_handler (int syscall_num) {