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

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