vfs: complete restructure of vfs

This commit is contained in:
Michael Shaw 2024-01-24 18:15:04 -05:00
commit b7c10e80c2
5 changed files with 70 additions and 175 deletions

View file

@ -136,11 +136,11 @@ void schedule()
static struct file_descriptor *proc_insert_charfd(struct proc *proc, int fdno, int flags)
{
struct file_descriptor *fd = fd_special();
fd->vnode = (struct vnode){ 0 };
fd->file->vnode = (struct vnode){ 0 };
fd->pos = 0;
fd->fd = fdno;
fd->flags = flags;
fd->mode = FD_TYPE_CHARDEV;
fd->file->vnode.flags = VFS_VNO_CHARDEV;
fd->buf = NULL;
fd->buf_len = 0;
fd->fs = NULL;

View file

@ -12,6 +12,26 @@
typedef void (*syscall_t)(uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5);
syscall_t syscall_table[SYSCALL_MAX];
static slab_t *fd_slab;
struct file_descriptor *fd_special()
{
struct file_descriptor *fd = slab_alloc(fd_slab);
if (!fd) {
kprintf(LOG_ERROR "Failed to allocate file descriptor\n");
return NULL;
}
memset(fd, 0, sizeof(struct file_descriptor));
return fd;
}
void fd_special_free(struct file_descriptor *fd)
{
slab_free(fd_slab, fd);
}
void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4, uint64_t arg5)
{
@ -20,7 +40,7 @@ void syscall(uint64_t syscall_no, uint64_t arg1, uint64_t arg2, uint64_t arg3, u
syscall(arg1, arg2, arg3, arg4, arg5);
}
int sys_read(int fd, void *buf, size_t count)
ssize_t sys_read(int fd, void *buf, size_t count)
{
if ((uintptr_t)buf >= hhdm_start)
return -EFAULT;
@ -35,21 +55,24 @@ int sys_read(int fd, void *buf, size_t count)
return -EBADF;
struct file_descriptor *fdesc = (void *)fdesc_node->value;
struct file *file = fdesc->file;
void *tmp_buf = kzalloc(count, ALLOC_KERN);
if (tmp_buf == NULL)
return -ENOMEM;
int ret = read(fdesc, tmp_buf, count);
ssize_t ret = vfs_read(file, tmp_buf, fdesc->pos, count);
if (ret > 0)
memcpy(buf, tmp_buf, count);
else
ret = -1;
fdesc->pos += ret;
kfree(tmp_buf);
return ret;
}
int sys_write(int fd, void *buf, size_t count)
ssize_t sys_write(int fd, void *buf, size_t count)
{
if ((uintptr_t)buf >= hhdm_start)
return -EFAULT;
@ -64,13 +87,14 @@ int sys_write(int fd, void *buf, size_t count)
return -EBADF;
struct file_descriptor *fdesc = (void *)fdesc_node->value;
struct file *file = fdesc->file;
void *tmp_buf = kzalloc(count, ALLOC_KERN);
if (tmp_buf == NULL)
return -ENOMEM;
memcpy(tmp_buf, buf, count);
int ret = write(fdesc, tmp_buf, count);
ssize_t ret = vfs_write(file, tmp_buf, fdesc->pos, count);
if (ret < 0)
ret = -1;
@ -87,12 +111,19 @@ int sys_open(const char *pathname, int flags)
if (proc == NULL)
return -1;
struct file_descriptor *fdesc = open(pathname, flags);
if (fdesc == NULL)
return -1;
int err;
struct file *file = vfs_open(pathname, &err);
if (file == NULL)
return err;
struct file_descriptor *fdesc = slab_alloc(fd_slab);
uint64_t fdno = rbt_next_key(&proc->fd_map);
fdesc->fd = fdno;
fdesc->file = file;
fdesc->pos = 0;
fdesc->flags = flags;
struct rbnode *node = rbt_insert(&proc->fd_map, fdno);
node->value = (uintptr_t)fdesc;
@ -122,6 +153,8 @@ static void syscall_insert(uint64_t syscall_no, syscall_t syscall)
void syscall_init()
{
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
memset(syscall_table, 0, sizeof(syscall_table));
syscall_insert(SYS_READ, (syscall_t)sys_read);
syscall_insert(SYS_WRITE, (syscall_t)sys_write);