DiskBackedFileSystem.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/DiskBackedFileSystem.h>
  30. #include <Kernel/FileSystem/FileDescription.h>
  31. #include <Kernel/KBuffer.h>
  32. #include <Kernel/Process.h>
  33. //#define DBFS_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(DiskBackedFS& 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 DiskBackedFS flush here,
  76. // not some DiskBackedFS 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. DiskBackedFS& 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. DiskBackedFS::DiskBackedFS(BlockDevice& device)
  104. : m_device(device)
  105. {
  106. }
  107. DiskBackedFS::~DiskBackedFS()
  108. {
  109. }
  110. bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* description)
  111. {
  112. #ifdef DBFS_DEBUG
  113. klog() << "DiskBackedFileSystem::write_block " << index << ", size=" << data.size();
  114. #endif
  115. bool allow_cache = !description || !description->is_direct();
  116. if (!allow_cache) {
  117. flush_specific_block_if_needed(index);
  118. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  119. device().write_raw(base_offset, block_size(), data);
  120. return true;
  121. }
  122. auto& entry = cache().get(index);
  123. memcpy(entry.data, data, block_size());
  124. entry.is_dirty = true;
  125. entry.has_data = true;
  126. cache().set_dirty(true);
  127. return true;
  128. }
  129. bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description)
  130. {
  131. #ifdef DBFS_DEBUG
  132. klog() << "DiskBackedFileSystem::write_blocks " << index << " x" << count;
  133. #endif
  134. for (unsigned i = 0; i < count; ++i)
  135. write_block(index + i, data + i * block_size(), description);
  136. return true;
  137. }
  138. bool DiskBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const
  139. {
  140. #ifdef DBFS_DEBUG
  141. klog() << "DiskBackedFileSystem::read_block " << index;
  142. #endif
  143. bool allow_cache = !description || !description->is_direct();
  144. if (!allow_cache) {
  145. const_cast<DiskBackedFS*>(this)->flush_specific_block_if_needed(index);
  146. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  147. bool success = device().read_raw(base_offset, block_size(), buffer);
  148. ASSERT(success);
  149. return true;
  150. }
  151. auto& entry = cache().get(index);
  152. if (!entry.has_data) {
  153. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  154. bool success = device().read_raw(base_offset, block_size(), entry.data);
  155. entry.has_data = true;
  156. ASSERT(success);
  157. }
  158. memcpy(buffer, entry.data, block_size());
  159. return true;
  160. }
  161. bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const
  162. {
  163. if (!count)
  164. return false;
  165. if (count == 1)
  166. return read_block(index, buffer, description);
  167. u8* out = buffer;
  168. for (unsigned i = 0; i < count; ++i) {
  169. if (!read_block(index + i, out, description))
  170. return false;
  171. out += block_size();
  172. }
  173. return true;
  174. }
  175. void DiskBackedFS::flush_specific_block_if_needed(unsigned index)
  176. {
  177. LOCKER(m_lock);
  178. if (!cache().is_dirty())
  179. return;
  180. cache().for_each_entry([&](CacheEntry& entry) {
  181. if (entry.is_dirty && entry.block_index == index) {
  182. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  183. device().write_raw(base_offset, block_size(), entry.data);
  184. entry.is_dirty = false;
  185. }
  186. });
  187. }
  188. void DiskBackedFS::flush_writes_impl()
  189. {
  190. LOCKER(m_lock);
  191. if (!cache().is_dirty())
  192. return;
  193. u32 count = 0;
  194. cache().for_each_entry([&](CacheEntry& entry) {
  195. if (!entry.is_dirty)
  196. return;
  197. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  198. device().write_raw(base_offset, block_size(), entry.data);
  199. ++count;
  200. entry.is_dirty = false;
  201. });
  202. cache().set_dirty(false);
  203. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  204. }
  205. void DiskBackedFS::flush_writes()
  206. {
  207. flush_writes_impl();
  208. }
  209. DiskCache& DiskBackedFS::cache() const
  210. {
  211. if (!m_cache)
  212. m_cache = make<DiskCache>(const_cast<DiskBackedFS&>(*this));
  213. return *m_cache;
  214. }
  215. }