Browse Source

Kernel: Move DiskDevice::block_size() up to BlockDevice

All block devices should have a block size, after all. This defaults to
PAGE_SIZE if no size is specified.
Andreas Kling 6 years ago
parent
commit
5de483cfbb

+ 5 - 1
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 };
 };

+ 2 - 2
Kernel/Devices/DiskDevice.cpp

@@ -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)
 {
 }
 

+ 1 - 2
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);
 };

+ 5 - 10
Kernel/Devices/DiskPartition.cpp

@@ -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)
 {
 }
@@ -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

+ 2 - 3
Kernel/Devices/DiskPartition.h

@@ -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;
@@ -23,7 +22,7 @@ public:
 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;

+ 5 - 10
Kernel/Devices/FileBackedDiskDevice.cpp

@@ -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);
@@ -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);
 }
 

+ 2 - 4
Kernel/Devices/FileBackedDiskDevice.h

@@ -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;
 
@@ -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 };
 };

+ 1 - 6
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<FloppyDiskDevice*>(this)->read_blocks(index, 1, data);

+ 0 - 1
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;

+ 0 - 5
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())

+ 0 - 1
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;