BlockBasedFileSystem.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  8. namespace Kernel {
  9. class BlockBasedFileSystem : public FileBackedFileSystem {
  10. public:
  11. TYPEDEF_DISTINCT_ORDERED_ID(u64, BlockIndex);
  12. virtual ~BlockBasedFileSystem() override;
  13. virtual bool initialize() override;
  14. u64 logical_block_size() const { return m_logical_block_size; };
  15. virtual void flush_writes() override;
  16. void flush_writes_impl();
  17. protected:
  18. explicit BlockBasedFileSystem(FileDescription&);
  19. KResult read_block(BlockIndex, UserOrKernelBuffer*, size_t count, size_t offset = 0, bool allow_cache = true) const;
  20. KResult read_blocks(BlockIndex, unsigned count, UserOrKernelBuffer&, bool allow_cache = true) const;
  21. bool raw_read(BlockIndex, UserOrKernelBuffer&);
  22. bool raw_write(BlockIndex, const UserOrKernelBuffer&);
  23. bool raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer&);
  24. bool raw_write_blocks(BlockIndex index, size_t count, const UserOrKernelBuffer&);
  25. KResult write_block(BlockIndex, const UserOrKernelBuffer&, size_t count, size_t offset = 0, bool allow_cache = true);
  26. KResult write_blocks(BlockIndex, unsigned count, const UserOrKernelBuffer&, bool allow_cache = true);
  27. u64 m_logical_block_size { 512 };
  28. private:
  29. DiskCache& cache() const;
  30. void flush_specific_block_if_needed(BlockIndex index);
  31. mutable Mutex m_cache_lock;
  32. mutable OwnPtr<DiskCache> m_cache;
  33. };
  34. }
  35. template<>
  36. struct AK::Formatter<Kernel::BlockBasedFileSystem::BlockIndex> : AK::Formatter<FormatString> {
  37. void format(FormatBuilder& builder, Kernel::BlockBasedFileSystem::BlockIndex value)
  38. {
  39. return AK::Formatter<FormatString>::format(builder, "{}", value.value());
  40. }
  41. };