Kernel: Require StorageDevice reference for StorageDevicePartition

Allowing creation of StorageDevicePartition objects for any arbitrary
BlockDevice objects means that we could technically create a
StorageDevicePartition for another StorageDevicePartition which is
obviously not the intention for this code. Instead, require to pass a
StorageDevice reference to ensure this cannot happen.
This commit is contained in:
Liav A 2023-06-30 11:27:29 +03:00 committed by Andrew Kaster
parent f08f339e9a
commit c216e7439f
Notes: sideshowbarker 2024-07-17 01:51:00 +09:00
2 changed files with 7 additions and 5 deletions

View file

@ -6,12 +6,13 @@
#include <Kernel/Debug.h>
#include <Kernel/Devices/DeviceManagement.h>
#include <Kernel/Devices/Storage/StorageDevice.h>
#include <Kernel/Devices/Storage/StorageDevicePartition.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
namespace Kernel {
NonnullLockRefPtr<StorageDevicePartition> StorageDevicePartition::create(BlockDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
NonnullLockRefPtr<StorageDevicePartition> StorageDevicePartition::create(StorageDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
{
auto partition_or_error = DeviceManagement::try_create_device<StorageDevicePartition>(device, minor_number, metadata);
// FIXME: Find a way to propagate errors
@ -19,7 +20,7 @@ NonnullLockRefPtr<StorageDevicePartition> StorageDevicePartition::create(BlockDe
return partition_or_error.release_value();
}
StorageDevicePartition::StorageDevicePartition(BlockDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
StorageDevicePartition::StorageDevicePartition(StorageDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
: BlockDevice(100, minor_number, device.block_size())
, m_device(device)
, m_metadata(metadata)

View file

@ -13,11 +13,12 @@
namespace Kernel {
class StorageDevice;
class StorageDevicePartition final : public BlockDevice {
friend class DeviceManagement;
public:
static NonnullLockRefPtr<StorageDevicePartition> create(BlockDevice&, MinorNumber, Partition::DiskPartitionMetadata);
static NonnullLockRefPtr<StorageDevicePartition> create(StorageDevice&, MinorNumber, Partition::DiskPartitionMetadata);
virtual ~StorageDevicePartition();
virtual void start_request(AsyncBlockDeviceRequest&) override;
@ -35,10 +36,10 @@ public:
Partition::DiskPartitionMetadata const& metadata() const;
private:
StorageDevicePartition(BlockDevice&, MinorNumber, Partition::DiskPartitionMetadata);
StorageDevicePartition(StorageDevice&, MinorNumber, Partition::DiskPartitionMetadata);
virtual StringView class_name() const override;
LockWeakPtr<BlockDevice> m_device;
LockWeakPtr<StorageDevice> m_device;
Partition::DiskPartitionMetadata m_metadata;
};