Volume-centric VFS implementation
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m41s

This commit is contained in:
2026-02-25 08:53:54 +01:00
parent 62a6543dab
commit 704db2dfa4
26 changed files with 441 additions and 406 deletions

View File

@@ -1,64 +1,6 @@
#include <fs/vfs.h>
#include <libk/fieldsizeof.h>
#include <libk/std.h>
bool path_validate_char (char ch) {
return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
(ch == '_') || (ch == '-') || (ch == '/') || (ch == '.'));
}
bool path_validate (const char* path) {
if (path == NULL || *path == '\0')
return false;
const char* ptr = path;
if (*ptr != '/')
return false;
while (*ptr != '\0') {
if (*ptr == '/' && *(ptr + 1) == '/')
return false;
if (!path_validate_char (*ptr))
return false;
ptr++;
}
if (ptr > path + 1 && *(ptr - 1) == '/')
return false;
return true;
}
bool path_parse (const char* source, char* mountpoint, const char** path) {
if (source == NULL || mountpoint == NULL || path == NULL)
return false;
size_t i = 0;
while (source[i] != ':' && source[i] != '\0') {
if (i >= (fieldsizeof (struct vfs_mountpoint, key) - 1))
return false;
mountpoint[i] = source[i];
i++;
}
if (source[i] != ':' || i == 0)
return false;
mountpoint[i] = '\0';
const char* internal_path = &source[i + 1];
if (!path_validate (internal_path))
return false;
*path = internal_path;
return true;
}
#include <path_defs.h>
const char* path_basename (const char* path) {
if (path == NULL)

View File

@@ -3,11 +3,8 @@
#include <libk/std.h>
/* Path scheme: MOUNTPOINT:/path/to/file.txt */
/* Path scheme: VOLUME:/path/to/file.txt */
bool path_validate_char (char ch);
bool path_validate (const char* path);
bool path_parse (const char* source, char* mountpoint, const char** path);
const char* path_basename (const char* path);
#endif // _KERNEL_FS_PATH_H

View File

@@ -8,7 +8,7 @@
#include <libk/string.h>
#include <limine/requests.h>
#include <mm/liballoc.h>
#include <path.h>
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
@@ -16,7 +16,7 @@
static struct tar_file* tar_get_file (struct tarfs* tarfs, const char* filename) {
for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
if ((tarfs->tarfs_files[i].header != NULL) &&
(strncmp (tarfs->tarfs_files[i].header->filename, filename, MAX_PATH) == 0))
(strncmp (tarfs->tarfs_files[i].header->filename, filename, PATH_MAX) == 0))
return &tarfs->tarfs_files[i];
}
return NULL;
@@ -54,8 +54,7 @@ static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr) {
return i;
}
int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
struct reschedule_ctx* rctx) {
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
struct tarfs* tarfs = malloc (sizeof (*tarfs));
if (tarfs == NULL)
@@ -63,9 +62,9 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
memset (tarfs, 0, sizeof (*tarfs));
mountpoint->udata = tarfs;
volume->udata = tarfs;
struct device* back_device = mountpoint->back_device;
struct device* back_device = volume->back_device;
size_t total_size, sector_size;
int ret;
@@ -74,7 +73,7 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
ret = back_device->ops[XDRV_GET_SIZE](back_device, proc, rctx, &total_size, NULL, NULL, NULL);
if (ret < 0) {
spin_unlock (&back_device->lock);
free (mountpoint->udata);
free (volume->udata);
return ret;
}
@@ -82,7 +81,7 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
NULL);
if (ret < 0) {
spin_unlock (&back_device->lock);
free (mountpoint->udata);
free (volume->udata);
return ret;
}
@@ -90,7 +89,7 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
if (buffer == NULL) {
spin_unlock (&back_device->lock);
free (mountpoint->udata);
free (volume->udata);
return ret;
}
@@ -114,15 +113,15 @@ int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc,
return ret;
}
int tarfs_describe (struct vfs_mountpoint* mountpoint, const char* path, struct desc* desc) {
(void)mountpoint;
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
(void)volume;
const char* filename = path_basename (path);
if (filename == NULL)
return -ST_BAD_PATH;
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
struct tar_file* file = tar_get_file ((struct tarfs*)volume->udata, filename);
if (file == NULL)
return -ST_NOT_FOUND;
@@ -133,16 +132,16 @@ int tarfs_describe (struct vfs_mountpoint* mountpoint, const char* path, struct
return ST_OK;
}
int tarfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
int tarfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
(void)mountpoint;
(void)volume;
const char* filename = path_basename (path);
if (filename == NULL)
return -ST_BAD_PATH;
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
struct tar_file* file = tar_get_file ((struct tarfs*)volume->udata, filename);
if (file == NULL)
return -ST_NOT_FOUND;

View File

@@ -31,13 +31,13 @@ struct tarfs {
uint8_t* buffer;
};
struct vfs_mountpoint;
struct vfs_volume;
int tarfs_mount (struct vfs_mountpoint* mountpoint, struct proc* proc, struct reschedule_ctx* rctx);
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int tarfs_describe (struct vfs_mountpoint* mountpoint, const char* path, struct desc* desc);
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc);
int tarfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
int tarfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
#endif // _KERNEL_FS_TARFS_H

View File

@@ -10,216 +10,182 @@
#include <mm/liballoc.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/suspension_q.h>
#include <status.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
static struct vfs_mount_table mount_table;
static struct vfs_volume_table volume_table;
static struct vfs_mountpoint* vfs_find_mountpoint (const char* mountpoint) {
static struct vfs_volume* vfs_find_volume (const char* volume) {
struct hash_node_link* found_link = NULL;
size_t mountpoint_len = strlen (mountpoint);
uint32_t hash = hash_fnv32 (mountpoint, strlen (mountpoint));
size_t volume_len = strlen (volume);
uint32_t hash = hash_fnv32 (volume, strlen (volume));
spin_lock (&mount_table.lock);
hash_find (&mount_table, mountpoint, mountpoint_len, hash,
lengthof (mount_table.mountpoint_buckets), mountpoint_buckets, struct vfs_mountpoint,
mount_table_link, key, found_link);
spin_unlock (&mount_table.lock);
spin_lock (&volume_table.lock);
hash_find (&volume_table, volume, volume_len, hash, lengthof (volume_table.volume_buckets),
volume_buckets, struct vfs_volume, volume_table_link, key, found_link);
spin_unlock (&volume_table.lock);
if (found_link == NULL)
return NULL;
return hash_entry (found_link, struct vfs_mountpoint, mount_table_link);
return hash_entry (found_link, struct vfs_volume, volume_table_link);
}
int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_device,
struct proc* proc, struct reschedule_ctx* rctx) {
if (strlen_null (key) > fieldsizeof (struct vfs_mountpoint, key))
int vfs_create_volume (const char* key, int fs_type, struct device* back_device, struct proc* proc,
struct reschedule_ctx* rctx) {
if (strlen_null (key) > VOLUME_MAX)
return -ST_OOB_ERROR;
struct vfs_mountpoint* mountpoint = malloc (sizeof (*mountpoint));
struct vfs_volume* volume = malloc (sizeof (*volume));
if (mountpoint == NULL)
if (volume == NULL)
return -ST_OOM_ERROR;
memset (mountpoint, 0, sizeof (*mountpoint));
memset (volume, 0, sizeof (*volume));
memcpy (mountpoint->key, key, strlen_null (key));
mountpoint->fs_type = fs_type;
mountpoint->back_device = back_device;
memcpy (volume->key, key, strlen_null (key));
volume->fs_type = fs_type;
volume->back_device = back_device;
volume->lock = SPIN_LOCK_INIT;
switch (mountpoint->fs_type) {
switch (volume->fs_type) {
case VFS_TARFS: {
mountpoint->driver_ops.mount = &tarfs_mount;
mountpoint->driver_ops.describe = &tarfs_describe;
mountpoint->driver_ops.read = &tarfs_read;
volume->driver_ops.mount = &tarfs_mount;
volume->driver_ops.describe = &tarfs_describe;
volume->driver_ops.read = &tarfs_read;
} break;
default: {
free (mountpoint);
free (volume);
return -ST_MOUNT_ERROR;
} break;
}
int ret = mountpoint->driver_ops.mount (mountpoint, proc, rctx);
int ret = volume->driver_ops.mount (volume, proc, rctx);
if (ret < 0) {
free (mountpoint);
free (volume);
return ret;
}
uint32_t mp_hash = hash_fnv32 (mountpoint->key, strlen (mountpoint->key));
uint32_t mp_hash = hash_fnv32 (volume->key, strlen (volume->key));
spin_lock (&mount_table.lock);
spin_lock (&volume_table.lock);
hash_insert (&mount_table, &mountpoint->mount_table_link, mp_hash,
lengthof (mount_table.mountpoint_buckets), mountpoint_buckets);
hash_insert (&volume_table, &volume->volume_table_link, mp_hash,
lengthof (volume_table.volume_buckets), volume_buckets);
spin_unlock (&mount_table.lock);
spin_unlock (&volume_table.lock);
return ST_OK;
}
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path) {
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
int vfs_volume_open (struct proc* proc, const char* volume_name, struct reschedule_ctx* rctx) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
if (vmp == NULL)
if (volume == NULL)
return -ST_NOT_FOUND;
struct vfs_handle* handle = malloc (sizeof (*handle));
spin_lock (&volume->lock);
if (handle == NULL)
return -ST_OOM_ERROR;
memset (handle, 0, sizeof (*handle));
handle->mountpoint = vmp;
handle->ownerpg = procgroup;
strncpy (handle->path, path, sizeof (handle->path));
spin_lock (&procgroup->lock);
int id = handle->id = id_alloc (&procgroup->vfs_handle_id_alloc);
if (id < 0) {
free (handle);
spin_unlock (&procgroup->lock);
return -ST_OOM_ERROR;
if (!volume->locked) {
volume->locked = true;
volume->owner = proc;
spin_unlock (&volume->lock);
return ST_OK;
} else {
if (proc == VFS_KERNEL) {
spin_unlock (&volume->lock);
return -ST_TRY_AGAIN;
} else {
proc_sq_suspend (proc, &volume->sq, &volume->lock, rctx);
return ST_OK;
}
}
rbtree_insert (struct vfs_handle, &procgroup->vfs_handle_tree, &handle->handle_tree_link,
handle_tree_link, id);
spin_unlock (&procgroup->lock);
return id;
}
int vfs_close (struct procgroup* procgroup, int id) {
struct vfs_handle* handle = NULL;
int vfs_volume_close (struct proc* proc, const char* volume_name, struct reschedule_ctx* rctx) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
spin_lock (&procgroup->lock);
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
if (handle == NULL) {
spin_unlock (&procgroup->lock);
if (volume == NULL)
return -ST_NOT_FOUND;
spin_lock (&volume->lock);
if (volume->locked && volume->owner != proc) {
spin_unlock (&volume->lock);
return -ST_PERMISSION_ERROR;
}
spin_lock (&handle->lock);
spin_lock (&volume->sq.lock);
rbtree_delete (&procgroup->vfs_handle_tree, &handle->handle_tree_link);
struct list_node_link* node = volume->sq.proc_list;
id_free (&procgroup->vfs_handle_id_alloc, handle->id);
if (node) {
struct proc_sq_entry* sq_entry = list_entry (node, struct proc_sq_entry, sq_link);
struct proc* resumed_proc = sq_entry->proc;
spin_unlock (&handle->lock);
spin_unlock (&procgroup->lock);
volume->owner = proc;
volume->locked = true;
free (handle);
spin_unlock (&volume->sq.lock);
spin_unlock (&volume->lock);
proc_sq_resume (resumed_proc, sq_entry, rctx);
return ST_OK;
}
volume->locked = false;
volume->owner = NULL;
spin_unlock (&volume->sq.lock);
spin_unlock (&volume->lock);
return ST_OK;
}
int vfs_describe (struct procgroup* procgroup, int id, struct desc* desc) {
struct vfs_handle* handle = NULL;
int vfs_read (struct proc* proc, const char* volume_name, const char* path, uint8_t* buffer,
size_t off, size_t size, struct reschedule_ctx* rctx) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
spin_lock (&procgroup->lock);
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
if (handle == NULL) {
spin_unlock (&procgroup->lock);
if (volume == NULL)
return -ST_NOT_FOUND;
spin_lock (&volume->lock);
if (volume->locked && volume->owner != proc) {
spin_unlock (&volume->lock);
return -ST_PERMISSION_ERROR;
}
spin_unlock (&procgroup->lock);
spin_unlock (&volume->lock);
spin_lock (&handle->lock);
int ret = handle->mountpoint->driver_ops.describe (handle->mountpoint, handle->path, desc);
spin_unlock (&handle->lock);
return ret;
return volume->driver_ops.read (volume, path, buffer, off, size);
}
int vfs_read (struct procgroup* procgroup, int id, uint8_t* buffer, size_t off, size_t size) {
struct vfs_handle* handle = NULL;
int vfs_describe (struct proc* proc, const char* volume_name, const char* path, struct desc* desc,
struct reschedule_ctx* rctx) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
spin_lock (&procgroup->lock);
rbtree_find (struct vfs_handle, &procgroup->vfs_handle_tree, id, handle, handle_tree_link, id);
if (handle == NULL) {
spin_unlock (&procgroup->lock);
if (volume == NULL)
return -ST_NOT_FOUND;
spin_lock (&volume->lock);
if (volume->locked && volume->owner != proc) {
spin_unlock (&volume->lock);
return -ST_PERMISSION_ERROR;
}
spin_unlock (&procgroup->lock);
spin_unlock (&volume->lock);
spin_lock (&handle->lock);
int ret =
handle->mountpoint->driver_ops.read (handle->mountpoint, handle->path, buffer, off, size);
spin_unlock (&handle->lock);
return ret;
}
int vfs_kernel_read (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)
return -ST_NOT_FOUND;
return vmp->driver_ops.read (vmp, path, buffer, off, size);
}
int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc* desc) {
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
return -ST_NOT_FOUND;
return vmp->driver_ops.describe (vmp, path, desc);
}
void vfs_procgroup_cleanup (struct procgroup* procgroup) {
struct vfs_handle* handle;
struct rb_node_link* node;
rbtree_first (&procgroup->vfs_handle_tree, node);
while (node != NULL) {
struct rb_node_link* next;
rbtree_next (node, next);
handle = rbtree_entry (node, struct vfs_handle, handle_tree_link);
node = next;
vfs_close (procgroup, handle->id);
}
return volume->driver_ops.describe (volume, path, desc);
}
void vfs_init (void) {
memset (&mount_table, 0, sizeof (mount_table));
memset (&volume_table, 0, sizeof (volume_table));
mount_table.lock = SPIN_LOCK_INIT;
volume_table.lock = SPIN_LOCK_INIT;
}

View File

@@ -7,64 +7,56 @@
#include <libk/list.h>
#include <libk/rbtree.h>
#include <libk/std.h>
#include <path.h>
#include <path_defs.h>
#include <proc/proc.h>
#include <proc/procgroup.h>
#include <proc/reschedule.h>
#include <proc/suspension_q.h>
#include <sync/spin_lock.h>
#define VFS_KERNEL ((struct proc*)0x123)
#define VFS_TARFS 0
struct vfs_mountpoint;
struct vfs_volume;
struct vfs_handle {
int id;
struct vfs_mountpoint* mountpoint;
char path[MAX_PATH];
struct procgroup* ownerpg;
struct rb_node_link handle_tree_link;
spin_lock_t lock;
};
struct vfs_mountpoint {
char key[0x100];
struct hash_node_link mount_table_link;
struct vfs_volume {
char key[VOLUME_MAX];
struct hash_node_link volume_table_link;
int fs_type;
spin_lock_t lock;
struct proc* owner;
bool locked;
struct proc_suspension_q sq;
struct {
int (*mount) (struct vfs_mountpoint* mountpoint, struct proc* proc,
struct reschedule_ctx* rctx);
int (*mount) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int (*describe) (struct vfs_mountpoint* mountpoint, const char* path, struct desc* desc);
int (*describe) (struct vfs_volume* volume, const char* path, struct desc* desc);
int (*read) (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
int (*read) (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
} driver_ops;
struct device* back_device;
void* udata
};
struct vfs_mount_table {
struct hash_node_link* mountpoint_buckets[1024];
struct vfs_volume_table {
struct hash_node_link* volume_buckets[1024];
spin_lock_t lock;
};
int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_device,
struct proc* proc, struct reschedule_ctx* rctx);
int vfs_create_volume (const char* key, int fs_type, struct device* back_device, struct proc* proc,
struct reschedule_ctx* rctx);
int vfs_describe (struct procgroup* procgroup, int id, struct desc* desc);
int vfs_volume_open (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_read (struct procgroup* procgroup, int id, uint8_t* buffer, size_t off, size_t size);
int vfs_volume_close (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_close (struct procgroup* procgroup, int id);
int vfs_read (struct proc* proc, const char* volume, const char* path, uint8_t* buffer, size_t off,
size_t size, struct reschedule_ctx* rctx);
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path);
int vfs_kernel_read (const char* mountpoint, const char* path, uint8_t* buffer, size_t off,
size_t size);
int vfs_kernel_describe (const char* mountpoint, const char* path, struct desc* desc);
void vfs_procgroup_cleanup (struct procgroup* procgroup);
int vfs_describe (struct proc* proc, const char* volume, const char* path, struct desc* desc,
struct reschedule_ctx* rctx);
void vfs_init (void);