Break ioctl() up into smaller syscalls
This commit is contained in:
@ -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));
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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
228
kernel/syscall/fs.c
Normal 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
16
kernel/syscall/fs.h
Normal 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_
|
||||
@ -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;
|
||||
}
|
||||
@ -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_
|
||||
@ -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,
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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_
|
||||
|
||||
24
share/sysdefs/fs.h
Normal file
24
share/sysdefs/fs.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef SHARE_SYSDEFS_FS_H_
|
||||
#define SHARE_SYSDEFS_FS_H_
|
||||
|
||||
#define FS_OF_READ (1<<0)
|
||||
#define FS_OF_WRITE (1<<1)
|
||||
#define FS_OF_MAKE (1<<2)
|
||||
|
||||
#define FSSTAT_DIR 0
|
||||
#define FSSTAT_FILE 1
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
size_t size;
|
||||
int32_t type;
|
||||
} FsStat;
|
||||
|
||||
typedef struct {
|
||||
FsStat stat;
|
||||
char name[0x100];
|
||||
} FsDirent;
|
||||
|
||||
#endif // SHARE_SYSDEFS_FS_H_
|
||||
@ -1,40 +0,0 @@
|
||||
#ifndef SHARE_SYSDEFS_IOCTL_H_
|
||||
#define SHARE_SYSDEFS_IOCTL_H_
|
||||
|
||||
#define IOCTL_NOHANDLE (-1)
|
||||
|
||||
#define IOCTL_OPENF 0
|
||||
#define IOCTL_CLOSEF 1
|
||||
#define IOCTL_READ 2
|
||||
#define IOCTL_STAT 3
|
||||
#define IOCTL_WRITE 4
|
||||
#define IOCTL_FETCHDIRENT 5
|
||||
#define IOCTL_MKDIR 6
|
||||
|
||||
#define IOCTL_F_READ (1<<0)
|
||||
#define IOCTL_F_WRITE (1<<1)
|
||||
#define IOCTL_F_MAKE (1<<2)
|
||||
|
||||
#define IOCTLSTAT_DIR 0
|
||||
#define IOCTLSTAT_FILE 1
|
||||
|
||||
#if !defined(__ASSEMBLER__)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct IoctlStat {
|
||||
size_t size;
|
||||
int32_t type;
|
||||
} IoctlStat;
|
||||
|
||||
typedef struct IoctlDirent {
|
||||
IoctlStat stat;
|
||||
char name[0x100];
|
||||
} IoctlDirent;
|
||||
|
||||
typedef int32_t IOH;
|
||||
|
||||
#endif
|
||||
|
||||
#endif // SHARE_SYSDEFS_IOCTL_H_
|
||||
@ -24,6 +24,13 @@
|
||||
#define SYS_PROC_ARGV 23
|
||||
#define SYS_PROC_LISTSIZE 24
|
||||
#define SYS_PROC_STAT 25
|
||||
#define SYS_FS_OPENF 26
|
||||
#define SYS_FS_CLOSEF 27
|
||||
#define SYS_FS_READ 28
|
||||
#define SYS_FS_STAT 29
|
||||
#define SYS_FS_WRITE 30
|
||||
#define SYS_FS_FETCHDIRENT 31
|
||||
#define SYS_FS_MKDIR 32
|
||||
|
||||
|
||||
#endif // SHARE_HDRS_SYSCALL_H_
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
#include <system/system.h>
|
||||
#include <syscall/syscall.h>
|
||||
#include <sysdefs/syscall.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/fs.h>
|
||||
#include <sysdefs/proc.h>
|
||||
#include <sysdefs/devctl.h>
|
||||
#include <uprintf.h>
|
||||
@ -11,10 +11,6 @@ void debugprint(const char *string) {
|
||||
syscall(SYS_DEBUGPRINT, (uint64_t)string, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
|
||||
return syscall(SYS_IOCTL, ioh, cmd, arg1, arg2, arg3, 0);
|
||||
}
|
||||
|
||||
int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out) {
|
||||
return syscall(SYS_MMAN_MAP, (uint64_t)addr, (uint64_t)size, prot, flags, (uint64_t)out, 0);
|
||||
}
|
||||
@ -99,3 +95,30 @@ int32_t proc_stat(size_t idx, ProcStat *pstat) {
|
||||
return syscall(SYS_PROC_STAT, (uint64_t)idx, (uint64_t)pstat, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_openf(char *path, uint64_t flags) {
|
||||
return syscall(SYS_FS_OPENF, (uint64_t)path, flags, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_closef(int32_t fsh) {
|
||||
return syscall(SYS_FS_CLOSEF, (uint64_t)fsh, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_write(int32_t fsh, const uint8_t *buffer, size_t len, size_t off) {
|
||||
return syscall(SYS_FS_WRITE, (uint64_t)fsh, (uint64_t)buffer, (uint64_t)len, (uint64_t)off, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_read(int32_t fsh, uint8_t *const buffer, size_t len, size_t off) {
|
||||
return syscall(SYS_FS_READ, (uint64_t)fsh, (uint64_t)buffer, (uint64_t)len, (uint64_t)off, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_stat(char *path, FsStat *statbuf) {
|
||||
return syscall(SYS_FS_STAT, (uint64_t)path, (uint64_t)statbuf, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_fetchdirent(char *path, FsDirent *direntbuf, size_t idx) {
|
||||
return syscall(SYS_FS_FETCHDIRENT, (uint64_t)path, (uint64_t)direntbuf, (uint64_t)idx, 0, 0, 0);
|
||||
}
|
||||
|
||||
int32_t fs_mkdir(char *path) {
|
||||
return syscall(SYS_FS_MKDIR, (uint64_t)path, 0, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
@ -6,9 +6,9 @@
|
||||
#include <stdbool.h>
|
||||
#include <sysdefs/devctl.h>
|
||||
#include <sysdefs/proc.h>
|
||||
#include <sysdefs/fs.h>
|
||||
|
||||
void debugprint(const char *string);
|
||||
int32_t ioctl(uint64_t ioh, uint64_t cmd, uint64_t arg1, uint64_t arg2, uint64_t arg3);
|
||||
int32_t mman_map(uint8_t *addr, size_t size, uint64_t prot, uint64_t flags, uint8_t **out);
|
||||
int32_t mman_unmap(uint8_t *addr);
|
||||
int32_t schedrelease(void);
|
||||
@ -30,5 +30,12 @@ int32_t proc_arglen(PID_t pid);
|
||||
int32_t proc_argv(PID_t pid, size_t *argslen1, char **argbuf1, size_t maxargs);
|
||||
int32_t proc_listsize(void);
|
||||
int32_t proc_stat(size_t idx, ProcStat *pstat);
|
||||
int32_t fs_openf(char *path, uint64_t flags);
|
||||
int32_t fs_closef(int32_t fsh);
|
||||
int32_t fs_write(int32_t fsh, const uint8_t *buffer, size_t len, size_t off);
|
||||
int32_t fs_read(int32_t fsh, uint8_t *const buffer, size_t len, size_t off);
|
||||
int32_t fs_stat(char *path, FsStat *statbuf);
|
||||
int32_t fs_fetchdirent(char *path, FsDirent *direntbuf, size_t idx);
|
||||
int32_t fs_mkdir(char *path);
|
||||
|
||||
#endif // ULIB_SYSTEM_SYSTEM_H_
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
#include <dlinklist.h>
|
||||
|
||||
#include <errors.h>
|
||||
#include <sysdefs/ioctl.h>
|
||||
#include <sysdefs/fs.h>
|
||||
#include <sysdefs/mman.h>
|
||||
#include <sysdefs/proc.h>
|
||||
#include <sysdefs/sched.h>
|
||||
|
||||
@ -44,15 +44,15 @@ void fs_fetch(void) {
|
||||
FS_FETCH_CONFIG.types = false;
|
||||
}
|
||||
|
||||
IoctlStat statbuf; ZERO(&statbuf);
|
||||
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)path, 0) != E_OK) {
|
||||
FsStat statbuf; ZERO(&statbuf);
|
||||
if (fs_stat(path, &statbuf) != E_OK) {
|
||||
uprintf("fs: could not stat %s\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
if (statbuf.type == IOCTLSTAT_FILE) {
|
||||
IOH ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)path, IOCTL_F_READ, 0);
|
||||
if (ioh < 0) {
|
||||
if (statbuf.type == FSSTAT_FILE) {
|
||||
int32_t h = fs_openf(path, FS_OF_READ);
|
||||
if (h < 0) {
|
||||
uprintf("fs: could not open %s\n", path);
|
||||
return;
|
||||
}
|
||||
@ -60,7 +60,7 @@ void fs_fetch(void) {
|
||||
uint8_t *buf = umalloc(statbuf.size+1);
|
||||
string_memset(buf, 0, statbuf.size+1);
|
||||
|
||||
if (ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0) < 0) {
|
||||
if (fs_read(h, buf, statbuf.size, 0)) {
|
||||
uprintf("fs: coult not read %s\n", path);
|
||||
goto donefile;
|
||||
return;
|
||||
@ -69,14 +69,14 @@ void fs_fetch(void) {
|
||||
uprintf("%s", buf);
|
||||
donefile:
|
||||
ufree(buf);
|
||||
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
||||
} else if (statbuf.type == IOCTLSTAT_DIR) {
|
||||
IoctlDirent *dirents = (IoctlDirent *)umalloc(statbuf.size * sizeof(*dirents));
|
||||
fs_closef(h);
|
||||
} else if (statbuf.type == FSSTAT_DIR) {
|
||||
FsDirent *dirents = (FsDirent *)umalloc(statbuf.size * sizeof(*dirents));
|
||||
|
||||
for (size_t i = 0; i < statbuf.size; i++) {
|
||||
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)path, (uint64_t)&dirents[i], i);
|
||||
fs_fetchdirent(path, &dirents[i], i);
|
||||
|
||||
IoctlDirent *dirent = &dirents[i];
|
||||
FsDirent *dirent = &dirents[i];
|
||||
|
||||
if (FS_FETCH_CONFIG.names) {
|
||||
if (FS_FETCH_CONFIG.plain) {
|
||||
@ -87,7 +87,7 @@ void fs_fetch(void) {
|
||||
}
|
||||
|
||||
if (FS_FETCH_CONFIG.sizes) {
|
||||
if (dirent->stat.type == IOCTLSTAT_FILE) {
|
||||
if (dirent->stat.type == FSSTAT_FILE) {
|
||||
if (FS_FETCH_CONFIG.humsz) {
|
||||
char *membuf = umalloc(20);
|
||||
|
||||
@ -105,7 +105,7 @@ void fs_fetch(void) {
|
||||
uprintf("%-15zu ", dirent->stat.size);
|
||||
}
|
||||
}
|
||||
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
|
||||
} else if (dirent->stat.type == FSSTAT_DIR) {
|
||||
if (FS_FETCH_CONFIG.plain) {
|
||||
uprintf("- ");
|
||||
} else {
|
||||
@ -115,9 +115,9 @@ void fs_fetch(void) {
|
||||
}
|
||||
|
||||
if (FS_FETCH_CONFIG.types) {
|
||||
if (dirent->stat.type == IOCTLSTAT_FILE) {
|
||||
if (dirent->stat.type == FSSTAT_FILE) {
|
||||
uprintf("%c ", 'F');
|
||||
} else if (dirent->stat.type == IOCTLSTAT_DIR) {
|
||||
} else if (dirent->stat.type == FSSTAT_DIR) {
|
||||
uprintf("%c ", 'D');
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@ void fs_mkd(void) {
|
||||
|
||||
char *path = *(args()+1);
|
||||
|
||||
int32_t r = ioctl(IOCTL_NOHANDLE, IOCTL_MKDIR, (uint64_t)path, 0, 0);
|
||||
int32_t r = fs_mkdir(path);
|
||||
if (r != E_OK) {
|
||||
uprintf("fs: could not create %s\n", path);
|
||||
}
|
||||
|
||||
@ -26,15 +26,15 @@ void fs_mkf(void) {
|
||||
uprintf("fs mkf: Could not parse args: %d\n", ret);
|
||||
}
|
||||
|
||||
IOH ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)path, IOCTL_F_MAKE | IOCTL_F_WRITE, 0);
|
||||
if (ioh < 0) {
|
||||
int32_t h = fs_openf(path, FS_OF_MAKE | FS_OF_WRITE);
|
||||
if (h < 0) {
|
||||
uprintf("fs mkf: could not create %s\n", path);
|
||||
return;
|
||||
}
|
||||
if (FS_MKF_CONFIG.write != NULL) {
|
||||
if (ioctl(ioh, IOCTL_WRITE, (uint64_t)FS_MKF_CONFIG.write, string_len(FS_MKF_CONFIG.write), 0) < 0) {
|
||||
if (fs_write(h, (const uint8_t *)FS_MKF_CONFIG.write, string_len(FS_MKF_CONFIG.write), 0) < 0) {
|
||||
uprintf("fs mkf: could not write to %s\n", path);
|
||||
}
|
||||
}
|
||||
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
||||
fs_closef(h);
|
||||
}
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
int showtree(char *root, int indent) {
|
||||
#define INDENT() for (size_t i = 0; i < indent; i++) uprintf(" ")
|
||||
|
||||
IoctlStat statbuf; ZERO(&statbuf);
|
||||
if (ioctl(IOCTL_NOHANDLE, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)root, 0) != E_OK) {
|
||||
FsStat statbuf; ZERO(&statbuf);
|
||||
if (fs_stat(root, &statbuf) != E_OK) {
|
||||
uprintf("fs: could not stat %s\n", root);
|
||||
return -1;
|
||||
}
|
||||
@ -18,16 +18,16 @@ int showtree(char *root, int indent) {
|
||||
}
|
||||
|
||||
|
||||
if (statbuf.type == IOCTLSTAT_FILE) {
|
||||
if (statbuf.type == FSSTAT_FILE) {
|
||||
return 0;
|
||||
}
|
||||
if (statbuf.type == IOCTLSTAT_DIR) {
|
||||
IoctlDirent *dirents = (IoctlDirent *)umalloc(statbuf.size * sizeof(*dirents));
|
||||
if (statbuf.type == FSSTAT_DIR) {
|
||||
FsDirent *dirents = (FsDirent *)umalloc(statbuf.size * sizeof(*dirents));
|
||||
|
||||
for (size_t i = 0; i < statbuf.size; i++) {
|
||||
ioctl(IOCTL_NOHANDLE, IOCTL_FETCHDIRENT, (uint64_t)root, (uint64_t)&dirents[i], i);
|
||||
fs_fetchdirent(root, &dirents[i], i);
|
||||
|
||||
IoctlDirent *dirent = &dirents[i];
|
||||
FsDirent *dirent = &dirents[i];
|
||||
|
||||
if (string_strcmp(dirent->name, ".") == 0 || string_strcmp(dirent->name, "..") == 0) {
|
||||
continue;
|
||||
|
||||
@ -56,26 +56,24 @@ void set_config(void) {
|
||||
}
|
||||
|
||||
void do_file(char *filepath) {
|
||||
int32_t ret;
|
||||
|
||||
int32_t ioh = ioctl(IOCTL_NOHANDLE, IOCTL_OPENF, (uint64_t)filepath, IOCTL_F_READ, 0);
|
||||
if (ioh < 0) {
|
||||
LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(ioh));
|
||||
int32_t h = fs_openf(filepath, FS_OF_READ);
|
||||
if (h < 0) {
|
||||
LOG(LOG_ERR, "Could not open %s: %s\n", filepath, ERRSTRING(h));
|
||||
return;
|
||||
}
|
||||
|
||||
IoctlStat statbuf; ZERO(&statbuf);
|
||||
FsStat statbuf; ZERO(&statbuf);
|
||||
|
||||
ioctl(ioh, IOCTL_STAT, (uint64_t)&statbuf, (uint64_t)filepath, 0);
|
||||
if (statbuf.type != IOCTLSTAT_FILE) {
|
||||
fs_stat(filepath, &statbuf);
|
||||
if (statbuf.type != FSSTAT_FILE) {
|
||||
LOG(LOG_ERR, "%s is not a file (%d)\n", filepath, statbuf.type);
|
||||
return;
|
||||
}
|
||||
uint8_t *buf = umalloc(statbuf.size+1);
|
||||
string_memset(buf, 0, statbuf.size+1);
|
||||
|
||||
if ((ret = ioctl(ioh, IOCTL_READ, (uint64_t)buf, statbuf.size, 0)) < 0) {
|
||||
LOG(LOG_ERR, "Could not read %s (%d): %s\n", filepath, ioh, ERRSTRING(ioh));
|
||||
if (fs_read(h, buf, statbuf.size, 0) < 0) {
|
||||
LOG(LOG_ERR, "Could not read %s (%d): %s\n", filepath, h, ERRSTRING(h));
|
||||
goto done;
|
||||
}
|
||||
|
||||
@ -89,7 +87,7 @@ void do_file(char *filepath) {
|
||||
|
||||
done:
|
||||
ufree(buf);
|
||||
ioctl(ioh, IOCTL_CLOSEF, 0, 0, 0);
|
||||
fs_closef(h);
|
||||
}
|
||||
|
||||
void do_mode_interactive(void) {
|
||||
|
||||
Reference in New Issue
Block a user