Kernel: Rewrite basically all current disk io stuff

This is a big commit that was kinda hard to split to smaller ones.

Essentially we now look at all the mass storage devices from PCI
and initialize (P)ATA devices. This doesn't provide any more functionality
but better abstractions and everything doesn't have to be on its old
default port that might be different for modern computers.
This commit is contained in:
Bananymous
2023-02-26 03:00:29 +02:00
parent 048a2ebb95
commit ee5d02aa70
13 changed files with 570 additions and 475 deletions

View File

@@ -318,7 +318,7 @@ namespace Kernel
return inodes;
}
BAN::ErrorOr<Ext2FS*> Ext2FS::create(DiskDevice::Partition& partition)
BAN::ErrorOr<Ext2FS*> Ext2FS::create(StorageDevice::Partition& partition)
{
Ext2FS* ext2fs = new Ext2FS(partition);
if (ext2fs == nullptr)
@@ -344,8 +344,7 @@ namespace Kernel
uint32_t lba = 1024 / sector_size;
uint32_t sector_count = 1024 / sector_size;
if (!m_partition.read_sectors(lba, sector_count, superblock_buffer))
return BAN::Error::from_string("Could not read from partition");
TRY(m_partition.read_sectors(lba, sector_count, superblock_buffer));
memcpy(&m_superblock, superblock_buffer, sizeof(Ext2::Superblock));
}
@@ -398,13 +397,11 @@ namespace Kernel
return BAN::Error::from_string("Could not allocate memory for block group descriptor table");
BAN::ScopeGuard _([block_group_descriptor_table_buffer] { kfree(block_group_descriptor_table_buffer); });
if (!m_partition.read_sectors(
TRY(m_partition.read_sectors(
block_group_descriptor_table_block * sectors_per_block,
block_group_descriptor_table_sector_count,
block_group_descriptor_table_buffer
))
return BAN::Error::from_string("Could not read from partition");
));
TRY(m_block_group_descriptors.resize(number_of_block_groups));
for (uint32_t i = 0; i < number_of_block_groups; i++)
@@ -464,8 +461,7 @@ namespace Kernel
BAN::Vector<uint8_t> block_buffer;
TRY(block_buffer.resize(block_size));
if (!m_partition.read_sectors(block * sectors_per_block, sectors_per_block, block_buffer.data()))
return BAN::Error::from_string("Could not read from partition");
TRY(m_partition.read_sectors(block * sectors_per_block, sectors_per_block, block_buffer.data()));
return block_buffer;
}

View File

@@ -1,17 +1,22 @@
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <kernel/FS/Ext2.h>
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/PCI.h>
#include <kernel/Storage/ATAController.h>
namespace Kernel
{
static VirtualFileSystem* s_instance = nullptr;
void VirtualFileSystem::initialize(BAN::RefCounted<Inode> root_inode)
BAN::ErrorOr<void> VirtualFileSystem::initialize()
{
ASSERT(s_instance == nullptr);
s_instance = new VirtualFileSystem(root_inode);
ASSERT(s_instance);
s_instance = new VirtualFileSystem();
if (s_instance == nullptr)
return BAN::Error::from_string("Could not allocate the Virtual File System");
return s_instance->initialize_impl();
}
VirtualFileSystem& VirtualFileSystem::get()
@@ -25,6 +30,88 @@ namespace Kernel
return s_instance != nullptr;
}
BAN::ErrorOr<void> VirtualFileSystem::initialize_impl()
{
// Initialize all storage controllers
for (auto& device : PCI::get().devices())
{
if (device.class_code != 0x01)
continue;
switch (device.subclass)
{
case 0x0:
dwarnln("unsupported SCSI Bus Controller");
break;
case 0x1:
case 0x5:
TRY(m_storage_controllers.push_back(TRY(ATAController::create(device))));
break;
case 0x2:
dwarnln("unsupported Floppy Disk Controller");
break;
case 0x3:
dwarnln("unsupported IPI Bus Controller");
break;
case 0x4:
dwarnln("unsupported RAID Controller");
break;
case 0x6:
dwarnln("unsupported Serial ATA Controller");
break;
case 0x7:
dwarnln("unsupported Serial Attached SCSI Controller");
break;
case 0x8:
dwarnln("unsupported Non-Volatile Memory Controller");
break;
case 0x80:
dwarnln("unsupported Unknown Storage Controller");
break;
}
}
// Initialize partitions on all devices on found controllers
for (auto controller : m_storage_controllers)
{
for (auto device : controller->devices())
{
if (device->total_size() == 0)
continue;
auto result = device->initialize_partitions();
if (result.is_error())
{
dwarnln("{}", result.error());
continue;
}
for (auto& partition : device->partitions())
{
if (partition.name() == "banan-root"_sv)
{
if (m_root_inode)
dwarnln("multiple root partitions found");
else
{
auto ext2fs_or_error = Ext2FS::create(partition);
if (ext2fs_or_error.is_error())
dwarnln("{}", ext2fs_or_error.error());
else
// FIXME: We leave a dangling pointer to ext2fs. This might be okay since
// root fs sould probably be always mounted
m_root_inode = ext2fs_or_error.release_value()->root_inode();
}
}
}
}
}
if (m_root_inode)
return {};
return BAN::Error::from_string("Could not locate root partition");
}
BAN::ErrorOr<BAN::RefCounted<Inode>> VirtualFileSystem::from_absolute_path(BAN::StringView path)
{
if (path.front() != '/')