1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

Kernel/Devices: Abstract SysFS Device add/remove methods more properly

It is starting to get a little messy with how each device can try to add
or remove itself to either /sys/dev/block or /sys/dev/char directories.

To better do this, we introduce 4 virtual methods to take care of that,
so until we ensure all nodes in /sys/dev/block and /sys/dev/char are
actual symlinks, we allow the Device base class to call virtual methods
upon insertion or before being destroying, so it add itself elegantly to
either of these directories or remove itself when needed.

For special cases where we need to create symlinks, we have two virtual
methods to be called otherwise to do almost the same thing mentioned
before, but to use symlinks instead.
This commit is contained in:
Liav A 2022-07-16 10:39:57 +03:00 committed by Linus Groh
parent da8d18b263
commit 3af70cb0fc
10 changed files with 107 additions and 46 deletions

View file

@ -5,6 +5,7 @@
*/
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/FileSystem/SysFS/Subsystems/DeviceIdentifiers/BlockDevicesDirectory.h>
namespace Kernel {
@ -26,6 +27,40 @@ void AsyncBlockDeviceRequest::start()
BlockDevice::~BlockDevice() = default;
void BlockDevice::after_inserting_add_symlink_to_device_identifier_directory()
{
VERIFY(m_symlink_sysfs_component);
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.append(*m_symlink_sysfs_component);
});
}
void BlockDevice::before_will_be_destroyed_remove_symlink_from_device_identifier_directory()
{
VERIFY(m_symlink_sysfs_component);
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.remove(*m_symlink_sysfs_component);
});
}
// FIXME: This method will be eventually removed after all nodes in /sys/dev/block/ are symlinks
void BlockDevice::after_inserting_add_to_device_identifier_directory()
{
VERIFY(m_sysfs_component);
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.append(*m_sysfs_component);
});
}
// FIXME: This method will be eventually removed after all nodes in /sys/dev/block/ are symlinks
void BlockDevice::before_will_be_destroyed_remove_from_device_identifier_directory()
{
VERIFY(m_sysfs_component);
SysFSBlockDevicesDirectory::the().devices_list({}).with([&](auto& list) -> void {
list.remove(*m_sysfs_component);
});
}
bool BlockDevice::read_block(u64 index, UserOrKernelBuffer& buffer)
{
auto read_request_or_error = try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, 1, buffer, m_block_size);