diff --git a/fs/ext2.c b/fs/ext2.c index bb6125a..5bf9839 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -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; } diff --git a/fs/vfs.c b/fs/vfs.c index 6c2579b..5590422 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -4,7 +4,6 @@ #include #include - #include 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; } diff --git a/include/fs/vfs.h b/include/fs/vfs.h index dc6ba14..268224c 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -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]; }; diff --git a/include/kernel/slab.h b/include/kernel/slab.h index f51c733..e6f30a4 100644 --- a/include/kernel/slab.h +++ b/include/kernel/slab.h @@ -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 */