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