FileBackedFileSystem.cpp 9.9 KB

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