1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:38:10 +00:00

BlockDevice -> DiskDevice.

BlockDevice was the wrong name for this abstraction, since a block device
is a type of file in a unix system, and we should use that name for that
concept in the fs implementation.
This commit is contained in:
Andreas Kling 2018-10-16 11:21:49 +02:00
parent 72bb80a9ae
commit 9cd0a34b5c
12 changed files with 62 additions and 64 deletions

View file

@ -1,9 +0,0 @@
#include "BlockDevice.h"
BlockDevice::BlockDevice()
{
}
BlockDevice::~BlockDevice()
{
}

View file

@ -1,40 +1,40 @@
#include "DeviceBackedFileSystem.h" #include "DiskBackedFileSystem.h"
//#define DBFS_DEBUG //#define DBFS_DEBUG
DeviceBackedFileSystem::DeviceBackedFileSystem(RetainPtr<BlockDevice>&& device) DiskBackedFileSystem::DiskBackedFileSystem(RetainPtr<DiskDevice>&& device)
: m_device(std::move(device)) : m_device(std::move(device))
{ {
ASSERT(m_device); ASSERT(m_device);
} }
DeviceBackedFileSystem::~DeviceBackedFileSystem() DiskBackedFileSystem::~DiskBackedFileSystem()
{ {
} }
bool DeviceBackedFileSystem::writeBlock(unsigned index, const ByteBuffer& data) bool DiskBackedFileSystem::writeBlock(unsigned index, const ByteBuffer& data)
{ {
ASSERT(data.size() == blockSize()); ASSERT(data.size() == blockSize());
#ifdef DBFS_DEBUG #ifdef DBFS_DEBUG
printf("DeviceBackedFileSystem::writeBlock %u\n", index); printf("DiskBackedFileSystem::writeBlock %u\n", index);
#endif #endif
qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize()); qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
return device().write(baseOffset, blockSize(), data.pointer()); return device().write(baseOffset, blockSize(), data.pointer());
} }
bool DeviceBackedFileSystem::writeBlocks(unsigned index, unsigned count, const ByteBuffer& data) bool DiskBackedFileSystem::writeBlocks(unsigned index, unsigned count, const ByteBuffer& data)
{ {
#ifdef DBFS_DEBUG #ifdef DBFS_DEBUG
printf("DeviceBackedFileSystem::writeBlocks %u x%u\n", index, count); printf("DiskBackedFileSystem::writeBlocks %u x%u\n", index, count);
#endif #endif
qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize()); qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
return device().write(baseOffset, count * blockSize(), data.pointer()); return device().write(baseOffset, count * blockSize(), data.pointer());
} }
ByteBuffer DeviceBackedFileSystem::readBlock(unsigned index) const ByteBuffer DiskBackedFileSystem::readBlock(unsigned index) const
{ {
#ifdef DBFS_DEBUG #ifdef DBFS_DEBUG
printf("DeviceBackedFileSystem::readBlock %u\n", index); printf("DiskBackedFileSystem::readBlock %u\n", index);
#endif #endif
auto buffer = ByteBuffer::createUninitialized(blockSize()); auto buffer = ByteBuffer::createUninitialized(blockSize());
qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize()); qword baseOffset = static_cast<qword>(index) * static_cast<qword>(blockSize());
@ -44,7 +44,7 @@ ByteBuffer DeviceBackedFileSystem::readBlock(unsigned index) const
return buffer; return buffer;
} }
ByteBuffer DeviceBackedFileSystem::readBlocks(unsigned index, unsigned count) const ByteBuffer DiskBackedFileSystem::readBlocks(unsigned index, unsigned count) const
{ {
if (!count) if (!count)
return nullptr; return nullptr;
@ -64,7 +64,7 @@ ByteBuffer DeviceBackedFileSystem::readBlocks(unsigned index, unsigned count) co
return blocks; return blocks;
} }
void DeviceBackedFileSystem::setBlockSize(unsigned blockSize) void DiskBackedFileSystem::setBlockSize(unsigned blockSize)
{ {
if (blockSize == m_blockSize) if (blockSize == m_blockSize)
return; return;
@ -72,7 +72,7 @@ void DeviceBackedFileSystem::setBlockSize(unsigned blockSize)
invalidateCaches(); invalidateCaches();
} }
void DeviceBackedFileSystem::invalidateCaches() void DiskBackedFileSystem::invalidateCaches()
{ {
// FIXME: Implement block cache. // FIXME: Implement block cache.
} }

View file

@ -3,17 +3,17 @@
#include "FileSystem.h" #include "FileSystem.h"
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
class DeviceBackedFileSystem : public FileSystem { class DiskBackedFileSystem : public FileSystem {
public: public:
virtual ~DeviceBackedFileSystem() override; virtual ~DiskBackedFileSystem() override;
BlockDevice& device() { return *m_device; } DiskDevice& device() { return *m_device; }
const BlockDevice& device() const { return *m_device; } const DiskDevice& device() const { return *m_device; }
unsigned blockSize() const { return m_blockSize; } unsigned blockSize() const { return m_blockSize; }
protected: protected:
explicit DeviceBackedFileSystem(RetainPtr<BlockDevice>&&); explicit DiskBackedFileSystem(RetainPtr<DiskDevice>&&);
void setBlockSize(unsigned); void setBlockSize(unsigned);
void invalidateCaches(); void invalidateCaches();
@ -26,5 +26,5 @@ protected:
private: private:
unsigned m_blockSize { 0 }; unsigned m_blockSize { 0 };
RetainPtr<BlockDevice> m_device; RetainPtr<DiskDevice> m_device;
}; };

View file

@ -0,0 +1,9 @@
#include "DiskDevice.h"
DiskDevice::DiskDevice()
{
}
DiskDevice::~DiskDevice()
{
}

View file

@ -3,9 +3,9 @@
#include <AK/Retainable.h> #include <AK/Retainable.h>
#include <AK/Types.h> #include <AK/Types.h>
class BlockDevice : public Retainable<BlockDevice> { class DiskDevice : public Retainable<DiskDevice> {
public: public:
virtual ~BlockDevice(); virtual ~DiskDevice();
virtual unsigned blockSize() const = 0; virtual unsigned blockSize() const = 0;
virtual bool readBlock(unsigned index, byte*) const = 0; virtual bool readBlock(unsigned index, byte*) const = 0;
@ -15,6 +15,6 @@ public:
virtual bool write(qword offset, unsigned length, const byte*) = 0; virtual bool write(qword offset, unsigned length, const byte*) = 0;
protected: protected:
BlockDevice(); DiskDevice();
}; };

View file

@ -10,13 +10,13 @@
//#define EXT2_DEBUG //#define EXT2_DEBUG
RetainPtr<Ext2FileSystem> Ext2FileSystem::create(RetainPtr<BlockDevice> device) RetainPtr<Ext2FileSystem> Ext2FileSystem::create(RetainPtr<DiskDevice>&& device)
{ {
return adopt(*new Ext2FileSystem(std::move(device))); return adopt(*new Ext2FileSystem(std::move(device)));
} }
Ext2FileSystem::Ext2FileSystem(RetainPtr<BlockDevice> device) Ext2FileSystem::Ext2FileSystem(RetainPtr<DiskDevice>&& device)
: DeviceBackedFileSystem(std::move(device)) : DiskBackedFileSystem(std::move(device))
{ {
} }

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "DeviceBackedFileSystem.h" #include "DiskBackedFileSystem.h"
#include "UnixTypes.h" #include "UnixTypes.h"
#include <AK/Buffer.h> #include <AK/Buffer.h>
#include <AK/OwnPtr.h> #include <AK/OwnPtr.h>
@ -9,9 +9,9 @@ struct ext2_group_desc;
struct ext2_inode; struct ext2_inode;
struct ext2_super_block; struct ext2_super_block;
class Ext2FileSystem final : public DeviceBackedFileSystem { class Ext2FileSystem final : public DiskBackedFileSystem {
public: public:
static RetainPtr<Ext2FileSystem> create(RetainPtr<BlockDevice>); static RetainPtr<Ext2FileSystem> create(RetainPtr<DiskDevice>&&);
virtual ~Ext2FileSystem() override; virtual ~Ext2FileSystem() override;
private: private:
@ -19,7 +19,7 @@ private:
typedef unsigned GroupIndex; typedef unsigned GroupIndex;
typedef unsigned InodeIndex; typedef unsigned InodeIndex;
explicit Ext2FileSystem(RetainPtr<BlockDevice>); explicit Ext2FileSystem(RetainPtr<DiskDevice>&&);
const ext2_super_block& superBlock() const; const ext2_super_block& superBlock() const;
const ext2_group_desc& blockGroupDescriptor(unsigned groupIndex) const; const ext2_group_desc& blockGroupDescriptor(unsigned groupIndex) const;

View file

@ -1,18 +1,18 @@
#define _FILE_OFFSET_BITS 64 #define _FILE_OFFSET_BITS 64
#include "FileBackedBlockDevice.h" #include "FileBackedDiskDevice.h"
#include <cstring> #include <cstring>
#include <sys/stat.h> #include <sys/stat.h>
//#define FBBD_DEBUG //#define FBBD_DEBUG
#define IGNORE_FILE_LENGTH // Useful for e.g /dev/hda2 #define IGNORE_FILE_LENGTH // Useful for e.g /dev/hda2
RetainPtr<FileBackedBlockDevice> FileBackedBlockDevice::create(String&& imagePath, unsigned blockSize) RetainPtr<FileBackedDiskDevice> FileBackedDiskDevice::create(String&& imagePath, unsigned blockSize)
{ {
return adopt(*new FileBackedBlockDevice(std::move(imagePath), blockSize)); return adopt(*new FileBackedDiskDevice(std::move(imagePath), blockSize));
} }
FileBackedBlockDevice::FileBackedBlockDevice(String&& imagePath, unsigned blockSize) FileBackedDiskDevice::FileBackedDiskDevice(String&& imagePath, unsigned blockSize)
: m_imagePath(std::move(imagePath)) : m_imagePath(std::move(imagePath))
, m_blockSize(blockSize) , m_blockSize(blockSize)
{ {
@ -23,35 +23,35 @@ FileBackedBlockDevice::FileBackedBlockDevice(String&& imagePath, unsigned blockS
m_file = fopen(m_imagePath.characters(), "r+"); m_file = fopen(m_imagePath.characters(), "r+");
} }
FileBackedBlockDevice::~FileBackedBlockDevice() FileBackedDiskDevice::~FileBackedDiskDevice()
{ {
} }
unsigned FileBackedBlockDevice::blockSize() const unsigned FileBackedDiskDevice::blockSize() const
{ {
return m_blockSize; return m_blockSize;
} }
bool FileBackedBlockDevice::readBlock(unsigned index, byte* out) const bool FileBackedDiskDevice::readBlock(unsigned index, byte* out) const
{ {
qword offset = index * m_blockSize; qword offset = index * m_blockSize;
return read(offset, blockSize(), out); return read(offset, blockSize(), out);
} }
bool FileBackedBlockDevice::writeBlock(unsigned index, const byte* data) bool FileBackedDiskDevice::writeBlock(unsigned index, const byte* data)
{ {
qword offset = index * m_blockSize; qword offset = index * m_blockSize;
return write(offset, blockSize(), data); return write(offset, blockSize(), data);
} }
bool FileBackedBlockDevice::read(qword offset, unsigned length, byte* out) const bool FileBackedDiskDevice::read(qword offset, unsigned length, byte* out) const
{ {
#ifndef IGNORE_FILE_LENGTH #ifndef IGNORE_FILE_LENGTH
if (offset + length >= m_fileLength) if (offset + length >= m_fileLength)
return false; return false;
#endif #endif
#ifdef FBBD_DEBUG #ifdef FBBD_DEBUG
printf("[FileBackedBlockDevice] Read device @ offset %llx, length %u\n", offset, length); printf("[FileBackedDiskDevice] Read device @ offset %llx, length %u\n", offset, length);
#endif #endif
fseeko(m_file, offset, SEEK_SET); fseeko(m_file, offset, SEEK_SET);
unsigned nread = fread(out, sizeof(byte), length, m_file); unsigned nread = fread(out, sizeof(byte), length, m_file);
@ -59,14 +59,14 @@ bool FileBackedBlockDevice::read(qword offset, unsigned length, byte* out) const
return true; return true;
} }
bool FileBackedBlockDevice::write(qword offset, unsigned length, const byte* data) bool FileBackedDiskDevice::write(qword offset, unsigned length, const byte* data)
{ {
#ifndef IGNORE_FILE_LENGTH #ifndef IGNORE_FILE_LENGTH
if (offset + length >= m_fileLength) if (offset + length >= m_fileLength)
return false; return false;
#endif #endif
#ifdef FBBD_DEBUG #ifdef FBBD_DEBUG
printf("[FileBackedBlockDevice] Write device @ offset %llx, length %u\n", offset, length); printf("[FileBackedDiskDevice] Write device @ offset %llx, length %u\n", offset, length);
#endif #endif
fseeko(m_file, offset, SEEK_SET); fseeko(m_file, offset, SEEK_SET);
// size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
@ -75,8 +75,8 @@ bool FileBackedBlockDevice::write(qword offset, unsigned length, const byte* dat
return true; return true;
} }
const char* FileBackedBlockDevice::className() const const char* FileBackedDiskDevice::className() const
{ {
return "FileBackedBlockDevice"; return "FileBackedDiskDevice";
} }

View file

@ -1,15 +1,15 @@
#pragma once #pragma once
#include "BlockDevice.h" #include "DiskDevice.h"
#include <AK/RetainPtr.h> #include <AK/RetainPtr.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <stdio.h> #include <stdio.h>
class FileBackedBlockDevice final : public BlockDevice { class FileBackedDiskDevice final : public DiskDevice {
public: public:
static RetainPtr<FileBackedBlockDevice> create(String&& imagePath, unsigned blockSize); static RetainPtr<FileBackedDiskDevice> create(String&& imagePath, unsigned blockSize);
virtual ~FileBackedBlockDevice() override; virtual ~FileBackedDiskDevice() override;
bool isValid() const { return m_file; } bool isValid() const { return m_file; }
@ -22,7 +22,7 @@ public:
private: private:
virtual const char* className() const override; virtual const char* className() const override;
FileBackedBlockDevice(String&& imagePath, unsigned blockSize); FileBackedDiskDevice(String&& imagePath, unsigned blockSize);
String m_imagePath; String m_imagePath;
FILE* m_file { nullptr }; FILE* m_file { nullptr };

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "BlockDevice.h" #include "DiskDevice.h"
#include "InodeIdentifier.h" #include "InodeIdentifier.h"
#include "InodeMetadata.h" #include "InodeMetadata.h"
#include "Limits.h" #include "Limits.h"

View file

@ -9,13 +9,13 @@ AK_OBJS = \
../AK/kmalloc.o ../AK/kmalloc.o
VFS_OBJS = \ VFS_OBJS = \
BlockDevice.o \ DiskDevice.o \
FileBackedBlockDevice.o \ FileBackedDiskDevice.o \
FileSystem.o \ FileSystem.o \
Ext2FileSystem.o \ Ext2FileSystem.o \
VirtualFileSystem.o \ VirtualFileSystem.o \
FileHandle.o \ FileHandle.o \
DeviceBackedFileSystem.o \ DiskBackedFileSystem.o \
SyntheticFileSystem.o \ SyntheticFileSystem.o \
InodeIdentifier.o \ InodeIdentifier.o \
CharacterDevice.o \ CharacterDevice.o \
@ -29,8 +29,6 @@ OBJS = $(AK_OBJS) $(VFS_OBJS)
CXXFLAGS = -std=c++17 -O0 -W -Wall -Wextra -Wconversion -I. -I.. -ggdb3 -Wno-class-memaccess CXXFLAGS = -std=c++17 -O0 -W -Wall -Wextra -Wconversion -I. -I.. -ggdb3 -Wno-class-memaccess
#test.o: BlockDevice.h FileBackedBlockDevice.h FileSystem.h Ext2FileSystem.h VirtualFileSystem.h FileHandle.h
all: $(PROGRAM) all: $(PROGRAM)
.cpp.o: .cpp.o:

View file

@ -1,5 +1,5 @@
#include "Ext2FileSystem.h" #include "Ext2FileSystem.h"
#include "FileBackedBlockDevice.h" #include "FileBackedDiskDevice.h"
#include "VirtualFileSystem.h" #include "VirtualFileSystem.h"
#include "FileHandle.h" #include "FileHandle.h"
#include "SyntheticFileSystem.h" #include "SyntheticFileSystem.h"
@ -217,7 +217,7 @@ int main(int c, char** v)
RetainPtr<FileSystem> makeFileSystem(const char* imagePath) RetainPtr<FileSystem> makeFileSystem(const char* imagePath)
{ {
auto fsImage = FileBackedBlockDevice::create(imagePath, 512); auto fsImage = FileBackedDiskDevice::create(imagePath, 512);
if (!fsImage->isValid()) { if (!fsImage->isValid()) {
fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath); fprintf(stderr, "Failed to open fs image file '%s'\n", imagePath);
exit(1); exit(1);