diff --git a/kernel/include/kernel/FS/DevFS/FileSystem.h b/kernel/include/kernel/FS/DevFS/FileSystem.h index 2eac5aae..de0a4a97 100644 --- a/kernel/include/kernel/FS/DevFS/FileSystem.h +++ b/kernel/include/kernel/FS/DevFS/FileSystem.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -17,7 +18,6 @@ namespace Kernel void add_device(BAN::RefPtr); void add_inode(BAN::StringView path, BAN::RefPtr); - void for_each_device(const BAN::Function& callback); dev_t get_next_dev() const; int get_next_input_device() const; @@ -32,6 +32,8 @@ namespace Kernel private: mutable SpinLock m_device_lock; + BAN::Vector> m_devices; + Semaphore m_sync_done; Semaphore m_sync_semaphore; volatile bool m_should_sync { false }; diff --git a/kernel/kernel/FS/DevFS/FileSystem.cpp b/kernel/kernel/FS/DevFS/FileSystem.cpp index a0207018..20c3214d 100644 --- a/kernel/kernel/FS/DevFS/FileSystem.cpp +++ b/kernel/kernel/FS/DevFS/FileSystem.cpp @@ -39,17 +39,12 @@ namespace Kernel { while (true) { - s_instance->m_device_lock.lock(); - s_instance->for_each_inode( - [](BAN::RefPtr inode) - { - if (inode->is_device()) - static_cast(inode.ptr())->update(); - return BAN::Iteration::Continue; - } - ); - s_instance->m_device_lock.unlock(); - Scheduler::get().reschedule(); + { + LockGuard _(s_instance->m_device_lock); + for (auto& device : s_instance->m_devices) + device->update(); + } + SystemTimer::get().sleep(10); } }, nullptr ); @@ -72,16 +67,11 @@ namespace Kernel s_instance->m_device_lock.lock(); } - s_instance->for_each_inode( - [](BAN::RefPtr inode) - { - if (inode->is_device()) - if (((Device*)inode.ptr())->is_storage_device()) - if (auto ret = static_cast(inode.ptr())->sync_disk_cache(); ret.is_error()) - dwarnln("disk sync: {}", ret.error()); - return BAN::Iteration::Continue; - } - ); + for (auto& device : s_instance->m_devices) + if (device->is_storage_device()) + if (auto ret = static_cast(device.ptr())->sync_disk_cache(); ret.is_error()) + dwarnln("disk sync: {}", ret.error()); + s_instance->m_should_sync = false; s_instance->m_sync_done.unblock(); } @@ -118,29 +108,19 @@ namespace Kernel void DevFileSystem::add_device(BAN::RefPtr device) { + LockGuard _(m_device_lock); ASSERT(!device->name().contains('/')); MUST(static_cast(root_inode().ptr())->link_inode(*device, device->name())); + MUST(m_devices.push_back(device)); } void DevFileSystem::add_inode(BAN::StringView path, BAN::RefPtr inode) { + ASSERT(!inode->is_device()); ASSERT(!path.contains('/')); MUST(static_cast(root_inode().ptr())->link_inode(*inode, path)); } - void DevFileSystem::for_each_device(const BAN::Function& callback) - { - LockGuard _(m_device_lock); - for_each_inode( - [&](BAN::RefPtr inode) - { - if (!inode->is_device()) - return BAN::Iteration::Continue; - return callback(static_cast(inode.ptr())); - } - ); - } - dev_t DevFileSystem::get_next_dev() const { LockGuard _(m_device_lock); diff --git a/kernel/kernel/PCI.cpp b/kernel/kernel/PCI.cpp index f93d9425..a869f79f 100644 --- a/kernel/kernel/PCI.cpp +++ b/kernel/kernel/PCI.cpp @@ -172,12 +172,8 @@ namespace Kernel::PCI dprintln("ATA: {}", res.error()); break; case 0x08: - // FIXME: HACK if inode initialization fails before it attaches to DevFS, - // it will kernel panic. This is used to make nvme eternal if (auto res = NVMeController::create(pci_device); res.is_error()) dprintln("NVMe: {}", res.error()); - else - res.value()->ref(); break; default: dprintln("unsupported storage device (pci {2H}.{2H}.{2H})", pci_device.class_code(), pci_device.subclass(), pci_device.prog_if());