fs: move fs init into kernel cmdline

Previously, any partition with an ext2 filesystem would be mounted by
the gpt header parser. With this patch, the kernel cmdline `root=val` is
responsible for mounting the root filesystem.
This commit is contained in:
Michael Shaw 2023-10-19 11:08:54 -04:00
commit 25a828437a
10 changed files with 150 additions and 32 deletions

View file

@ -35,6 +35,16 @@ struct block_device *block_register(char *name, struct block_device_ops *ops, vo
return bdev;
}
struct block_device *block_get_device(const char *name)
{
for (size_t i = 0; i < block_devices_count; i++) {
if (!strcmp(block_devices[i]->name, name))
return block_devices[i];
}
return NULL;
}
inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size)
{
return bdev->ops.read(bdev, buf, offset + bdev->lba_start, size);

View file

@ -114,9 +114,10 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode)
return 0;
}
int ext2_open_file(struct ext2fs *fs, struct file *file, const char *path)
int ext2_open_file(void *extfs, struct file *file, const char *path)
{
/* find the inode of the file */
struct ext2fs *fs = (struct ext2fs *)extfs;
struct ext2_inode *inode = kmalloc(sizeof(struct ext2_inode), ALLOC_KERN);
if (!inode)
return -ENOMEM;
@ -199,17 +200,22 @@ int ext2_open_file(struct ext2fs *fs, struct file *file, const char *path)
return 0;
}
int ext2_close_file(struct ext2fs *fs, struct file *file)
int ext2_close_file(void *extfs, struct file *file)
{
return 0;
}
int ext2_write_file(struct ext2fs *fs, struct file *file, const void *buf, size_t count)
int ext2_write_file(void *extfs, struct file *file, void *buf, size_t count)
{
return 0;
}
int ext2_read_file(struct ext2fs *fs, struct file *file, const void *buf, size_t count)
int ext2_read_file(void *extfs, struct file *file, void *buf, size_t count)
{
return 0;
}
int ext2_seek_file(void *extfs, struct file *file, size_t offset)
{
return 0;
}
@ -290,45 +296,59 @@ next:
}
#endif /* KDEBUG */
struct ext2fs *ext2_init_fs(struct block_device *bdev)
struct fs *ext2_init_fs(struct block_device *bdev)
{
struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN);
struct ext2fs *extfs = kmalloc(sizeof(*extfs), ALLOC_KERN);
if (!ret)
if (!extfs)
return NULL;
if (ext2_read_super(bdev, &ret->sb) < 0) {
kfree(ret);
if (ext2_read_super(bdev, &extfs->sb) < 0) {
kfree(extfs);
return NULL;
}
ret->bdev = bdev;
ret->block_size = 1024 << ret->sb.log_block_size;
extfs->bdev = bdev;
extfs->block_size = 1024 << extfs->sb.log_block_size;
bdev->fs = ret;
bdev->fs = extfs;
/* read block group descriptor table */
uint32_t num_groups = ret->sb.blocks_count / ret->sb.blocks_per_group;
if (ret->sb.blocks_count % ret->sb.blocks_per_group)
uint32_t num_groups = extfs->sb.blocks_count / extfs->sb.blocks_per_group;
if (extfs->sb.blocks_count % extfs->sb.blocks_per_group)
num_groups++;
uint32_t bgdt_block = 2 + (ret->sb.first_data_block / ret->sb.blocks_per_group);
uint32_t bgdt_block = 2 + (extfs->sb.first_data_block / extfs->sb.blocks_per_group);
uint32_t bgdt_size = num_groups * sizeof(struct ext2_group_desc);
char *buf = kmalloc(bgdt_size, ALLOC_DMA);
ext2_read_block(bdev->fs, buf, bgdt_block);
ret->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
memcpy(ret->bgdt, buf, bgdt_size);
extfs->bgdt = (struct ext2_group_desc *)kmalloc(bgdt_size, ALLOC_KERN);
memcpy(extfs->bgdt, buf, bgdt_size);
kfree(buf);
#ifdef KDEBUG
struct ext2_inode root;
ext2_read_inode(ret, &root, 2);
ext2_print_dir(ret, &root, 0);
ext2_read_inode(extfs, &root, 2);
ext2_print_dir(extfs, &root, 0);
#endif /* KDEBUG */
struct fs *ret = kmalloc(sizeof(struct fs), ALLOC_KERN);
ret->fs = extfs;
ret->type = VFS_TYPE_EXT2;
struct fs_ops ops = {
.open = ext2_open_file,
.close = ext2_close_file,
.read = ext2_read_file,
.write = ext2_write_file,
.seek = ext2_seek_file,
};
ret->ops = ops;
return ret;
}

View file

@ -73,11 +73,6 @@ void block_gpt_init(struct block_device *dev)
bdev->lba_start = entry->lba_first;
bdev->block_count = entry->lba_last - entry->lba_first + 1;
struct ext2fs *fs = ext2_init_fs(bdev);
if (fs) {
kprintf(LOG_INFO "Found ext2 filesystem on device %s\n", buf);
}
}
dev->partition_count = used_count;

View file

@ -1,12 +1,38 @@
/* SDPX-License-Identifier: GPL-2.0-only */
#include <fs/vfs.h>
#include <kernel/slab.h>
#include <kernel/block.h>
#include <kernel/rbtree.h>
void vfs_init(struct block_device *root)
#include <fs/ext2.h>
#include <fs/vfs.h>
struct rbtree *kfd;
typedef struct fs *(*vfs_init_t)(struct block_device *);
void vfs_init(const char *rootfs)
{
kfd = kzalloc(sizeof(struct rbtree), ALLOC_DMA);
/* mount root */
struct block_device *rootdev = block_get_device(rootfs);
if (!rootdev) {
kprintf(LOG_ERROR "Failed to find root device %s\n", rootfs);
return;
} else {
kprintf(LOG_DEBUG "Found root device %s\n", rootfs);
/* TODO: detect filesystem type */
/* for now, assume ext2 */
struct fs *fs = ext2_init_fs(rootdev);
if (!fs) {
kprintf(LOG_ERROR "Failed to mount root device %s\n", rootfs);
return;
} else {
kprintf(LOG_DEBUG "Mounted root device %s\n", rootfs);
}
}
}
int write(int fd, const void *buf, size_t count)