ext2: use struct vnode instead of struct file

This commit is contained in:
Michael Shaw 2024-01-26 18:41:09 -05:00
commit d61edcbe7b
6 changed files with 55 additions and 28 deletions

View file

@ -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;

View file

@ -8,8 +8,9 @@
#include <fs/vfs.h>
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);
}