From 9d7f97ccd53fb2cd5ff9631bc8d8740d100dff30 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 15 Jul 2024 22:09:21 +0300 Subject: [PATCH] Kernel: Implement DevFileSystem::remove_device This function cleanly removes the devices from the whole filesystem. USB devices are now removed from the filesystem as soon as they are destroyed. --- kernel/include/kernel/FS/DevFS/FileSystem.h | 2 ++ kernel/kernel/FS/DevFS/FileSystem.cpp | 15 +++++++++++++++ kernel/kernel/USB/HID/HIDDriver.cpp | 5 ++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/kernel/include/kernel/FS/DevFS/FileSystem.h b/kernel/include/kernel/FS/DevFS/FileSystem.h index 3e7feee796..a27c606ebf 100644 --- a/kernel/include/kernel/FS/DevFS/FileSystem.h +++ b/kernel/include/kernel/FS/DevFS/FileSystem.h @@ -18,6 +18,8 @@ namespace Kernel void initialize_device_updater(); void add_device(BAN::RefPtr); + void remove_device(BAN::RefPtr); + void add_inode(BAN::StringView path, BAN::RefPtr); void initiate_sync(bool should_block); diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index 92c40ec257..902700c517 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -112,6 +112,21 @@ namespace Kernel MUST(m_devices.push_back(device)); } + void DevFileSystem::remove_device(BAN::RefPtr device) + { + LockGuard _(m_device_lock); + ASSERT(!device->name().contains('/')); + MUST(static_cast(root_inode().ptr())->unlink(device->name())); + for (size_t i = 0; i < m_devices.size(); i++) + { + if (m_devices[i] == device) + { + m_devices.remove(i); + break; + } + } + } + void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr inode) { ASSERT(!inode->is_device()); diff --git a/kernel/kernel/USB/HID/HIDDriver.cpp b/kernel/kernel/USB/HID/HIDDriver.cpp index 19f878b34d..ec895361c2 100644 --- a/kernel/kernel/USB/HID/HIDDriver.cpp +++ b/kernel/kernel/USB/HID/HIDDriver.cpp @@ -78,7 +78,10 @@ namespace Kernel {} USBHIDDriver::~USBHIDDriver() - {} + { + if (m_hid_device) + DevFileSystem::get().remove_device(m_hid_device); + } BAN::ErrorOr USBHIDDriver::initialize() {