ladybird/Kernel/FileSystem/BlockBasedFileSystem.h
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

51 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/FileSystem/FileBackedFileSystem.h>
#include <Kernel/Locking/MutexProtected.h>
namespace Kernel {
class BlockBasedFileSystem : public FileBackedFileSystem {
public:
AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, BlockIndex);
virtual ~BlockBasedFileSystem() override;
u64 logical_block_size() const { return m_logical_block_size; };
virtual void flush_writes() override;
void flush_writes_impl();
protected:
explicit BlockBasedFileSystem(OpenFileDescription&);
virtual ErrorOr<void> initialize_while_locked() override;
ErrorOr<void> read_block(BlockIndex, UserOrKernelBuffer*, size_t count, u64 offset = 0, bool allow_cache = true) const;
ErrorOr<void> read_blocks(BlockIndex, unsigned count, UserOrKernelBuffer&, bool allow_cache = true) const;
ErrorOr<void> raw_read(BlockIndex, UserOrKernelBuffer&);
ErrorOr<void> raw_write(BlockIndex, UserOrKernelBuffer const&);
ErrorOr<void> raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer&);
ErrorOr<void> raw_write_blocks(BlockIndex index, size_t count, UserOrKernelBuffer const&);
ErrorOr<void> write_block(BlockIndex, UserOrKernelBuffer const&, size_t count, u64 offset = 0, bool allow_cache = true);
ErrorOr<void> write_blocks(BlockIndex, unsigned count, UserOrKernelBuffer const&, bool allow_cache = true);
u64 m_logical_block_size { 512 };
private:
DiskCache& cache() const;
void flush_specific_block_if_needed(BlockIndex index);
mutable MutexProtected<OwnPtr<DiskCache>> m_cache;
};
}