2021-01-20 00:33:00 +03:00
|
|
|
/*
|
2021-04-28 23:46:44 +03:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-01-20 00:33:00 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-20 00:33:00 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Memory.h>
|
|
|
|
#include <AK/StringView.h>
|
2021-09-11 09:19:20 +03:00
|
|
|
#include <Kernel/Devices/DeviceManagement.h>
|
2021-09-07 14:39:11 +03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2021-01-20 00:33:00 +03:00
|
|
|
#include <Kernel/Storage/RamdiskController.h>
|
|
|
|
#include <Kernel/Storage/RamdiskDevice.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-08-06 14:49:36 +03:00
|
|
|
NonnullRefPtr<RamdiskDevice> RamdiskDevice::create(const RamdiskController& controller, NonnullOwnPtr<Memory::Region>&& region, int major, int minor)
|
2021-01-20 00:33:00 +03:00
|
|
|
{
|
2021-10-03 02:22:16 +03:00
|
|
|
// FIXME: Try to not hardcode a maximum of 16 partitions per drive!
|
|
|
|
size_t drive_index = minor / 16;
|
|
|
|
// FIXME: We need a way of formatting strings with KString!
|
|
|
|
auto device_name = String::formatted("ramdisk{}", drive_index);
|
|
|
|
auto device_name_kstring = KString::must_create(device_name.view());
|
|
|
|
|
|
|
|
auto device_or_error = DeviceManagement::try_create_device<RamdiskDevice>(controller, move(region), major, minor, move(device_name_kstring));
|
2021-09-10 14:44:46 +03:00
|
|
|
// FIXME: Find a way to propagate errors
|
|
|
|
VERIFY(!device_or_error.is_error());
|
|
|
|
return device_or_error.release_value();
|
2021-01-20 00:33:00 +03:00
|
|
|
}
|
|
|
|
|
2021-08-27 17:13:03 +03:00
|
|
|
RamdiskDevice::RamdiskDevice(const RamdiskController&, NonnullOwnPtr<Memory::Region>&& region, int major, int minor, NonnullOwnPtr<KString> device_name)
|
|
|
|
: StorageDevice(major, minor, 512, region->size() / 512, move(device_name))
|
2021-01-20 00:33:00 +03:00
|
|
|
, m_region(move(region))
|
|
|
|
{
|
2021-03-16 22:23:16 +03:00
|
|
|
dmesgln("Ramdisk: Device #{} @ {}, Capacity={}", minor, m_region->vaddr(), max_addressable_block() * 512);
|
2021-01-20 00:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RamdiskDevice::~RamdiskDevice()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-11 02:46:09 +03:00
|
|
|
StringView RamdiskDevice::class_name() const
|
2021-01-20 00:33:00 +03:00
|
|
|
{
|
2021-10-03 01:24:00 +03:00
|
|
|
return "RamdiskDevice"sv;
|
2021-01-20 00:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RamdiskDevice::start_request(AsyncBlockDeviceRequest& request)
|
|
|
|
{
|
2021-07-18 02:13:34 +03:00
|
|
|
MutexLocker locker(m_lock);
|
2021-01-20 00:33:00 +03:00
|
|
|
|
|
|
|
u8* base = m_region->vaddr().as_ptr();
|
|
|
|
size_t size = m_region->size();
|
|
|
|
u8* offset = base + request.block_index() * 512;
|
|
|
|
size_t length = request.block_count() * 512;
|
|
|
|
|
|
|
|
if ((offset + length > base + size) || (offset + length < base)) {
|
|
|
|
request.complete(AsyncDeviceRequest::Failure);
|
|
|
|
} else {
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> result;
|
2021-01-20 00:33:00 +03:00
|
|
|
if (request.request_type() == AsyncBlockDeviceRequest::Read) {
|
2021-09-07 13:09:52 +03:00
|
|
|
result = request.buffer().write(offset, length);
|
2021-01-20 00:33:00 +03:00
|
|
|
} else {
|
2021-09-07 13:09:52 +03:00
|
|
|
result = request.buffer().read(offset, length);
|
2021-01-20 00:33:00 +03:00
|
|
|
}
|
2021-11-08 02:51:39 +03:00
|
|
|
request.complete(!result.is_error() ? AsyncDeviceRequest::Success : AsyncDeviceRequest::MemoryFault);
|
2021-01-20 00:33:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|