2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-25 01:35:19 +00:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-29 02:55:54 +00:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2020-01-22 21:23:50 +00:00
|
|
|
#include <Kernel/FileSystem/InodeMetadata.h>
|
2021-08-14 08:40:37 +00:00
|
|
|
#include <Kernel/FileSystem/SysFS.h>
|
2021-08-14 07:13:27 +00:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
|
|
|
#include <Kernel/Sections.h>
|
2019-02-15 23:47:20 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-14 07:13:27 +00:00
|
|
|
static Singleton<MutexProtected<HashMap<u32, Device*>>> s_all_devices;
|
2020-01-22 21:23:50 +00:00
|
|
|
|
2021-08-14 07:13:27 +00:00
|
|
|
MutexProtected<HashMap<u32, Device*>>& Device::all_devices()
|
2020-01-22 21:23:50 +00:00
|
|
|
{
|
|
|
|
return *s_all_devices;
|
|
|
|
}
|
|
|
|
|
2021-08-14 08:40:37 +00:00
|
|
|
NonnullRefPtr<SysFSDeviceComponent> SysFSDeviceComponent::must_create(Device const& device)
|
|
|
|
{
|
|
|
|
return adopt_ref_if_nonnull(new SysFSDeviceComponent(device)).release_nonnull();
|
|
|
|
}
|
|
|
|
SysFSDeviceComponent::SysFSDeviceComponent(Device const& device)
|
|
|
|
: SysFSComponent(String::formatted("{}:{}", device.major(), device.minor()))
|
2021-09-10 11:44:46 +00:00
|
|
|
, m_block_device(device.is_block_device())
|
2021-08-14 08:40:37 +00:00
|
|
|
{
|
2021-09-10 11:44:46 +00:00
|
|
|
VERIFY(device.is_block_device() || device.is_character_device());
|
2021-08-14 08:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNMAP_AFTER_INIT NonnullRefPtr<SysFSDevicesDirectory> SysFSDevicesDirectory::must_create(SysFSRootDirectory const& root_directory)
|
|
|
|
{
|
|
|
|
auto devices_directory = adopt_ref_if_nonnull(new SysFSDevicesDirectory(root_directory)).release_nonnull();
|
|
|
|
devices_directory->m_components.append(SysFSBlockDevicesDirectory::must_create(*devices_directory));
|
|
|
|
devices_directory->m_components.append(SysFSCharacterDevicesDirectory::must_create(*devices_directory));
|
|
|
|
return devices_directory;
|
|
|
|
}
|
|
|
|
SysFSDevicesDirectory::SysFSDevicesDirectory(SysFSRootDirectory const& root_directory)
|
|
|
|
: SysFSDirectory("dev"sv, root_directory)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<SysFSBlockDevicesDirectory> SysFSBlockDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
|
|
|
|
{
|
|
|
|
return adopt_ref_if_nonnull(new SysFSBlockDevicesDirectory(devices_directory)).release_nonnull();
|
|
|
|
}
|
|
|
|
SysFSBlockDevicesDirectory::SysFSBlockDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
|
|
|
|
: SysFSDirectory("block"sv, devices_directory)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
KResult SysFSBlockDevicesDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
|
|
|
{
|
|
|
|
VERIFY(m_parent_directory);
|
|
|
|
callback({ ".", { fsid, component_index() }, 0 });
|
|
|
|
callback({ "..", { fsid, m_parent_directory->component_index() }, 0 });
|
|
|
|
|
|
|
|
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
|
|
|
|
for (auto& exposed_device : list) {
|
2021-09-10 11:44:46 +00:00
|
|
|
if (!exposed_device.is_block_device())
|
2021-08-14 08:40:37 +00:00
|
|
|
continue;
|
|
|
|
callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
RefPtr<SysFSComponent> SysFSBlockDevicesDirectory::lookup(StringView name)
|
|
|
|
{
|
|
|
|
return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
|
|
|
|
for (auto& exposed_device : list) {
|
2021-09-10 11:44:46 +00:00
|
|
|
if (!exposed_device.is_block_device())
|
2021-08-14 08:40:37 +00:00
|
|
|
continue;
|
|
|
|
if (exposed_device.name() == name)
|
|
|
|
return exposed_device;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<SysFSCharacterDevicesDirectory> SysFSCharacterDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
|
|
|
|
{
|
|
|
|
return adopt_ref_if_nonnull(new SysFSCharacterDevicesDirectory(devices_directory)).release_nonnull();
|
|
|
|
}
|
|
|
|
SysFSCharacterDevicesDirectory::SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
|
|
|
|
: SysFSDirectory("char"sv, devices_directory)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
KResult SysFSCharacterDevicesDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
|
|
|
|
{
|
|
|
|
VERIFY(m_parent_directory);
|
|
|
|
callback({ ".", { fsid, component_index() }, 0 });
|
|
|
|
callback({ "..", { fsid, m_parent_directory->component_index() }, 0 });
|
|
|
|
|
|
|
|
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
|
|
|
|
for (auto& exposed_device : list) {
|
2021-09-10 11:44:46 +00:00
|
|
|
if (exposed_device.is_block_device())
|
2021-08-14 08:40:37 +00:00
|
|
|
continue;
|
|
|
|
callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
|
|
|
|
{
|
|
|
|
return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
|
|
|
|
for (auto& exposed_device : list) {
|
2021-09-10 11:44:46 +00:00
|
|
|
if (exposed_device.is_block_device())
|
2021-08-14 08:40:37 +00:00
|
|
|
continue;
|
|
|
|
if (exposed_device.name() == name)
|
|
|
|
return exposed_device;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:48:15 +00:00
|
|
|
void Device::for_each(Function<void(Device&)> callback)
|
|
|
|
{
|
2021-08-14 07:13:27 +00:00
|
|
|
all_devices().with_exclusive([&](auto& map) -> void {
|
|
|
|
for (auto& entry : map)
|
|
|
|
callback(*entry.value);
|
|
|
|
});
|
2019-08-18 11:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Device* Device::get_device(unsigned major, unsigned minor)
|
|
|
|
{
|
2021-08-14 07:13:27 +00:00
|
|
|
return all_devices().with_exclusive([&](auto& map) -> Device* {
|
|
|
|
auto it = map.find(encoded_device(major, minor));
|
|
|
|
if (it == map.end())
|
|
|
|
return nullptr;
|
|
|
|
return it->value;
|
|
|
|
});
|
2019-08-18 11:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 21:23:50 +00:00
|
|
|
Device::Device(unsigned major, unsigned minor)
|
2019-06-07 09:43:58 +00:00
|
|
|
: m_major(major)
|
|
|
|
, m_minor(minor)
|
2019-02-17 09:38:07 +00:00
|
|
|
{
|
2020-01-22 21:23:50 +00:00
|
|
|
u32 device_id = encoded_device(major, minor);
|
2021-08-14 07:13:27 +00:00
|
|
|
all_devices().with_exclusive([&](auto& map) -> void {
|
|
|
|
auto it = map.find(device_id);
|
|
|
|
if (it != map.end()) {
|
|
|
|
dbgln("Already registered {},{}: {}", major, minor, it->value->class_name());
|
|
|
|
}
|
|
|
|
VERIFY(!map.contains(device_id));
|
|
|
|
map.set(device_id, this);
|
|
|
|
});
|
2021-09-10 11:44:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::after_inserting()
|
|
|
|
{
|
|
|
|
VERIFY(!m_sysfs_component);
|
2021-08-14 08:40:37 +00:00
|
|
|
auto sys_fs_component = SysFSDeviceComponent::must_create(*this);
|
|
|
|
m_sysfs_component = sys_fs_component;
|
|
|
|
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
|
|
|
|
list.append(sys_fs_component);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::before_removing()
|
|
|
|
{
|
2021-09-10 11:44:46 +00:00
|
|
|
m_state = State::BeingRemoved;
|
|
|
|
VERIFY(m_sysfs_component);
|
2021-08-14 08:40:37 +00:00
|
|
|
SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> void {
|
2021-09-10 11:44:46 +00:00
|
|
|
list.remove(*m_sysfs_component);
|
2021-08-14 08:40:37 +00:00
|
|
|
});
|
2019-02-17 09:38:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 23:47:20 +00:00
|
|
|
Device::~Device()
|
|
|
|
{
|
2021-08-14 08:40:37 +00:00
|
|
|
VERIFY(m_state == State::BeingRemoved);
|
2021-08-14 07:13:27 +00:00
|
|
|
u32 device_id = encoded_device(m_major, m_minor);
|
|
|
|
all_devices().with_exclusive([&](auto& map) -> void {
|
|
|
|
VERIFY(map.contains(device_id));
|
|
|
|
map.remove(encoded_device(m_major, m_minor));
|
|
|
|
});
|
2019-02-15 23:47:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 09:17:36 +00:00
|
|
|
String Device::absolute_path() const
|
2019-02-15 23:47:20 +00:00
|
|
|
{
|
2021-08-14 04:01:19 +00:00
|
|
|
// FIXME: I assume we can't really provide a well known path in the kernel
|
|
|
|
// because this is a violation of abstraction layers between userland and the
|
|
|
|
// kernel, but maybe the whole name of "absolute_path" is just wrong as this
|
|
|
|
// is really not an "absolute_path".
|
|
|
|
return String::formatted("device:{},{}", major(), minor());
|
2019-02-15 23:47:20 +00:00
|
|
|
}
|
2019-08-17 09:17:36 +00:00
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
String Device::absolute_path(const OpenFileDescription&) const
|
2019-08-17 09:17:36 +00:00
|
|
|
{
|
|
|
|
return absolute_path();
|
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-11-02 18:16:01 +00:00
|
|
|
void Device::process_next_queued_request(Badge<AsyncDeviceRequest>, const AsyncDeviceRequest& completed_request)
|
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker lock(m_requests_lock);
|
2021-02-06 23:59:32 +00:00
|
|
|
VERIFY(!m_requests.is_empty());
|
|
|
|
VERIFY(m_requests.first().ptr() == &completed_request);
|
|
|
|
m_requests.remove(m_requests.begin());
|
|
|
|
if (!m_requests.is_empty()) {
|
|
|
|
auto* next_request = m_requests.first().ptr();
|
|
|
|
next_request->do_start(move(lock));
|
2020-11-02 18:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2020-11-02 18:16:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|