1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:27:35 +00:00

Kernel: Remove most String usage from storage_name() API

This change is another minor step towards removing `AK::String` from
the Kernel. Instead of dynamically allocating the storage_name we can
instead allocate it via a KString in the factory for each device, and
then push the device name down into the StorageDevice base class.

We don't have a way of doing `AK::String::formatted(..)` with a KString
at the moment, so cleaning that up will be left for a later day.
This commit is contained in:
Brian Gianforcaro 2021-10-02 16:22:16 -07:00 committed by Andreas Kling
parent 3a945051fc
commit 70ad18fbcd
8 changed files with 45 additions and 40 deletions

View file

@ -11,19 +11,26 @@
#include <Kernel/Storage/IDEChannel.h>
#include <Kernel/Storage/IDEController.h>
#include <Kernel/Storage/PATADiskDevice.h>
#include <Kernel/Storage/StorageManagement.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<PATADiskDevice> PATADiskDevice::create(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block)
{
auto device_or_error = DeviceManagement::try_create_device<PATADiskDevice>(controller, channel, type, interface_type, capabilities, max_addressable_block);
auto minor_device_number = StorageManagement::minor_number();
// FIXME: We need a way of formatting strings with KString.
auto device_name = String::formatted("hd{:c}", 'a' + minor_device_number);
auto device_name_kstring = KString::must_create(device_name.view());
auto device_or_error = DeviceManagement::try_create_device<PATADiskDevice>(controller, channel, type, interface_type, capabilities, max_addressable_block, minor_device_number, move(device_name_kstring));
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();
}
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)
UNMAP_AFTER_INIT PATADiskDevice::PATADiskDevice(const IDEController& controller, IDEChannel& channel, DriveType type, InterfaceType interface_type, u16 capabilities, u64 max_addressable_block, int minor_device_number, NonnullOwnPtr<KString> device_name)
: StorageDevice(controller, StorageManagement::major_number(), minor_device_number, 512, max_addressable_block, move(device_name))
, m_capabilities(capabilities)
, m_channel(channel)
, m_drive_type(type)
@ -45,11 +52,6 @@ void PATADiskDevice::start_request(AsyncBlockDeviceRequest& request)
m_channel->start_request(request, is_slave(), m_capabilities);
}
String PATADiskDevice::storage_name() const
{
return String::formatted("hd{:c}", 'a' + minor());
}
bool PATADiskDevice::is_slave() const
{
return m_drive_type == DriveType::Slave;

View file

@ -43,10 +43,9 @@ public:
// ^BlockDevice
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
private:
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64);
PATADiskDevice(const IDEController&, IDEChannel&, DriveType, InterfaceType, u16, u64, int, NonnullOwnPtr<KString>);
// ^DiskDevice
virtual StringView class_name() const override;

View file

@ -15,14 +15,20 @@ namespace Kernel {
NonnullRefPtr<RamdiskDevice> RamdiskDevice::create(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
{
auto device_or_error = DeviceManagement::try_create_device<RamdiskDevice>(controller, move(region), major, minor);
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t drive_index = minor / 16;
// FIXME: We need a way of formatting strings with KString!
auto device_name = String::formatted("ramdisk{}", drive_index);
auto device_name_kstring = KString::must_create(device_name.view());
auto device_or_error = DeviceManagement::try_create_device<RamdiskDevice>(controller, move(region), major, minor, move(device_name_kstring));
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();
}
RamdiskDevice::RamdiskDevice(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
: StorageDevice(controller, major, minor, 512, region->size() / 512)
RamdiskDevice::RamdiskDevice(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor, NonnullOwnPtr<KString> device_name)
: StorageDevice(controller, major, minor, 512, region->size() / 512, move(device_name))
, m_region(move(region))
{
dmesgln("Ramdisk: Device #{} @ {}, Capacity={}", minor, m_region->vaddr(), max_addressable_block() * 512);
@ -59,11 +65,4 @@ void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request)
}
}
String RamdiskDevice::storage_name() const
{
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
size_t drive_index = minor() / 16;
return String::formatted("ramdisk{}", drive_index);
}
}

View file

@ -23,10 +23,9 @@ public:
// ^DiskDevice
virtual StringView class_name() const override;
virtual String storage_name() const override;
private:
RamdiskDevice(const RamdiskController&, NonnullOwnPtr<Memory::Region>&&, int major, int minor);
RamdiskDevice(const RamdiskController&, NonnullOwnPtr<Memory::Region>&&, int major, int minor, NonnullOwnPtr<KString> device_name);
// ^BlockDevice
virtual void start_request(AsyncBlockDeviceRequest&) override;

View file

@ -10,19 +10,26 @@
#include <Kernel/Storage/AHCIController.h>
#include <Kernel/Storage/IDEChannel.h>
#include <Kernel/Storage/SATADiskDevice.h>
#include <Kernel/Storage/StorageManagement.h>
namespace Kernel {
NonnullRefPtr<SATADiskDevice> SATADiskDevice::create(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block)
{
auto device_or_error = DeviceManagement::try_create_device<SATADiskDevice>(controller, port, sector_size, max_addressable_block);
auto minor_device_number = StorageManagement::minor_number();
// FIXME: We need a way of formatting strings with KString.
auto device_name = String::formatted("hd{:c}", 'a' + minor_device_number);
auto device_name_kstring = KString::must_create(device_name.view());
auto device_or_error = DeviceManagement::try_create_device<SATADiskDevice>(controller, port, sector_size, max_addressable_block, minor_device_number, move(device_name_kstring));
// FIXME: Find a way to propagate errors
VERIFY(!device_or_error.is_error());
return device_or_error.release_value();
}
SATADiskDevice::SATADiskDevice(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block)
: StorageDevice(controller, sector_size, max_addressable_block)
SATADiskDevice::SATADiskDevice(const AHCIController& controller, const AHCIPort& port, size_t sector_size, u64 max_addressable_block, int minor_number, NonnullOwnPtr<KString> device_name)
: StorageDevice(controller, StorageManagement::major_number(), minor_number, sector_size, max_addressable_block, move(device_name))
, m_port(port)
{
}
@ -41,9 +48,4 @@ void SATADiskDevice::start_request(AsyncBlockDeviceRequest& request)
m_port.strong_ref()->start_request(request);
}
String SATADiskDevice::storage_name() const
{
return String::formatted("hd{:c}", 'a' + minor());
}
}

View file

@ -31,10 +31,9 @@ public:
// ^StorageDevice
// ^BlockDevice
virtual void start_request(AsyncBlockDeviceRequest&) override;
virtual String storage_name() const override;
private:
SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block);
SATADiskDevice(const AHCIController&, const AHCIPort&, size_t sector_size, u64 max_addressable_block, int minor_device_number, NonnullOwnPtr<KString> device_name);
// ^DiskDevice
virtual StringView class_name() const override;

View file

@ -13,16 +13,18 @@
namespace Kernel {
StorageDevice::StorageDevice(const StorageController& controller, size_t sector_size, u64 max_addressable_block)
StorageDevice::StorageDevice(const StorageController& controller, size_t sector_size, u64 max_addressable_block, NonnullOwnPtr<KString> device_name)
: BlockDevice(StorageManagement::major_number(), StorageManagement::minor_number(), sector_size)
, m_storage_controller(controller)
, m_device_name(move(device_name))
, m_max_addressable_block(max_addressable_block)
{
}
StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, u64 max_addressable_block)
StorageDevice::StorageDevice(const StorageController& controller, int major, int minor, size_t sector_size, u64 max_addressable_block, NonnullOwnPtr<KString> device_name)
: BlockDevice(major, minor, sector_size)
, m_storage_controller(controller)
, m_storage_device_name(move(device_name))
, m_max_addressable_block(max_addressable_block)
{
}
@ -189,6 +191,11 @@ KResultOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const U
return pos + remaining;
}
StringView StorageDevice::storage_name() const
{
return m_storage_device_name->view();
}
bool StorageDevice::can_write(const OpenFileDescription&, size_t offset) const
{
return offset < (max_addressable_block() * block_size());

View file

@ -28,17 +28,14 @@ public:
virtual bool can_read(const OpenFileDescription&, size_t) const override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
virtual bool can_write(const OpenFileDescription&, size_t) const override;
// FIXME: This is being used only during early boot, find a better way to find devices...
virtual String storage_name() const = 0;
virtual void prepare_for_unplug() { m_partitions.clear(); }
StringView storage_name() const;
NonnullRefPtrVector<DiskPartition> partitions() const { return m_partitions; }
protected:
StorageDevice(const StorageController&, size_t, u64);
StorageDevice(const StorageController&, int, int, size_t, u64);
StorageDevice(const StorageController&, size_t, u64, NonnullOwnPtr<KString>);
StorageDevice(const StorageController&, int, int, size_t, u64, NonnullOwnPtr<KString>);
// ^DiskDevice
virtual StringView class_name() const override;
@ -46,6 +43,7 @@ private:
mutable IntrusiveListNode<StorageDevice, RefPtr<StorageDevice>> m_list_node;
NonnullRefPtr<StorageController> m_storage_controller;
NonnullRefPtrVector<DiskPartition> m_partitions;
NonnullOwnPtr<KString> m_storage_device_name;
u64 m_max_addressable_block;
};