Break ioctl() up into smaller syscalls

This commit is contained in:
2025-10-14 20:05:41 +02:00
parent c34a253d11
commit 406434fed0
21 changed files with 375 additions and 320 deletions

View File

@ -72,7 +72,7 @@ int32_t littlefs_vobj_write(struct VfsObj *vobj, const uint8_t *const buffer, si
return E_OK;
}
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, IoctlStat *statbuf) {
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, FsStat *statbuf) {
struct lfs_info info;
spinlock_acquire(&vmp->spinlock);
@ -84,10 +84,10 @@ int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, IoctlStat *st
}
if (info.type == LFS_TYPE_REG) {
statbuf->type = IOCTLSTAT_FILE;
statbuf->type = FSSTAT_FILE;
statbuf->size = info.size;
} else if (info.type == LFS_TYPE_DIR) {
statbuf->type = IOCTLSTAT_DIR;
statbuf->type = FSSTAT_DIR;
statbuf->size = 0;
// TODO: find a better way than this... !!!
lfs_dir_t dir;
@ -170,7 +170,7 @@ struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32
return vobj;
}
int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, IoctlDirent *direntbuf, size_t idx) {
int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, FsDirent *direntbuf, size_t idx) {
size_t i = 0;
struct lfs_info statinfo;
int ok;
@ -199,10 +199,10 @@ int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, IoctlD
while (lfs_dir_read(&vmp->fs.littlefs.instance, &dir, &entinfo) > 0) {
if (i == idx) {
if (entinfo.type == LFS_TYPE_REG) {
direntbuf->stat.type = IOCTLSTAT_FILE;
direntbuf->stat.type = FSSTAT_FILE;
direntbuf->stat.size = entinfo.size;
} else if (entinfo.type == LFS_TYPE_DIR) {
direntbuf->stat.type = IOCTLSTAT_DIR;
direntbuf->stat.type = FSSTAT_DIR;
}
hal_memcpy(direntbuf->name, entinfo.name, sizeof(direntbuf->name));

View File

@ -4,7 +4,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "fs/littlefs/lfs.h"
#include "sysdefs/ioctl.h"
#include "sysdefs/fs.h"
#define LITTLEFS_BLOCK_SIZE 512
@ -17,8 +17,8 @@ typedef struct {
int32_t littlefs_cleanup(struct VfsMountPoint *vmp);
struct VfsObj *littlefs_open(struct VfsMountPoint *vmp, const char *path, uint32_t flags);
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, IoctlStat *statbuf);
int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, IoctlDirent *direntbuf, size_t idx);
int32_t littlefs_stat(struct VfsMountPoint *vmp, const char *path, FsStat *statbuf);
int32_t littlefs_fetchdirent(struct VfsMountPoint *vmp, const char *path, FsDirent *direntbuf, size_t idx);
int32_t littlefs_mkdir(struct VfsMountPoint *vmp, const char *path);
int portlfs_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);

View File

@ -14,7 +14,7 @@
#include "bootinfo/bootinfo.h"
#include "ipc/pipe/pipe.h"
#include "sysdefs/proc.h"
#include "sysdefs/ioctl.h"
#include "sysdefs/fs.h"
#define PROC_REAPER_FREQ 30
@ -75,12 +75,12 @@ ElfAuxval proc_load_elf_segs(Proc *proc, uint8_t *data) {
}
Proc *proc_spawnuser(char *mountpoint, char *path) {
IoctlStat stat;
FsStat stat;
if (vfs_stat(mountpoint, path, &stat) != E_OK) {
return NULL;
}
if (stat.type != IOCTLSTAT_FILE) {
if (stat.type != FSSTAT_FILE) {
return NULL;
}

228
kernel/syscall/fs.c Normal file
View File

@ -0,0 +1,228 @@
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
#include "sysdefs/fs.h"
#include "errors.h"
#include "path/path.h"
#include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#define _MP_MAX 0xff
#define _PATH_MAX VFS_PATH_MAX
int32_t SYSCALL2(sys_fs_openf, opath1, oflags1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
uint64_t oflags = oflags1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
VfsObj *vobj = vfs_open(mp, path, oflags);
if (vobj == NULL) {
ret = E_NOENTRY;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
if (proc->vobjcnt < PROC_VFSHANDLES_MAX) {
for (size_t i = 0; i < PROC_VFSHANDLES_MAX; i++) {
if (proc->vobjs[i] == NULL) {
proc->vobjs[i] = vobj;
ret = i;
break;
}
}
} else {
ret = E_NOMEMORY;
}
done:
return ret;
}
int32_t SYSCALL1(sys_fs_closef, fsh1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
vfs_close(vobj);
proc->vobjs[fsh] = NULL;
ret = E_OK;
done:
return ret;
}
int32_t SYSCALL4(sys_fs_write, fsh1, buffer1, len1, off1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
const uint8_t *const buffer = (const uint8_t *const)buffer1;
size_t len = (size_t)len1;
size_t off = (size_t)off1;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->write(vobj, buffer, len, off);
done:
return ret;
}
int32_t SYSCALL4(sys_fs_read, fsh1, buffer1, len1, off1) {
uint64_t fsh = fsh1;
int32_t ret = E_BADIO;
uint8_t *const buffer = (uint8_t *const)buffer1;
size_t len = (size_t)len1;
size_t off = (size_t)off1;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (fsh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[fsh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->read(vobj, buffer, len, off);
done:
return ret;
}
int32_t SYSCALL2(sys_fs_stat, opath1, fsstat1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
FsStat *fsstat = (FsStat *)fsstat1;
if (fsstat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_stat(mp, path, fsstat);
done:
return ret;
}
int32_t SYSCALL3(sys_fs_fetchdirent, opath1, direntbuf1, idx1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
FsDirent *direntbuf = (FsDirent *)direntbuf1;
if (direntbuf == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)idx1;
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_fetchdirent(mp, path, direntbuf, idx);
done:
return ret;
}
int32_t SYSCALL1(sys_fs_mkdir, opath1) {
int32_t ret = E_BADIO;
const char *opath = (const char *)opath1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[_MP_MAX];
char path[_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_mkdir(mp, path);
done:
return ret;
}

16
kernel/syscall/fs.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef SYSCALL_FS_H_
#define SYSCALL_FS_H_
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
int32_t SYSCALL2(sys_fs_openf, opath1, oflags1);
int32_t SYSCALL1(sys_fs_closef, fsh1);
int32_t SYSCALL4(sys_fs_write, fsh1, buffer1, len1, off1);
int32_t SYSCALL4(sys_fs_read, fsh1, buffer1, len1, off1);
int32_t SYSCALL2(sys_fs_stat, opath1, fsstat1);
int32_t SYSCALL3(sys_fs_fetchdirent, opath1, direntbuf1, idx1);
int32_t SYSCALL1(sys_fs_mkdir, opath1);
#endif // SYSCALL_FS_H_

View File

@ -1,205 +0,0 @@
#include <stdint.h>
#include <stddef.h>
#include "syscall.h"
#include "sysdefs/ioctl.h"
#include "errors.h"
#include "path/path.h"
#include "vfs/vfs.h"
#include "kprintf.h"
#include "proc/proc.h"
#include "spinlock/spinlock.h"
#define IOCTL_MP_MAX 0xff
#define IOCTL_PATH_MAX VFS_PATH_MAX
int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3) {
uint64_t ioh = ioh1;
uint64_t cmd = cmd1;
int32_t ret = E_BADIO;
switch (cmd) {
case IOCTL_OPENF: {
const char *opath = (const char *)arg1;
uint64_t oflags = arg2;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
VfsObj *vobj = vfs_open(mp, path, oflags);
if (vobj == NULL) {
ret = E_NOENTRY;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
if (proc->vobjcnt < PROC_VFSHANDLES_MAX) {
proc->vobjs[proc->vobjcnt++] = vobj;
ret = proc->vobjcnt - 1;
} else {
ret = E_NOMEMORY;
}
} break;
case IOCTL_CLOSEF: {
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t i = 0;
for (; i < PROC_VFSHANDLES_MAX; i++) {
if (proc->vobjs[i] == vobj) {
break;
}
}
vfs_close(vobj);
proc->vobjs[i] = NULL;
ret = E_OK;
} break;
case IOCTL_WRITE: {
const uint8_t *const buffer = (const uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->write(vobj, buffer, len, off);
} break;
case IOCTL_READ: {
uint8_t *const buffer = (uint8_t *const)arg1;
size_t len = (size_t)arg2;
size_t off = (size_t)arg3;
if (buffer == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
if (ioh >= PROC_VFSHANDLES_MAX) {
ret = E_INVALIDARGUMENT;
goto done;
}
spinlock_acquire(&PROCS.spinlock);
Proc *proc = PROCS.current;
spinlock_release(&PROCS.spinlock);
VfsObj *vobj = proc->vobjs[ioh];
if (vobj == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
ret = vobj->read(vobj, buffer, len, off);
} break;
case IOCTL_STAT: {
const char *opath = (const char *)arg2;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
IoctlStat *iostat = (IoctlStat *)arg1;
if (iostat == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_stat(mp, path, iostat);
} break;
case IOCTL_FETCHDIRENT: {
const char *opath = (const char *)arg1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
IoctlDirent *direntbuf = (IoctlDirent *)arg2;
if (direntbuf == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)arg3;
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_fetchdirent(mp, path, direntbuf, idx);
} break;
case IOCTL_MKDIR: {
const char *opath = (const char *)arg1;
if (opath == NULL) {
ret = E_INVALIDARGUMENT;
goto done;
}
size_t idx = (size_t)arg3;
char mp[IOCTL_MP_MAX];
char path[IOCTL_PATH_MAX];
path_parse(opath, mp, path);
ret = vfs_mkdir(mp, path);
} break;
default: {
ret = E_INVALIDARGUMENT;
goto done;
} break;
}
done:
return ret;
}

View File

@ -1,9 +0,0 @@
#ifndef SYSCALL_IOCTL_H_
#define SYSCALL_IOCTL_H_
#include <stdint.h>
#include "syscall.h"
int32_t SYSCALL5(sys_ioctl, ioh1, cmd1, arg1, arg2, arg3);
#endif // SYSCALL_IOCTL_H_

View File

@ -3,7 +3,6 @@
#include "errors.h"
#include "kprintf.h"
#include "sysdefs/syscall.h"
#include "ioctl.h"
#include "ipcpipe.h"
#include "mman.h"
#include "sched.h"
@ -11,6 +10,7 @@
#include "randcrypto.h"
#include "vfs.h"
#include "proc.h"
#include "fs.h"
int32_t SYSCALL1(sys_debugprint, string) {
char *p = (char *)string;
@ -20,7 +20,6 @@ int32_t SYSCALL1(sys_debugprint, string) {
SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = {
[SYS_DEBUGPRINT] = &sys_debugprint,
[SYS_IOCTL] = &sys_ioctl,
[SYS_MMAN_MAP] = &sys_mman_map,
[SYS_MMAN_UNMAP] = &sys_mman_unmap,
[SYS_SCHEDRELEASE] = &sys_schedrelease,
@ -42,4 +41,11 @@ SyscallFn SYSCALL_TABLE[SYSCALLS_MAX] = {
[SYS_PROC_ARGV] = &sys_proc_argv,
[SYS_PROC_LISTSIZE] = &sys_proc_listsize,
[SYS_PROC_STAT] = &sys_proc_stat,
[SYS_FS_OPENF] = &sys_fs_openf,
[SYS_FS_CLOSEF] = &sys_fs_closef,
[SYS_FS_READ] = &sys_fs_read,
[SYS_FS_WRITE] = &sys_fs_write,
[SYS_FS_STAT] = &sys_fs_stat,
[SYS_FS_FETCHDIRENT] = &sys_fs_fetchdirent,
[SYS_FS_MKDIR] = &sys_fs_mkdir,
};

View File

@ -51,7 +51,7 @@ void vfs_init_littlefs(VfsMountPoint *mp, bool format) {
mp->mkdir = &littlefs_mkdir;
}
int32_t vfs_stat(char *mountpoint, const char *path, IoctlStat *stat) {
int32_t vfs_stat(char *mountpoint, const char *path, FsStat *stat) {
VfsMountPoint *mp = NULL;
spinlock_acquire(&VFS_TABLE.spinlock);
@ -79,7 +79,7 @@ int32_t vfs_mkdir(char *mountpoint, const char *path) {
return mp->mkdir(mp, path);
}
int32_t vfs_fetchdirent(char *mountpoint, const char *path, IoctlDirent *direntbuf, size_t idx) {
int32_t vfs_fetchdirent(char *mountpoint, const char *path, FsDirent *direntbuf, size_t idx) {
VfsMountPoint *mp = NULL;
spinlock_acquire(&VFS_TABLE.spinlock);

View File

@ -7,7 +7,7 @@
#include "spinlock/spinlock.h"
#include "fs/portlfs/portlfs.h"
#include "storedev/storedev.h"
#include "sysdefs/ioctl.h"
#include "sysdefs/fs.h"
#include "compiler/attr.h"
#define VFS_MOUNTPOINT_LABEL_MAX 128
@ -50,8 +50,8 @@ typedef struct VfsMountPoint {
VfsObj *(*open)(struct VfsMountPoint *vmp, const char *path, uint32_t flags);
int32_t (*cleanup)(struct VfsMountPoint *vmp);
int32_t (*stat)(struct VfsMountPoint *vmp, const char *path, IoctlStat *statbuf);
int32_t (*fetchdirent)(struct VfsMountPoint *vmp, const char *path, IoctlDirent *direntbuf, size_t idx);
int32_t (*stat)(struct VfsMountPoint *vmp, const char *path, FsStat *statbuf);
int32_t (*fetchdirent)(struct VfsMountPoint *vmp, const char *path, FsDirent *direntbuf, size_t idx);
int32_t (*mkdir)(struct VfsMountPoint *vmp, const char *path);
union {
@ -72,8 +72,8 @@ int32_t vfs_unmount(char *mountpoint);
int32_t vfs_mount(char *mountpoint, int32_t fstype, StoreDev *backingsd, bool format);
void vfs_close(VfsObj *vobj);
VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags);
int32_t vfs_stat(char *mountpoint, const char *path, IoctlStat *stat);
int32_t vfs_fetchdirent(char *mountpoint, const char *path, IoctlDirent *direntbuf, size_t idx);
int32_t vfs_stat(char *mountpoint, const char *path, FsStat *stat);
int32_t vfs_fetchdirent(char *mountpoint, const char *path, FsDirent *direntbuf, size_t idx);
int32_t vfs_mkdir(char *mountpoint, const char *path);
#endif // VFS_VFS_H_