DiskBackedFileSystem.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "DiskBackedFileSystem.h"
  2. #include "i386.h"
  3. #include <AK/InlineLRUCache.h>
  4. //#define DBFS_DEBUG
  5. struct BlockIdentifier {
  6. unsigned fsid { 0 };
  7. unsigned index { 0 };
  8. bool operator==(const BlockIdentifier& other) const { return fsid == other.fsid && index == other.index; }
  9. };
  10. namespace AK {
  11. template<>
  12. struct Traits<BlockIdentifier> {
  13. static unsigned hash(const BlockIdentifier& block_id) { return pair_int_hash(block_id.fsid, block_id.index); }
  14. static void dump(const BlockIdentifier& block_id) { kprintf("[block %02u:%08u]", block_id.fsid, block_id.index); }
  15. };
  16. }
  17. class CachedBlock : public InlineLinkedListNode<CachedBlock> {
  18. public:
  19. CachedBlock(const BlockIdentifier& block_id, const ByteBuffer& buffer)
  20. : m_key(block_id)
  21. , m_buffer(buffer)
  22. {
  23. }
  24. BlockIdentifier m_key;
  25. CachedBlock* m_next { nullptr };
  26. CachedBlock* m_prev { nullptr };
  27. ByteBuffer m_buffer;
  28. };
  29. Lockable<InlineLRUCache<BlockIdentifier, CachedBlock>>& block_cache()
  30. {
  31. static Lockable<InlineLRUCache<BlockIdentifier, CachedBlock>>* s_cache;
  32. if (!s_cache)
  33. s_cache = new Lockable<InlineLRUCache<BlockIdentifier, CachedBlock>>;
  34. return *s_cache;
  35. }
  36. DiskBackedFS::DiskBackedFS(Retained<DiskDevice>&& device)
  37. : m_device(move(device))
  38. {
  39. }
  40. DiskBackedFS::~DiskBackedFS()
  41. {
  42. }
  43. bool DiskBackedFS::write_block(unsigned index, const ByteBuffer& data)
  44. {
  45. #ifdef DBFS_DEBUG
  46. kprintf("DiskBackedFileSystem::write_block %u, size=%u\n", index, data.size());
  47. #endif
  48. ASSERT(data.size() == block_size());
  49. {
  50. LOCKER(block_cache().lock());
  51. if (auto* cached_block = block_cache().resource().get({ fsid(), index }))
  52. cached_block->m_buffer = data;
  53. }
  54. DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
  55. return device().write(base_offset, block_size(), data.pointer());
  56. }
  57. bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const ByteBuffer& data)
  58. {
  59. #ifdef DBFS_DEBUG
  60. kprintf("DiskBackedFileSystem::write_blocks %u x%u\n", index, count);
  61. #endif
  62. // FIXME: Maybe reorder this so we send out the write commands before updating cache?
  63. {
  64. LOCKER(block_cache().lock());
  65. for (unsigned i = 0; i < count; ++i) {
  66. if (auto* cached_block = block_cache().resource().get({ fsid(), index + i }))
  67. cached_block->m_buffer = data.slice(i * block_size(), block_size());
  68. }
  69. }
  70. DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
  71. return device().write(base_offset, count * block_size(), data.pointer());
  72. }
  73. ByteBuffer DiskBackedFS::read_block(unsigned index) const
  74. {
  75. #ifdef DBFS_DEBUG
  76. kprintf("DiskBackedFileSystem::read_block %u\n", index);
  77. #endif
  78. {
  79. LOCKER(block_cache().lock());
  80. if (auto* cached_block = block_cache().resource().get({ fsid(), index }))
  81. return cached_block->m_buffer;
  82. }
  83. auto buffer = ByteBuffer::create_uninitialized(block_size());
  84. //kprintf("created block buffer with size %u\n", block_size());
  85. DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
  86. auto* buffer_pointer = buffer.pointer();
  87. bool success = device().read(base_offset, block_size(), buffer_pointer);
  88. ASSERT(success);
  89. ASSERT(buffer.size() == block_size());
  90. {
  91. LOCKER(block_cache().lock());
  92. block_cache().resource().put({ fsid(), index }, CachedBlock({ fsid(), index }, buffer));
  93. }
  94. return buffer;
  95. }
  96. ByteBuffer DiskBackedFS::read_blocks(unsigned index, unsigned count) const
  97. {
  98. if (!count)
  99. return nullptr;
  100. if (count == 1)
  101. return read_block(index);
  102. auto blocks = ByteBuffer::create_uninitialized(count * block_size());
  103. byte* out = blocks.pointer();
  104. for (unsigned i = 0; i < count; ++i) {
  105. auto block = read_block(index + i);
  106. if (!block)
  107. return nullptr;
  108. memcpy(out, block.pointer(), block.size());
  109. out += block_size();
  110. }
  111. return blocks;
  112. }
  113. void DiskBackedFS::set_block_size(unsigned block_size)
  114. {
  115. if (block_size == m_block_size)
  116. return;
  117. m_block_size = block_size;
  118. }