vfs: complete restructure of vfs
This commit is contained in:
parent
67050f1a93
commit
b7c10e80c2
5 changed files with 70 additions and 175 deletions
129
fs/vfs.c
129
fs/vfs.c
|
|
@ -8,67 +8,11 @@
|
|||
#include <fs/vfs.h>
|
||||
|
||||
static struct rbtree *kfd;
|
||||
static slab_t *fd_slab;
|
||||
static slab_t *file_slab;
|
||||
static struct fs *rootfs;
|
||||
static int fd_counter = 0;
|
||||
|
||||
typedef struct fs *(*vfs_init_t)(struct block_device *);
|
||||
|
||||
int write(struct file_descriptor *fdesc, void *buf, size_t count)
|
||||
{
|
||||
if (fdesc->mode == FD_TYPE_CHARDEV) {
|
||||
if (fdesc->buf_write)
|
||||
return fdesc->buf_write(fdesc, buf, count);
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
|
||||
return -EISDIR;
|
||||
|
||||
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
|
||||
return -EBADF;
|
||||
|
||||
if (fdesc->flags & O_RDONLY)
|
||||
return -EBADF;
|
||||
|
||||
int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
fdesc->pos = MIN(fdesc->pos + ret, fdesc->vnode.size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int read(struct file_descriptor *fdesc, void *buf, size_t count)
|
||||
{
|
||||
if (fdesc->mode == FD_TYPE_CHARDEV) {
|
||||
if (fdesc->buf_read)
|
||||
return fdesc->buf_read(fdesc, buf, count);
|
||||
return -EBADF;
|
||||
}
|
||||
|
||||
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
|
||||
return -EISDIR;
|
||||
|
||||
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
|
||||
return -EBADF;
|
||||
|
||||
if (fdesc->flags & O_WRONLY)
|
||||
return -EBADF;
|
||||
|
||||
int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
|
||||
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
|
||||
fdesc->pos = MIN(fdesc->pos + ret, fdesc->vnode.size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count)
|
||||
{
|
||||
/* TODO: implement */
|
||||
|
|
@ -91,6 +35,12 @@ ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count)
|
|||
if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
|
||||
return -EISDIR;
|
||||
|
||||
if (off >= file->vnode.size)
|
||||
return 0;
|
||||
|
||||
if (off + count > file->vnode.size)
|
||||
count = file->vnode.size - off;
|
||||
|
||||
return file->vnode.fs->ops->read(file->vnode.fs, &file->vnode, buf, off, count);
|
||||
}
|
||||
|
||||
|
|
@ -101,13 +51,6 @@ int vfs_statf(struct file *file, struct statbuf *statbuf)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf)
|
||||
{
|
||||
statbuf->size = fdesc->vnode.size;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct file *vfs_open(const char *pathname, int *err)
|
||||
{
|
||||
/* allocate a file */
|
||||
|
|
@ -134,47 +77,6 @@ struct file *vfs_open(const char *pathname, int *err)
|
|||
return file;
|
||||
}
|
||||
|
||||
struct file_descriptor *open(const char *pathname, int flags)
|
||||
{
|
||||
/* allocate a file descriptor */
|
||||
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));
|
||||
|
||||
/* find the file */
|
||||
int result = rootfs->ops->open(rootfs, &fd->vnode, pathname);
|
||||
|
||||
if (result < 0) {
|
||||
fd->fd = result;
|
||||
return fd;
|
||||
} else if (result == VFSE_IS_BDEV) {
|
||||
/* TODO: continue search into mounted filesystems */
|
||||
}
|
||||
|
||||
/* add the file descriptor to the tree */
|
||||
fd->fd = fd_counter;
|
||||
fd->fs = rootfs;
|
||||
fd_counter += 1;
|
||||
|
||||
struct rbnode *fdnode = rbt_insert(kfd, fd->fd);
|
||||
fdnode->value = (uint64_t)fd;
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int close(struct file_descriptor *fdesc)
|
||||
{
|
||||
struct rbnode *node = rbt_search(kfd, fdesc->fd);
|
||||
rbt_delete(kfd, node);
|
||||
slab_free(fd_slab, fdesc);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vfs_close(struct file *file)
|
||||
{
|
||||
slab_free(file_slab, file);
|
||||
|
|
@ -216,24 +118,6 @@ DIR *opendir(const char *name)
|
|||
return dir;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *dir)
|
||||
{
|
||||
if (!dir)
|
||||
|
|
@ -330,6 +214,5 @@ void vfs_init(const char *rootdev_name)
|
|||
rootfs = fs;
|
||||
|
||||
/* init file descriptor slab */
|
||||
fd_slab = slab_create(sizeof(struct file_descriptor), 16 * KB, 0);
|
||||
file_slab = slab_create(sizeof(struct file), 16 * KB, 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,12 +70,10 @@ struct file {
|
|||
|
||||
struct file_descriptor {
|
||||
struct file *file;
|
||||
struct vnode vnode;
|
||||
|
||||
int fd;
|
||||
uint64_t pos;
|
||||
uint32_t flags;
|
||||
uint32_t mode;
|
||||
|
||||
char *buf;
|
||||
size_t buf_len;
|
||||
|
|
@ -107,15 +105,11 @@ struct statbuf {
|
|||
};
|
||||
|
||||
void vfs_init(const char *rootfs);
|
||||
int write(struct file_descriptor *fdesc, void *buf, size_t count);
|
||||
ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count);
|
||||
ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count);
|
||||
int vfs_statf(struct file *file, struct statbuf *statbuf);
|
||||
struct file *vfs_open(const char *pathname, int *err);
|
||||
int vfs_close(struct file *file);
|
||||
int read(struct file_descriptor *fdesc, void *buf, size_t count);
|
||||
struct file_descriptor *open(const char *pathname, int flags);
|
||||
int close(struct file_descriptor *fdesc);
|
||||
int unlink(const char *pathname);
|
||||
int statfd(struct file_descriptor *fdesc, struct statbuf *statbuf);
|
||||
int mkdir(const char *pathname);
|
||||
|
|
|
|||
59
init/cmd.c
59
init/cmd.c
|
|
@ -73,22 +73,20 @@ int kcmd_cat(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
struct file_descriptor *fd = open(argv[1], 0);
|
||||
if (!fd) {
|
||||
kprintf("Failed to open %s: fd is NULL\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
int err;
|
||||
struct file *file = vfs_open(argv[1], &err);
|
||||
|
||||
if (fd->fd < 0) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
|
||||
if (!file) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buf[1024];
|
||||
char lastchar = 0;
|
||||
|
||||
size_t off = 0;
|
||||
while (true) {
|
||||
int n = read(fd, buf, sizeof(buf));
|
||||
ssize_t n = vfs_read(file, buf, off, sizeof(buf));
|
||||
if (n < 0) {
|
||||
kprintf("Failed to read %s: %s\n", argv[1], strerror(-n));
|
||||
return 1;
|
||||
|
|
@ -98,6 +96,8 @@ int kcmd_cat(int argc, char **argv)
|
|||
break;
|
||||
}
|
||||
|
||||
off += n;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
console_putchar(buf[i]);
|
||||
}
|
||||
|
|
@ -105,7 +105,7 @@ int kcmd_cat(int argc, char **argv)
|
|||
lastchar = buf[n - 1];
|
||||
}
|
||||
|
||||
close(fd);
|
||||
vfs_close(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -173,27 +173,16 @@ int kcmd_stat(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
struct file_descriptor *fd = open(argv[1], 0);
|
||||
int err;
|
||||
struct file *file = vfs_open(argv[1], &err);
|
||||
|
||||
if (!fd) {
|
||||
kprintf("Failed to open %s: fd is NULL\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fd->fd < 0) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct statbuf st;
|
||||
int ret = statfd(fd, &st);
|
||||
if (ret < 0) {
|
||||
kprintf("Failed to stat %s: %s\n", argv[1], strerror(-ret));
|
||||
if (!file) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
kprintf("File: %s\n", argv[1]);
|
||||
kprintf("Size: %d\n", st.size);
|
||||
kprintf("Size: %d\n", file->vnode.size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -222,14 +211,10 @@ int kcmd_elf(int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
struct file_descriptor *fd = open(argv[1], 0);
|
||||
if (fd == NULL) {
|
||||
kprintf("Failed to open %s: fd is NULL\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fd < 0) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-fd->fd));
|
||||
int err;
|
||||
struct file *file = vfs_open(argv[1], &err);
|
||||
if (!file) {
|
||||
kprintf("Failed to open %s: %s\n", argv[1], strerror(-err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -237,17 +222,17 @@ int kcmd_elf(int argc, char **argv)
|
|||
if (!buf)
|
||||
return -ENOMEM;
|
||||
|
||||
int ret = read(fd, buf, 4096);
|
||||
int ret = vfs_read(file, buf, 0, 4096);
|
||||
if (ret < 0) {
|
||||
kprintf("Failed to read %s: %s\n", argv[1], strerror(-ret));
|
||||
close(fd);
|
||||
vfs_close(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char elf_magic[] = { 0x7F, 'E', 'L', 'F' };
|
||||
if (strncmp(buf, elf_magic, 4)) {
|
||||
kprintf("Not an ELF file\n");
|
||||
close(fd);
|
||||
vfs_close(file);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -271,7 +256,7 @@ int kcmd_elf(int argc, char **argv)
|
|||
kprintf(" Section header count: %d\n", hdr->e_shnum);
|
||||
kprintf(" Section header string table index: %d\n", hdr->e_shstrndx);
|
||||
|
||||
close(fd);
|
||||
vfs_close(file);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue