fat_io_lib finally works, implement virtual partition devices, manage devices via string keys
All checks were successful
Build documentation / build-and-deploy (push) Successful in 3m35s

This commit is contained in:
2026-03-01 00:00:27 +01:00
parent baa13fb695
commit 0533c2705d
37 changed files with 619 additions and 139 deletions

View File

@@ -1,4 +1,5 @@
#include <desc.h>
#include <devices.h>
#include <fs/fat1.h>
#include <fs/fatfs.h>
#include <fs/fatfs_ctx.h>
@@ -13,13 +14,15 @@
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
uint32_t sector_count) {
struct vfs_volume* volume = ctx->udata;
struct device* back_device = volume->back_device;
if (sector_count == 0)
return 0;
size_t sector_size;
size_t phys_sector, phys_sector_count;
int ret;
@@ -29,7 +32,7 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
return 0;
}
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
@@ -38,33 +41,97 @@ static int fat1_diskio_read (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* bu
ret = device_op (back_device, XDRV_READ, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return -1;
return 0;
}
spin_unlock (&back_device->lock);
if (ret < 0)
return -1;
return 0;
return 0;
return 1;
}
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
(void)proc, (void)rctx;
static int fat1_diskio_write (struct fatfs_ctx* ctx, uint32_t sector, uint8_t* buffer,
uint32_t sector_count) {
struct vfs_volume* volume = ctx->udata;
struct device* back_device = volume->back_device;
if (sector_count == 0)
return 0;
size_t sector_size;
size_t phys_sector, phys_sector_count;
int ret;
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
}
vfs_translate (sector, sector_count, FAT_SECTOR_SIZE, sector_size, &phys_sector,
&phys_sector_count);
ret = device_op (back_device, XDRV_WRITE, NULL, NULL, &phys_sector, &phys_sector_count, buffer);
if (ret < 0) {
spin_unlock (&back_device->lock);
return 0;
}
spin_unlock (&back_device->lock);
if (ret < 0)
return 0;
return 1;
}
int fatfs_mount (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = malloc (sizeof (*fatfs_ctx));
int r;
if (fatfs_ctx == NULL)
return -ST_OOM_ERROR;
memset (fatfs_ctx, 0, sizeof (*fatfs_ctx));
fl_attach_media (fatfs_ctx, &fat1_diskio_read, NULL);
fatfs_ctx->udata = volume;
volume->udata = fatfs_ctx;
fl_init (fatfs_ctx);
fl_attach_media (fatfs_ctx, &fat1_diskio_read, &fat1_diskio_write);
return ST_OK;
}
int fatfs16_format (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
int r = fatfs_format_fat16 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat16");
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs32_format (struct vfs_volume* volume) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
struct device* back_device = volume->back_device;
size_t total_size;
spin_lock (&back_device->lock);
device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
spin_unlock (&back_device->lock);
size_t sectors = div_align_up (total_size, FAT_SECTOR_SIZE);
int r = fatfs_format_fat32 (fatfs_ctx, &fatfs_ctx->_fs, sectors, "mop3 fat32");
return r < 0 ? -ST_FORMAT_ERROR : ST_OK;
}
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
@@ -114,6 +181,23 @@ int fatfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, si
return ST_OK;
}
int fatfs_write (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
struct fatfs_ctx* fatfs_ctx = volume->udata;
FL_FILE* file = fl_fopen (fatfs_ctx, path, "wb+");
if (file == NULL)
return -ST_NOT_FOUND;
fl_fseek (fatfs_ctx, file, off, SEEK_SET);
fl_fwrite (fatfs_ctx, buffer, 1, size, file);
fl_fclose (fatfs_ctx, file);
return ST_OK;
}
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num) {
struct fatfs_ctx* fatfs_ctx = volume->udata;

View File

@@ -10,13 +10,20 @@
struct vfs_volume;
int fatfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int fatfs_mount (struct vfs_volume* volume);
int fatfs16_format (struct vfs_volume* volume);
int fatfs32_format (struct vfs_volume* volume);
int fatfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc);
int fatfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int fatfs_write (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int fatfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num);

View File

@@ -1,4 +1,5 @@
#include <desc.h>
#include <devices.h>
#include <fs/path.h>
#include <fs/tarfs.h>
#include <fs/vfs.h>
@@ -12,7 +13,6 @@
#include <path_defs.h>
#include <status.h>
#include <sys/debug.h>
#include <xdrv_device.h>
static struct tar_file* tar_get_file (struct tarfs* tarfs, const char* filename) {
for (size_t i = 0; i < TARFS_FILES_MAX; i++) {
@@ -55,7 +55,7 @@ static size_t tar_parse (struct tarfs* tarfs, uint8_t* addr) {
return i;
}
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx) {
int tarfs_mount (struct vfs_volume* volume) {
struct tarfs* tarfs = malloc (sizeof (*tarfs));
if (tarfs == NULL)
@@ -71,14 +71,14 @@ int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule
spin_lock (&back_device->lock);
ret = device_op (back_device, XDRV_GET_SIZE, proc, rctx, &total_size);
ret = device_op (back_device, XDRV_GET_SIZE, NULL, NULL, &total_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
free (volume->udata);
return ret;
}
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, proc, rctx, &sector_size);
ret = device_op (back_device, XDRV_GET_SECTOR_SIZE, NULL, NULL, &sector_size);
if (ret < 0) {
spin_unlock (&back_device->lock);
free (volume->udata);
@@ -96,7 +96,7 @@ int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule
size_t sector_count = 1;
for (size_t sector = 0; sector < total_size / sector_size; sector++) {
uint8_t* dest = (uint8_t*)((uintptr_t)buffer + (sector * sector_size));
ret = device_op (back_device, XDRV_READ, proc, rctx, &sector, &sector_count, dest);
ret = device_op (back_device, XDRV_READ, NULL, NULL, &sector, &sector_count, dest);
}
spin_unlock (&back_device->lock);
@@ -113,6 +113,11 @@ int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule
return ret;
}
int tarfs_format (struct vfs_volume* volume) {
(void)volume;
return ST_OK;
}
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc) {
struct tarfs* tarfs = volume->udata;
@@ -190,3 +195,9 @@ int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct di
return ST_DIR_NO_ENTRIES;
}
int tarfs_write (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size) {
(void)volume, (void)path, (void)buffer, (void)off, (void)size;
return ST_OK;
}

View File

@@ -34,13 +34,18 @@ struct tarfs {
struct vfs_volume;
int tarfs_mount (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int tarfs_mount (struct vfs_volume* volume);
int tarfs_format (struct vfs_volume* volume);
int tarfs_describe (struct vfs_volume* volume, const char* path, struct desc* desc);
int tarfs_read (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int tarfs_write (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int tarfs_read_dir_entry (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num);

View File

@@ -22,8 +22,8 @@ static struct vfs_volume_table volume_table;
static struct vfs_volume* vfs_find_volume (const char* volume) {
struct hash_node_link* found_link = NULL;
size_t volume_len = strlen (volume);
uint32_t hash = hash_fnv32 (volume, strlen (volume));
size_t volume_len = strlen_null (volume);
uint32_t hash = hash_fnv32 (volume, volume_len);
spin_lock (&volume_table.lock);
hash_find (&volume_table, volume, volume_len, hash, lengthof (volume_table.volume_buckets),
@@ -36,8 +36,7 @@ static struct vfs_volume* vfs_find_volume (const char* volume) {
return hash_entry (found_link, struct vfs_volume, volume_table_link);
}
int vfs_create_volume (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) {
if (strlen_null (key) > VOLUME_MAX)
return -ST_OOB_ERROR;
@@ -56,14 +55,26 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device,
switch (volume->fs_type) {
case VFS_TARFS:
volume->driver_ops.mount = &tarfs_mount;
volume->driver_ops.format = &tarfs_format;
volume->driver_ops.describe = &tarfs_describe;
volume->driver_ops.read = &tarfs_read;
volume->driver_ops.write = &tarfs_write;
volume->driver_ops.read_dir_entry = &tarfs_read_dir_entry;
break;
case VFS_FAT16:
volume->driver_ops.mount = &fatfs_mount;
volume->driver_ops.format = &fatfs16_format;
volume->driver_ops.describe = &fatfs_describe;
volume->driver_ops.read = &fatfs_read;
volume->driver_ops.write = &fatfs_write;
volume->driver_ops.read_dir_entry = &fatfs_read_dir_entry;
break;
case VFS_FAT32:
volume->driver_ops.mount = &fatfs_mount;
volume->driver_ops.format = &fatfs32_format;
volume->driver_ops.describe = &fatfs_describe;
volume->driver_ops.read = &fatfs_read;
volume->driver_ops.write = &fatfs_write;
volume->driver_ops.read_dir_entry = &fatfs_read_dir_entry;
break;
default:
@@ -71,14 +82,14 @@ int vfs_create_volume (const char* key, int fs_type, struct device* back_device,
return -ST_MOUNT_ERROR;
}
int ret = volume->driver_ops.mount (volume, proc, rctx);
int ret = volume->driver_ops.mount (volume);
if (ret < 0) {
free (volume);
return ret;
}
uint32_t mp_hash = hash_fnv32 (volume->key, strlen (volume->key));
uint32_t mp_hash = hash_fnv32 (volume->key, strlen_null (volume->key));
spin_lock (&volume_table.lock);
@@ -135,7 +146,7 @@ int vfs_volume_close (struct proc* proc, const char* volume_name, struct resched
struct proc_sq_entry* sq_entry = list_entry (node, struct proc_sq_entry, sq_link);
struct proc* resumed_proc = sq_entry->proc;
volume->owner = proc;
volume->owner = resumed_proc;
volume->locked = true;
spin_unlock (&volume->sq.lock);
@@ -154,6 +165,24 @@ int vfs_volume_close (struct proc* proc, const char* volume_name, struct resched
return ST_OK;
}
int vfs_format (struct proc* proc, const char* volume_name) {
struct vfs_volume* volume = vfs_find_volume (volume_name);
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 (&volume->lock);
return volume->driver_ops.format (volume);
}
int vfs_read (struct proc* proc, const char* volume_name, const char* path, uint8_t* buffer,
size_t off, size_t size) {
struct vfs_volume* volume = vfs_find_volume (volume_name);

View File

@@ -32,13 +32,18 @@ struct vfs_volume {
bool locked;
struct proc_suspension_q sq;
struct {
int (*mount) (struct vfs_volume* volume, struct proc* proc, struct reschedule_ctx* rctx);
int (*mount) (struct vfs_volume* volume);
int (*format) (struct vfs_volume* volume);
int (*describe) (struct vfs_volume* volume, const char* path, struct desc* desc);
int (*read) (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int (*write) (struct vfs_volume* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);
int (*read_dir_entry) (struct vfs_volume* volume, const char* path, struct dir_entry* entry,
size_t entry_num);
} driver_ops;
@@ -51,13 +56,14 @@ struct vfs_volume_table {
spin_lock_t lock;
};
int vfs_create_volume (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);
int vfs_volume_open (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_volume_close (struct proc* proc, const char* volume, struct reschedule_ctx* rctx);
int vfs_format (struct proc* proc, const char* volume_name);
int vfs_read (struct proc* proc, const char* volume, const char* path, uint8_t* buffer, size_t off,
size_t size);