Virtual filesystem and ramdiskfs
All checks were successful
Build documentation / build-and-deploy (push) Successful in 27s

This commit is contained in:
2026-02-11 21:36:50 +01:00
parent 9e6035bd68
commit 4ad1519e06
21 changed files with 528 additions and 128 deletions

View File

@@ -0,0 +1,12 @@
#ifndef _M_FS_DESC_BUFFER_H
#define _M_FS_DESC_BUFFER_H
#define FS_FILE 0
#define FS_DIR 1
struct fs_desc_buffer {
int type;
size_t size;
};
#endif // _M_FS_DESC_BUFFER_H

View File

@@ -6,6 +6,7 @@
#include <amd64/msr-index.h> #include <amd64/msr-index.h>
#include <amd64/msr.h> #include <amd64/msr.h>
#include <aux/compiler.h> #include <aux/compiler.h>
#include <fs/vfs.h>
#include <irq/irq.h> #include <irq/irq.h>
#include <libk/std.h> #include <libk/std.h>
#include <limine/limine.h> #include <limine/limine.h>
@@ -13,7 +14,6 @@
#include <mm/liballoc.h> #include <mm/liballoc.h>
#include <mm/pmm.h> #include <mm/pmm.h>
#include <proc/proc.h> #include <proc/proc.h>
#include <rd/rd.h>
#include <sys/debug.h> #include <sys/debug.h>
#include <sys/mm.h> #include <sys/mm.h>
#include <sys/smp.h> #include <sys/smp.h>
@@ -40,13 +40,15 @@ void bootmain (void) {
pmm_init (); pmm_init ();
mm_init (); mm_init ();
rd_init ();
uacpi_setup_early_table_access ((void*)uacpi_memory_buffer, sizeof (uacpi_memory_buffer)); uacpi_setup_early_table_access ((void*)uacpi_memory_buffer, sizeof (uacpi_memory_buffer));
amd64_ioapic_init (); amd64_ioapic_init ();
amd64_hpet_init (); amd64_hpet_init ();
vfs_init ();
vfs_create_mountpoint ("ramdisk", VFS_RAMDISKFS);
smp_init (); smp_init ();
proc_init (); proc_init ();

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_spawn_rd ("spin.exe"); struct proc* spin_proc = proc_from_file ("ramdisk", "/spin.exe");
struct cpu* spin_cpu = thiscpu; struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu); proc_register (spin_proc, &spin_cpu);

96
kernel/fs/path.c Normal file
View File

@@ -0,0 +1,96 @@
#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 == '_'));
}
bool path_validate (const char* path) {
if (path == NULL || *path == '\0')
return false;
const char* ptr = path;
if (*ptr == ':')
return false;
while (*ptr != ':' && *ptr != '\0') {
if (!path_validate_char (*ptr))
return false;
ptr++;
}
if (*ptr != ':')
return false;
ptr++;
if (*ptr != '/')
return false;
while (*ptr != '\0') {
if (*ptr == '/' && *(ptr + 1) == '/')
return false;
if (!path_validate_char (*ptr))
return false;
ptr++;
}
if (ptr > path && *(ptr - 1) == '/' && *(ptr - 2) != ':')
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;
}
const char* path_basename (const char* path) {
if (path == NULL)
return NULL;
const char* last_slash = NULL;
const char* ptr = path;
while (*ptr != '\0') {
if (*ptr == '/')
last_slash = ptr;
ptr++;
}
if (last_slash == NULL)
return path;
return last_slash + 1;
}

13
kernel/fs/path.h Normal file
View File

@@ -0,0 +1,13 @@
#ifndef _KERNEL_FS_PATH_H
#define _KERNEL_FS_PATH_H
#include <libk/std.h>
/* Path scheme: MOUNTPOINT:/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

138
kernel/fs/ramdiskfs.c Normal file
View File

@@ -0,0 +1,138 @@
#include <fs/path.h>
#include <fs/ramdiskfs.h>
#include <fs/vfs.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <m/fs_desc_buffer.h>
struct ramdisk_tar_header {
char filename[100];
uint8_t mode[8];
uint8_t uid[8];
uint8_t gid[8];
uint8_t size[12];
uint8_t mtime[12];
uint8_t checksum[8];
uint8_t type_flag;
} PACKED;
struct ramdisk_tar_file {
struct ramdisk_tar_header* header;
uint8_t* content;
size_t size;
};
#define RAMDISK_FILES_MAX 128
#define RAMDISK_PATH "/boot/mop3dist.tar"
static struct ramdisk_tar_file ramdisk_files[RAMDISK_FILES_MAX];
static struct ramdisk_tar_file* ramdisk_get_file (const char* filename) {
for (size_t i = 0; i < RAMDISK_FILES_MAX; i++) {
if ((ramdisk_files[i].header != NULL) &&
(memcmp (ramdisk_files[i].header->filename, filename, strlen (filename)) == 0))
return &ramdisk_files[i];
}
return NULL;
}
static size_t ramdisk_tar_get_size (uint8_t* in) {
size_t size = 0;
size_t j;
size_t count = 1;
for (j = 11; j > 0; j--, count *= 8)
size += ((in[j - 1] - '0') * count);
return size;
}
static size_t ramdisk_tar_parse (uint8_t* addr) {
size_t i;
for (i = 0;; i++) {
struct ramdisk_tar_header* hdr = (struct ramdisk_tar_header*)addr;
if (hdr->filename[i] == '\0')
break;
size_t size = ramdisk_tar_get_size (hdr->size);
ramdisk_files[i].header = hdr;
ramdisk_files[i].content = (uint8_t*)((uintptr_t)hdr + 512);
ramdisk_files[i].size = ramdisk_tar_get_size ((uint8_t*)hdr->size);
addr += ((size / 512) + 1) * 512;
if (size % 512)
addr += 512;
}
return i;
}
bool ramdiskfs_mount (struct vfs_mountpoint* mountpoint) {
(void)mountpoint;
struct limine_module_response* module = limine_module_request.response;
uint8_t* rd_addr = NULL;
for (size_t i = 0; i < module->module_count; i++) {
struct limine_file* file = module->modules[i];
if (memcmp (file->path, RAMDISK_PATH, strlen (RAMDISK_PATH)) == 0) {
rd_addr = file->address;
}
}
if (rd_addr == NULL)
return false;
ramdisk_tar_parse (rd_addr);
return true;
}
int ramdiskfs_describe (struct vfs_mountpoint* mountpoint, const char* path,
struct fs_desc_buffer* desc) {
(void)mountpoint;
const char* filename = path_basename (path);
if (filename == NULL)
return -VFS_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -VFS_NOT_FOUND;
desc->size = file->size;
desc->type = FS_FILE;
return VFS_OK;
}
int ramdiskfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer,
size_t off, size_t size) {
(void)mountpoint;
const char* filename = path_basename (path);
if (filename == NULL)
return -VFS_BAD_PATH;
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
if (file == NULL)
return -VFS_NOT_FOUND;
if (off + size > file->size)
return -VFS_OOB_ERROR;
memcpy (buffer, (void*)((uintptr_t)file->content + off), size);
return VFS_OK;
}

15
kernel/fs/ramdiskfs.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef _KERNEL_FS_RAMDISKFS_H
#define _KERNEL_FS_RAMDISKFS_H
#include <libk/std.h>
#include <m/fs_desc_buffer.h>
struct vfs_mountpoint;
bool ramdiskfs_mount (struct vfs_mountpoint* mountpoint);
int ramdiskfs_describe (struct vfs_mountpoint* mountpoint, const char* path,
struct fs_desc_buffer* desc);
int ramdiskfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer,
size_t off, size_t size);
#endif // _KERNEL_FS_RAMDISKFS_H

7
kernel/fs/src.mk Normal file
View File

@@ -0,0 +1,7 @@
c += fs/vfs.c \
fs/ramdiskfs.c \
fs/path.c
o += fs/vfs.o \
fs/ramdiskfs.o \
fs/path.o

99
kernel/fs/vfs.c Normal file
View File

@@ -0,0 +1,99 @@
#include <fs/ramdiskfs.h>
#include <fs/vfs.h>
#include <libk/fieldsizeof.h>
#include <libk/hash.h>
#include <libk/lengthof.h>
#include <libk/std.h>
#include <libk/string.h>
#include <mm/liballoc.h>
#include <sync/spin_lock.h>
#include <sys/debug.h>
static struct vfs_mount_table mount_table;
static struct vfs_mountpoint* vfs_find_mountpoint (const char* mountpoint) {
struct hash_node_link* found_link = NULL;
size_t mountpoint_len = strlen (mountpoint);
uint32_t hash = hash_fnv32 (mountpoint, strlen (mountpoint));
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);
if (found_link == NULL)
return NULL;
return hash_entry (found_link, struct vfs_mountpoint, mount_table_link);
}
struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
if (strlen_null (key) > fieldsizeof (struct vfs_mountpoint, key))
return NULL;
struct vfs_mountpoint* mountpoint = malloc (sizeof (*mountpoint));
if (mountpoint == NULL)
return NULL;
memset (mountpoint, 0, sizeof (*mountpoint));
memcpy (mountpoint->key, key, strlen_null (key));
mountpoint->fs_type = fs_type;
mountpoint->lock = SPIN_LOCK_INIT;
switch (mountpoint->fs_type) {
case VFS_RAMDISKFS: {
mountpoint->driver_ops.mount = &ramdiskfs_mount;
mountpoint->driver_ops.describe = &ramdiskfs_describe;
mountpoint->driver_ops.read = &ramdiskfs_read;
} break;
default: {
free (mountpoint);
return NULL;
} break;
}
bool ok = mountpoint->driver_ops.mount (mountpoint);
if (!ok) {
free (mountpoint);
return NULL;
}
uint32_t mp_hash = hash_fnv32 (mountpoint->key, strlen (mountpoint->key));
spin_lock (&mount_table.lock);
hash_insert (&mount_table, &mountpoint->mount_table_link, mp_hash,
lengthof (mount_table.mountpoint_buckets), mountpoint_buckets);
spin_unlock (&mount_table.lock);
return mountpoint;
}
int vfs_describe (const char* mountpoint, const char* path, struct fs_desc_buffer* desc) {
struct vfs_mountpoint* vmp = vfs_find_mountpoint (mountpoint);
if (vmp == NULL)
return -VFS_NOT_FOUND;
return vmp->driver_ops.describe (vmp, path, desc);
}
int vfs_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 -VFS_NOT_FOUND;
return vmp->driver_ops.read (vmp, path, buffer, off, size);
}
void vfs_init (void) {
memset (&mount_table, 0, sizeof (mount_table));
mount_table.lock = SPIN_LOCK_INIT;
}

42
kernel/fs/vfs.h Normal file
View File

@@ -0,0 +1,42 @@
#ifndef _KERNEL_FS_VFS_H
#define _KERNEL_FS_VFS_H
#include <libk/hash.h>
#include <libk/list.h>
#include <libk/std.h>
#include <m/fs_desc_buffer.h>
#include <sync/spin_lock.h>
#define VFS_OK 0
#define VFS_EXISTS 1
#define VFS_NOT_FOUND 2
#define VFS_BAD_PATH 3
#define VFS_OOB_ERROR 4
#define VFS_RAMDISKFS 0
struct vfs_mountpoint {
char key[0x100];
struct hash_node_link mount_table_link;
int fs_type;
spin_lock_t lock;
struct {
bool (*mount) (struct vfs_mountpoint* mountpoint);
int (*describe) (struct vfs_mountpoint* mountpoint, const char* path,
struct fs_desc_buffer* desc);
int (*read) (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
size_t size);
} driver_ops;
};
struct vfs_mount_table {
struct hash_node_link* mountpoint_buckets[1024];
spin_lock_t lock;
};
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);
void vfs_init (void);
#endif // _KERNEL_FS_VFS_H

View File

@@ -0,0 +1,6 @@
#ifndef _KERNEL_LIBK_FIELDSIZEOF_H
#define _KERNEL_LIBK_FIELDSIZEOF_H
#define fieldsizeof(type, field) (sizeof (((type*)0)->field))
#endif // _KERNEL_LIBK_FIELDSIZEOF_H

51
kernel/libk/hash.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef _KERNEL_LIBK_HASH_H
#define _KERNEL_LIBK_HASH_H
#include <libk/std.h>
struct hash_node_link {
struct hash_node_link* next;
uint32_t hash;
};
#define hash_entry(ptr, type, member) ((type*)((char*)(ptr) - offsetof (type, member)))
static inline uint32_t hash_fnv32 (const void* data, size_t len) {
uint32_t hash = 2166136261U;
const uint8_t* bp = (const uint8_t*)data;
const uint8_t* be = bp + len;
while (bp < be) {
hash ^= (uint32_t)*bp++;
hash *= 16777619U;
}
return hash;
}
#define hash_insert(table, new_link, hash_val, table_size, buckets_name) \
do { \
uint32_t __idx = (hash_val) % (table_size); \
(new_link)->hash = (hash_val); \
(new_link)->next = (table)->buckets_name[__idx]; \
(table)->buckets_name[__idx] = (new_link); \
} while (0)
#define hash_find(table, key_ptr, key_size, hash_val, table_size, buckets_name, type, member, \
key_field, out_link) \
do { \
uint32_t __idx = (hash_val) % (table_size); \
struct hash_node_link* __curr = (table)->buckets_name[__idx]; \
(out_link) = NULL; \
while (__curr) { \
if (__curr->hash == (hash_val)) { \
type* __entry = hash_entry (__curr, type, member); \
if (memcmp (__entry->key_field, (key_ptr), (key_size)) == 0) { \
(out_link) = __curr; \
break; \
} \
} \
__curr = __curr->next; \
} \
} while (0)
#endif // _KERNEL_LIBK_HASH_H

6
kernel/libk/lengthof.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef _KERNEL_LIBK_LENGTHOF_H
#define _KERNEL_LIBK_LENGTHOF_H
#define lengthof(x) (sizeof ((x)) / sizeof ((x)[0]))
#endif // _KERNEL_LIBK_LENGTHOF_H

View File

@@ -9,4 +9,6 @@ void strncpy (char* dst, const char* src, size_t n);
size_t strlen (const char* str); size_t strlen (const char* str);
int memcmp (const void* s1, const void* s2, size_t n); int memcmp (const void* s1, const void* s2, size_t n);
#define strlen_null(x) (strlen ((x)) + 1)
#endif // _KERNEL_LIBK_STRING_H #endif // _KERNEL_LIBK_STRING_H

View File

@@ -1,5 +1,6 @@
#include <aux/compiler.h> #include <aux/compiler.h>
#include <aux/elf.h> #include <aux/elf.h>
#include <fs/vfs.h>
#include <irq/irq.h> #include <irq/irq.h>
#include <libk/align.h> #include <libk/align.h>
#include <libk/list.h> #include <libk/list.h>
@@ -7,12 +8,12 @@
#include <libk/std.h> #include <libk/std.h>
#include <libk/string.h> #include <libk/string.h>
#include <limine/requests.h> #include <limine/requests.h>
#include <m/fs_desc_buffer.h>
#include <mm/liballoc.h> #include <mm/liballoc.h>
#include <mm/pmm.h> #include <mm/pmm.h>
#include <proc/proc.h> #include <proc/proc.h>
#include <proc/procgroup.h> #include <proc/procgroup.h>
#include <proc/resource.h> #include <proc/resource.h>
#include <rd/rd.h>
#include <sync/spin_lock.h> #include <sync/spin_lock.h>
#include <sys/debug.h> #include <sys/debug.h>
#include <sys/mm.h> #include <sys/mm.h>
@@ -100,17 +101,38 @@ struct elf_aux proc_load_segments (struct proc* proc, uint8_t* elf) {
return aux; return aux;
} }
struct proc* proc_spawn_rd (char* name) { struct proc* proc_from_file (const char* mountpoint, const char* path) {
struct rd_file* rd_file = rd_get_file (name); struct fs_desc_buffer desc;
bool ok = proc_check_elf (rd_file->content); if (vfs_describe (mountpoint, path, &desc) != VFS_OK)
DEBUG ("Spawning %s, elf header %s\n", name, ok ? "ok" : "bad");
if (!ok)
return NULL; return NULL;
return proc_from_elf (rd_file->content); if (desc.type != FS_FILE)
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) != VFS_OK) {
free (temp_buffer);
return NULL;
}
bool ok = proc_check_elf (temp_buffer);
DEBUG ("Spawning %s:%s, elf header %s\n", mountpoint, path, ok ? "ok" : "bad");
if (!ok) {
free (temp_buffer);
return NULL;
}
struct proc* proc = proc_from_elf (temp_buffer);
free (temp_buffer);
return proc;
} }
struct proc* proc_find_pid (int pid) { struct proc* proc_find_pid (int pid) {
@@ -275,11 +297,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_spawn_rd ("spin.exe"); struct proc* spin_proc = proc_from_file ("ramdisk", "/spin.exe");
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_spawn_rd ("init.exe"); struct proc* init = proc_from_file ("ramdisk", "/init.exe");
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_spawn_rd (char* name); struct proc* proc_from_file (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,81 +0,0 @@
#include <aux/compiler.h>
#include <libk/std.h>
#include <libk/string.h>
#include <limine/requests.h>
#include <rd/rd.h>
#include <sys/debug.h>
#define RD_FILES_MAX 64
#define RD_PATH "/boot/mop3dist.tar"
static struct rd_file rd_files[RD_FILES_MAX];
struct rd_file* rd_get_file (char* filename) {
for (size_t i = 0; i < RD_FILES_MAX; i++) {
if ((rd_files[i].hdr != NULL) &&
(memcmp (rd_files[i].hdr->filename, filename, strlen (filename)) == 0))
return &rd_files[i];
}
return NULL;
}
static size_t rd_tar_get_size (uint8_t* in) {
size_t size = 0;
size_t j;
size_t count = 1;
for (j = 11; j > 0; j--, count *= 8)
size += ((in[j - 1] - '0') * count);
return size;
}
static size_t rd_tar_parse (uint8_t* addr) {
size_t i;
for (i = 0;; i++) {
struct tar_hdr* hdr = (struct tar_hdr*)addr;
if (hdr->filename[i] == '\0')
break;
size_t size = rd_tar_get_size (hdr->size);
rd_files[i].hdr = hdr;
rd_files[i].content = (uint8_t*)((uintptr_t)hdr + 512);
rd_files[i].size = rd_tar_get_size ((uint8_t*)hdr->size);
DEBUG ("filename=%s\n", hdr->filename);
addr += ((size / 512) + 1) * 512;
if (size % 512)
addr += 512;
}
return i;
}
void rd_init (void) {
struct limine_module_response* module = limine_module_request.response;
uint8_t* rd_addr = NULL;
for (size_t i = 0; i < module->module_count; i++) {
struct limine_file* file = module->modules[i];
DEBUG ("%s\n", file->path);
if (memcmp (file->path, RD_PATH, strlen (RD_PATH)) == 0) {
rd_addr = file->address;
}
}
if (rd_addr == NULL) {
DEBUG ("mop3dist.tar NOT FOUND!\n");
for (;;)
;
}
rd_tar_parse (rd_addr);
}

View File

@@ -1,27 +0,0 @@
#ifndef _KERNEL_RD_RD_H
#define _KERNEL_RD_RD_H
#include <aux/compiler.h>
#include <libk/std.h>
struct tar_hdr {
char filename[100];
uint8_t mode[8];
uint8_t uid[8];
uint8_t gid[8];
uint8_t size[12];
uint8_t mtime[12];
uint8_t checksum[8];
uint8_t type_flag;
} PACKED;
struct rd_file {
struct tar_hdr* hdr;
uint8_t* content;
size_t size;
};
struct rd_file* rd_get_file (char* filename);
void rd_init (void);
#endif // _KERNEL_RD_RD_H

View File

@@ -1,3 +0,0 @@
c += rd/rd.c
o += rd/rd.o

View File

@@ -5,6 +5,6 @@ include mm/src.mk
include limine/src.mk include limine/src.mk
include uACPI/src.mk include uACPI/src.mk
include irq/src.mk include irq/src.mk
include rd/src.mk
include proc/src.mk include proc/src.mk
include syscall/src.mk include syscall/src.mk
include fs/src.mk