BlockBasedFileSystem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/IntrusiveList.h>
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/FileSystem/BlockBasedFileSystem.h>
  9. #include <Kernel/Process.h>
  10. namespace Kernel {
  11. struct CacheEntry {
  12. IntrusiveListNode<CacheEntry> list_node;
  13. BlockBasedFileSystem::BlockIndex block_index { 0 };
  14. u8* data { nullptr };
  15. bool has_data { false };
  16. };
  17. class DiskCache {
  18. public:
  19. explicit DiskCache(BlockBasedFileSystem& fs)
  20. : m_fs(fs)
  21. , m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size()))
  22. , m_entries(KBuffer::create_with_size(m_entry_count * sizeof(CacheEntry)))
  23. {
  24. for (size_t i = 0; i < m_entry_count; ++i) {
  25. entries()[i].data = m_cached_block_data.data() + i * m_fs.block_size();
  26. m_clean_list.append(entries()[i]);
  27. }
  28. }
  29. ~DiskCache() = default;
  30. bool is_dirty() const { return m_dirty; }
  31. void set_dirty(bool b) { m_dirty = b; }
  32. void mark_all_clean()
  33. {
  34. while (auto* entry = m_dirty_list.first())
  35. m_clean_list.prepend(*entry);
  36. m_dirty = false;
  37. }
  38. void mark_dirty(CacheEntry& entry)
  39. {
  40. m_dirty_list.prepend(entry);
  41. m_dirty = true;
  42. }
  43. void mark_clean(CacheEntry& entry)
  44. {
  45. m_clean_list.prepend(entry);
  46. }
  47. CacheEntry& get(BlockBasedFileSystem::BlockIndex block_index) const
  48. {
  49. if (auto it = m_hash.find(block_index); it != m_hash.end()) {
  50. auto& entry = const_cast<CacheEntry&>(*it->value);
  51. VERIFY(entry.block_index == block_index);
  52. return entry;
  53. }
  54. if (m_clean_list.is_empty()) {
  55. // Not a single clean entry! Flush writes and try again.
  56. // NOTE: We want to make sure we only call FileBackedFileSystem flush here,
  57. // not some FileBackedFileSystem subclass flush!
  58. m_fs.flush_writes_impl();
  59. return get(block_index);
  60. }
  61. VERIFY(m_clean_list.last());
  62. auto& new_entry = *m_clean_list.last();
  63. m_clean_list.prepend(new_entry);
  64. m_hash.remove(new_entry.block_index);
  65. m_hash.set(block_index, &new_entry);
  66. new_entry.block_index = block_index;
  67. new_entry.has_data = false;
  68. return new_entry;
  69. }
  70. const CacheEntry* entries() const { return (const CacheEntry*)m_entries.data(); }
  71. CacheEntry* entries() { return (CacheEntry*)m_entries.data(); }
  72. template<typename Callback>
  73. void for_each_dirty_entry(Callback callback)
  74. {
  75. for (auto& entry : m_dirty_list)
  76. callback(entry);
  77. }
  78. private:
  79. BlockBasedFileSystem& m_fs;
  80. size_t m_entry_count { 10000 };
  81. mutable HashMap<BlockBasedFileSystem::BlockIndex, CacheEntry*> m_hash;
  82. mutable IntrusiveList<CacheEntry, RawPtr<CacheEntry>, &CacheEntry::list_node> m_clean_list;
  83. mutable IntrusiveList<CacheEntry, RawPtr<CacheEntry>, &CacheEntry::list_node> m_dirty_list;
  84. KBuffer m_cached_block_data;
  85. KBuffer m_entries;
  86. bool m_dirty { false };
  87. };
  88. BlockBasedFileSystem::BlockBasedFileSystem(FileDescription& file_description)
  89. : FileBackedFileSystem(file_description)
  90. {
  91. VERIFY(file_description.file().is_seekable());
  92. }
  93. BlockBasedFileSystem::~BlockBasedFileSystem()
  94. {
  95. }
  96. KResult BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBuffer& data, size_t count, size_t offset, bool allow_cache)
  97. {
  98. Locker locker(m_lock);
  99. VERIFY(m_logical_block_size);
  100. VERIFY(offset + count <= block_size());
  101. dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_block {}, size={}", index, count);
  102. if (!allow_cache) {
  103. flush_specific_block_if_needed(index);
  104. auto base_offset = index.value() * block_size() + offset;
  105. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  106. if (seek_result.is_error())
  107. return seek_result.error();
  108. auto nwritten = file_description().write(data, count);
  109. if (nwritten.is_error())
  110. return nwritten.error();
  111. VERIFY(nwritten.value() == count);
  112. return KSuccess;
  113. }
  114. auto& entry = cache().get(index);
  115. if (count < block_size()) {
  116. // Fill the cache first.
  117. auto result = read_block(index, nullptr, block_size());
  118. if (result.is_error())
  119. return result;
  120. }
  121. if (!data.read(entry.data + offset, count))
  122. return EFAULT;
  123. cache().mark_dirty(entry);
  124. entry.has_data = true;
  125. return KSuccess;
  126. }
  127. bool BlockBasedFileSystem::raw_read(BlockIndex index, UserOrKernelBuffer& buffer)
  128. {
  129. Locker locker(m_lock);
  130. auto base_offset = index.value() * m_logical_block_size;
  131. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  132. VERIFY(!seek_result.is_error());
  133. auto nread = file_description().read(buffer, m_logical_block_size);
  134. VERIFY(!nread.is_error());
  135. VERIFY(nread.value() == m_logical_block_size);
  136. return true;
  137. }
  138. bool BlockBasedFileSystem::raw_write(BlockIndex index, const UserOrKernelBuffer& buffer)
  139. {
  140. Locker locker(m_lock);
  141. auto base_offset = index.value() * m_logical_block_size;
  142. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  143. VERIFY(!seek_result.is_error());
  144. auto nwritten = file_description().write(buffer, m_logical_block_size);
  145. VERIFY(!nwritten.is_error());
  146. VERIFY(nwritten.value() == m_logical_block_size);
  147. return true;
  148. }
  149. bool BlockBasedFileSystem::raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer& buffer)
  150. {
  151. Locker locker(m_lock);
  152. auto current = buffer;
  153. for (auto block = index.value(); block < (index.value() + count); block++) {
  154. if (!raw_read(BlockIndex { block }, current))
  155. return false;
  156. current = current.offset(logical_block_size());
  157. }
  158. return true;
  159. }
  160. bool BlockBasedFileSystem::raw_write_blocks(BlockIndex index, size_t count, const UserOrKernelBuffer& buffer)
  161. {
  162. Locker locker(m_lock);
  163. auto current = buffer;
  164. for (auto block = index.value(); block < (index.value() + count); block++) {
  165. if (!raw_write(block, current))
  166. return false;
  167. current = current.offset(logical_block_size());
  168. }
  169. return true;
  170. }
  171. KResult BlockBasedFileSystem::write_blocks(BlockIndex index, unsigned count, const UserOrKernelBuffer& data, bool allow_cache)
  172. {
  173. Locker locker(m_lock);
  174. VERIFY(m_logical_block_size);
  175. dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_blocks {}, count={}", index, count);
  176. for (unsigned i = 0; i < count; ++i) {
  177. auto result = write_block(BlockIndex { index.value() + i }, data.offset(i * block_size()), block_size(), 0, allow_cache);
  178. if (result.is_error())
  179. return result;
  180. }
  181. return KSuccess;
  182. }
  183. KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* buffer, size_t count, size_t offset, bool allow_cache) const
  184. {
  185. Locker locker(m_lock);
  186. VERIFY(m_logical_block_size);
  187. VERIFY(offset + count <= block_size());
  188. dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::read_block {}", index);
  189. if (!allow_cache) {
  190. const_cast<BlockBasedFileSystem*>(this)->flush_specific_block_if_needed(index);
  191. auto base_offset = index.value() * block_size() + offset;
  192. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  193. if (seek_result.is_error())
  194. return seek_result.error();
  195. auto nread = file_description().read(*buffer, count);
  196. if (nread.is_error())
  197. return nread.error();
  198. VERIFY(nread.value() == count);
  199. return KSuccess;
  200. }
  201. auto& entry = cache().get(index);
  202. if (!entry.has_data) {
  203. auto base_offset = index.value() * block_size();
  204. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  205. if (seek_result.is_error())
  206. return seek_result.error();
  207. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  208. auto nread = file_description().read(entry_data_buffer, block_size());
  209. if (nread.is_error())
  210. return nread.error();
  211. VERIFY(nread.value() == block_size());
  212. entry.has_data = true;
  213. }
  214. if (buffer && !buffer->write(entry.data + offset, count))
  215. return EFAULT;
  216. return KSuccess;
  217. }
  218. KResult BlockBasedFileSystem::read_blocks(BlockIndex index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const
  219. {
  220. Locker locker(m_lock);
  221. VERIFY(m_logical_block_size);
  222. if (!count)
  223. return EINVAL;
  224. if (count == 1)
  225. return read_block(index, &buffer, block_size(), 0, allow_cache);
  226. auto out = buffer;
  227. for (unsigned i = 0; i < count; ++i) {
  228. auto result = read_block(BlockIndex { index.value() + i }, &out, block_size(), 0, allow_cache);
  229. if (result.is_error())
  230. return result;
  231. out = out.offset(block_size());
  232. }
  233. return KSuccess;
  234. }
  235. void BlockBasedFileSystem::flush_specific_block_if_needed(BlockIndex index)
  236. {
  237. Locker locker(m_lock);
  238. if (!cache().is_dirty())
  239. return;
  240. Vector<CacheEntry*, 32> cleaned_entries;
  241. cache().for_each_dirty_entry([&](CacheEntry& entry) {
  242. if (entry.block_index != index) {
  243. size_t base_offset = entry.block_index.value() * block_size();
  244. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  245. VERIFY(!seek_result.is_error());
  246. // FIXME: Should this error path be surfaced somehow?
  247. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  248. [[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
  249. cleaned_entries.append(&entry);
  250. }
  251. });
  252. // NOTE: We make a separate pass to mark entries clean since marking them clean
  253. // moves them out of the dirty list which would disturb the iteration above.
  254. for (auto* entry : cleaned_entries)
  255. cache().mark_clean(*entry);
  256. }
  257. void BlockBasedFileSystem::flush_writes_impl()
  258. {
  259. Locker locker(m_lock);
  260. if (!cache().is_dirty())
  261. return;
  262. u32 count = 0;
  263. cache().for_each_dirty_entry([&](CacheEntry& entry) {
  264. auto base_offset = entry.block_index.value() * block_size();
  265. auto seek_result = file_description().seek(base_offset, SEEK_SET);
  266. VERIFY(!seek_result.is_error());
  267. // FIXME: Should this error path be surfaced somehow?
  268. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  269. [[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
  270. ++count;
  271. });
  272. cache().mark_all_clean();
  273. dbgln("{}: Flushed {} blocks to disk", class_name(), count);
  274. }
  275. void BlockBasedFileSystem::flush_writes()
  276. {
  277. flush_writes_impl();
  278. }
  279. DiskCache& BlockBasedFileSystem::cache() const
  280. {
  281. if (!m_cache)
  282. m_cache = make<DiskCache>(const_cast<BlockBasedFileSystem&>(*this));
  283. return *m_cache;
  284. }
  285. }