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

Kernel/SysFS: Mark SysFSDirectory traverse and lookup methods as final

This enforces us to remove duplicated code across the SysFS code. This
results in great simplification of how the SysFS works now, because we
enforce one way to treat SysFSDirectory objects.
This commit is contained in:
Liav A 2022-04-23 01:06:37 +03:00 committed by Andreas Kling
parent 6733f19b3c
commit 70afa0b171
18 changed files with 114 additions and 178 deletions

View file

@ -23,9 +23,12 @@ UNMAP_AFTER_INIT void PCIBusSysFSDirectory::initialize()
UNMAP_AFTER_INIT PCIBusSysFSDirectory::PCIBusSysFSDirectory()
: SysFSDirectory(SysFSComponentRegistry::the().buses_directory())
{
MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
auto pci_device = PCIDeviceSysFSDirectory::create(*this, device_identifier.address());
m_components.append(pci_device);
MUST(m_child_components.with([&](auto& list) -> ErrorOr<void> {
MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
auto pci_device = PCIDeviceSysFSDirectory::create(*this, device_identifier.address());
list.append(pci_device);
}));
return {};
}));
}

View file

@ -17,7 +17,26 @@ UNMAP_AFTER_INIT NonnullRefPtr<PCIDeviceSysFSDirectory> PCIDeviceSysFSDirectory:
{
// FIXME: Handle allocation failure gracefully
auto device_name = MUST(KString::formatted("{:04x}:{:02x}:{:02x}.{}", address.domain(), address.bus(), address.device(), address.function()));
return adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(move(device_name), parent_directory, address));
auto directory = adopt_ref(*new (nothrow) PCIDeviceSysFSDirectory(move(device_name), parent_directory, address));
MUST(directory->m_child_components.with([&](auto& list) -> ErrorOr<void> {
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::VENDOR_ID, 2));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::DEVICE_ID, 2));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::CLASS, 1));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::SUBCLASS, 1));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::REVISION_ID, 1));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::PROG_IF, 1));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::SUBSYSTEM_ID, 2));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR0, 4));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR1, 4));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR2, 4));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR3, 4));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR4, 4));
list.append(PCIDeviceAttributeSysFSComponent::create(*directory, PCI::RegisterOffset::BAR5, 4));
return {};
}));
return directory;
}
UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(NonnullOwnPtr<KString> device_directory_name, SysFSDirectory const& parent_directory, PCI::Address address)
@ -25,21 +44,6 @@ UNMAP_AFTER_INIT PCIDeviceSysFSDirectory::PCIDeviceSysFSDirectory(NonnullOwnPtr<
, m_address(address)
, m_device_directory_name(move(device_directory_name))
{
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::VENDOR_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::DEVICE_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::CLASS, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBCLASS, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::REVISION_ID, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::PROG_IF, 1));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_VENDOR_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::SUBSYSTEM_ID, 2));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR0, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR1, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR2, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR3, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR4, 4));
m_components.append(PCIDeviceAttributeSysFSComponent::create(*this, PCI::RegisterOffset::BAR5, 4));
}
}

View file

@ -12,32 +12,6 @@ namespace Kernel {
static SysFSUSBBusDirectory* s_procfs_usb_bus_directory;
ErrorOr<void> SysFSUSBBusDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
SpinlockLocker lock(m_lock);
// Note: if the parent directory is null, it means something bad happened as this should not happen for the USB directory.
VERIFY(m_parent_directory);
TRY(callback({ "."sv, { fsid, component_index() }, 0 }));
TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 }));
for (auto const& device_node : m_device_nodes) {
InodeIdentifier identifier = { fsid, device_node.component_index() };
TRY(callback({ device_node.name(), identifier, 0 }));
}
return {};
}
RefPtr<SysFSComponent> SysFSUSBBusDirectory::lookup(StringView name)
{
SpinlockLocker lock(m_lock);
for (auto& device_node : m_device_nodes) {
if (device_node.name() == name) {
return device_node;
}
}
return {};
}
RefPtr<SysFSUSBDeviceInformation> SysFSUSBBusDirectory::device_node_for(USB::Device& device)
{
RefPtr<USB::Device> checked_device = device;

View file

@ -23,9 +23,6 @@ public:
void plug(USB::Device&);
void unplug(USB::Device&);
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
private:
explicit SysFSUSBBusDirectory(SysFSBusDirectory&);

View file

@ -28,31 +28,4 @@ SysFSBlockDevicesDirectory& SysFSBlockDevicesDirectory::the()
return *s_the;
}
ErrorOr<void> SysFSBlockDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
VERIFY(m_parent_directory);
TRY(callback({ "."sv, { fsid, component_index() }, 0 }));
TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 }));
return m_devices_list.with([&](auto& list) -> ErrorOr<void> {
for (auto& exposed_device : list) {
VERIFY(exposed_device.is_block_device());
TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
}
return {};
});
}
RefPtr<SysFSComponent> SysFSBlockDevicesDirectory::lookup(StringView name)
{
return m_devices_list.with([&](auto& list) -> RefPtr<SysFSComponent> {
for (auto& exposed_device : list) {
VERIFY(exposed_device.is_block_device());
if (exposed_device.name() == name)
return exposed_device;
}
return nullptr;
});
}
}

View file

@ -17,18 +17,13 @@ class SysFSBlockDevicesDirectory final : public SysFSDirectory {
public:
virtual StringView name() const override { return "block"sv; }
static NonnullRefPtr<SysFSBlockDevicesDirectory> must_create(SysFSDeviceIdentifiersDirectory const&);
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
static SysFSBlockDevicesDirectory& the();
using DevicesList = SpinlockProtected<IntrusiveList<&SysFSDeviceComponent::m_list_node>>;
DevicesList& devices_list(Badge<Device>) { return m_devices_list; }
ChildList& devices_list(Badge<Device>) { return m_child_components; }
private:
explicit SysFSBlockDevicesDirectory(SysFSDeviceIdentifiersDirectory const&);
DevicesList m_devices_list;
};
}

View file

@ -21,20 +21,6 @@ SysFSCharacterDevicesDirectory::SysFSCharacterDevicesDirectory(SysFSDeviceIdenti
{
s_the = this;
}
ErrorOr<void> SysFSCharacterDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
{
VERIFY(m_parent_directory);
TRY(callback({ "."sv, { fsid, component_index() }, 0 }));
TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 }));
return m_devices_list.with([&](auto& list) -> ErrorOr<void> {
for (auto& exposed_device : list) {
VERIFY(!exposed_device.is_block_device());
TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
}
return {};
});
}
SysFSCharacterDevicesDirectory& SysFSCharacterDevicesDirectory::the()
{
@ -42,16 +28,4 @@ SysFSCharacterDevicesDirectory& SysFSCharacterDevicesDirectory::the()
return *s_the;
}
RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
{
return m_devices_list.with([&](auto& list) -> RefPtr<SysFSComponent> {
for (auto& exposed_device : list) {
VERIFY(!exposed_device.is_block_device());
if (exposed_device.name() == name)
return exposed_device;
}
return nullptr;
});
}
}

View file

@ -17,18 +17,13 @@ class SysFSCharacterDevicesDirectory final : public SysFSDirectory {
public:
virtual StringView name() const override { return "char"sv; }
static NonnullRefPtr<SysFSCharacterDevicesDirectory> must_create(SysFSDeviceIdentifiersDirectory const&);
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
static SysFSCharacterDevicesDirectory& the();
using DevicesList = SpinlockProtected<IntrusiveList<&SysFSDeviceComponent::m_list_node>>;
DevicesList& devices_list(Badge<Device>) { return m_devices_list; }
ChildList& devices_list(Badge<Device>) { return m_child_components; }
private:
explicit SysFSCharacterDevicesDirectory(SysFSDeviceIdentifiersDirectory const&);
DevicesList m_devices_list;
};
}

View file

@ -25,7 +25,6 @@ public:
private:
SysFSDeviceComponent(NonnullOwnPtr<KString> major_minor_formatted_device_name, Device const&);
IntrusiveListNode<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>> m_list_node;
bool m_block_device;
NonnullOwnPtr<KString> m_major_minor_formatted_device_name;

View file

@ -15,8 +15,12 @@ namespace Kernel {
UNMAP_AFTER_INIT NonnullRefPtr<SysFSDeviceIdentifiersDirectory> SysFSDeviceIdentifiersDirectory::must_create(SysFSRootDirectory const& root_directory)
{
auto devices_directory = adopt_ref_if_nonnull(new SysFSDeviceIdentifiersDirectory(root_directory)).release_nonnull();
devices_directory->m_components.append(SysFSBlockDevicesDirectory::must_create(*devices_directory));
devices_directory->m_components.append(SysFSCharacterDevicesDirectory::must_create(*devices_directory));
MUST(devices_directory->m_child_components.with([&](auto& list) -> ErrorOr<void> {
list.append(SysFSBlockDevicesDirectory::must_create(*devices_directory));
list.append(SysFSCharacterDevicesDirectory::must_create(*devices_directory));
return {};
}));
return devices_directory;
}
SysFSDeviceIdentifiersDirectory::SysFSDeviceIdentifiersDirectory(SysFSRootDirectory const& root_directory)

View file

@ -58,8 +58,11 @@ void BIOSSysFSDirectory::create_components()
dbgln("BIOSSysFSDirectory: invalid smbios structure table length");
return;
}
m_components.append(BIOSSysFSComponent::must_create(BIOSSysFSComponent::Type::DMIEntryPoint, m_dmi_entry_point, m_dmi_entry_point_length));
m_components.append(BIOSSysFSComponent::must_create(BIOSSysFSComponent::Type::SMBIOSTable, m_smbios_structure_table, m_smbios_structure_table_length));
MUST(m_child_components.with([&](auto& list) -> ErrorOr<void> {
list.append(BIOSSysFSComponent::must_create(BIOSSysFSComponent::Type::DMIEntryPoint, m_dmi_entry_point, m_dmi_entry_point_length));
list.append(BIOSSysFSComponent::must_create(BIOSSysFSComponent::Type::SMBIOSTable, m_smbios_structure_table, m_smbios_structure_table_length));
return {};
}));
}
UNMAP_AFTER_INIT void BIOSSysFSDirectory::initialize_dmi_exposer()

View file

@ -22,10 +22,13 @@ UNMAP_AFTER_INIT void FirmwareSysFSDirectory::initialize()
void FirmwareSysFSDirectory::create_components()
{
m_components.append(BIOSSysFSDirectory::must_create(*this));
if (ACPI::is_enabled())
m_components.append(ACPI::ACPISysFSDirectory::must_create(*this));
m_components.append(PowerStateSwitchNode::must_create(*this));
MUST(m_child_components.with([&](auto& list) -> ErrorOr<void> {
list.append(BIOSSysFSDirectory::must_create(*this));
if (ACPI::is_enabled())
list.append(ACPI::ACPISysFSDirectory::must_create(*this));
list.append(PowerStateSwitchNode::must_create(*this));
return {};
}));
}
UNMAP_AFTER_INIT FirmwareSysFSDirectory::FirmwareSysFSDirectory()