fs: reorginize memory allocations and frees

This commit is contained in:
Michael Shaw 2024-01-30 00:09:56 -05:00
commit 070af3aba0
4 changed files with 65 additions and 29 deletions

View file

@ -1279,6 +1279,11 @@ static int ext2_read_vno(struct fs *vfs, struct vnode *vnode, void *buf, size_t
return ext2_inode_read_block(fs, (struct ext2_inode *)&inode, buf, blockno);
}
static int ext2_read_dirents(struct fs *vfs, struct vnode *vnode, struct dirent **out)
{
return ext2_readdir(vnode, out);
}
struct fs *ext2_init_fs(struct block_device *bdev)
{
struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN);
@ -1322,11 +1327,8 @@ struct fs *ext2_init_fs(struct block_device *bdev)
ext2_ops = kmalloc(sizeof(struct fs_ops), ALLOC_KERN);
if (!ext2_ops) {
kfree(extfs->bgdt);
kfree(extfs);
return NULL;
}
if (!ext2_ops)
goto out;
ext2_ops->open = ext2_open_file;
ext2_ops->read = ext2_read_file;
@ -1341,25 +1343,14 @@ struct fs *ext2_init_fs(struct block_device *bdev)
ret->ops = ext2_ops;
struct ext2_inode *root_inode = kmalloc(sizeof(struct ext2_inode), ALLOC_DMA);
if (!root_inode) {
kfree(extfs->bgdt);
kfree(extfs);
kfree(ext2_ops);
kfree(ret);
return NULL;
}
if (!root_inode)
goto out_2;
ext2_read_inode(ret->fs, root_inode, EXT2_ROOT_INO);
ret->root = kmalloc(sizeof(struct vnode), ALLOC_KERN);
if (!ret->root) {
kfree(extfs->bgdt);
kfree(extfs);
kfree(ext2_ops);
kfree(ret);
kfree(root_inode);
return NULL;
}
if (!ret->root)
goto out_3;
memset(ret->root, 0, sizeof(struct vnode));
ret->root->name[0] = '/';
@ -1372,4 +1363,15 @@ struct fs *ext2_init_fs(struct block_device *bdev)
ret->root->no_free = true;
return ret;
out_3:
kfree(ret);
kfree(root_inode);
out_2:
kfree(ext2_ops);
out:
kfree(extfs->bgdt);
kfree(extfs);
return NULL;
}

View file

@ -4,7 +4,6 @@
#include <kernel/rbtree.h>
#include <fs/ext2.h>
#include <fs/vfs.h>
static struct rbtree *kfd;
@ -26,8 +25,7 @@ struct file *vfs_open(const char *pathname, int *err)
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;
goto out;
}
memset(file, 0, sizeof(struct file));
@ -43,12 +41,16 @@ struct file *vfs_open(const char *pathname, int *err)
if (result < 0) {
*err = result;
slab_free(file_slab, file);
slab_free(vnode_slab, vnode);
return NULL;
goto out_2;
}
return file;
out_2:
slab_free(file_slab, file);
out:
slab_free(vnode_slab, vnode);
return NULL;
}
int vfs_close(struct file *file)
@ -197,6 +199,17 @@ char *dirname(char *path)
return path;
}
static void vfs_dealloc(struct fs *fs)
{
ATTEMPT_FREE(fs->ops);
if (fs->root)
ATTEMPT_FREE(fs->root->dirents);
ATTEMPT_FREE(fs->root);
ATTEMPT_FREE(fs->mount_point);
ATTEMPT_FREE(fs);
}
struct fs *vfs_mount(const char *dev, const char *mount_point)
{
/* TODO: detect filesystem type */
@ -217,6 +230,20 @@ struct fs *vfs_mount(const char *dev, const char *mount_point)
fs->mount_point = kmalloc(strlen(mount_point) + 1, ALLOC_KERN);
strcpy(fs->mount_point, mount_point);
/* read dirents from fs root */
struct vnode *root = fs->root;
struct dirent *dirents = NULL;
int num_dirents = fs->ops->readdir(root, &dirents);
if (num_dirents < 0) {
kprintf(LOG_ERROR "Failed to read root directory\n");
vfs_dealloc(fs);
fs = NULL;
goto out;
}
root->dirents = dirents;
root->no_free = true;
out:
return fs;
}

View file

@ -65,15 +65,16 @@ struct vnode {
struct vnode *next;
struct dirent *dirents; /* for directories */
size_t num_dirents;
bool mount_ptr; /* does this node point to a root of a mounted fs? */
bool no_free; /* permanently cached vnodes */
};
struct dirent {
uint64_t inode;
uint16_t reclen;
ftype_t type;
uint64_t inode; /* fs-specific inode number */
uint16_t reclen; /* length of this record */
ftype_t type; /* type: file, dir, chrdev, blkdev, etc. */
char name[256];
};

View file

@ -12,6 +12,12 @@
#define ALLOC_KERN 0x01
#define ALLOC_DMA 0x02
#define ATTEMPT_FREE(_x) \
if (_x) { \
kfree(_x); \
_x = NULL; \
}
typedef struct _slab {
size_t size; /* size of each object */
size_t tsize; /* total size of this slab */