summaryrefslogtreecommitdiff
path: root/Kernel/Storage/Partition
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2021-08-27 08:08:43 +0300
committerAndreas Kling <kling@serenityos.org>2021-09-08 00:42:20 +0200
commitfb7b4caa574e8d4ad72657525590c5076d97a65a (patch)
treeee23eb822b2099db5ade437646ca2d28b91e7e97 /Kernel/Storage/Partition
parent74c4c864bdc625c3584cbe6107a1617b946d7a86 (diff)
downloadserenity-fb7b4caa574e8d4ad72657525590c5076d97a65a.zip
Kernel/Storage: Implement basic AHCI hotplug support
This is really a basic support for AHCI hotplug events, so we know how to add a node representing the device in /sys/dev/block and removing it according to the event type (insertion/removal). This change doesn't take into account what happens if the device was mounted or a read/write operation is being handled. For this to work correctly, StorageManagement now uses the Singleton container, as it might be accessed simultaneously from many CPUs for hotplug events. DiskPartition holds a WeakPtr instead of a RefPtr, to allow removal of a StorageDevice object from the heap. StorageDevices are now stored and being referenced to via an IntrusiveList to make it easier to remove them on hotplug event. In future changes, all of the stated above might change, but for now, this commit represents the least amount of changes to make everything to work correctly.
Diffstat (limited to 'Kernel/Storage/Partition')
-rw-r--r--Kernel/Storage/Partition/DiskPartition.cpp13
-rw-r--r--Kernel/Storage/Partition/DiskPartition.h3
2 files changed, 10 insertions, 6 deletions
diff --git a/Kernel/Storage/Partition/DiskPartition.cpp b/Kernel/Storage/Partition/DiskPartition.cpp
index 367a3b3a4f..17d474f011 100644
--- a/Kernel/Storage/Partition/DiskPartition.cpp
+++ b/Kernel/Storage/Partition/DiskPartition.cpp
@@ -33,7 +33,10 @@ const DiskPartitionMetadata& DiskPartition::metadata() const
void DiskPartition::start_request(AsyncBlockDeviceRequest& request)
{
- auto sub_request_or_error = m_device->try_make_request<AsyncBlockDeviceRequest>(request.request_type(),
+ auto device = m_device.strong_ref();
+ if (!device)
+ request.complete(AsyncBlockDeviceRequest::RequestResult::Failure);
+ auto sub_request_or_error = device->try_make_request<AsyncBlockDeviceRequest>(request.request_type(),
request.block_index() + m_metadata.start_block(), request.block_count(), request.buffer(), request.buffer_size());
if (sub_request_or_error.is_error())
TODO();
@@ -44,28 +47,28 @@ KResultOr<size_t> DiskPartition::read(OpenFileDescription& fd, u64 offset, UserO
{
unsigned adjust = m_metadata.start_block() * block_size();
dbgln_if(OFFD_DEBUG, "DiskPartition::read offset={}, adjust={}, len={}", fd.offset(), adjust, len);
- return m_device->read(fd, offset + adjust, outbuf, len);
+ return m_device.strong_ref()->read(fd, offset + adjust, outbuf, len);
}
bool DiskPartition::can_read(const OpenFileDescription& fd, size_t offset) const
{
unsigned adjust = m_metadata.start_block() * block_size();
dbgln_if(OFFD_DEBUG, "DiskPartition::can_read offset={}, adjust={}", offset, adjust);
- return m_device->can_read(fd, offset + adjust);
+ return m_device.strong_ref()->can_read(fd, offset + adjust);
}
KResultOr<size_t> DiskPartition::write(OpenFileDescription& fd, u64 offset, const UserOrKernelBuffer& inbuf, size_t len)
{
unsigned adjust = m_metadata.start_block() * block_size();
dbgln_if(OFFD_DEBUG, "DiskPartition::write offset={}, adjust={}, len={}", offset, adjust, len);
- return m_device->write(fd, offset + adjust, inbuf, len);
+ return m_device.strong_ref()->write(fd, offset + adjust, inbuf, len);
}
bool DiskPartition::can_write(const OpenFileDescription& fd, size_t offset) const
{
unsigned adjust = m_metadata.start_block() * block_size();
dbgln_if(OFFD_DEBUG, "DiskPartition::can_write offset={}, adjust={}", offset, adjust);
- return m_device->can_write(fd, offset + adjust);
+ return m_device.strong_ref()->can_write(fd, offset + adjust);
}
StringView DiskPartition::class_name() const
diff --git a/Kernel/Storage/Partition/DiskPartition.h b/Kernel/Storage/Partition/DiskPartition.h
index 0352ee2883..46f99a1fcd 100644
--- a/Kernel/Storage/Partition/DiskPartition.h
+++ b/Kernel/Storage/Partition/DiskPartition.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/RefPtr.h>
+#include <AK/WeakPtr.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Storage/Partition/DiskPartitionMetadata.h>
@@ -32,7 +33,7 @@ private:
DiskPartition(BlockDevice&, unsigned, DiskPartitionMetadata);
- NonnullRefPtr<BlockDevice> m_device;
+ WeakPtr<BlockDevice> m_device;
DiskPartitionMetadata m_metadata;
};