2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-01-25 12:07:10 -03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 03:19:20 -03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-09-07 08:39:11 -03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-12-25 15:23:35 -03:00
|
|
|
#include <Kernel/Storage/Partition/DiskPartition.h>
|
2019-06-02 06:38:37 -03:00
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2020-12-26 11:53:30 -03:00
|
|
|
NonnullRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
|
2019-06-02 06:38:37 -03:00
|
|
|
{
|
2021-09-11 03:19:20 -03:00
|
|
|
auto partition_or_error = DeviceManagement::try_create_device<DiskPartition>(device, minor_number, metadata);
|
2021-09-10 08:44:46 -03:00
|
|
|
// FIXME: Find a way to propagate errors
|
|
|
|
|
VERIFY(!partition_or_error.is_error());
|
|
|
|
|
return partition_or_error.release_value();
|
2019-06-02 06:38:37 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-26 11:53:30 -03:00
|
|
|
DiskPartition::DiskPartition(BlockDevice& device, unsigned minor_number, DiskPartitionMetadata metadata)
|
|
|
|
|
: BlockDevice(100, minor_number, device.block_size())
|
2019-08-21 11:45:52 -03:00
|
|
|
, m_device(device)
|
2020-12-26 11:53:30 -03:00
|
|
|
, m_metadata(metadata)
|
2019-06-02 06:38:37 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DiskPartition::~DiskPartition()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-31 08:17:03 -03:00
|
|
|
const DiskPartitionMetadata& DiskPartition::metadata() const
|
|
|
|
|
{
|
|
|
|
|
return m_metadata;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-02 15:16:01 -03:00
|
|
|
void DiskPartition::start_request(AsyncBlockDeviceRequest& request)
|
|
|
|
|
{
|
2021-08-27 02:08:43 -03:00
|
|
|
auto device = m_device.strong_ref();
|
|
|
|
|
if (!device)
|
|
|
|
|
request.complete(AsyncBlockDeviceRequest::RequestResult::Failure);
|
|
|
|
|
auto sub_request_or_error = device->try_make_request<AsyncBlockDeviceRequest>(request.request_type(),
|
2021-09-07 11:40:54 -03:00
|
|
|
request.block_index() + m_metadata.start_block(), request.block_count(), request.buffer(), request.buffer_size());
|
|
|
|
|
if (sub_request_or_error.is_error())
|
|
|
|
|
TODO();
|
|
|
|
|
request.add_sub_request(sub_request_or_error.release_value());
|
2020-11-02 15:16:01 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<size_t> DiskPartition::read(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer& outbuf, size_t len)
|
2020-04-09 18:36:39 -03:00
|
|
|
{
|
2022-01-25 15:25:24 -03:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 08:12:00 -03:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::read offset={}, adjust={}, len={}", fd.offset(), adjust, len);
|
2021-08-27 02:08:43 -03:00
|
|
|
return m_device.strong_ref()->read(fd, offset + adjust, outbuf, len);
|
2020-04-10 06:44:42 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-25 15:17:49 -03:00
|
|
|
bool DiskPartition::can_read(const OpenFileDescription& fd, u64 offset) const
|
2020-04-10 06:44:42 -03:00
|
|
|
{
|
2022-01-25 15:25:24 -03:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 08:12:00 -03:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::can_read offset={}, adjust={}", offset, adjust);
|
2021-08-27 02:08:43 -03:00
|
|
|
return m_device.strong_ref()->can_read(fd, offset + adjust);
|
2020-04-09 18:36:39 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<size_t> DiskPartition::write(OpenFileDescription& fd, u64 offset, const UserOrKernelBuffer& inbuf, size_t len)
|
2020-04-09 18:36:39 -03:00
|
|
|
{
|
2022-01-25 15:25:24 -03:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 08:12:00 -03:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::write offset={}, adjust={}, len={}", offset, adjust, len);
|
2021-08-27 02:08:43 -03:00
|
|
|
return m_device.strong_ref()->write(fd, offset + adjust, inbuf, len);
|
2020-04-10 06:44:42 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-25 15:19:35 -03:00
|
|
|
bool DiskPartition::can_write(const OpenFileDescription& fd, u64 offset) const
|
2020-04-10 06:44:42 -03:00
|
|
|
{
|
2022-01-25 15:25:24 -03:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 08:12:00 -03:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::can_write offset={}, adjust={}", offset, adjust);
|
2021-08-27 02:08:43 -03:00
|
|
|
return m_device.strong_ref()->can_write(fd, offset + adjust);
|
2020-04-09 18:36:39 -03:00
|
|
|
}
|
|
|
|
|
|
2021-07-10 20:46:09 -03:00
|
|
|
StringView DiskPartition::class_name() const
|
2019-06-02 06:38:37 -03:00
|
|
|
{
|
2021-10-02 19:24:00 -03:00
|
|
|
return "DiskPartition"sv;
|
2019-06-02 06:38:37 -03:00
|
|
|
}
|
2020-02-15 21:27:42 -03:00
|
|
|
|
|
|
|
|
}
|