VFS mountpoint backing device system
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m24s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m24s
This commit is contained in:
@@ -1,136 +0,0 @@
|
||||
#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>
|
||||
#include <m/fs_desc_buffer.h>
|
||||
#include <m/status.h>
|
||||
#include <sys/debug.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_FILENAME_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) &&
|
||||
(strncmp (ramdisk_files[i].header->filename, filename, RAMDISK_FILENAME_MAX) == 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 < RAMDISK_FILES_MAX; i++) {
|
||||
struct ramdisk_tar_header* hdr = (struct ramdisk_tar_header*)addr;
|
||||
|
||||
if (hdr->filename[0] == '\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 += 512 + ((size + 511) & ~511);
|
||||
}
|
||||
|
||||
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 (strncmp (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 -ST_BAD_PATH;
|
||||
|
||||
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
|
||||
|
||||
if (file == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
desc->size = file->size;
|
||||
desc->type = FS_FILE;
|
||||
|
||||
return ST_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 -ST_BAD_PATH;
|
||||
|
||||
struct ramdisk_tar_file* file = ramdisk_get_file (filename);
|
||||
|
||||
if (file == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size));
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#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
|
||||
@@ -1,7 +1,7 @@
|
||||
c += fs/vfs.c \
|
||||
fs/ramdiskfs.c \
|
||||
fs/tarfs.c \
|
||||
fs/path.c
|
||||
|
||||
o += fs/vfs.o \
|
||||
fs/ramdiskfs.o \
|
||||
fs/tarfs.o \
|
||||
fs/path.o
|
||||
|
||||
142
kernel/fs/tarfs.c
Normal file
142
kernel/fs/tarfs.c
Normal file
@@ -0,0 +1,142 @@
|
||||
#include <fs/path.h>
|
||||
#include <fs/tarfs.h>
|
||||
#include <fs/vfs.h>
|
||||
#include <libk/align.h>
|
||||
#include <libk/minmax.h>
|
||||
#include <libk/std.h>
|
||||
#include <libk/string.h>
|
||||
#include <limine/requests.h>
|
||||
#include <m/fs_desc_buffer.h>
|
||||
#include <m/path.h>
|
||||
#include <m/status.h>
|
||||
#include <m/xdrv_device.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
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))
|
||||
return &tarfs->tarfs_files[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static size_t 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 tar_parse (struct tarfs* tarfs, uint8_t* addr) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < TARFS_FILES_MAX; i++) {
|
||||
struct tar_header* hdr = (struct tar_header*)addr;
|
||||
|
||||
if (hdr->filename[0] == '\0')
|
||||
break;
|
||||
|
||||
size_t size = tar_get_size (hdr->size);
|
||||
|
||||
tarfs->tarfs_files[i].header = hdr;
|
||||
tarfs->tarfs_files[i].content = (uint8_t*)((uintptr_t)hdr + 512);
|
||||
tarfs->tarfs_files[i].size = tar_get_size ((uint8_t*)hdr->size);
|
||||
|
||||
addr += 512 + ((size + 511) & ~511);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int tarfs_mount (struct vfs_mountpoint* mountpoint, struct device_op_ctx* op_ctx) {
|
||||
struct tarfs* tarfs = malloc (sizeof (*tarfs));
|
||||
|
||||
if (tarfs == NULL)
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
memset (tarfs, 0, sizeof (*tarfs));
|
||||
|
||||
mountpoint->udata = tarfs;
|
||||
|
||||
struct device* back_device = mountpoint->back_device;
|
||||
size_t total_size;
|
||||
int ret;
|
||||
|
||||
spin_lock (&back_device->lock);
|
||||
|
||||
ret = back_device->ops[XDRV_GET_SIZE](back_device, op_ctx, &total_size, NULL, NULL, NULL);
|
||||
if (ret < 0) {
|
||||
spin_unlock (&back_device->lock);
|
||||
free (mountpoint->udata);
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t* buffer = malloc (total_size);
|
||||
|
||||
if (buffer == NULL) {
|
||||
spin_unlock (&back_device->lock);
|
||||
free (mountpoint->udata);
|
||||
return ret;
|
||||
}
|
||||
|
||||
size_t pos = 0;
|
||||
ret = back_device->ops[XDRV_READ](back_device, op_ctx, &pos, &total_size, buffer, NULL);
|
||||
|
||||
spin_unlock (&back_device->lock);
|
||||
|
||||
if (ret < 0) {
|
||||
free (buffer);
|
||||
return ret;
|
||||
}
|
||||
|
||||
tarfs->buffer = buffer;
|
||||
|
||||
tar_parse (tarfs, tarfs->buffer);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int tarfs_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 -ST_BAD_PATH;
|
||||
|
||||
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
|
||||
|
||||
if (file == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
desc->size = file->size;
|
||||
desc->type = FS_FILE;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int tarfs_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 -ST_BAD_PATH;
|
||||
|
||||
struct tar_file* file = tar_get_file ((struct tarfs*)mountpoint->udata, filename);
|
||||
|
||||
if (file == NULL)
|
||||
return -ST_NOT_FOUND;
|
||||
|
||||
memcpy (buffer, (void*)((uintptr_t)file->content + off), min (size, file->size));
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
42
kernel/fs/tarfs.h
Normal file
42
kernel/fs/tarfs.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef _KERNEL_FS_TARFS_H
|
||||
#define _KERNEL_FS_TARFS_H
|
||||
|
||||
#include <device/device.h>
|
||||
#include <libk/std.h>
|
||||
#include <m/fs_desc_buffer.h>
|
||||
|
||||
struct 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 tar_file {
|
||||
struct tar_header* header;
|
||||
uint8_t* content;
|
||||
size_t size;
|
||||
};
|
||||
|
||||
#define TARFS_FILES_MAX 128
|
||||
|
||||
struct tarfs {
|
||||
struct tar_file tarfs_files[TARFS_FILES_MAX];
|
||||
uint8_t* buffer;
|
||||
};
|
||||
|
||||
struct vfs_mountpoint;
|
||||
|
||||
int tarfs_mount (struct vfs_mountpoint* mountpoint, struct device_op_ctx* op_ctx);
|
||||
|
||||
int tarfs_describe (struct vfs_mountpoint* mountpoint, const char* path,
|
||||
struct fs_desc_buffer* desc);
|
||||
|
||||
int tarfs_read (struct vfs_mountpoint* mountpoint, const char* path, uint8_t* buffer, size_t off,
|
||||
size_t size);
|
||||
|
||||
#endif // _KERNEL_FS_TARFS_H
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <fs/ramdiskfs.h>
|
||||
#include <device/device.h>
|
||||
#include <fs/tarfs.h>
|
||||
#include <fs/vfs.h>
|
||||
#include <libk/fieldsizeof.h>
|
||||
#include <libk/hash.h>
|
||||
@@ -30,38 +31,40 @@ static struct vfs_mountpoint* vfs_find_mountpoint (const char* mountpoint) {
|
||||
return hash_entry (found_link, struct vfs_mountpoint, mount_table_link);
|
||||
}
|
||||
|
||||
struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
|
||||
int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_device,
|
||||
struct device_op_ctx* op_ctx) {
|
||||
if (strlen_null (key) > fieldsizeof (struct vfs_mountpoint, key))
|
||||
return NULL;
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
struct vfs_mountpoint* mountpoint = malloc (sizeof (*mountpoint));
|
||||
|
||||
if (mountpoint == NULL)
|
||||
return NULL;
|
||||
return -ST_OOM_ERROR;
|
||||
|
||||
memset (mountpoint, 0, sizeof (*mountpoint));
|
||||
|
||||
memcpy (mountpoint->key, key, strlen_null (key));
|
||||
mountpoint->fs_type = fs_type;
|
||||
mountpoint->lock = SPIN_LOCK_INIT;
|
||||
mountpoint->back_device = back_device;
|
||||
|
||||
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;
|
||||
case VFS_TARFS: {
|
||||
mountpoint->driver_ops.mount = &tarfs_mount;
|
||||
mountpoint->driver_ops.describe = &tarfs_describe;
|
||||
mountpoint->driver_ops.read = &tarfs_read;
|
||||
} break;
|
||||
default: {
|
||||
free (mountpoint);
|
||||
return NULL;
|
||||
return -ST_MOUNT_ERROR;
|
||||
} break;
|
||||
}
|
||||
|
||||
bool ok = mountpoint->driver_ops.mount (mountpoint);
|
||||
int ret = mountpoint->driver_ops.mount (mountpoint, op_ctx);
|
||||
|
||||
if (!ok) {
|
||||
if (ret < 0) {
|
||||
free (mountpoint);
|
||||
return NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t mp_hash = hash_fnv32 (mountpoint->key, strlen (mountpoint->key));
|
||||
@@ -73,7 +76,7 @@ struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type) {
|
||||
|
||||
spin_unlock (&mount_table.lock);
|
||||
|
||||
return mountpoint;
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int vfs_open (struct procgroup* procgroup, const char* mountpoint, const char* path) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef _KERNEL_FS_VFS_H
|
||||
#define _KERNEL_FS_VFS_H
|
||||
|
||||
#include <device/device.h>
|
||||
#include <libk/hash.h>
|
||||
#include <libk/list.h>
|
||||
#include <libk/std.h>
|
||||
@@ -8,7 +9,7 @@
|
||||
#include <proc/procgroup.h>
|
||||
#include <sync/spin_lock.h>
|
||||
|
||||
#define VFS_RAMDISKFS 0
|
||||
#define VFS_TARFS 0
|
||||
|
||||
struct vfs_mountpoint {
|
||||
char key[0x100];
|
||||
@@ -18,12 +19,14 @@ struct vfs_mountpoint {
|
||||
bool locked;
|
||||
struct procgroup* ownerpg;
|
||||
struct {
|
||||
bool (*mount) (struct vfs_mountpoint* mountpoint);
|
||||
int (*mount) (struct vfs_mountpoint* mountpoint, struct device_op_ctx* op_ctx);
|
||||
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 device* back_device;
|
||||
void* udata
|
||||
};
|
||||
|
||||
struct vfs_mount_table {
|
||||
@@ -31,14 +34,21 @@ struct vfs_mount_table {
|
||||
spin_lock_t lock;
|
||||
};
|
||||
|
||||
struct vfs_mountpoint* vfs_create_mountpoint (const char* key, int fs_type);
|
||||
int vfs_create_mountpoint (const char* key, int fs_type, struct device* back_device,
|
||||
struct device_op_ctx* op_ctx);
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user