2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-12-19 12:50:57 +02:00
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-08 12:33:14 +01:00
|
|
|
#include <AK/Memory.h>
|
2020-03-23 13:45:10 +01:00
|
|
|
#include <AK/StringView.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2020-12-19 12:50:57 +02:00
|
|
|
#include <Kernel/Storage/StorageDevice.h>
|
2021-02-25 19:36:49 +02:00
|
|
|
#include <Kernel/Storage/StorageManagement.h>
|
2021-10-08 22:20:26 +02:00
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
2019-07-28 23:44:01 +10:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-27 17:13:03 +03:00
|
|
|
StorageDevice::StorageDevice(int major, int minor, size_t sector_size, u64 max_addressable_block, NonnullOwnPtr<KString> device_name)
|
2020-12-19 12:50:57 +02:00
|
|
|
: BlockDevice(major, minor, sector_size)
|
2021-08-27 17:13:03 +03:00
|
|
|
, m_early_storage_device_name(move(device_name))
|
2020-12-19 12:50:57 +02:00
|
|
|
, m_max_addressable_block(max_addressable_block)
|
2019-07-28 23:44:01 +10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-11 01:46:09 +02:00
|
|
|
StringView StorageDevice::class_name() const
|
2019-07-28 23:44:01 +10:00
|
|
|
{
|
2021-10-02 15:24:00 -07:00
|
|
|
return "StorageDevice"sv;
|
2019-07-28 23:44:01 +10:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> StorageDevice::read(OpenFileDescription&, u64 offset, UserOrKernelBuffer& outbuf, size_t len)
|
2020-02-09 21:43:22 +11:00
|
|
|
{
|
2020-04-10 19:44:42 +10:00
|
|
|
unsigned index = offset / block_size();
|
2020-02-09 21:43:22 +11:00
|
|
|
u16 whole_blocks = len / block_size();
|
2021-06-16 16:44:15 +02:00
|
|
|
size_t remaining = len % block_size();
|
2020-02-09 21:43:22 +11:00
|
|
|
|
|
|
|
unsigned blocks_per_page = PAGE_SIZE / block_size();
|
|
|
|
|
|
|
|
// PATAChannel will chuck a wobbly if we try to read more than PAGE_SIZE
|
|
|
|
// at a time, because it uses a single page for its DMA buffer.
|
|
|
|
if (whole_blocks >= blocks_per_page) {
|
|
|
|
whole_blocks = blocks_per_page;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:02:17 +01:00
|
|
|
dbgln_if(STORAGE_DEVICE_DEBUG, "StorageDevice::read() index={}, whole_blocks={}, remaining={}", index, whole_blocks, remaining);
|
2020-02-09 21:43:22 +11:00
|
|
|
|
|
|
|
if (whole_blocks > 0) {
|
2021-09-07 16:40:54 +02:00
|
|
|
auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index, whole_blocks, outbuf, whole_blocks * block_size()));
|
2020-11-02 11:16:01 -07:00
|
|
|
auto result = read_request->wait();
|
|
|
|
if (result.wait_result().was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2020-11-02 11:16:01 -07:00
|
|
|
switch (result.request_result()) {
|
|
|
|
case AsyncDeviceRequest::Failure:
|
|
|
|
case AsyncDeviceRequest::Cancelled:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::MemoryFault:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-11-02 11:16:01 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-09 21:43:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t pos = whole_blocks * block_size();
|
|
|
|
|
|
|
|
if (remaining > 0) {
|
2021-09-06 03:29:52 +04:30
|
|
|
auto data_result = ByteBuffer::create_uninitialized(block_size());
|
|
|
|
if (!data_result.has_value())
|
|
|
|
return ENOMEM;
|
|
|
|
auto data = data_result.release_value();
|
2020-09-11 21:11:07 -06:00
|
|
|
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
|
2021-09-07 16:40:54 +02:00
|
|
|
auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()));
|
2020-11-02 11:16:01 -07:00
|
|
|
auto result = read_request->wait();
|
|
|
|
if (result.wait_result().was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2020-11-02 11:16:01 -07:00
|
|
|
switch (result.request_result()) {
|
|
|
|
case AsyncDeviceRequest::Failure:
|
2020-02-09 21:43:22 +11:00
|
|
|
return pos;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::Cancelled:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::MemoryFault:
|
|
|
|
// This should never happen, we're writing to a kernel buffer!
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-02 11:16:01 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-09-07 12:09:52 +02:00
|
|
|
TRY(outbuf.write(data.data(), pos, remaining));
|
2020-02-09 21:43:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos + remaining;
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
bool StorageDevice::can_read(const OpenFileDescription&, size_t offset) const
|
2020-02-09 21:43:22 +11:00
|
|
|
{
|
2020-12-19 12:50:57 +02:00
|
|
|
return offset < (max_addressable_block() * block_size());
|
2020-02-09 21:43:22 +11:00
|
|
|
}
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<size_t> StorageDevice::write(OpenFileDescription&, u64 offset, const UserOrKernelBuffer& inbuf, size_t len)
|
2020-02-09 21:43:22 +11:00
|
|
|
{
|
2020-04-10 19:44:42 +10:00
|
|
|
unsigned index = offset / block_size();
|
2020-02-09 21:43:22 +11:00
|
|
|
u16 whole_blocks = len / block_size();
|
2021-06-16 16:44:15 +02:00
|
|
|
size_t remaining = len % block_size();
|
2020-02-09 21:43:22 +11:00
|
|
|
|
|
|
|
unsigned blocks_per_page = PAGE_SIZE / block_size();
|
|
|
|
|
|
|
|
// PATAChannel will chuck a wobbly if we try to write more than PAGE_SIZE
|
|
|
|
// at a time, because it uses a single page for its DMA buffer.
|
|
|
|
if (whole_blocks >= blocks_per_page) {
|
|
|
|
whole_blocks = blocks_per_page;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-12 14:02:17 +01:00
|
|
|
dbgln_if(STORAGE_DEVICE_DEBUG, "StorageDevice::write() index={}, whole_blocks={}, remaining={}", index, whole_blocks, remaining);
|
2020-02-09 21:43:22 +11:00
|
|
|
|
|
|
|
if (whole_blocks > 0) {
|
2021-09-07 16:40:54 +02:00
|
|
|
auto write_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index, whole_blocks, inbuf, whole_blocks * block_size()));
|
2020-11-02 11:16:01 -07:00
|
|
|
auto result = write_request->wait();
|
|
|
|
if (result.wait_result().was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2020-11-02 11:16:01 -07:00
|
|
|
switch (result.request_result()) {
|
|
|
|
case AsyncDeviceRequest::Failure:
|
|
|
|
case AsyncDeviceRequest::Cancelled:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::MemoryFault:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-11-02 11:16:01 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2020-02-09 21:43:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
off_t pos = whole_blocks * block_size();
|
|
|
|
|
|
|
|
// since we can only write in block_size() increments, if we want to do a
|
|
|
|
// partial write, we have to read the block's content first, modify it,
|
|
|
|
// then write the whole block back to the disk.
|
|
|
|
if (remaining > 0) {
|
2021-09-06 03:29:52 +04:30
|
|
|
// FIXME: Do something sensible with this OOM scenario.
|
|
|
|
auto data = ByteBuffer::create_zeroed(block_size()).release_value();
|
2020-09-11 21:11:07 -06:00
|
|
|
auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data.data());
|
2020-11-02 11:16:01 -07:00
|
|
|
|
|
|
|
{
|
2021-09-07 16:40:54 +02:00
|
|
|
auto read_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Read, index + whole_blocks, 1, data_buffer, block_size()));
|
2020-11-02 11:16:01 -07:00
|
|
|
auto result = read_request->wait();
|
|
|
|
if (result.wait_result().was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2020-11-02 11:16:01 -07:00
|
|
|
switch (result.request_result()) {
|
|
|
|
case AsyncDeviceRequest::Failure:
|
|
|
|
return pos;
|
|
|
|
case AsyncDeviceRequest::Cancelled:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::MemoryFault:
|
|
|
|
// This should never happen, we're writing to a kernel buffer!
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-02 11:16:01 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 12:09:52 +02:00
|
|
|
TRY(inbuf.read(data.data(), pos, remaining));
|
2020-11-02 11:16:01 -07:00
|
|
|
|
|
|
|
{
|
2021-09-07 16:40:54 +02:00
|
|
|
auto write_request = TRY(try_make_request<AsyncBlockDeviceRequest>(AsyncBlockDeviceRequest::Write, index + whole_blocks, 1, data_buffer, block_size()));
|
2020-11-02 11:16:01 -07:00
|
|
|
auto result = write_request->wait();
|
|
|
|
if (result.wait_result().was_interrupted())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINTR;
|
2020-11-02 11:16:01 -07:00
|
|
|
switch (result.request_result()) {
|
|
|
|
case AsyncDeviceRequest::Failure:
|
|
|
|
return pos;
|
|
|
|
case AsyncDeviceRequest::Cancelled:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EIO;
|
2020-11-02 11:16:01 -07:00
|
|
|
case AsyncDeviceRequest::MemoryFault:
|
|
|
|
// This should never happen, we're writing to a kernel buffer!
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-11-02 11:16:01 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-02-09 21:43:22 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
return pos + remaining;
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:13:03 +03:00
|
|
|
StringView StorageDevice::early_storage_name() const
|
2021-10-02 16:22:16 -07:00
|
|
|
{
|
2021-08-27 17:13:03 +03:00
|
|
|
return m_early_storage_device_name->view();
|
2021-10-02 16:22:16 -07:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
bool StorageDevice::can_write(const OpenFileDescription&, size_t offset) const
|
2019-07-28 23:44:01 +10:00
|
|
|
{
|
2020-12-19 12:50:57 +02:00
|
|
|
return offset < (max_addressable_block() * block_size());
|
2019-07-28 23:44:01 +10:00
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> StorageDevice::ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg)
|
2021-10-08 22:20:26 +02:00
|
|
|
{
|
|
|
|
switch (request) {
|
|
|
|
case STORAGE_DEVICE_GET_SIZE: {
|
|
|
|
size_t disk_size = m_max_addressable_block * block_size();
|
|
|
|
return copy_to_user(Userspace<size_t*>(arg), &disk_size);
|
|
|
|
break;
|
|
|
|
}
|
2021-10-09 11:19:51 +02:00
|
|
|
case STORAGE_DEVICE_GET_BLOCK_SIZE: {
|
|
|
|
size_t size = block_size();
|
|
|
|
return copy_to_user(Userspace<size_t*>(arg), &size);
|
|
|
|
break;
|
|
|
|
}
|
2021-10-08 22:20:26 +02:00
|
|
|
default:
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|