Skip to content

Commit

Permalink
Kernel: Move DiskDevice::block_size() up to BlockDevice
Browse files Browse the repository at this point in the history
All block devices should have a block size, after all. This defaults to
PAGE_SIZE if no size is specified.
  • Loading branch information
awesomekling committed Aug 21, 2019
1 parent 52366e3 commit 5de483c
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 45 deletions.
6 changes: 5 additions & 1 deletion Kernel/Devices/BlockDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ class BlockDevice : public Device {
public:
virtual ~BlockDevice() override;

size_t block_size() const { return m_block_size; }
virtual bool is_seekable() const override { return true; }

protected:
BlockDevice(unsigned major, unsigned minor)
BlockDevice(unsigned major, unsigned minor, size_t block_size = PAGE_SIZE)
: Device(major, minor)
, m_block_size(block_size)
{
}

private:
virtual bool is_block_device() const final { return true; }

size_t m_block_size { 0 };
};
4 changes: 2 additions & 2 deletions Kernel/Devices/DiskDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <Kernel/Devices/DiskDevice.h>

DiskDevice::DiskDevice(int major, int minor)
: BlockDevice(major, minor)
DiskDevice::DiskDevice(int major, int minor, size_t block_size)
: BlockDevice(major, minor, block_size)
{
}

Expand Down
3 changes: 1 addition & 2 deletions Kernel/Devices/DiskDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class DiskDevice : public BlockDevice {
public:
virtual ~DiskDevice() override;

virtual unsigned block_size() const = 0;
virtual bool read_block(unsigned index, u8*) const = 0;
virtual bool write_block(unsigned index, const u8*) = 0;
bool read(DiskOffset, unsigned length, u8*) const;
Expand All @@ -23,5 +22,5 @@ class DiskDevice : public BlockDevice {
virtual bool is_disk_device() const override { return true; };

protected:
DiskDevice(int major, int minor);
DiskDevice(int major, int minor, size_t block_size = 512);
};
15 changes: 5 additions & 10 deletions Kernel/Devices/DiskPartition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

// #define OFFD_DEBUG

NonnullRefPtr<DiskPartition> DiskPartition::create(NonnullRefPtr<DiskDevice> device, unsigned block_offset)
NonnullRefPtr<DiskPartition> DiskPartition::create(DiskDevice& device, unsigned block_offset)
{
return adopt(*new DiskPartition(move(device), block_offset));
return adopt(*new DiskPartition(device, block_offset));
}

DiskPartition::DiskPartition(NonnullRefPtr<DiskDevice> device, unsigned block_offset)
: DiskDevice(100, 0)
, m_device(move(device))
DiskPartition::DiskPartition(DiskDevice& device, unsigned block_offset)
: DiskDevice(100, 0, device.block_size())
, m_device(device)
, m_block_offset(block_offset)
{
}
Expand All @@ -18,11 +18,6 @@ DiskPartition::~DiskPartition()
{
}

unsigned DiskPartition::block_size() const
{
return m_device->block_size();
}

bool DiskPartition::read_block(unsigned index, u8* out) const
{
#ifdef OFFD_DEBUG
Expand Down
5 changes: 2 additions & 3 deletions Kernel/Devices/DiskPartition.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

class DiskPartition final : public DiskDevice {
public:
static NonnullRefPtr<DiskPartition> create(NonnullRefPtr<DiskDevice>, unsigned block_offset);
static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset);
virtual ~DiskPartition();

virtual unsigned block_size() const override;
virtual bool read_block(unsigned index, u8* out) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
Expand All @@ -23,7 +22,7 @@ class DiskPartition final : public DiskDevice {
private:
virtual const char* class_name() const override;

DiskPartition(NonnullRefPtr<DiskDevice>, unsigned block_offset);
DiskPartition(DiskDevice&, unsigned block_offset);

NonnullRefPtr<DiskDevice> m_device;
unsigned m_block_offset;
Expand Down
15 changes: 5 additions & 10 deletions Kernel/Devices/FileBackedDiskDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ RefPtr<FileBackedDiskDevice> FileBackedDiskDevice::create(String&& image_path, u
return adopt(*new FileBackedDiskDevice(move(image_path), block_size));
}

FileBackedDiskDevice::FileBackedDiskDevice(String&& image_path, unsigned block_size)
: m_image_path(move(image_path))
, m_block_size(block_size)
FileBackedDiskDevice::FileBackedDiskDevice(const String& image_path, size_t block_size)
: DiskDevice(0, 0, block_size)
, m_image_path(image_path)
{
struct stat st;
int result = stat(m_image_path.characters(), &st);
Expand All @@ -27,20 +27,15 @@ FileBackedDiskDevice::~FileBackedDiskDevice()
{
}

unsigned FileBackedDiskDevice::block_size() const
{
return m_block_size;
}

bool FileBackedDiskDevice::read_block(unsigned index, u8* out) const
{
DiskOffset offset = index * m_block_size;
DiskOffset offset = index * block_size();
return read_internal(offset, block_size(), out);
}

bool FileBackedDiskDevice::write_block(unsigned index, const u8* data)
{
DiskOffset offset = index * m_block_size;
DiskOffset offset = index * block_size();
return write_internal(offset, block_size(), data);
}

Expand Down
6 changes: 2 additions & 4 deletions Kernel/Devices/FileBackedDiskDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

class FileBackedDiskDevice final : public DiskDevice {
public:
static RefPtr<FileBackedDiskDevice> create(String&& image_path, unsigned block_size);
static RefPtr<FileBackedDiskDevice> create(const String& image_path, size_t block_size);
virtual ~FileBackedDiskDevice() override;

bool is_valid() const { return m_file; }

virtual unsigned block_size() const override;
virtual bool read_block(unsigned index, u8* out) const override;
virtual bool write_block(unsigned index, const u8*) override;

Expand All @@ -23,10 +22,9 @@ class FileBackedDiskDevice final : public DiskDevice {
bool read_internal(DiskOffset, unsigned length, u8* out) const;
bool write_internal(DiskOffset, unsigned length, const u8* data);

FileBackedDiskDevice(String&& imagePath, unsigned block_size);
FileBackedDiskDevice(const String& image_path, size_t block_size);

String m_image_path;
FILE* m_file { nullptr };
DiskOffset m_file_length { 0 };
unsigned m_block_size { 0 };
};
7 changes: 1 addition & 6 deletions Kernel/Devices/FloppyDiskDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const char* FloppyDiskDevice::class_name() const

FloppyDiskDevice::FloppyDiskDevice(FloppyDiskDevice::DriveType type)
: IRQHandler(IRQ_FLOPPY_DRIVE)
, DiskDevice(89, (type == FloppyDiskDevice::DriveType::Master) ? 0 : 1)
, DiskDevice(89, (type == FloppyDiskDevice::DriveType::Master) ? 0 : 1, BYTES_PER_SECTOR)
, m_io_base_addr((type == FloppyDiskDevice::DriveType::Master) ? 0x3F0 : 0x370)
{
initialize();
Expand All @@ -94,11 +94,6 @@ FloppyDiskDevice::~FloppyDiskDevice()
{
}

unsigned FloppyDiskDevice::block_size() const
{
return BYTES_PER_SECTOR;
}

bool FloppyDiskDevice::read_block(unsigned index, u8* data) const
{
return const_cast<FloppyDiskDevice*>(this)->read_blocks(index, 1, data);
Expand Down
1 change: 0 additions & 1 deletion Kernel/Devices/FloppyDiskDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ class FloppyDiskDevice final : public IRQHandler
virtual ~FloppyDiskDevice() override;

// ^DiskDevice
virtual unsigned block_size() const override;
virtual bool read_block(unsigned index, u8*) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
Expand Down
5 changes: 0 additions & 5 deletions Kernel/Devices/PATADiskDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ const char* PATADiskDevice::class_name() const
return "PATADiskDevice";
}

unsigned PATADiskDevice::block_size() const
{
return 512;
}

bool PATADiskDevice::read_blocks(unsigned index, u16 count, u8* out)
{
if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource())
Expand Down
1 change: 0 additions & 1 deletion Kernel/Devices/PATADiskDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class PATADiskDevice final : public DiskDevice {
virtual ~PATADiskDevice() override;

// ^DiskDevice
virtual unsigned block_size() const override;
virtual bool read_block(unsigned index, u8*) const override;
virtual bool write_block(unsigned index, const u8*) override;
virtual bool read_blocks(unsigned index, u16 count, u8*) override;
Expand Down

0 comments on commit 5de483c

Please sign in to comment.