update main #1

Merged
Sinipelto merged 240 commits from Bananymous/banan-os:main into main 2023-11-20 13:20:51 +02:00
2 changed files with 30 additions and 0 deletions
Showing only changes of commit 8b4f661acb - Show all commits

View File

@ -4,6 +4,7 @@
#include <BAN/Iteration.h> #include <BAN/Iteration.h>
#include <kernel/FS/FileSystem.h> #include <kernel/FS/FileSystem.h>
#include <kernel/FS/TmpFS/Inode.h> #include <kernel/FS/TmpFS/Inode.h>
#include <kernel/LockGuard.h>
#include <kernel/Memory/PageTable.h> #include <kernel/Memory/PageTable.h>
#include <kernel/SpinLock.h> #include <kernel/SpinLock.h>
@ -140,6 +141,7 @@ namespace Kernel
template<TmpFuncs::with_block_buffer_callback F> template<TmpFuncs::with_block_buffer_callback F>
void TmpFileSystem::with_block_buffer(size_t index, F callback) void TmpFileSystem::with_block_buffer(size_t index, F callback)
{ {
LockGuard _(m_lock);
paddr_t block_paddr = find_block(index); paddr_t block_paddr = find_block(index);
PageTable::with_fast_page(block_paddr, [&] { PageTable::with_fast_page(block_paddr, [&] {
BAN::ByteSpan buffer(reinterpret_cast<uint8_t*>(PageTable::fast_page()), PAGE_SIZE); BAN::ByteSpan buffer(reinterpret_cast<uint8_t*>(PageTable::fast_page()), PAGE_SIZE);

View File

@ -49,6 +49,8 @@ namespace Kernel
BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpFileSystem::open_inode(ino_t ino) BAN::ErrorOr<BAN::RefPtr<TmpInode>> TmpFileSystem::open_inode(ino_t ino)
{ {
LockGuard _(m_lock);
if (m_inode_cache.contains(ino)) if (m_inode_cache.contains(ino))
return m_inode_cache[ino]; return m_inode_cache[ino];
@ -66,6 +68,8 @@ namespace Kernel
BAN::ErrorOr<void> TmpFileSystem::add_to_cache(BAN::RefPtr<TmpInode> inode) BAN::ErrorOr<void> TmpFileSystem::add_to_cache(BAN::RefPtr<TmpInode> inode)
{ {
LockGuard _(m_lock);
if (!m_inode_cache.contains(inode->ino())) if (!m_inode_cache.contains(inode->ino()))
TRY(m_inode_cache.insert(inode->ino(), inode)); TRY(m_inode_cache.insert(inode->ino(), inode));
return {}; return {};
@ -73,12 +77,16 @@ namespace Kernel
void TmpFileSystem::remove_from_cache(BAN::RefPtr<TmpInode> inode) void TmpFileSystem::remove_from_cache(BAN::RefPtr<TmpInode> inode)
{ {
LockGuard _(m_lock);
ASSERT(m_inode_cache.contains(inode->ino())); ASSERT(m_inode_cache.contains(inode->ino()));
m_inode_cache.remove(inode->ino()); m_inode_cache.remove(inode->ino());
} }
void TmpFileSystem::read_inode(ino_t ino, TmpInodeInfo& out) void TmpFileSystem::read_inode(ino_t ino, TmpInodeInfo& out)
{ {
LockGuard _(m_lock);
auto inode_location = find_inode(ino); auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] { PageTable::with_fast_page(inode_location.paddr, [&] {
out = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index); out = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -87,6 +95,8 @@ namespace Kernel
void TmpFileSystem::write_inode(ino_t ino, const TmpInodeInfo& info) void TmpFileSystem::write_inode(ino_t ino, const TmpInodeInfo& info)
{ {
LockGuard _(m_lock);
auto inode_location = find_inode(ino); auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] { PageTable::with_fast_page(inode_location.paddr, [&] {
auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index); auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -96,6 +106,8 @@ namespace Kernel
void TmpFileSystem::delete_inode(ino_t ino) void TmpFileSystem::delete_inode(ino_t ino)
{ {
LockGuard _(m_lock);
auto inode_location = find_inode(ino); auto inode_location = find_inode(ino);
PageTable::with_fast_page(inode_location.paddr, [&] { PageTable::with_fast_page(inode_location.paddr, [&] {
auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index); auto& inode_info = PageTable::fast_page_as_sized<TmpInodeInfo>(inode_location.index);
@ -109,6 +121,8 @@ namespace Kernel
BAN::ErrorOr<ino_t> TmpFileSystem::allocate_inode(const TmpInodeInfo& info) BAN::ErrorOr<ino_t> TmpFileSystem::allocate_inode(const TmpInodeInfo& info)
{ {
LockGuard _(m_lock);
constexpr size_t inodes_per_page = PAGE_SIZE / sizeof(TmpInodeInfo); constexpr size_t inodes_per_page = PAGE_SIZE / sizeof(TmpInodeInfo);
ino_t ino = first_inode; ino_t ino = first_inode;
@ -133,6 +147,8 @@ namespace Kernel
TmpFileSystem::InodeLocation TmpFileSystem::find_inode(ino_t ino) TmpFileSystem::InodeLocation TmpFileSystem::find_inode(ino_t ino)
{ {
LockGuard _(m_lock);
ASSERT_GTE(ino, first_inode); ASSERT_GTE(ino, first_inode);
ASSERT_LT(ino, max_inodes); ASSERT_LT(ino, max_inodes);
@ -149,6 +165,8 @@ namespace Kernel
void TmpFileSystem::free_block(size_t index) void TmpFileSystem::free_block(size_t index)
{ {
LockGuard _(m_lock);
constexpr size_t addresses_per_page = PAGE_SIZE / sizeof(PageInfo); constexpr size_t addresses_per_page = PAGE_SIZE / sizeof(PageInfo);
const size_t index_of_page = (index - first_data_page) / addresses_per_page; const size_t index_of_page = (index - first_data_page) / addresses_per_page;
@ -167,6 +185,8 @@ namespace Kernel
BAN::ErrorOr<size_t> TmpFileSystem::allocate_block() BAN::ErrorOr<size_t> TmpFileSystem::allocate_block()
{ {
LockGuard _(m_lock);
size_t result = first_data_page; size_t result = first_data_page;
TRY(for_each_indirect_paddr_allocating(m_data_pages, [&] (paddr_t paddr, bool allocated) { TRY(for_each_indirect_paddr_allocating(m_data_pages, [&] (paddr_t paddr, bool allocated) {
if (allocated) if (allocated)
@ -179,12 +199,16 @@ namespace Kernel
paddr_t TmpFileSystem::find_block(size_t index) paddr_t TmpFileSystem::find_block(size_t index)
{ {
LockGuard _(m_lock);
ASSERT_GT(index, 0); ASSERT_GT(index, 0);
return find_indirect(m_data_pages, index - first_data_page, 3); return find_indirect(m_data_pages, index - first_data_page, 3);
} }
paddr_t TmpFileSystem::find_indirect(PageInfo root, size_t index, size_t depth) paddr_t TmpFileSystem::find_indirect(PageInfo root, size_t index, size_t depth)
{ {
LockGuard _(m_lock);
ASSERT(root.flags() & PageInfo::Flags::Present); ASSERT(root.flags() & PageInfo::Flags::Present);
if (depth == 0) if (depth == 0)
{ {
@ -214,6 +238,8 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F> template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<BAN::Iteration> TmpFileSystem::for_each_indirect_paddr_allocating_internal(PageInfo page_info, F callback, size_t depth) BAN::ErrorOr<BAN::Iteration> 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); ASSERT(page_info.flags() & PageInfo::Flags::Present);
if (depth == 0) if (depth == 0)
{ {
@ -268,6 +294,8 @@ namespace Kernel
template<TmpFuncs::for_each_indirect_paddr_allocating_callback F> template<TmpFuncs::for_each_indirect_paddr_allocating_callback F>
BAN::ErrorOr<void> TmpFileSystem::for_each_indirect_paddr_allocating(PageInfo page_info, F callback, size_t depth) BAN::ErrorOr<void> 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)); BAN::Iteration result = TRY(for_each_indirect_paddr_allocating_internal(page_info, callback, depth));
ASSERT(result == BAN::Iteration::Break); ASSERT(result == BAN::Iteration::Break);
return {}; return {};