BlockBasedFileSystem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. int BlockBasedFS::write_block(unsigned index, const UserOrKernelBuffer& 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=" << count;
  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.is_error())
  119. return -EIO; // TODO: Return error code as-is, could be -EFAULT!
  120. ASSERT(nwritten.value() == count);
  121. return 0;
  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. if (!data.read(entry.data + offset, count))
  129. return -EFAULT;
  130. entry.is_dirty = true;
  131. entry.has_data = true;
  132. cache().set_dirty(true);
  133. return 0;
  134. }
  135. bool BlockBasedFS::raw_read(unsigned index, UserOrKernelBuffer& buffer)
  136. {
  137. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  138. file_description().seek(base_offset, SEEK_SET);
  139. auto nread = file_description().read(buffer, m_logical_block_size);
  140. ASSERT(!nread.is_error());
  141. ASSERT(nread.value() == m_logical_block_size);
  142. return true;
  143. }
  144. bool BlockBasedFS::raw_write(unsigned index, const UserOrKernelBuffer& buffer)
  145. {
  146. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  147. file_description().seek(base_offset, SEEK_SET);
  148. auto nwritten = file_description().write(buffer, m_logical_block_size);
  149. ASSERT(!nwritten.is_error());
  150. ASSERT(nwritten.value() == m_logical_block_size);
  151. return true;
  152. }
  153. bool BlockBasedFS::raw_read_blocks(unsigned index, size_t count, UserOrKernelBuffer& buffer)
  154. {
  155. auto current = buffer;
  156. for (unsigned block = index; block < (index + count); block++) {
  157. if (!raw_read(block, current))
  158. return false;
  159. current = current.offset(logical_block_size());
  160. }
  161. return true;
  162. }
  163. bool BlockBasedFS::raw_write_blocks(unsigned index, size_t count, const UserOrKernelBuffer& buffer)
  164. {
  165. auto current = buffer;
  166. for (unsigned block = index; block < (index + count); block++) {
  167. if (!raw_write(block, current))
  168. return false;
  169. current = current.offset(logical_block_size());
  170. }
  171. return true;
  172. }
  173. int BlockBasedFS::write_blocks(unsigned index, unsigned count, const UserOrKernelBuffer& data, bool allow_cache)
  174. {
  175. ASSERT(m_logical_block_size);
  176. #ifdef BBFS_DEBUG
  177. klog() << "BlockBasedFileSystem::write_blocks " << index << " x" << count;
  178. #endif
  179. for (unsigned i = 0; i < count; ++i)
  180. write_block(index + i, data.offset(i * block_size()), block_size(), 0, allow_cache);
  181. return 0;
  182. }
  183. int BlockBasedFS::read_block(unsigned index, UserOrKernelBuffer* buffer, size_t count, size_t offset, bool allow_cache) const
  184. {
  185. ASSERT(m_logical_block_size);
  186. ASSERT(offset + count <= block_size());
  187. #ifdef BBFS_DEBUG
  188. klog() << "BlockBasedFileSystem::read_block " << index;
  189. #endif
  190. if (!allow_cache) {
  191. const_cast<BlockBasedFS*>(this)->flush_specific_block_if_needed(index);
  192. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size()) + static_cast<u32>(offset);
  193. file_description().seek(base_offset, SEEK_SET);
  194. auto nread = file_description().read(*buffer, count);
  195. if (nread.is_error())
  196. return -EIO;
  197. ASSERT(nread.value() == count);
  198. return 0;
  199. }
  200. auto& entry = cache().get(index);
  201. if (!entry.has_data) {
  202. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  203. file_description().seek(base_offset, SEEK_SET);
  204. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  205. auto nread = file_description().read(entry_data_buffer, block_size());
  206. if (nread.is_error())
  207. return -EIO;
  208. ASSERT(nread.value() == block_size());
  209. entry.has_data = true;
  210. }
  211. if (buffer && !buffer->write(entry.data + offset, count))
  212. return -EFAULT;
  213. return 0;
  214. }
  215. int BlockBasedFS::read_blocks(unsigned index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const
  216. {
  217. ASSERT(m_logical_block_size);
  218. if (!count)
  219. return false;
  220. if (count == 1)
  221. return read_block(index, &buffer, block_size(), 0, allow_cache);
  222. auto out = buffer;
  223. for (unsigned i = 0; i < count; ++i) {
  224. auto err = read_block(index + i, &out, block_size(), 0, allow_cache);
  225. if (err < 0)
  226. return err;
  227. out = out.offset(block_size());
  228. }
  229. return 0;
  230. }
  231. void BlockBasedFS::flush_specific_block_if_needed(unsigned index)
  232. {
  233. LOCKER(m_lock);
  234. if (!cache().is_dirty())
  235. return;
  236. cache().for_each_entry([&](CacheEntry& entry) {
  237. if (entry.is_dirty && entry.block_index == index) {
  238. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  239. file_description().seek(base_offset, SEEK_SET);
  240. // FIXME: Should this error path be surfaced somehow?
  241. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  242. (void)file_description().write(entry_data_buffer, block_size());
  243. entry.is_dirty = false;
  244. }
  245. });
  246. }
  247. void BlockBasedFS::flush_writes_impl()
  248. {
  249. LOCKER(m_lock);
  250. if (!cache().is_dirty())
  251. return;
  252. u32 count = 0;
  253. cache().for_each_entry([&](CacheEntry& entry) {
  254. if (!entry.is_dirty)
  255. return;
  256. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  257. file_description().seek(base_offset, SEEK_SET);
  258. // FIXME: Should this error path be surfaced somehow?
  259. auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
  260. (void)file_description().write(entry_data_buffer, block_size());
  261. ++count;
  262. entry.is_dirty = false;
  263. });
  264. cache().set_dirty(false);
  265. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  266. }
  267. void BlockBasedFS::flush_writes()
  268. {
  269. flush_writes_impl();
  270. }
  271. DiskCache& BlockBasedFS::cache() const
  272. {
  273. if (!m_cache)
  274. m_cache = make<DiskCache>(const_cast<BlockBasedFS&>(*this));
  275. return *m_cache;
  276. }
  277. }