DiskBackedFileSystem.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/Arch/i386/CPU.h>
  27. #include <Kernel/Devices/BlockDevice.h>
  28. #include <Kernel/FileSystem/DiskBackedFileSystem.h>
  29. #include <Kernel/FileSystem/FileDescription.h>
  30. #include <Kernel/KBuffer.h>
  31. #include <Kernel/Process.h>
  32. //#define DBFS_DEBUG
  33. namespace Kernel {
  34. struct CacheEntry {
  35. time_t timestamp { 0 };
  36. u32 block_index { 0 };
  37. u8* data { nullptr };
  38. bool has_data { false };
  39. bool is_dirty { false };
  40. };
  41. class DiskCache {
  42. public:
  43. explicit DiskCache(DiskBackedFS& fs)
  44. : m_fs(fs)
  45. , m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size()))
  46. , m_entries(KBuffer::create_with_size(m_entry_count * sizeof(CacheEntry)))
  47. {
  48. for (size_t i = 0; i < m_entry_count; ++i) {
  49. entries()[i].data = m_cached_block_data.data() + i * m_fs.block_size();
  50. }
  51. }
  52. ~DiskCache() {}
  53. bool is_dirty() const { return m_dirty; }
  54. void set_dirty(bool b) { m_dirty = b; }
  55. CacheEntry& get(u32 block_index) const
  56. {
  57. auto now = kgettimeofday().tv_sec;
  58. CacheEntry* oldest_clean_entry = nullptr;
  59. for (size_t i = 0; i < m_entry_count; ++i) {
  60. auto& entry = const_cast<CacheEntry&>(entries()[i]);
  61. if (entry.block_index == block_index) {
  62. entry.timestamp = now;
  63. return entry;
  64. }
  65. if (!entry.is_dirty) {
  66. if (!oldest_clean_entry)
  67. oldest_clean_entry = &entry;
  68. else if (entry.timestamp < oldest_clean_entry->timestamp)
  69. oldest_clean_entry = &entry;
  70. }
  71. }
  72. if (!oldest_clean_entry) {
  73. // Not a single clean entry! Flush writes and try again.
  74. // NOTE: We want to make sure we only call DiskBackedFS flush here,
  75. // not some DiskBackedFS subclass flush!
  76. m_fs.flush_writes_impl();
  77. return get(block_index);
  78. }
  79. // Replace the oldest clean entry.
  80. auto& new_entry = *oldest_clean_entry;
  81. new_entry.timestamp = now;
  82. new_entry.block_index = block_index;
  83. new_entry.has_data = false;
  84. new_entry.is_dirty = false;
  85. return new_entry;
  86. }
  87. const CacheEntry* entries() const { return (const CacheEntry*)m_entries.data(); }
  88. CacheEntry* entries() { return (CacheEntry*)m_entries.data(); }
  89. template<typename Callback>
  90. void for_each_entry(Callback callback)
  91. {
  92. for (size_t i = 0; i < m_entry_count; ++i)
  93. callback(entries()[i]);
  94. }
  95. private:
  96. DiskBackedFS& m_fs;
  97. size_t m_entry_count { 10000 };
  98. KBuffer m_cached_block_data;
  99. KBuffer m_entries;
  100. bool m_dirty { false };
  101. };
  102. DiskBackedFS::DiskBackedFS(BlockDevice& device)
  103. : m_device(device)
  104. {
  105. }
  106. DiskBackedFS::~DiskBackedFS()
  107. {
  108. }
  109. bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* description)
  110. {
  111. #ifdef DBFS_DEBUG
  112. klog() << "DiskBackedFileSystem::write_block " << index << ", size=" << data.size();
  113. #endif
  114. bool allow_cache = !description || !description->is_direct();
  115. if (!allow_cache) {
  116. flush_specific_block_if_needed(index);
  117. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  118. device().write_raw(base_offset, block_size(), data);
  119. return true;
  120. }
  121. auto& entry = cache().get(index);
  122. memcpy(entry.data, data, block_size());
  123. entry.is_dirty = true;
  124. entry.has_data = true;
  125. cache().set_dirty(true);
  126. return true;
  127. }
  128. bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description)
  129. {
  130. #ifdef DBFS_DEBUG
  131. klog() << "DiskBackedFileSystem::write_blocks " << index << " x%u" << count;
  132. #endif
  133. for (unsigned i = 0; i < count; ++i)
  134. write_block(index + i, data + i * block_size(), description);
  135. return true;
  136. }
  137. bool DiskBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const
  138. {
  139. #ifdef DBFS_DEBUG
  140. klog() << "DiskBackedFileSystem::read_block " << index;
  141. #endif
  142. bool allow_cache = !description || !description->is_direct();
  143. if (!allow_cache) {
  144. const_cast<DiskBackedFS*>(this)->flush_specific_block_if_needed(index);
  145. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  146. bool success = device().read_raw(base_offset, block_size(), buffer);
  147. ASSERT(success);
  148. return true;
  149. }
  150. auto& entry = cache().get(index);
  151. if (!entry.has_data) {
  152. u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
  153. bool success = device().read_raw(base_offset, block_size(), entry.data);
  154. entry.has_data = true;
  155. ASSERT(success);
  156. }
  157. memcpy(buffer, entry.data, block_size());
  158. return true;
  159. }
  160. bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const
  161. {
  162. if (!count)
  163. return false;
  164. if (count == 1)
  165. return read_block(index, buffer, description);
  166. u8* out = buffer;
  167. for (unsigned i = 0; i < count; ++i) {
  168. if (!read_block(index + i, out, description))
  169. return false;
  170. out += block_size();
  171. }
  172. return true;
  173. }
  174. void DiskBackedFS::flush_specific_block_if_needed(unsigned index)
  175. {
  176. LOCKER(m_lock);
  177. if (!cache().is_dirty())
  178. return;
  179. cache().for_each_entry([&](CacheEntry& entry) {
  180. if (entry.is_dirty && entry.block_index == index) {
  181. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  182. device().write_raw(base_offset, block_size(), entry.data);
  183. entry.is_dirty = false;
  184. }
  185. });
  186. }
  187. void DiskBackedFS::flush_writes_impl()
  188. {
  189. LOCKER(m_lock);
  190. if (!cache().is_dirty())
  191. return;
  192. u32 count = 0;
  193. cache().for_each_entry([&](CacheEntry& entry) {
  194. if (!entry.is_dirty)
  195. return;
  196. u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
  197. device().write_raw(base_offset, block_size(), entry.data);
  198. ++count;
  199. entry.is_dirty = false;
  200. });
  201. cache().set_dirty(false);
  202. dbg() << class_name() << ": Flushed " << count << " blocks to disk";
  203. }
  204. void DiskBackedFS::flush_writes()
  205. {
  206. flush_writes_impl();
  207. }
  208. DiskCache& DiskBackedFS::cache() const
  209. {
  210. if (!m_cache)
  211. m_cache = make<DiskCache>(const_cast<DiskBackedFS&>(*this));
  212. return *m_cache;
  213. }
  214. }