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.
This commit is contained in:
Liav A 2022-08-20 09:28:02 +03:00 committed by Andrew Kaster
parent 24977996a6
commit fea3cb5ff9
Notes: sideshowbarker 2024-07-17 05:12:43 +09:00
9 changed files with 103 additions and 54 deletions

View file

@ -140,7 +140,7 @@ ErrorOr<void> Ext2FS::initialize_while_locked()
}
}
m_root_inode = static_ptr_cast<Ext2FSInode>(TRY(get_inode({ fsid(), EXT2_ROOT_INO })));
m_root_inode = TRY(build_root_inode());
return {};
}
@ -770,10 +770,29 @@ ErrorOr<void> Ext2FSInode::flush_metadata()
return {};
}
ErrorOr<NonnullLockRefPtr<Ext2FSInode>> Ext2FS::build_root_inode() const
{
MutexLocker locker(m_lock);
BlockIndex block_index;
unsigned offset;
if (!find_block_containing_inode(EXT2_ROOT_INO, block_index, offset))
return EINVAL;
auto inode = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) Ext2FSInode(const_cast<Ext2FS&>(*this), EXT2_ROOT_INO)));
auto buffer = UserOrKernelBuffer::for_kernel_buffer(reinterpret_cast<u8*>(&inode->m_raw_inode));
TRY(read_block(block_index, &buffer, sizeof(ext2_inode), offset));
return inode;
}
ErrorOr<NonnullLockRefPtr<Inode>> Ext2FS::get_inode(InodeIdentifier inode) const
{
MutexLocker locker(m_lock);
VERIFY(inode.fsid() == fsid());
VERIFY(m_root_inode);
if (inode.index() == EXT2_ROOT_INO)
return *m_root_inode;
{
auto it = m_inode_cache.find(inode.index());
@ -1690,12 +1709,12 @@ unsigned Ext2FS::free_inode_count() const
ErrorOr<void> Ext2FS::prepare_to_clear_last_mount()
{
MutexLocker locker(m_lock);
for (auto& it : m_inode_cache) {
if (it.value->ref_count() > 1)
return EBUSY;
}
BlockBasedFileSystem::remove_disk_cache_before_last_unmount();
m_inode_cache.clear();
m_root_inode = nullptr;
return {};