Revert "Kernel: Change Ext2FS to be backed by a file instead of a block device"

This reverts commit 6b59311d4b.

Reverting these changes since they broke things.
Fixes #1608.
This commit is contained in:
Andreas Kling 2020-04-03 21:20:51 +02:00
parent 9ae3cced76
commit c2a8bbcb59
Notes: sideshowbarker 2024-07-19 07:58:15 +09:00
10 changed files with 90 additions and 116 deletions

View File

@ -27,12 +27,12 @@
#include <AK/StringView.h> #include <AK/StringView.h>
#include <Kernel/Arch/i386/CPU.h> #include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Devices/BlockDevice.h> #include <Kernel/Devices/BlockDevice.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h> #include <Kernel/FileSystem/DiskBackedFileSystem.h>
#include <Kernel/FileSystem/FileDescription.h> #include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/KBuffer.h> #include <Kernel/KBuffer.h>
#include <Kernel/Process.h> #include <Kernel/Process.h>
//#define FBFS_DEBUG //#define DBFS_DEBUG
namespace Kernel { namespace Kernel {
@ -46,7 +46,7 @@ struct CacheEntry {
class DiskCache { class DiskCache {
public: public:
explicit DiskCache(FileBackedFS& fs) explicit DiskCache(DiskBackedFS& fs)
: m_fs(fs) : m_fs(fs)
, m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size())) , m_cached_block_data(KBuffer::create_with_size(m_entry_count * m_fs.block_size()))
, m_entries(KBuffer::create_with_size(m_entry_count * sizeof(CacheEntry))) , m_entries(KBuffer::create_with_size(m_entry_count * sizeof(CacheEntry)))
@ -81,8 +81,8 @@ public:
} }
if (!oldest_clean_entry) { if (!oldest_clean_entry) {
// Not a single clean entry! Flush writes and try again. // Not a single clean entry! Flush writes and try again.
// NOTE: We want to make sure we only call FileBackedFS flush here, // NOTE: We want to make sure we only call DiskBackedFS flush here,
// not some FileBackedFS subclass flush! // not some DiskBackedFS subclass flush!
m_fs.flush_writes_impl(); m_fs.flush_writes_impl();
return get(block_index); return get(block_index);
} }
@ -107,52 +107,39 @@ public:
} }
private: private:
FileBackedFS& m_fs; DiskBackedFS& m_fs;
size_t m_entry_count { 10000 }; size_t m_entry_count { 10000 };
KBuffer m_cached_block_data; KBuffer m_cached_block_data;
KBuffer m_entries; KBuffer m_entries;
bool m_dirty { false }; bool m_dirty { false };
}; };
FileBackedFS::FileBackedFS(FileDescription& file_description) DiskBackedFS::DiskBackedFS(BlockDevice& device)
: m_file_description(file_description) : m_device(device)
{
ASSERT(m_file_description->file().is_seekable());
}
FileBackedFS::~FileBackedFS()
{ {
} }
bool FileBackedFS::write_block(unsigned index, const u8* data, FileDescription* description, bool use_logical_block_size) DiskBackedFS::~DiskBackedFS()
{ {
ASSERT(m_logical_block_size); }
#ifdef FBFS_DEBUG
klog() << "FileBackedFileSystem::write_block " << index << ", size=" << data.size(); bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* description)
{
#ifdef DBFS_DEBUG
klog() << "DiskBackedFileSystem::write_block " << index << ", size=" << data.size();
#endif #endif
bool allow_cache = !description || !description->is_direct(); bool allow_cache = !description || !description->is_direct();
/* We need to distinguish between "virtual disk reads" and "file system
reads". Previously, when we read the ext2 superblock for example, we called
device().read_raw() to fetch the superblock. Now we can't use that call
anymore because we are not backed by a BlockDevice, so the block size
(sector size) for reading the superblock is still 512 bytes like when we
used device().read_raw(), but we need to implement such functionality in the
FileBackedFileSystem layer instead. */
auto effective_block_size = use_logical_block_size ? m_logical_block_size : block_size();
if (!allow_cache) { if (!allow_cache) {
flush_specific_block_if_needed(index); flush_specific_block_if_needed(index);
u32 base_offset = static_cast<u32>(index) * static_cast<u32>(effective_block_size); u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
m_file_description->seek(base_offset, SEEK_SET); device().write_raw(base_offset, block_size(), data);
auto nwritten = m_file_description->write(data, effective_block_size);
ASSERT((size_t)nwritten == effective_block_size);
return true; return true;
} }
auto& entry = cache().get(index); auto& entry = cache().get(index);
memcpy(entry.data, data, effective_block_size); memcpy(entry.data, data, block_size());
entry.is_dirty = true; entry.is_dirty = true;
entry.has_data = true; entry.has_data = true;
@ -160,68 +147,53 @@ bool FileBackedFS::write_block(unsigned index, const u8* data, FileDescription*
return true; return true;
} }
bool FileBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description, bool use_logical_block_size) bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description)
{ {
ASSERT(m_logical_block_size); #ifdef DBFS_DEBUG
#ifdef FBFS_DEBUG klog() << "DiskBackedFileSystem::write_blocks " << index << " x" << count;
klog() << "FileBackedFileSystem::write_blocks " << index << " x" << count;
#endif #endif
for (unsigned i = 0; i < count; ++i) for (unsigned i = 0; i < count; ++i)
write_block(index + i, data + i * block_size(), description, use_logical_block_size); write_block(index + i, data + i * block_size(), description);
return true; return true;
} }
bool FileBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description, bool use_logical_block_size, bool cache_disabled) const bool DiskBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const
{ {
ASSERT(m_logical_block_size); #ifdef DBFS_DEBUG
#ifdef FBFS_DEBUG klog() << "DiskBackedFileSystem::read_block " << index;
klog() << "FileBackedFileSystem::read_block " << index;
#endif #endif
bool allow_cache = !description || !description->is_direct(); bool allow_cache = !description || !description->is_direct();
/* We need to distinguish between "virtual disk reads" and "file system if (!allow_cache) {
reads". Previously, when we read the ext2 superblock for example, we called const_cast<DiskBackedFS*>(this)->flush_specific_block_if_needed(index);
device().read_raw() to fetch the superblock. Now we can't use that call u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
anymore because we are not backed by a BlockDevice, so the block size bool success = device().read_raw(base_offset, block_size(), buffer);
(sector size) for reading the superblock is still 512 bytes like when we ASSERT(success);
used device().read_raw(), but we need to implement such functionality in the
FileBackedFileSystem layer instead. */
auto effective_block_size = use_logical_block_size ? m_logical_block_size : block_size();
if (!allow_cache || cache_disabled) {
if (!cache_disabled)
const_cast<FileBackedFS*>(this)->flush_specific_block_if_needed(index);
u32 base_offset = static_cast<u32>(index) * static_cast<u32>(effective_block_size);
const_cast<FileDescription&>(*m_file_description).seek(base_offset, SEEK_SET);
auto nread = const_cast<FileDescription&>(*m_file_description).read(buffer, effective_block_size);
ASSERT((size_t)nread == effective_block_size);
return true; return true;
} }
auto& entry = cache().get(index); auto& entry = cache().get(index);
if (!entry.has_data) { if (!entry.has_data) {
u32 base_offset = static_cast<u32>(index) * static_cast<u32>(effective_block_size); u32 base_offset = static_cast<u32>(index) * static_cast<u32>(block_size());
const_cast<FileDescription&>(*m_file_description).seek(base_offset, SEEK_SET); bool success = device().read_raw(base_offset, block_size(), entry.data);
auto nread = const_cast<FileDescription&>(*m_file_description).read(entry.data, effective_block_size);
entry.has_data = true; entry.has_data = true;
ASSERT((size_t)nread == effective_block_size); ASSERT(success);
} }
memcpy(buffer, entry.data, effective_block_size); memcpy(buffer, entry.data, block_size());
return true; return true;
} }
bool FileBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description, bool use_logical_block_size, bool cache_disabled) const bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const
{ {
ASSERT(m_logical_block_size);
if (!count) if (!count)
return false; return false;
if (count == 1) if (count == 1)
return read_block(index, buffer, description, use_logical_block_size, cache_disabled); return read_block(index, buffer, description);
u8* out = buffer; u8* out = buffer;
for (unsigned i = 0; i < count; ++i) { for (unsigned i = 0; i < count; ++i) {
if (!read_block(index + i, out, description, use_logical_block_size, cache_disabled)) if (!read_block(index + i, out, description))
return false; return false;
out += block_size(); out += block_size();
} }
@ -229,7 +201,7 @@ bool FileBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileD
return true; return true;
} }
void FileBackedFS::flush_specific_block_if_needed(unsigned index) void DiskBackedFS::flush_specific_block_if_needed(unsigned index)
{ {
LOCKER(m_lock); LOCKER(m_lock);
if (!cache().is_dirty()) if (!cache().is_dirty())
@ -237,14 +209,13 @@ void FileBackedFS::flush_specific_block_if_needed(unsigned index)
cache().for_each_entry([&](CacheEntry& entry) { cache().for_each_entry([&](CacheEntry& entry) {
if (entry.is_dirty && entry.block_index == index) { if (entry.is_dirty && entry.block_index == index) {
u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size()); u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
m_file_description->seek(base_offset, SEEK_SET); device().write_raw(base_offset, block_size(), entry.data);
m_file_description->write(entry.data, block_size());
entry.is_dirty = false; entry.is_dirty = false;
} }
}); });
} }
void FileBackedFS::flush_writes_impl() void DiskBackedFS::flush_writes_impl()
{ {
LOCKER(m_lock); LOCKER(m_lock);
if (!cache().is_dirty()) if (!cache().is_dirty())
@ -254,8 +225,7 @@ void FileBackedFS::flush_writes_impl()
if (!entry.is_dirty) if (!entry.is_dirty)
return; return;
u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size()); u32 base_offset = static_cast<u32>(entry.block_index) * static_cast<u32>(block_size());
m_file_description->seek(base_offset, SEEK_SET); device().write_raw(base_offset, block_size(), entry.data);
m_file_description->write(entry.data, block_size());
++count; ++count;
entry.is_dirty = false; entry.is_dirty = false;
}); });
@ -263,15 +233,15 @@ void FileBackedFS::flush_writes_impl()
dbg() << class_name() << ": Flushed " << count << " blocks to disk"; dbg() << class_name() << ": Flushed " << count << " blocks to disk";
} }
void FileBackedFS::flush_writes() void DiskBackedFS::flush_writes()
{ {
flush_writes_impl(); flush_writes_impl();
} }
DiskCache& FileBackedFS::cache() const DiskCache& DiskBackedFS::cache() const
{ {
if (!m_cache) if (!m_cache)
m_cache = make<DiskCache>(const_cast<FileBackedFS&>(*this)); m_cache = make<DiskCache>(const_cast<DiskBackedFS&>(*this));
return *m_cache; return *m_cache;
} }

View File

@ -26,45 +26,38 @@
#pragma once #pragma once
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/FileSystem.h> #include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/Forward.h> #include <Kernel/Forward.h>
namespace Kernel { namespace Kernel {
class FileBackedFS : public FS { class DiskBackedFS : public FS {
public: public:
virtual ~FileBackedFS() override; virtual ~DiskBackedFS() override;
virtual bool is_file_backed() const override { return true; } virtual bool is_disk_backed() const override { return true; }
File& file() { return m_file_description->file(); } BlockDevice& device() { return *m_device; }
FileDescription& file_description() { return *m_file_description; } const BlockDevice& device() const { return *m_device; }
const File& file() const { return m_file_description->file(); }
const FileDescription& file_description() const { return *m_file_description; }
virtual void flush_writes() override; virtual void flush_writes() override;
void flush_writes_impl(); void flush_writes_impl();
size_t logical_block_size() const { return m_logical_block_size; };
protected: protected:
explicit FileBackedFS(FileDescription&); explicit DiskBackedFS(BlockDevice&);
bool read_block(unsigned index, u8* buffer, FileDescription* = nullptr, bool use_logical_block_size = false, bool cache_disabled = false) const; bool read_block(unsigned index, u8* buffer, FileDescription* = nullptr) const;
bool read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* = nullptr, bool use_logical_block_size = false, bool cache_disabled = false) const; bool read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* = nullptr) const;
bool write_block(unsigned index, const u8*, FileDescription* = nullptr, bool use_logical_block_size = false); bool write_block(unsigned index, const u8*, FileDescription* = nullptr);
bool write_blocks(unsigned index, unsigned count, const u8*, FileDescription* = nullptr, bool use_logical_block_size = false); bool write_blocks(unsigned index, unsigned count, const u8*, FileDescription* = nullptr);
size_t m_logical_block_size { 512 };
private: private:
DiskCache& cache() const; DiskCache& cache() const;
void flush_specific_block_if_needed(unsigned index); void flush_specific_block_if_needed(unsigned index);
NonnullRefPtr<FileDescription> m_file_description; NonnullRefPtr<BlockDevice> m_device;
mutable OwnPtr<DiskCache> m_cache; mutable OwnPtr<DiskCache> m_cache;
}; };

View File

@ -64,13 +64,13 @@ static u8 to_ext2_file_type(mode_t mode)
return EXT2_FT_UNKNOWN; return EXT2_FT_UNKNOWN;
} }
NonnullRefPtr<Ext2FS> Ext2FS::create(FileDescription& file_description) NonnullRefPtr<Ext2FS> Ext2FS::create(BlockDevice& device)
{ {
return adopt(*new Ext2FS(file_description)); return adopt(*new Ext2FS(device));
} }
Ext2FS::Ext2FS(FileDescription& file_description) Ext2FS::Ext2FS(BlockDevice& device)
: FileBackedFS(file_description) : DiskBackedFS(device)
{ {
} }
@ -81,7 +81,7 @@ Ext2FS::~Ext2FS()
bool Ext2FS::flush_super_block() bool Ext2FS::flush_super_block()
{ {
LOCKER(m_lock); LOCKER(m_lock);
bool success = write_blocks(2, 1, (const u8*)&m_super_block, nullptr, true); bool success = device().write_blocks(2, 1, (const u8*)&m_super_block);
ASSERT(success); ASSERT(success);
return true; return true;
} }
@ -96,7 +96,7 @@ const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const
bool Ext2FS::initialize() bool Ext2FS::initialize()
{ {
LOCKER(m_lock); LOCKER(m_lock);
bool success = read_blocks(2, 1, (u8*)&m_super_block, nullptr, true, true); bool success = const_cast<BlockDevice&>(device()).read_blocks(2, 1, (u8*)&m_super_block);
ASSERT(success); ASSERT(success);
auto& super_block = this->super_block(); auto& super_block = this->super_block();
@ -534,7 +534,7 @@ void Ext2FS::flush_writes()
} }
} }
FileBackedFS::flush_writes(); DiskBackedFS::flush_writes();
// Uncache Inodes that are only kept alive by the index-to-inode lookup cache. // Uncache Inodes that are only kept alive by the index-to-inode lookup cache.
// We don't uncache Inodes that are being watched by at least one InodeWatcher. // We don't uncache Inodes that are being watched by at least one InodeWatcher.

View File

@ -28,7 +28,7 @@
#include <AK/Bitmap.h> #include <AK/Bitmap.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h> #include <Kernel/FileSystem/DiskBackedFileSystem.h>
#include <Kernel/FileSystem/Inode.h> #include <Kernel/FileSystem/Inode.h>
#include <Kernel/FileSystem/ext2_fs.h> #include <Kernel/FileSystem/ext2_fs.h>
#include <Kernel/KBuffer.h> #include <Kernel/KBuffer.h>
@ -88,12 +88,11 @@ private:
ext2_inode m_raw_inode; ext2_inode m_raw_inode;
}; };
class Ext2FS final : public FileBackedFS { class Ext2FS final : public DiskBackedFS {
friend class Ext2FSInode; friend class Ext2FSInode;
public: public:
static NonnullRefPtr<Ext2FS> create(FileDescription&); static NonnullRefPtr<Ext2FS> create(BlockDevice&);
virtual ~Ext2FS() override; virtual ~Ext2FS() override;
virtual bool initialize() override; virtual bool initialize() override;
@ -110,7 +109,7 @@ private:
typedef unsigned BlockIndex; typedef unsigned BlockIndex;
typedef unsigned GroupIndex; typedef unsigned GroupIndex;
typedef unsigned InodeIndex; typedef unsigned InodeIndex;
explicit Ext2FS(FileDescription&); explicit Ext2FS(BlockDevice&);
const ext2_super_block& super_block() const { return m_super_block; } const ext2_super_block& super_block() const { return m_super_block; }
const ext2_group_desc& group_descriptor(GroupIndex) const; const ext2_group_desc& group_descriptor(GroupIndex) const;

View File

@ -98,6 +98,13 @@ off_t FileDescription::seek(off_t offset, int whence)
if (!m_file->is_seekable()) if (!m_file->is_seekable())
return -EINVAL; return -EINVAL;
auto metadata = this->metadata();
if (!metadata.is_valid())
return -EIO;
if (metadata.is_socket() || metadata.is_fifo())
return -ESPIPE;
off_t new_offset; off_t new_offset;
switch (whence) { switch (whence) {
@ -108,9 +115,7 @@ off_t FileDescription::seek(off_t offset, int whence)
new_offset = m_current_offset + offset; new_offset = m_current_offset + offset;
break; break;
case SEEK_END: case SEEK_END:
if (!metadata().is_valid()) new_offset = metadata.size;
return -EIO;
new_offset = metadata().size;
break; break;
default: default:
return -EINVAL; return -EINVAL;

View File

@ -87,7 +87,7 @@ public:
int block_size() const { return m_block_size; } int block_size() const { return m_block_size; }
virtual bool is_file_backed() const { return false; } virtual bool is_disk_backed() const { return false; }
protected: protected:
FS(); FS();

View File

@ -35,7 +35,7 @@
#include <Kernel/Arch/i386/CPU.h> #include <Kernel/Arch/i386/CPU.h>
#include <Kernel/Devices/BlockDevice.h> #include <Kernel/Devices/BlockDevice.h>
#include <Kernel/FileSystem/Custody.h> #include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h> #include <Kernel/FileSystem/DiskBackedFileSystem.h>
#include <Kernel/FileSystem/FileDescription.h> #include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/VirtualFileSystem.h> #include <Kernel/FileSystem/VirtualFileSystem.h>
#include <Kernel/Heap/kmalloc.h> #include <Kernel/Heap/kmalloc.h>
@ -746,10 +746,10 @@ Optional<KBuffer> procfs$df(InodeIdentifier)
fs_object.add("readonly", fs.is_readonly()); fs_object.add("readonly", fs.is_readonly());
fs_object.add("mount_flags", mount.flags()); fs_object.add("mount_flags", mount.flags());
if (fs.is_file_backed()) if (fs.is_disk_backed())
fs_object.add("source", static_cast<const FileBackedFS&>(fs).file_description().absolute_path()); fs_object.add("device", static_cast<const DiskBackedFS&>(fs).device().absolute_path());
else else
fs_object.add("source", fs.class_name()); fs_object.add("device", fs.class_name());
}); });
array.finish(); array.finish();
return builder.build(); return builder.build();

View File

@ -28,7 +28,7 @@
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <Kernel/Devices/BlockDevice.h> #include <Kernel/Devices/BlockDevice.h>
#include <Kernel/FileSystem/Custody.h> #include <Kernel/FileSystem/Custody.h>
#include <Kernel/FileSystem/FileBackedFileSystem.h> #include <Kernel/FileSystem/DiskBackedFileSystem.h>
#include <Kernel/FileSystem/FileDescription.h> #include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/FileSystem/FileSystem.h> #include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/VirtualFileSystem.h> #include <Kernel/FileSystem/VirtualFileSystem.h>
@ -126,7 +126,14 @@ bool VFS::mount_root(FS& file_system)
} }
m_root_inode = move(root_inode); m_root_inode = move(root_inode);
klog() << "VFS: mounted root from " << m_root_inode->fs().class_name() << " (" << static_cast<FileBackedFS&>(m_root_inode->fs()).file_description().absolute_path() << ")"; char device_name[32];
if (m_root_inode->fs().is_disk_backed()) {
auto& device = static_cast<DiskBackedFS&>(m_root_inode->fs()).device();
sprintf(device_name, "%d,%d", device.major(), device.minor());
} else {
sprintf(device_name, "not-a-disk");
}
klog() << "VFS: mounted root on " << m_root_inode->fs().class_name() << " (" << device_name << ")";
m_mounts.append(move(mount)); m_mounts.append(move(mount));
return true; return true;

View File

@ -56,8 +56,8 @@ OBJS = \
DoubleBuffer.o \ DoubleBuffer.o \
FileSystem/Custody.o \ FileSystem/Custody.o \
FileSystem/DevPtsFS.o \ FileSystem/DevPtsFS.o \
FileSystem/DiskBackedFileSystem.o \
FileSystem/Ext2FileSystem.o \ FileSystem/Ext2FileSystem.o \
FileSystem/FileBackedFileSystem.o \
FileSystem/FIFO.o \ FileSystem/FIFO.o \
FileSystem/File.o \ FileSystem/File.o \
FileSystem/FileDescription.o \ FileSystem/FileDescription.o \

View File

@ -292,7 +292,7 @@ void init_stage2()
} }
} }
} }
auto e2fs = Ext2FS::create(*FileDescription::create(root_dev)); auto e2fs = Ext2FS::create(root_dev);
if (!e2fs->initialize()) { if (!e2fs->initialize()) {
klog() << "init_stage2: couldn't open root filesystem"; klog() << "init_stage2: couldn't open root filesystem";
hang(); hang();