1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

Kernel: Expose device presence in /sys/dev/char and /sys/dev/block

These files are not marked as block devices or character devices so they
are not meant to be used as device nodes. The filenames are formatted to
the pattern "major:minor", but a Userland program need to call the parse
these format and inspect the the major and minor numbers and create the
real device nodes in /dev.

Later on, it might be a good idea to ensure we don't create new
SysFSComponents on the heap for each Device, but rather generate
them only when required (and preferably to not create a SysFSComponent
at all if possible).
This commit is contained in:
Liav A 2021-08-14 11:40:37 +03:00 committed by Andreas Kling
parent 009feefee0
commit 6a9c717a30
5 changed files with 183 additions and 2 deletions

View file

@ -6,6 +6,7 @@
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Devices/Device.h>
#include <Kernel/FileSystem/SysFS.h>
#include <Kernel/Sections.h>
@ -35,6 +36,11 @@ UNMAP_AFTER_INIT void SysFSComponentRegistry::register_new_component(SysFSCompon
m_root_directory->m_components.append(component);
}
SysFSComponentRegistry::DevicesList& SysFSComponentRegistry::devices_list()
{
return m_devices_list;
}
NonnullRefPtr<SysFSRootDirectory> SysFSRootDirectory::create()
{
return adopt_ref(*new (nothrow) SysFSRootDirectory);
@ -57,7 +63,9 @@ SysFSRootDirectory::SysFSRootDirectory()
: SysFSDirectory(".")
{
auto buses_directory = SysFSBusDirectory::must_create(*this);
auto devices_directory = SysFSDevicesDirectory::must_create(*this);
m_components.append(buses_directory);
m_components.append(devices_directory);
m_buses_directory = buses_directory;
}

View file

@ -9,9 +9,11 @@
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/FileSystem/SysFSComponent.h>
#include <Kernel/Locking/MutexProtected.h>
namespace Kernel {
class SysFSDevicesDirectory;
class SysFSRootDirectory final : public SysFSDirectory {
friend class SysFSComponentRegistry;
@ -24,6 +26,50 @@ private:
RefPtr<SysFSBusDirectory> m_buses_directory;
};
class SysFSDeviceComponent final
: public SysFSComponent
, public Weakable<SysFSDeviceComponent> {
friend class SysFSComponentRegistry;
public:
static NonnullRefPtr<SysFSDeviceComponent> must_create(Device const&);
Device const& device() const { return *m_associated_device; }
private:
explicit SysFSDeviceComponent(Device const&);
IntrusiveListNode<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>> m_list_node;
RefPtr<Device> m_associated_device;
};
class SysFSDevicesDirectory final : public SysFSDirectory {
public:
static NonnullRefPtr<SysFSDevicesDirectory> must_create(SysFSRootDirectory const&);
private:
explicit SysFSDevicesDirectory(SysFSRootDirectory const&);
};
class SysFSBlockDevicesDirectory final : public SysFSDirectory {
public:
static NonnullRefPtr<SysFSBlockDevicesDirectory> must_create(SysFSDevicesDirectory const&);
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
private:
explicit SysFSBlockDevicesDirectory(SysFSDevicesDirectory const&);
};
class SysFSCharacterDevicesDirectory final : public SysFSDirectory {
public:
static NonnullRefPtr<SysFSCharacterDevicesDirectory> must_create(SysFSDevicesDirectory const&);
virtual KResult traverse_as_directory(unsigned, Function<bool(FileSystem::DirectoryEntryView const&)>) const override;
virtual RefPtr<SysFSComponent> lookup(StringView name) override;
private:
explicit SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const&);
};
class SysFSBusDirectory : public SysFSDirectory {
friend class SysFSComponentRegistry;
@ -35,6 +81,8 @@ private:
};
class SysFSComponentRegistry {
using DevicesList = MutexProtected<IntrusiveList<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>, &SysFSDeviceComponent::m_list_node>>;
public:
static SysFSComponentRegistry& the();
@ -49,9 +97,12 @@ public:
void register_new_bus_directory(SysFSDirectory&);
SysFSBusDirectory& buses_directory();
DevicesList& devices_list();
private:
Mutex m_lock;
NonnullRefPtr<SysFSRootDirectory> m_root_directory;
DevicesList m_devices_list;
};
class SysFS final : public FileSystem {