serenity/Kernel/FileSystem/FileSystem.cpp
Liav A 0fd7b688af Kernel: Introduce support for using FileSystem object in multiple mounts
The idea is to enable mounting FileSystem objects across multiple mounts
in contrast to what happened until now - each mount has its own unique
FileSystem object being attached to it.

Considering a situation of mounting a block device at 2 different mount
points at in system, there were a couple of critical flaws due to how
the previous "design" worked:
1. BlockBasedFileSystem(s) that pointed to the same actual device had a
separate DiskCache object being attached to them. Because both instances
were not synchronized by any means, corruption of the filesystem is most
likely achieveable by a simple cache flush of either of the instances.
2. For superblock-oriented filesystems (such as the ext2 filesystem),
lack of synchronization between both instances can lead to severe
corruption in the superblock, which could render the entire filesystem
unusable.
3. Flags of a specific filesystem implementation (for example, with xfs
on Linux, one can instruct to mount it with the discard option) must be
honored across multiple mounts, to ensure expected behavior against a
particular filesystem.

This patch put the foundations to start fix the issues mentioned above.
However, there are still major issues to solve, so this is only a start.
2022-10-22 16:57:52 -04:00

83 lines
1.7 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/HashMap.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/Inode.h>
#include <Kernel/InterruptDisabler.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Net/LocalSocket.h>
namespace Kernel {
static u32 s_lastFileSystemID;
static Singleton<HashMap<FileSystemID, FileSystem*>> s_file_system_map;
static HashMap<FileSystemID, FileSystem*>& all_file_systems()
{
return *s_file_system_map;
}
FileSystem::FileSystem()
: m_fsid(++s_lastFileSystemID)
{
s_file_system_map->set(m_fsid, this);
}
FileSystem::~FileSystem()
{
s_file_system_map->remove(m_fsid);
}
FileSystem* FileSystem::from_fsid(FileSystemID id)
{
auto it = all_file_systems().find(id);
if (it != all_file_systems().end())
return (*it).value;
return nullptr;
}
ErrorOr<void> FileSystem::prepare_to_unmount()
{
return m_attach_count.with([&](auto& attach_count) -> ErrorOr<void> {
if (attach_count == 1)
return prepare_to_clear_last_mount();
return {};
});
}
FileSystem::DirectoryEntryView::DirectoryEntryView(StringView n, InodeIdentifier i, u8 ft)
: name(n)
, inode(i)
, file_type(ft)
{
}
void FileSystem::sync()
{
Inode::sync_all();
NonnullLockRefPtrVector<FileSystem, 32> file_systems;
{
InterruptDisabler disabler;
for (auto& it : all_file_systems())
file_systems.append(*it.value);
}
for (auto& fs : file_systems)
fs.flush_writes();
}
void FileSystem::lock_all()
{
for (auto& it : all_file_systems()) {
it.value->m_lock.lock();
}
}
}