serenity/Kernel/FileSystem/FileSystem.cpp
Liav A fea3cb5ff9 Kernel/FileSystem: Discard safely filesystems when unmounted last time
This commit reached that goal of "safely discarding" a filesystem by
doing the following:
1. Stop using the s_file_system_map HashMap as it was an unsafe measure
to access pointers of FileSystems. Instead, make sure to register all
FileSystems at the VFS layer, with an IntrusiveList, to avoid problems
related to OOM conditions.
2. Make sure to cleanly remove the DiskCache object from a BlockBased
filesystem, so the destructor of such object will not need to do that in
the destruction point.
3. For ext2 filesystems, don't cache the root inode at m_inode_cache
HashMap. The reason for this is that when unmounting an ext2 filesystem,
we lookup at the cache to see if there's a reference to a cached inode
and if that's the case, we fail with EBUSY. If we keep the m_root_inode
also being referenced at the m_inode_cache map, we have 2 references to
that object, which will lead to fail with EBUSY. Also, it's much simpler
to always ask for a root inode and get it immediately from m_root_inode,
instead of looking up the cache for that inode.
2022-10-22 16:57:52 -04:00

58 lines
1.1 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/FileSystem/VirtualFileSystem.h>
#include <Kernel/InterruptDisabler.h>
#include <Kernel/Memory/MemoryManager.h>
#include <Kernel/Net/LocalSocket.h>
namespace Kernel {
static u32 s_lastFileSystemID;
FileSystem::FileSystem()
: m_fsid(++s_lastFileSystemID)
{
}
FileSystem::~FileSystem()
{
}
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();
VirtualFileSystem::the().sync_filesystems();
}
void FileSystem::lock_all()
{
VirtualFileSystem::the().lock_all_filesystems();
}
}