BlockBasedFileSystem.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/IntrusiveList.h>
  27. #include <Kernel/FileSystem/BlockBasedFileSystem.h>
  28. #include <Kernel/Process.h>
  29. //#define BBFS_DEBUG
  30. namespace Kernel {
  31. class DiskCache {
  32. public:
  33. explicit DiskCache(BlockBasedFS& fs)
  34. : m_fs(fs)
  35. , m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size()))
  36. , m_entries(KBuffer::create_with_size(m_entry_count * sizeof(BlockBasedFS::CacheEntry&)))
  37. {
  38. for (size_t i = 0; i < m_entry_count; ++i) {
  39. entries()[i].data = m_cached_block_data.data() + i * m_fs.block_size();
  40. m_clean_list.append(entries()[i]);
  41. }
  42. }
  43. ~DiskCache() { }
  44. bool is_dirty() const { return m_dirty; }
  45. void set_dirty(bool b) { m_dirty = b; }
  46. void mark_all_clean()
  47. {
  48. while (auto* entry = m_dirty_list.first())
  49. m_clean_list.prepend(*entry);
  50. m_dirty = false;
  51. }
  52. void mark_dirty(BlockBasedFS::CacheEntry& entry)
  53. {
  54. m_dirty_list.prepend(entry);
  55. m_dirty = true;
  56. }
  57. void mark_clean(BlockBasedFS::CacheEntry& entry)
  58. {
  59. m_clean_list.prepend(entry);
  60. }
  61. BlockBasedFS::CacheEntry& get(u32 block_index) const
  62. {
  63. if (auto it = m_hash.find(block_index); it != m_hash.end()) {
  64. auto& entry = const_cast<BlockBasedFS::CacheEntry&>(*it->value);
  65. ASSERT(entry.block_index == block_index);
  66. return entry;
  67. }
  68. if (m_clean_list.is_empty()) {
  69. // Not a single clean entry! Flush writes and try again.
  70. // NOTE: We want to make sure we only call FileBackedFS flush here,
  71. // not some FileBackedFS subclass flush!
  72. m_fs.flush_writes_impl();
  73. return get(block_index);
  74. }
  75. ASSERT(m_clean_list.last());
  76. auto& new_entry = *m_clean_list.last();
  77. m_clean_list.prepend(new_entry);
  78. m_hash.remove(new_entry.block_index);
  79. m_hash.set(block_index, &new_entry);
  80. new_entry.block_index = block_index;
  81. new_entry.has_data = false;
  82. return new_entry;
  83. }
  84. const BlockBasedFS::CacheEntry* entries() const { return (const BlockBasedFS::CacheEntry*)m_entries.data(); }
  85. BlockBasedFS::CacheEntry* entries() { return (BlockBasedFS::CacheEntry*)m_entries.data(); }
  86. template<typename Callback>
  87. void for_each_clean_entry(Callback callback)
  88. {
  89. for (auto& entry : m_clean_list)
  90. callback(entry);
  91. }
  92. template<typename Callback>
  93. void for_each_dirty_entry(Callback callback)
  94. {
  95. for (auto& entry : m_dirty_list)
  96. callback(entry);
  97. }
  98. private:
  99. BlockBasedFS& m_fs;
  100. size_t m_entry_count { 10000 };
  101. mutable HashMap<u32, BlockBasedFS::CacheEntry*> m_hash;
  102. mutable IntrusiveList<BlockBasedFS::CacheEntry, &BlockBasedFS::CacheEntry::list_node> m_clean_list;
  103. mutable IntrusiveList<BlockBasedFS::CacheEntry, &BlockBasedFS::CacheEntry::list_node> m_dirty_list;
  104. KBuffer m_cached_block_data;
  105. KBuffer m_entries;
  106. bool m_dirty { false };
  107. };
  108. BlockBasedFS::BlockBasedFS(FileDescription& file_description)
  109. : FileBackedFS(file_description)
  110. {
  111. ASSERT(file_description.file().is_seekable());
  112. }
  113. BlockBasedFS::~BlockBasedFS()
  114. {
  115. }
  116. int BlockBasedFS::write_block(unsigned index, const UserOrKernelBuffer& data, size_t count, size_t offset, bool allow_cache)
  117. {
  118. ASSERT(m_logical_block_size);
  119. ASSERT(offset + count <= block_size());
  120. #ifdef BBFS_DEBUG
  121. klog() << "BlockBasedFileSystem::write_block " << index << ", size=" << count;
  122. #endif
  123. if (!allow_cache) {
  124. flush_specific_block_if_needed(index);
  125. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size()) + offset;
  126. file_description().seek(base_offset, SEEK_SET);
  127. auto nwritten = file_description().write(data, count);
  128. if (nwritten.is_error())
  129. return -EIO; // TODO: Return error code as-is, could be -EFAULT!
  130. ASSERT(nwritten.value() == count);
  131. return 0;
  132. }
  133. auto entry_or_error = cache_block(index);
  134. if (entry_or_error.is_error())
  135. return -EIO;
  136. if (!entry_or_error.value().has_data)
  137. return -EIO;
  138. if (count < block_size()) {
  139. // Fill the cache first.
  140. if (!force_cache_block(index))
  141. return -EIO;
  142. }
  143. if (!data.read(entry_or_error.value().data + offset, count))
  144. return -EFAULT;
  145. cache().mark_dirty(entry_or_error.value());
  146. // Update the actual entry and not the copy of it!
  147. cache().get(index).has_data = true;
  148. return 0;
  149. }
  150. bool BlockBasedFS::force_cache_block(unsigned index) const
  151. {
  152. auto& entry = cache().get(index);
  153. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  154. file_description().seek(base_offset, SEEK_SET);
  155. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  156. auto nread = file_description().read(entry_data_buffer, block_size());
  157. if (nread.is_error())
  158. return false;
  159. ASSERT(nread.value() == block_size());
  160. entry.has_data = true;
  161. return true;
  162. }
  163. KResultOr<BlockBasedFS::CacheEntry> BlockBasedFS::cache_block(unsigned index) const
  164. {
  165. auto& entry = cache().get(index);
  166. if (!entry.has_data) {
  167. if (!force_cache_block(index))
  168. return KResult(-EIO);
  169. }
  170. return entry;
  171. }
  172. bool BlockBasedFS::raw_read(unsigned index, UserOrKernelBuffer& buffer)
  173. {
  174. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  175. file_description().seek(base_offset, SEEK_SET);
  176. auto nread = file_description().read(buffer, m_logical_block_size);
  177. ASSERT(!nread.is_error());
  178. ASSERT(nread.value() == m_logical_block_size);
  179. return true;
  180. }
  181. bool BlockBasedFS::raw_write(unsigned index, const UserOrKernelBuffer& buffer)
  182. {
  183. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  184. file_description().seek(base_offset, SEEK_SET);
  185. auto nwritten = file_description().write(buffer, m_logical_block_size);
  186. ASSERT(!nwritten.is_error());
  187. ASSERT(nwritten.value() == m_logical_block_size);
  188. return true;
  189. }
  190. bool BlockBasedFS::raw_read_blocks(unsigned index, size_t count, UserOrKernelBuffer& buffer)
  191. {
  192. auto current = buffer;
  193. for (unsigned block = index; block < (index + count); block++) {
  194. if (!raw_read(block, current))
  195. return false;
  196. current = current.offset(logical_block_size());
  197. }
  198. return true;
  199. }
  200. bool BlockBasedFS::raw_write_blocks(unsigned index, size_t count, const UserOrKernelBuffer& buffer)
  201. {
  202. auto current = buffer;
  203. for (unsigned block = index; block < (index + count); block++) {
  204. if (!raw_write(block, current))
  205. return false;
  206. current = current.offset(logical_block_size());
  207. }
  208. return true;
  209. }
  210. int BlockBasedFS::write_blocks(unsigned index, unsigned count, const UserOrKernelBuffer& data, bool allow_cache)
  211. {
  212. ASSERT(m_logical_block_size);
  213. #ifdef BBFS_DEBUG
  214. klog() << "BlockBasedFileSystem::write_blocks " << index << " x" << count;
  215. #endif
  216. for (unsigned i = 0; i < count; ++i)
  217. write_block(index + i, data.offset(i * block_size()), block_size(), 0, allow_cache);
  218. return 0;
  219. }
  220. int BlockBasedFS::read_block(unsigned index, UserOrKernelBuffer& buffer, size_t count, size_t offset, bool allow_cache) const
  221. {
  222. ASSERT(m_logical_block_size);
  223. ASSERT(offset + count <= block_size());
  224. #ifdef BBFS_DEBUG
  225. klog() << "BlockBasedFileSystem::read_block " << index;
  226. #endif
  227. if (!allow_cache) {
  228. const_cast<BlockBasedFS*>(this)->flush_specific_block_if_needed(index);
  229. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size()) + static_cast<u32>(offset);
  230. file_description().seek(base_offset, SEEK_SET);
  231. auto nread = file_description().read(buffer, count);
  232. if (nread.is_error())
  233. return -EIO;
  234. ASSERT(nread.value() == count);
  235. return 0;
  236. }
  237. auto entry_or_error = cache_block(index);
  238. if (entry_or_error.is_error())
  239. return -EIO;
  240. if (!entry_or_error.value().has_data)
  241. return -EIO;
  242. if (!buffer.write(entry_or_error.value().data + offset, count))
  243. return -EFAULT;
  244. return 0;
  245. }
  246. int BlockBasedFS::read_blocks(unsigned index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const
  247. {
  248. ASSERT(m_logical_block_size);
  249. if (!count)
  250. return false;
  251. if (count == 1)
  252. return read_block(index, buffer, block_size(), 0, allow_cache);
  253. auto out = buffer;
  254. for (unsigned i = 0; i < count; ++i) {
  255. auto err = read_block(index + i, out, block_size(), 0, allow_cache);
  256. if (err < 0)
  257. return err;
  258. out = out.offset(block_size());
  259. }
  260. return 0;
  261. }
  262. void BlockBasedFS::flush_specific_block_if_needed(unsigned index)
  263. {
  264. LOCKER(m_lock);
  265. if (!cache().is_dirty())
  266. return;
  267. Vector<CacheEntry*, 32> cleaned_entries;
  268. cache().for_each_dirty_entry([&](BlockBasedFS::CacheEntry& entry) {
  269. if (entry.block_index != index) {
  270. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  271. file_description().seek(base_offset, SEEK_SET);
  272. // FIXME: Should this error path be surfaced somehow?
  273. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  274. [[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
  275. cleaned_entries.append(&entry);
  276. }
  277. });
  278. // NOTE: We make a separate pass to mark entries clean since marking them clean
  279. // moves them out of the dirty list which would disturb the iteration above.
  280. for (auto* entry : cleaned_entries)
  281. cache().mark_clean(*entry);
  282. }
  283. void BlockBasedFS::flush_writes_impl()
  284. {
  285. LOCKER(m_lock);
  286. if (!cache().is_dirty())
  287. return;
  288. u32 count = 0;
  289. cache().for_each_dirty_entry([&](BlockBasedFS::CacheEntry& entry) {
  290. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  291. file_description().seek(base_offset, SEEK_SET);
  292. // FIXME: Should this error path be surfaced somehow?
  293. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  294. [[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
  295. ++count;
  296. });
  297. cache().mark_all_clean();
  298. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  299. }
  300. void BlockBasedFS::flush_writes()
  301. {
  302. flush_writes_impl();
  303. }
  304. DiskCache& BlockBasedFS::cache() const
  305. {
  306. if (!m_cache)
  307. m_cache = make<DiskCache>(const_cast<BlockBasedFS&>(*this));
  308. return *m_cache;
  309. }
  310. }