From ecee76b7418c4b3ed17a0a8690e8f8cf6c0736a1 Mon Sep 17 00:00:00 2001 From: Liav A Date: Mon, 6 Apr 2020 11:54:21 +0300 Subject: [PATCH] Kernel: Change Ext2FS to be backed by a file instead of a block device In contrast to the previous patchset that was reverted, this time we use a "special" method to access a file with block size of 512 bytes (like a harddrive essentially). --- Kernel/FileSystem/Ext2FileSystem.cpp | 14 +-- Kernel/FileSystem/Ext2FileSystem.h | 9 +- ...ileSystem.cpp => FileBackedFileSystem.cpp} | 98 ++++++++++++------- ...kedFileSystem.h => FileBackedFileSystem.h} | 24 +++-- Kernel/FileSystem/FileDescription.cpp | 11 +-- Kernel/FileSystem/FileSystem.h | 2 +- Kernel/FileSystem/ProcFS.cpp | 8 +- Kernel/FileSystem/VirtualFileSystem.cpp | 11 +-- Kernel/Makefile | 2 +- Kernel/init.cpp | 2 +- 10 files changed, 104 insertions(+), 77 deletions(-) rename Kernel/FileSystem/{DiskBackedFileSystem.cpp => FileBackedFileSystem.cpp} (68%) rename Kernel/FileSystem/{DiskBackedFileSystem.h => FileBackedFileSystem.h} (72%) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 982148a9a2..7b36144640 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -64,13 +64,13 @@ static u8 to_ext2_file_type(mode_t mode) return EXT2_FT_UNKNOWN; } -NonnullRefPtr Ext2FS::create(BlockDevice& device) +NonnullRefPtr Ext2FS::create(FileDescription& file_description) { - return adopt(*new Ext2FS(device)); + return adopt(*new Ext2FS(file_description)); } -Ext2FS::Ext2FS(BlockDevice& device) - : DiskBackedFS(device) +Ext2FS::Ext2FS(FileDescription& file_description) + : FileBackedFS(file_description) { } @@ -81,7 +81,7 @@ Ext2FS::~Ext2FS() bool Ext2FS::flush_super_block() { LOCKER(m_lock); - bool success = device().write_blocks(2, 1, (const u8*)&m_super_block); + bool success = raw_write(2, (const u8*)&m_super_block); ASSERT(success); return true; } @@ -96,7 +96,7 @@ const ext2_group_desc& Ext2FS::group_descriptor(GroupIndex group_index) const bool Ext2FS::initialize() { LOCKER(m_lock); - bool success = const_cast(device()).read_blocks(2, 1, (u8*)&m_super_block); + bool success = raw_read(2, (u8*)&m_super_block); ASSERT(success); auto& super_block = this->super_block(); @@ -543,7 +543,7 @@ void Ext2FS::flush_writes() } } - DiskBackedFS::flush_writes(); + FileBackedFS::flush_writes(); // 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. diff --git a/Kernel/FileSystem/Ext2FileSystem.h b/Kernel/FileSystem/Ext2FileSystem.h index 526d40db87..ac69be96bc 100644 --- a/Kernel/FileSystem/Ext2FileSystem.h +++ b/Kernel/FileSystem/Ext2FileSystem.h @@ -28,7 +28,7 @@ #include #include -#include +#include #include #include #include @@ -88,11 +88,12 @@ private: ext2_inode m_raw_inode; }; -class Ext2FS final : public DiskBackedFS { +class Ext2FS final : public FileBackedFS { friend class Ext2FSInode; public: - static NonnullRefPtr create(BlockDevice&); + static NonnullRefPtr create(FileDescription&); + virtual ~Ext2FS() override; virtual bool initialize() override; @@ -109,7 +110,7 @@ private: typedef unsigned BlockIndex; typedef unsigned GroupIndex; typedef unsigned InodeIndex; - explicit Ext2FS(BlockDevice&); + explicit Ext2FS(FileDescription&); const ext2_super_block& super_block() const { return m_super_block; } const ext2_group_desc& group_descriptor(GroupIndex) const; diff --git a/Kernel/FileSystem/DiskBackedFileSystem.cpp b/Kernel/FileSystem/FileBackedFileSystem.cpp similarity index 68% rename from Kernel/FileSystem/DiskBackedFileSystem.cpp rename to Kernel/FileSystem/FileBackedFileSystem.cpp index 1e955b3633..a3d30b90c9 100644 --- a/Kernel/FileSystem/DiskBackedFileSystem.cpp +++ b/Kernel/FileSystem/FileBackedFileSystem.cpp @@ -27,12 +27,12 @@ #include #include #include -#include +#include #include #include #include -//#define DBFS_DEBUG +//#define FBFS_DEBUG namespace Kernel { @@ -46,7 +46,7 @@ struct CacheEntry { class DiskCache { public: - explicit DiskCache(DiskBackedFS& fs) + explicit DiskCache(FileBackedFS& fs) : m_fs(fs) , 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))) @@ -81,8 +81,8 @@ public: } if (!oldest_clean_entry) { // Not a single clean entry! Flush writes and try again. - // NOTE: We want to make sure we only call DiskBackedFS flush here, - // not some DiskBackedFS subclass flush! + // NOTE: We want to make sure we only call FileBackedFS flush here, + // not some FileBackedFS subclass flush! m_fs.flush_writes_impl(); return get(block_index); } @@ -107,26 +107,28 @@ public: } private: - DiskBackedFS& m_fs; + FileBackedFS& m_fs; size_t m_entry_count { 10000 }; KBuffer m_cached_block_data; KBuffer m_entries; bool m_dirty { false }; }; -DiskBackedFS::DiskBackedFS(BlockDevice& device) - : m_device(device) +FileBackedFS::FileBackedFS(FileDescription& file_description) + : m_file_description(file_description) +{ + ASSERT(m_file_description->file().is_seekable()); +} + +FileBackedFS::~FileBackedFS() { } -DiskBackedFS::~DiskBackedFS() +bool FileBackedFS::write_block(unsigned index, const u8* data, FileDescription* description) { -} - -bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* description) -{ -#ifdef DBFS_DEBUG - klog() << "DiskBackedFileSystem::write_block " << index << ", size=" << data.size(); + ASSERT(m_logical_block_size); +#ifdef FBFS_DEBUG + klog() << "FileBackedFileSystem::write_block " << index << ", size=" << data.size(); #endif bool allow_cache = !description || !description->is_direct(); @@ -134,7 +136,9 @@ bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* if (!allow_cache) { flush_specific_block_if_needed(index); u32 base_offset = static_cast(index) * static_cast(block_size()); - device().write_raw(base_offset, block_size(), data); + m_file_description->seek(base_offset, SEEK_SET); + auto nwritten = m_file_description->write(data, block_size()); + ASSERT(nwritten == block_size()); return true; } @@ -147,45 +151,67 @@ bool DiskBackedFS::write_block(unsigned index, const u8* data, FileDescription* return true; } -bool DiskBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description) +bool FileBackedFS::raw_read(unsigned index, u8* buffer) { -#ifdef DBFS_DEBUG - klog() << "DiskBackedFileSystem::write_blocks " << index << " x" << count; + u32 base_offset = static_cast(index) * static_cast(m_logical_block_size); + m_file_description->seek(base_offset, SEEK_SET); + auto nread = m_file_description->read(buffer, m_logical_block_size); + ASSERT((size_t)nread == m_logical_block_size); + return true; +} +bool FileBackedFS::raw_write(unsigned index, const u8* buffer) +{ + u32 base_offset = static_cast(index) * static_cast(m_logical_block_size); + m_file_description->seek(base_offset, SEEK_SET); + auto nwritten = m_file_description->write(buffer, m_logical_block_size); + ASSERT((size_t)nwritten == m_logical_block_size); + return true; +} + +bool FileBackedFS::write_blocks(unsigned index, unsigned count, const u8* data, FileDescription* description) +{ + ASSERT(m_logical_block_size); +#ifdef FBFS_DEBUG + klog() << "FileBackedFileSystem::write_blocks " << index << " x" << count; #endif for (unsigned i = 0; i < count; ++i) write_block(index + i, data + i * block_size(), description); return true; } -bool DiskBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const +bool FileBackedFS::read_block(unsigned index, u8* buffer, FileDescription* description) const { -#ifdef DBFS_DEBUG - klog() << "DiskBackedFileSystem::read_block " << index; + ASSERT(m_logical_block_size); +#ifdef FBFS_DEBUG + klog() << "FileBackedFileSystem::read_block " << index; #endif bool allow_cache = !description || !description->is_direct(); if (!allow_cache) { - const_cast(this)->flush_specific_block_if_needed(index); + const_cast(this)->flush_specific_block_if_needed(index); u32 base_offset = static_cast(index) * static_cast(block_size()); - bool success = device().read_raw(base_offset, block_size(), buffer); - ASSERT(success); + const_cast(*m_file_description).seek(base_offset, SEEK_SET); + auto nread = const_cast(*m_file_description).read(buffer, block_size()); + ASSERT(nread == block_size()); return true; } auto& entry = cache().get(index); if (!entry.has_data) { u32 base_offset = static_cast(index) * static_cast(block_size()); - bool success = device().read_raw(base_offset, block_size(), entry.data); + const_cast(*m_file_description).seek(base_offset, SEEK_SET); + auto nread = const_cast(*m_file_description).read(entry.data, block_size()); entry.has_data = true; - ASSERT(success); + ASSERT(nread == block_size()); } memcpy(buffer, entry.data, block_size()); return true; } -bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const +bool FileBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* description) const { + ASSERT(m_logical_block_size); if (!count) return false; if (count == 1) @@ -201,7 +227,7 @@ bool DiskBackedFS::read_blocks(unsigned index, unsigned count, u8* buffer, FileD return true; } -void DiskBackedFS::flush_specific_block_if_needed(unsigned index) +void FileBackedFS::flush_specific_block_if_needed(unsigned index) { LOCKER(m_lock); if (!cache().is_dirty()) @@ -209,13 +235,14 @@ void DiskBackedFS::flush_specific_block_if_needed(unsigned index) cache().for_each_entry([&](CacheEntry& entry) { if (entry.is_dirty && entry.block_index == index) { u32 base_offset = static_cast(entry.block_index) * static_cast(block_size()); - device().write_raw(base_offset, block_size(), entry.data); + m_file_description->seek(base_offset, SEEK_SET); + m_file_description->write(entry.data, block_size()); entry.is_dirty = false; } }); } -void DiskBackedFS::flush_writes_impl() +void FileBackedFS::flush_writes_impl() { LOCKER(m_lock); if (!cache().is_dirty()) @@ -225,7 +252,8 @@ void DiskBackedFS::flush_writes_impl() if (!entry.is_dirty) return; u32 base_offset = static_cast(entry.block_index) * static_cast(block_size()); - device().write_raw(base_offset, block_size(), entry.data); + m_file_description->seek(base_offset, SEEK_SET); + m_file_description->write(entry.data, block_size()); ++count; entry.is_dirty = false; }); @@ -233,15 +261,15 @@ void DiskBackedFS::flush_writes_impl() dbg() << class_name() << ": Flushed " << count << " blocks to disk"; } -void DiskBackedFS::flush_writes() +void FileBackedFS::flush_writes() { flush_writes_impl(); } -DiskCache& DiskBackedFS::cache() const +DiskCache& FileBackedFS::cache() const { if (!m_cache) - m_cache = make(const_cast(*this)); + m_cache = make(const_cast(*this)); return *m_cache; } diff --git a/Kernel/FileSystem/DiskBackedFileSystem.h b/Kernel/FileSystem/FileBackedFileSystem.h similarity index 72% rename from Kernel/FileSystem/DiskBackedFileSystem.h rename to Kernel/FileSystem/FileBackedFileSystem.h index a5909eaf39..ac2fa9fca3 100644 --- a/Kernel/FileSystem/DiskBackedFileSystem.h +++ b/Kernel/FileSystem/FileBackedFileSystem.h @@ -26,38 +26,48 @@ #pragma once +#include #include #include namespace Kernel { -class DiskBackedFS : public FS { +class FileBackedFS : public FS { public: - virtual ~DiskBackedFS() override; + virtual ~FileBackedFS() override; - virtual bool is_disk_backed() const override { return true; } + virtual bool is_file_backed() const override { return true; } - BlockDevice& device() { return *m_device; } - const BlockDevice& device() const { return *m_device; } + File& file() { return m_file_description->file(); } + FileDescription& file_description() { return *m_file_description; } + const File& file() const { return m_file_description->file(); } + const FileDescription& file_description() const { return *m_file_description; } virtual void flush_writes() override; void flush_writes_impl(); + size_t logical_block_size() const { return m_logical_block_size; }; + protected: - explicit DiskBackedFS(BlockDevice&); + explicit FileBackedFS(FileDescription&); bool read_block(unsigned index, u8* buffer, FileDescription* = nullptr) const; bool read_blocks(unsigned index, unsigned count, u8* buffer, FileDescription* = nullptr) const; + bool raw_read(unsigned index, u8* buffer); + bool raw_write(unsigned index, const u8* buffer); + bool write_block(unsigned index, const u8*, FileDescription* = nullptr); bool write_blocks(unsigned index, unsigned count, const u8*, FileDescription* = nullptr); + size_t m_logical_block_size { 512 }; + private: DiskCache& cache() const; void flush_specific_block_if_needed(unsigned index); - NonnullRefPtr m_device; + NonnullRefPtr m_file_description; mutable OwnPtr m_cache; }; diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index dfe169f582..88ff913ca7 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -98,13 +98,6 @@ off_t FileDescription::seek(off_t offset, int whence) if (!m_file->is_seekable()) 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; switch (whence) { @@ -115,7 +108,9 @@ off_t FileDescription::seek(off_t offset, int whence) new_offset = m_current_offset + offset; break; case SEEK_END: - new_offset = metadata.size; + if (!metadata().is_valid()) + return -EIO; + new_offset = metadata().size; break; default: return -EINVAL; diff --git a/Kernel/FileSystem/FileSystem.h b/Kernel/FileSystem/FileSystem.h index 32e9fbf47e..4eb70fd89e 100644 --- a/Kernel/FileSystem/FileSystem.h +++ b/Kernel/FileSystem/FileSystem.h @@ -87,7 +87,7 @@ public: int block_size() const { return m_block_size; } - virtual bool is_disk_backed() const { return false; } + virtual bool is_file_backed() const { return false; } protected: FS(); diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index f3c1880be4..2247b3d9e2 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include #include @@ -746,10 +746,10 @@ Optional procfs$df(InodeIdentifier) fs_object.add("readonly", fs.is_readonly()); fs_object.add("mount_flags", mount.flags()); - if (fs.is_disk_backed()) - fs_object.add("device", static_cast(fs).device().absolute_path()); + if (fs.is_file_backed()) + fs_object.add("source", static_cast(fs).file_description().absolute_path()); else - fs_object.add("device", fs.class_name()); + fs_object.add("source", fs.class_name()); }); array.finish(); return builder.build(); diff --git a/Kernel/FileSystem/VirtualFileSystem.cpp b/Kernel/FileSystem/VirtualFileSystem.cpp index 08e2253e32..9f3380c803 100644 --- a/Kernel/FileSystem/VirtualFileSystem.cpp +++ b/Kernel/FileSystem/VirtualFileSystem.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include @@ -126,14 +126,7 @@ bool VFS::mount_root(FS& file_system) } m_root_inode = move(root_inode); - char device_name[32]; - if (m_root_inode->fs().is_disk_backed()) { - auto& device = static_cast(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 << ")"; + klog() << "VFS: mounted root from " << m_root_inode->fs().class_name() << " (" << static_cast(m_root_inode->fs()).file_description().absolute_path() << ")"; m_mounts.append(move(mount)); return true; diff --git a/Kernel/Makefile b/Kernel/Makefile index da739aa8cf..992e93bd56 100644 --- a/Kernel/Makefile +++ b/Kernel/Makefile @@ -56,8 +56,8 @@ OBJS = \ DoubleBuffer.o \ FileSystem/Custody.o \ FileSystem/DevPtsFS.o \ - FileSystem/DiskBackedFileSystem.o \ FileSystem/Ext2FileSystem.o \ + FileSystem/FileBackedFileSystem.o \ FileSystem/FIFO.o \ FileSystem/File.o \ FileSystem/FileDescription.o \ diff --git a/Kernel/init.cpp b/Kernel/init.cpp index 5b9777d3c0..ddb622f40f 100644 --- a/Kernel/init.cpp +++ b/Kernel/init.cpp @@ -292,7 +292,7 @@ void init_stage2() } } } - auto e2fs = Ext2FS::create(root_dev); + auto e2fs = Ext2FS::create(*FileDescription::create(root_dev)); if (!e2fs->initialize()) { klog() << "init_stage2: couldn't open root filesystem"; hang();