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

@@ -1 +1,5 @@
include ../make/ufuncs.mk
$(eval $(call add_lib,libstring))
include ../make/user.mk include ../make/user.mk

View File

@@ -1,7 +1,4 @@
#include <m/system.h> #include <m/system.h>
#include <string.h>
void app_main (void) { void app_main (void) {}
for (;;) {
/* test ('x'); */
}
}

6
include/m/path.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef _M_PATH_H
#define _M_PATH_H
#define MAX_PATH 1024
#endif // _M_PATH_H

View File

@@ -14,5 +14,9 @@
#define SYS_ARGUMENT_PTR 11 #define SYS_ARGUMENT_PTR 11
#define SYS_DEVICE_DO 12 #define SYS_DEVICE_DO 12
#define SYS_EXEC 13 #define SYS_EXEC 13
#define SYS_OPEN 14
#define SYS_CLOSE 15
#define SYS_READ 16
#define SYS_DESCRIBE 17
#endif // _M_SYSCALL_DEFS_H #endif // _M_SYSCALL_DEFS_H

1
init.cmd Normal file
View File

@@ -0,0 +1 @@
Hello world!

View File

@@ -38,9 +38,19 @@ void app_main (void) {
/* process_exec ("ramdisk:/ce"); */ /* process_exec ("ramdisk:/ce"); */
process_spawn (&app_proc, (void*)'b'); /* process_spawn (&app_proc, (void*)'b'); */
process_spawn (&app_proc, (void*)'c'); /* process_spawn (&app_proc, (void*)'c'); */
process_spawn (&app_proc, (void*)'d'); /* process_spawn (&app_proc, (void*)'d'); */
const char* path = "ramdisk:/init.cmd";
char buffer[1024];
memset (buffer, 0, sizeof (buffer));
open (path);
read (path, 0, (uint8_t*)buffer, sizeof (buffer));
close (path);
terminal_print (buffer, strlen (buffer));
for (;;) { for (;;) {
int ch = kb_read_key (); int ch = kb_read_key ();

View File

@@ -87,7 +87,7 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
atomic_fetch_sub (&cpu_counter, 1); 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; struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu); proc_register (spin_proc, &spin_cpu);

View File

@@ -4,7 +4,7 @@
bool path_validate_char (char ch) { bool path_validate_char (char ch) {
return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || 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) { bool path_validate (const char* path) {

View File

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

View File

@@ -7,6 +7,7 @@
#include <libk/string.h> #include <libk/string.h>
#include <m/status.h> #include <m/status.h>
#include <mm/liballoc.h> #include <mm/liballoc.h>
#include <proc/procgroup.h>
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
#include <sys/debug.h> #include <sys/debug.h>
@@ -75,7 +76,9 @@ struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
return mountpoint; 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); struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL) if (vmp == NULL)
@@ -83,6 +86,51 @@ int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffe
spin_lock (&vmp->lock); 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); int ret = vmp->driver_ops.describe (vmp, path, desc);
spin_unlock (&vmp->lock); spin_unlock (&vmp->lock);
@@ -90,7 +138,8 @@ int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffe
return ret; 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); struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL) 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); 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); int ret = vmp->driver_ops.read (vmp, path, buffer, off, size);
spin_unlock (&vmp->lock); spin_unlock (&vmp->lock);
@@ -105,6 +159,26 @@ int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t
return ret; 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) { void vfs_init (void) {
memset (&mount_table, 0, sizeof (mount_table)); memset (&mount_table, 0, sizeof (mount_table));

View File

@@ -5,6 +5,7 @@
#include <libk/list.h> #include <libk/list.h>
#include <libk/std.h> #include <libk/std.h>
#include <m/fs_desc_buffer.h> #include <m/fs_desc_buffer.h>
#include <proc/procgroup.h>
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
#define VFS_RAMDISKFS 0 #define VFS_RAMDISKFS 0
@@ -14,6 +15,8 @@ struct vfs_mountpoint {
struct hash_node_link mount_table_link; struct hash_node_link mount_table_link;
int fs_type; int fs_type;
spin_lock_t lock; spin_lock_t lock;
bool locked;
struct procgroup* ownerpg;
struct { struct {
bool (*mount) (struct vfs_mountpoint* mountpoint); bool (*mount) (struct vfs_mountpoint* mountpoint);
int (*describe) (struct vfs_mountpoint* mountpoint, const char* path, 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); 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_describe (struct procgroup* procgroup, const char* mountpoint, const char* path,
int vfs_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off, size_t size); 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); void vfs_init (void);
#endif // _KERNEL_FS_VFS_H #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; 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; struct fs_desc_buffer desc;
if (vfs_describe (mountpoint, path, &desc) != ST_OK) if (vfs_open (procgroup, mountpoint, path) != ST_OK)
return NULL; return NULL;
if (desc.type != FS_FILE) if (vfs_describe (procgroup, mountpoint, path, &desc) != ST_OK) {
vfs_close (procgroup, mountpoint, path);
return NULL; return NULL;
}
if (desc.type != FS_FILE) {
vfs_close (procgroup, mountpoint, path);
return NULL;
}
uint8_t* temp_buffer = malloc (desc.size); uint8_t* temp_buffer = malloc (desc.size);
if (temp_buffer == NULL) if (temp_buffer == NULL) {
return NULL; vfs_close (procgroup, mountpoint, path);
if (vfs_read (mountpoint, path, temp_buffer, 0, desc.size) != ST_OK) {
free (temp_buffer);
return NULL; 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); bool ok = proc_check_elf (temp_buffer);
DEBUG ("Spawning %s:%s, elf header %s\n", mountpoint, path, ok ? "ok" : "bad"); 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); irq_attach (&proc_irq_sched, NULL, CPU_REQUEST_SCHED);
#endif #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; struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu); 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); init->procgroup->capabilities |= (PROC_CAP_TERMINAL | PROC_CAP_KB);
struct cpu* init_cpu = thiscpu; struct cpu* init_cpu = thiscpu;
proc_register (init, &init_cpu); 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); struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf);
bool proc_register (struct proc* proc, struct cpu** reschedule_cpu); bool proc_register (struct proc* proc, struct cpu** reschedule_cpu);
struct proc* proc_find_pid (int pid); 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); void proc_init (void);
#endif // _KERNEL_PROC_PROC_H #endif // _KERNEL_PROC_PROC_H

View File

@@ -1,3 +1,4 @@
#include <fs/vfs.h>
#include <libk/rbtree.h> #include <libk/rbtree.h>
#include <libk/std.h> #include <libk/std.h>
#include <libk/string.h> #include <libk/string.h>
@@ -195,6 +196,10 @@ void procgroup_detach (struct procgroup* procgroup, struct proc* proc) {
proc_delete_resource (resource, &reschedule_cpu); 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; struct list_node_link *mapping_link, *mapping_link_tmp;
list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) { list_foreach (procgroup->mappings, mapping_link, mapping_link_tmp) {
struct proc_mapping* mapping = struct proc_mapping* mapping =

View File

@@ -248,7 +248,7 @@ DEFINE_SYSCALL (sys_exec) {
if (!path_parse (path, mountpoint, &subpath)) if (!path_parse (path, mountpoint, &subpath))
return SYSRESULT (-ST_BAD_PATH); 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) if (new == NULL)
return SYSRESULT (-ST_EXEC_ERROR); return SYSRESULT (-ST_EXEC_ERROR);
@@ -261,6 +261,125 @@ DEFINE_SYSCALL (sys_exec) {
return SYSRESULT (pid); 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[] = { static syscall_handler_func_t handler_table[] = {
[SYS_QUIT] = &sys_quit, [SYS_QUIT] = &sys_quit,
[SYS_TEST] = &sys_test, [SYS_TEST] = &sys_test,
@@ -275,6 +394,10 @@ static syscall_handler_func_t handler_table[] = {
[SYS_MUTEX_UNLOCK] = &sys_mutex_unlock, [SYS_MUTEX_UNLOCK] = &sys_mutex_unlock,
[SYS_DEVICE_DO] = &sys_device_do, [SYS_DEVICE_DO] = &sys_device_do,
[SYS_EXEC] = &sys_exec, [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) { syscall_handler_func_t syscall_find_handler (int syscall_num) {

View File

@@ -40,3 +40,15 @@ int device_do (int device_id, int cmd, void* a1, void* a2, void* a3, void* a4) {
} }
int exec (const char* path) { return (int)do_syscall (SYS_EXEC, path); } int exec (const char* path) { return (int)do_syscall (SYS_EXEC, path); }
int open (const char* path) { return (int)do_syscall (SYS_OPEN, path); }
int close (const char* path) { return (int)do_syscall (SYS_CLOSE, path); }
int read (const char* path, size_t off, uint8_t* buffer, size_t size) {
return (int)do_syscall (SYS_READ, path, off, buffer, size);
}
int describe (const char* path, struct fs_desc_buffer* desc) {
return (int)do_syscall (SYS_DESCRIBE, path, desc);
}

View File

@@ -1,6 +1,7 @@
#ifndef _LIBMSL_M_SYSTEM_H #ifndef _LIBMSL_M_SYSTEM_H
#define _LIBMSL_M_SYSTEM_H #define _LIBMSL_M_SYSTEM_H
#include <m/fs_desc_buffer.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
@@ -52,4 +53,16 @@ int device_do (int device_id, int cmd, void* a1, void* a2, void* a3, void* a4);
/* Run external ELF program */ /* Run external ELF program */
int exec (const char* path); int exec (const char* path);
/* Open a file */
int open (const char* path);
/* Close a file */
int close (const char* path);
/* Read a file */
int read (const char* path, size_t off, uint8_t* buffer, size_t size);
/* describe a file */
int describe (const char* path, struct fs_desc_buffer* desc);
#endif // _LIBMSL_M_SYSTEM_H #endif // _LIBMSL_M_SYSTEM_H

View File

@@ -1,8 +1,9 @@
exe := $(foreach d,$(apps),$(wildcard $(d)/$(d))) exe := $(foreach d,$(apps),$(wildcard $(d)/$(d)))
extra := init.cmd
all_dist: mop3dist.tar all_dist: mop3dist.tar
mop3dist.tar: mop3dist.tar:
tar -cf $@ --transform='s|.*/||' $(exe) tar -cf $@ --transform='s|.*/||' $(exe) $(extra)
.PHONY: all_dist .PHONY: all_dist