Skip to content

Commit

Permalink
Kernel: Don't explicitly seek before I/O in BlockBasedFileSystem
Browse files Browse the repository at this point in the history
Use the new FileDescription APIs to avoid doing seek+read or seek+write
as two separate operations.
  • Loading branch information
awesomekling committed Jul 16, 2021
1 parent d1395f2 commit abf0249
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions Kernel/FileSystem/BlockBasedFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,7 @@ KResult BlockBasedFileSystem::write_block(BlockIndex index, const UserOrKernelBu
if (!allow_cache) {
flush_specific_block_if_needed(index);
auto base_offset = index.value() * block_size() + offset;
auto seek_result = file_description().seek(base_offset, SEEK_SET);
if (seek_result.is_error())
return seek_result.error();
auto nwritten = file_description().write(data, count);
auto nwritten = file_description().write(base_offset, data, count);
if (nwritten.is_error())
return nwritten.error();
VERIFY(nwritten.value() == count);
Expand All @@ -153,9 +150,7 @@ bool BlockBasedFileSystem::raw_read(BlockIndex index, UserOrKernelBuffer& buffer
{
Locker locker(m_lock);
auto base_offset = index.value() * m_logical_block_size;
auto seek_result = file_description().seek(base_offset, SEEK_SET);
VERIFY(!seek_result.is_error());
auto nread = file_description().read(buffer, m_logical_block_size);
auto nread = file_description().read(buffer, base_offset, m_logical_block_size);
VERIFY(!nread.is_error());
VERIFY(nread.value() == m_logical_block_size);
return true;
Expand All @@ -165,9 +160,7 @@ bool BlockBasedFileSystem::raw_write(BlockIndex index, const UserOrKernelBuffer&
{
Locker locker(m_lock);
auto base_offset = index.value() * m_logical_block_size;
auto seek_result = file_description().seek(base_offset, SEEK_SET);
VERIFY(!seek_result.is_error());
auto nwritten = file_description().write(buffer, m_logical_block_size);
auto nwritten = file_description().write(base_offset, buffer, m_logical_block_size);
VERIFY(!nwritten.is_error());
VERIFY(nwritten.value() == m_logical_block_size);
return true;
Expand Down Expand Up @@ -220,10 +213,7 @@ KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* b
if (!allow_cache) {
const_cast<BlockBasedFileSystem*>(this)->flush_specific_block_if_needed(index);
auto base_offset = index.value() * block_size() + offset;
auto seek_result = file_description().seek(base_offset, SEEK_SET);
if (seek_result.is_error())
return seek_result.error();
auto nread = file_description().read(*buffer, count);
auto nread = file_description().read(*buffer, base_offset, count);
if (nread.is_error())
return nread.error();
VERIFY(nread.value() == count);
Expand All @@ -233,11 +223,8 @@ KResult BlockBasedFileSystem::read_block(BlockIndex index, UserOrKernelBuffer* b
auto& entry = cache().get(index);
if (!entry.has_data) {
auto base_offset = index.value() * block_size();
auto seek_result = file_description().seek(base_offset, SEEK_SET);
if (seek_result.is_error())
return seek_result.error();
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
auto nread = file_description().read(entry_data_buffer, block_size());
auto nread = file_description().read(entry_data_buffer, base_offset, block_size());
if (nread.is_error())
return nread.error();
VERIFY(nread.value() == block_size());
Expand Down Expand Up @@ -276,11 +263,8 @@ void BlockBasedFileSystem::flush_specific_block_if_needed(BlockIndex index)
cache().for_each_dirty_entry([&](CacheEntry& entry) {
if (entry.block_index != index) {
size_t base_offset = entry.block_index.value() * block_size();
auto seek_result = file_description().seek(base_offset, SEEK_SET);
VERIFY(!seek_result.is_error());
// FIXME: Should this error path be surfaced somehow?
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
[[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
[[maybe_unused]] auto rc = file_description().write(base_offset, entry_data_buffer, block_size());
cleaned_entries.append(&entry);
}
});
Expand All @@ -298,11 +282,8 @@ void BlockBasedFileSystem::flush_writes_impl()
u32 count = 0;
cache().for_each_dirty_entry([&](CacheEntry& entry) {
auto base_offset = entry.block_index.value() * block_size();
auto seek_result = file_description().seek(base_offset, SEEK_SET);
VERIFY(!seek_result.is_error());
// FIXME: Should this error path be surfaced somehow?
auto entry_data_buffer = UserOrKernelBuffer::for_kernel_buffer(entry.data);
[[maybe_unused]] auto rc = file_description().write(entry_data_buffer, block_size());
[[maybe_unused]] auto rc = file_description().write(base_offset, entry_data_buffer, block_size());
++count;
});
cache().mark_all_clean();
Expand Down

0 comments on commit abf0249

Please sign in to comment.