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. #include <Kernel/Locking/ProtectedValue.h>
  9. namespace Kernel {
  10. class BlockBasedFileSystem : public FileBackedFileSystem {
  11. public:
  12. TYPEDEF_DISTINCT_ORDERED_ID(u64, BlockIndex);
  13. virtual ~BlockBasedFileSystem() override;
  14. virtual KResult initialize() override;
  15. u64 logical_block_size() const { return m_logical_block_size; };
  16. virtual void flush_writes() override;
  17. void flush_writes_impl();
  18. protected:
  19. explicit BlockBasedFileSystem(FileDescription&);
  20. KResult read_block(BlockIndex, UserOrKernelBuffer*, size_t count, size_t offset = 0, bool allow_cache = true) const;
  21. KResult read_blocks(BlockIndex, unsigned count, UserOrKernelBuffer&, bool allow_cache = true) const;
  22. bool raw_read(BlockIndex, UserOrKernelBuffer&);
  23. bool raw_write(BlockIndex, const UserOrKernelBuffer&);
  24. bool raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer&);
  25. bool raw_write_blocks(BlockIndex index, size_t count, const UserOrKernelBuffer&);
  26. KResult write_block(BlockIndex, const UserOrKernelBuffer&, size_t count, size_t offset = 0, bool allow_cache = true);
  27. KResult write_blocks(BlockIndex, unsigned count, const UserOrKernelBuffer&, bool allow_cache = true);
  28. u64 m_logical_block_size { 512 };
  29. private:
  30. DiskCache& cache() const;
  31. void flush_specific_block_if_needed(BlockIndex index);
  32. mutable ProtectedValue<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. };