2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-07 21:02:01 +03:00
|
|
|
#include <Kernel/Arch/i386/CPU.h>
|
2019-09-30 11:31:06 +03:00
|
|
|
#include <Kernel/FileSystem/DiskBackedFileSystem.h>
|
2019-11-05 21:35:12 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-09-30 11:31:06 +03:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-03-24 00:03:17 +03:00
|
|
|
#include <Kernel/Process.h>
|
2018-10-25 13:35:49 +03:00
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
//#define DBFS_DEBUG
|
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
struct CacheEntry {
|
2020-01-27 12:56:04 +03:00
|
|
|
time_t timestamp { 0 };
|
2019-09-30 11:31:06 +03:00
|
|
|
u32 block_index { 0 };
|
|
|
|
u8* data { nullptr };
|
|
|
|
bool has_data { false };
|
|
|
|
bool is_dirty { false };
|
2019-02-10 22:07:14 +03:00
|
|
|
};
|
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
class DiskCache {
|
|
|
|
public:
|
2019-09-30 12:45:22 +03:00
|
|
|
explicit DiskCache(DiskBackedFS& fs)
|
|
|
|
: m_fs(fs)
|
|
|
|
, m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size()))
|
2019-10-08 11:42:52 +03:00
|
|
|
, m_entries(KBuffer::create_with_size(m_entry_count * sizeof(CacheEntry)))
|
2019-09-30 11:31:06 +03:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_entry_count; ++i) {
|
2019-10-08 11:42:52 +03:00
|
|
|
entries()[i].data = m_cached_block_data.data() + i * m_fs.block_size();
|
2019-09-30 11:31:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-10 22:07:14 +03:00
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
~DiskCache() {}
|
2019-02-10 22:07:14 +03:00
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
bool is_dirty() const { return m_dirty; }
|
|
|
|
void set_dirty(bool b) { m_dirty = b; }
|
2019-02-10 22:07:14 +03:00
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
CacheEntry& get(u32 block_index) const
|
2019-02-10 22:07:14 +03:00
|
|
|
{
|
2019-09-30 11:31:06 +03:00
|
|
|
auto now = kgettimeofday().tv_sec;
|
|
|
|
|
|
|
|
CacheEntry* oldest_clean_entry = nullptr;
|
|
|
|
for (size_t i = 0; i < m_entry_count; ++i) {
|
2019-10-08 11:42:52 +03:00
|
|
|
auto& entry = const_cast<CacheEntry&>(entries()[i]);
|
2019-09-30 11:31:06 +03:00
|
|
|
if (entry.block_index == block_index) {
|
|
|
|
entry.timestamp = now;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
if (!entry.is_dirty) {
|
|
|
|
if (!oldest_clean_entry)
|
|
|
|
oldest_clean_entry = &entry;
|
|
|
|
else if (entry.timestamp < oldest_clean_entry->timestamp)
|
|
|
|
oldest_clean_entry = &entry;
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 12:45:22 +03:00
|
|
|
if (!oldest_clean_entry) {
|
|
|
|
// Not a single clean entry! Flush writes and try again.
|
2019-11-03 02:21:17 +03:00
|
|
|
// NOTE: We want to make sure we only call DiskBackedFS flush here,
|
|
|
|
// not some DiskBackedFS subclass flush!
|
|
|
|
m_fs.flush_writes_impl();
|
2019-09-30 12:45:22 +03:00
|
|
|
return get(block_index);
|
|
|
|
}
|
2019-09-30 11:31:06 +03:00
|
|
|
|
|
|
|
// Replace the oldest clean entry.
|
|
|
|
auto& new_entry = *oldest_clean_entry;
|
|
|
|
new_entry.timestamp = now;
|
|
|
|
new_entry.block_index = block_index;
|
|
|
|
new_entry.has_data = false;
|
|
|
|
new_entry.is_dirty = false;
|
|
|
|
return new_entry;
|
2019-02-10 22:07:14 +03:00
|
|
|
}
|
|
|
|
|
2019-10-08 11:42:52 +03:00
|
|
|
const CacheEntry* entries() const { return (const CacheEntry*)m_entries.data(); }
|
|
|
|
CacheEntry* entries() { return (CacheEntry*)m_entries.data(); }
|
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_entry(Callback callback)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_entry_count; ++i)
|
2019-10-08 11:42:52 +03:00
|
|
|
callback(entries()[i]);
|
2019-09-30 11:31:06 +03:00
|
|
|
}
|
2019-02-10 22:07:14 +03:00
|
|
|
|
2019-09-30 12:45:22 +03:00
|
|
|
private:
|
|
|
|
DiskBackedFS& m_fs;
|
2019-09-30 11:31:06 +03:00
|
|
|
size_t m_entry_count { 10000 };
|
|
|
|
KBuffer m_cached_block_data;
|
2019-10-08 11:42:52 +03:00
|
|
|
KBuffer m_entries;
|
2019-09-30 11:31:06 +03:00
|
|
|
bool m_dirty { false };
|
2019-02-10 22:07:14 +03:00
|
|
|
};
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
DiskBackedFS::DiskBackedFS(NonnullRefPtr<DiskDevice>&& device)
|
2018-10-17 11:55:43 +03:00
|
|
|
: m_device(move(device))
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-11-15 19:13:10 +03:00
|
|
|
DiskBackedFS::~DiskBackedFS()
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* description)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
#ifdef DBFS_DEBUG
|
2019-01-31 19:31:23 +03:00
|
|
|
kprintf("DiskBackedFileSystem::write_block %u, size=%u\n", index, data.size());
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2019-11-05 21:35:12 +03:00
|
|
|
|
|
|
|
bool allow_cache = !description || !description->is_direct();
|
|
|
|
|
|
|
|
if (!allow_cache) {
|
|
|
|
flush_specific_block_if_needed(index);
|
|
|
|
DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
|
|
|
|
device().write(base_offset, block_size(), data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
auto& entry = cache().get(index);
|
2019-09-30 12:20:51 +03:00
|
|
|
memcpy(entry.data, data, block_size());
|
2019-09-30 11:31:06 +03:00
|
|
|
entry.is_dirty = true;
|
|
|
|
entry.has_data = true;
|
2019-04-27 18:30:32 +03:00
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
cache().set_dirty(true);
|
2019-04-25 23:05:53 +03:00
|
|
|
return true;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
#ifdef DBFS_DEBUG
|
2019-01-31 19:31:23 +03:00
|
|
|
kprintf("DiskBackedFileSystem::write_blocks %u x%u\n", index, count);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2019-04-25 23:05:53 +03:00
|
|
|
for (unsigned i = 0; i < count; ++i)
|
2019-11-05 21:35:12 +03:00
|
|
|
write_block(index + i, data + i * block_size(), description);
|
2019-04-25 23:05:53 +03:00
|
|
|
return true;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
bool DiskBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
#ifdef DBFS_DEBUG
|
2019-01-31 19:31:23 +03:00
|
|
|
kprintf("DiskBackedFileSystem::read_block %u\n", index);
|
2018-10-10 12:53:07 +03:00
|
|
|
#endif
|
2019-04-25 23:05:53 +03:00
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
bool allow_cache = !description || !description->is_direct();
|
|
|
|
|
|
|
|
if (!allow_cache) {
|
|
|
|
const_cast<DiskBackedFS*>(this)->flush_specific_block_if_needed(index);
|
|
|
|
DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
|
|
|
|
bool success = device().read(base_offset, block_size(), buffer);
|
|
|
|
ASSERT(success);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-30 11:31:06 +03:00
|
|
|
auto& entry = cache().get(index);
|
|
|
|
if (!entry.has_data) {
|
|
|
|
DiskOffset base_offset = static_cast<DiskOffset>(index) * static_cast<DiskOffset>(block_size());
|
|
|
|
bool success = device().read(base_offset, block_size(), entry.data);
|
|
|
|
entry.has_data = true;
|
|
|
|
ASSERT(success);
|
2019-02-10 22:07:14 +03:00
|
|
|
}
|
2019-09-30 12:04:30 +03:00
|
|
|
memcpy(buffer, entry.data, block_size());
|
|
|
|
return true;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
if (!count)
|
2019-09-30 12:04:30 +03:00
|
|
|
return false;
|
2018-10-10 12:53:07 +03:00
|
|
|
if (count == 1)
|
2019-11-05 21:35:12 +03:00
|
|
|
return read_block(index, buffer, description);
|
2019-09-30 12:04:30 +03:00
|
|
|
u8* out = buffer;
|
2018-10-10 12:53:07 +03:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
2019-11-05 21:35:12 +03:00
|
|
|
if (!read_block(index + i, out, description))
|
2019-09-30 12:04:30 +03:00
|
|
|
return false;
|
2019-01-31 19:31:23 +03:00
|
|
|
out += block_size();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-09-30 12:04:30 +03:00
|
|
|
return true;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 21:35:12 +03:00
|
|
|
void DiskBackedFS::flush_specific_block_if_needed(unsigned index)
|
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
if (!cache().is_dirty())
|
|
|
|
return;
|
|
|
|
cache().for_each_entry([&](CacheEntry& entry) {
|
|
|
|
if (entry.is_dirty && entry.block_index == index) {
|
|
|
|
DiskOffset base_offset = static_cast<DiskOffset>(entry.block_index) * static_cast<DiskOffset>(block_size());
|
|
|
|
device().write(base_offset, block_size(), entry.data);
|
|
|
|
entry.is_dirty = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-03 02:21:17 +03:00
|
|
|
void DiskBackedFS::flush_writes_impl()
|
2019-04-25 23:05:53 +03:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
2019-09-30 11:31:06 +03:00
|
|
|
if (!cache().is_dirty())
|
|
|
|
return;
|
|
|
|
u32 count = 0;
|
|
|
|
cache().for_each_entry([&](CacheEntry& entry) {
|
|
|
|
if (!entry.is_dirty)
|
|
|
|
return;
|
|
|
|
DiskOffset base_offset = static_cast<DiskOffset>(entry.block_index) * static_cast<DiskOffset>(block_size());
|
|
|
|
device().write(base_offset, block_size(), entry.data);
|
|
|
|
++count;
|
|
|
|
entry.is_dirty = false;
|
|
|
|
});
|
|
|
|
cache().set_dirty(false);
|
2019-11-03 02:21:17 +03:00
|
|
|
dbg() << class_name() << ": Flushed " << count << " blocks to disk";
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiskBackedFS::flush_writes()
|
|
|
|
{
|
|
|
|
flush_writes_impl();
|
2019-09-30 11:31:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DiskCache& DiskBackedFS::cache() const
|
|
|
|
{
|
|
|
|
if (!m_cache)
|
2019-09-30 12:45:22 +03:00
|
|
|
m_cache = make<DiskCache>(const_cast<DiskBackedFS&>(*this));
|
2019-09-30 11:31:06 +03:00
|
|
|
return *m_cache;
|
2019-04-25 23:05:53 +03:00
|
|
|
}
|