1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 01:35:06 +00:00
serenity/Kernel/Storage/StorageDevice.h
Liav A fb7b4caa57 Kernel/Storage: Implement basic AHCI hotplug support
This is really a basic support for AHCI hotplug events, so we know how
to add a node representing the device in /sys/dev/block and removing it
according to the event type (insertion/removal).

This change doesn't take into account what happens if the device was
mounted or a read/write operation is being handled.

For this to work correctly, StorageManagement now uses the Singleton
container, as it might be accessed simultaneously from many CPUs
for hotplug events. DiskPartition holds a WeakPtr instead of a RefPtr,
to allow removal of a StorageDevice object from the heap.
StorageDevices are now stored and being referenced to via an
IntrusiveList to make it easier to remove them on hotplug event.

In future changes, all of the stated above might change, but for now,
this commit represents the least amount of changes to make everything
to work correctly.
2021-09-08 00:42:20 +02:00

52 lines
1.7 KiB
C++

/*
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/IntrusiveList.h>
#include <Kernel/Devices/BlockDevice.h>
#include <Kernel/Interrupts/IRQHandler.h>
#include <Kernel/Locking/Mutex.h>
#include <Kernel/Storage/Partition/DiskPartition.h>
#include <Kernel/Storage/StorageController.h>
namespace Kernel {
class StorageDevice : public BlockDevice {
friend class StorageManagement;
public:
virtual u64 max_addressable_block() const { return m_max_addressable_block; }
NonnullRefPtr<StorageController> controller() const;
// ^BlockDevice
virtual KResultOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override;
virtual bool can_read(const OpenFileDescription&, size_t) const override;
virtual KResultOr<size_t> write(OpenFileDescription&, u64, const UserOrKernelBuffer&, size_t) override;
virtual bool can_write(const OpenFileDescription&, size_t) const override;
// FIXME: This is being used only during early boot, find a better way to find devices...
virtual String storage_name() const = 0;
virtual void prepare_for_unplug() { m_partitions.clear(); }
NonnullRefPtrVector<DiskPartition> partitions() const { return m_partitions; }
protected:
StorageDevice(const StorageController&, size_t, u64);
StorageDevice(const StorageController&, int, int, size_t, u64);
// ^DiskDevice
virtual StringView class_name() const override;
private:
mutable IntrusiveListNode<StorageDevice, RefPtr<StorageDevice>> m_list_node;
NonnullRefPtr<StorageController> m_storage_controller;
NonnullRefPtrVector<DiskPartition> m_partitions;
u64 m_max_addressable_block;
};
}