FileBackedFileSystem.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, FileDescription* description)
  112. {
  113. ASSERT(m_logical_block_size);
  114. #ifdef FBFS_DEBUG
  115. klog() << "FileBackedFileSystem::write_block " << index << ", size=" << data.size();
  116. #endif
  117. bool allow_cache = !description || !description->is_direct();
  118. if (!allow_cache) {
  119. flush_specific_block_if_needed(index);
  120. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  121. m_file_description->seek(base_offset, SEEK_SET);
  122. auto nwritten = m_file_description->write(data, block_size());
  123. ASSERT(nwritten == block_size());
  124. return true;
  125. }
  126. auto& entry = cache().get(index);
  127. memcpy(entry.data, data, block_size());
  128. entry.is_dirty = true;
  129. entry.has_data = true;
  130. cache().set_dirty(true);
  131. return true;
  132. }
  133. bool FileBackedFS::raw_read(unsigned index, u8* buffer)
  134. {
  135. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  136. m_file_description->seek(base_offset, SEEK_SET);
  137. auto nread = m_file_description->read(buffer, m_logical_block_size);
  138. ASSERT((size_t)nread == m_logical_block_size);
  139. return true;
  140. }
  141. bool FileBackedFS::raw_write(unsigned index, const u8* buffer)
  142. {
  143. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(m_logical_block_size);
  144. m_file_description->seek(base_offset, SEEK_SET);
  145. auto nwritten = m_file_description->write(buffer, m_logical_block_size);
  146. ASSERT((size_t)nwritten == m_logical_block_size);
  147. return true;
  148. }
  149. bool FileBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description)
  150. {
  151. ASSERT(m_logical_block_size);
  152. #ifdef FBFS_DEBUG
  153. klog() << "FileBackedFileSystem::write_blocks " << index << " x" << count;
  154. #endif
  155. for (unsigned i = 0; i < count; ++i)
  156. write_block(index + i, data + i * block_size(), description);
  157. return true;
  158. }
  159. bool FileBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const
  160. {
  161. ASSERT(m_logical_block_size);
  162. #ifdef FBFS_DEBUG
  163. klog() << "FileBackedFileSystem::read_block " << index;
  164. #endif
  165. bool allow_cache = !description || !description->is_direct();
  166. if (!allow_cache) {
  167. const_cast<FileBackedFS*>(this)->flush_specific_block_if_needed(index);
  168. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  169. const_cast<FileDescription&>(*m_file_description).seek(base_offset, SEEK_SET);
  170. auto nread = const_cast<FileDescription&>(*m_file_description).read(buffer, block_size());
  171. ASSERT(nread == block_size());
  172. return true;
  173. }
  174. auto& entry = cache().get(index);
  175. if (!entry.has_data) {
  176. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  177. const_cast<FileDescription&>(*m_file_description).seek(base_offset, SEEK_SET);
  178. auto nread = const_cast<FileDescription&>(*m_file_description).read(entry.data, block_size());
  179. entry.has_data = true;
  180. ASSERT(nread == block_size());
  181. }
  182. memcpy(buffer, entry.data, block_size());
  183. return true;
  184. }
  185. bool FileBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const
  186. {
  187. ASSERT(m_logical_block_size);
  188. if (!count)
  189. return false;
  190. if (count == 1)
  191. return read_block(index, buffer, description);
  192. u8* out = buffer;
  193. for (unsigned i = 0; i < count; ++i) {
  194. if (!read_block(index + i, out, description))
  195. return false;
  196. out += block_size();
  197. }
  198. return true;
  199. }
  200. void FileBackedFS::flush_specific_block_if_needed(unsigned index)
  201. {
  202. LOCKER(m_lock);
  203. if (!cache().is_dirty())
  204. return;
  205. cache().for_each_entry([&](CacheEntry& entry) {
  206. if (entry.is_dirty && entry.block_index == index) {
  207. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  208. m_file_description->seek(base_offset, SEEK_SET);
  209. m_file_description->write(entry.data, block_size());
  210. entry.is_dirty = false;
  211. }
  212. });
  213. }
  214. void FileBackedFS::flush_writes_impl()
  215. {
  216. LOCKER(m_lock);
  217. if (!cache().is_dirty())
  218. return;
  219. u32 count = 0;
  220. cache().for_each_entry([&](CacheEntry& entry) {
  221. if (!entry.is_dirty)
  222. return;
  223. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  224. m_file_description->seek(base_offset, SEEK_SET);
  225. m_file_description->write(entry.data, block_size());
  226. ++count;
  227. entry.is_dirty = false;
  228. });
  229. cache().set_dirty(false);
  230. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  231. }
  232. void FileBackedFS::flush_writes()
  233. {
  234. flush_writes_impl();
  235. }
  236. DiskCache& FileBackedFS::cache() const
  237. {
  238. if (!m_cache)
  239. m_cache = make<DiskCache>(const_cast<FileBackedFS&>(*this));
  240. return *m_cache;
  241. }
  242. }