2020-07-02 09:48:08 +00:00
|
|
|
/*
|
2022-01-08 18:57:33 +00:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-07-02 09:48:08 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-02 09:48:08 +00:00
|
|
|
*/
|
|
|
|
|
2020-11-24 15:38:30 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-02 09:48:08 +00:00
|
|
|
#include <Kernel/FileSystem/BlockBasedFileSystem.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2020-12-28 23:05:37 +00:00
|
|
|
struct CacheEntry {
|
2021-04-16 12:03:24 +00:00
|
|
|
IntrusiveListNode<CacheEntry> list_node;
|
2021-07-10 22:34:36 +00:00
|
|
|
BlockBasedFileSystem::BlockIndex block_index { 0 };
|
2020-12-28 23:05:37 +00:00
|
|
|
u8* data { nullptr };
|
|
|
|
bool has_data { false };
|
|
|
|
};
|
|
|
|
|
2020-07-02 09:48:08 +00:00
|
|
|
class DiskCache {
|
|
|
|
public:
|
2021-08-01 11:33:06 +00:00
|
|
|
static constexpr size_t EntryCount = 10000;
|
|
|
|
explicit DiskCache(BlockBasedFileSystem& fs, NonnullOwnPtr<KBuffer> cached_block_data, NonnullOwnPtr<KBuffer> entries_buffer)
|
2020-07-02 09:48:08 +00:00
|
|
|
: m_fs(fs)
|
2021-08-01 11:33:06 +00:00
|
|
|
, m_cached_block_data(move(cached_block_data))
|
|
|
|
, m_entries(move(entries_buffer))
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-08-01 11:33:06 +00:00
|
|
|
for (size_t i = 0; i < EntryCount; ++i) {
|
|
|
|
entries()[i].data = m_cached_block_data->data() + i * m_fs.block_size();
|
2020-11-24 15:38:30 +00:00
|
|
|
m_clean_list.append(entries()[i]);
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 13:42:08 +00:00
|
|
|
~DiskCache() = default;
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2022-01-08 17:57:37 +00:00
|
|
|
bool is_dirty() const { return !m_dirty_list.is_empty(); }
|
2022-01-08 19:11:47 +00:00
|
|
|
bool entry_is_dirty(CacheEntry const& entry) const { return m_dirty_list.contains(entry); }
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2020-11-24 15:38:30 +00:00
|
|
|
void mark_all_clean()
|
|
|
|
{
|
|
|
|
while (auto* entry = m_dirty_list.first())
|
|
|
|
m_clean_list.prepend(*entry);
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:05:37 +00:00
|
|
|
void mark_dirty(CacheEntry& entry)
|
2020-11-24 15:38:30 +00:00
|
|
|
{
|
|
|
|
m_dirty_list.prepend(entry);
|
|
|
|
}
|
|
|
|
|
2020-12-28 23:05:37 +00:00
|
|
|
void mark_clean(CacheEntry& entry)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2020-11-24 15:38:30 +00:00
|
|
|
m_clean_list.prepend(entry);
|
|
|
|
}
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2022-01-08 19:11:47 +00:00
|
|
|
CacheEntry* get(BlockBasedFileSystem::BlockIndex block_index) const
|
|
|
|
{
|
|
|
|
auto it = m_hash.find(block_index);
|
|
|
|
if (it == m_hash.end())
|
|
|
|
return nullptr;
|
|
|
|
auto& entry = const_cast<CacheEntry&>(*it->value);
|
|
|
|
VERIFY(entry.block_index == block_index);
|
|
|
|
return &entry;
|
|
|
|
}
|
|
|
|
|
2022-01-23 15:35:28 +00:00
|
|
|
ErrorOr<CacheEntry*> ensure(BlockBasedFileSystem::BlockIndex block_index) const
|
2020-11-24 15:38:30 +00:00
|
|
|
{
|
2022-01-08 19:11:47 +00:00
|
|
|
if (auto* entry = get(block_index))
|
2022-01-23 15:35:28 +00:00
|
|
|
return entry;
|
2020-11-24 11:08:52 +00:00
|
|
|
|
2020-11-24 15:38:30 +00:00
|
|
|
if (m_clean_list.is_empty()) {
|
2020-07-02 09:48:08 +00:00
|
|
|
// Not a single clean entry! Flush writes and try again.
|
2021-07-10 22:33:27 +00:00
|
|
|
// NOTE: We want to make sure we only call FileBackedFileSystem flush here,
|
|
|
|
// not some FileBackedFileSystem subclass flush!
|
2020-07-02 09:48:08 +00:00
|
|
|
m_fs.flush_writes_impl();
|
2022-01-08 18:57:33 +00:00
|
|
|
return ensure(block_index);
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_clean_list.last());
|
2020-11-24 15:38:30 +00:00
|
|
|
auto& new_entry = *m_clean_list.last();
|
|
|
|
m_clean_list.prepend(new_entry);
|
2020-11-24 11:08:52 +00:00
|
|
|
|
2020-11-24 15:38:30 +00:00
|
|
|
m_hash.remove(new_entry.block_index);
|
2022-01-23 15:35:28 +00:00
|
|
|
TRY(m_hash.try_set(block_index, &new_entry));
|
2020-11-24 11:08:52 +00:00
|
|
|
|
2020-07-02 09:48:08 +00:00
|
|
|
new_entry.block_index = block_index;
|
|
|
|
new_entry.has_data = false;
|
2020-11-24 15:38:30 +00:00
|
|
|
|
2022-01-23 15:35:28 +00:00
|
|
|
return &new_entry;
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
CacheEntry const* entries() const { return (CacheEntry const*)m_entries->data(); }
|
2021-08-01 11:33:06 +00:00
|
|
|
CacheEntry* entries() { return (CacheEntry*)m_entries->data(); }
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2020-11-24 15:38:30 +00:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_dirty_entry(Callback callback)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2020-11-24 15:38:30 +00:00
|
|
|
for (auto& entry : m_dirty_list)
|
|
|
|
callback(entry);
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-10 22:34:36 +00:00
|
|
|
BlockBasedFileSystem& m_fs;
|
|
|
|
mutable HashMap<BlockBasedFileSystem::BlockIndex, CacheEntry*> m_hash;
|
2021-09-09 12:00:59 +00:00
|
|
|
mutable IntrusiveList<&CacheEntry::list_node> m_clean_list;
|
|
|
|
mutable IntrusiveList<&CacheEntry::list_node> m_dirty_list;
|
2021-08-01 11:33:06 +00:00
|
|
|
NonnullOwnPtr<KBuffer> m_cached_block_data;
|
|
|
|
NonnullOwnPtr<KBuffer> m_entries;
|
2020-07-02 09:48:08 +00:00
|
|
|
};
|
|
|
|
|
2021-09-07 11:39:11 +00:00
|
|
|
BlockBasedFileSystem::BlockBasedFileSystem(OpenFileDescription& file_description)
|
2021-07-10 22:33:27 +00:00
|
|
|
: FileBackedFileSystem(file_description)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(file_description.file().is_seekable());
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 19:15:15 +00:00
|
|
|
BlockBasedFileSystem::~BlockBasedFileSystem() = default;
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::initialize()
|
2021-08-01 11:33:06 +00:00
|
|
|
{
|
|
|
|
VERIFY(block_size() != 0);
|
2022-04-10 22:08:07 +00:00
|
|
|
auto cached_block_data = TRY(KBuffer::try_create_with_size("BlockBasedFS: Cache blocks"sv, DiskCache::EntryCount * block_size()));
|
|
|
|
auto entries_data = TRY(KBuffer::try_create_with_size("BlockBasedFS: Cache entries"sv, DiskCache::EntryCount * sizeof(CacheEntry)));
|
2021-09-07 13:15:08 +00:00
|
|
|
auto disk_cache = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DiskCache(*this, move(cached_block_data), move(entries_data))));
|
2021-08-01 11:33:06 +00:00
|
|
|
|
2021-08-07 21:50:37 +00:00
|
|
|
m_cache.with_exclusive([&](auto& cache) {
|
|
|
|
cache = move(disk_cache);
|
|
|
|
});
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-08-01 11:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::write_block(BlockIndex index, UserOrKernelBuffer const& data, size_t count, u64 offset, bool allow_cache)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_logical_block_size);
|
|
|
|
VERIFY(offset + count <= block_size());
|
2021-02-12 12:51:34 +00:00
|
|
|
dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_block {}, size={}", index, count);
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2021-12-25 22:44:04 +00:00
|
|
|
// NOTE: We copy the `data` to write into a local buffer before taking the cache lock.
|
|
|
|
// This makes sure any page faults caused by accessing the data will occur before
|
|
|
|
// we tie down the cache.
|
2022-01-20 17:47:39 +00:00
|
|
|
auto buffered_data = TRY(ByteBuffer::create_uninitialized(count));
|
2021-12-25 22:44:04 +00:00
|
|
|
|
|
|
|
TRY(data.read(buffered_data.bytes()));
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return m_cache.with_exclusive([&](auto& cache) -> ErrorOr<void> {
|
2021-08-07 21:50:37 +00:00
|
|
|
if (!allow_cache) {
|
|
|
|
flush_specific_block_if_needed(index);
|
2022-01-25 18:23:53 +00:00
|
|
|
u64 base_offset = index.value() * block_size() + offset;
|
2021-09-05 19:24:37 +00:00
|
|
|
auto nwritten = TRY(file_description().write(base_offset, data, count));
|
|
|
|
VERIFY(nwritten == count);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-08-07 21:50:37 +00:00
|
|
|
}
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2022-01-23 15:35:28 +00:00
|
|
|
auto entry = TRY(cache->ensure(index));
|
2021-08-07 21:50:37 +00:00
|
|
|
if (count < block_size()) {
|
|
|
|
// Fill the cache first.
|
2021-09-05 19:24:37 +00:00
|
|
|
TRY(read_block(index, nullptr, block_size()));
|
2021-08-07 21:50:37 +00:00
|
|
|
}
|
2022-01-23 15:35:28 +00:00
|
|
|
memcpy(entry->data + offset, buffered_data.data(), count);
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2022-01-23 15:35:28 +00:00
|
|
|
cache->mark_dirty(*entry);
|
|
|
|
entry->has_data = true;
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-08-07 21:50:37 +00:00
|
|
|
});
|
2020-12-24 16:18:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 00:53:02 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::raw_read(BlockIndex index, UserOrKernelBuffer& buffer)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-07-05 19:38:17 +00:00
|
|
|
auto base_offset = index.value() * m_logical_block_size;
|
2022-01-23 00:53:02 +00:00
|
|
|
auto nread = TRY(file_description().read(buffer, base_offset, m_logical_block_size));
|
|
|
|
VERIFY(nread == m_logical_block_size);
|
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
2021-02-26 16:15:32 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::raw_write(BlockIndex index, UserOrKernelBuffer const& buffer)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-07-05 19:38:17 +00:00
|
|
|
auto base_offset = index.value() * m_logical_block_size;
|
2022-01-23 00:53:02 +00:00
|
|
|
auto nwritten = TRY(file_description().write(base_offset, buffer, m_logical_block_size));
|
|
|
|
VERIFY(nwritten == m_logical_block_size);
|
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 00:53:02 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::raw_read_blocks(BlockIndex index, size_t count, UserOrKernelBuffer& buffer)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2020-09-12 03:11:07 +00:00
|
|
|
auto current = buffer;
|
2021-07-05 19:38:17 +00:00
|
|
|
for (auto block = index.value(); block < (index.value() + count); block++) {
|
2022-01-23 00:53:02 +00:00
|
|
|
TRY(raw_read(BlockIndex { block }, current));
|
2020-09-12 03:11:07 +00:00
|
|
|
current = current.offset(logical_block_size());
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
2022-01-23 00:53:02 +00:00
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
2021-02-26 16:15:32 +00:00
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::raw_write_blocks(BlockIndex index, size_t count, UserOrKernelBuffer const& buffer)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2020-09-12 03:11:07 +00:00
|
|
|
auto current = buffer;
|
2021-07-05 19:38:17 +00:00
|
|
|
for (auto block = index.value(); block < (index.value() + count); block++) {
|
2022-01-23 00:53:02 +00:00
|
|
|
TRY(raw_write(block, current));
|
2020-09-12 03:11:07 +00:00
|
|
|
current = current.offset(logical_block_size());
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
2022-01-23 00:53:02 +00:00
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::write_blocks(BlockIndex index, unsigned count, UserOrKernelBuffer const& data, bool allow_cache)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_logical_block_size);
|
2021-02-12 12:51:34 +00:00
|
|
|
dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::write_blocks {}, count={}", index, count);
|
2021-01-20 20:11:01 +00:00
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
2021-09-05 19:24:37 +00:00
|
|
|
TRY(write_block(BlockIndex { index.value() + i }, data.offset(i * block_size()), block_size(), 0, allow_cache));
|
2021-01-20 20:11:01 +00:00
|
|
|
}
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 18:24:48 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* buffer, size_t count, u64 offset, bool allow_cache) const
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_logical_block_size);
|
|
|
|
VERIFY(offset + count <= block_size());
|
2021-02-12 12:51:34 +00:00
|
|
|
dbgln_if(BBFS_DEBUG, "BlockBasedFileSystem::read_block {}", index);
|
2020-07-02 09:48:08 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return m_cache.with_exclusive([&](auto& cache) -> ErrorOr<void> {
|
2021-08-07 21:50:37 +00:00
|
|
|
if (!allow_cache) {
|
|
|
|
const_cast<BlockBasedFileSystem*>(this)->flush_specific_block_if_needed(index);
|
2022-01-25 18:24:48 +00:00
|
|
|
u64 base_offset = index.value() * block_size() + offset;
|
2021-09-05 19:24:37 +00:00
|
|
|
auto nread = TRY(file_description().read(*buffer, base_offset, count));
|
|
|
|
VERIFY(nread == count);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-08-07 21:50:37 +00:00
|
|
|
}
|
2021-07-15 23:08:00 +00:00
|
|
|
|
2022-01-23 15:35:28 +00:00
|
|
|
auto* entry = TRY(cache->ensure(index));
|
|
|
|
if (!entry->has_data) {
|
2021-08-07 21:50:37 +00:00
|
|
|
auto base_offset = index.value() * block_size();
|
2022-01-23 15:35:28 +00:00
|
|
|
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry->data);
|
2021-09-05 19:24:37 +00:00
|
|
|
auto nread = TRY(file_description().read(entry_data_buffer, base_offset, block_size()));
|
|
|
|
VERIFY(nread == block_size());
|
2022-01-23 15:35:28 +00:00
|
|
|
entry->has_data = true;
|
2021-08-07 21:50:37 +00:00
|
|
|
}
|
2021-09-07 10:09:52 +00:00
|
|
|
if (buffer)
|
2022-01-23 15:35:28 +00:00
|
|
|
TRY(buffer->write(entry->data + offset, count));
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-08-07 21:50:37 +00:00
|
|
|
});
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> BlockBasedFileSystem::read_blocks(BlockIndex index, unsigned count, UserOrKernelBuffer& buffer, bool allow_cache) const
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(m_logical_block_size);
|
2020-07-02 09:48:08 +00:00
|
|
|
if (!count)
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINVAL;
|
2020-07-02 09:48:08 +00:00
|
|
|
if (count == 1)
|
2020-12-28 23:05:37 +00:00
|
|
|
return read_block(index, &buffer, block_size(), 0, allow_cache);
|
2020-09-12 03:11:07 +00:00
|
|
|
auto out = buffer;
|
2020-07-02 09:48:08 +00:00
|
|
|
for (unsigned i = 0; i < count; ++i) {
|
2021-09-05 19:24:37 +00:00
|
|
|
TRY(read_block(BlockIndex { index.value() + i }, &out, block_size(), 0, allow_cache));
|
2020-09-12 03:11:07 +00:00
|
|
|
out = out.offset(block_size());
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-07-02 09:48:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 22:34:36 +00:00
|
|
|
void BlockBasedFileSystem::flush_specific_block_if_needed(BlockIndex index)
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-08-07 21:50:37 +00:00
|
|
|
m_cache.with_exclusive([&](auto& cache) {
|
|
|
|
if (!cache->is_dirty())
|
|
|
|
return;
|
2022-01-08 19:11:47 +00:00
|
|
|
auto* entry = cache->get(index);
|
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
if (!cache->entry_is_dirty(*entry))
|
|
|
|
return;
|
|
|
|
size_t base_offset = entry->block_index.value() * block_size();
|
|
|
|
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry->data);
|
|
|
|
(void)file_description().write(base_offset, entry_data_buffer, block_size());
|
2020-07-02 09:48:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-10 22:34:36 +00:00
|
|
|
void BlockBasedFileSystem::flush_writes_impl()
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
2021-08-07 21:50:37 +00:00
|
|
|
size_t count = 0;
|
|
|
|
m_cache.with_exclusive([&](auto& cache) {
|
|
|
|
if (!cache->is_dirty())
|
|
|
|
return;
|
|
|
|
cache->for_each_dirty_entry([&](CacheEntry& entry) {
|
|
|
|
auto base_offset = entry.block_index.value() * block_size();
|
|
|
|
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
|
|
|
|
[[maybe_unused]] auto rc = file_description().write(base_offset, entry_data_buffer, block_size());
|
|
|
|
++count;
|
|
|
|
});
|
|
|
|
cache->mark_all_clean();
|
|
|
|
dbgln("{}: Flushed {} blocks to disk", class_name(), count);
|
2020-07-02 09:48:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-10 22:34:36 +00:00
|
|
|
void BlockBasedFileSystem::flush_writes()
|
2020-07-02 09:48:08 +00:00
|
|
|
{
|
|
|
|
flush_writes_impl();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|