diff --git a/Kernel/Devices/BlockDevice.h b/Kernel/Devices/BlockDevice.h index ebfaf1736c5..a568a5ffc04 100644 --- a/Kernel/Devices/BlockDevice.h +++ b/Kernel/Devices/BlockDevice.h @@ -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 }; }; diff --git a/Kernel/Devices/DiskDevice.cpp b/Kernel/Devices/DiskDevice.cpp index 9fc78514973..f63250e154f 100644 --- a/Kernel/Devices/DiskDevice.cpp +++ b/Kernel/Devices/DiskDevice.cpp @@ -1,7 +1,7 @@ #include -DiskDevice::DiskDevice(int major, int minor) - : BlockDevice(major, minor) +DiskDevice::DiskDevice(int major, int minor, size_t block_size) + : BlockDevice(major, minor, block_size) { } diff --git a/Kernel/Devices/DiskDevice.h b/Kernel/Devices/DiskDevice.h index 83f38fe9a36..0fcb90f7b4a 100644 --- a/Kernel/Devices/DiskDevice.h +++ b/Kernel/Devices/DiskDevice.h @@ -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; @@ -23,5 +22,5 @@ public: 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); }; diff --git a/Kernel/Devices/DiskPartition.cpp b/Kernel/Devices/DiskPartition.cpp index 562ca39e6ec..e49521a3db4 100644 --- a/Kernel/Devices/DiskPartition.cpp +++ b/Kernel/Devices/DiskPartition.cpp @@ -2,14 +2,14 @@ // #define OFFD_DEBUG -NonnullRefPtr DiskPartition::create(NonnullRefPtr device, unsigned block_offset) +NonnullRefPtr 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 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) { } @@ -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 diff --git a/Kernel/Devices/DiskPartition.h b/Kernel/Devices/DiskPartition.h index dfdeab4c747..9b73a1f4557 100644 --- a/Kernel/Devices/DiskPartition.h +++ b/Kernel/Devices/DiskPartition.h @@ -5,10 +5,9 @@ class DiskPartition final : public DiskDevice { public: - static NonnullRefPtr create(NonnullRefPtr, unsigned block_offset); + static NonnullRefPtr 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; @@ -23,7 +22,7 @@ public: private: virtual const char* class_name() const override; - DiskPartition(NonnullRefPtr, unsigned block_offset); + DiskPartition(DiskDevice&, unsigned block_offset); NonnullRefPtr m_device; unsigned m_block_offset; diff --git a/Kernel/Devices/FileBackedDiskDevice.cpp b/Kernel/Devices/FileBackedDiskDevice.cpp index 4c10f2e1355..55b0b9f6f15 100644 --- a/Kernel/Devices/FileBackedDiskDevice.cpp +++ b/Kernel/Devices/FileBackedDiskDevice.cpp @@ -12,9 +12,9 @@ RefPtr 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); @@ -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); } diff --git a/Kernel/Devices/FileBackedDiskDevice.h b/Kernel/Devices/FileBackedDiskDevice.h index a630bdfdd2c..c0ebf12e0c7 100644 --- a/Kernel/Devices/FileBackedDiskDevice.h +++ b/Kernel/Devices/FileBackedDiskDevice.h @@ -8,12 +8,11 @@ class FileBackedDiskDevice final : public DiskDevice { public: - static RefPtr create(String&& image_path, unsigned block_size); + static RefPtr 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; @@ -23,10 +22,9 @@ private: 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 }; }; diff --git a/Kernel/Devices/FloppyDiskDevice.cpp b/Kernel/Devices/FloppyDiskDevice.cpp index 632a8b5d5fc..eb1165d41d9 100644 --- a/Kernel/Devices/FloppyDiskDevice.cpp +++ b/Kernel/Devices/FloppyDiskDevice.cpp @@ -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(); @@ -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(this)->read_blocks(index, 1, data); diff --git a/Kernel/Devices/FloppyDiskDevice.h b/Kernel/Devices/FloppyDiskDevice.h index 441bf3a5bee..5623318eb56 100644 --- a/Kernel/Devices/FloppyDiskDevice.h +++ b/Kernel/Devices/FloppyDiskDevice.h @@ -136,7 +136,6 @@ public: 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; diff --git a/Kernel/Devices/PATADiskDevice.cpp b/Kernel/Devices/PATADiskDevice.cpp index 627b952180c..778bbcaf12d 100644 --- a/Kernel/Devices/PATADiskDevice.cpp +++ b/Kernel/Devices/PATADiskDevice.cpp @@ -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()) diff --git a/Kernel/Devices/PATADiskDevice.h b/Kernel/Devices/PATADiskDevice.h index cd23f705e15..8b191ad87b6 100644 --- a/Kernel/Devices/PATADiskDevice.h +++ b/Kernel/Devices/PATADiskDevice.h @@ -30,7 +30,6 @@ public: 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;