From ad16d47783fff6ae4ecebfcd84586d7a1129c877 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Sun, 29 Oct 2023 22:27:36 -0400 Subject: [PATCH] vfs: change fs_ops struct in fs to a pointer --- fs/ext2.c | 24 +++++++++++++++++------- fs/vfs.c | 12 ++++++------ include/fs/vfs.h | 2 +- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/fs/ext2.c b/fs/ext2.c index cd66110..bc2966f 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -15,6 +15,8 @@ #define ITYPE_DECL(_ino, _de) [(_ino >> 12)] = (_de) #define DTYPE_DECL(_de, _ino) [(_de)] = (_ino) +static struct fs_ops *ext2_ops; + static uint8_t inode_to_ftype[] = { ITYPE_DECL(EXT2_INO_FIFO, VFS_FILE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, VFS_FILE_CHARDEV), ITYPE_DECL(EXT2_INO_DIR, VFS_FILE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, VFS_FILE_BLKDEV), @@ -1162,14 +1164,22 @@ struct fs *ext2_init_fs(struct block_device *bdev) ret->fs = extfs; ret->type = VFS_TYPE_EXT2; - struct fs_ops ops = { .open = ext2_open_file, - .read = ext2_read_file, - .write = ext2_write_file, - .readdir = ext2_readdir, - .mkdir = ext2_mkdir, - .unlink = ext2_unlink }; + ext2_ops = kmalloc(sizeof(struct fs_ops), ALLOC_KERN); - ret->ops = ops; + if(!ext2_ops) { + kfree(extfs->bgdt); + kfree(extfs); + return NULL; + } + + ext2_ops->open = ext2_open_file; + ext2_ops->read = ext2_read_file; + ext2_ops->write = ext2_write_file; + ext2_ops->readdir = ext2_readdir; + ext2_ops->mkdir = ext2_mkdir; + ext2_ops->unlink = ext2_unlink; + + ret->ops = ext2_ops; return ret; } diff --git a/fs/vfs.c b/fs/vfs.c index 8122b4f..fef574b 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -29,7 +29,7 @@ int write(int fd, void *buf, size_t count) if (fdesc->file.type != VFS_FILE_FILE) return -EBADF; - int ret = fdesc->fs->ops.write(fdesc->fs, &fdesc->file, buf, fdesc->pos, count); + int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->file, buf, fdesc->pos, count); if (ret < 0) return ret; @@ -54,7 +54,7 @@ int read(int fd, void *buf, size_t count) if (fdesc->file.type != VFS_FILE_FILE) return -EBADF; - int ret = fdesc->fs->ops.read(fdesc->fs, &fdesc->file, buf, fdesc->pos, count); + int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->file, buf, fdesc->pos, count); if (ret < 0) return ret; @@ -90,7 +90,7 @@ int open(const char *pathname, int flags) memset(fd, 0, sizeof(struct file_descriptor)); /* find the file */ - int result = rootfs->ops.open(rootfs, &fd->file, pathname); + int result = rootfs->ops->open(rootfs, &fd->file, pathname); if (result < 0) { kprintf(LOG_ERROR "Failed to open %s: %s\n", pathname, strerror(-result)); @@ -166,12 +166,12 @@ size_t tell(int fd) int unlink(const char *pathname) { - return rootfs->ops.unlink(rootfs, pathname); + return rootfs->ops->unlink(rootfs, pathname); } int mkdir(const char *pathname) { - return rootfs->ops.mkdir(rootfs, pathname); + return rootfs->ops->mkdir(rootfs, pathname); } DIR *opendir(const char *name) @@ -215,7 +215,7 @@ struct dirent *readdir(DIR *dir) return NULL; if (dir->dirents == NULL) - dir->num_dirents = fs->ops.readdir(fs, file->inode_num, &dir->dirents); + dir->num_dirents = fs->ops->readdir(fs, file->inode_num, &dir->dirents); if (dir->dirents == NULL) return NULL; diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 55e64f6..e91650c 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -106,7 +106,7 @@ struct fs_ops { struct fs { void *fs; uint32_t type; - struct fs_ops ops; + struct fs_ops *ops; char *mount_point; };