summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-10-02 16:22:16 -0700
committerAndreas Kling <kling@serenityos.org>2021-10-03 13:36:10 +0200
commit70ad18fbcda87ac8305d50edd9e1a85d9b461c8f (patch)
tree1d5b2fab80e0d86cfbdc8f021baebaa825ca394d /Kernel
parent3a945051fc70b772558496c2a02d1f09320cb134 (diff)
downloadserenity-70ad18fbcda87ac8305d50edd9e1a85d9b461c8f.zip
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.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/Storage/PATADiskDevice.cpp18
-rw-r--r--Kernel/Storage/PATADiskDevice.h3
-rw-r--r--Kernel/Storage/RamdiskDevice.cpp19
-rw-r--r--Kernel/Storage/RamdiskDevice.h3
-rw-r--r--Kernel/Storage/SATADiskDevice.cpp18
-rw-r--r--Kernel/Storage/SATADiskDevice.h3
-rw-r--r--Kernel/Storage/StorageDevice.cpp11
-rw-r--r--Kernel/Storage/StorageDevice.h10
8 files changed, 45 insertions, 40 deletions
diff --git a/Kernel/Storage/PATADiskDevice.cpp b/Kernel/Storage/PATADiskDevice.cpp
index 5bed61ac03..8dfe190122 100644
--- a/Kernel/Storage/PATADiskDevice.cpp
+++ b/Kernel/Storage/PATADiskDevice.cpp
@@ -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;
diff --git a/Kernel/Storage/PATADiskDevice.h b/Kernel/Storage/PATADiskDevice.h
index 77fc49b77a..9b604cee98 100644
--- a/Kernel/Storage/PATADiskDevice.h
+++ b/Kernel/Storage/PATADiskDevice.h
@@ -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;
diff --git a/Kernel/Storage/RamdiskDevice.cpp b/Kernel/Storage/RamdiskDevice.cpp
index baa859181e..5f18732231 100644
--- a/Kernel/Storage/RamdiskDevice.cpp
+++ b/Kernel/Storage/RamdiskDevice.cpp
@@ -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);
-}
-
}
diff --git a/Kernel/Storage/RamdiskDevice.h b/Kernel/Storage/RamdiskDevice.h
index 117bc0f525..e4cd63f1dc 100644
--- a/Kernel/Storage/RamdiskDevice.h
+++ b/Kernel/Storage/RamdiskDevice.h
@@ -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;
diff --git a/Kernel/Storage/SATADiskDevice.cpp b/Kernel/Storage/SATADiskDevice.cpp
index d90f86533c..05b1f2ce8c 100644
--- a/Kernel/Storage/SATADiskDevice.cpp
+++ b/Kernel/Storage/SATADiskDevice.cpp
@@ -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());
-}
-
}
diff --git a/Kernel/Storage/SATADiskDevice.h b/Kernel/Storage/SATADiskDevice.h
index 76f6d21fb9..7240c502c1 100644
--- a/Kernel/Storage/SATADiskDevice.h
+++ b/Kernel/Storage/SATADiskDevice.h
@@ -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;
diff --git a/Kernel/Storage/StorageDevice.cpp b/Kernel/Storage/StorageDevice.cpp
index 04e3221a69..a012f54fa2 100644
--- a/Kernel/Storage/StorageDevice.cpp
+++ b/Kernel/Storage/StorageDevice.cpp
@@ -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());
diff --git a/Kernel/Storage/StorageDevice.h b/Kernel/Storage/StorageDevice.h
index 6f54efeff8..83215c97d2 100644
--- a/Kernel/Storage/StorageDevice.h
+++ b/Kernel/Storage/StorageDevice.h
@@ -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;
};