DiskBackedFileSystem.cpp 4.2 KB

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