From 07b5920f3f3e177eeff0489b0d964eb2d92b42b6 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 7 Nov 2023 16:03:52 +0200 Subject: [PATCH] Kernel: Lock TmpFS in all its methods --- kernel/include/kernel/FS/TmpFS/FileSystem.h | 2 ++ kernel/kernel/FS/TmpFS/FileSystem.cpp | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/kernel/include/kernel/FS/TmpFS/FileSystem.h b/kernel/include/kernel/FS/TmpFS/FileSystem.h index 5205ff17..e07008b1 100644 --- a/kernel/include/kernel/FS/TmpFS/FileSystem.h +++ b/kernel/include/kernel/FS/TmpFS/FileSystem.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -140,6 +141,7 @@ namespace Kernel template void TmpFileSystem::with_block_buffer(size_t index, F callback) { + LockGuard _(m_lock); paddr_t block_paddr = find_block(index); PageTable::with_fast_page(block_paddr, [&] { BAN::ByteSpan buffer(reinterpret_cast(PageTable::fast_page()), PAGE_SIZE); diff --git a/kernel/kernel/FS/TmpFS/FileSystem.cpp b/kernel/kernel/FS/TmpFS/FileSystem.cpp index 6e2cfd81..7ab95e0d 100644 --- a/kernel/kernel/FS/TmpFS/FileSystem.cpp +++ b/kernel/kernel/FS/TmpFS/FileSystem.cpp @@ -49,6 +49,8 @@ namespace Kernel BAN::ErrorOr> TmpFileSystem::open_inode(ino_t ino) { + LockGuard _(m_lock); + if (m_inode_cache.contains(ino)) return m_inode_cache[ino]; @@ -66,6 +68,8 @@ namespace Kernel BAN::ErrorOr TmpFileSystem::add_to_cache(BAN::RefPtr inode) { + LockGuard _(m_lock); + if (!m_inode_cache.contains(inode->ino())) TRY(m_inode_cache.insert(inode->ino(), inode)); return {}; @@ -73,12 +77,16 @@ namespace Kernel void TmpFileSystem::remove_from_cache(BAN::RefPtr inode) { + LockGuard _(m_lock); + ASSERT(m_inode_cache.contains(inode->ino())); m_inode_cache.remove(inode->ino()); } void TmpFileSystem::read_inode(ino_t ino, TmpInodeInfo& out) { + LockGuard _(m_lock); + auto inode_location = find_inode(ino); PageTable::with_fast_page(inode_location.paddr, [&] { out = PageTable::fast_page_as_sized(inode_location.index); @@ -87,6 +95,8 @@ namespace Kernel void TmpFileSystem::write_inode(ino_t ino, const TmpInodeInfo& info) { + LockGuard _(m_lock); + auto inode_location = find_inode(ino); PageTable::with_fast_page(inode_location.paddr, [&] { auto& inode_info = PageTable::fast_page_as_sized(inode_location.index); @@ -96,6 +106,8 @@ namespace Kernel void TmpFileSystem::delete_inode(ino_t ino) { + LockGuard _(m_lock); + auto inode_location = find_inode(ino); PageTable::with_fast_page(inode_location.paddr, [&] { auto& inode_info = PageTable::fast_page_as_sized(inode_location.index); @@ -109,6 +121,8 @@ namespace Kernel BAN::ErrorOr TmpFileSystem::allocate_inode(const TmpInodeInfo& info) { + LockGuard _(m_lock); + constexpr size_t inodes_per_page = PAGE_SIZE / sizeof(TmpInodeInfo); ino_t ino = first_inode; @@ -133,6 +147,8 @@ namespace Kernel TmpFileSystem::InodeLocation TmpFileSystem::find_inode(ino_t ino) { + LockGuard _(m_lock); + ASSERT_GTE(ino, first_inode); ASSERT_LT(ino, max_inodes); @@ -149,6 +165,8 @@ namespace Kernel void TmpFileSystem::free_block(size_t index) { + LockGuard _(m_lock); + constexpr size_t addresses_per_page = PAGE_SIZE / sizeof(PageInfo); const size_t index_of_page = (index - first_data_page) / addresses_per_page; @@ -167,6 +185,8 @@ namespace Kernel BAN::ErrorOr TmpFileSystem::allocate_block() { + LockGuard _(m_lock); + size_t result = first_data_page; TRY(for_each_indirect_paddr_allocating(m_data_pages, [&] (paddr_t paddr, bool allocated) { if (allocated) @@ -179,12 +199,16 @@ namespace Kernel paddr_t TmpFileSystem::find_block(size_t index) { + LockGuard _(m_lock); + ASSERT_GT(index, 0); return find_indirect(m_data_pages, index - first_data_page, 3); } paddr_t TmpFileSystem::find_indirect(PageInfo root, size_t index, size_t depth) { + LockGuard _(m_lock); + ASSERT(root.flags() & PageInfo::Flags::Present); if (depth == 0) { @@ -214,6 +238,8 @@ namespace Kernel template BAN::ErrorOr TmpFileSystem::for_each_indirect_paddr_allocating_internal(PageInfo page_info, F callback, size_t depth) { + LockGuard _(m_lock); + ASSERT(page_info.flags() & PageInfo::Flags::Present); if (depth == 0) { @@ -268,6 +294,8 @@ namespace Kernel template BAN::ErrorOr TmpFileSystem::for_each_indirect_paddr_allocating(PageInfo page_info, F callback, size_t depth) { + LockGuard _(m_lock); + BAN::Iteration result = TRY(for_each_indirect_paddr_allocating_internal(page_info, callback, depth)); ASSERT(result == BAN::Iteration::Break); return {};