From 9a3aa7eb0b3f8822bb95442681fbf851f53369fd Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Boric Date: Tue, 16 Mar 2021 20:23:16 +0100 Subject: [PATCH] Kernel: Refactor storage stack with u64 as number of blocks --- Kernel/Storage/AHCIPort.cpp | 4 ++-- Kernel/Storage/IDEChannel.cpp | 9 ++++++--- Kernel/Storage/PATADiskDevice.cpp | 16 ++++------------ Kernel/Storage/PATADiskDevice.h | 5 ++--- Kernel/Storage/RamdiskDevice.cpp | 9 ++------- Kernel/Storage/RamdiskDevice.h | 1 - Kernel/Storage/SATADiskDevice.cpp | 4 ++-- Kernel/Storage/SATADiskDevice.h | 4 ++-- Kernel/Storage/StorageDevice.cpp | 4 ++-- Kernel/Storage/StorageDevice.h | 8 ++++---- 10 files changed, 26 insertions(+), 38 deletions(-) diff --git a/Kernel/Storage/AHCIPort.cpp b/Kernel/Storage/AHCIPort.cpp index 8383f7b998b..0064365efa5 100644 --- a/Kernel/Storage/AHCIPort.cpp +++ b/Kernel/Storage/AHCIPort.cpp @@ -243,7 +243,7 @@ bool AHCIPort::initialize() size_t logical_sector_size = 512; size_t physical_sector_size = 512; - size_t max_addressable_sector = 0; + u64 max_addressable_sector = 0; if (identify_device()) { auto identify_block = map_typed(m_parent_handler->get_identify_metadata_physical_region(m_port_index)); // Check if word 106 is valid before using it! @@ -266,7 +266,7 @@ bool AHCIPort::initialize() m_port_registers.cmd = m_port_registers.cmd | (1 << 24); } - dmesgln("AHCI Port {}: max lba {:x}, L/P sector size - {}/{} ", representative_port_index(), max_addressable_sector, logical_sector_size, physical_sector_size); + dmesgln("AHCI Port {}: Device found, Capacity={}, Bytes per logical sector={}, Bytes per physical sector={}", representative_port_index(), max_addressable_sector * logical_sector_size, logical_sector_size, physical_sector_size); // FIXME: We don't support ATAPI devices yet, so for now we don't "create" them if (!is_atapi_attached()) { diff --git a/Kernel/Storage/IDEChannel.cpp b/Kernel/Storage/IDEChannel.cpp index a59342fdb9c..837e5347a31 100644 --- a/Kernel/Storage/IDEChannel.cpp +++ b/Kernel/Storage/IDEChannel.cpp @@ -388,12 +388,15 @@ UNMAP_AFTER_INIT void IDEChannel::detect_disks() u16 capabilities = wbufbase[ATA_IDENT_CAPABILITIES / sizeof(u16)]; if (cyls == 0 || heads == 0 || spt == 0) continue; - dbgln("IDEChannel: {} {} device found: Type={}, Name={}, C/H/Spt={}/{}/{}, Capabilities=0x{:04x}", channel_type_string(), channel_string(i), interface_type == PATADiskDevice::InterfaceType::ATA ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), cyls, heads, spt, capabilities); + u64 max_addressable_block = cyls * heads * spt; + if (capabilities & ATA_CAP_LBA) + max_addressable_block = (wbufbase[(ATA_IDENT_MAX_LBA + 2) / sizeof(u16)] << 16) | wbufbase[ATA_IDENT_MAX_LBA / sizeof(u16)]; + dbgln("IDEChannel: {} {} {} device found: Name={}, Capacity={}, C/H/Spt={}/{}/{}, Capabilities=0x{:04x}", channel_type_string(), channel_string(i), interface_type == PATADiskDevice::InterfaceType::ATA ? "ATA" : "ATAPI", ((char*)bbuf.data() + 54), max_addressable_block * 512, cyls, heads, spt, capabilities); if (i == 0) { - m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, interface_type, cyls, heads, spt, capabilities); + m_master = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Master, interface_type, capabilities, max_addressable_block); } else { - m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, interface_type, cyls, heads, spt, capabilities); + m_slave = PATADiskDevice::create(m_parent_controller, *this, PATADiskDevice::DriveType::Slave, interface_type, capabilities, max_addressable_block); } } } diff --git a/Kernel/Storage/PATADiskDevice.cpp b/Kernel/Storage/PATADiskDevice.cpp index 7df785ef37b..4649b715812 100644 --- a/Kernel/Storage/PATADiskDevice.cpp +++ b/Kernel/Storage/PATADiskDevice.cpp @@ -33,16 +33,13 @@ namespace Kernel { -UNMAP_AFTER_INIT NonnullRefPtr PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 cylinders, u16 heads, u16 spt, u16 capabilities) +UNMAP_AFTER_INIT NonnullRefPtr PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block) { - return adopt(*new PATADiskDevice(controller, channel, type, interface_type, cylinders, heads, spt, capabilities)); + return adopt(*new PATADiskDevice(controller, channel, type, interface_type, capabilities, max_addressable_block)); } -UNMAP_AFTER_INIT PATADiskDevice::PATADiskDevice(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 cylinders, u16 heads, u16 spt, u16 capabilities) - : StorageDevice(controller, 512, 0) - , m_cylinders(cylinders) - , m_heads(heads) - , m_sectors_per_track(spt) +UNMAP_AFTER_INIT PATADiskDevice::PATADiskDevice(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block) + : StorageDevice(controller, 512, max_addressable_block) , m_capabilities(capabilities) , m_channel(channel) , m_drive_type(type) @@ -70,11 +67,6 @@ String PATADiskDevice::device_name() const return String::formatted("hd{:c}", 'a' + minor()); } -size_t PATADiskDevice::max_addressable_block() const -{ - return m_cylinders * m_heads * m_sectors_per_track; -} - bool PATADiskDevice::is_slave() const { return m_drive_type == DriveType::Slave; diff --git a/Kernel/Storage/PATADiskDevice.h b/Kernel/Storage/PATADiskDevice.h index 4c5ee48a35c..f843ab14def 100644 --- a/Kernel/Storage/PATADiskDevice.h +++ b/Kernel/Storage/PATADiskDevice.h @@ -57,19 +57,18 @@ public: }; public: - static NonnullRefPtr create(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u16, u16, u16); + static NonnullRefPtr create(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64); virtual ~PATADiskDevice() override; // ^StorageDevice virtual Type type() const override { return StorageDevice::Type::IDE; } - virtual size_t max_addressable_block() const override; // ^BlockDevice virtual void start_request(AsyncBlockDeviceRequest&) override; virtual String device_name() const override; private: - PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u16, u16, u16); + PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64); // ^DiskDevice virtual const char* class_name() const override; diff --git a/Kernel/Storage/RamdiskDevice.cpp b/Kernel/Storage/RamdiskDevice.cpp index e6b89533371..ae037f716a4 100644 --- a/Kernel/Storage/RamdiskDevice.cpp +++ b/Kernel/Storage/RamdiskDevice.cpp @@ -38,10 +38,10 @@ NonnullRefPtr RamdiskDevice::create(const RamdiskController& cont } RamdiskDevice::RamdiskDevice(const RamdiskController& controller, OwnPtr&& region, int major, int minor) - : StorageDevice(controller, major, minor, 512, 0) + : StorageDevice(controller, major, minor, 512, m_region->size() / 512) , m_region(move(region)) { - dmesgln("Ramdisk: Device #{} @ {}, length {}", minor, m_region->vaddr(), m_region->size()); + dmesgln("Ramdisk: Device #{} @ {}, Capacity={}", minor, m_region->vaddr(), max_addressable_block() * 512); } RamdiskDevice::~RamdiskDevice() @@ -53,11 +53,6 @@ const char* RamdiskDevice::class_name() const return "RamdiskDevice"; } -size_t RamdiskDevice::max_addressable_block() const -{ - return m_region->size() / 512; -} - void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request) { LOCKER(m_lock); diff --git a/Kernel/Storage/RamdiskDevice.h b/Kernel/Storage/RamdiskDevice.h index 1846b09dce1..9de4ad77483 100644 --- a/Kernel/Storage/RamdiskDevice.h +++ b/Kernel/Storage/RamdiskDevice.h @@ -43,7 +43,6 @@ public: // ^StorageDevice virtual Type type() const override { return StorageDevice::Type::Ramdisk; } - virtual size_t max_addressable_block() const override; // ^BlockDevice virtual void start_request(AsyncBlockDeviceRequest&) override; diff --git a/Kernel/Storage/SATADiskDevice.cpp b/Kernel/Storage/SATADiskDevice.cpp index 0c7f4f10d7a..a41a66c2169 100644 --- a/Kernel/Storage/SATADiskDevice.cpp +++ b/Kernel/Storage/SATADiskDevice.cpp @@ -33,12 +33,12 @@ namespace Kernel { -NonnullRefPtr SATADiskDevice::create(const AHCIController& controller, const AHCIPort& port, size_t sector_size, size_t max_addressable_block) +NonnullRefPtr SATADiskDevice::create(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block) { return adopt(*new SATADiskDevice(controller, port, sector_size, max_addressable_block)); } -SATADiskDevice::SATADiskDevice(const AHCIController& controller, const AHCIPort& port, size_t sector_size, size_t max_addressable_block) +SATADiskDevice::SATADiskDevice(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block) : StorageDevice(controller, sector_size, max_addressable_block) , m_port(port) { diff --git a/Kernel/Storage/SATADiskDevice.h b/Kernel/Storage/SATADiskDevice.h index d9df0446a98..e26cfba906e 100644 --- a/Kernel/Storage/SATADiskDevice.h +++ b/Kernel/Storage/SATADiskDevice.h @@ -44,7 +44,7 @@ public: }; public: - static NonnullRefPtr create(const AHCIController&, const AHCIPort&, size_t sector_size, size_t max_addressable_block); + static NonnullRefPtr create(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block); virtual ~SATADiskDevice() override; // ^StorageDevice @@ -54,7 +54,7 @@ public: virtual String device_name() const override; private: - SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, size_t max_addressable_block); + SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block); // ^DiskDevice virtual const char* class_name() const override; diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp index 0763813f9c9..b158fdfa9f5 100644 --- a/Kernel/Storage/StorageDevice.cpp +++ b/Kernel/Storage/StorageDevice.cpp @@ -33,14 +33,14 @@ namespace Kernel { -StorageDevice::StorageDevice(const StorageController& controller, size_t sector_size, size_t max_addressable_block) +StorageDevice::StorageDevice(const StorageController& controller, size_t sector_size, u64 max_addressable_block) : BlockDevice(StorageManagement::major_number(), StorageManagement::minor_number(), sector_size) , m_storage_controller(controller) , m_max_addressable_block(max_addressable_block) { } -StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, size_t max_addressable_block) +StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, u64 max_addressable_block) : BlockDevice(major, minor, sector_size) , m_storage_controller(controller) , m_max_addressable_block(max_addressable_block) diff --git a/Kernel/Storage/StorageDevice.h b/Kernel/Storage/StorageDevice.h index a16ef113016..c2c3537c2d3 100644 --- a/Kernel/Storage/StorageDevice.h +++ b/Kernel/Storage/StorageDevice.h @@ -47,7 +47,7 @@ public: public: virtual Type type() const = 0; - virtual size_t max_addressable_block() const { return m_max_addressable_block; } + virtual u64 max_addressable_block() const { return m_max_addressable_block; } NonnullRefPtr controller() const; @@ -61,15 +61,15 @@ public: virtual mode_t required_mode() const override { return 0600; } protected: - StorageDevice(const StorageController&, size_t, size_t); - StorageDevice(const StorageController&, int, int, size_t, size_t); + StorageDevice(const StorageController&, size_t, u64); + StorageDevice(const StorageController&, int, int, size_t, u64); // ^DiskDevice virtual const char* class_name() const override; private: NonnullRefPtr m_storage_controller; NonnullRefPtrVector m_partitions; - size_t m_max_addressable_block; + u64 m_max_addressable_block; }; }