BlockBasedFileSystem.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <Kernel/FileSystem/BlockBasedFileSystem.h>
  27. #include <Kernel/Process.h>
  28. //#define BBFS_DEBUG
  29. namespace Kernel {
  30. struct CacheEntry {
  31. time_t timestamp { 0 };
  32. u32 block_index { 0 };
  33. u8* data { nullptr };
  34. bool has_data { false };
  35. bool is_dirty { 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. }
  47. }
  48. ~DiskCache() { }
  49. bool is_dirty() const { return m_dirty; }
  50. void set_dirty(bool b) { m_dirty = b; }
  51. CacheEntry& get(u32 block_index) const
  52. {
  53. auto now = kgettimeofday().tv_sec;
  54. CacheEntry* oldest_clean_entry = nullptr;
  55. for (size_t i = 0; i < m_entry_count; ++i) {
  56. auto& entry = const_cast<CacheEntry&>(entries()[i]);
  57. if (entry.block_index == block_index) {
  58. entry.timestamp = now;
  59. return entry;
  60. }
  61. if (!entry.is_dirty) {
  62. if (!oldest_clean_entry)
  63. oldest_clean_entry = &entry;
  64. else if (entry.timestamp < oldest_clean_entry->timestamp)
  65. oldest_clean_entry = &entry;
  66. }
  67. }
  68. if (!oldest_clean_entry) {
  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. // Replace the oldest clean entry.
  76. auto& new_entry = *oldest_clean_entry;
  77. new_entry.timestamp = now;
  78. new_entry.block_index = block_index;
  79. new_entry.has_data = false;
  80. new_entry.is_dirty = false;
  81. return new_entry;
  82. }
  83. const CacheEntry* entries() const { return (const CacheEntry*)m_entries.data(); }
  84. CacheEntry* entries() { return (CacheEntry*)m_entries.data(); }
  85. template<typename Callback>
  86. void for_each_entry(Callback callback)
  87. {
  88. for (size_t i = 0; i < m_entry_count; ++i)
  89. callback(entries()[i]);
  90. }
  91. private:
  92. BlockBasedFS& m_fs;
  93. size_t m_entry_count { 10000 };
  94. KBuffer m_cached_block_data;
  95. KBuffer m_entries;
  96. bool m_dirty { false };
  97. };
  98. BlockBasedFS::BlockBasedFS(FileDescription& file_description)
  99. : FileBackedFS(file_description)
  100. {
  101. ASSERT(file_description.file().is_seekable());
  102. }
  103. BlockBasedFS::~BlockBasedFS()
  104. {
  105. }
  106. bool BlockBasedFS::write_block(unsigned index, const u8* data, size_t count, size_t offset, bool allow_cache)
  107. {
  108. ASSERT(m_logical_block_size);
  109. ASSERT(offset + count <= block_size());
  110. #ifdef BBFS_DEBUG
  111. klog() << "BlockBasedFileSystem::write_block " << index << ", size=" << data.size();
  112. #endif
  113. if (!allow_cache) {
  114. flush_specific_block_if_needed(index);
  115. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size()) + offset;
  116. file_description().seek(base_offset, SEEK_SET);
  117. auto nwritten = file_description().write(data, count);
  118. if (nwritten < 0)
  119. return false;
  120. ASSERT(static_cast<size_t>(nwritten) == count);
  121. return true;
  122. }
  123. auto& entry = cache().get(index);
  124. if (count < block_size()) {
  125. // Fill the cache first.
  126. read_block(index, nullptr, block_size());
  127. }
  128. memcpy(entry.data + offset, data, count);
  129. entry.is_dirty = true;
  130. entry.has_data = true;
  131. cache().set_dirty(true);
  132. return true;
  133. }
  134. bool BlockBasedFS::raw_read(unsigned index, u8* buffer)
  135. {
  136. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  137. file_description().seek(base_offset, SEEK_SET);
  138. auto nread = file_description().read(buffer, m_logical_block_size);
  139. ASSERT((size_t)nread == m_logical_block_size);
  140. return true;
  141. }
  142. bool BlockBasedFS::raw_write(unsigned index, const u8* buffer)
  143. {
  144. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  145. file_description().seek(base_offset, SEEK_SET);
  146. auto nwritten = file_description().write(buffer, m_logical_block_size);
  147. ASSERT((size_t)nwritten == m_logical_block_size);
  148. return true;
  149. }
  150. bool BlockBasedFS::raw_read_blocks(unsigned index, size_t count, u8* buffer)
  151. {
  152. for (unsigned block = index; block < (index + count); block++) {
  153. if (!raw_read(block, buffer))
  154. return false;
  155. buffer += logical_block_size();
  156. }
  157. return true;
  158. }
  159. bool BlockBasedFS::raw_write_blocks(unsigned index, size_t count, const u8* buffer)
  160. {
  161. for (unsigned block = index; block < (index + count); block++) {
  162. if (!raw_write(block, buffer))
  163. return false;
  164. buffer += logical_block_size();
  165. }
  166. return true;
  167. }
  168. bool BlockBasedFS::write_blocks(unsigned index, unsigned count, const u8* data, bool allow_cache)
  169. {
  170. ASSERT(m_logical_block_size);
  171. #ifdef BBFS_DEBUG
  172. klog() << "BlockBasedFileSystem::write_blocks " << index << " x" << count;
  173. #endif
  174. for (unsigned i = 0; i < count; ++i)
  175. write_block(index + i, data + i * block_size(), block_size(), 0, allow_cache);
  176. return true;
  177. }
  178. bool BlockBasedFS::read_block(unsigned index, u8* buffer, size_t count, size_t offset, bool allow_cache) const
  179. {
  180. ASSERT(m_logical_block_size);
  181. ASSERT(offset + count <= block_size());
  182. #ifdef BBFS_DEBUG
  183. klog() << "BlockBasedFileSystem::read_block " << index;
  184. #endif
  185. if (!allow_cache) {
  186. const_cast<BlockBasedFS*>(this)->flush_specific_block_if_needed(index);
  187. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size()) + static_cast<u32>(offset);
  188. file_description().seek(base_offset, SEEK_SET);
  189. auto nread = file_description().read(buffer, count);
  190. if (nread < 0)
  191. return false;
  192. ASSERT(static_cast<size_t>(nread) == count);
  193. return true;
  194. }
  195. auto& entry = cache().get(index);
  196. if (!entry.has_data) {
  197. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  198. file_description().seek(base_offset, SEEK_SET);
  199. auto nread = file_description().read(entry.data, block_size());
  200. if (nread < 0)
  201. return false;
  202. ASSERT(static_cast<size_t>(nread) == block_size());
  203. entry.has_data = true;
  204. }
  205. if (buffer)
  206. memcpy(buffer, entry.data + offset, count);
  207. return true;
  208. }
  209. bool BlockBasedFS::read_blocks(unsigned index, unsigned count, u8* buffer, bool allow_cache) const
  210. {
  211. ASSERT(m_logical_block_size);
  212. if (!count)
  213. return false;
  214. if (count == 1)
  215. return read_block(index, buffer, block_size(), 0, allow_cache);
  216. u8* out = buffer;
  217. for (unsigned i = 0; i < count; ++i) {
  218. if (!read_block(index + i, out, block_size(), 0, allow_cache))
  219. return false;
  220. out += block_size();
  221. }
  222. return true;
  223. }
  224. void BlockBasedFS::flush_specific_block_if_needed(unsigned index)
  225. {
  226. LOCKER(m_lock);
  227. if (!cache().is_dirty())
  228. return;
  229. cache().for_each_entry([&](CacheEntry& entry) {
  230. if (entry.is_dirty && entry.block_index == index) {
  231. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  232. file_description().seek(base_offset, SEEK_SET);
  233. file_description().write(entry.data, block_size());
  234. entry.is_dirty = false;
  235. }
  236. });
  237. }
  238. void BlockBasedFS::flush_writes_impl()
  239. {
  240. LOCKER(m_lock);
  241. if (!cache().is_dirty())
  242. return;
  243. u32 count = 0;
  244. cache().for_each_entry([&](CacheEntry& entry) {
  245. if (!entry.is_dirty)
  246. return;
  247. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  248. file_description().seek(base_offset, SEEK_SET);
  249. file_description().write(entry.data, block_size());
  250. ++count;
  251. entry.is_dirty = false;
  252. });
  253. cache().set_dirty(false);
  254. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  255. }
  256. void BlockBasedFS::flush_writes()
  257. {
  258. flush_writes_impl();
  259. }
  260. DiskCache& BlockBasedFS::cache() const
  261. {
  262. if (!m_cache)
  263. m_cache = make<DiskCache>(const_cast<BlockBasedFS&>(*this));
  264. return *m_cache;
  265. }
  266. }