2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-02-16 02:52:58 +03:00
|
|
|
#pragma once
|
|
|
|
|
2022-01-29 18:51:02 +03:00
|
|
|
#include <AK/IntegralMath.h>
|
2019-04-03 13:36:40 +03:00
|
|
|
#include <Kernel/Devices/Device.h>
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <Kernel/Library/LockWeakable.h>
|
2019-02-16 02:52:58 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-03-26 20:32:44 +03:00
|
|
|
class AsyncBlockDeviceRequest;
|
|
|
|
|
|
|
|
class BlockDevice : public Device {
|
|
|
|
public:
|
|
|
|
virtual ~BlockDevice() override;
|
|
|
|
|
|
|
|
size_t block_size() const { return m_block_size; }
|
|
|
|
u8 block_size_log() const { return m_block_size_log; }
|
|
|
|
virtual bool is_seekable() const override { return true; }
|
|
|
|
|
|
|
|
bool read_block(u64 index, UserOrKernelBuffer&);
|
2022-04-01 20:58:27 +03:00
|
|
|
bool write_block(u64 index, UserOrKernelBuffer const&);
|
2022-03-26 20:32:44 +03:00
|
|
|
|
|
|
|
virtual void start_request(AsyncBlockDeviceRequest&) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
BlockDevice(MajorNumber major, MinorNumber minor, size_t block_size = PAGE_SIZE)
|
|
|
|
: Device(major, minor)
|
|
|
|
, m_block_size(block_size)
|
|
|
|
{
|
|
|
|
// 512 is the minimum sector size in most block devices
|
|
|
|
VERIFY(m_block_size >= 512);
|
|
|
|
VERIFY(is_power_of_two(m_block_size));
|
|
|
|
m_block_size_log = AK::log2(m_block_size);
|
|
|
|
}
|
|
|
|
|
2022-04-23 11:09:41 +03:00
|
|
|
protected:
|
2022-03-26 20:32:44 +03:00
|
|
|
virtual bool is_block_device() const final { return true; }
|
|
|
|
|
2022-07-16 10:39:57 +03:00
|
|
|
virtual void after_inserting_add_symlink_to_device_identifier_directory() override final;
|
|
|
|
virtual void before_will_be_destroyed_remove_symlink_from_device_identifier_directory() override final;
|
|
|
|
|
2022-04-23 11:09:41 +03:00
|
|
|
private:
|
2022-07-16 10:39:57 +03:00
|
|
|
// FIXME: These methods will be eventually removed after all nodes in /sys/dev/block/ are symlinks
|
|
|
|
virtual void after_inserting_add_to_device_identifier_directory() override final;
|
|
|
|
virtual void before_will_be_destroyed_remove_from_device_identifier_directory() override final;
|
|
|
|
|
2022-03-26 20:32:44 +03:00
|
|
|
size_t m_block_size { 0 };
|
|
|
|
u8 m_block_size_log { 0 };
|
|
|
|
};
|
2020-11-02 21:16:01 +03:00
|
|
|
|
2021-05-03 11:06:15 +03:00
|
|
|
class AsyncBlockDeviceRequest final : public AsyncDeviceRequest {
|
2020-11-02 21:16:01 +03:00
|
|
|
public:
|
|
|
|
enum RequestType {
|
|
|
|
Read,
|
|
|
|
Write
|
|
|
|
};
|
|
|
|
AsyncBlockDeviceRequest(Device& block_device, RequestType request_type,
|
2022-04-01 20:58:27 +03:00
|
|
|
u64 block_index, u32 block_count, UserOrKernelBuffer const& buffer, size_t buffer_size);
|
2020-11-02 21:16:01 +03:00
|
|
|
|
|
|
|
RequestType request_type() const { return m_request_type; }
|
2021-03-16 22:31:45 +03:00
|
|
|
u64 block_index() const { return m_block_index; }
|
2020-11-02 21:16:01 +03:00
|
|
|
u32 block_count() const { return m_block_count; }
|
2022-03-26 20:32:44 +03:00
|
|
|
size_t block_size() const { return m_block_device.block_size(); }
|
2020-11-02 21:16:01 +03:00
|
|
|
UserOrKernelBuffer& buffer() { return m_buffer; }
|
2022-04-01 20:58:27 +03:00
|
|
|
UserOrKernelBuffer const& buffer() const { return m_buffer; }
|
2020-11-02 21:16:01 +03:00
|
|
|
size_t buffer_size() const { return m_buffer_size; }
|
|
|
|
|
|
|
|
virtual void start() override;
|
2021-08-05 21:54:37 +03:00
|
|
|
virtual StringView name() const override
|
2020-11-02 21:16:01 +03:00
|
|
|
{
|
|
|
|
switch (m_request_type) {
|
|
|
|
case Read:
|
2021-08-05 21:54:37 +03:00
|
|
|
return "BlockDeviceRequest (read)"sv;
|
2020-11-02 21:16:01 +03:00
|
|
|
case Write:
|
2021-08-05 21:54:37 +03:00
|
|
|
return "BlockDeviceRequest (write)"sv;
|
2020-11-02 21:16:01 +03:00
|
|
|
default:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-02 21:16:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BlockDevice& m_block_device;
|
|
|
|
const RequestType m_request_type;
|
2021-03-16 22:31:45 +03:00
|
|
|
const u64 m_block_index;
|
2020-11-02 21:16:01 +03:00
|
|
|
const u32 m_block_count;
|
|
|
|
UserOrKernelBuffer m_buffer;
|
|
|
|
const size_t m_buffer_size;
|
|
|
|
};
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|