mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:22:46 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									52366e3f02
								
							
						
					
					
						commit
						5de483cfbb
					
				
					 11 changed files with 23 additions and 45 deletions
				
			
		|  | @ -6,14 +6,18 @@ class BlockDevice : public Device { | ||||||
| public: | public: | ||||||
|     virtual ~BlockDevice() override; |     virtual ~BlockDevice() override; | ||||||
| 
 | 
 | ||||||
|  |     size_t block_size() const { return m_block_size; } | ||||||
|     virtual bool is_seekable() const override { return true; } |     virtual bool is_seekable() const override { return true; } | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|     BlockDevice(unsigned major, unsigned minor) |     BlockDevice(unsigned major, unsigned minor, size_t block_size = PAGE_SIZE) | ||||||
|         : Device(major, minor) |         : Device(major, minor) | ||||||
|  |         , m_block_size(block_size) | ||||||
|     { |     { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|     virtual bool is_block_device() const final { return true; } |     virtual bool is_block_device() const final { return true; } | ||||||
|  | 
 | ||||||
|  |     size_t m_block_size { 0 }; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| #include <Kernel/Devices/DiskDevice.h> | #include <Kernel/Devices/DiskDevice.h> | ||||||
| 
 | 
 | ||||||
| DiskDevice::DiskDevice(int major, int minor) | DiskDevice::DiskDevice(int major, int minor, size_t block_size) | ||||||
|     : BlockDevice(major, minor) |     : BlockDevice(major, minor, block_size) | ||||||
| { | { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -11,7 +11,6 @@ class DiskDevice : public BlockDevice { | ||||||
| public: | public: | ||||||
|     virtual ~DiskDevice() override; |     virtual ~DiskDevice() override; | ||||||
| 
 | 
 | ||||||
|     virtual unsigned block_size() const = 0; |  | ||||||
|     virtual bool read_block(unsigned index, u8*) const = 0; |     virtual bool read_block(unsigned index, u8*) const = 0; | ||||||
|     virtual bool write_block(unsigned index, const u8*) = 0; |     virtual bool write_block(unsigned index, const u8*) = 0; | ||||||
|     bool read(DiskOffset, unsigned length, u8*) const; |     bool read(DiskOffset, unsigned length, u8*) const; | ||||||
|  | @ -23,5 +22,5 @@ public: | ||||||
|     virtual bool is_disk_device() const override { return true; }; |     virtual bool is_disk_device() const override { return true; }; | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
|     DiskDevice(int major, int minor); |     DiskDevice(int major, int minor, size_t block_size = 512); | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -2,14 +2,14 @@ | ||||||
| 
 | 
 | ||||||
| // #define OFFD_DEBUG
 | // #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) | DiskPartition::DiskPartition(DiskDevice& device, unsigned block_offset) | ||||||
|     : DiskDevice(100, 0) |     : DiskDevice(100, 0, device.block_size()) | ||||||
|     , m_device(move(device)) |     , m_device(device) | ||||||
|     , m_block_offset(block_offset) |     , 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 | bool DiskPartition::read_block(unsigned index, u8* out) const | ||||||
| { | { | ||||||
| #ifdef OFFD_DEBUG | #ifdef OFFD_DEBUG | ||||||
|  |  | ||||||
|  | @ -5,10 +5,9 @@ | ||||||
| 
 | 
 | ||||||
| class DiskPartition final : public DiskDevice { | class DiskPartition final : public DiskDevice { | ||||||
| public: | public: | ||||||
|     static NonnullRefPtr<DiskPartition> create(NonnullRefPtr<DiskDevice>, unsigned block_offset); |     static NonnullRefPtr<DiskPartition> create(DiskDevice&, unsigned block_offset); | ||||||
|     virtual ~DiskPartition(); |     virtual ~DiskPartition(); | ||||||
| 
 | 
 | ||||||
|     virtual unsigned block_size() const override; |  | ||||||
|     virtual bool read_block(unsigned index, u8* out) const override; |     virtual bool read_block(unsigned index, u8* out) const override; | ||||||
|     virtual bool write_block(unsigned index, const u8*) override; |     virtual bool write_block(unsigned index, const u8*) override; | ||||||
|     virtual bool read_blocks(unsigned index, u16 count, u8*) override; |     virtual bool read_blocks(unsigned index, u16 count, u8*) override; | ||||||
|  | @ -23,7 +22,7 @@ public: | ||||||
| private: | private: | ||||||
|     virtual const char* class_name() const override; |     virtual const char* class_name() const override; | ||||||
| 
 | 
 | ||||||
|     DiskPartition(NonnullRefPtr<DiskDevice>, unsigned block_offset); |     DiskPartition(DiskDevice&, unsigned block_offset); | ||||||
| 
 | 
 | ||||||
|     NonnullRefPtr<DiskDevice> m_device; |     NonnullRefPtr<DiskDevice> m_device; | ||||||
|     unsigned m_block_offset; |     unsigned m_block_offset; | ||||||
|  |  | ||||||
|  | @ -12,9 +12,9 @@ RefPtr<FileBackedDiskDevice> FileBackedDiskDevice::create(String&& image_path, u | ||||||
|     return adopt(*new FileBackedDiskDevice(move(image_path), block_size)); |     return adopt(*new FileBackedDiskDevice(move(image_path), block_size)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| FileBackedDiskDevice::FileBackedDiskDevice(String&& image_path, unsigned block_size) | FileBackedDiskDevice::FileBackedDiskDevice(const String& image_path, size_t block_size) | ||||||
|     : m_image_path(move(image_path)) |     : DiskDevice(0, 0, block_size) | ||||||
|     , m_block_size(block_size) |     , m_image_path(image_path) | ||||||
| { | { | ||||||
|     struct stat st; |     struct stat st; | ||||||
|     int result = stat(m_image_path.characters(), &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 | 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); |     return read_internal(offset, block_size(), out); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool FileBackedDiskDevice::write_block(unsigned index, const u8* data) | 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); |     return write_internal(offset, block_size(), data); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -8,12 +8,11 @@ | ||||||
| 
 | 
 | ||||||
| class FileBackedDiskDevice final : public DiskDevice { | class FileBackedDiskDevice final : public DiskDevice { | ||||||
| public: | 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; |     virtual ~FileBackedDiskDevice() override; | ||||||
| 
 | 
 | ||||||
|     bool is_valid() const { return m_file; } |     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 read_block(unsigned index, u8* out) const override; | ||||||
|     virtual bool write_block(unsigned index, const u8*) 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 read_internal(DiskOffset, unsigned length, u8* out) const; | ||||||
|     bool write_internal(DiskOffset, unsigned length, const u8* data); |     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; |     String m_image_path; | ||||||
|     FILE* m_file { nullptr }; |     FILE* m_file { nullptr }; | ||||||
|     DiskOffset m_file_length { 0 }; |     DiskOffset m_file_length { 0 }; | ||||||
|     unsigned m_block_size { 0 }; |  | ||||||
| }; | }; | ||||||
|  |  | ||||||
|  | @ -84,7 +84,7 @@ const char* FloppyDiskDevice::class_name() const | ||||||
| 
 | 
 | ||||||
| FloppyDiskDevice::FloppyDiskDevice(FloppyDiskDevice::DriveType type) | FloppyDiskDevice::FloppyDiskDevice(FloppyDiskDevice::DriveType type) | ||||||
|     : IRQHandler(IRQ_FLOPPY_DRIVE) |     : 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) |     , m_io_base_addr((type == FloppyDiskDevice::DriveType::Master) ? 0x3F0 : 0x370) | ||||||
| { | { | ||||||
|     initialize(); |     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 | bool FloppyDiskDevice::read_block(unsigned index, u8* data) const | ||||||
| { | { | ||||||
|     return const_cast<FloppyDiskDevice*>(this)->read_blocks(index, 1, data); |     return const_cast<FloppyDiskDevice*>(this)->read_blocks(index, 1, data); | ||||||
|  |  | ||||||
|  | @ -136,7 +136,6 @@ public: | ||||||
|     virtual ~FloppyDiskDevice() override; |     virtual ~FloppyDiskDevice() override; | ||||||
| 
 | 
 | ||||||
|     // ^DiskDevice
 |     // ^DiskDevice
 | ||||||
|     virtual unsigned block_size() const override; |  | ||||||
|     virtual bool read_block(unsigned index, u8*) const override; |     virtual bool read_block(unsigned index, u8*) const override; | ||||||
|     virtual bool write_block(unsigned index, const u8*) override; |     virtual bool write_block(unsigned index, const u8*) override; | ||||||
|     virtual bool read_blocks(unsigned index, u16 count, u8*) override; |     virtual bool read_blocks(unsigned index, u16 count, u8*) override; | ||||||
|  |  | ||||||
|  | @ -99,11 +99,6 @@ const char* PATADiskDevice::class_name() const | ||||||
|     return "PATADiskDevice"; |     return "PATADiskDevice"; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| unsigned PATADiskDevice::block_size() const |  | ||||||
| { |  | ||||||
|     return 512; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| bool PATADiskDevice::read_blocks(unsigned index, u16 count, u8* out) | bool PATADiskDevice::read_blocks(unsigned index, u16 count, u8* out) | ||||||
| { | { | ||||||
|     if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource()) |     if (m_channel.m_bus_master_base && m_channel.m_dma_enabled.resource()) | ||||||
|  |  | ||||||
|  | @ -30,7 +30,6 @@ public: | ||||||
|     virtual ~PATADiskDevice() override; |     virtual ~PATADiskDevice() override; | ||||||
| 
 | 
 | ||||||
|     // ^DiskDevice
 |     // ^DiskDevice
 | ||||||
|     virtual unsigned block_size() const override; |  | ||||||
|     virtual bool read_block(unsigned index, u8*) const override; |     virtual bool read_block(unsigned index, u8*) const override; | ||||||
|     virtual bool write_block(unsigned index, const u8*) override; |     virtual bool write_block(unsigned index, const u8*) override; | ||||||
|     virtual bool read_blocks(unsigned index, u16 count, u8*) override; |     virtual bool read_blocks(unsigned index, u16 count, u8*) override; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling