2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2022-03-01 20:24:01 -05:00
|
|
|
#include <Kernel/Storage/DiskPartition.h>
|
2019-06-02 19:38:37 +10:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
NonnullLockRefPtr<DiskPartition> DiskPartition::create(BlockDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
|
2019-06-02 19:38:37 +10:00
|
|
|
{
|
2021-09-11 09:19:20 +03:00
|
|
|
auto partition_or_error = DeviceManagement::try_create_device<DiskPartition>(device, minor_number, metadata);
|
2021-09-10 14: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 19:38:37 +10:00
|
|
|
}
|
|
|
|
|
2022-07-23 11:15:35 +03:00
|
|
|
DiskPartition::DiskPartition(BlockDevice& device, MinorNumber minor_number, Partition::DiskPartitionMetadata metadata)
|
2020-12-26 16:53:30 +02:00
|
|
|
: BlockDevice(100, minor_number, device.block_size())
|
2019-08-21 16:45:52 +02:00
|
|
|
, m_device(device)
|
2020-12-26 16:53:30 +02:00
|
|
|
, m_metadata(metadata)
|
2019-06-02 19:38:37 +10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:15:15 -06:00
|
|
|
DiskPartition::~DiskPartition() = default;
|
2019-06-02 19:38:37 +10:00
|
|
|
|
2022-02-12 14:21:28 -05:00
|
|
|
Partition::DiskPartitionMetadata const& DiskPartition::metadata() const
|
2020-12-31 13:17:03 +02:00
|
|
|
{
|
|
|
|
return m_metadata;
|
|
|
|
}
|
|
|
|
|
2020-11-02 11:16:01 -07:00
|
|
|
void DiskPartition::start_request(AsyncBlockDeviceRequest& request)
|
|
|
|
{
|
2021-08-27 08: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 16:40:54 +02: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 11:16:01 -07:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> DiskPartition::read(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer& outbuf, size_t len)
|
2020-04-10 00:36:39 +03:00
|
|
|
{
|
2022-01-25 20:25:24 +02:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 12:12:00 +01:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::read offset={}, adjust={}, len={}", fd.offset(), adjust, len);
|
2021-08-27 08:08:43 +03:00
|
|
|
return m_device.strong_ref()->read(fd, offset + adjust, outbuf, len);
|
2020-04-10 19:44:42 +10:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool DiskPartition::can_read(OpenFileDescription const& fd, u64 offset) const
|
2020-04-10 19:44:42 +10:00
|
|
|
{
|
2022-01-25 20:25:24 +02:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 12:12:00 +01:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::can_read offset={}, adjust={}", offset, adjust);
|
2021-08-27 08:08:43 +03:00
|
|
|
return m_device.strong_ref()->can_read(fd, offset + adjust);
|
2020-04-10 00:36:39 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> DiskPartition::write(OpenFileDescription& fd, u64 offset, UserOrKernelBuffer const& inbuf, size_t len)
|
2020-04-10 00:36:39 +03:00
|
|
|
{
|
2022-01-25 20:25:24 +02:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 12:12:00 +01:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::write offset={}, adjust={}, len={}", offset, adjust, len);
|
2021-08-27 08:08:43 +03:00
|
|
|
return m_device.strong_ref()->write(fd, offset + adjust, inbuf, len);
|
2020-04-10 19:44:42 +10:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool DiskPartition::can_write(OpenFileDescription const& fd, u64 offset) const
|
2020-04-10 19:44:42 +10:00
|
|
|
{
|
2022-01-25 20:25:24 +02:00
|
|
|
u64 adjust = m_metadata.start_block() * block_size();
|
2021-03-12 12:12:00 +01:00
|
|
|
dbgln_if(OFFD_DEBUG, "DiskPartition::can_write offset={}, adjust={}", offset, adjust);
|
2021-08-27 08:08:43 +03:00
|
|
|
return m_device.strong_ref()->can_write(fd, offset + adjust);
|
2020-04-10 00:36:39 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 01:46:09 +02:00
|
|
|
StringView DiskPartition::class_name() const
|
2019-06-02 19:38:37 +10:00
|
|
|
{
|
2021-10-02 15:24:00 -07:00
|
|
|
return "DiskPartition"sv;
|
2019-06-02 19:38:37 +10:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|