BlockBasedFileSystem.cpp 11 KB

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