From d61edcbe7b2a073a39fd799b75d08cc17f6bb85b Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Fri, 26 Jan 2024 18:41:09 -0500 Subject: [PATCH] ext2: use `struct vnode` instead of `struct file` --- fs/ext2.c | 13 ++++++++----- fs/vfs.c | 46 ++++++++++++++++++++++++++++++++-------------- include/fs/vfs.h | 15 ++++++++++----- init/cmd.c | 2 +- sched/load.c | 2 +- sched/proc.c | 5 +++-- 6 files changed, 55 insertions(+), 28 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index 1d3e74a..7fbe8ad 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -675,13 +675,15 @@ static int ext2_open_file(struct fs *vfs, struct vnode *vnode, const char *path) return 0; } -static int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out) +static int ext2_readdir(struct vnode *vnode, struct dirent **out) { + struct fs *vfs = vnode->fs; struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN); if (!inode) return -ENOMEM; - int ret = ext2_read_inode(fs->fs, inode, ino_num); + struct ext2fs *extfs = (struct ext2fs *)vfs->fs; + int ret = ext2_read_inode(extfs, inode, vnode->ino_num); if (ret < 0) { kfree(inode); return ret; @@ -692,7 +694,6 @@ static int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out) return -ENOTDIR; } - struct ext2fs *extfs = (struct ext2fs *)fs->fs; struct ext2_dir_entry *entry = kmalloc(extfs->block_size, ALLOC_DMA); void *oentry = entry; if (!entry) { @@ -746,8 +747,9 @@ static int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out) return num_entries; } -static int ext2_write_file(struct fs *vfs, struct vnode *vnode, void *buf, size_t offset, size_t count) +static int ext2_write_file(struct vnode *vnode, void *buf, size_t offset, size_t count) { + struct fs *vfs = vnode->fs; struct ext2fs *fs = vfs->fs; struct ext2_inode inode; @@ -1172,8 +1174,9 @@ static int ext2_unlink(struct fs *vfs, const char *path) return -ENOENT; } -static int ext2_read_file(struct fs *vfs, struct vnode *vnode, void *buf, size_t offset, size_t count) +static int ext2_read_file(struct vnode *vnode, void *buf, size_t offset, size_t count) { + struct fs *vfs = vnode->fs; struct ext2fs *fs = vfs->fs; struct ext2_inode inode; diff --git a/fs/vfs.c b/fs/vfs.c index 7f02339..6c2579b 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -8,8 +8,9 @@ #include static struct rbtree *kfd; -static slab_t *file_slab; static struct fs *rootfs; +static slab_t *file_slab; +static slab_t *vnode_slab; typedef struct fs *(*vfs_init_t)(struct block_device *); @@ -22,18 +23,28 @@ struct file *vfs_open(const char *pathname, int *err) return NULL; } + struct vnode *vnode = slab_alloc(vnode_slab); + if (!vnode) { + kprintf(LOG_ERROR "Failed to allocate vnode struct\n"); + slab_free(file_slab, file); + return NULL; + } + memset(file, 0, sizeof(struct file)); + file->vnode = vnode; + /* terrible temporary hack */ if (pathname == NULL && (uintptr_t)err == 0x4) return file; /* find the file */ - int result = rootfs->ops->open(rootfs, &file->vnode, pathname); + int result = rootfs->ops->open(rootfs, vnode, pathname); if (result < 0) { *err = result; slab_free(file_slab, file); + slab_free(vnode_slab, vnode); return NULL; } @@ -42,6 +53,12 @@ struct file *vfs_open(const char *pathname, int *err) int vfs_close(struct file *file) { + if (!file) + return -EBADF; + + if (!file->vnode->no_free) + slab_free(vnode_slab, file->vnode); + slab_free(file_slab, file); return 0; } @@ -52,10 +69,10 @@ ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count) if (file->type == FTYPE_CHARDEV) return -EBADF; - if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) + if ((file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_REG) return -EISDIR; - return file->vnode.fs->ops->write(file->vnode.fs, &file->vnode, buf, off, count); + return file->vnode->fs->ops->write(file->vnode, buf, off, count); } ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) @@ -65,21 +82,21 @@ ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) return -EBADF; /* TODO: implement */ - if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG) + if ((file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_REG) return -EISDIR; - if (off >= file->vnode.size) + if (off >= file->vnode->size) return 0; - if (off + count > file->vnode.size) - count = file->vnode.size - off; + 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); + return file->vnode->fs->ops->read(file->vnode, buf, off, count); } int vfs_statf(struct file *file, struct statbuf *statbuf) { - statbuf->size = file->vnode.size; + statbuf->size = file->vnode->size; return 0; } @@ -103,9 +120,9 @@ DIR *opendir(const char *name) return NULL; } - struct fs *fs = file->vnode.fs; + struct fs *fs = file->vnode->fs; - if ((file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) { + if ((file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) { vfs_close(file); return NULL; } @@ -126,11 +143,11 @@ struct dirent *readdir(DIR *dir) struct fs *fs = dir->fs; - if ((dir->file->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) + if ((dir->file->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) return NULL; if (dir->dirents == NULL) - dir->num_dirents = fs->ops->readdir(fs, dir->file->vnode.ino_num, &dir->dirents); + dir->num_dirents = fs->ops->readdir(dir->file->vnode, &dir->dirents); if (dir->dirents == NULL) return NULL; @@ -227,4 +244,5 @@ void vfs_init(const char *rootdev_name) /* init file descriptor slab */ file_slab = slab_create(sizeof(struct file), 16 * KB, 0); + vnode_slab = slab_create(sizeof(struct vnode), 16 * KB, 0); } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index e889589..b531317 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -63,6 +63,11 @@ struct vnode { struct fs *fs; struct vnode *ptr; struct vnode *next; + + struct dirent *dirents; /* for directories */ + + bool mount_point; /* is this vnode a fs root? */ + bool no_free; /* permanently cached vnodes */ }; struct dirent { @@ -81,7 +86,7 @@ typedef struct _DIR { } DIR; struct file { - struct vnode vnode; + struct vnode *vnode; ino_t inode_num; /* inode number */ uint32_t type; /* type of the file */ @@ -104,11 +109,11 @@ struct file_descriptor { }; struct fs_ops { - int (*read)(struct fs *fs, struct vnode *vnode, void *buf, size_t offset, size_t size); - int (*write)(struct fs *fs, struct vnode *vnode, void *buf, size_t offset, size_t size); + int (*read)(struct vnode *vnode, void *buf, size_t offset, size_t size); + int (*write)(struct vnode *vnode, void *buf, size_t offset, size_t size); int (*open)(struct fs *fs, struct vnode *vnode, const char *path); - int (*readdir)(struct fs *fs, uint32_t ino, struct dirent **dir); - int (*mkdir)(struct fs *fs, const char *path); + int (*readdir)(struct vnode *vnode, struct dirent **dir); + int (*mkdir)(struct fs *fs, const char *path); /* to be removed */ int (*unlink)(struct fs *fs, const char *path); }; diff --git a/init/cmd.c b/init/cmd.c index bd06a24..a43c65e 100644 --- a/init/cmd.c +++ b/init/cmd.c @@ -182,7 +182,7 @@ int kcmd_stat(int argc, char **argv) } kprintf("File: %s\n", argv[1]); - kprintf("Size: %d\n", file->vnode.size); + kprintf("Size: %d\n", file->vnode->size); return 0; } diff --git a/sched/load.c b/sched/load.c index d928d7a..ca5a48d 100644 --- a/sched/load.c +++ b/sched/load.c @@ -44,7 +44,7 @@ pid_t elf_load_proc(char *fname) if (!file) return -ENOENT; - size_t size = file->vnode.size; + size_t size = file->vnode->size; char *buf = kmalloc(size, ALLOC_KERN); if (!buf) diff --git a/sched/proc.c b/sched/proc.c index 156dcea..c3e9c29 100644 --- a/sched/proc.c +++ b/sched/proc.c @@ -147,16 +147,17 @@ static struct file_descriptor *proc_insert_charfd(struct proc *proc, int fdno, i { struct file_descriptor *fd = fd_special(); fd->file = vfs_open(NULL, (void *)0x4); /* FIXME LATER */ - fd->file->vnode = (struct vnode){ 0 }; fd->pos = 0; fd->fd = fdno; fd->flags = flags; fd->file->type = FTYPE_CHARDEV; - fd->file->vnode.flags = VFS_VNO_CHARDEV; + fd->file->vnode->flags = VFS_VNO_CHARDEV; fd->buf = NULL; fd->buf_len = 0; fd->fs = NULL; + memset(fd->file->vnode, 0, sizeof(struct vnode)); + fd->buf_write = NULL; fd->buf_read = NULL;